// RPC contract (Effect flavor) — template // // Docs: https://opencode.ai/v2/docs/build/plugins/effect/rpc/ // // Copy to src/rpc.ts — the implementation template's doc-accurate import is // "./rpc.js", so the file must be named rpc.ts. Contract only, no // implementation. Same contract shape as rpc.ts, but with Effect Schema // instead of raw JSON Schema. Registered by rpc-implementation-effect.ts. // // Rpc.define is imported from "@opencode/plugin/rpc" here too, NOT from // "@opencode/plugin/effect". The Effect page imports Schema from the root // "effect" module. // // TYPE NOTE: the shipped `ValueSchema` type is // Schema.Codec | StandardSchemaV1 | JsonSchema // and Effect's `Schema.Struct` is not a `Codec`, so `tsc --strict` can reject // the form below. The page states JSON Schema and Standard Schema are supported // here too, so either of these typechecks instead: // input: { type: "object", properties: { query: { type: "string" } }, required: ["query"] } // input: z.object({ query: z.string() }) // or Valibot / ArkType // A method needs BOTH input and output to be present for the types to line up. // // PREREQUISITE: install @opencode/plugin at your OpenCode version and restart // the service — see the PREREQUISITE block in promise-plugin.ts. // import { Rpc } from "@opencode/plugin/rpc" import { Schema } from "effect" export const Acme = Rpc.define({ // The page does not describe how `id` affects method routing. Pick a name // that will not collide. id: "acme", methods: { search: { // Effect Schema validates and infers types, so the handler needs no casts. input: Schema.Struct({ query: Schema.String }), output: Schema.Struct({ text: Schema.String }), // Omit input or output if the method takes or returns nothing. // Each error key becomes the error's `type`. errors: { not_found: Schema.Struct({ query: Schema.String }), }, }, }, events: { // Event data must be an object. updated: { schema: Schema.Struct({ itemID: Schema.String, text: Schema.String }), }, }, }) // The Effect page also states that JSON Schema and Standard Schema are // supported here, so an Effect contract is not restricted to Effect Schema.