SDK

Type-safe JS/TS client cho OpenCode server.


Tổng quan

SDK OpenCode JS/TS cung cấp type-safe client để tương tác với server. Dùng nó để xây dựng integrations và điều khiển OpenCode programmatically.

Xem thêm về Server. Ví dụ từ cộng đồng: Ecosystem projects.


Cài đặt

npm install @opencode-ai/sdk

Create client

import { createOpencode } from "@opencode-ai/sdk"

const { client } = await createOpencode()

Options

OptionTypeMô tảDefault
hostnamestringServer hostname127.0.0.1
portnumberServer port4096
timeoutnumberTimeout (ms)5000
configConfigConfig object{}

Client only (kết nối tới server đang chạy)

import { createOpencodeClient } from "@opencode-ai/sdk"

const client = createOpencodeClient({
  baseUrl: "http://localhost:4096",
})

Structured Output

Yêu cầu model trả về JSON theo schema:

const result = await client.session.prompt({
  path: { id: sessionId },
  body: {
    parts: [{ type: "text", text: "Research Anthropic" }],
    format: {
      type: "json_schema",
      schema: {
        type: "object",
        properties: {
          company: { type: "string" },
          founded: { type: "number" },
        },
        required: ["company", "founded"],
      },
    },
  },
})

console.log(result.data.info.structured_output)

APIs

Global

const health = await client.global.health()
console.log(health.data.version)

App

// Log
await client.app.log({
  body: { service: "my-app", level: "info", message: "Done" },
})

// List agents
const agents = await client.app.agents()

Sessions

// Create
const session = await client.session.create({
  body: { title: "My session" },
})

// List
const sessions = await client.session.list()

// Prompt
const result = await client.session.prompt({
  path: { id: session.id },
  body: {
    model: { providerID: "anthropic", modelID: "claude-3-5-sonnet-20241022" },
    parts: [{ type: "text", text: "Hello!" }],
  },
})

// Inject context (no reply)
await client.session.prompt({
  path: { id: session.id },
  body: {
    noReply: true,
    parts: [{ type: "text", text: "Context here" }],
  },
})

Files

// Search text
const results = await client.find.text({
  query: { pattern: "function.*opencode" },
})

// Find files
const files = await client.find.files({
  query: { query: "*.ts", type: "file" },
})

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

TUI

// Append to prompt
await client.tui.appendPrompt({
  body: { text: "Add this" },
})

// Show toast
await client.tui.showToast({
  body: { message: "Done!", variant: "success" },
})

Auth

await client.auth.set({
  path: { id: "anthropic" },
  body: { type: "api", key: "your-api-key" },
})

Events

const events = await client.event.subscribe()
for await (const event of events.stream) {
  console.log("Event:", event.type, event.properties)
}

Types

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

Tất cả types được generate từ server's OpenAPI spec.


Errors

try {
  await client.session.get({ path: { id: "invalid-id" } })
} catch (error) {
  console.error("Failed:", (error as Error).message)
}