| --- |
| title: SDK |
| description: opencode ์๋ฒ๋ฅผ ์ํ ํ์
์์ ํ JS ํด๋ผ์ด์ธํธ. |
| --- |
|
|
| import config from "../../../../config.mjs" |
| export const typesUrl = `${config.github}/blob/dev/packages/sdk/js/src/gen/types.gen.ts` |
|
|
| opencode JS/TS SDK๋ ์๋ฒ์ ์ํธ ์์ฉ์ ์ํ ์ ํ ์์ ํ ํด๋ผ์ด์ธํธ๋ฅผ ์ ๊ณตํฉ๋๋ค. |
| ํตํฉ ๋ฐ ์ ์ด opencode programmatically๋ฅผ ๊ตฌ์ถํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. |
|
|
| [Learn more](/docs/server) ์๋ฒ๊ฐ ์ด๋ป๊ฒ ์๋ํ๋์? ์๋ฅผ ๋ค์ด, ์ปค๋ฎค๋ํฐ์ ์ํด ๊ตฌ์ถ ๋ [projects](/docs/ecosystem |
|
|
| --- |
|
|
| |
|
|
| npm์์ SDK ์ค์น: |
|
|
| ```bash |
| npm install @opencode-ai/sdk |
| ``` |
|
|
| --- |
|
|
| |
|
|
| opencode์ ์ธ์คํด์ค ๋ง๋ค๊ธฐ: |
|
|
| ```javascript |
| import { createOpencode } from "@opencode-ai/sdk" |
|
|
| const { client } = await createOpencode() |
| ``` |
|
|
| ์๋ฒ์ ํด๋ผ์ด์ธํธ ๋ชจ๋ ์์ |
|
|
| |
|
|
| | ์ต์
| ์ ํ | ์ค๋ช
| ๊ธฐ๋ณธ | |
| | ---------- | ------------- | ----------------------- | ----------- | |
| | `hostname` | `string` | ์๋ฒ ํธ์คํธ๋ช
| `127.0.0.1` | |
| | `port` | `number` | ์๋ฒํฌํธ | `4096` | |
| | `signal` | `AbortSignal` | ์ทจ์ ์ ํธ | `undefined` | |
| | `timeout` | `number` | ์๋ฒ ์์์์ ์๊ฐ ์์ | `5000` | |
| | `config` | `Config` | ๊ตฌ์ฑ ๊ฐ์ฒด | `{}` | |
|
|
| --- |
|
|
| |
|
|
| ๊ตฌ์ฑ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ ์ ์์ต๋๋ค. ์ธ์คํด์ค๋ ์ฌ์ ํ `opencode.json`๋ฅผ ์ ํํ์ง๋ง ๊ตฌ์ฑ ์ธ๋ผ์ธ์ ์ถ๊ฐ ํ ์ ์์ต๋๋ค. |
|
|
| ```javascript |
| 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์ ์คํ ์ธ์คํด์ค๊ฐ ์ด๋ฏธ ์๋ค๋ฉด ํด๋ผ์ด์ธํธ ์ธ์คํด์ค๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค. |
|
|
| ```javascript |
| import { createOpencodeClient } from "@opencode-ai/sdk" |
|
|
| const client = createOpencodeClient({ |
| baseUrl: "http://localhost:4096", |
| }) |
| ``` |
|
|
| |
|
|
| | ์ต์
| ์ ํ | ์ค๋ช
| ๊ธฐ๋ณธ | |
| | --------------- | ---------- | --------------------------------- | ----------------------- | |
| | `baseUrl` | `string` | ์๋ฒ์ URL | `http://localhost:4096` | |
| | `fetch` | `function` | ์ฌ์ฉ์ ์ ์ fetch ๊ตฌํ | `globalThis.fetch` | |
| | `parseAs` | `string` | ์๋ต ํ์ฑ ๋ฐฉ๋ฒ | `auto` | |
| | `responseStyle` | `string` | ๋ฐํ ์คํ์ผ: `data` ๋๋ `fields` | `fields` | |
| | `throwOnError` | `boolean` | ๋ฐํ ์ ์ค๋ฅ | `false` | |
|
|
| --- |
|
|
| |
|
|
| SDK์๋ ๋ชจ๋ API ์ ํ์ TypeScript ์ ์๊ฐ ํฌํจ๋์ด ์์ต๋๋ค. ์ง์ ๊ฐ์ ธ ์ค๊ธฐ : |
|
|
| ```typescript |
| import type { Session, Message, Part } from "@opencode-ai/sdk" |
| ``` |
|
|
| ๋ชจ๋ ์ ํ์ ์๋ฒ์ OpenAPI ์ฌ์์์ ์์ฑ๋๋ฉฐ <a href={typesUrl}>types ํ์ผ</a>์์ ์ฌ์ฉํ ์ ์์ต๋๋ค. |
|
|
| --- |
|
|
| |
|
|
| SDK๋ ์ก์ ์ ์๋ ์ค๋ฅ๋ฅผ ๋์ง ์ ์์ต๋๋ค: |
|
|
| ```typescript |
| try { |
| await client.session.get({ path: { id: "invalid-id" } }) |
| } catch (error) { |
| console.error("Failed to get session:", (error as Error).message) |
| } |
| ``` |
|
|
| --- |
|
|
| |
|
|
| JSON ์คํค๋ง์ ํจ๊ป `format`์ ์ง์ ํ์ฌ ๋ชจ๋ธ์์ ๊ตฌ์กฐํ๋ JSON ์ถ๋ ฅ์ ์์ฒญํ ์ ์์ต๋๋ค. ๋ชจ๋ธ์ `StructuredOutput` ๋๊ตฌ๋ฅผ ์ฌ์ฉํ์ฌ ์คํค๋ง์ ์ผ์นํ๋ ๊ฒ์ฆ๋ JSON์ ๋ฐํํฉ๋๋ค. |
|
|
| |
|
|
| ```typescript |
| const result = await client.session.prompt({ |
| path: { id: sessionId }, |
| body: { |
| parts: [{ type: "text", text: "Research Anthropic and provide company info" }], |
| format: { |
| type: "json_schema", |
| schema: { |
| type: "object", |
| properties: { |
| company: { type: "string", description: "Company name" }, |
| founded: { type: "number", description: "Year founded" }, |
| products: { |
| type: "array", |
| items: { type: "string" }, |
| description: "Main products", |
| }, |
| }, |
| required: ["company", "founded"], |
| }, |
| }, |
| }, |
| }) |
|
|
| // Access the structured output |
| console.log(result.data.info.structured_output) |
| // { company: "Anthropic", founded: 2021, products: ["Claude", "Claude API"] } |
| ``` |
|
|
| |
|
|
| | ์ ํ | ์ค๋ช
| |
| | ------------- | ------------------------------------------------- | |
| | `text` | ๊ธฐ๋ณธ๊ฐ. ํ์ค ํ
์คํธ ์๋ต (๊ตฌ์กฐํ๋ ์ถ๋ ฅ ์์) | |
| | `json_schema` | ์ ๊ณต๋ ์คํค๋ง์ ์ผ์นํ๋ ๊ฒ์ฆ๋ JSON์ ๋ฐํํฉ๋๋ค | |
|
|
| |
|
|
| `type: 'json_schema'`๋ฅผ ์ฌ์ฉํ ๋ ๋ค์์ ์ ๊ณตํ์ญ์์ค: |
|
|
| | ํ๋ | ์ ํ | ์ค๋ช
| |
| | ------------ | --------------- | ------------------------------------------- | |
| | `type` | `'json_schema'` | ํ์. JSON ์คํค๋ง ๋ชจ๋๋ฅผ ์ง์ ํฉ๋๋ค | |
| | `schema` | `object` | ํ์. ์ถ๋ ฅ ๊ตฌ์กฐ๋ฅผ ์ ์ํ๋ JSON ์คํค๋ง ๊ฐ์ฒด | |
| | `retryCount` | `number` | ์ ํ ์ฌํญ. ๊ฒ์ฆ ์ฌ์๋ ํ์ (๊ธฐ๋ณธ๊ฐ: 2) | |
|
|
| |
|
|
| ๋ชจ๋ธ์ด ๋ชจ๋ ์ฌ์๋ ํ์๋ ์ ํจํ ๊ตฌ์กฐํ๋ ์ถ๋ ฅ์ ์์ฑํ์ง ๋ชปํ๋ฉด ์๋ต์ `StructuredOutputError`๊ฐ ํฌํจ๋ฉ๋๋ค: |
|
|
| ```typescript |
| if (result.data.info.error?.name === "StructuredOutputError") { |
| console.error("Failed to produce structured output:", result.data.info.error.message) |
| console.error("Attempts:", result.data.info.error.retries) |
| } |
| ``` |
|
|
| |
|
|
| 1. **๋ช
ํํ ์ค๋ช
์ ๊ณต**: ๋ชจ๋ธ์ด ์ถ์ถํ ๋ฐ์ดํฐ๋ฅผ ์ดํดํ๋ ๋ฐ ๋์์ด ๋๋๋ก ์คํค๋ง ์์ฑ์ ๋ช
ํํ ์ค๋ช
์ ์ ๊ณตํ์ญ์์ค. |
| 2. **`required` ์ฌ์ฉ**: ํ์ ํ๋๋ฅผ ์ง์ ํ๋ ค๋ฉด `required`๋ฅผ ์ฌ์ฉํ์ญ์์ค. |
| 3. **์คํค๋ง๋ฅผ ์ง์ค์ ์ผ๋ก ์ ์ง**: ๋ณต์กํ ์ค์ฒฉ ์คํค๋ง๋ ๋ชจ๋ธ์ด ์ฌ๋ฐ๋ฅด๊ฒ ์ฑ์ฐ๊ธฐ ๋ ์ด๋ ค์ธ ์ ์์ต๋๋ค. |
| 4. **์ ์ ํ `retryCount` ์ค์ **: ๋ณต์กํ ์คํค๋ง์ ๊ฒฝ์ฐ ๋๋ฆฌ๊ณ ๋จ์ํ ์คํค๋ง์ ๊ฒฝ์ฐ ์ค์ด์ญ์์ค. |
|
|
| --- |
|
|
| |
|
|
| SDK๋ type-safe ํด๋ผ์ด์ธํธ๋ฅผ ํตํด ๋ชจ๋ ์๋ฒ API๋ฅผ ๋
ธ์ถํฉ๋๋ค. |
|
|
| --- |
|
|
| |
|
|
| | ๋ฉ์๋ | ์ค๋ช
| ์๋ต | |
| | ----------------- | ---------------------- | ------------------------------------ | |
| | `global.health()` | ์๋ฒ ๊ฑด๊ฐ ๋ฐ ๋ฒ์ ํ์ธ | `{ healthy: true, version: string }` | |
|
|
| --- |
|
|
| |
|
|
| ```javascript |
| const health = await client.global.health() |
| console.log(health.data.version) |
| ``` |
|
|
| --- |
|
|
| |
|
|
| | ๋ฐฉ๋ฒ | ์ค๋ช
| ์๋ต | |
| | -------------- | ------------------------- | ------------------------------------------- | |
| | `app.log()` | ๋ก๊ทธ ํญ๋ชฉ ์์ฑ | `boolean` | |
| | `app.agents()` | ์ด์ฉ ๊ฐ๋ฅํ ๋ชจ๋ ์์ด์ ํธ | <a href={typesUrl}><code>Agent[]</code></a> | |
|
|
| --- |
|
|
| |
|
|
| ```javascript |
| // 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() |
| ``` |
|
|
| --- |
|
|
| |
|
|
| | ๋ฐฉ๋ฒ | ์ค๋ช
| ์๋ต | |
| | ------------------- | ----------------------- | --------------------------------------------- | |
| | `project.list()` | ๋ชจ๋ ํ๋ก์ ํธ ๋ณด๊ธฐ | <a href={typesUrl}><code>Project[]</code></a> | |
| | `project.current()` | ํ์ฌ ํ๋ก์ ํธ ๊ฐ์ ธ ์ค๊ธฐ | <a href={typesUrl}><code>Project</code></a> | |
|
|
| --- |
|
|
| |
|
|
| ```javascript |
| // List all projects |
| const projects = await client.project.list() |
|
|
| // Get current project |
| const currentProject = await client.project.current() |
| ``` |
|
|
| --- |
|
|
| |
|
|
| | ๋ฐฉ๋ฒ | ์ค๋ช
| ์๋ต | |
| | ------------ | ------------------- | ---------------------------------------- | |
| | `path.get()` | ํ์ฌ ๊ฒฝ๋ก ๊ฐ์ ธ ์ค๊ธฐ | <a href={typesUrl}><code>Path</code></a> | |
|
|
| --- |
|
|
| |
|
|
| ```javascript |
| // Get current path information |
| const pathInfo = await client.path.get() |
| ``` |
|
|
| --- |
|
|
| |
|
|
| | ๋ฐฉ๋ฒ | ์ค๋ช
| ์๋ต | |
| | -------------------- | -------------------------- | ----------------------------------------------------------------------------------------------------- | |
| | `config.get()` | ๊ตฌ์ฑ ์ ๋ณด | <a href={typesUrl}><code>Config</code></a> | |
| | `config.providers()` | ๋ชฉ๋ก ์ ๊ณต์
์ฒด ๋ฐ ๊ธฐ๋ณธ ๋ชจ๋ธ | `{ providers: `<a href={typesUrl}><code>Provider[]</code></a>`, default: { [key: string]: string } }` | |
|
|
| --- |
|
|
| |
|
|
| ```javascript |
| const config = await client.config.get() |
|
|
| const { providers, default: defaults } = await client.config.providers() |
| ``` |
|
|
| --- |
|
|
| |
|
|
| | ๋ฉ์๋ | ์ค๋ช
| ๋น๊ณ | |
| | ---------------------------------------------------------- | ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| | `session.list()` | ์ธ์
์ผ๋ | <a href={typesUrl}><code>Session[]</code></a> | |
| | `session.get({ path })` | ์ธ์
๊ฐ์ ธ ์ค๊ธฐ | <a href={typesUrl}><code>Session</code></a> | |
| | `session.children({ path })` | ํ์ ์ธ์
๋ชฉ๋ก | ๋ฐํ <a href={typesUrl}><code>Session</code></a> | |
| | `session.create({ body })` | ์ธ์
๋ง๋ค๊ธฐ | ๋ฆฌํด <a href={typesUrl}><code>Session</code></a> | |
| | `session.delete({ path })` | ์ธ์
์ญ์ | `boolean` ๋ฐํ | |
| | `session.update({ path, body })` | ์
๋ฐ์ดํธ ์ธ์
์์ฑ | ๋ฐํ <a href={typesUrl}><code>Session</code></a> | |
| | `session.init({ path, body })` | ์ฑ ์ด๊ธฐํ ๋ฐ `AGENTS.md` ๋ถ์ | `boolean`๋ฅผ ๋ฐํ | |
| | `session.abort({ path })` | ์ด์ ์ค์ธ ์ธ์
| ๋ฐํ `boolean` | |
| | `session.share({ path })` | ๊ณต์ ์ธ์
| ๋ฐํ <a href={typesUrl}><code>Session</code></a> | |
| | `session.unshare({ path })` | ๊ณต์ ์ธ์
์ทจ์ | ๋ฐํ <a href={typesUrl}><code>Session</code></a> | |
| | `session.summarize({ path, body })` | ์ธ์
์์ฝ | ๋ฐํ `boolean` | |
| | `session.messages({ path })` | ์ธ์
์ ๋ฉ์์ง ๋ชฉ๋ก | `{ info: `<a href={typesUrl}><code>Message</code></a>`, parts: `<a href={typesUrl}><code>Part</code></a>`}[]` | |
| | `session.message({ path })` | ๋ฉ์์ง ์์ธ์ ๋ณด | ๋ฐํ `{ info: `<a href={typesUrl}><code>Message</code></a>`, parts: `<a href={typesUrl}><code>Part[]</code></a>`}` | |
| | `session.prompt({ path, body })` | prompt ๋ฉ์์ง ๋ณด๋ด๊ธฐ | `body.noReply: true`๋ UserMessage(์ปจํ
์คํธ ์ ์ฉ)๋ฅผ ๋ฐํํฉ๋๋ค. ๊ธฐ๋ณธ๊ฐ์ AI ์๋ต๊ณผ ํจ๊ป <a href={typesUrl}><code>AssistantMessage</code></a>๋ฅผ ๋ฐํํฉ๋๋ค. [๊ตฌ์กฐํ๋ ์ถ๋ ฅ](#๊ตฌ์กฐํ๋-์ถ๋ ฅ)์ ์ํ `body.outputFormat`์ ์ง์ํฉ๋๋ค | |
| | `session.command({ path, body })` | ์ธ์
์ผ๋ก ๋ช
๋ น์ ์ ์ก | `{ info: `<a href={typesUrl}><code>AssistantMessage</code></a>`, parts: `<a href={typesUrl}><code>Part[]</code></a>`}` | |
| | `session.shell({ path, body })` | shell ๋ช
๋ น์ ์คํ | <a href={typesUrl}><code>AssistantMessage</code></a> | |
| | `session.revert({ path, body })` | ๋ฉ์์ง ๋ค์ ๋ณํ | <a href={typesUrl}><code>Session</code></a> | |
| | `session.unrevert({ path })` | ๋ฉ์์ง ๋๋๋ฆฌ๊ธฐ ์ทจ์ | ๋ฐํ <a href={typesUrl}><code>Session</code></a> | |
| | `postSessionByIdPermissionsByPermissionId({ path, body })` | ํ๊ฐ ์์ฒญ ๋์ | ๋ฐํ `boolean` | |
|
|
| --- |
|
|
| |
|
|
| ```javascript |
| // 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." }], |
| }, |
| }) |
| ``` |
|
|
| --- |
|
|
| |
|
|
| | ๋ฐฉ๋ฒ | ์ค๋ช
| ์๋ต | |
| | ------------------------- | ---------------------------- | -------------------------------------------------------------------------------------- | |
| | `find.text({ query })` | ํ์ผ์์ ํ
์คํธ ๊ฒ์ | `path`, `lines`, `line_number`, `absolute_offset`, `submatches`์ ์ผ์นํ๋ ๊ฐ์ฒด์ ๋ฐฐ์ด | |
| | `find.files({ query })` | ์ด๋ฆ์ ํ์ผ ๋ฐ ๋๋ ํ ๋ฆฌ ์ฐพ๊ธฐ | `string[]` (๊ฒฝ๋ก) | |
| | `find.symbols({ query })` | ์์
๊ณต๊ฐ ์ฌ๋ณผ ์ฐพ๊ธฐ | <a href={typesUrl}><code>Symbol[]</code></a> | |
| | `file.read({ query })` | ํ์ผ ๋ณด๊ธฐ | `{ type: "raw" \| "patch", content: string }` | |
| | `file.status({ query? })` | ํธ๋ ๋ ํ์ผ ์ํ๋ฅผ ํ์ธ | <a href={typesUrl}><code>File[]</code></a> | |
|
|
| `find.files`๋ ๋ช๋ช ์ ํ์ ์ธ ์กฐํ ๋ถ์ผ๋ฅผ ์ง์ํฉ๋๋ค: |
|
|
| - `type`: `"file"` ๋๋ `"directory"` |
| - `directory`: ๊ฒ์์ ๋ํ ํ๋ก์ ํธ ๋ฃจํธ๋ฅผ override |
| - `limit`: ์ต๋ ๊ฒฐ๊ณผ (1-200) |
|
|
| --- |
|
|
| |
|
|
| ```javascript |
| // 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.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` | |
|
|
| --- |
|
|
| |
|
|
| ```javascript |
| // Control TUI interface |
| await client.tui.appendPrompt({ |
| body: { text: "Add this to prompt" }, |
| }) |
|
|
| await client.tui.showToast({ |
| body: { message: "Task completed", variant: "success" }, |
| }) |
| ``` |
|
|
| --- |
|
|
| |
|
|
| | ๋ฐฉ๋ฒ | ์ค๋ช
| ์๋ต | |
| | ------------------- | -------------- | --------- | |
| | `auth.set({ ... })` | ์ธ์ฆ ์๊ฒฉ ์ฆ๋ช
| `boolean` | |
|
|
| --- |
|
|
| |
|
|
| ```javascript |
| await client.auth.set({ |
| path: { id: "anthropic" }, |
| body: { type: "api", key: "your-api-key" }, |
| }) |
| ``` |
|
|
| --- |
|
|
| |
|
|
| | ๋ฐฉ๋ฒ | ์ค๋ช
| ์๋ต | |
| | ------------------- | ----------------------- | ----------------------- | |
| | `event.subscribe()` | ์๋ฒ-sent ์ด๋ฒคํธ ์คํธ๋ฆผ | ์๋ฒ-sent ์ด๋ฒคํธ ์คํธ๋ฆผ | |
|
|
| --- |
|
|
| |
|
|
| ```javascript |
| // 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) |
| } |
| ``` |
|
|