Backend: OpenCode Server API
Relevant source files
The following files were used as context for generating this wiki page:The OpenCode Server API is the core backend component of the OpenCode ecosystem. It is a TypeScript application built to run on the Bun runtime. The server's primary responsibility is to manage agent sessions, process user requests, execute tools, and facilitate communication between the AI agents and various clients, such as the terminal user interface (TUI).
This server exposes a RESTful API that is formally defined by an OpenAPI specification. This specification acts as a single source of truth for the API contract, enabling the automatic generation of type-safe client SDKs for different languages, such as Go and JavaScript. This client-server architecture allows for a decoupled system where clients can interact with the agent's logic without needing to know the implementation details.
Sources: packages/opencode/src/server/server.ts, packages/opencode/AGENTS.md, README.md
Architecture Overview
OpenCode employs a client-server architecture. The backend server, written in TypeScript, handles all core business logic related to AI agent sessions. Clients, like the Go-based TUI, communicate with this server through a well-defined REST API. Any changes to the server's API endpoints require regenerating the client SDKs to maintain synchronization.
Sources: packages/opencode/AGENTS.md, CONTRIBUTING.md
The following diagram illustrates the high-level architecture and the SDK generation flow.
This diagram shows how the server.ts file provides an openapi.json specification, which is then used by generate.ts to create client SDKs for various frontends.
Sources: packages/opencode/src/server/server.ts, packages/sdk/openapi.json, packages/sdk/stainless/generate.ts, packages/opencode/AGENTS.md
API Server
The heart of the backend is the server.ts file. It uses a simple router to define API endpoints for managing agent sessions and messages.
Key responsibilities include:
- Session Management: Creating, retrieving, and managing the lifecycle of agent sessions.
- Message Handling: Receiving user prompts, passing them to the appropriate agent session, and streaming responses back.
- WebSocket Communication: Establishing a WebSocket connection for real-time, bidirectional communication with the client for a given session.
- API Spec Hosting: Serving the
openapi.jsonfile, which allows clients and developers to understand the API contract.
Sources: packages/opencode/src/server/server.ts
Key API Endpoints
The server exposes several REST endpoints, primarily for session and message management.
| Method | Path | Description |
|---|---|---|
POST | /v1/session | Creates a new agent session. |
GET | /v1/session/{id} | Retrieves details for a specific session. |
POST | /v1/session/{id}/message | Sends a new message/prompt to a session. |
GET | /v1/session/{id}/message | Retrieves all messages for a session. |
GET | /v1/session/{id}/message/{msgId} | Retrieves a specific message from a session. |
GET | /v1/session/{id}/socket | Upgrades the HTTP connection to a WebSocket for real-time communication. |
GET | /openapi.json | Serves the OpenAPI 3.0 specification file. |
Sources: packages/opencode/src/server/server.ts, packages/sdk/openapi.json
API Specification (openapi.json)
The packages/sdk/openapi.json file is the machine-readable contract for the entire backend API. It is generated based on the routes and schemas defined in server.ts. This file is crucial as it serves as the single source of truth for generating client-side SDKs, ensuring that clients and the server are always in sync.
Below is an example snippet from openapi.json defining the endpoint to create a new session.
{
"/v1/session": {
"post": {
"summary": "Session.create",
"operationId": "Session_create",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/SessionCreate"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Session",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Session"
}
}
}
}
}
}
}
}
Sources: packages/sdk/openapi.json
SDK Generation
Client SDKs are generated automatically from the openapi.json specification. This process is managed by scripts and tools like Stainless, as seen in packages/sdk/stainless/generate.ts.
The development workflow mandates that after modifying server endpoints in server.ts, the developer must regenerate the client SDKs. This ensures that the type-safe client libraries used by the TUI and other frontends are always up-to-date with the latest API changes.
Sources: packages/sdk/stainless/generate.ts, CONTRIBUTING.md, packages/opencode/AGENTS.md
Generation Flow
The following diagram shows the steps involved from modifying the server to generating a new SDK.
This flowchart illustrates the SDK generation process.
Sources: CONTRIBUTING.md, packages/sdk/stainless/generate.ts, packages/opencode/AGENTS.md
Session and Agent Interaction Flow
The server's main purpose is to orchestrate the interaction between a user (via a client) and an AI agent. The process involves creating a session, sending messages, and receiving streamed responses.
The following sequence diagram details a typical user interaction:
This diagram shows the sequence of events from creating a session to receiving a response from the agent.
Sources: packages/opencode/src/server/server.ts, packages/opencode/src/session/prompt/gemini.txt
Conclusion
The OpenCode Server API is a well-structured and robust backend system that serves as the central nervous system for the entire OpenCode platform. By leveraging a Bun and TypeScript stack, a formal OpenAPI specification, and an automated SDK generation process, it provides a scalable and maintainable foundation. This architecture allows for clear separation of concerns between the core agent logic on the server and the various user-facing clients, enabling rapid development and ensuring consistency across the ecosystem.
Sources: packages/opencode/src/server/server.ts, packages/opencode/AGENTS.md, README.md