插件开发
状态:2026-11-16。如何构建、测试和发布 Orquelo 插件。线上协议见插件协议;hook 的细节见 Hook 参考。返回指南目录。
状态:2026-11-16。如何构建、测试和发布 Orquelo 插件。线上协议见插件协议;hook 的细节见 Hook 参考。返回指南目录。
1. 插件是什么
插件是一个带 plugin.yaml 的目录。实例是它在 Host 配置 plugins: 下的一次具体使用,拥有自己的 id、配置、选项、数据目录和 scope。插件只在清单声明的角色中起作用,并且只服务于在 uses 中列出其实例的 runtime。身份与权限始终来自 Host(CallContext),从不来自插件或模型。
2. 快速入门 A:不写代码
一个 manifest 插件:提供某个 MCP 服务器的工具、一个技能、一个拦截危险 shell 命令的 hook,以及一个为每次请求添加上下文的 hook。
release-helper/
plugin.yaml
skills/release-notes/SKILL.md
hooks/no-force-push.py
hooks/add-branch.pyplugin.yaml:
api: v1
name: release-helper
version: 0.1.0
description: Release tools from an MCP server, a release-notes skill, and two hooks.
host: ">=0.1.0 <1.0.0"
execution: manifest
roles: [tool, skill, guard] # tool:mcp_servers;skill:skills;guard:pre_tool 和 user_input hook
mcp_servers:
- name: gh
command: [npx, -y, "@modelcontextprotocol/server-github"]
env: {GITHUB_TOKEN: "${option.token}"}
approval: ["create_*", "merge_*"] # 这些工具需要人审批
options:
token: {type: string, title: GitHub token, required: true, sensitive: true}
skills:
- skills/release-notes
hooks:
- {event: pre_tool, match: "bash|powershell", command: [python3, "${PLUGIN_ROOT}/hooks/no-force-push.py"], timeout: 5s}
- {event: user_input, command: [python3, "${PLUGIN_ROOT}/hooks/add-branch.py"], on_error: ignore}skills/release-notes/SKILL.md(面向模型的文本使用英文):
---
name: release-notes
description: Write release notes from the merged pull requests since the last tag.
---
List the pull requests merged since the last tag with the gh tools, group them
by label, and write one line per change in the imperative mood.hooks/no-force-push.py 以退出码 2 拦截;stderr 是模型看到的原因:
#!/usr/bin/env python3
import json, sys
event = json.load(sys.stdin)
if "push --force" in json.dumps(event.get("tool_input", {})):
print("force pushes are not allowed; push a new commit instead", file=sys.stderr)
sys.exit(2)hooks/add-branch.py 添加上下文,模型会把它作为该次 run 的参考资料看到:
#!/usr/bin/env python3
import json, sys
json.load(sys.stdin)
print(json.dumps({"hookSpecificOutput": {"hookEventName": "UserPromptSubmit",
"additionalContext": "Releases are cut from the main branch."}}))如果 Host 配置设置了 policy.mcp_commands,hook 的可执行文件(这里是 python3)也必须列在其中。只有插件同时声明了 context 角色(roles: [tool, skill, guard, context])时,user_input 的上下文才会到达模型;否则这段文本会被暂存但永远不会被读取。每个事件的细节见 Hook 参考。
加载插件。 在 Host 配置中:
plugin_dirs: [/path/to/plugins] # release-helper/ 的上级目录
plugins:
- {id: release, plugin: release-helper}
- {id: agent, plugin: agent, uses: [kimi, files, release], config: {model: kimi}}然后:
| 步骤 | 命令 |
|---|---|
| 保存敏感选项(值从 stdin 读取) | orquelo secret set -c CONFIG release token |
| 查看每个实例及其每个 MCP 服务器和技能的状态 | orquelo plugins -c CONFIG |
| 把配置应用到运行中的 Host | orquelo reload -c CONFIG |
| 首次启动之前,以及修改插件文件之后 | orquelo plugin approve -c CONFIG release-helper(在此之前插件以 not_approved 不可用,文件变化后则以 changed_since_approved 不可用) |
orquelo plugin add -c CONFIG [-uses a,b] [-option KEY=VALUE]... ID PLUGIN 可以不编辑文件直接添加实例。
3. 快速入门 B:任意语言
process 插件是一个在 stdin 和 stdout 上使用 JSON-RPC 2.0 的可执行文件。plugins/notes-python 是一个只用标准库的完整 Python 示例:两个工具、一个 guard、上下文和一个技能。它通过与 Go 插件相同的一致性测试集(internal/process 中的 TestPythonPlugin)。
plugin.yaml:
api: v1
name: notes-python
version: 0.1.0
description: Example process plugin in Python (standard library only) with tools, a guard, context and a skill.
execution: process
roles: [tool, guard, context, skill]
platforms: [darwin, linux]
process:
command: # os/arch → 可执行文件,相对插件目录
darwin/arm64: main.py
darwin/amd64: main.py
linux/amd64: main.py
linux/arm64: main.py
config_schema:
type: object
properties:
blocked_words: {type: array, items: {type: string}}main.py 依次包括:
| 部分 | 作用 |
|---|---|
read() / write() | 一条消息 = Content-Length: N\r\n\r\n + N 字节 JSON。写入时持锁,因为应答和回调共用 stdout |
initialize | 保存 data_dir 和 config;返回 {"manifest": {"name", "version", "roles"}}。这三项与 plugin.yaml 不一致时 Host 拒绝该进程 |
HANDLERS | 方法名 → 函数:tool/list、tool/invoke、guard/check、context/get、skill/list、skill/load、shutdown。未知方法应答 -32601 |
call_host() | 回调:插件向 Host 发出的请求,这里是携带 tool/invoke 所给 call_id 的 host/remind,只允许在该调用期间使用 |
main() | 读取消息。应答唤醒等待中的 call_host;通知($/cancel)被忽略;每个请求在独立线程上运行,这样工具调用可以等待回调应答,而循环继续读取 |
| 错误 | 抛出的异常变成 {"code": -32000, "message", "data": {"code": "notes.failed", "message"}} |
进程以插件目录为工作目录,环境变量是最小集合(PATH、HOME、USER、LANG、LC_ALL、TMPDIR、TZ;Windows 上另加系统变量)。stdout 只写协议帧;日志写到 stderr,Host 会把它记入日志(脱敏,每个进程最多 8 MiB)。
Windows。 process.command 指定的可执行文件不经 shell 启动。脚本需要启动器(例如系统可以直接运行的 .exe 或 .cmd);notes-python 因此声明了 platforms: [darwin, linux]。
4. Go SDK
pkg/plugin/serve 把 Go 插件作为进程运行:serve.Main(manifestYAML, New),其中 New 是 plugin.Factory(func(plugin.Env) (plugin.Plugin, error))。插件实现 pkg/plugin 中的角色接口(Tool、Guard、ContextProvider 等);SDK 负责协议,并把回调转成 Caller/ToolHost/RuntimeHost 的方法。同一份代码既可以作为 builtin 编译进 Host(仅限第一方),也可以作为 process 插件发布;internal/process 的测试以两种方式运行 localenv 并得到相同结果。
5. plugin.yaml 参考
未知字段会被拒绝,因此拼写错误不会悄悄丢掉声明。
| 字段 | 类型 | 必填 | 含义与规则 |
|---|---|---|---|
api | string | 是 | 契约版本;v1 |
name | string | 是 | ^[a-z0-9][a-z0-9-]{0,62}$;稳定 id |
version | string | 是 | 插件版本;必须与进程在 initialize 中报告的一致 |
description | string | 建议 | 英文;展示给运维人员和模型 |
host | string | 否 | 插件适用的 Host 版本范围,如 ">=0.1.0 <1.0.0" |
execution | string | 否 | manifest、process 或 builtin;省略时为 manifest |
roles | list | 是 | 第 6 节中的至少一个角色;进程必须报告相同的列表 |
publisher | string | 否 | 展示给运维人员的声明;Host 记录真实来源 |
default | string | 否 | available(默认)或 enabled(属于 orquelo init 的起始配置) |
platforms | list | 否 | os 或 os/arch,如 darwin、linux/amd64;其他平台上实例不可用(platform_unsupported) |
works_with | object | 否 | transports: [feishu]:只用于这些渠道 |
resumable | bool | 否 | 仅 runtime:Host 重启后从检查点继续 |
requires | list | 否 | 所用实例中必须提供的角色(requirement_missing) |
depends | list | 否 | 必须已安装的其他插件:name 或 name@range |
prerequisites | list | 否 | Orquelo 之外的软件,带检查和安装步骤(依赖) |
shell | map | 否 | path:${PLUGIN_ROOT} 或 ${PLUGIN_DEPS} 下、排在 shell PATH 最前面的目录;env:给 shell 的变量(同一页,ADR 0001 决定 9) |
permissions | object | 否 | 申请项,安装时展示;从不构成授权 |
options | map | 否 | 类型化设置(第 7 节);键 ^[A-Za-z_][A-Za-z0-9_]{0,63}$;type 为 string、number、boolean、file、directory 之一,并需要 title |
config_schema | JSON Schema | 否 | 加载时校验实例 config(config_invalid) |
display、locales | object | 否 | 运维界面的消息 key 和语言文件 |
inputs | list | 否 | 仅模型:text、image |
process | object | process | command(os/arch → 可执行文件)、args |
mcp_servers | list | manifest | name,command(stdio)与 url(Streamable HTTP)恰好其一,env、tools.include/exclude、approval、parallel;仅 URL 服务器可用的 headers(每次请求都发送,例如 X-API-Key: "${option.api_key}";实例 config.headers 中同名的头覆盖它)、auth: oauth 与 scopes。url、command、env 与 headers 中的每个 ${option.<key>} 都必须是已声明的选项,否则清单被拒绝 |
skills | list | manifest | 含 SKILL.md 的目录 |
hooks | list | manifest | event、match、command、timeout(默认 10s)、on_error(默认 deny,或 ignore);每个事件需要它所对应的角色(Hook 参考) |
manifest 插件至少需要 mcp_servers、skills、hooks 之一。MCP 命令、URL、env、header 和 hook 命令中的值可以使用 ${PLUGIN_ROOT}(插件目录)、${PLUGIN_DATA}(实例数据目录)、${PLUGIN_DEPS} 和 ${option.KEY}。敏感选项不能出现在 MCP 或 hook 的命令行中,因为本机任何进程都能读取命令行:出现时该服务或 hook 无法启动,并提示改为通过 env(或 header)传入(飞书 #255)。
6. 角色
角色是 pkg/plugin 中的接口及一组协议方法。权力是固定的:扩展只能做其角色允许的事。
| 角色 | 方法(协议) | 可以 | 失败时 | Host 超时 |
|---|---|---|---|---|
tool | tool/list、tool/invoke | 提供工具;每次调用经过账本和审批 | 返回给模型的错误结果 | 工具自身的(MCP 为 call_timeout,默认 2m) |
skill | skill/list、skill/load | 提供卡片和文本;不授予任何权限 | 该实例的技能被排除 | — |
guard | guard/check;可选 check_input、check_result、check_completion | 调用前拒绝或要求审批;拒绝输入;扣留结果或添加备注;要求继续工作 | 拒绝 / 拒收 / 扣留;完成检查被跳过 | check 5 s;其他 10 s(有 hook 时更长) |
guard(可选,Hook 模型) | guard/rewrite、guard/filter_tools、guard/before_model、guard/after_model、guard/check_step | 改写调用参数或模型调用(需要 grants: [rewrite],ADR 0005);从 run 中移除工具;拒绝模型调用;在一批工具调用之后添加上下文或停止 run | 改写和模型检查失败时拒绝调用;工具过滤和步骤检查失败时被跳过 | 改写 5 s;其他 10 s |
reviewer | reviewer/review | 批准、拒绝或升级一次审批;需要 grants: [review] | 升级给人 | 30 s |
context | context/get | 在 run 开始时添加参考条目;不能下指令,运维 policy.hooks 中的实例除外,其条目作为指令到达模型 | 省略 | 5 s |
observer | observer/observe;可选 observer/before_compact | 读取已提交事实(run.finished、session.*、tool.finished、question.*、compaction.done、delivery.*);指出总结必须保留的事实 | 从持久游标重试 | 每个事件 2 min;压缩前 30 s |
compactor | compactor/compact | 写出会话总结记录 | 下一个压缩器,最后是内置压缩器 | 120 s |
context_processor | processor/process | 返回对历史的类型化编辑(缩短、脱敏、隐藏、注释) | 丢弃该处理器的编辑 | 5 s |
service | service/serve | 通过 BackgroundHost 做自己的长期工作 | 退避重启,1 s 翻倍至 5 min | — |
model | model/chat(+ model/delta 流) | 生成响应 | run 失败 | run 超时 |
runtime | runtime/execute | 执行任务,可用 RuntimeHost 回调 | run 失败或重试 | run 超时 |
transport、trigger、presentation、delivery | transport/start、transport/fetch、trigger/match、presentation/present、delivery/deliver | 连接的各渠道阶段 | 按阶段(架构) | — |
所有角色共同遵守的规则见扩展点第 6 节:只收紧、每个 run 固定、确定的顺序(uses 顺序)、作用域决定是否出现、输入输出有界、副作用即工具调用、恢复复用记录。
7. 配置与密钥
config | options | |
|---|---|---|
| 声明位置 | config_schema(JSON Schema) | 带类型的 options: 条目 |
| 设置位置 | 实例的 config: | 实例的 options:、密钥存储或默认值 |
| 敏感值 | 不允许:请使用选项(属性名像凭据时,如 api_key、token、password,orquelo plugin test 失败) | sensitive: true,用 orquelo secret set -c CONFIG INSTANCE NAME(stdin)保存,或用 ${ENV} |
| 插件收到 | Env.Config / initialize.config | Env.Options / initialize.options;清单值中的 ${option.KEY} |
每个顶层 config_schema 属性都需要 description(缺少时 orquelo plugin test 失败):admin 插件的 config_describe 会把它展示给在对话中配置 Host 的模型,因此用英文写明该设置的作用、默认值与单位,以及任何成本或安全影响。
缺少必填选项时实例以 option_missing 不可用。密钥加密存储;插件不得记录或返回选项值。调用时的按用户密钥(用户自己的 token)使用 host/secret_get、host/secret_put、host/secret_delete:按调用主体隔离,其他实例或主体看不到。插件的持久状态放在实例数据目录(Env.DataDir、initialize.data_dir、${PLUGIN_DATA}),它在重启和升级后保留。
登录(ADR 0012)让 agent 能使用 CLI 和网站。登录结果存放在 CLI 或网站本来就会存放的位置,因此工具在调用中不会额外得到任何东西。从模型接收 secret: 引用(用户在登录中输入的内容)的工具用 host/resolve_secret(builtin 插件用 plugin.SecretResolver)解析;Host 会从该 run 此后的工具结果中删除这个值。网站经由 agent-browser CLI 访问,因此网站的验证码与任何 CLI 一样经 localenv 的 secret_env 输入(P87 S6)。登录技能是唯一标明 metadata.orquelo.login: true 的技能。
8. 测试
| 插件 | 方法 |
|---|---|
| Go | pkg/plugin/plugintest 的测试集:ManifestSuite、ToolSuite、GuardSuite、ContextSuite、SkillSuite、ModelSuite、TriggerSuite、PresentationSuite、DeliverySuite、RuntimeSuite、TransportSuite。它们接受 plugintest.T,*testing.T 满足该接口 |
| 任意语言 | orquelo plugin test [-config JSON] [-options JSON] DIR(-options:插件的选项值,形式同实例的 options):读取 DIR/plugin.yaml,检查清单,按 Host 的方式启动插件,并在真实协议上运行每个已声明角色的测试集。逐个打印 PASS <测试集> 或 FAIL <测试集>,每个问题缩进一行,有失败时以非零码退出。需要真实后端的角色(model、delivery、transport、service、observer)报告为 SKIP |
| 消息 | 用 schemas/protocol/v1(JSON Schema 2020-12,由 Go 代码生成)校验请求和应答 |
测试集能发现的问题举例:skill/load 返回了技能目录之外的文件、guard 对普通意图报错、工具列表中的参数 schema 无效、各语言文件的 key 不一致。
9. 发布与安装
orquelo plugin install -c CONFIG [-upgrade] [-yes] SOURCE 接受:
| 来源 | 形式 |
|---|---|
| 本地目录 | ./release-helper |
| Git,固定提交 | git+https://github.com/org/repo#<40 位十六进制提交>[:子目录];分支和标签会被拒绝 |
| 归档 | https://…/plugin.tgz#sha256=<64 位十六进制>(.tar.gz、.tgz、.zip,最大 200 MiB) |
| 目录(catalog) | 已配置 catalogs 中的插件名(orquelo plugin search) |
安装会先暂存插件,规划依赖和前置条件(-yes 或策略 allow_system_installs 允许安装系统包;插件自身的依赖在已配置实例加载时也会安装),以目录交换的方式提交,失败时回滚;-upgrade 替换已安装版本并保留备份。orquelo plugin doctor -c CONFIG NAME 重新检查前置条件。安装从不激活插件:只有配置中的实例才会运行。
Claude Code 与 Codex 插件包。 安装含 .claude-plugin/plugin.json、.codex-plugin/plugin.json 或根目录 plugin.json 的目录时,会在不运行其代码的情况下把它转换成 manifest 插件:
| 转换 | 排除并附说明 |
|---|---|
技能;命令(转为技能);MCP 服务器(stdio 与 HTTP),映射路径和选项变量;PreToolUse、PostToolUse、UserPromptSubmit、Stop、SubagentStop、SessionStart、PreCompact、PermissionRequest 的命令式 hook(on_error: ignore,与这些工具一致) | agents、output styles、LSP、workflows、themes、monitors、channels、apps、bin/、dependencies、旧版 SSE 服务器、非命令式 hook、需要 shell 的 hook、其他 hook 事件 |
10. 每个插件都遵守的规则
- ◆身份只来自 Host。 按用户的数据以
CallContext.principal.user为键;从不信任参数或模型输出中的 id。 - ◆只收紧。 guard 限制,context 添加,reviewer 只有在获得授权时才决定审批,observer 只读。任何扩展都不能放宽其他扩展的决定或运维
policy。 - ◆副作用即工具调用。 run 期间的副作用是一个工具,因此经过账本和审批。
- ◆按 key 幂等。 工具调用携带
key,重试时不变;崩溃后结果未知的调用会被报告,从不盲目重放。 - ◆不使用 shell。 命令和可执行文件都不经 shell 启动;需要 shell 的命令在导入时被拒绝。
- ◆stdout 只用于协议。 进程只向 stdout 写帧;stderr 被记录并脱敏。
- ◆有界。 每次调用都有 Host 超时;返回简短文本;文件放在数据目录。