RPC dei plugin Effect
I plugin Effect possono esporre metodi ed eventi che restituiscono Effect e Stream tipizzati.
Usa Rpc.define con Effect Schema per definire l’RPC.
import { Rpc } from "@opencode/plugin/rpc"
import { Schema } from "effect"
export const Acme = Rpc.define({
id: "acme",
methods: {
search: {
input: Schema.Struct({ query: Schema.String }),
output: Schema.Struct({ text: Schema.String }),
errors: {
not_found: Schema.Struct({ query: Schema.String }),
},
},
},
events: {
updated: {
schema: Schema.Struct({ itemID: Schema.String, text: Schema.String }),
},
},
})Effect Schema valida i valori e ne inferisce i tipi TypeScript.
input: Schema.Struct({ query: Schema.String })Sono supportati anche JSON Schema e Standard Schema.
Usa input per l’argomento del metodo e output per il valore restituito. Entrambi sono obbligatori.
Se il metodo non accetta o non restituisce alcun valore, usa Schema.Struct({}), chiamalo con {}
e restituisci {}.
search: {
input: Schema.Struct({ query: Schema.String }),
output: Schema.Struct({ text: Schema.String }),
}Aggiungi gli errori previsti a errors. Ogni chiave diventa il type dell’errore.
errors: {
not_found: Schema.Struct({ query: Schema.String }),
}Aggiungi gli eventi alla mappa events di primo livello. I dati di ogni evento devono essere un oggetto.
La mappa è obbligatoria: usa events: {} se l’RPC non ha eventi.
events: {
updated: {
schema: Schema.Struct({ itemID: Schema.String, text: Schema.String }),
},
}Registra l’implementazione dall’Effect del plugin:
import { Plugin } from "@opencode/plugin/effect"
import { Effect } from "effect"
import { Acme } from "./rpc.js"
export default Plugin.define({
id: "acme-effect-plugin",
effect: (ctx) =>
Effect.gen(function* () {
const registration = yield* ctx.rpc.register(Acme, {
search: ({ query }, context) =>
findText(query).pipe(
Effect.flatMap((text) =>
text
? Effect.succeed({ text })
: Effect.fail(context.error("not_found", "Result not found", { query })),
),
),
})
const acme = ctx.rpc(Acme)
const result = yield* acme.search({ query: "hello" })
yield* registration.events.emit("updated", { itemID: "item-1", text: result.text })
}).pipe(Effect.orDie),
})La mappa degli errori diventa il tipo di errore di ogni Effect dei metodi. Usa
context.error(...) per creare un errore dichiarato.
Una volta registrato l’RPC, può essere chiamato via HTTP o da un altro plugin.
Crea il client Effect, poi crea il subclient RPC:
import { OpenCode } from "@opencode/client/effect"
import { Effect } from "effect"
import { FetchHttpClient } from "effect/http"
import { Acme } from "opencode-acme-plugin/rpc"
const program = Effect.gen(function* () {
const client = yield* OpenCode.make({ baseUrl: "http://localhost:4096" })
const acme = client.rpc(Acme)
return yield* acme.search({ query: "hello" })
})
const result = await Effect.runPromise(program.pipe(Effect.provide(FetchHttpClient.layer)))Un altro plugin Effect può creare un subclient locale dal suo contesto:
import { Effect } from "effect"
effect: (ctx) =>
Effect.gen(function* () {
const acme = ctx.rpc(Acme)
const result = yield* acme.search({ query: "hello" })
yield* Effect.logInfo(result.text)
}).pipe(Effect.orDie)Gli eventi RPC sono Stream. Sottoscriviti per nome di evento ed esegui lo Stream in un fiber con scope:
import { Effect, Stream } from "effect"
const acme = ctx.rpc(Acme)
yield* acme.events.subscribe("updated").pipe(
Stream.runForEach((event) => Effect.logInfo(event.data.text)),
Effect.forkScoped,
)Le sottoscrizioni ricevono solo gli eventi in tempo reale e si chiudono quando interrompi la lettura dello Stream.