This commit is contained in:
2026-01-13 01:16:05 +08:00
commit bf88cfab5c
9 changed files with 670 additions and 0 deletions

58
plugin/README.md Normal file
View File

@@ -0,0 +1,58 @@
# OpenCode TypeScript Plugin 测试说明
## 概述
本示例演示 OpenCode Plugin 可以使用 TypeScript 编写,并且可以直接使用 Bun 运行。
## 项目结构
```
plugin/
├── package.json # 项目配置
├── build.ts # Bun 构建脚本
├── src/
│ └── index.ts # TypeScript Plugin 源码
└── dist/ # 构建输出目录(自动生成)
```
## 安装依赖
```bash
cd plugin
bun install
```
## 直接运行(使用 Bun TypeScript 支持)
```bash
bun run src/index.ts
```
## 构建
```bash
bun run build
```
## Plugin 配置
在项目的 `opencode.json` 中添加 plugin 引用:
```json
{
"plugin": [
"./plugin/src/index.ts"
]
}
```
OpenCode 会自动识别 TypeScript 文件并使用 Bun 运行,无需预先编译。
## 功能说明
这个示例 plugin 定义了一个 `greet` 工具,用于打招呼。
TypeScript 优势:
- 类型安全
- 智能提示
- 编译时错误检查
- 更好的代码维护性
## 核心特性
1. **原生 TypeScript 支持**OpenCode 可以直接加载和运行 `.ts` 文件
2. **Bun 运行时**:使用 Bun 的内置 TypeScript 编译器,无需配置 tsc
3. **热重载**:修改源码后自动重新加载
4. **零配置**:只需在 `opencode.json` 中指定 `.ts` 文件路径

4
plugin/build.ts Normal file
View File

@@ -0,0 +1,4 @@
#!/usr/bin/env bun
import { $ } from "bun";
await $`bun build src/index.ts --outdir dist --target bun`;

9
plugin/package.json Normal file
View File

@@ -0,0 +1,9 @@
{
"name": "opencode-typescript-plugin-example",
"version": "0.0.1",
"type": "module",
"scripts": {
"dev": "bun run src/index.ts",
"build": "bun run build.ts"
}
}

22
plugin/src/index.ts Normal file
View File

@@ -0,0 +1,22 @@
import { tool } from "@opencode-ai/plugin/tool";
import type { Plugin, PluginInput } from "@opencode-ai/plugin";
const MyPlugin: Plugin = async (ctx: PluginInput) => {
console.log("Plugin loaded!", ctx.project.name);
return {
tool: {
greet: tool({
description: "Greet someone",
args: {
name: tool.schema.string().describe("The name to greet"),
},
async execute(args) {
return `Hello, ${args.name}!`;
},
}),
},
};
};
export default MyPlugin;