Effect-плагины
@opencode/plugin/effect — версия API плагинов OpenCode, нативная для Effect. Его операции контекста возвращают
Effect или Stream, обратные вызовы возвращают Effect, а время жизни плагина представлено Scope.
OpenCode предоставляет effect (включая экспортируемые подпути effect/*) и @opencode/plugin во время работы при загрузке
плагинов и их зависимостей из node_modules. Устанавливайте их как peer/dev-зависимости для локальной проверки типов и тестирования,
а не включайте в бандл плагина.
bun add --peer @opencode/plugin effect@4.0.1Экспортируйте 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",
"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 выполняется при загрузке плагина. При перезагрузке или выгрузке область видимости 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)
}),Контекст предоставляет Effect-клиент подключённого сервера OpenCode, а также преобразования, хуки, хранилище, перезагрузки и параметры только для плагинов. Он не предоставляет приватные сервисы Core OpenCode.
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 })
}),
})Преобразования синхронно редактируют состояние через редактор. OpenCode применяет преобразования в порядке регистрации внутри каждого домена. Сначала источники добавляют провайдеров и неизменяемые определения моделей; затем преобразования моделей редактируют кандидатов активных провайдеров. Возврат регистрации через yield оставляет её в scope плагина.
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[] }; перезагрузки провайдеров также инвалидируют весь результат моделей.
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. Ответы клиента включают разрешённую локацию и данные схемы.
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()
}),Schema: 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.
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 редактирует принадлежащую ему копию исходной модели.
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 })
})Schemas: 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 })
}),Преобразования моделей редактируют всю коллекцию кандидатов активных провайдеров. Отключённые кандидаты остаются редактируемыми, пока все
преобразования не завершатся; 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()
}),Schema: 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>
}Прочитайте команды, доступные в текущей локации.
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) })
}),Текущее преобразование команд только добавляет. Исполнитель получает сессию, вложения промпта и режим доставки.
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()
}),Schemas: 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,
})
}),Подключайтесь ключом или управляйте попыткой OAuth через подключённый API.
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 — это 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()
}),Schemas: 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 })
}),Плагины управляют 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 в преобразовании, чтобы отключить сервер и разорвать подключение, или disabled: false, чтобы включить его и разрешить
OpenCode подключиться. OpenCode согласует жизненный цикл сервера по преобразованной конфигурации.
Вызывайте reload() после изменения внешнего состояния, используемого преобразованием, чтобы повторно применить преобразования и согласовать серверы.
effect: (ctx) =>
Effect.gen(function* () {
const mcp = ctx.mcp
yield* mcp.reload()
}),Schemas: 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>
}Перечислите активные, неудачные и разрешённые плагины в текущей локации.
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 })
}),Schemas: Plugin.Info, Plugin.Source.
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()
}),Schemas: 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)
}),Schemas: 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"
>Прочитайте навыки в текущей локации.
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()
}),Schema: 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>
}Сохраняйте, читайте и удаляйте постоянные значения JSON в области ID плагина.
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 })
}),Хранилище принимает тип JSON из Effect и возвращает 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. Обратный вызов преобразования синхронный: он не должен возвращать 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 плагина.
Effect-инструменты принимают то же объявление ввода свободной формы, что и Promise-инструменты. Если модель не объявляет этот формат, инструмент сохраняет обычный объектный ввод.
У инструментов есть id, содержащий действующее имя, а get() возвращает undefined, когда такого ID нет. Используйте
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, чтобы удалить его преобразование и перестроить
из оставшихся, обнаружив любое более раннее определение, которое оно переопределяло. Освобождение идемпотентно, а закрытие
scope плагина также освобождает его регистрации.
Каждый запрос к модели захватывает снимок инструментов. Перезагрузка и освобождение влияют на будущие снимки, а не на определения или исполнители, уже захваченные существующим запросом. Исполнители, замыкающиеся на изменяемые данные плагина, всё равно наблюдают эти данные; захватите значение внутри преобразования, когда оно должно оставаться привязанным к этому определению.
Schemas: 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>
}Прочитайте сведения о репозитории, состояние рабочей копии или различия файлов.
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 })
}),Зарегистрируйте провайдера в области локации через преобразование области видимости. Провайдеры, соответствующие обнаруженному типу репозитория,
выбираются автоматически; используйте 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")
})
}),Обратные вызовы провайдера получают текущую локацию, корень рабочей копии, канонический корень проекта и необязательное хранилище репозитория. Обратные вызовы diff также получают выбранный режим, запрошенный контекст и бюджет байтов вывода. Обнаружение репозитория по-прежнему использует встроенные детекторы Git и Mercurial OpenCode.
Провайдеры могут реализовать init(scope), чтобы создать репозиторий в scope.worktree. Инициализация выбирает
провайдера по ID (по умолчанию Git), а не по обнаруженному типу репозитория. Зарегистрируйте провайдера с init, даже
когда в локации ещё нет репозитория; OpenCode проверяет, что каталог существует и не имеет репозитория, перед вызовом,
затем заново обнаруживает проект. Новым форматам репозиториев всё ещё нужна поддержка обнаружения помимо этого обратного вызова.
Schemas: 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, чтобы отключить веб-поиск.
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()
}),Schemas: 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>
}Подписка на публичные события сервера возвращает Stream из Effect. Запускайте обработчик в отдельном файбере в области видимости плагина, чтобы он прервался автоматически.
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,
)
}),Schema: 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. Модели они не передаются.
Часть system, содержащая инструкции OpenCode, также перечисляет каждый источник в metadata.experimental.instructions как
{ key, text }, например core/date или core/environment, чтобы хук мог различать источники. Список — копия:
правка text части не обновляет его. Это экспериментально и может измениться.
Изменяйте настройки запроса к модели и при необходимости ограничьте хук одним провайдером. Событие несёт тот же kind,
что и HTTP-хуки ниже.
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 соединения; изменение любого из них переоткрывает сокет. HTTP-хуки по-прежнему выполняются для любого запроса, на который
откатывается маршрут WebSocket.
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>
}Изменяйте команды shell, рабочие каталоги, тайм-ауты, исполняемые файлы или переменные окружения перед выполнением.
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>
}Общие типы регистрации показывают, какие операции требуют scope плагина.
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всегда выполняются на экземпляреeffectрелиза OpenCode-хоста. Используйте API и пути модулейeffect, совместимые с релизом OpenCode, на который ориентируетесь; зависимости, собранные на другой мажорной версииeffect(например, Effect 3), тоже получаютeffectхоста и не поддерживаются. - Предоставляются только публичные подпути
effect. Плагин, импортирующий один из приватных модулейinternalEffect, не загружается с ошибкой, называющей путь. - Скомпилированный бинарник OpenCode не включает ресурсы Scalar и Swagger UI, используемые страницами документации HTTP API Effect; при обслуживании этих страниц из плагина вместо них показывается уведомление.
Точка входа пакета экспортирует Plugin.define с функцией effect.
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