|
| 1 | +--- |
| 2 | +title: SDK |
| 3 | +description: JS SDK for the opencode server. |
| 4 | +--- |
| 5 | + |
| 6 | +import config from "../../../../config.mjs" |
| 7 | +export const typesUrl = `${config.github}/blob/dev/packages/sdk/js/src/gen/types.gen.ts` |
| 8 | + |
| 9 | +The opencode [JS/TS SDK](https://www.npmjs.com/package/@opencode-ai/sdk) provides a type-safe client for interacting with the opencode server. You can use it to build custom integrations and control opencode programmatically. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## Install |
| 14 | + |
| 15 | +Install the SDK from npm: |
| 16 | + |
| 17 | +```bash |
| 18 | +npm install @opencode-ai/sdk |
| 19 | +``` |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## Create client |
| 24 | + |
| 25 | +Create a client instance to connect to your opencode server: |
| 26 | + |
| 27 | +```javascript |
| 28 | +import { createOpencodeClient } from "@opencode-ai/sdk" |
| 29 | + |
| 30 | +const client = createOpencodeClient({ |
| 31 | + baseUrl: "http://localhost:4096", |
| 32 | +}) |
| 33 | +``` |
| 34 | + |
| 35 | +#### Options |
| 36 | + |
| 37 | +| Option | Type | Description | Default | |
| 38 | +| --------- | ---------- | --------------------------- | ----------------------- | |
| 39 | +| `baseUrl` | `string` | URL of the opencode server | `http://localhost:4096` | |
| 40 | +| `fetch` | `function` | Custom fetch implementation | `globalThis.fetch` | |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## Start server |
| 45 | + |
| 46 | +You can also programmatically start an opencode server: |
| 47 | + |
| 48 | +```javascript |
| 49 | +import { createOpencodeServer } from "@opencode-ai/sdk" |
| 50 | + |
| 51 | +const server = await createOpencodeServer({ |
| 52 | + host: "127.0.0.1", |
| 53 | + port: 4096, |
| 54 | +}) |
| 55 | + |
| 56 | +console.log(`Server running at ${server.url}`) |
| 57 | + |
| 58 | +server.close() |
| 59 | +``` |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## Types |
| 64 | + |
| 65 | +The SDK includes TypeScript definitions for all API types. Import them directly: |
| 66 | + |
| 67 | +```typescript |
| 68 | +import type { Session, Message, Part } from "@opencode-ai/sdk" |
| 69 | +``` |
| 70 | + |
| 71 | +All types are generated from the server's OpenAPI specification and available in the <a href={typesUrl}>types file</a>. |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +## APIs |
| 76 | + |
| 77 | +The SDK exposes all server APIs through a type-safe client interface. |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +### App |
| 82 | + |
| 83 | +| Method | Description | Response | |
| 84 | +| ------------ | ------------------ | --------------------------------------- | |
| 85 | +| `app.get()` | Get app info | <a href={typesUrl}><code>App</code></a> | |
| 86 | +| `app.init()` | Initialize the app | `boolean` | |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +#### Examples |
| 91 | + |
| 92 | +```javascript |
| 93 | +const app = await client.app.get() |
| 94 | +await client.app.init() |
| 95 | +``` |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +### Config |
| 100 | + |
| 101 | +| Method | Description | Response | |
| 102 | +| -------------------- | --------------------------------- | ----------------------------------------------------------------------------------------------------- | |
| 103 | +| `config.get()` | Get config info | <a href={typesUrl}><code>Config</code></a> | |
| 104 | +| `config.providers()` | List providers and default models | `{ providers: `<a href={typesUrl}><code>Provider[]</code></a>`, default: { [key: string]: string } }` | |
| 105 | + |
| 106 | +--- |
| 107 | + |
| 108 | +#### Examples |
| 109 | + |
| 110 | +```javascript |
| 111 | +const config = await client.config.get() |
| 112 | +const { providers, default: defaults } = await client.config.providers() |
| 113 | +``` |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +### Sessions |
| 118 | + |
| 119 | +| Method | Description | Notes | |
| 120 | +| ------------------------------------------------------------- | ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------- | |
| 121 | +| `session.list()` | List sessions | Returns <a href={typesUrl}><code>Session[]</code></a> | |
| 122 | +| `session.get({ id })` | Get session | Returns <a href={typesUrl}><code>Session</code></a> | |
| 123 | +| `session.children({ id })` | List child sessions | Returns <a href={typesUrl}><code>Session[]</code></a> | |
| 124 | +| `session.create({ parentID?, title? })` | Create session | Returns <a href={typesUrl}><code>Session</code></a> | |
| 125 | +| `session.delete({ id })` | Delete session | Returns `boolean` | |
| 126 | +| `session.update({ id, title? })` | Update session properties | Returns <a href={typesUrl}><code>Session</code></a> | |
| 127 | +| `session.init({ id, messageID, providerID, modelID })` | Analyze app and create `AGENTS.md` | Returns `boolean` | |
| 128 | +| `session.abort({ id })` | Abort a running session | Returns `boolean` | |
| 129 | +| `session.share({ id })` | Share session | Returns <a href={typesUrl}><code>Session</code></a> | |
| 130 | +| `session.unshare({ id })` | Unshare session | Returns <a href={typesUrl}><code>Session</code></a> | |
| 131 | +| `session.summarize({ id, providerID, modelID })` | Summarize session | Returns `boolean` | |
| 132 | +| `session.messages({ id })` | List messages in a session | Returns `{ info: `<a href={typesUrl}><code>Message</code></a>`, parts: `<a href={typesUrl}><code>Part[]</code></a>`}[]` | |
| 133 | +| `session.message({ id, messageID })` | Get message details | Returns `{ info: `<a href={typesUrl}><code>Message</code></a>`, parts: `<a href={typesUrl}><code>Part[]</code></a>`}` | |
| 134 | +| `session.chat({ id, ...chatInput })` | Send chat message | Returns <a href={typesUrl}><code>Message</code></a> | |
| 135 | +| `session.shell({ id, agent, command })` | Run a shell command | Returns <a href={typesUrl}><code>Message</code></a> | |
| 136 | +| `session.revert({ id, messageID, partID? })` | Revert a message | Returns <a href={typesUrl}><code>Session</code></a> | |
| 137 | +| `session.unrevert({ id })` | Restore reverted messages | Returns <a href={typesUrl}><code>Session</code></a> | |
| 138 | +| `session.permissions.respond({ id, permissionID, response })` | Respond to a permission request | Returns `boolean` | |
| 139 | + |
| 140 | +--- |
| 141 | + |
| 142 | +#### Examples |
| 143 | + |
| 144 | +```javascript |
| 145 | +// Create and manage sessions |
| 146 | +const session = await client.session.create({ title: "My session" }) |
| 147 | +const sessions = await client.session.list() |
| 148 | + |
| 149 | +// Send messages |
| 150 | +const message = await client.session.chat({ |
| 151 | + id: session.id, |
| 152 | + providerID: "anthropic", |
| 153 | + modelID: "claude-3-5-sonnet-20241022", |
| 154 | + parts: [{ type: "text", text: "Hello!" }], |
| 155 | +}) |
| 156 | +``` |
| 157 | + |
| 158 | +--- |
| 159 | + |
| 160 | +### Files |
| 161 | + |
| 162 | +| Method | Description | Response | |
| 163 | +| ------------------------- | ---------------------------- | ------------------------------------------------------------------------------------------- | |
| 164 | +| `find.text({ pattern })` | Search for text in files | Array of match objects with `path`, `lines`, `line_number`, `absolute_offset`, `submatches` | |
| 165 | +| `find.files({ query })` | Find files by name | `string[]` (file paths) | |
| 166 | +| `find.symbols({ query })` | Find workspace symbols | <a href={typesUrl}><code>Symbol[]</code></a> | |
| 167 | +| `file.read({ path })` | Read a file | `{ type: "raw" \| "patch", content: string }` | |
| 168 | +| `file.status()` | Get status for tracked files | <a href={typesUrl}><code>File[]</code></a> | |
| 169 | + |
| 170 | +--- |
| 171 | + |
| 172 | +#### Examples |
| 173 | + |
| 174 | +```javascript |
| 175 | +// Search and read files |
| 176 | +const textResults = await client.find.text({ pattern: "function.*opencode" }) |
| 177 | +const files = await client.find.files({ query: "*.ts" }) |
| 178 | +const content = await client.file.read({ path: "src/index.ts" }) |
| 179 | +``` |
| 180 | + |
| 181 | +--- |
| 182 | + |
| 183 | +### Logging |
| 184 | + |
| 185 | +| Method | Description | Response | |
| 186 | +| ------------------------------------------------ | --------------- | --------- | |
| 187 | +| `log.write({ service, level, message, extra? })` | Write log entry | `boolean` | |
| 188 | + |
| 189 | +--- |
| 190 | + |
| 191 | +#### Examples |
| 192 | + |
| 193 | +```javascript |
| 194 | +await client.log.write({ |
| 195 | + service: "my-app", |
| 196 | + level: "info", |
| 197 | + message: "Operation completed", |
| 198 | +}) |
| 199 | +``` |
| 200 | + |
| 201 | +--- |
| 202 | + |
| 203 | +### Agents |
| 204 | + |
| 205 | +| Method | Description | Response | |
| 206 | +| -------------- | ------------------------- | ------------------------------------------- | |
| 207 | +| `agent.list()` | List all available agents | <a href={typesUrl}><code>Agent[]</code></a> | |
| 208 | + |
| 209 | +--- |
| 210 | + |
| 211 | +#### Examples |
| 212 | + |
| 213 | +```javascript |
| 214 | +const agents = await client.agent.list() |
| 215 | +``` |
| 216 | + |
| 217 | +--- |
| 218 | + |
| 219 | +### TUI |
| 220 | + |
| 221 | +| Method | Description | Response | |
| 222 | +| --------------------------------------------- | --------------------------------- | ---------------------- | |
| 223 | +| `tui.appendPrompt({ text })` | Append text to the prompt | `boolean` | |
| 224 | +| `tui.openHelp()` | Open the help dialog | `boolean` | |
| 225 | +| `tui.openSessions()` | Open the session selector | `boolean` | |
| 226 | +| `tui.openThemes()` | Open the theme selector | `boolean` | |
| 227 | +| `tui.openModels()` | Open the model selector | `boolean` | |
| 228 | +| `tui.submitPrompt()` | Submit the current prompt | `boolean` | |
| 229 | +| `tui.clearPrompt()` | Clear the prompt | `boolean` | |
| 230 | +| `tui.executeCommand({ command })` | Execute a command | `boolean` | |
| 231 | +| `tui.showToast({ title?, message, variant })` | Show toast notification | `boolean` | |
| 232 | +| `tui.control.next()` | Wait for the next control request | Control request object | |
| 233 | +| `tui.control.response({ body })` | Respond to a control request | `boolean` | |
| 234 | + |
| 235 | +--- |
| 236 | + |
| 237 | +#### Examples |
| 238 | + |
| 239 | +```javascript |
| 240 | +// Control TUI interface |
| 241 | +await client.tui.appendPrompt({ text: "Add this to prompt" }) |
| 242 | +await client.tui.showToast({ |
| 243 | + message: "Task completed", |
| 244 | + variant: "success", |
| 245 | +}) |
| 246 | +``` |
| 247 | + |
| 248 | +--- |
| 249 | + |
| 250 | +### Auth |
| 251 | + |
| 252 | +| Method | Description | Response | |
| 253 | +| ------------------------------- | ------------------------------ | --------- | |
| 254 | +| `auth.set({ id, ...authData })` | Set authentication credentials | `boolean` | |
| 255 | + |
| 256 | +--- |
| 257 | + |
| 258 | +#### Examples |
| 259 | + |
| 260 | +```javascript |
| 261 | +await client.auth.set({ |
| 262 | + id: "anthropic", |
| 263 | + type: "api", |
| 264 | + key: "your-api-key", |
| 265 | +}) |
| 266 | +``` |
| 267 | + |
| 268 | +--- |
| 269 | + |
| 270 | +### Events |
| 271 | + |
| 272 | +| Method | Description | Response | |
| 273 | +| ------------------- | ------------------------- | ------------------------- | |
| 274 | +| `event.subscribe()` | Server-sent events stream | Server-sent events stream | |
| 275 | + |
| 276 | +--- |
| 277 | + |
| 278 | +#### Examples |
| 279 | + |
| 280 | +```javascript |
| 281 | +// Listen to real-time events |
| 282 | +const eventStream = await client.event.subscribe() |
| 283 | +for await (const event of eventStream) { |
| 284 | + console.log("Event:", event.type, event.properties) |
| 285 | +} |
| 286 | +``` |
| 287 | + |
| 288 | +--- |
| 289 | + |
| 290 | +## Error handling |
| 291 | + |
| 292 | +The SDK throws typed errors that you can catch and handle: |
| 293 | + |
| 294 | +```typescript |
| 295 | +try { |
| 296 | + const session = await client.session.get({ id: "invalid-id" }) |
| 297 | +} catch (error) { |
| 298 | + console.error("Failed to get session:", error.message) |
| 299 | +} |
| 300 | +``` |
0 commit comments