插件开发

快速开始

.agent/plugins/ 目录下创建插件文件:

// .agent/plugins/my-plugin.js
module.exports = function(Plugin) {
  return class MyPlugin extends Plugin {
    constructor(config = {}) {
      super()
      this.name = 'my-plugin'
      this.version = '1.0.0'
      this.description = '我的工具插件'
      this.priority = 10
    }

    install(framework) {
      const { z } = require('zod')
      framework.registerTool({
        name: 'my_tool',
        description: '我的工具',
        inputSchema: z.object({
          param: z.string().describe('参数描述')
        }),
        execute: async (args, framework) => {
          return { success: true, result: args.param }
        }
      })
      return this
    }
  }
}

插件结构

必须属性

生命周期

注册工具

framework.registerTool({
  name: 'tool_name',
  description: '工具描述',
  inputSchema: z.object({
    param: z.string().describe('参数描述')
  }),
  execute: async (args, framework) => {
    return { success: true, result: '...' }
  }
})
提示: 如果插件需要第三方库(如 zod),需要先调用 install 工具安装依赖。

第三方依赖

如果插件需要使用第三方 npm 包:

  1. 创建插件文件(暂不加载)
  2. 调用 install { package: "包名" } 安装依赖
  3. 调用 reload_plugins 重载插件

Python 插件

除了 JavaScript 插件,框架还支持 Python 插件。创建 .agent/plugins/*.py 文件:

# .agent/plugins/my-python-plugin.py

plugin_info = {
    "name": "my-python-plugin",
    "version": "1.0.0",
    "description": "我的 Python 插件"
}

def execute_tool(tool_name, params):
    """执行工具"""
    if tool_name == "hello":
        return {"success": True, "result": f"Hello, {params.get('name', 'World')}!"}
    return {"success": False, "error": "Unknown tool"}

使用 Python 插件工具:

python_plugin {
   plugin: "my-python-plugin",
   tool: "hello",
   params: { name: "Foliko" }
}
提示: Python 插件会自动加载并注册到框架中。