हिन्दी
दस्तावेज़
OpenCode पर बनाएँ
प्लगइन्स
Effect प्लगइन RPC

Effect प्लगइन RPC

Effect प्लगइन के मेथड्स और इवेंट्स को टाइप्ड Effects और Streams के रूप में एक्सपोज़ करें।

Effect प्लगइन्स ऐसे मेथड्स और इवेंट्स एक्सपोज़ कर सकते हैं जो टाइप्ड Effects और Streams लौटाते हैं।

परिभाषित करें (Define)

RPC को परिभाषित करने के लिए Effect Schema के साथ Rpc.define का उपयोग करें।

src/rpc.ts
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 }),
    },
  },
})

सत्यापन (Validation)

Effect Schema मानों को सत्यापित करता है और उनके TypeScript प्रकारों का अनुमान लगाता है।

input: Schema.Struct({ query: Schema.String })

JSON Schema और Standard Schema भी समर्थित हैं।

इनपुट और आउटपुट

मेथड तर्क के लिए input और इसके रिटर्न मान के लिए output का उपयोग करें। जब कोई मान न हो, तो इनमें से किसी को भी छोड़ दें।

search: {
  input: Schema.Struct({ query: Schema.String }),
  output: Schema.Struct({ text: Schema.String }),
}

त्रुटियाँ (Errors)

अपेक्षित विफलताओं को errors में जोड़ें। प्रत्येक कुंजी त्रुटि का type बन जाती है।

errors: {
  not_found: Schema.Struct({ query: Schema.String }),
}

इवेंट्स (Events)

शीर्ष-स्तरीय events मैप में इवेंट्स जोड़ें। इवेंट डेटा एक ऑब्जेक्ट होना चाहिए।

events: {
  updated: {
    schema: Schema.Struct({ itemID: Schema.String, text: Schema.String }),
  },
}

लागू करें (Implement)

प्लगइन Effect से कार्यान्वयन को पंजीकृत करें:

src/index.ts
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),
})

त्रुटि मैप प्रत्येक मेथड Effect का त्रुटि प्रकार बन जाता है। घोषित त्रुटि बनाने के लिए context.error(...) का उपयोग करें।

कॉल करें (Call)

एक बार RPC पंजीकृत हो जाने के बाद, इसे HTTP पर या किसी अन्य प्लगइन से कॉल किया जा सकता है।

HTTP

Effect क्लाइंट बनाएँ, फिर 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)))

प्लगइन (Plugin)

एक अन्य Effect प्लगइन अपने कॉन्टेक्स्ट से एक स्थानीय सबक्लाइंट बना सकता है:

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)

सब्सक्राइब करें (Subscribe)

RPC इवेंट्स Streams होते हैं। इवेंट नाम से सब्सक्राइब करें और Stream को एक स्कोप्ड फ़ाइबर में चलाएँ:

{/* prettier-ignore */}

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,
)

सब्सक्रिप्शन केवल लाइव होते हैं और Stream का उपभोग बंद होने पर बंद हो जाते हैं।