插件 RPC
将插件的自定义方法与事件暴露给其他插件及客户端。
插件可以暴露在服务端运行的自定义方法和事件,供其他插件或客户端直接调用和订阅。
定义契约
使用 Rpc.define 声明 RPC 的方法、错误与事件:
import { Rpc } from "@opencode/plugin/rpc"
export const Acme = Rpc.define({
id: "acme",
methods: {
search: {
input: {
type: "object",
properties: { query: { type: "string" } },
required: ["query"],
additionalProperties: false,
},
output: {
type: "object",
properties: { text: { type: "string" } },
required: ["text"],
additionalProperties: false,
},
errors: {
not_found: {
type: "object",
properties: { query: { type: "string" } },
required: ["query"],
additionalProperties: false,
},
},
},
},
events: {
updated: {
schema: {
type: "object",
properties: {
itemID: { type: "string" },
text: { type: "string" },
},
required: ["itemID", "text"],
additionalProperties: false,
},
},
},
})数据校验
RPC 定义用于描述方法输入输出、事件载荷与错误数据的结构,支持两种 Schema 格式:
- JSON Schema:简单直接,无需额外依赖。
- 任意兼容 Standard Schema 的校验库:
- Zod
- Valibot
- ArkType
输入与输出
每个方法都可以通过 input 定义参数结构,通过 output 定义返回值结构。如果方法不接受参数或不返回值,省略对应字段即可:
search: {
input: {
type: "object",
properties: { query: { type: "string" } },
required: ["query"],
},
output: {
type: "object",
properties: { text: { type: "string" } },
required: ["text"],
},
}使用 JSON Schema 时,TypeScript 中的值类型为 unknown,使用前需自行做类型收窄;使用 Standard Schema 时则会自动推导输入与输出类型。
错误
为方法添加 errors 映射表以声明预期内的业务错误。每个键都会成为错误的 type,其 Schema 定义错误的 data 结构:
errors: {
not_found: {
type: "object",
properties: { query: { type: "string" } },
required: ["query"],
additionalProperties: false,
},
}以 rpc. 开头的错误名由 OpenCode 框架保留,请勿占用。
事件
在顶层 events 映射表中定义事件,每个事件通过 schema 指定推送给订阅方的数据结构。
事件数据必须是对象。如果没有需要携带的载荷,请使用空对象 Schema:
events: {
refreshed: {
schema: { type: "object", additionalProperties: false },
},
}标量值、数组、null 和 undefined 均不是合法的事件数据。
实现与注册
在插件的 setup 中注册具体实现:
import { Plugin } from "@opencode/plugin"
import { Acme } from "./rpc.js"
export default Plugin.define({
id: "acme-plugin",
async setup(ctx) {
const registration = await ctx.rpc.register(Acme, {
search: async (input, context) => {
const { query } = input as { query: string }
const text = await findText(query, { signal: context.signal })
if (!text) return context.error("not_found", "Result not found", { query })
return { text }
},
})
const acme = ctx.rpc(Acme)
const result = await acme.search({ query: "hello" })
await registration.events.emit("updated", { itemID: "item-1", text: "ready" })
},
})注册完成后,当前插件自身也可以通过 ctx.rpc(Acme) 进行调用。
处理函数的第二个参数包含用于取消操作的 signal 以及用于构造声明式错误的 context.error(...)。你可以直接 return 或 throw 该错误。
单个插件可以注册多个 RPC。释放(dispose)注册句柄即可注销对应 RPC。
调用方式
RPC 注册后,即可通过 HTTP 或其他插件调用。
通过 HTTP 调用
创建一个 OpenCode 客户端,然后调用 client.rpc 获取对应 RPC 的子客户端:
import { OpenCode } from "@opencode/client"
import { Acme } from "opencode-acme-plugin/rpc"
const client = OpenCode.make({
baseUrl: "http://localhost:4096",
})
const acme = client.rpc(Acme)
const result = await acme.search({ query: "hello" })在插件中调用
插件上下文中已经内置了 OpenCode 客户端。例如,TUI 插件可以直接从 context.client 创建相同的 RPC 子客户端:
import { Plugin } from "@opencode/plugin/tui"
import { Acme } from "opencode-acme-plugin/rpc"
export default Plugin.define({
id: "acme-tui",
async setup(context) {
const acme = context.client.rpc(Acme)
const result = await acme.search({ query: "hello" })
console.log(result)
},
})GUI 插件则从 ctx.client 读取桌面应用内置服务器的客户端:
import { Command, Plugin } from "@opencode/plugin/gui"
import { Acme } from "opencode-acme-plugin/rpc"
export default Plugin.define({
setup(ctx) {
ctx.add(Command, {
id: "search",
title: "Search Acme",
run: async () => {
const result = await ctx.client.rpc(Acme).search({ query: "hello" })
console.log(result)
},
})
},
})请在每次调用处实时读取 ctx.client,因为服务器重启后该引用会更新。服务器不可达时,所有调用都会 reject。
订阅事件
使用 events.on 注册回调监听,不再需要时调用返回的取消函数:
const unsubscribe = acme.events.on("updated", (event) => {
console.log(event.type, event.location.directory, event.data.text)
})
unsubscribe()如需以异步可迭代方式消费,请使用 events.subscribe("updated"):
for await (const event of acme.events.subscribe("updated")) {
console.log(event.data.text)
}事件订阅仅限实时推送,断开连接期间错过的事件不会补发。
- 订阅时传入局部事件名(如
updated)。 - 接收到的事件
type带有完整前缀(如rpc.acme.updated)。 - 每条事件都包含
data与location。 - 插件卸载时会自动关闭其持有的所有订阅。
外部客户端会收到来自所有位置的事件,必要时请检查 event.location。服务端插件仍需处于已配置且正在运行的状态。