Effect eklentilerinde RPC
Effect eklentileri, tipli Effect’ler ve Stream’ler döndüren yöntemler ve olaylar sunabilir.
RPC’yi tanımlamak için Effect Schema ile Rpc.define kullanın.
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 değerleri doğrular ve TypeScript türlerini çıkarır.
input: Schema.Struct({ query: Schema.String })JSON Schema ve Standard Schema da desteklenir.
Yöntemin argümanı için input, dönüş değeri için output kullanın. İkisi de zorunludur. Girdi veya çıktı yoksa ilgili
şema için Schema.Struct({}) kullanın. Girdi yoksa yöntemi {} ile çağırın, çıktı yoksa {} döndürün.
search: {
input: Schema.Struct({ query: Schema.String }),
output: Schema.Struct({ text: Schema.String }),
}Beklenen başarısızlıkları errors listesine ekleyin. Her anahtar hatanın type değeri olur.
errors: {
not_found: Schema.Struct({ query: Schema.String }),
}Olayları üst düzey events eşlemesine ekleyin. Olay verisi bir nesne olmalıdır. Eşleme zorunludur; RPC’nin olayı yoksa
events: {} kullanın.
events: {
updated: {
schema: Schema.Struct({ itemID: Schema.String, text: Schema.String }),
},
}Uygulamayı eklenti Effect’inden kaydedin:
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),
})Hata eşlemesi her yöntem Effect’inin hata türü olur. Bildirilen bir hata oluşturmak için context.error(...) kullanın.
RPC kaydedildikten sonra HTTP üzerinden veya başka bir eklentiden çağrılabilir.
Effect istemcisini oluşturun, ardından RPC alt istemcisini oluşturun:
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)))Başka bir Effect eklentisi kendi bağlamından yerel bir alt istemci oluşturabilir:
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)RPC olayları Stream’lerdir. Olay adıyla abone olun ve Stream’i kapsamlı bir fiber’da çalıştırın:
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,
)Aboneler yalnızca canlı olayları alır. Stream’deki olayların okunması durduğunda abonelik kapanır.