文档
日本語ドキュメント
SDK

SDK

OpenCode SDK は、OpenCode サーバーとのプログラム的なやり取りのための型安全なクライアントを提供します。

インストール

npm install @opencode-ai/sdk

クライアントのセットアップ

フルインスタンス

import { createOpencode } from "@opencode-ai/sdk"
const { client } = await createOpencode()

クライアントのみ

import { createOpencodeClient } from "@opencode-ai/sdk"
const client = createOpencodeClient({
  baseUrl: "http://localhost:4096"
})

設定

const opencode = await createOpencode({
  hostname: "127.0.0.1",
  port: 4096,
  config: {
    model: "anthropic/claude-3-5-sonnet"
  }
})

TypeScript サポート

import type { Session, Message, Part } from "@opencode-ai/sdk"

API リファレンス

ヘルスチェック

const health = await client.global.health()

セッション

await client.session.list()
await client.session.create()
await client.session.prompt({
  path: { id: sessionId },
  body: {
    model: { providerID: "anthropic", modelID: "claude-3-5-sonnet" },
    parts: [{ type: "text", text: "あなたのクエリ" }]
  }
})

ファイル

const content = await client.file.read({
  query: { path: "src/index.ts" }
})

イベント

const events = await client.event.subscribe()
for await (const event of events.stream) {
  console.log("イベント:", event.type)
}