文档
中文文档
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)
}