跳转到内容

插件作者指南

插件作者面对的是一条窄而明确的公共契约:@twinklerg/coden/plugin。工具名称、风险、JSON Schema、返回值和取消协作必须准确;包入口必须是 ESM 构建产物。公共类型帮助兼容,但不会把插件放进隔离运行时。

{
"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。
类型 触发信任
项目本地插件(.coden/plugins/*.ts、--plugin 目录) 首次加载时人工确认工作区
项目 npm 插件 plugin install/plugin sync 确认后记录;启动时未信任则跳过并提示
用户级本地插件 无确认(当前用户主动安装)
全局 npm 插件 无启动确认(用户主动安装)

确认只接受 y(--yes 只影响插件命令,不影响工作区信任)。信任记录保存工作区真实路径(trusted-workspaces.json,0600),符号链接别名不会绕过。

  • 插件不是沙箱:在 CodeN 主进程内执行,拥有完整用户进程权限,可访问文件、网络、环境变量与子进程。
  • 工具 risk 只影响调用确认,不提供进程隔离。
  • 安装校验导入插件入口:即使禁用生命周期脚本,顶层代码与静态依赖也会以当前用户权限运行。
  • 本地 .ts 插件通过 data: URL 加载,无法被 Node 运行时加载(见 运行时能力矩阵)。
选项 行为
默认 --ignore-scripts,生命周期脚本不运行
--allow-scripts 当前包与传递依赖以用户权限运行 preinstall/install/postinstall;先警告并再次确认
  • npm 入口解析后必须位于包目录内,且是安全解析的真实路径(withinBoundary + safeRealpath)。
  • 本地插件目录的信任基于真实路径;目录/文件需通过真实性校验。
  • 工具 schema 编译失败、工具重名、导出非法都会跳过该插件,不影响其他插件与内置工具。
  1. 只安装可信来源的插件;审查发布内容(npm pack --dry-run、锁文件 diff)。
  2. 默认保持生命周期脚本禁用;确有需要时再显式 --allow-scripts 并检查依赖树。
  3. 项目插件清单与 bun.lock 提交版本控制,node_modules 不提交。
  4. 敏感仓库先确认工作区内容再信任项目插件/Hook。
  5. 不信任的插件不要保留在全局作用域(全局插件不重复确认)。