What is the high-level architecture of OpenCode?
what's the high level architecture of opencode?
Perfect! Now I have a good understanding. Let me provide a comprehensive summary:
##High-Level Architecture of OpenCode
OpenCode is an open-source AI coding agent with a sophisticated, modular architecture. Here's the overview:
###Core Components
-
Main CLI (
packages/opencode)- The heart of the system
- Built with Bun runtime and written in TypeScript
- Provides commands like
run,generate,serve,debug,auth, etc. - Uses
yargsfor CLI argument parsing
-
Server (
packages/opencode/src/server)- Hono HTTP server (lightweight web framework)
- Exposes REST/OpenAPI endpoints for all functionality
- Uses Server-Sent Events (SSE) for streaming responses
- WebSocket support via Hono for real-time communication
- Handles CORS with intelligent origin whitelisting
-
Agent System (
packages/opencode/src/agent)- Multiple agents that can be switched between:
- build: Default agent with full access for development
- plan: Read-only agent for analysis and code exploration
- general: Subagent for complex searches and multitask work
- Agents have configurable:
- Permission rulesets
- Model selection
- Temperature and top-p settings
- Custom prompts
- Multiple agents that can be switched between:
-
Session Management (
packages/opencode/src/session)- Handles conversation state and message history
- Features include:
- Message compaction (condense long histories)
- Session prompts (system instructions)
- Session status tracking
- Revert capability
- Summary generation
-
Provider Integration (
packages/opencode/src/provider)- Model-agnostic architecture - supports Claude, OpenAI, Google, local models
- Uses Vercel AI SDK for LLM abstraction
- Custom OpenAI-compatible provider implementation
- Provider authentication handling
-
Event Bus (
packages/opencode/src/bus)- Pub/Sub architecture for internal communication
- Type-safe events with Zod validation
- Global bus for cross-instance events
- Instance-scoped event subscriptions
###Project & Development Features
-
Project Management (
packages/opencode/src/project)- Worktree support
- Version control integration (git)
- File system operations
- Project bootstrapping
-
Development Tools
- LSP Client (
packages/opencode/src/lsp): Language Server Protocol integration - File Operations (
packages/opencode/src/file): Ripgrep for searching, file watching - Patch System (
packages/opencode/src/patch): File modification and diffing - Permission System (
packages/opencode/src/permission): Fine-grained access control - Tool Registry (
packages/opencode/src/tool): Extensible tool system - MCP (
packages/opencode/src/mcp): Model Context Protocol support with OAuth
- LSP Client (
-
Terminal UI (
packages/opencode/src/cli/cmd/tui)- Built with TSX components
- Interactive terminal UI for user interactions
###Supporting Services
-
SDK (
packages/sdk/js)- JavaScript SDK for OpenCode programmatic access
- OpenAPI documentation generation
-
Web Interface (
packages/web)- Astro-based static website for documentation and landing page
- Published to opencode.ai
-
Console (
packages/console)- Multi-part web application:
app: Main web UI for managing workspaces (SolidJS + Vite)core: Backend business logic with Drizzle ORMfunction: Lambda/serverless functionsresource: Infrastructure resourcesmail: Email services
- Multi-part web application:
-
Desktop (
packages/desktop)- Desktop application build (BETA) - Available for macOS, Windows, Linux
-
Additional Packages
extensions: IDE extensions (VS Code, etc.)plugin: Plugin systemscript: Scripting systemslack: Slack integrationui: Shared UI component libraryutil: Shared utilitiesidentity: Authentication/identityenterprise: Enterprise featuresfunction: Serverless functions
###Infrastructure
- SST (Serverless Stack) for AWS deployment configuration
- Turbo monorepo orchestration tool
- Bun package manager and runtime
- Drizzle ORM for database operations
- Hono OpenAPI integration with auto-documentation
###Key Architectural Patterns
- Namespace-based Organization: Most modules use TypeScript namespaces for organizing related functionality
- Instance State Management: Projects are isolated in "instances" with scoped state
- Type Safety: Heavy use of Zod schemas for validation
- Composability: Agent permissions, formatters, and tools are composed/merged
- Streaming: SSE and streaming responses for long-running operations
- Client/Server Separation: CLI can run locally while being driven remotely (future mobile support)
The architecture is designed to be provider-agnostic, extensible, and terminal-first, while maintaining the ability to run in various execution contexts (CLI, web, desktop, mobile).
END OF POST