Complete toolkit for configuring and extending OpenCode: agent creation, custom slash commands, configuration management, plugin development, and SDK usage.
98
98%
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Advisory
Suggest reviewing before use
Complete API reference for @opencode-ai/sdk.
Creates both an OpenCode client and starts a local server.
import { createOpencode } from "@opencode-ai/sdk"
const { client, server } = await createOpencode({
hostname?: string, // Default: "127.0.0.1"
port?: number, // Default: 4096
timeout?: number, // Default: 5000ms
config?: Config // Custom config overrides
})
// Clean up
server.close()Creates only the client (connects to existing server).
import { createOpencodeClient } from "@opencode-ai/sdk"
const client = createOpencodeClient({
baseUrl?: string, // Default: "http://127.0.0.1:4096"
directory?: string, // Working directory
throwOnError?: boolean, // Throw on API errors
responseStyle?: "data" | "fields" // Response format
})client.session.*
List all sessions.
const { data, error } = await client.session.list()
// data: Session[]Create a new session.
const { data, error } = await client.session.create({
body?: {
title?: string,
parentID?: string
}
})
// data: SessionGet a specific session.
const { data, error } = await client.session.get({
path: { id: string }
})
// data: SessionDelete a session.
const { data, error } = await client.session.delete({
path: { id: string }
})Update session properties.
const { data, error } = await client.session.update({
path: { id: string },
body: {
title?: string,
pinned?: boolean
}
})Send a message to a session.
const { data, error } = await client.session.prompt({
path: { id: string },
body: {
parts: Part[], // Message content
model?: { // Optional model override
providerID: string,
modelID: string
},
noReply?: boolean // Don't trigger AI response
}
})type Part =
| { type: "text", text: string }
| { type: "file", path: string, content?: string }
| { type: "image", url: string }Send message and return immediately (async processing).
const { data, error } = await client.session.promptAsync({
path: { id: string },
body: {
parts: Part[],
model?: { providerID: string, modelID: string }
}
})Get all messages for a session.
const { data, error } = await client.session.messages({
path: { id: string }
})
// data: Message[]Get a specific message.
const { data, error } = await client.session.message({
path: { id: string, messageID: string }
})
// data: MessageSend a slash command.
const { data, error } = await client.session.command({
path: { id: string },
body: {
command: string // e.g., "/help", "/init"
}
})Execute a shell command.
const { data, error } = await client.session.shell({
path: { id: string },
body: {
command: string
}
})Get session status (idle, busy, etc.).
const { data, error } = await client.session.status({
path: { id: string }
})Abort current session activity.
const { data, error } = await client.session.abort({
path: { id: string }
})Fork a session at a specific message.
const { data, error } = await client.session.fork({
path: { id: string },
body: {
messageID: string
}
})Get child sessions (forks).
const { data, error } = await client.session.children({
path: { id: string }
})Share or unshare a session.
const { data, error } = await client.session.share({
path: { id: string }
})
// Returns share URL
const { data, error } = await client.session.unshare({
path: { id: string }
})Get the git diff for session changes.
const { data, error } = await client.session.diff({
path: { id: string }
})Revert or restore messages.
const { data, error } = await client.session.revert({
path: { id: string },
body: { messageID: string }
})
const { data, error } = await client.session.unrevert({
path: { id: string }
})Generate session summary.
const { data, error } = await client.session.summarize({
path: { id: string }
})Get todo list for session.
const { data, error } = await client.session.todo({
path: { id: string }
})Initialize AGENTS.md for project.
const { data, error } = await client.session.init({
path: { id: string }
})client.project.*
List all projects.
const { data, error } = await client.project.list()
// data: Project[]Get current project.
const { data, error } = await client.project.current()
// data: Projectclient.file.*
List files in a directory.
const { data, error } = await client.file.list({
query: {
path: string,
recursive?: boolean
}
})Read file content.
const { data, error } = await client.file.read({
query: {
path: string
}
})Get file status (git status).
const { data, error } = await client.file.status()client.find.*
Search for text in files.
const { data, error } = await client.find.text({
query: {
query: string,
path?: string,
caseSensitive?: boolean,
regex?: boolean
}
})Find files by name.
const { data, error } = await client.find.files({
query: {
query: string,
path?: string
}
})Find code symbols.
const { data, error } = await client.find.symbols({
query: {
query: string
}
})client.tool.*
Get list of all tool IDs.
const { data, error } = await client.tool.ids()
// data: string[]Get tools with JSON schemas for a provider/model.
const { data, error } = await client.tool.list({
query: {
providerID: string,
modelID: string
}
})client.event.*
Subscribe to server-sent events.
const result = await client.event.subscribe()
for await (const event of result.events) {
// event.type: string
// event.data: any
console.log(event)
}
// Or with options
const result = await client.event.subscribe({
query: { sessionID?: string }
})session.createdsession.deletedsession.updatedmessage.createdmessage.updatedtool.startedtool.completedpermission.requestedclient.config.*
Get current configuration.
const { data, error } = await client.config.get()Update configuration.
const { data, error } = await client.config.update({
body: {
theme?: string,
model?: { providerID: string, modelID: string }
}
})List configured providers.
const { data, error } = await client.config.providers()client.provider.*
List available providers.
const { data, error } = await client.provider.list()Get authentication methods for providers.
const { data, error } = await client.provider.auth()Start OAuth flow.
const { data, error } = await client.provider.oauth.authorize({
path: { provider: string }
})Handle OAuth callback.
const { data, error } = await client.provider.oauth.callback({
path: { provider: string },
query: { code: string, state?: string }
})client.mcp.*
Get MCP server status.
const { data, error } = await client.mcp.status()Dynamically add MCP server.
const { data, error } = await client.mcp.add({
body: {
name: string,
command: string,
args?: string[],
env?: Record<string, string>
}
})client.tui.*
Control the terminal UI programmatically.
Append text to the prompt.
const { data, error } = await client.tui.appendPrompt({
body: { text: string }
})Submit the current prompt.
const { data, error } = await client.tui.submitPrompt()Clear the prompt.
const { data, error } = await client.tui.clearPrompt()Show a toast notification.
const { data, error } = await client.tui.showToast({
body: {
message: string,
type?: "info" | "success" | "warning" | "error"
}
})Open various dialogs.
await client.tui.openHelp()
await client.tui.openSessions()
await client.tui.openThemes()
await client.tui.openModels()Execute a TUI command.
const { data, error } = await client.tui.executeCommand({
body: { command: string }
})client.pty.*
Manage pseudo-terminal sessions.
const { data, error } = await client.pty.list()const { data, error } = await client.pty.create({
body: {
cols?: number,
rows?: number,
cwd?: string
}
})await client.pty.get({ path: { id: string } })
await client.pty.update({ path: { id: string }, body: { cols, rows } })
await client.pty.remove({ path: { id: string } })
await client.pty.connect({ path: { id: string } })client.auth.*
Set authentication credentials.
const { data, error } = await client.auth.set({
path: { provider: string },
body: {
type: "key" | "oauth",
key?: string,
access?: string,
refresh?: string,
expires?: number
}
})Import types directly from the SDK:
import type {
Session,
Message,
Part,
Project,
Model,
Provider,
Permission,
UserMessage,
Auth,
Config,
Event
} from "@opencode-ai/sdk"const { data, error, response } = await client.session.get({
path: { id: "invalid" }
})
if (error) {
console.error("Error:", error)
console.error("Status:", response.status)
} else {
console.log("Session:", data)
}
// Or with throwOnError
try {
const client = createOpencodeClient({ throwOnError: true })
const { data } = await client.session.get({ path: { id: "test" } })
} catch (e) {
console.error("API Error:", e)
}