Effect プラグイン
@opencode/plugin/effect は OpenCode プラグイン API の Effect ネイティブ版です。コンテキストの操作は Effect または Stream を返し、コールバックは Effect を返し、プラグインのライフタイムは Scope で表現されます。
OpenCode は、プラグインとその node_modules の依存関係を読み込むときに、effect(エクスポートされた effect/* サブパスを含む)と @opencode/plugin を実行時に提供します。プラグインにバンドルするのではなく、ローカルの型チェックとテストのために peer/dev 依存関係としてインストールしてください。
bun add --peer @opencode/plugin effect@4.0.1.opencode/plugins/ から Effect プラグインをエクスポートすると、自動的に読み込まれます。
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",
"opencode-acme-effect-plugin@1.2.0",
"@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"))
}),
})バックグラウンドの作業にはスコープ付きファイバーを使用します。クリーンアップ時にプラグインのスコープがファイバーを中断します。
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 クライアントに加えて、プラグイン専用の transform、フック、ストレージ、再読み込み、オプションを公開します。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 から読み取ります。不明な値は使用前に型を絞ってください。
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 })
}),
})Transform はエディタを通じて同期的に状態を編集します。OpenCode は各ドメイン内で登録順に transform を適用します。最初にソースがプロバイダーと不変のモデル定義を提供し、次にモデルの transform がアクティブなプロバイダーの候補を編集します。登録を 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,
})
})
}),
})モデルの transform は、アクティブなプロバイダーの候補全体にポリシーを適用できます。
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)
})
})
}),transform が使用する外部の状態が変わったら reload を呼び出します。ここで loadFromSource() は { info: Provider.Info, models: readonly Model.Info[] } のエントリを返し、プロバイダーの再読み込みはモデルの結果全体も無効にします。
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)
}),すべてのエージェントを読むか、ID で 1 つ取得します。クライアントのレスポンスには、解決された 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 })
}),エージェントを transform または再読み込みします。
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。
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 で 1 つのプロバイダーのメタデータを確認します。
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,
})
}),プロバイダーの transform はプロバイダーの設定とソースの定義を編集します。エディタの読み取りは非アクティブなプロバイダーも含み、不変の定義を公開します。models.update はソースモデルの所有コピーを編集します。
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 はそのプロバイダーを除外します。
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、Model.Info。
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 })
}),モデルの transform は、アクティブなプロバイダーの候補全体を編集します。無効な候補もすべての transform が終わるまで編集でき、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 は、モデルの transform の出力ではなく、非アクティブなテンプレートを含む不変のソース定義を読み取ります。
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。
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>
}現在の location で利用可能なコマンドを読み取ります。
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) })
}),現在のコマンドの transform は追加のみです。エグゼキューターはセッション、プロンプトの添付、配信モードを受け取ります。
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、Session.Inbox.Delivery。
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 })
}),連携と認証方法を transform します。OAuth の方法のコールバックは Effect で、スコープ付きのリソースを取得できます。
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、Integration.Method、Connection.Info、Form.Answer。
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 サーバーとその現在の接続状態を一覧表示します。
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 })
}),プラグインは transform を通じてのみ 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")
})
}),transform で disabled: true を設定するとサーバーを無効にして切断し、disabled: false を設定すると有効にして OpenCode の接続を許可します。OpenCode は transform 後の設定からサーバーのライフサイクルを調整します。
transform が使用する外部の状態が変わったら reload() を呼び出して、transform を再適用しサーバーを調整します。
effect: (ctx) =>
Effect.gen(function* () {
const mcp = ctx.mcp
yield* mcp.reload()
}),スキーマ:Mcp.Server、Mcp.LocalConfigEncoded、Mcp.RemoteConfigEncoded。
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>
}現在の location のアクティブ、失敗、解決済みのプラグインを一覧表示します。
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、Plugin.Source。
interface PluginApi<E = never> {
readonly list: PluginListOperation<E>
}
interface Context {
readonly plugin: PluginApi<unknown>
}現在の location で利用可能な参照を読み取ります。
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、Reference.LocalSource、Reference.GitSource。
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 で削除します。削除は実行中の処理を停止し、子セッションを再帰的に削除します。
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 で要求します。完了した要約ではなく、受け付けられた受信トレイの項目を返し、コンパクションはデフォルトで次のステップの境界で実行されます。代わりにキューの順序を保つには 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 で添付、一覧表示、削除します。エントリはセッションの指示内に <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、Model.Ref、Session.Inbox.User、Session.Inbox.Synthetic、InstructionEntry.Info。
type SessionDomain = Pick<
SessionApi<unknown>,
| "list"
| "create"
| "get"
| "remove"
| "switchAgent"
| "switchModel"
| "prompt"
| "generate"
| "command"
| "compact"
| "synthetic"
| "interrupt"
| "update"
| "move"
| "wait"
| "context"
| "instructions"
>現在の location のスキルを読み取ります。
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) })
}),スキルを transform して再読み込みします。ブランド付きの値には再エクスポートされた 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。
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 にスコープされた永続的な 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")
}),プレフィックスに一致するキーを、カーソルを使ってページ単位で取得します。
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 型を受け付け、Effect を直接返します。
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 で登録します。エグゼキューターはデコードされた入力を受け取り、型付きの出力、表示コンテンツ、メタデータを含む Effect を返します。
エディタは add、update、remove に対応しています。transform のコールバックは同期で、Effect や Promise を返してはいけません。外部データは登録または再読み込みの前に読み込んでください。OpenCode は新しいエディタ上で、アクティブな transform を登録順に再生します。同じ実効的なツール名では、後の有効な登録が以前の登録を上書きします。
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() を呼び出します。再読み込みはアクティブな transform を順序を変えずに再生し、プラグインの effect は再実行しません。
Effect のツールは、Promise のツールと同じ自由形式の入力の宣言を受け付けます。モデルがその形式を提示しない場合、ツールは通常のオブジェクト入力のままです。
ツールの id には実際に使われる名前が入ります。その ID のツールが存在しない場合、get() は undefined を返します。update と remove には、名前空間を含めた実際のツール名(上の例では acme_greeting)を渡します。名前空間のドットは _ に変換されます。ツール名では英字、数字、_、- 以外の文字が _ に置き換わります。存在しない名前は無視されます。新しいツールを作成するには、add に完全な定義を渡してください。更新しても名前と名前空間は変わりません。ネストした値を直接変更するのではなく、新しいスキーマやオプションを代入して置き換えてください。無効な更新はログに記録され、以前の定義がそのまま残ります。
yield* ctx.tool.transform((editor) => {
editor.update("acme_greeting", (tool) => {
tool.description = "Greet the user by name"
})
editor.remove("acme_obsolete")
})更新と削除は、MCP カタログの更新後も含め、追加とともに順番に再生されます。
transform はスコープ付きの登録を返します。yield* registration.dispose を実行すると、その transform を削除して残りの transform から再構築し、上書きしていた以前の定義が現れます。破棄は冪等で、プラグインのスコープを閉じてもその登録が破棄されます。
各モデルリクエストはツールのスナップショットを取得します。再読み込みと破棄は将来のスナップショットに影響し、既存のリクエストがすでに取得した定義やエグゼキューターには影響しません。可変のプラグインデータをクローズオーバーするエグゼキューターはそのデータを引き続き参照するため、その定義に紐付けたい値は transform の内部で取り込んでください。
スキーマ:Tool.Content、Tool.TextContent、Tool.FileContent。
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>
}リポジトリの情報、作業コピーの状態、ファイルの Diff を読み取ります。
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 })
}),スコープ付きの transform で location 単位のプロバイダーを登録します。検出されたリポジトリの種類に一致するプロバイダーが自動的に選択され、別のプロバイダーを選ぶには 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")
})
}),プロバイダーのコールバックは、現在の location、作業コピーのルート、カノニカルなプロジェクトルート、任意のリポジトリのストアを受け取ります。Diff のコールバックはさらに、選択されたモード、要求されたコンテキスト、出力のバイトバジェットを受け取ります。リポジトリの検出は、引き続き OpenCode 組み込みの Git と Mercurial の検出器を使用します。
プロバイダーは任意で init(scope) を実装し、scope.worktree にリポジトリを作成できます。初期化は検出されたリポジトリの種類ではなく、ID(デフォルトは Git)でプロバイダーを選択します。location にまだリポジトリがない場合でも、init を持つプロバイダーを登録してください。OpenCode は呼び出す前にディレクトリが存在しリポジトリがないことを確認し、その後プロジェクトを再検出します。新しいリポジトリ形式には、このコールバックとは別に検出のサポートが必要です。
スキーマ:Vcs.Info、Vcs.FileStatus、FileDiff.Info。
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 のエグゼキューターを登録し、デフォルトのプロバイダーを選択します。デフォルトを false にすると Web 検索が無効になります。
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、WebSearch.Result。
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 です。自動的な中断のために、プラグインのスコープでその消費者をフォークしてください。
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。
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 フックのメッセージは、保存されたメッセージの metadata を読み取り専用で持ちます(たとえば prompt フックが設定した内容)。これはモデルには送信されません。
OpenCode の指示を保持する system パーツは、各ソースを metadata.experimental.instructions に { key, text } として列挙します(core/date や core/environment など)。フックはこれでソースを区別できます。リストはコピーであるため、パーツの text を編集しても更新されません。これは実験的な機能で変更される可能性があります。
モデルリクエストの設定を変更し、必要に応じてフックを 1 つのプロバイダーに限定します。イベントは、以下の 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" },
)
}),ネイティブのプロバイダーのリクエストまたはレスポンスを変更します。ボディは 1 回限りのストリームであるため、読み取る前に複製するか置き換えてください。どちらのフックもセッションが発行するすべてのリクエストで実行され、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 でストリーミングするプロバイダーはセッションごとに 1 つの接続を再利用するため、HTTP フックがそのトラフィックを見ることはありません。実験的な experimental.ws.handshake フックは、接続が選択される前にモデル呼び出しごとに 1 回実行され、接続の 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 は検証しません。
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" },
)
}),プロバイダーの失敗に対する再試行の判断を上書きするか、その遅延をミリ秒で置き換えます。フックは、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 が使用するバージョンに固定し、開発時に 1 つのコピーだけがインストールされるようにします。
{
"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 パーサーが 1 つのランタイムコピーを共有するようにします。
effectを公開するプラグインの成果物にバンドルしないでください。バンドラーでビルドする場合は、effectとeffect/*を external に指定します。- プラグインとその
node_modulesの依存関係は、常にホストの OpenCode リリースのeffectインスタンスに対して実行されます。対象とする OpenCode のリリースと互換性のあるeffectの API とモジュールパスを使用してください。別の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