diff --git a/backend/plugins/README.md b/backend/plugins/README.md index 8bd3053..a5d9cb7 100644 --- a/backend/plugins/README.md +++ b/backend/plugins/README.md @@ -1,54 +1,62 @@ # 第三方插件目录 -在此目录下创建子文件夹,每个文件夹一个插件。 +将插件文件夹放入此目录,运行代码生成器即可自动注册——无需手动编辑 `plugins.json`。 ## 目录结构 ``` plugins/ my-hello/ - plugin.json # 插件声明 - main.go # 插件入口 + go.mod # Go 模块声明(module xxx) + plugin.json # 插件声明(name, struct) + main.go # 插件入口 ... ``` -## 注册插件(3 步) +## 注册(2 步) -Go 不支持运行时动态加载,需要通过代码生成注册: +**1. 放入插件**:将插件目录复制到 `backend/plugins/` 下。 -**1. 编辑 `ai-core/plugins.json`**,添加插件条目: - -```json -{ "name": "my-hello", "import": "你的模块路径", "struct": "HelloPlugin" } -``` - -**2. 运行代码生成器**: +**2. 运行生成器**: ```bash -cd backend/ai-core && go run cmd/gen_plugins.go +cd backend/ai-core/cmd && go run gen_plugins.go ``` -这会生成 `cmd/plugins_gen.go`,自动包含所有插件的 import 和注册代码。 +生成器会自动: +- 扫描 `plugins/` 下所有子目录 +- 读取每个目录的 `plugin.json` 和 `go.mod` +- 生成 `plugins_gen.go`(含 import + 注册代码) +- 自动向 `go.mod` 添加 `require` + `replace` 指令 -**3. 重新编译 ai-core**: +**3. 重新编译**: ```bash cd backend/ai-core && go build -o main.exe ./cmd/ ``` -## plugin.json 格式 +或使用 `go generate`: -每个插件目录下的声明文件: +```bash +cd backend/ai-core && go generate ./cmd/... +``` + +## plugin.json 格式 ```json { "name": "my-hello", - "version": "1.0.0", - "author": "your-name", - "description": "我的第一个插件" + "struct": "HelloPlugin" } ``` +| 字段 | 必填 | 说明 | +|------|------|------| +| name | ✅ | 插件唯一名称 | +| struct | ✅ | 插件结构体名(实现 Plugin 接口的那个类型) | + +`import` 路径自动从 `go.mod` 的 `module` 声明读取,无需手写。 + ## 插件开发 详见 [docs/api/plugin-development.md](../../docs/api/plugin-development.md) diff --git a/docs/api/plugin-development.md b/docs/api/plugin-development.md index 7e3a390..97a53ff 100644 --- a/docs/api/plugin-development.md +++ b/docs/api/plugin-development.md @@ -147,20 +147,20 @@ func (t *HelloTool) Complexity() sdk.ToolComplexity { return sdk.ComplexitySimpl ## 注册 -1. 将插件代码放到 `backend/plugins/<插件名>/` -2. 在 `backend/ai-core/plugins.json` 中添加条目: - -```json -{ "name": "my-hello", "import": "git.yeij.top/AskaEth/Cyrene-Plugins/my-hello", "struct": "HelloPlugin" } -``` - -3. 运行代码生成器: +1. 将插件目录复制到 `backend/plugins/`(需包含 `go.mod` + `plugin.json`) +2. 运行代码生成器(自动扫描、生成注册代码、补 go.mod): ```bash cd backend/ai-core/cmd && go run gen_plugins.go ``` -4. 重新编译 ai-core。**不需要修改 main.go。** +3. 重新编译 ai-core: + +```bash +cd backend/ai-core && go build -o main.exe ./cmd/ +``` + +**不需编辑 `plugins.json`,不需修改 `main.go`。** 生成器自动从 `go.mod` 读取模块路径。 ---