跳转到内容

工具插件执行模型

工具插件是 CodeN 的标志性扩展点。加载后,工具与内置工具一起进入 Registry;Registry 暴露名称、描述和输入 Schema;模型产生调用;执行器运行 Hook、校验与权限流程;插件执行结果再作为 tool message 回到 Agent 循环。risk 只参与审批策略,不限制插件进程权限。

  1. 启动时,本地与 npm 插件经过导出校验后尝试注册;非法或重名工具不会替换已有工具。
  2. 每个 model step 都把当前 Registry 中的名称、描述和 inputSchema 发送给 Provider。
  3. 模型返回结构化 tool call。CodeN 先运行 PreToolUse;若 Hook 更新输入,会重新执行 Schema、路径、风险与权限检查。
  4. 权限通过后,执行器把输入和含 AbortSignal 的 ToolContext 交给工具。超时或取消只保证发出信号,主进程插件必须协作停止。
  5. 成功或错误结果被截断到运行时上限并写成 tool message;Agent 使用该结果发起下一次模型请求,而不是由 CodeN 代替模型解释结果。
  6. PostToolUse 或 PostToolUseFailure 观察结果;整个用户 turn 仍受 maxSteps 与 Stop Hook 控制。

这条路径说明插件为何既强大又敏感:它同时进入模型的决策面和本机的执行面。

工具 风险 输入要点 关键行为
read read path、offset(默认 1)、limit(默认 500,上限 2000) 按 1 起始行偏移读 UTF-8 文本;输出上限 50000 字符,超出提示省略;O_NOFOLLOW 打开
write modify path、content 创建或完整覆盖;文件模式 0600;自动创建父目录
edit modify path、oldText、newText 精确替换唯一匹配块;不唯一返回 edit.multiple_matches,未找到返回 edit.no_match
bash modify(命令可升级为 dangerous) command、timeout(默认 30000,范围 100–60000)、maxOutput(默认 30000,范围 1000–100000) bash -lc 以工作区为 cwd;非零退出码、超时或取消都标记错误;超时终止进程组
activate_skill read name 按名称加载 Skill 的完整说明与根目录;无效名称返回 skill.not_found;仅读取已发现的入口文件并校验内容未变

工具按模型返回的 tool_calls 顺序执行;单次调用超时 60 秒(超时需要工具配合取消)。所有工具输出在回填上下文前截断到 50000 字符。

等级 默认交互模式 --smart-approve --auto
read 自动允许 自动允许 自动允许
modify 人工确认(y/s/N) LLM 审查,不确定转人工 自动允许
dangerous 人工确认(仅 y/N,无会话授权) 人工确认 自动允许(Hook 的 deny 仍生效)

以下命令片段会导致 bash 风险升级为 dangerous:

rm -r / rm --recursive
sudo
git reset --hard
git clean -f
git push --force / --force-with-lease
git checkout -- <path>
git restore
mkfs / fdisk / dd
killall / pkill
curl|sh or wget|sh
path contains /etc /usr /bin /sbin

风险分类是防误操作的启发式护栏,不是安全沙箱:bash 与插件拥有当前用户进程权限。

  • 本地或 npm 插件注册的工具使用各自声明的 risk 字段,参与相同的授权流程。
  • 工具名须匹配 ^[a-zA-Z][a-zA-Z0-9_-]{0,63}$;重名(含与内置工具冲突)导致该插件整体跳过。
  • 插件工具执行受同样的 60 秒超时与 AbortSignal 协作取消约束。
{
"name": "@scope/coden-plugin-example",
"version": "1.0.0",
"type": "module",
"files": ["dist"],
"coden": {
"apiVersion": 1,
"plugin": "./dist/index.js"
}
}
字段 要求
type 必须是 "module"
coden.apiVersion 必须是 1
coden.plugin 以 ./ 开头的 .js 或 .mjs 路径;不得含 ..、\、:// 或空白
入口文件 必须发布构建后的 JS;只发布 TypeScript 源码会被拒绝
入口边界 解析后的真实路径必须位于包目录内

单工具:默认导出 ToolDefinition:

import type { ToolDefinition } from "@twinklerg/coden/plugin";
const tool: ToolDefinition = {
name: "example_echo",
description: "Echo supplied text",
risk: "read",
inputSchema: {
type: "object",
additionalProperties: false,
required: ["text"],
properties: { text: { type: "string" } },
},
async execute(input) {
const { text } = input as { text: string };
return { content: text };
},
};
export default tool;

多工具:默认导出 CodeNPlugin,其中 name 必须等于 npm 包名:

import type { CodeNPlugin, ToolDefinition } from "@twinklerg/coden/plugin";
const readTool: ToolDefinition = {
name: "example_read",
description: "Read example data",
risk: "read",
inputSchema: { type: "object", additionalProperties: false },
async execute() {
return { content: "read" };
},
};
const plugin: CodeNPlugin = {
apiVersion: 1,
name: "@scope/coden-plugin-example",
tools: [readTool],
};
export default plugin;
检查 失败时的错误
导出不是工具或插件对象 plugin.export_invalid
apiVersion 不是 1 plugin.api_unsupported
CodeNPlugin.name 与包名不一致 plugin.name_mismatch
tools 为空或含非法工具 plugin.export_invalid
工具名不匹配 ^[a-zA-Z][a-zA-Z0-9_-]{0,63}$ tool.invalid_name
与已有工具重名 tool.duplicate(本地加载)/ plugin.tool_conflict(npm 组合)
inputSchema 非法 tool.invalid_schema
  • @twinklerg/coden/plugin 是唯一公开子路径;主入口 "." 未公开,import "@twinklerg/coden" 会被拒绝。
  • ToolDefinition/CodeNPlugin 类型从该子路径导入;把 @twinklerg/coden 放入 devDependencies,import type 在构建时移除,运行时不会加载另一份 CodeN。
  • ToolContext.signal 是 AbortSignal:tsconfig 需提供相应 lib(如 @types/node 或 DOM)。
  • 契约以仓库 src/plugin/index.ts 为唯一来源;构建自动生成 dist/plugin/index.js 与 dist/plugin/index.d.ts,不发布 src。