Effect クライアント
型付きの Effect と Stream を返す @opencode/client/effect を使って、Effect プログラムから OpenCode HTTP API を呼び出します。
@opencode/client/effect は OpenCode HTTP API 向けの Effect ネイティブクライアントです。型付きの Effect と Stream を返し、レスポンスを OpenCode スキーマ値にデコードします。
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、header、signal オプションを受け付けます。
{/* prettier-ignore */}
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 を返します。
{/* prettier-ignore */}
import { Effect, Stream } from "effect"
yield* client.event
.subscribe()
.pipe(Stream.runForEach((event) => Effect.logInfo("OpenCode event", { type: event.type })))ネイティブイベントと RPC イベントの Stream は、クライアントごとに 1 つの遅延接続を共有します。Stream を構築しただけでは接続されず、消費を開始した時点で接続されます。1 つのコンシューマーを停止しても他のコンシューマーは動作を継続し、最後のコンシューマーが終了するとソースが閉じられます。ソースの EOF または失敗により現在のサブスクリプションは終了し、自動リトライやリプレイは行われません。途中から参加したネイティブコンシューマーは、ライブイベントの前に現在の接続マーカーを受け取ります。
プラグイン RPC
Promise クライアントやサーバープラグインと同じ共有コントラクトを使用します。
{/* prettier-ignore */}
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 }),
),
)メソッドの引数と戻り値はコントラクトから推論されます。オプションの第 2 引数には location、signal、headers を指定します。location を省略すると通常のリクエストデフォルトが使用されます。呼び出しはそれを消費している Effect とともに割り込まれます。メソッドのエラーマップは Effect のエラーチャネルに推論されます。宣言されたエラーはそのデータスキーマを通じてデコードされます。型付きサブクライアントは汎用 HTTP RPC エラーラッパーを取り除き、予約済みの rpc.* タイプがフレームワーク障害を識別します。
RPC イベントはコールバック形式の on リスナーではなく、型付き Stream です。すべてのロケーションからその RPC のイベントを受信し、それぞれに必須の location と rpc.acme.updated のような通常のプレフィックス付きタイプが含まれます。これは自身のロケーションに固定されているサーバープラグインのハンドルとは異なります。定義、スキーマ、登録、ライブサブスクリプションのセマンティクスについては 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)),
)起動せずに検出するか、登録済みの該当サービスを正確に停止できます。
{/* prettier-ignore */}
const endpoint = yield* Service.discover()
yield* Service.stop()