Effect प्लगइन
Effect-नेटिव प्लगइन API के साथ सर्वर प्लगइन लिखें।
@opencode/plugin/effect, OpenCode प्लगइन API का Effect-नेटिव संस्करण है। इसके कॉन्टेक्स्ट ऑपरेशन Effects या Streams लौटाते हैं, कॉलबैक Effects लौटाते हैं, और प्लगइन का जीवनकाल Scope द्वारा दर्शाया जाता है।
प्लगइन और उनकी node_modules निर्भरताओं को लोड करते समय OpenCode रनटाइम पर effect (एक्सपोर्ट किए गए effect/* सबपाथ सहित) और @opencode/plugin प्रदान करता है। इन्हें अपने प्लगइन में बंडल करने के बजाय स्थानीय टाइप-चेकिंग और परीक्षण के लिए peer/dev निर्भरताओं के रूप में इंस्टॉल करें।
bun add --peer @opencode/plugin [email protected]Effect प्लगइन को स्वचालित रूप से लोड करने के लिए इसे .opencode/plugins/ से एक्सपोर्ट करें।
import { Plugin } from "@opencode/plugin/effect"
import { Effect } from "effect"
export default Plugin.define({
id: "example",
effect: (ctx) =>
Effect.gen(function* () {
const storage = ctx.storage
yield* storage.set("loaded", true)
}),
})प्रकाशित पैकेज और .opencode/plugins/ के बाहर की प्लगइन डायरेक्टरी अन्य सर्वर प्लगइन के समान ही plugins कॉन्फ़िगरेशन का उपयोग करती हैं।
{
"$schema": "https://opencode.ai/config.json",
"plugins": [
"opencode-acme-effect-plugin",
"[email protected]",
"@acme/opencode-effect-plugin",
"./plugins/local-effect",
{
"package": "@acme/opencode-effect-plugin",
"options": { "agent": "reviewer", "strict": true },
},
],
}सक्रियण, पैकेज रिज़ॉल्यूशन और कॉन्फ़िगरेशन प्राथमिकता के लिए प्लगइन कॉन्फ़िगर करें देखें।
import { Plugin } from "@opencode/plugin/effect"
import { Effect } from "effect"
export default Plugin.define({
id: "local-effect",
effect: (ctx) =>
Effect.gen(function* () {
yield* Effect.logInfo("Effect plugin loaded", { version: ctx.app.version })
}),
})जीवनकाल
प्लगइन लोड होने पर effect चलता है। जब प्लगइन रीलोड या अनलोड होता है, तो इसका स्कोप बंद हो जाता है, जिससे रजिस्ट्रेशन, स्कोप्ड फ़ाइबर और फ़ाइनलाइज़र एक साथ रिलीज़ हो जाते हैं।
import { Plugin } from "@opencode/plugin/effect"
import { Effect } from "effect"
export default Plugin.define({
id: "lifecycle",
effect: (ctx) =>
Effect.gen(function* () {
yield* Effect.logInfo("loaded", { version: ctx.app.version })
yield* Effect.addFinalizer(() => Effect.logInfo("unloaded"))
}),
})बैकग्राउंड कार्य के लिए स्कोप्ड फ़ाइबर का उपयोग करें। क्लीनअप के दौरान प्लगइन स्कोप फ़ाइबर को बाधित (interrupt) कर देता है।
effect: (ctx) =>
Effect.gen(function* () {
const storage = ctx.storage
yield* Effect.repeat(
storage.set("heartbeat", { time: Date.now() }),
{ schedule: Schedule.spaced("1 minute") },
).pipe(Effect.forkScoped)
}),कॉन्टेक्स्ट
कॉन्टेक्स्ट कनेक्टेड OpenCode सर्वर के लिए Effect क्लाइंट के साथ-साथ केवल-प्लगइन ट्रांसफ़ॉर्म, हुक, स्टोरेज, रीलोड और विकल्प उपलब्ध कराता है। यह OpenCode की निजी Core सेवाओं को उजागर नहीं करता है।
effect: (ctx) =>
Effect.gen(function* () {
const plugins = ctx.plugin
const active = yield* plugins.list().pipe(Effect.orDie)
yield* Effect.logInfo("active plugins", { count: active.data.length })
}),प्लगइन परिभाषा और संपूर्ण कॉन्टेक्स्ट निम्नानुसार घोषित किए जाते हैं।
interface Context {
readonly app: App
readonly location: Location.Info
readonly options: PluginOptions
readonly agent: AgentDomain
readonly aisdk: AISDKDomain
readonly command: CommandDomain
readonly event: EventDomain
readonly experimental: {
readonly terminal: Pick<ExperimentalApi<unknown>["persistentPty"], "read">
readonly generate: GenerateApi<unknown>
}
readonly integration: IntegrationDomain
readonly mcp: MCPDomain
readonly model: ModelDomain
/** @deprecated Use `experimental.generate.text`. */
readonly generate: Pick<ExperimentalGenerateApi<unknown>, "text">
readonly permission: PermissionDomain
readonly plugin: Pick<PluginApi<unknown>, "list">
readonly provider: ProviderDomain
readonly reference: ReferenceDomain
readonly rpc: RpcDomain
readonly session: SessionDomain
readonly shell: ShellDomain
readonly skill: SkillDomain
readonly storage: StorageDomain
readonly tool: ToolDomain
readonly vcs: VcsDomain
readonly websearch: WebSearchDomain
readonly worktree: WorktreeDomain
}
interface Plugin<R = Scope.Scope> {
readonly id: string
readonly effect: (context: Context) => Effect.Effect<void, never, R>
}विकल्प
opencode.json(c) में ऑब्जेक्ट रूप के साथ विकल्प पास करें।
{
"plugins": [
{
"package": "./plugins/company-effect",
"options": { "strict": true },
},
],
}ctx.options से विकल्प पढ़ें। उपयोग करने से पहले अज्ञात मानों (unknown values) को नैरो (narrow) करें।
import { Plugin } from "@opencode/plugin/effect"
import { Effect } from "effect"
export default Plugin.define({
id: "company",
effect: (ctx) =>
Effect.gen(function* () {
const strict = ctx.options.strict === true
yield* Effect.logInfo("company plugin configured", { strict })
}),
})ट्रांसफ़ॉर्म
ट्रांसफ़ॉर्म एक एडिटर के माध्यम से सिंक्रोनस रूप से स्टेट को संपादित करते हैं। OpenCode प्रत्येक डोमेन के भीतर रजिस्ट्रेशन के क्रम में ट्रांसफ़ॉर्म लागू करता है। स्रोत पहले प्रोवाइडर और अपरिवर्तनीय मॉडल परिभाषाएँ प्रदान करते हैं; उसके बाद मॉडल ट्रांसफ़ॉर्म सक्रिय प्रोवाइडर के उम्मीदवारों को संपादित करते हैं। रजिस्ट्रेशन को यील्ड (yield) करने से यह प्लगइन स्कोप में बना रहता है।
import { Model, Plugin, Provider } from "@opencode/plugin/effect"
import { Effect } from "effect"
export default Plugin.define({
id: "company.models",
effect: (ctx) =>
Effect.gen(function* () {
const providerID = Provider.ID.make("acme")
const models = [{ ...Model.Info.default(providerID, Model.ID.make("reasoner")), name: "Acme Reasoner" }]
yield* ctx.provider.transform((editor) => {
editor.add({
info: {
...Provider.Info.empty(providerID),
name: "Acme",
activation: "enabled",
package: "@opencode/ai/providers/openai-compatible",
settings: { baseURL: "http://127.0.0.1:8000/v1" },
},
models,
})
})
}),
})एक मॉडल ट्रांसफ़ॉर्म संपूर्ण सक्रिय-प्रोवाइडर उम्मीदवार संग्रह पर नीति लागू कर सकता है।
effect: (ctx) =>
Effect.gen(function* () {
yield* ctx.model.transform((editor) => {
editor
.list()
.filter((model) => model.cost.some((tier) => tier.output > 20))
.forEach((model) => {
editor.remove(model.providerID, model.id)
})
})
}),जब किसी ट्रांसफ़ॉर्म द्वारा उपयोग की जाने वाली बाहरी स्टेट बदलती है, तो reload कॉल करें। यहाँ, loadFromSource() { info: Provider.Info, models: readonly Model.Info[] } प्रविष्टियाँ लौटाता है; प्रोवाइडर रीलोड संपूर्ण मॉडल परिणाम को भी अमान्य (invalidate) कर देते हैं।
effect: (ctx) =>
Effect.gen(function* () {
const provider = ctx.provider
const source = { providers: yield* loadFromSource() }
yield* provider.transform((editor) => {
source.providers.forEach((provider) => editor.add(provider))
})
yield* Effect.repeat(
Effect.gen(function* () {
source.providers = yield* loadFromSource()
yield* provider.reload()
}),
{ schedule: Schedule.spaced("1 minute") },
).pipe(Effect.forkScoped)
}),API
एजेंट
सभी एजेंट पढ़ें या ID द्वारा किसी एक को फ़ेच करें। क्लाइंट प्रतिक्रियाओं में रिज़ॉल्व किया गया स्थान (location) और स्कीमा डेटा शामिल होता है।
effect: (ctx) =>
Effect.gen(function* () {
const agent = ctx.agent
const agents = yield* agent.list().pipe(Effect.orDie)
const build = yield* agent.get({ agentID: Agent.ID.make("build") }).pipe(Effect.orDie)
yield* Effect.logInfo("agents", { count: agents.data.length, build: build.data.name })
}),एजेंट को ट्रांसफ़ॉर्म या रीलोड करें।
effect: (ctx) =>
Effect.gen(function* () {
const agent = ctx.agent
yield* agent.transform((editor) => {
editor.default("build")
editor.update("build", (item) => (item.description = "Builds features and fixes bugs"))
editor.remove("legacy")
})
yield* agent.reload()
}),स्कीमा: Agent.Info (opens in a new tab)।
interface AgentEditor {
list(): readonly Types.DeepMutable<Agent.Info>[]
get(id: string): Types.DeepMutable<Agent.Info> | undefined
default(id: string | undefined): void
update(id: string, update: (agent: Types.DeepMutable<Agent.Info>) => void): void
remove(id: string): void
}
interface AgentDomain extends AgentApi<unknown> {
readonly transform: Transform<AgentEditor>
readonly reload: () => Effect.Effect<void>
}प्रोवाइडर
उपलब्ध प्रोवाइडर पढ़ें या ID द्वारा किसी एक प्रोवाइडर के मेटाडेटा का निरीक्षण करें।
effect: (ctx) =>
Effect.gen(function* () {
const provider = ctx.provider
const providers = yield* provider.list().pipe(Effect.orDie)
const anthropic = yield* provider.get({ providerID: Provider.ID.make("anthropic") }).pipe(Effect.orDie)
yield* Effect.logInfo("providers", {
providers: providers.data.length,
anthropic: anthropic.data.name,
})
}),प्रोवाइडर ट्रांसफ़ॉर्म प्रोवाइडर सेटिंग्स और स्रोत परिभाषाओं को संपादित करते हैं। एडिटर रीड्स में निष्क्रिय प्रोवाइडर शामिल होते हैं और अपरिवर्तनीय परिभाषाएँ उजागर होती हैं; models.update किसी स्रोत मॉडल की स्वामित्व वाली प्रति (owned copy) को संपादित करता है।
effect: (ctx) =>
Effect.gen(function* () {
yield* ctx.provider.transform((editor) => {
editor.update("anthropic", (provider) => {
provider.headers = { ...provider.headers, "x-company": "engineering" }
})
editor.models.update("anthropic", "claude-sonnet-5-5", (model) => (model.name = "Team Sonnet"))
editor.models.remove("anthropic", "legacy-model")
editor.remove("legacy-provider")
})
}),models.set के साथ स्रोत इन्वेंट्री को प्रतिस्थापित करें, फिर इसकी कैप्चर की गई परिभाषाएँ बदलने के बाद रीलोड करें।
effect: (ctx) =>
Effect.gen(function* () {
const provider = ctx.provider
const source = { models: yield* loadModels() }
yield* provider.transform((editor) => editor.models.set("acme", source.models))
source.models = yield* loadModels()
yield* provider.reload()
}),खाता-विशिष्ट डिस्कवरी के लिए, इन्वेंट्री लोड करने के लिए उपयोग किए गए कनेक्शन को sourceConnection के रूप में पास करें। जब कोई अन्य कनेक्शन चयनित होता है, तो OpenCode उस प्रोवाइडर को तब तक बाहर रखता है जब तक आपका प्लगइन अपनी रीफ़्रेश की गई इन्वेंट्री प्रकाशित न कर दे।
{/* prettier-ignore */}
const connection = yield* ctx.integration.connection.active("acme")
if (!connection) return
const inventory = yield* loadInventory(connection)
yield* ctx.provider.transform((editor) => {
editor.add({ info: inventory.provider, models: inventory.models, sourceConnection: connection })
})स्कीमा: Provider.Info (opens in a new tab), Model.Info (opens in a new tab)।
interface ProviderRecord {
readonly provider: Provider.Info
readonly models: ReadonlyMap<string, Model.Info>
readonly sourceConnection?: ConnectionInfo
}
interface ProviderEditor {
list(): readonly ProviderRecord[]
get(providerID: string): ProviderRecord | undefined
add(input: { info: Provider.Info; models: readonly Model.Info[]; sourceConnection?: ConnectionInfo }): void
update(providerID: string, update: (provider: Types.DeepMutable<Provider.Info>) => void): void
remove(providerID: string): void
readonly models: {
set(providerID: string, models: readonly Model.Info[]): void
update(providerID: string, modelID: string, update: (model: Types.DeepMutable<Model.Info>) => void): void
remove(providerID: string, modelID: string): void
}
}
interface ProviderDomain extends ProviderApi<unknown> {
readonly transform: Transform<ProviderEditor>
readonly reload: () => Effect.Effect<void>
}मॉडल
उपलब्ध मॉडल और डिफ़ॉल्ट चयन पढ़ें।
effect: (ctx) =>
Effect.gen(function* () {
const model = ctx.model
const models = yield* model.list().pipe(Effect.orDie)
const selected = yield* model.default().pipe(Effect.orDie)
yield* Effect.logInfo("models", { count: models.data.length, selected: selected.data?.name })
}),मॉडल ट्रांसफ़ॉर्म संपूर्ण सक्रिय-प्रोवाइडर उम्मीदवार संग्रह को संपादित करते हैं। सभी ट्रांसफ़ॉर्म समाप्त होने तक अक्षम उम्मीदवार संपादन योग्य रहते हैं; update केवल किसी उपलब्ध प्रोवाइडर के अंतर्गत ही मॉडल जोड़ सकता है।
effect: (ctx) =>
Effect.gen(function* () {
yield* ctx.model.transform((editor) => {
editor.list("anthropic").forEach((model) => {
editor.update(model.providerID, model.id, (draft) => {
draft.enabled = draft.capabilities.tools
})
})
editor.default.set("anthropic", "claude-sonnet-5-5")
editor.remove("anthropic", "legacy-model")
})
}),कॉलबैक रॉ मॉडल ओवरराइड्स को संपादित करते हैं। जब OpenCode परिणाम को कमिट करता है, तो प्रोवाइडर डिफ़ॉल्ट एक बार मर्ज किए जाते हैं; अपरिवर्तित रीड्स उसी परिणाम का पुनः उपयोग करते हैं। editor.provider मॉडल-ट्रांसफ़ॉर्म आउटपुट के बजाय निष्क्रिय टेम्पलेट्स सहित अपरिवर्तनीय स्रोत परिभाषाओं को पढ़ता है।
effect: (ctx) =>
Effect.gen(function* () {
const model = ctx.model
yield* model.transform((editor) => {
const source = editor.provider.get("anthropic")?.models.get("claude-sonnet-5-5")
if (!source) return
editor.update("anthropic", source.id, (draft) => {
draft.limit.context = Math.min(draft.limit.context, source.limit.context)
})
})
yield* model.reload()
}),स्कीमा: Model.Info (opens in a new tab)।
interface ModelEditor {
list(providerID?: string): readonly Types.DeepMutable<Model.Info>[]
get(providerID: string, modelID: string): Types.DeepMutable<Model.Info> | undefined
update(providerID: string, modelID: string, update: (model: Types.DeepMutable<Model.Info>) => void): void
remove(providerID: string, modelID: string): void
readonly default: {
get(): { providerID: string; modelID: string } | undefined
set(providerID: string, modelID: string): void
}
readonly provider: {
list(): readonly ProviderRecord[]
get(providerID: string): ProviderRecord | undefined
}
}
interface ModelDomain extends ModelApi<unknown> {
readonly transform: Transform<ModelEditor>
readonly reload: () => Effect.Effect<void>
}कमांड
वर्तमान स्थान पर उपलब्ध कमांड पढ़ें।
effect: (ctx) =>
Effect.gen(function* () {
const command = ctx.command
const commands = yield* command.list().pipe(Effect.orDie)
yield* Effect.logInfo("commands", { names: commands.data.map((item) => item.name) })
}),वर्तमान कमांड ट्रांसफ़ॉर्म केवल जोड़ने (add-only) के लिए है। एक निष्पादक (executor) को सेशन, प्रॉम्प्ट अटैचमेंट और डिलीवरी मोड प्राप्त होते हैं।
effect: (ctx) =>
Effect.gen(function* () {
const command = ctx.command
const session = ctx.session
yield* command.transform((editor) => {
editor.add({
name: "security-review",
description: "Review changes for security issues",
execute: (input) =>
session.prompt({
...input.prompt,
sessionID: input.sessionID,
text: `Review these changes for security issues.\n\n${input.prompt.text}`,
delivery: input.delivery,
}).pipe(Effect.asVoid),
})
})
yield* command.reload()
}),स्कीमा: Command.Info (opens in a new tab), Session.Inbox.Delivery (opens in a new tab)।
interface CommandInvocation {
readonly sessionID: Session.ID
readonly prompt: PromptInput.Prompt
readonly delivery: SessionInbox.Delivery
}
interface CommandDefinition {
readonly name: string
readonly description?: string
readonly execute: (input: CommandInvocation) => Effect.Effect<void, unknown>
}
interface CommandEditor {
add(definition: CommandDefinition): void
}
interface CommandDomain extends Pick<CommandApi<unknown>, "list"> {
readonly transform: Transform<CommandEditor>
readonly reload: () => Effect.Effect<void>
}इंटीग्रेशन
इंटीग्रेशन पढ़ें और यदि सक्रिय क्रेडेंशियल मौजूद हो तो उसे रिज़ॉल्व करें।
effect: (ctx) =>
Effect.gen(function* () {
const integration = ctx.integration
const integrations = yield* integration.list().pipe(Effect.orDie)
const github = yield* integration.get({ integrationID: Integration.ID.make("github") }).pipe(Effect.orDie)
const connection = yield* integration.connection.active("github")
const credential = connection ? yield* integration.connection.resolve(connection).pipe(Effect.orDie) : undefined
yield* Effect.logInfo("integration", {
count: integrations.data.length,
github: github.data.name,
credential: credential?.type,
})
}),किसी कुंजी के साथ कनेक्ट करें या कनेक्टेड API के माध्यम से OAuth प्रयास संचालित करें।
effect: (ctx) =>
Effect.gen(function* () {
const integration = ctx.integration
const token = yield* Config.Redacted("GITHUB_TOKEN").pipe(Effect.orDie)
yield* integration.connect
.key({ integrationID: Integration.ID.make("github"), key: Redacted.value(token) })
.pipe(Effect.orDie)
const attempt = yield* integration.oauth
.connect({
integrationID: Integration.ID.make("acme"),
methodID: Integration.MethodID.make("oauth"),
})
.pipe(Effect.orDie)
const status = yield* integration.oauth
.status({
integrationID: Integration.ID.make("acme"),
attemptID: attempt.data.attemptID,
})
.pipe(Effect.orDie)
yield* Effect.logInfo("OAuth status", { status: status.data })
}),इंटीग्रेशन और प्रमाणीकरण विधियों को ट्रांसफ़ॉर्म करें। OAuth विधि कॉलबैक Effects होते हैं और स्कोप्ड संसाधन प्राप्त कर सकते हैं।
effect: (ctx) =>
Effect.gen(function* () {
const integration = ctx.integration
yield* integration.transform((editor) => {
editor.update("acme", (item) => (item.name = "Acme"))
editor.method.update({
integrationID: "acme",
method: { id: "device", type: "oauth", label: "Sign in with Acme" },
authorize: () =>
Effect.succeed({
mode: "code",
url: "https://acme.example/device",
instructions: "Enter the displayed code",
callback: (code) => exchangeCode(code),
}),
})
})
yield* integration.reload()
}),स्कीमा: Integration.Info (opens in a new tab), Integration.Method (opens in a new tab), Connection.Info (opens in a new tab), Form.Answer (opens in a new tab)।
type IntegrationOAuthAuthorization = {
readonly url: string
readonly instructions: string
readonly expiresAt?: number
} & (
| { readonly mode: "auto"; readonly callback: Effect.Effect<Credential.OAuth, unknown> }
| { readonly mode: "code"; readonly callback: (code: string) => Effect.Effect<Credential.OAuth, unknown> }
)
type IntegrationOAuthMethodRegistration = {
readonly integrationID: string
readonly method: IntegrationOAuthMethod
readonly authorize: (answer: Form.Answer) => Effect.Effect<IntegrationOAuthAuthorization, unknown, Scope.Scope>
readonly refresh?: (credential: Credential.OAuth) => Effect.Effect<Credential.OAuth, unknown>
readonly label?: (credential: Credential.OAuth) => string | undefined
}
interface IntegrationEditor {
list(): readonly IntegrationRef[]
get(id: string): IntegrationRef | undefined
update(id: string, update: (integration: IntegrationRef) => void): void
remove(id: string): void
readonly method: {
list(integrationID: string): readonly IntegrationMethod[]
update(input: IntegrationMethodRegistration): void
remove(integrationID: string, method: IntegrationMethod): void
}
}
interface IntegrationDomain extends Omit<IntegrationApi<unknown>, "wellknown"> {
readonly transform: Transform<IntegrationEditor>
readonly reload: () => Effect.Effect<void>
readonly connection: {
readonly active: (integrationID: string) => Effect.Effect<ConnectionInfo | undefined>
readonly resolve: (connection: ConnectionInfo) => Effect.Effect<Credential.Value | undefined, unknown>
}
}MCP
MCP सर्वर और उनकी वर्तमान कनेक्शन स्थिति को सूचीबद्ध करें।
effect: (ctx) =>
Effect.gen(function* () {
const mcp = ctx.mcp
const servers = yield* mcp.list().pipe(Effect.orDie)
yield* Effect.logInfo("MCP servers", { count: servers.data.length })
}),प्लगइन केवल ट्रांसफ़ॉर्म के माध्यम से MCP सर्वर प्रबंधित करते हैं। किसी सर्वर को जोड़ने या बदलने के लिए editor.set, उसका कॉन्फ़िगरेशन बदलने के लिए editor.update, और उसे हटाने के लिए editor.remove का उपयोग करें। editor.list और editor.get के साथ कॉन्फ़िगरेशन का निरीक्षण करें।
effect: (ctx) =>
Effect.gen(function* () {
const mcp = ctx.mcp
yield* mcp.transform((editor) => {
const servers = editor.list()
const docs = editor.get("docs")
editor.set("docs", { type: "remote", url: "https://mcp.example.com" })
editor.update("docs", (server) => (server.disabled = false))
editor.remove("legacy")
})
}),किसी सर्वर को अक्षम करने और डिस्कनेक्ट करने के लिए ट्रांसफ़ॉर्म में disabled: true सेट करें, या इसे सक्षम करने और OpenCode को कनेक्ट करने की अनुमति देने के लिए disabled: false सेट करें। OpenCode ट्रांसफ़ॉर्म किए गए कॉन्फ़िगरेशन से सर्वर के जीवनकाल का सामंजस्य (reconcile) करता है।
ट्रांसफ़ॉर्म द्वारा उपयोग की जाने वाली बाहरी स्टेट बदलने के बाद ट्रांसफ़ॉर्म को फिर से लागू करने और सर्वर का सामंजस्य करने के लिए reload() कॉल करें।
effect: (ctx) =>
Effect.gen(function* () {
const mcp = ctx.mcp
yield* mcp.reload()
}),स्कीमा: Mcp.Server (opens in a new tab), Mcp.LocalConfigEncoded (opens in a new tab), Mcp.RemoteConfigEncoded (opens in a new tab)।
interface MCPEditor {
list(): readonly [string, Types.DeepMutable<Mcp.ServerConfig>][]
get(name: string): Types.DeepMutable<Mcp.ServerConfig> | undefined
set(name: string, config: Mcp.ServerConfig): void
update(name: string, update: (config: Types.DeepMutable<Mcp.ServerConfig>) => void): void
remove(name: string): void
}
interface MCPDomain extends Pick<McpApi<unknown>, "list"> {
readonly transform: Transform<MCPEditor>
readonly reload: () => Effect.Effect<void>
}प्लगइन
वर्तमान स्थान पर सक्रिय, विफल और रिज़ॉल्व किए गए प्लगइन को सूचीबद्ध करें।
effect: (ctx) =>
Effect.gen(function* () {
const plugin = ctx.plugin
const plugins = yield* plugin.list().pipe(Effect.orDie)
yield* Effect.forEach(plugins.data, (item) => Effect.logInfo("plugin", item), { discard: true })
}),जब केवल सक्रिय प्लगइन मायने रखते हों, तो Effect पाइपलाइन में स्कीमा मानों को फ़िल्टर करें।
effect: (ctx) =>
Effect.gen(function* () {
const plugin = ctx.plugin
const active = yield* plugin.list().pipe(
Effect.orDie,
Effect.map((result) => result.data.filter((item) => item.state.status === "active")),
)
yield* Effect.logInfo("active plugin count", { count: active.length })
}),स्कीमा: Plugin.Info (opens in a new tab), Plugin.Source (opens in a new tab)।
interface PluginApi<E = never> {
readonly list: PluginListOperation<E>
}
interface Context {
readonly plugin: PluginApi<unknown>
}रेफरेंस
वर्तमान स्थान पर उपलब्ध रेफरेंस पढ़ें।
effect: (ctx) =>
Effect.gen(function* () {
const reference = ctx.reference
const references = yield* reference.list().pipe(Effect.orDie)
yield* Effect.logInfo("references", { count: references.data.length })
}),स्थानीय और Git रेफरेंस जोड़ें या हटाएँ, फिर बाहरी स्टेट बदलने के बाद रीलोड करें। get(name) वर्तमान कॉन्फ़िगर किया गया स्रोत लौटाता है, या जब नाम अनुपस्थित होता है तो undefined लौटाता है।
effect: (ctx) =>
Effect.gen(function* () {
const reference = ctx.reference
yield* reference.transform((editor) => {
editor.add("handbook", { type: "local", path: "/workspace/docs/handbook" })
editor.add("standards", { type: "git", repository: "https://github.com/acme/standards", branch: "main" })
const handbook = editor.get("handbook")
editor.remove("legacy")
})
yield* reference.reload()
}),स्कीमा: Reference.Info (opens in a new tab), Reference.LocalSource (opens in a new tab), Reference.GitSource (opens in a new tab)।
interface ReferenceEditor {
add(name: string, source: ReferenceLocalSource | ReferenceGitSource): void
remove(name: string): void
list(): readonly (readonly [string, ReferenceLocalSource | ReferenceGitSource])[]
get(name: string): ReferenceLocalSource | ReferenceGitSource | undefined
}
interface ReferenceDomain extends ReferenceApi<unknown> {
readonly transform: Transform<ReferenceEditor>
readonly reload: () => Effect.Effect<void>
}सेशन
कोई सेशन बनाएँ या पढ़ें, फिर बाद के कार्य में उपयोग किए जाने वाले एजेंट और मॉडल का चयन करें।
effect: (ctx) =>
Effect.gen(function* () {
const session = ctx.session
const created = yield* session.create({ title: "Review" }).pipe(Effect.orDie)
const current = yield* session.get({ sessionID: created.id }).pipe(Effect.orDie)
yield* session.switchAgent({ sessionID: current.id, agent: Agent.ID.make("build") }).pipe(Effect.orDie)
yield* session
.switchModel({
sessionID: current.id,
model: { providerID: Provider.ID.make("anthropic"), id: Model.ID.make("claude-sonnet-5-5") },
})
.pipe(Effect.orDie)
}),प्रॉम्प्ट, अस्थायी जनरेशन अनुरोध, कमांड या सिंथेटिक संदेश भेजें।
effect: (ctx) =>
Effect.gen(function* () {
const session = ctx.session
const created = yield* session.create({ title: "Automation" }).pipe(Effect.orDie)
yield* session.prompt({ sessionID: created.id, text: "Review the current changes" }).pipe(Effect.orDie)
const summary = yield* session
.generate({ sessionID: created.id, prompt: "Summarize this project" })
.pipe(Effect.orDie)
yield* session.command({ sessionID: created.id, name: "review", text: "--staged" }).pipe(Effect.orDie)
yield* session
.synthetic({ sessionID: created.id, text: `Summary generated: ${summary.text}`, resume: false })
.pipe(Effect.orDie)
}),एक बार में एक पृष्ठ के रूप में सेशन सूचीबद्ध करें। अगला पृष्ठ प्राप्त करने के लिए, cursor.next को cursor के रूप में वापस पास करें। कर्सर आपके फ़िल्टर याद रखता है, इसलिए आपको उन्हें दोहराने की आवश्यकता नहीं है।
effect: (ctx) =>
Effect.gen(function* () {
const page = yield* ctx.session.list({ directory: ctx.location.directory, limit: 20 }).pipe(Effect.orDie)
const next = page.cursor.next
? yield* ctx.session.list({ cursor: page.cursor.next, limit: 20 }).pipe(Effect.orDie)
: undefined
}),remove के साथ किसी अस्थायी सेशन को हटाएँ। हटाने से सक्रिय निष्पादन रुक जाता है और चाइल्ड सेशन पुनरावर्ती रूप से (recursively) हटा दिए जाते हैं।
effect: (ctx) =>
Effect.gen(function* () {
const created = yield* ctx.session.create({ title: "Temporary worker" }).pipe(Effect.orDie)
yield* ctx.session.remove({ sessionID: created.id }).pipe(Effect.orDie)
}),compact के साथ कॉम्पैक्शन का अनुरोध करें। यह स्वीकृत इनबॉक्स आइटम लौटाता है, न कि पूर्ण सारांश; कॉम्पैक्शन डिफ़ॉल्ट रूप से अगली चरण सीमा (step boundary) पर चलता है। इसके बजाय कतार क्रम (queue order) बनाए रखने के लिए delivery: "queue" पास करें।
effect: (ctx) =>
Effect.gen(function* () {
const created = yield* ctx.session.create({ title: "Review" }).pipe(Effect.orDie)
const compaction = yield* ctx.session.compact({ sessionID: created.id }).pipe(Effect.orDie)
}),instructions.entry के साथ कुंजी-आधारित (keyed) सेशन निर्देश प्रविष्टियाँ जोड़ें, सूचीबद्ध करें या हटाएँ। प्रविष्टियाँ सेशन के निर्देशों में <context key="..."> ब्लॉक के रूप में रेंडर होती हैं, और संदेश टर्न को कतार में लगाए बिना अगली चरण सीमा पर अपडेट या निष्कासन की घोषणा की जाती है।
effect: (ctx) =>
Effect.gen(function* () {
const created = yield* ctx.session.create({ title: "Review" }).pipe(Effect.orDie)
yield* ctx.session.instructions.entry
.put({ sessionID: created.id, key: "deploy-target", value: "production" })
.pipe(Effect.orDie)
const entries = yield* ctx.session.instructions.entry.list({ sessionID: created.id }).pipe(Effect.orDie)
yield* ctx.session.instructions.entry.remove({ sessionID: created.id, key: "deploy-target" }).pipe(Effect.orDie)
}),स्कीमा: Session.Info (opens in a new tab), Model.Ref (opens in a new tab), Session.Inbox.User (opens in a new tab), Session.Inbox.Synthetic (opens in a new tab), InstructionEntry.Info (opens in a new tab)।
type SessionDomain = Pick<
SessionApi<unknown>,
| "list"
| "create"
| "get"
| "remove"
| "switchAgent"
| "switchModel"
| "prompt"
| "generate"
| "command"
| "compact"
| "synthetic"
| "interrupt"
| "update"
| "move"
| "wait"
| "context"
| "instructions"
>स्किल
वर्तमान स्थान पर स्किल पढ़ें।
effect: (ctx) =>
Effect.gen(function* () {
const skill = ctx.skill
const skills = yield* skill.list().pipe(Effect.orDie)
yield* Effect.logInfo("skills", { ids: skills.data.map((item) => item.id) })
}),स्किल को ट्रांसफ़ॉर्म और रीलोड करें। ब्रांडेड मानों के लिए पुनः-एक्सपोर्ट किए गए Effect स्कीमा कंस्ट्रक्टर का उपयोग करें। get(id) वर्तमान एडिटर प्रविष्टि लौटाता है, या जब स्किल अनुपस्थित हो तो undefined लौटाता है।
effect: (ctx) =>
Effect.gen(function* () {
const skill = ctx.skill
yield* skill.transform((editor) => {
editor.add(Skill.Info.make({
id: Skill.ID.make("review"),
name: Skill.Name.make("Review"),
description: "Review the current changes",
path: Skill.Info.fields.path.make("/workspace/.opencode/skills/review/SKILL.md"),
content: "Review the current changes for correctness and missing tests.",
}))
const review = editor.get("review")
editor.update("review", (item) => (item.autoinvoke = true))
editor.remove("legacy")
})
yield* skill.reload()
}),स्कीमा: Skill.Info (opens in a new tab)।
interface SkillEditor {
list(): readonly Types.DeepMutable<Skill.Info>[]
get(id: string): Types.DeepMutable<Skill.Info> | undefined
add(skill: Skill.Info): void
update(id: string, update: (skill: Types.DeepMutable<Skill.Info>) => void): void
remove(id: string): void
}
interface SkillDomain extends SkillApi<unknown> {
readonly transform: Transform<SkillEditor>
readonly reload: () => Effect.Effect<void>
}स्टोरेज
प्लगइन ID तक सीमित स्थायी (durable) JSON मानों को संग्रहीत करें, पढ़ें और हटाएँ।
effect: (ctx) =>
Effect.gen(function* () {
const storage = ctx.storage
yield* storage.set("settings", { strict: true })
const settings = yield* storage.get("settings")
yield* Effect.logInfo("settings", { settings })
yield* storage.remove("settings")
}),कर्सर पेजिनेशन के साथ उपसर्ग (prefix) द्वारा कुंजियों को स्कैन करें।
effect: (ctx) =>
Effect.gen(function* () {
const storage = ctx.storage
const first = yield* storage.scan({ prefix: "cache/", limit: 100 })
const second = first.next
? yield* storage.scan({ prefix: "cache/", after: first.next, limit: 100 })
: { entries: [] }
yield* Effect.logInfo("cache entries", { count: first.entries.length + second.entries.length })
}),स्टोरेज Effect के JSON प्रकार को स्वीकार करता है और सीधे Effects लौटाता है।
interface StorageDomain {
readonly get: (key: string) => Effect.Effect<Schema.Json | undefined>
readonly set: (key: string, value: Schema.Json) => Effect.Effect<void>
readonly remove: (key: string) => Effect.Effect<void>
readonly scan: (options: StorageScanOptions) => Effect.Effect<StorageScanResult>
}टूल
Effect Schema के साथ टाइप्ड टूल रजिस्टर करें। निष्पादक (executor) डिकोड किया गया इनपुट प्राप्त करता है और टाइप्ड आउटपुट, प्रदर्शन सामग्री या मेटाडेटा वाला एक Effect लौटाता है।
एडिटर add, update और remove का समर्थन करता है। ट्रांसफ़ॉर्म कॉलबैक सिंक्रोनस होता है: इसे Effect या Promise नहीं लौटाना चाहिए। रजिस्टर करने या रीलोड करने से पहले बाहरी डेटा लोड करें। OpenCode एक नए एडिटर पर रजिस्ट्रेशन के क्रम में सक्रिय ट्रांसफ़ॉर्म को फिर से चलाता है; समान प्रभावी टूल नाम के लिए, बाद का वैध रजिस्ट्रेशन पहले वाले को ओवरराइड कर देता है।
effect: (ctx) =>
Effect.gen(function* () {
const tool = ctx.tool
yield* tool.transform((editor) => {
editor.namespace({
name: "acme",
description: "Customer account tools",
})
editor.add({
name: "greeting",
description: "Create a greeting",
input: Schema.Struct({ name: Schema.String }),
output: Schema.Struct({ greeting: Schema.String }),
options: { namespace: "acme", codemode: true },
execute: ({ name }, context) =>
Effect.gen(function* () {
yield* context.progress({ status: "greeting" })
return { output: { greeting: `Hello ${name}!` } }
}),
})
})
}),कॉलबैक द्वारा कैप्चर किए गए स्रोत डेटा को बदलने के बाद yield* ctx.tool.reload() कॉल करें। रीलोड सक्रिय ट्रांसफ़ॉर्म को उनका क्रम बदले बिना फिर से चलाता है; यह प्लगइन इफ़ेक्ट को दोबारा नहीं चलाता है।
Effect टूल वही फ्रीफ़ॉर्म इनपुट घोषणा स्वीकार करते हैं जो Promise टूल करते हैं। यदि मॉडल उस फ़ॉर्मेट की घोषणा नहीं करता है, तो टूल अपना सामान्य ऑब्जेक्ट इनपुट बनाए रखता है।
टूल में एक id होता है जिसमें उनका प्रभावी नाम शामिल होता है, और जब वह ID मौजूद न हो तो get() undefined लौटाता है। update और remove का उपयोग प्रभावी टूल नाम के साथ करें, जिसमें उसका नेमस्पेस भी शामिल हो (ऊपर acme_greeting)। नेमस्पेस में डॉट्स और टूल नामों में असमर्थित वर्ण _ बन जाते हैं। अनुपस्थित नामों को अनदेखा कर दिया जाता है; टूल बनाने के लिए पूर्ण परिभाषा के साथ add की आवश्यकता होती है। अपडेट नाम और नेमस्पेस को सुरक्षित रखते हैं। नेस्टेड मानों को म्यूटेट करने के बजाय उन्हें प्रतिस्थापित करने के लिए नए स्कीमा या विकल्प असाइन करें। अमान्य अपडेट लॉग किए जाते हैं और पिछली परिभाषा को यथावत छोड़ देते हैं।
{/* prettier-ignore */}
yield* ctx.tool.transform((editor) => {
editor.update("acme_greeting", (tool) => {
tool.description = "Greet the user by name"
})
editor.remove("acme_obsolete")
})अपडेट और निष्कासन, अतिरिक्त जोड़ (additions) के साथ क्रम में फिर से चलते हैं, जिसमें MCP कैटलॉग रीफ़्रेश के बाद भी शामिल है।
transform एक स्कोप्ड रजिस्ट्रेशन लौटाता है। इसके ट्रांसफ़ॉर्म को हटाने और शेष ट्रांसफ़ॉर्म से पुनर्निर्माण करने के लिए yield* registration.dispose चलाएँ, जिससे कोई भी पिछली परिभाषा जिसे इसने ओवरराइड किया था, फिर से प्रकट हो जाती है। डिस्पोज़ल आइडेम्पोटेंट (idempotent) है, और प्लगइन स्कोप को बंद करने से उसके रजिस्ट्रेशन भी डिस्पोज़ हो जाते हैं।
प्रत्येक मॉडल अनुरोध एक टूल स्नैपशॉट कैप्चर करता है। रीलोड और डिस्पोज़ल भविष्य के स्नैपशॉट को प्रभावित करते हैं, न कि किसी मौजूदा अनुरोध द्वारा पहले से कैप्चर की गई परिभाषाओं या निष्पादकों को। जो निष्पादक म्यूटेबल प्लगइन डेटा पर क्लोज़ ओवर करते हैं, वे अब भी उस डेटा को देखते हैं; जब किसी मान को उसी परिभाषा से जुड़ा रहना आवश्यक हो, तो उसे ट्रांसफ़ॉर्म के भीतर कैप्चर करें।
स्कीमा: Tool.Content (opens in a new tab), Tool.TextContent (opens in a new tab), Tool.FileContent (opens in a new tab)।
interface ToolEditor {
list(): readonly (Tool.Info & { readonly id: string })[]
get(id: string): (Tool.Info & { readonly id: string }) | undefined
namespace(namespace: { name: string; description: string }): void
add<Input extends Tool.ValueSchema<any>, Output extends Tool.ValueSchema<any> | undefined>(
tool: Tool.Info<Input, Output>,
): void
update(id: string, update: (tool: Types.Mutable<Tool.Info>) => void): void
remove(id: string): void
}
interface ToolDomain {
readonly list: () => Effect.Effect<readonly (Tool.Info & { readonly id: string })[]>
readonly transform: Transform<ToolEditor>
readonly reload: () => Effect.Effect<void>
}VCS
रिपॉजिटरी जानकारी, वर्किंग-कॉपी स्थिति या फ़ाइल डिफ़ पढ़ें।
effect: (ctx) =>
Effect.gen(function* () {
const vcs = ctx.vcs
const info = yield* vcs.get().pipe(Effect.orDie)
const branches = yield* vcs.branch.list({ search: "feature", limit: 10 }).pipe(Effect.orDie)
const changes = yield* vcs.status().pipe(Effect.orDie)
const diff = yield* vcs.diff({ mode: "working", context: 3 }).pipe(Effect.orDie)
yield* Effect.logInfo("vcs", { branch: info.data.branch.current, files: changes.data.length })
}),स्कोप्ड ट्रांसफ़ॉर्म के माध्यम से स्थान-स्कोप्ड (location-scoped) प्रोवाइडर रजिस्टर करें। पहचाने गए रिपॉजिटरी प्रकार से मेल खाने वाले प्रोवाइडर स्वचालित रूप से चुने जाते हैं; कोई भिन्न प्रोवाइडर चुनने के लिए editor.default.set का उपयोग करें।
effect: (ctx) =>
Effect.gen(function* () {
const vcs = ctx.vcs
yield* vcs.transform((editor) => {
editor.add({
id: "custom",
name: "Custom VCS",
info: () => Effect.succeed({ branch: { current: "feature", default: "main" } }),
branches: (input) => readBranches(input),
status: (scope) => readStatus(scope.worktree),
diff: (input) => readDiff(input),
})
editor.default.set("custom")
})
}),प्रोवाइडर कॉलबैक को वर्तमान स्थान, वर्किंग-कॉपी रूट, कैनोनिकल प्रोजेक्ट रूट और वैकल्पिक रिपॉजिटरी स्टोर प्राप्त होते हैं। डिफ़ कॉलबैक को चयनित मोड, अनुरोधित कॉन्टेक्स्ट और आउटपुट बाइट बजट भी प्राप्त होते हैं। रिपॉजिटरी डिस्कवरी OpenCode के बिल्ट-इन Git और Mercurial डिटेक्टरों का उपयोग करना जारी रखती है।
प्रोवाइडर scope.worktree में रिपॉजिटरी बनाने के लिए वैकल्पिक रूप से init(scope) लागू कर सकते हैं। इनिशियलाइज़ेशन पहचाने गए रिपॉजिटरी प्रकार के बजाय ID द्वारा प्रोवाइडर का चयन करता है (डिफ़ॉल्ट रूप से Git)। जब स्थान में अभी तक कोई रिपॉजिटरी न हो, तब भी init के साथ एक प्रोवाइडर रजिस्टर करें; OpenCode इसे कॉल करने से पहले जांचता है कि डायरेक्टरी मौजूद है और उसमें कोई रिपॉजिटरी नहीं है, फिर प्रोजेक्ट की फिर से खोज करता है। नए रिपॉजिटरी फ़ॉर्मेट को इस कॉलबैक के अलावा अब भी डिस्कवरी सहायता की आवश्यकता होती है।
स्कीमा: Vcs.Info (opens in a new tab), Vcs.FileStatus (opens in a new tab), FileDiff.Info (opens in a new tab)।
interface VcsEditor {
add(definition: VcsDefinition): void
readonly default: {
get(): string | undefined
set(selection: string): void
}
}
interface VcsDomain extends Omit<VcsApi<unknown>, "init"> {
readonly transform: Transform<VcsEditor>
readonly reload: () => Effect.Effect<void>
}
interface VcsDefinition {
readonly init?: (scope: VcsScope) => Effect.Effect<void, unknown>
// Existing info, branches, status, and diff callbacks are required.
}वेब खोज
प्रोवाइडर सूचीबद्ध करें या चयनित प्रोवाइडर के माध्यम से क्वेरी चलाएँ।
effect: (ctx) =>
Effect.gen(function* () {
const websearch = ctx.websearch
const providers = yield* websearch.providers().pipe(Effect.orDie)
const results = yield* websearch
.query({ query: "OpenCode plugins", providerID: WebSearch.ID.make("internal") })
.pipe(Effect.orDie)
yield* Effect.logInfo("websearch", { providers: providers.data.length, results: results.data.results.length })
}),एक Effect निष्पादक (executor) रजिस्टर करें और डिफ़ॉल्ट प्रोवाइडर चुनें। वेब खोज को अक्षम करने के लिए डिफ़ॉल्ट को false पर सेट करें।
effect: (ctx) =>
Effect.gen(function* () {
const websearch = ctx.websearch
yield* websearch.transform((editor) => {
editor.add({
id: "internal",
name: "Internal search",
execute: ({ query }) => searchInternal(query),
})
editor.default.set("internal")
})
yield* websearch.reload()
}),स्कीमा: WebSearch.Provider (opens in a new tab), WebSearch.Result (opens in a new tab)।
interface WebSearchDefinition {
readonly id: string
readonly name: string
readonly execute: (input: WebSearch.ProviderInput) => Effect.Effect<readonly WebSearch.Result[], unknown>
}
interface WebSearchEditor {
add(definition: WebSearchDefinition): void
readonly default: {
get(): string | false | undefined
set(selection: string | false): void
}
}
interface WebSearchDomain extends WebsearchApi<unknown> {
readonly transform: Transform<WebSearchEditor>
readonly reload: () => Effect.Effect<void>
}घटनाएँ
सार्वजनिक सर्वर घटना सब्सक्रिप्शन एक Effect Stream है। स्वचालित व्यवधान (automatic interruption) के लिए इसके उपभोक्ता (consumer) को प्लगइन स्कोप में फ़ॉर्क करें।
effect: (ctx) =>
Effect.gen(function* () {
const event = ctx.event
yield* event.subscribe().pipe(
Stream.tap((item) => Effect.logDebug("OpenCode event", { type: item.type })),
Stream.runDrain,
Effect.forkScoped,
)
}),केवल उन्हीं घटना प्रकारों को प्राप्त करने के लिए types पास करें जिनकी आपको आवश्यकता है। स्ट्रीम मिलान के अनुसार टाइप्ड होती है, इसलिए किसी टाइप गार्ड की आवश्यकता नहीं होती है।
effect: (ctx) =>
Effect.gen(function* () {
const event = ctx.event
yield* event.subscribe({ types: ["config.updated"] }).pipe(
Stream.runForEach(() => Effect.logInfo("configuration changed")),
Effect.forkScoped,
)
}),स्कीमा: V2EventEncoded (opens in a new tab)।
type EventType = Exclude<OpenCodeEvent["type"], "server.connected">
interface EventDomain {
readonly subscribe: <Type extends EventType = EventType>(input?: {
readonly types?: ReadonlyArray<Type>
}) => Stream.Stream<Extract<OpenCodeEvent, { type: Type }>, unknown>
}हुक
हुक लाइव ऑपरेशनों को इंटरसेप्ट करते हैं। कई प्लगइन एक ही हुक रजिस्टर कर सकते हैं; OpenCode उन्हें प्लगइन के क्रम में चलाता है ताकि बाद के हुक पहले के बदलाव देख सकें। रजिस्ट्रेशन प्लगइन स्कोप तक सक्रिय रहते हैं।
effect: (ctx) =>
Effect.gen(function* () {
const session = ctx.session
yield* session.hook("context", () => Effect.void)
}),सेशन
मॉडल डिस्पैच से ठीक पहले असेंबल किए गए सिस्टम निर्देशों, संदेशों या टूल को संशोधित करें। context एजेंट लूप के लिए चलता है; compaction, generate और title उन सहायक अनुरोधों के लिए चलते हैं। compaction और title एक result स्वीकार करते हैं जो मॉडल कॉल को छोड़ देता है।
effect: (ctx) =>
Effect.gen(function* () {
const session = ctx.session
yield* session.hook("context", (event) =>
Effect.sync(() => {
event.system.push({ type: "text", text: "Keep the review focused on correctness." })
delete event.tools.write
}),
)
yield* session.hook("compaction", (event) =>
Effect.map(summarize(event.messages), (summary) => {
event.result = { summary }
}),
)
}),context, compaction और generate हुक में संदेश अपने संग्रहीत संदेश का केवल-पढ़ने योग्य (read-only) metadata रखते हैं (उदाहरण के लिए, जो prompt हुक ने सेट किया हो)। इसे मॉडल को नहीं भेजा जाता है।
OpenCode के निर्देशों को रखने वाला system भाग metadata.experimental.instructions में प्रत्येक स्रोत को { key, text } के रूप में भी सूचीबद्ध करता है, जैसे core/date या core/environment, ताकि कोई हुक स्रोतों के बीच अंतर कर सके। यह सूची एक प्रतिलिपि है: इस भाग के text को संपादित करने से यह अपडेट नहीं होती है। यह प्रायोगिक (experimental) है और बदल सकता है।
मॉडल अनुरोध सेटिंग्स को संशोधित करें और वैकल्पिक रूप से हुक को किसी एक प्रोवाइडर तक सीमित करें। घटना में नीचे दिए गए HTTP हुक जैसा ही kind होता है।
effect: (ctx) =>
Effect.gen(function* () {
const session = ctx.session
yield* session.hook(
"model.request",
(event) => Effect.sync(() => (event.headers["x-plugin"] = "review")),
{ providerID: "anthropic" },
)
}),नेटिव प्रोवाइडर अनुरोधों या प्रतिक्रियाओं को संशोधित करें। उनकी बॉडी वन-शॉट स्ट्रीम होती हैं; किसी बॉडी को पढ़ने से पहले उसे क्लोन या प्रतिस्थापित करें। दोनों हुक सेशन द्वारा जारी किए गए प्रत्येक अनुरोध के लिए चलते हैं; यह इस बात पर निर्भर करते हुए कि किस फ़्लो ने इसे जारी किया है, event.kind का मान "primary", "compaction", "title" या "generate" होता है।
effect: (ctx) =>
Effect.gen(function* () {
const session = ctx.session
yield* session.hook("http.request", (event) =>
Effect.sync(() => {
event.request.headers.set("x-session-id", event.sessionID)
if (event.kind === "title") event.request.headers.set("x-priority", "background")
}),
)
yield* session.hook("http.response", (event) =>
Effect.sync(() => {
event.response = new Response(event.response.body, {
status: event.response.status,
headers: { ...Object.fromEntries(event.response.headers), "x-plugin": "review" },
})
}),
)
}),जो प्रोवाइडर WebSocket पर स्ट्रीम करते हैं, वे प्रति सेशन एक कनेक्शन का पुनः उपयोग करते हैं, इसलिए HTTP हुक उस ट्रैफ़िक को कभी नहीं देखते हैं। प्रायोगिक experimental.ws.handshake हुक कनेक्शन चुने जाने से पहले प्रति मॉडल कॉल एक बार चलता है और कनेक्शन url तथा headers को उजागर करता है; दोनों में से किसी को भी बदलने पर सॉकेट फिर से खुल जाता है। WebSocket रूट जिस किसी भी अनुरोध पर फ़ॉलबैक करता है, उसके लिए HTTP हुक अब भी चलते हैं।
effect: (ctx) =>
Effect.gen(function* () {
yield* ctx.session.hook(
"experimental.ws.handshake",
(event) =>
Effect.gen(function* () {
event.headers.authorization = `Bearer ${yield* mintToken(event.url)}`
delete event.headers["api-key"]
}),
{ providerID: "azure" },
)
}),experimental.ws.send और experimental.ws.receive स्वयं फ़्रेम को उजागर करते हैं: send तब चलता है जब प्रोवाइडर ड्राइवर एक आउटबाउंड फ़्रेम बना लेता है, और receive ड्राइवर द्वारा देखे जाने से पहले प्रत्येक इनबाउंड फ़्रेम पर चलता है। हुक के लौटने पर frame में जो कुछ भी होता है, वही नेटवर्क पर जाता है या ड्राइवर तक पहुँचता है; OpenCode इसे मान्य (validate) नहीं करता है।
effect: (ctx) =>
Effect.gen(function* () {
yield* ctx.session.hook(
"experimental.ws.send",
(event) =>
Effect.sync(() => {
const body = JSON.parse(event.frame)
if (body.type === "response.create") body.metadata = { ...body.metadata, session: event.sessionID }
event.frame = JSON.stringify(body)
}),
{ providerID: "openai" },
)
}),प्रोवाइडर विफलता के लिए पुनः प्रयास (retry) निर्णय को ओवरराइड करें या मिलीसेकंड में इसके विलंब को बदलें। यह हुक OpenCode द्वारा विफलता को वर्गीकृत करने और अपनी नीति प्रस्तावित करने के बाद, लेकिन किसी भी पुनः प्रयास को शेड्यूल करने से पहले चलता है। यह यह उजागर नहीं करता कि OpenCode आंतरिक रूप से अगला प्रयास कैसे करता है।
effect: (ctx) =>
Effect.gen(function* () {
yield* ctx.session.hook("retry", (event) =>
Effect.sync(() => {
if (event.error.status === 429) {
event.decision = { retry: true, delay: 10_000 }
return
}
if (event.error.type === "provider.invalid-request" && event.attempt === 2) {
event.decision = { retry: true, delay: 0 }
return
}
if (event.attempt >= 3) event.decision = { retry: false }
}),
)
}),प्रारंभिक decision OpenCode की नीति होती है, इसलिए हुक सामान्यतः टर्मिनल प्रोवाइडर विफलता को पुनः प्रयास योग्य बना सकते हैं या प्रस्तावित पुनः प्रयास पर वीटो लगा सकते हैं। कई हुक रजिस्ट्रेशन के क्रम में चलते हैं और बाद के हुक वर्तमान निर्णय देखते हैं। बिल्ट-इन अधिकतम प्रयास संख्या एक कठोर सीमा बनी रहती है। attempt वह वास्तविक प्रयास है जिसका प्रस्ताव किया जा रहा है; प्रारंभिक अनुरोध प्रयास 1 होता है, इसलिए पहला पुनः प्रयास प्रयास 2 होता है। अमान्य विलंब (NaN, अनंत या ऋणात्मक मान) गणना किए गए विलंब पर वापस आ जाते हैं। कॉन्टेक्स्ट-ओवरफ़्लो रिकवरी अलग रहती है क्योंकि यह उसी अनुरोध को फिर से करने के बजाय बातचीत को कॉम्पैक्ट करती है।
संदर्भ
interface SessionHooks {
readonly context: SessionContext
readonly compaction: SessionCompaction
readonly generate: SessionGenerate
readonly title: SessionTitle
readonly "model.request": SessionModelRequest
readonly "http.request": SessionHttpRequest
readonly "http.response": SessionHttpResponse
readonly "experimental.ws.handshake": SessionWebSocketHandshake
readonly "experimental.ws.send": SessionWebSocketSend
readonly "experimental.ws.receive": SessionWebSocketReceive
readonly retry: SessionRetry
}
type RetryDecision = { retry: false } | { retry: true; delay: number }
interface SessionRetry {
readonly sessionID: string
readonly agent: string
readonly model: { providerID: string; id: string; variant?: string }
readonly error: { type: string; message: string; status?: number }
readonly attempt: number
decision: RetryDecision
}
interface SessionHookDomain {
readonly hook: ModelHooks<SessionHooks>
}शेल
निष्पादन से पहले शेल कमांड, वर्किंग डायरेक्टरी, टाइमआउट, निष्पादन योग्य फ़ाइलों या एनवायरनमेंट वेरिएबल को संशोधित करें।
effect: (ctx) =>
Effect.gen(function* () {
const shell = ctx.shell
yield* shell.hook("create.before", (event) =>
Effect.sync(() => {
if (event.command === "npm") event.command = "bun"
event.timeout = Math.min(event.timeout, 60_000)
event.env.COMPANY_ENV = "development"
}),
)
}),संदर्भ
interface ShellCreateBefore {
command: string
cwd: string
timeout: number
shell: string
env: Record<string, string | undefined>
}
interface ShellHooks {
readonly "create.before": ShellCreateBefore
}
interface ShellHookDomain {
readonly hook: Hooks<ShellHooks>
}टूल
Before हुक इनपुट को प्रतिस्थापित कर सकते हैं या Tool.Error के साथ विफल हो सकते हैं।
effect: (ctx) =>
Effect.gen(function* () {
const tool = ctx.tool
yield* tool.hook("execute.before", (event) =>
event.tool === "write"
? Effect.fail(new Tool.Error({ message: "Writes are disabled" }))
: Effect.logDebug("tool input", { tool: event.tool, input: event.input }),
)
}),After हुक सफल परिणामों और विफलताओं का निरीक्षण या प्रतिस्थापन कर सकते हैं।
effect: (ctx) =>
Effect.gen(function* () {
const tool = ctx.tool
yield* tool.hook("execute.after", (event) => {
if (event.status === "error") return Effect.logWarning("tool failed", { message: event.error.message })
return Effect.sync(() => {
event.result = { ...event.result, metadata: { observed: true } }
})
})
}),संदर्भ
interface ToolHooks {
readonly "execute.before": ToolExecuteBefore
readonly "execute.after": ToolExecuteAfter
}
interface ToolFailures extends Record<keyof ToolHooks, unknown> {
readonly "execute.before": Tool.Error
readonly "execute.after": never
}
interface ToolHookDomain {
readonly hook: Hooks<ToolHooks, ToolFailures>
}साझा रजिस्ट्रेशन प्रकार दिखाते हैं कि किन ऑपरेशनों को प्लगइन स्कोप की आवश्यकता होती है।
interface Registration {
readonly dispose: Effect.Effect<void>
}
type Hooks<Spec, Failures extends Record<keyof Spec, unknown> = Record<keyof Spec, never>> = <Name extends keyof Spec>(
name: Name,
callback: (input: Spec[Name]) => Effect.Effect<void, Failures[Name]>,
) => Effect.Effect<Registration, never, Scope.Scope>
type Transform<Input> = (callback: (input: Input) => void) => Effect.Effect<Registration, never, Scope.Scope>प्रकाशित करें
एक पैकेज प्लगइन स्थानीय Effect प्लगइन के समान ही डिफ़ॉल्ट एक्सपोर्ट का उपयोग करता है। मुख्य एंट्रीपॉइंट से Effect कार्यान्वयन को एक्सपोर्ट करें और effect को peerDependency के रूप में घोषित करें। devDependencies के अंतर्गत, उसी संस्करण को पिन करें जिसका उपयोग @opencode/plugin करता है ताकि डेवलपमेंट में केवल एक ही कॉपी इंस्टॉल हो।
{
"name": "opencode-acme-effect-plugin",
"version": "1.0.0",
"type": "module",
"exports": {
".": "./src/index.ts"
},
"dependencies": {
"@opencode/plugin": "latest"
},
"peerDependencies": {
"effect": "^4.0.1"
},
"devDependencies": {
"effect": "4.0.1"
}
}OpenCode effect, एक्सपोर्ट किए गए effect/* सबपाथ और @opencode/plugin के इम्पोर्ट (प्लगइन के node_modules में निर्भरताओं से इम्पोर्ट सहित) को होस्ट के रनटाइम इंस्टेंस पर रीडायरेक्ट करता है ताकि फ़ाइबर, लॉगर और Schema पार्सर एक ही रनटाइम कॉपी साझा करें।
- प्रकाशित प्लगइन आर्टिफ़ैक्ट में
effectको बंडल न करें। यदि आप किसी बंडलर से बिल्ड करते हैं, तोeffectऔरeffect/*को external के रूप में चिह्नित करें। - प्लगइन और उनकी
node_modulesनिर्भरताएँ हमेशा होस्ट OpenCode रिलीज़ केeffectइंस्टेंस पर निष्पादित होती हैं। आपके द्वारा लक्षित OpenCode रिलीज़ के साथ संगतeffectAPI और मॉड्यूल पाथ का उपयोग करें; किसी अन्यeffectमेजर संस्करण (जैसे Effect 3) पर बनी निर्भरताओं को भी होस्ट काeffectही मिलता है और वे समर्थित नहीं हैं। - केवल सार्वजनिक
effectसबपाथ प्रदान किए जाते हैं। कोई प्लगइन जो Effect के किसी निजीinternalमॉड्यूल को इम्पोर्ट करता है, पाथ का नाम बताते हुए एक त्रुटि के साथ लोड होने में विफल हो जाता है। - संकलित OpenCode बाइनरी में Effect के HTTP API दस्तावेज़ पृष्ठों द्वारा उपयोग किए जाने वाले Scalar और Swagger UI एसेट्स शामिल नहीं हैं; किसी प्लगइन से उन पृष्ठों को सर्व करने पर इसके बजाय एक सूचना दिखाई जाती है।
पैकेज एंट्रीपॉइंट एक effect फ़ंक्शन के साथ Plugin.define को एक्सपोर्ट करता है।
import { Plugin } from "@opencode/plugin/effect"
import { Effect } from "effect"
export default Plugin.define({
id: "acme.published",
effect: (ctx) =>
Effect.gen(function* () {
const storage = ctx.storage
yield* storage.set("installed", true)
}),
})आपके द्वारा लक्षित OpenCode रिलीज़ के साथ संगत संस्करणों का उपयोग करें और केवल वर्कस्पेस-लिंक्ड कॉपी के बजाय इंस्टॉल किए गए पैकेज का परीक्षण करें।
bun pm pack
bun add ./opencode-acme-effect-plugin-1.0.0.tgz