Skip to content

SDK

ไคลเอนต์ JS ประเภทที่ปลอดภัยสำหรับเซิร์ฟเวอร์ opencode

opencode JS/TS SDK จัดเตรียมไคลเอ็นต์ประเภทที่ปลอดภัยสำหรับการโต้ตอบกับเซิร์ฟเวอร์ ใช้เพื่อสร้างการบูรณาการและควบคุม opencode โดยทางโปรแกรม

เรียนรู้เพิ่มเติม เกี่ยวกับวิธีการทำงานของเซิร์ฟเวอร์ ตัวอย่างเช่น ลองดู โครงการ ที่สร้างโดยชุมชน


ติดตั้ง

ติดตั้ง SDK จาก npm:

Terminal window
npm install @opencode-ai/sdk

สร้างไคลเอนต์

สร้างอินสแตนซ์ของ opencode:

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

สิ่งนี้จะเริ่มต้นทั้งเซิร์ฟเวอร์และไคลเอนต์

ตัวเลือก

ตัวเลือกTypeคำอธิบายค่าเริ่มต้น
hostnamestringชื่อโฮสต์ของเซิร์ฟเวอร์127.0.0.1
portnumberพอร์ตเซิร์ฟเวอร์4096
signalAbortSignalยกเลิกสัญญาณสำหรับการยกเลิกundefined
timeoutnumberหมดเวลาเป็น ms สำหรับการเริ่มต้นเซิร์ฟเวอร์5000
configConfigวัตถุการกำหนดค่า{}

การกำหนดค่า

คุณสามารถส่งผ่านออบเจ็กต์การกำหนดค่าเพื่อปรับแต่งลักษณะการทำงานได้ อินสแตนซ์ยังคงรับ opencode.json ของคุณ แต่คุณสามารถแทนที่หรือเพิ่มการกำหนดค่าแบบอินไลน์ได้:

import { createOpencode } from "@opencode-ai/sdk"
const opencode = await createOpencode({
hostname: "127.0.0.1",
port: 4096,
config: {
model: "anthropic/claude-3-5-sonnet-20241022",
},
})
console.log(`Server running at ${opencode.server.url}`)
opencode.server.close()

ลูกค้าเท่านั้น

หากคุณมีอินสแตนซ์ opencode ที่ทำงานอยู่แล้ว คุณสามารถสร้างอินสแตนซ์ไคลเอ็นต์เพื่อเชื่อมต่อได้:

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

ตัวเลือก

ตัวเลือกTypeคำอธิบายค่าเริ่มต้น
baseUrlstringURL ของเซิร์ฟเวอร์http://localhost:4096
fetchfunctionการใช้งานการดึงข้อมูลแบบกำหนดเองglobalThis.fetch
parseAsstringMethodการแยกวิเคราะห์การตอบสนองauto
responseStylestringรูปแบบการคืนสินค้า: data หรือ fieldsfields
throwOnErrorbooleanโยนข้อผิดพลาดแทนการส่งคืนfalse

ประเภท

SDK มีคำจำกัดความ TypeScript สำหรับ API ประเภททั้งหมด นำเข้าโดยตรง:

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

ทุกประเภทสร้างขึ้นจากข้อกำหนด OpenAPI ของเซิร์ฟเวอร์และมีอยู่ใน ไฟล์ประเภท


ข้อผิดพลาด

SDK อาจทำให้เกิดข้อผิดพลาดที่คุณสามารถจับและจัดการได้:

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

API

SDK เปิดเผย API ของเซิร์ฟเวอร์ทั้งหมดผ่านไคลเอ็นต์ประเภทที่ปลอดภัย


ทั่วโลก

Methodคำอธิบายการตอบสนอง
global.health()ตรวจสอบสภาพและเวอร์ชันของเซิร์ฟเวอร์{ healthy: true, version: string }

ตัวอย่าง

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

แอป

Methodคำอธิบายการตอบสนอง
app.log()เขียนรายการบันทึกboolean
app.agents()รายชื่อตัวแทนที่มีอยู่ทั้งหมดตัวแทน[]

ตัวอย่าง

// Write a log entry
await client.app.log({
body: {
service: "my-app",
level: "info",
message: "Operation completed",
},
})
// List available agents
const agents = await client.app.agents()

โครงการ

Methodคำอธิบายการตอบสนอง
project.list()แสดงรายการโครงการทั้งหมดProject[]
project.current()รับโครงการปัจจุบันProject

ตัวอย่าง

// List all projects
const projects = await client.project.list()
// Get current project
const currentProject = await client.project.current()

เส้นทาง

Methodคำอธิบายการตอบสนอง
path.get()รับเส้นทางปัจจุบันPath

ตัวอย่าง

// Get current path information
const pathInfo = await client.path.get()

การกำหนดค่า

Methodคำอธิบายการตอบสนอง
config.get()รับข้อมูลการกำหนดค่ากำหนดค่า
config.providers()ผู้ให้บริการรายชื่อและโมเดลเริ่มต้น{ providers: ผู้ให้บริการ[], default: { [key: string]: string } }

ตัวอย่าง

const config = await client.config.get()
const { providers, default: defaults } = await client.config.providers()

เซสชัน

Methodคำอธิบายหมายเหตุ
session.list()แสดงรายการเซสชันส่งคืน เซสชัน[]
session.get({ path })รับเซสชันส่งคืน เซสชัน
session.children({ path })แสดงรายการเซสชันย่อยส่งคืน เซสชัน[]
session.create({ body })สร้างเซสชันส่งคืน เซสชัน
session.delete({ path })ลบเซสชันส่งคืน boolean
session.update({ path, body })อัปเดตคุณสมบัติเซสชันส่งคืน เซสชัน
session.init({ path, body })วิเคราะห์แอปและสร้าง AGENTS.mdส่งคืน boolean
session.abort({ path })ยกเลิกเซสชันที่ทำงานอยู่ส่งคืน boolean
session.share({ path })แบ่งปันเซสชั่นส่งคืน เซสชัน
session.unshare({ path })เลิกแชร์เซสชันส่งคืน เซสชัน
session.summarize({ path, body })สรุปเซสชันส่งคืน boolean
session.messages({ path })แสดงรายการข้อความในเซสชันส่งคืน { info: ข้อความ, parts: ส่วน[]}[]
session.message({ path })รับรายละเอียดข้อความส่งคืน { info: ข้อความ, parts: ส่วน[]}
session.prompt({ path, body })ส่งข้อความแจ้งbody.noReply: true ส่งคืน UserMessage (บริบทเท่านั้น) ค่าเริ่มต้นส่งคืน AssistantMessage พร้อมการตอบสนองของ AI
session.command({ path, body })ส่งคำสั่งไปยังเซสชั่นส่งคืน { info: AssistantMessage, parts: ส่วน[]}
session.shell({ path, body })รันคำสั่ง shellส่งคืน AssistantMessage
session.revert({ path, body })คืนค่าข้อความส่งคืน เซสชัน
session.unrevert({ path })คืนค่าข้อความที่เปลี่ยนกลับส่งคืน เซสชัน
postSessionByIdPermissionsByPermissionId({ path, body })ตอบสนองต่อการร้องขอการอนุญาตส่งคืน boolean

ตัวอย่าง

// Create and manage sessions
const session = await client.session.create({
body: { title: "My session" },
})
const sessions = await client.session.list()
// Send a prompt message
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 without triggering AI response (useful for plugins)
await client.session.prompt({
path: { id: session.id },
body: {
noReply: true,
parts: [{ type: "text", text: "You are a helpful assistant." }],
},
})

ไฟล์

Methodคำอธิบายการตอบสนอง
find.text({ query })ค้นหาข้อความในไฟล์อาร์เรย์ของวัตถุที่ตรงกับ path, lines, line_number, absolute_offset, submatches
find.files({ query })ค้นหาไฟล์และไดเร็กทอรีตามชื่อstring[] (paths)
find.symbols({ query })ค้นหาสัญลักษณ์พื้นที่ทำงานSymbol[]
file.read({ query })อ่านไฟล์{ type: "raw" | "patch", content: string }
file.status({ query? })รับสถานะสำหรับไฟล์ที่ถูกติดตามFile[]

find.files รองรับช่องค้นหาเพิ่มเติมบางช่อง:

  • type: "file" หรือ "directory"
  • directory: แทนที่รูทโปรเจ็กต์สำหรับการค้นหา
  • limit: ผลลัพธ์สูงสุด (1–200)

ตัวอย่าง

// Search and read files
const textResults = await client.find.text({
query: { pattern: "function.*opencode" },
})
const files = await client.find.files({
query: { query: "*.ts", type: "file" },
})
const directories = await client.find.files({
query: { query: "packages", type: "directory", limit: 20 },
})
const content = await client.file.read({
query: { path: "src/index.ts" },
})

TUI

Methodคำอธิบายการตอบสนอง
tui.appendPrompt({ body })เพิ่มข้อความต่อท้ายข้อความแจ้งboolean
tui.openHelp()เปิดกล่องโต้ตอบความช่วยเหลือboolean
tui.openSessions()เปิดตัวเลือกเซสชันboolean
tui.openThemes()เปิดตัวเลือกธีมboolean
tui.openModels()เปิดตัวเลือกรุ่นboolean
tui.submitPrompt()ส่งข้อความแจ้งปัจจุบันboolean
tui.clearPrompt()ล้างข้อความแจ้งboolean
tui.executeCommand({ body })ดำเนินการคำสั่งboolean
tui.showToast({ body })แสดงการแจ้งเตือนขนมปังปิ้งboolean

ตัวอย่าง

// Control TUI interface
await client.tui.appendPrompt({
body: { text: "Add this to prompt" },
})
await client.tui.showToast({
body: { message: "Task completed", variant: "success" },
})

การรับรองความถูกต้อง

Methodคำอธิบายการตอบสนอง
auth.set({ ... })ตั้งค่าข้อมูลประจำตัวการรับรองความถูกต้องboolean

ตัวอย่าง

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

กิจกรรม

Methodคำอธิบายการตอบสนอง
event.subscribe()สตรีมเหตุการณ์ที่เซิร์ฟเวอร์ส่งสตรีมเหตุการณ์ที่เซิร์ฟเวอร์ส่ง

ตัวอย่าง

// Listen to real-time events
const events = await client.event.subscribe()
for await (const event of events.stream) {
console.log("Event:", event.type, event.properties)
}