प्लगइन RPC
अन्य प्लगइन्स और क्लाइंट्स के लिए प्लगइन के कस्टम मेथड्स और इवेंट्स को एक्सपोज़ करें।
प्लगइन्स ऐसे कस्टम मेथड्स और इवेंट्स को एक्सपोज़ कर सकते हैं जो सर्वर पर चलते हैं और जिन्हें अन्य प्लगइन्स या क्लाइंट्स द्वारा कॉल किया जा सकता है।
परिभाषित करें (Define)
RPC के मेथड्स, त्रुटियों और इवेंट्स को सूचीबद्ध करने के लिए Rpc.define का उपयोग करें।
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,
},
},
},
})सत्यापन (Validation)
एक RPC परिभाषा मेथड्स, इवेंट्स और त्रुटियों के इनपुट और आउटपुट के आकार का वर्णन करती है। यह दो स्कीमा फ़ॉर्मेट का समर्थन करती है:
- 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"],
},
}TypeScript में JSON Schema मान unknown होते हैं, इसलिए उपयोग करने से पहले उन्हें नैरो (narrow) करें। Standard Schema इनपुट और आउटपुट प्रकारों का स्वतः अनुमान लगा लेता है।
त्रुटियाँ (Errors)
अपेक्षित विफलताओं के लिए मेथड में errors मैप जोड़ें। प्रत्येक कुंजी त्रुटि का type बन जाती है, और उसका स्कीमा त्रुटि के data को परिभाषित करता है।
errors: {
not_found: {
type: "object",
properties: { query: { type: "string" } },
required: ["query"],
additionalProperties: false,
},
}rpc. से शुरू होने वाले त्रुटि नाम OpenCode द्वारा आरक्षित हैं।
इवेंट्स (Events)
शीर्ष-स्तरीय events मैप में इवेंट्स जोड़ें। प्रत्येक इवेंट में सब्सक्राइबर को भेजे जाने वाले डेटा के लिए एक स्कीमा होता है।
इवेंट डेटा एक ऑब्जेक्ट होना चाहिए। जब कोई डेटा न हो, तो एक खाली ऑब्जेक्ट स्कीमा का उपयोग करें:
events: {
refreshed: {
schema: { type: "object", additionalProperties: false },
},
}स्केलर, ऐरे, null, और undefined मान्य इवेंट डेटा नहीं हैं।
लागू करें (Implement)
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" })
},
})RPC पंजीकृत करने के बाद, वही प्लगइन इसे ctx.rpc(Acme) के माध्यम से कॉल कर सकता है।
दूसरे तर्क में रद्दीकरण के लिए signal और घोषित त्रुटियों के लिए context.error(...) शामिल होते हैं। आप त्रुटि को रिटर्न या थ्रो कर सकते हैं।
एक प्लगइन एक से अधिक RPC पंजीकृत कर सकता है। पंजीकरण को डिस्पोज़ करने से यह हट जाता है।
कॉल करें (Call)
एक बार RPC पंजीकृत हो जाने के बाद, इसे HTTP पर या किसी अन्य प्लगइन से कॉल किया जा सकता है।
HTTP
एक OpenCode क्लाइंट बनाएँ, फिर RPC के लिए सबक्लाइंट बनाने हेतु client.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" })प्लगइन (Plugin)
प्लगइन्स के पास पहले से ही एक 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) हो जाता है।
सब्सक्राइब करें (Subscribe)
कॉलबैक के लिए events.on का उपयोग करें और जब लिसनर की आवश्यकता न रहे, तो अनसब्सक्राइब करें:
const unsubscribe = acme.events.on("updated", (event) => {
console.log(event.type, event.location.directory, event.data.text)
})
unsubscribe()async iterable के लिए, events.subscribe("updated") का उपयोग करें:
for await (const event of acme.events.subscribe("updated")) {
console.log(event.data.text)
}इवेंट सब्सक्रिप्शन केवल लाइव होते हैं, इसलिए डिस्कनेक्ट हुए सब्सक्राइबर इवेंट से चूक जाते हैं।
- स्थानीय नाम से सब्सक्राइब करें, जैसे
updated। - प्राप्त प्रकार उपसर्ग (prefixed) वाला होता है, जैसे
rpc.acme.updated। - प्रत्येक इवेंट में
dataऔरlocationशामिल होता है। - प्लगइन अनलोड होने पर इसके सब्सक्रिप्शन बंद हो जाते हैं।
बाहरी क्लाइंट हर लोकेशन से इवेंट प्राप्त करते हैं, इसलिए आवश्यकता होने पर event.location की जाँच करें। सर्वर प्लगइन को अभी भी कॉन्फ़िगर और चालू होना आवश्यक है।