中文
文档
基于 OpenCode 构建
客户端
Effect 客户端

Effect 客户端

在 Effect 中使用 @opencode/client/effect 调用 OpenCode HTTP API,获取类型化的 Effect 与 Stream。

@opencode/client/effect 是面向 OpenCode HTTP API 的 Effect 原生客户端。它会返回类型化的 Effect 与 Stream,并将响应自动解码为 OpenCode Schema 值。

bun add @opencode/client [email protected]

创建客户端

传入服务器 URL 创建客户端,然后按 API 资源分组调用方法:

import { AbsolutePath, Location, OpenCode } from "@opencode/client/effect"
import { Effect } from "effect"
import { FetchHttpClient } from "effect/http"
 
const program = Effect.gen(function* () {
  const client = yield* OpenCode.make({ baseUrl: "http://localhost:4096" })
  const session = yield* client.session.create({
    location: Location.Ref.make({ directory: AbsolutePath.make("/workspace") }),
  })
  yield* client.session.prompt({
    sessionID: session.id,
    text: "Review the current changes",
  })
  return session
})
 
const session = await Effect.runPromise(program.pipe(Effect.provide(FetchHttpClient.layer)))

请求头与请求控制

在传入的 HttpClient 上配置默认请求头。原生操作使用标准的 Effect 中断机制(interruption)取消请求;RPC 方法还额外接受单次调用的 location、headers 和 signal 选项。

import { HttpClient, HttpClientRequest } from "effect/http"
 
const httpClient = yield* HttpClient.HttpClient
const client = yield* OpenCode.make({ baseUrl: "https://opencode.example.com" }).pipe(
  Effect.provideService(
    HttpClient.HttpClient,
    HttpClient.mapRequest(httpClient, (request) =>
      HttpClientRequest.setHeaders(request, { authorization: `Bearer ${process.env.OPENCODE_TOKEN}` }),
    ),
  ),
)
 
const sessions = yield* client.session.list()

事件流订阅

event.subscribe() 与 experimental.session.log() 等流式操作会返回 Effect Stream。

import { Effect, Stream } from "effect"
 
yield* client.event
  .subscribe()
  .pipe(Stream.runForEach((event) => Effect.logInfo("OpenCode event", { type: event.type })))

原生事件 Stream 与 RPC 事件 Stream 在每个客户端内共享一条懒加载连接。构造 Stream 时不会建立连接,消费时才会连接。停止单个消费者不会影响其他消费者,最后一个消费者退出后才会关闭底层源。事件源遇到 EOF 或故障时会结束当前所有订阅,不会自动重试或重放。后加入的原生消费者会在实时事件之前收到当前的连接标记。

插件 RPC

使用与 Promise 客户端和服务端插件相同的共享契约:

import { Acme } from "opencode-acme-plugin/rpc"
 
const acme = client.rpc(Acme)
const result = yield* acme.search({ query: "hello" }, { location: { directory: "/workspace" } })
 
yield* acme.events
  .subscribe("updated")
  .pipe(
    Stream.runForEach((event) =>
      Effect.logInfo("Plugin event", { type: event.type, location: event.location, text: event.data.text }),
    ),
  )

方法的参数和返回值类型均由契约自动推导。第二个可选参数用于传递 location、signal 和 headers;省略 location 时采用常规请求默认值。RPC 调用会随着消费它的 Effect 一起被中断。方法声明的错误映射会自动进入 Effect 的错误通道,已声明的错误会通过其数据 Schema 完成解码。类型化子客户端会移除通用的 HTTP RPC 错误外层包装;保留的 rpc.* 类型用于标识框架级故障。

RPC 事件是类型化 Stream,而不是回调式的 on 监听器。它们接收该 RPC 来自所有位置的事件,每条事件都带有必填的 location 以及带前缀的标准事件类型(如 rpc.acme.updated)。这与服务端插件句柄不同——服务端插件句柄绑定到自身所在位置。关于定义、Schema、注册与实时订阅语义,请参阅 Effect 插件 RPC。

本地后台服务

仅限 Node 环境的 @opencode/client/effect/service 入口以 Effect 形式提供本地后台服务的发现、启动、认证与停止能力。

bun add @effect/[email protected]

确保后台服务运行并为其创建已认证的客户端:

import { NodeFileSystem } from "@effect/platform-node"
import { OpenCode } from "@opencode/client/effect"
import { Service } from "@opencode/client/effect/service"
import { Effect } from "effect"
import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/http"
 
const program = Effect.gen(function* () {
  const endpoint = yield* Service.ensure()
  const httpClient = yield* HttpClient.HttpClient
  const client = yield* OpenCode.make({ baseUrl: endpoint.url }).pipe(
    Effect.provideService(
      HttpClient.HttpClient,
      HttpClient.mapRequest(httpClient, (request) =>
        HttpClientRequest.setHeaders(request, Service.headers(endpoint) ?? {}),
      ),
    ),
  )
  return yield* client.server.info()
})
 
const health = await Effect.runPromise(
  program.pipe(Effect.provide(FetchHttpClient.layer), Effect.provide(NodeFileSystem.layer)),
)

仅发现服务而不自动启动,或停止已注册的指定服务:

const endpoint = yield* Service.discover()
yield* Service.stop()