What is the high-level architecture of OpenCode?

dev
20399bbtui: rename kvStore to store for consistency
This post might have stale content, as dev is 2079 commits ahead.
Avatar of roguesherlock
Akash
posted

what's the high level architecture of opencode?

Avatar of anthropic/claude-haiku-4.5
Claude Haiku 4.5
commented

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

  1. 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 yargs for CLI argument parsing
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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

  1. Project Management (packages/opencode/src/project)

    • Worktree support
    • Version control integration (git)
    • File system operations
    • Project bootstrapping
  2. 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
  3. Terminal UI (packages/opencode/src/cli/cmd/tui)

    • Built with TSX components
    • Interactive terminal UI for user interactions

###Supporting Services

  1. SDK (packages/sdk/js)

    • JavaScript SDK for OpenCode programmatic access
    • OpenAPI documentation generation
  2. Web Interface (packages/web)

    • Astro-based static website for documentation and landing page
    • Published to opencode.ai
  3. Console (packages/console)

    • Multi-part web application:
      • app: Main web UI for managing workspaces (SolidJS + Vite)
      • core: Backend business logic with Drizzle ORM
      • function: Lambda/serverless functions
      • resource: Infrastructure resources
      • mail: Email services
  4. Desktop (packages/desktop)

    • Desktop application build (BETA) - Available for macOS, Windows, Linux
  5. Additional Packages

    • extensions: IDE extensions (VS Code, etc.)
    • plugin: Plugin system
    • script: Scripting system
    • slack: Slack integration
    • ui: Shared UI component library
    • util: Shared utilities
    • identity: Authentication/identity
    • enterprise: Enterprise features
    • function: 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

What is the high-level architecture of OpenCode? — anomalyco/opencode