插件协议
状态:2026-11-16,api v1。Host 与 process 插件之间的线上协议,用于以任意语言编写插件。方法表是 pkg/plugin/rpc/spec.go;schemas/protocol/v1 中的 JSON Schema 由它生成。返回指南目录;构建与发布插件见插件开发。
状态:2026-11-16,api v1。Host 与 process 插件之间的线上协议,用于以任意语言编写插件。方法表是 pkg/plugin/rpc/spec.go;schemas/protocol/v1 中的 JSON Schema 由它生成。返回指南目录;构建与发布插件见插件开发。
1. 传输
| 项目 | 规则 |
|---|---|
| 通道 | 进程的 stdin(Host → 插件)和 stdout(插件 → Host) |
| 消息 | JSON-RPC 2.0,UTF-8 JSON |
| 分帧 | Content-Length: N\r\n\r\n 后跟 N 字节,与 LSP 相同;头名不区分大小写 |
| 大小 | 每条消息最多 32 MiB(MaxFrame),每个头部行最多 64 KiB;更大的帧或更长的行会结束连接。一端同时最多处理 256 个请求,超出的请求得到错误 -32000 rpc: too many requests at once(飞书 #256) |
| stderr | 逐行记入 Host 日志,脱敏,每个进程最多 8 MiB |
| 启动 | 按 Host 的 os/arch 取 process.command,相对插件目录,不经 shell 启动 |
| 工作目录 | 插件目录 |
| 环境变量 | 仅 PATH、HOME、USER、LANG、LC_ALL、TMPDIR、TZ(Windows 上另有 SystemRoot、ComSpec、PATHEXT、TEMP、TMP、USERPROFILE、LOCALAPPDATA);配置通过 initialize 传入 |
双方在同一连接上发送请求。各方为自己的请求编号;应答携带它所回应请求的 id。插件处理请求时必须继续读取,因为 Host 可能在此期间应答回调或发送 $/cancel。
2. 生命周期
Host 启动进程 ──► initialize ──► 角色方法 …(双向回调)──► shutdown ──► 关闭 stdin
▲ │ 崩溃
└──── 2^n 秒后重启(最多 1 分钟)◄────┘- 启动。 实例加载时 Host 启动进程。
initialize(限时 15 s)。Host 发送实例的 id、配置、数据目录和选项值。插件应答它的清单以及它实现的可选方法。name、version或roles与plugin.yaml不一致时 Host 拒绝该进程;用 Go SDK 构建的插件会拒绝它不支持的api(错误rpc.api)。- 角色方法和回调并发进行。
shutdown(2 s),随后关闭 stdin 并停止进程。- 崩溃。 进行中的调用失败。Host 在下次需要时重启进程;连续启动失败后依次等待 2、4、8 … 秒,最多一分钟,期间应答
plugin.restarting。结果未知的工具调用在账本中记为未知并报告给模型,从不盲目重放。长工具(long: true)会以同一个key再次调用,必须按该 key 幂等。
3. 调用、回调与 call_id
可能需要 Host 的角色方法会携带 call_id:tool/invoke、runtime/execute、delivery/deliver、transport/start、service/serve、observer/observe、parser/parse。(model/chat 携带它只是为了标记流式应答的 model/delta 通知。)回调(host/*)必须携带该 call_id;Host 以该调用的权限(其 run、主体和 uses)执行回调,并且只在该调用进行期间允许。每个回调只允许出现在其允许范围列(第 7 节)所列的调用中;否则 Host 应答插件错误 rpc.not_allowed。
| 允许范围 | 调用 |
|---|---|
caller | 任何带 call_id 的调用:tool/invoke、runtime/execute 以及后台调用 |
background | 上述调用,再加 service/serve 和 observer/observe |
tool | tool/invoke |
runtime | runtime/execute |
transport | transport/start |
delivery | delivery/deliver |
parser | parser/parse |
通知没有 id,也没有应答:$/cancel(双方均可,{"id": <请求 id>},取消该请求;接收方可能已经执行)和 model/delta(模型插件在应答流式 model/chat 时发送 {"call_id", "text", "kind"};kind 为 text(缺省时即为它)、reasoning 或 tool_args,因此不发送它的插件流出的是文本)。
4. 错误
| JSON-RPC 码 | 含义 |
|---|---|
-32601 | 方法不存在。未在 initialize 中列出的可选方法,Host 不会调用 |
-32603 | 内部错误:结果无法编码 |
-32000 | 插件错误。带 data {"code", "message", "retryable", "not_sent"} 时是已分类错误;不带 data(其他任何失败,包括 Go SDK 处理函数 panic)时是未分类错误 |
使用稳定的点分码 <命名空间>.<原因>(notes.failed、rail.sold_out)。retryable: true 允许 Host 之后重试同一操作;not_sent: true 表示该操作肯定没有产生外部效果。不带 data 的错误视为可重试、结果未知。逻辑从不解析 message。
5. 数据约定
| 项目 | 规则 |
|---|---|
| 字段名 | snake_case,与 schema 一致 |
| 未知字段 | 忽略;新版本的对端会在 v1 内增加字段 |
| 省略的字段 | schema 中未标为必填的字段可以缺省;数组和 map 也可以为 null |
| 字节 | base64 字符串(产物和下载文件的 data) |
| 时间 | RFC 3339 字符串 |
| 任意 JSON | args、config、parameters 和检查点可以是任意 JSON 值 |
| 面向人的文本 | 由 Host 的语言文件渲染;面向模型的文本使用英文 |
6. 版本
版本是 plugin.yaml 和 initialize 中的 api;Host 支持 v1。v1 内只允许增量变更:新的可选字段、新的可选方法(通过 InitializeResult.optional 声明)、新的回调。任何会破坏现有插件的变更都需要新的 api 版本,与 v1 并存。
7. 方法
guard 返回的 Args(guard/rewrite)、Messages 与 Content(guard/before_model、guard/after_model)只对运维给了 grants: [rewrite] 的实例生效(ADR 0005);否则 Host 忽略并记录日志。
7.1 Host → 插件
| 方法 | 角色 | 可选 | 参数 | 结果 | Schema |
|---|---|---|---|---|---|
initialize | — | InitializeParams | InitializeResult | initialize.json | |
shutdown | — | {} | {} | shutdown.json | |
tool/list | tool | ContextParams | []ToolSpec | tool.list.json | |
tool/invoke | tool | InvokeParams | ToolResult | tool.invoke.json | |
skill/list | skill | ContextParams | []SkillCard | skill.list.json | |
skill/load | skill | SkillLoadParams | SkillDoc | skill.load.json | |
guard/check | guard | GuardParams | GuardDecision | guard.check.json | |
guard/check_input | guard | 是 | GuardInputParams | GuardDecision | guard.check_input.json |
guard/check_result | guard | 是 | GuardResultParams | ResultDecision | guard.check_result.json |
guard/check_completion | guard | 是 | GuardCompletionParams | CompletionDecision | guard.check_completion.json |
guard/rewrite | guard | 是 | GuardParams | RewriteDecision | guard.rewrite.json |
guard/filter_tools | guard | 是 | GuardToolsParams | ToolFilterDecision | guard.filter_tools.json |
guard/before_model | guard | 是 | GuardModelParams | ModelDecision | guard.before_model.json |
guard/after_model | guard | 是 | GuardModelParams | ModelDecision | guard.after_model.json |
guard/check_step | guard | 是 | GuardStepParams | StepDecision | guard.check_step.json |
reviewer/review | reviewer | GuardParams | ReviewDecision | reviewer.review.json | |
context/get | context | ContextGetParams | []ContextItem | context.get.json | |
compactor/compact | compactor | CompactParams | CompactRecord | compactor.compact.json | |
processor/process | context_processor | ProcessParams | []ViewEdit | processor.process.json | |
observer/observe | observer | ObserveParams | {} | observer.observe.json | |
observer/before_compact | observer | 是 | FlushParams | FlushResult | observer.before_compact.json |
service/serve | service | HostParams | {}(取消时返回) | service.serve.json | |
model/chat | model | ChatParams | ChatResponse | model.chat.json | |
trigger/match | trigger | TriggerInput | Decision | trigger.match.json | |
presentation/present | presentation | Output | []Item | presentation.present.json | |
runtime/execute | runtime | ExecuteParams | Result | runtime.execute.json | |
delivery/deliver | delivery | DeliverParams | Receipt | delivery.deliver.json | |
transport/start | transport | HostParams | {}(取消时返回) | transport.start.json | |
transport/fetch | transport | FetchParams | FetchResult | transport.fetch.json | |
parser/parse | parser | ParserParseParams | media.Document | parser.parse.json |
插件 → Host 的通知:model/delta(DeltaParams,model.delta.json)。双方均可发送:$/cancel(CancelParams,cancel.json)。
7.2 插件 → Host(回调)
| 方法 | 允许范围 | 参数 | 结果 | Schema |
|---|---|---|---|---|
host/tools | caller | HostParams | []ToolSpec | host.tools.json |
host/call_tool | caller | HostCallToolParams | ToolResult | host.call_tool.json |
host/chat | background | HostChatParams | ChatResponse | host.chat.json |
host/embed | background | HostEmbedParams | [][]float32 | host.embed.json |
host/put_artifact | caller | HostPutArtifactParams | ArtifactRef | host.put_artifact.json |
host/parse | caller | HostParseParams | media.Document | host.parse.json |
host/read_artifact | parser | HostReadArtifactParams | HostReadArtifactResult | host.read_artifact.json |
host/open_artifact | delivery | HostTextParams | HostArtifactResult | host.open_artifact.json |
host/skills | caller | HostParams | []SkillCard | host.skills.json |
host/load_skill | caller | HostLoadSkillParams | SkillDoc | host.load_skill.json |
host/context_items | caller | HostContextParams | []ContextItem | host.context_items.json |
host/report_spans | caller | HostSpansParams | {} | host.report_spans.json |
host/check_completion | caller | HostCompletionParams | []string | host.check_completion.json |
host/progress | runtime | HostTextParams | {} | host.progress.json |
host/ask | tool or runtime | HostAskParams | Answer | host.ask.json |
host/checkpoint | runtime | HostStateParams | {} | host.checkpoint.json |
host/commit | runtime | HostCommitParams | {} | host.commit.json |
host/session_entries | runtime | HostEntriesParams | []Entry | host.session_entries.json |
host/compact | caller | HostCompactParams | CompactRecord | host.compact.json |
host/process_context | caller | HostProcessParams | []ProcessorEdits | host.process_context.json |
host/before_compact | caller | HostFlushParams | []string | host.before_compact.json |
host/check_step | caller | HostStepParams | StepResult | host.check_step.json |
host/send | runtime | HostSendParams | {} | host.send.json |
host/read_tool_result | runtime | HostReadResultParams | ResultPage | host.read_tool_result.json |
host/remind | tool | HostRemindParams | {} | host.remind.json |
host/take_reminders | runtime | HostParams | []Reminder | host.take_reminders.json |
host/submit | transport | HostEventParams | {} | host.submit.json |
host/answer | transport | HostAnswerParams | AnswerResult | host.answer.json |
host/locale | transport | HostParams | 字符串 | host.locale.json |
host/run_for_event | transport | HostTextParams | HostRunResult | host.run_for_event.json |
host/run | transport | HostTextParams | RunView | host.run.json |
host/cancel_run | transport | HostTextParams | {} | host.cancel_run.json |
host/link_start | caller | HostParams | 字符串 | host.link_start.json |
host/link_finish | caller | HostTextParams | {} | host.link_finish.json |
host/secret_get | caller | HostSecretParams | HostSecretResult | host.secret_get.json |
host/secret_put | caller | HostSecretParams | {} | host.secret_put.json |
host/secret_delete | caller | HostSecretParams | {} | host.secret_delete.json |
host/resolve_secret | tool | HostTextParams | string | host.resolve_secret.json |
各角色方法的含义和限制见插件开发第 6 节和 Hook 参考;parser 角色的两个方法及其限制与失败见解析器协议。
8. 示例
示例省略了 Content-Length 头。→ 表示 Host 发往插件,← 表示插件发往 Host。context 对象(CallContext)是 Host 填写的可信上下文,第一个示例之后做了缩写。
initialize
1→ {"jsonrpc":"2.0","id":1,"method":"initialize","params":{"api":"v1","id":"notes","config":{"blocked_words":["secret"]},
2 "data_dir":"/var/orquelo/data/notes","options":{}}}
3← {"jsonrpc":"2.0","id":1,"result":{"manifest":{"name":"notes-python","version":"0.1.0","roles":["tool","guard","context","skill"]},
4 "optional":[]}}tool/list
1→ {"jsonrpc":"2.0","id":2,"method":"tool/list","params":{"context":{"run_id":"rn_7f3a","caller":"agent",
2 "principal":{"id":"pr_1","tenant":"default","connection":"feishu","external_id":"ou_ab","role":"user","user":"us_1"},
3 "conversation":{"id":"oc_1","kind":"direct"},"connection":"feishu","chain":["agent"],"session":"ss_9"}}}
4← {"jsonrpc":"2.0","id":2,"result":[{"name":"note_add","description":"Save one short note for later.",
5 "parameters":{"type":"object","properties":{"text":{"type":"string"}},"required":["text"]}}]}tool/invoke,中间穿插一次回调
1→ {"jsonrpc":"2.0","id":3,"method":"tool/invoke","params":{"call_id":"c12","call":{"name":"note_add",
2 "args":{"text":"the code is BLUE"},"key":"tc_4be1a9c0d2","context":{"run_id":"rn_7f3a","caller":"agent","…":"…"}}}}
3← {"jsonrpc":"2.0","id":1,"method":"host/remind","params":{"call_id":"c12","kind":"notes","text":"A note was saved."}}
4→ {"jsonrpc":"2.0","id":1,"result":{}}
5← {"jsonrpc":"2.0","id":3,"result":{"content":"Saved."}}插件的回调使用自己的 id 序列(1),与 Host 的序列(3)互不相关。
guard/check
→ {"jsonrpc":"2.0","id":4,"method":"guard/check","params":{"context":{"…":"…"},
"intent":{"name":"bash","args":{"command":"echo secret"},"provider":"files","key":"tc_9d02"}}}
← {"jsonrpc":"2.0","id":4,"result":{"deny":true,"reason":"the word 'secret' is blocked by notes-python"}}guard 也可以应答 {"ask":true,"reason":"…","approver":"admin"},要求人工审批。
context/get
→ {"jsonrpc":"2.0","id":5,"method":"context/get","params":{"context":{"…":"…"},"input":{"text":"what did I note?","locale":"zh-CN"}}}
← {"jsonrpc":"2.0","id":5,"result":[{"title":"Saved notes","content":"1 notes are saved; use note_list to read them."}]}skill/load
→ {"jsonrpc":"2.0","id":6,"method":"skill/load","params":{"context":{"…":"…"},"name":"take-notes"}}
← {"jsonrpc":"2.0","id":6,"result":{"content":"When the user asks you to remember something, call `note_add` …"}}$/cancel
→ {"jsonrpc":"2.0","method":"$/cancel","params":{"id":3}}错误应答
← {"jsonrpc":"2.0","id":6,"error":{"code":-32000,"message":"no skill named 'x'",
"data":{"code":"notes.failed","message":"no skill named 'x'"}}}9. 一致性
| 工具 | 用途 |
|---|---|
schemas/protocol/v1 | 每个方法一个 JSON Schema 2020-12 文件(params、result),以及 index.json(方法、方向、角色、可选、通知、允许范围)。由 go test ./pkg/plugin/rpc -update 从 pkg/plugin/rpc/spec.go 生成;与代码不一致时测试失败。可用于生成类型或校验消息 |
plugins/notes-python | 只用标准库的完整 Python 参考插件;TestPythonPlugin 用 Go 测试集运行它 |
orquelo plugin test [-config JSON] [-options JSON] DIR | -options 以实例 options 的形式给出插件的选项值,按 Host 的方式解析并展开(带 ${option.api_key} 的 URL 用这个 key 测试)。启动 DIR 中的插件,在本协议上运行每个已声明角色的测试集;逐个打印 PASS/FAIL,有失败时以非零码退出。与本指南一同加入 |