# ixi-enki/opencode architecture

> Architecture diagram of the GitHub repository ixi-enki/opencode, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ixi-enki/opencode
- Repository: https://github.com/ixi-enki/opencode

## Overview

1. **Monorepo shape**
   This is a TypeScript/Bun monorepo organized around one core product, OpenCode, plus adjacent cloud services, docs, desktop packaging, SDKs, and shared UI/library packages. The main architectural split is between **local agent/runtime code** and **hosted console/platform code**.

2. **Primary product runtime: `packages/opencode`**
   `packages/opencode` is the main AI coding agent. It contains the CLI, TUI, local server, session engine, tool implementations, plugin system, project/worktree management, storage, sync, and the HTTP API used by the local UI surfaces. This is the subsystem that turns user prompts into agent actions against the filesystem and external tools.

3. **Local execution flow**
   User input enters through the CLI/TUI (`src/cli`, `src/cli/cmd/run`, `src/cli/cmd/tui`) or via the local server HTTP API (`src/server/routes/instance/httpapi`). That request is routed into the session engine (`src/session`), which builds prompts, manages retries/compaction/summary state, invokes tools, and streams events back to the UI. The flow is event-driven rather than request/response only.

4. **Agent capabilities and tool boundaries**
   OpenCode’s agent layer is built around concrete tool boundaries: filesystem access, git, LSP, shell/PTy, MCP, patch/edit/write/read/search/webfetch/websearch, questions/permissions, and repo/project discovery. The permission subsystem gates risky operations, while the PTY and shell layers isolate terminal execution from the rest of the app. This is the key boundary between “reasoning” and “side effects.”

5. **Model/provider abstraction**
   `packages/core` and `packages/llm` provide the shared model/provider layer. They normalize many upstream APIs and protocols: OpenAI, Anthropic, Gemini, Bedrock, OpenRouter, Azure, GitHub Copilot, XAI, Cloudflare, and other compatible providers. The core package also contains the effect/runtime utilities and common session/message/domain types used across products.

6. **Session and persistence model**
   OpenCode persists local state through `src/storage`, `src/sync`, `src/session/*.sql.ts`, and migration history under `packages/opencode/migration`. Sessions, messages, workspace metadata, sync state, and usage/event data are stored in SQLite-like local persistence and migrated over time. Sync and projectors keep the UI, server, and storage views consistent.

7. **Project/worktree orchestration**
   `src/project`, `src/worktree`, `src/control-plane`, and related modules manage repository identity, workspace bootstrap, VCS state, and instance lifecycle. This layer is what makes the agent aware of “which codebase am I operating on?” and allows multiple workspaces/instances to coexist cleanly.

8. **Browser/desktop UI surfaces**
   There are two major UI frontends:
   - `packages/app`: a browser-based Solid/Vite app with rich session, file tree, prompt, settings, and layout contexts.
   - `packages/desktop`: an Electron app with a main process, preload bridge, renderer, updater, menus, IPC, and a sidecar/server model.
   Both sit on top of the same product concepts and largely consume shared UI primitives from `packages/ui`.

9. **Shared design system and UI primitives**
   `packages/ui` is the reusable component/theme library: buttons, dialogs, file/message renderers, markdown, toasts, diff views, icons, themes, i18n, and Storybook support. It serves the desktop renderer, web UIs, and documentation/share surfaces, keeping visual and interaction patterns consistent across products.

10. **Hosted console/platform stack**
   `packages/console` is the cloud control plane. It is split into:
   - `app`: SSR web app with routes for auth, workspace admin, billing, models, providers, usage, docs, downloads, webhooks, and the Zen API.
   - `core`: domain logic and DB schema for accounts, auth, billing, providers, workspaces, referrals, models, keys, and benchmarks.
   - `function`: background/log-processing and stats functions.
   - `mail` and `resource`: email templates and infrastructure resources.
   This stack is the SaaS boundary for auth, billing, provider configuration, and hosted API access.

11. **Hosted API/data plane**
   Within the console app, the `zen` routes act like a model/API gateway: they expose chat/models endpoints, enforce IP/key/model rate limits, apply provider selection/stickiness, and track usage. The gateway can proxy multiple upstream providers and is backed by Redis and the console DB. Webhooks and billing integrations (notably Stripe) feed back into this control plane.

12. **Sharing, enterprise, and public web**
   `packages/enterprise` is a separate share-focused app for rendering shared sessions/content. `packages/web` is the public marketing/docs site built with Astro/Starlight-style content collections and localized docs. These are outward-facing surfaces that consume product content but are not part of the local agent runtime.

13. **Telemetry and analytics**
   `packages/stats` is a dedicated analytics/usage pipeline with an ingestion server, core domain/database layer, Athena/Honeycomb-related jobs, and a small public app. It appears to aggregate product usage and inference/provider statistics separately from the main product databases.

14. **Integrations, SDKs, and packaging**
   The repo also ships editor and ecosystem integrations: `sdks/vscode`, `packages/extensions/zed`, `packages/plugin`, and `packages/slack`. `packages/sdk` contains generated JS SDKs from OpenAPI. `packages/containers` and `infra/` support build/deploy/containerization, while `script/` and `.github/workflows` automate release, publishing, docs, and validation.

15. **Cross-cutting infrastructure**
   SST config, Drizzle migrations, Redis, Electron, Astro, Solid, and Bun are the main infrastructure/runtime technologies that materially shape the architecture. Bun is the primary workspace runtime/tooling layer; Drizzle/SQLite back the local and cloud persistence; SST and workflow scripts handle deployment and release orchestration.

Main entry points: [`packages/opencode/src/session`](https://github.com/ixi-enki/opencode/tree/dev/packages/opencode/src/session) (Session engine), [`packages/opencode/src/server`](https://github.com/ixi-enki/opencode/tree/dev/packages/opencode/src/server) (Local server), [`packages/desktop`](https://github.com/ixi-enki/opencode/tree/dev/packages/desktop) (Desktop app), [`packages/app`](https://github.com/ixi-enki/opencode/tree/dev/packages/app) (Web app)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_local["Local runtime"]
  node_opencode_cli["CLI/TUI<br/>runtime entrypoints"]
  node_opencode_server["Local server<br/>http api host"]
  node_opencode_session["Session engine<br/>agent orchestration"]
  node_opencode_tools["Tools<br/>side-effect adapters"]
  node_opencode_perm{{"Permissions<br/>policy gate"}}
  node_opencode_project["Project/runtime<br/>workspace lifecycle"]
  node_opencode_storage[("Storage/sync<br/>local persistence")]
  node_opencode_plugins["Plugins/skills<br/>extensibility"]
end

subgraph group_cloud["Cloud platform"]
  node_console_app["Console app<br/>ssr control plane"]
  node_console_core[("Console core<br/>saas domain")]
  node_zen_gateway["Zen gateway<br/>api gateway"]
  node_stats_stack["Stats pipeline<br/>analytics pipeline"]
end

subgraph group_shared["Shared libraries"]
  node_core_models["Core models<br/>shared domain"]
  node_llm_runtime["LLM runtime<br/>provider runtime"]
  node_ui_lib["UI library<br/>design system"]
end

subgraph group_surfaces["External surfaces"]
  node_app_web["Web app<br/>solid app"]
  node_desktop_app["Desktop app<br/>electron shell"]
  node_enterprise_share["Enterprise share<br/>share app"]
  node_web_site["Docs site<br/>astro site"]
  node_sdk_pkg["SDKs<br/>generated clients"]
end

node_opencode_cli -->|"starts sessions"| node_opencode_session
node_opencode_server -->|"drives runtime"| node_opencode_session
node_opencode_session -->|"invokes tools"| node_opencode_tools
node_opencode_session -->|"checks policy"| node_opencode_perm
node_opencode_session -->|"calls models"| node_llm_runtime
node_opencode_session -->|"uses types"| node_core_models
node_opencode_project -->|"persists state"| node_opencode_storage
node_opencode_server -->|"loads workspace"| node_opencode_project
node_opencode_server -->|"reads/writes"| node_opencode_storage
node_opencode_plugins -->|"extends agent"| node_opencode_session
node_opencode_plugins -->|"registers providers"| node_llm_runtime
node_app_web -->|"syncs to local api"| node_opencode_server
node_desktop_app -->|"renders ui"| node_app_web
node_desktop_app -->|"hosts sidecar"| node_opencode_server
node_app_web -->|"uses components"| node_ui_lib
node_desktop_app -->|"uses components"| node_ui_lib
node_console_app -->|"uses domain"| node_console_core
node_console_app -->|"serves gateway"| node_zen_gateway
node_zen_gateway -->|"proxies providers"| node_llm_runtime
node_stats_stack -->|"reads usage"| node_console_core
node_enterprise_share -->|"renders shares"| node_opencode_session
node_web_site -->|"reuses styles"| node_ui_lib
node_sdk_pkg -->|"targets api"| node_console_app
node_sdk_pkg -->|"targets local api"| node_opencode_server

click node_opencode_cli "https://github.com/ixi-enki/opencode/tree/dev/packages/opencode/src/cli"
click node_opencode_server "https://github.com/ixi-enki/opencode/tree/dev/packages/opencode/src/server"
click node_opencode_session "https://github.com/ixi-enki/opencode/tree/dev/packages/opencode/src/session"
click node_opencode_tools "https://github.com/ixi-enki/opencode/tree/dev/packages/opencode/src/tool"
click node_opencode_perm "https://github.com/ixi-enki/opencode/tree/dev/packages/opencode/src/permission"
click node_opencode_project "https://github.com/ixi-enki/opencode/tree/dev/packages/opencode/src/project"
click node_opencode_storage "https://github.com/ixi-enki/opencode/tree/dev/packages/opencode/src/storage"
click node_opencode_plugins "https://github.com/ixi-enki/opencode/tree/dev/packages/opencode/src/plugin"
click node_core_models "https://github.com/ixi-enki/opencode/tree/dev/packages/core"
click node_llm_runtime "https://github.com/ixi-enki/opencode/tree/dev/packages/llm"
click node_ui_lib "https://github.com/ixi-enki/opencode/tree/dev/packages/ui"
click node_app_web "https://github.com/ixi-enki/opencode/tree/dev/packages/app"
click node_desktop_app "https://github.com/ixi-enki/opencode/tree/dev/packages/desktop"
click node_console_app "https://github.com/ixi-enki/opencode/tree/dev/packages/console/app"
click node_console_core "https://github.com/ixi-enki/opencode/tree/dev/packages/console/core"
click node_zen_gateway "https://github.com/ixi-enki/opencode/tree/dev/packages/console/app/src/routes/zen"
click node_stats_stack "https://github.com/ixi-enki/opencode/tree/dev/packages/stats"
click node_enterprise_share "https://github.com/ixi-enki/opencode/tree/dev/packages/enterprise"
click node_web_site "https://github.com/ixi-enki/opencode/tree/dev/packages/web"
click node_sdk_pkg "https://github.com/ixi-enki/opencode/tree/dev/packages/sdk"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_opencode_cli,node_opencode_server,node_opencode_session,node_opencode_tools,node_opencode_perm,node_opencode_project,node_opencode_storage,node_opencode_plugins toneBlue
class node_console_app,node_console_core,node_zen_gateway,node_stats_stack toneAmber
class node_core_models,node_llm_runtime,node_ui_lib toneMint
class node_app_web,node_desktop_app,node_enterprise_share,node_web_site,node_sdk_pkg toneRose
```

## Components

### Local runtime

OpenCode client, agent, and local server execution.

- [CLI/TUI](https://github.com/ixi-enki/opencode/tree/dev/packages/opencode/src/cli): runtime entrypoints · Command-line and terminal UI surfaces for interacting with the local agent. · `packages/opencode/src/cli`
- [Local server](https://github.com/ixi-enki/opencode/tree/dev/packages/opencode/src/server): http api host · Instance server and HTTP/WebSocket API that backs local UI surfaces and remote access. · `packages/opencode/src/server`
- [Session engine](https://github.com/ixi-enki/opencode/tree/dev/packages/opencode/src/session): agent orchestration · Prompting, retries, compaction, tool loops, message/state transitions, and event streaming. · `packages/opencode/src/session`
- [Tools](https://github.com/ixi-enki/opencode/tree/dev/packages/opencode/src/tool): side-effect adapters · Filesystem, git, shell/PTy, LSP, MCP, patch/edit/write/read/search, web fetch/search, and repository discovery tools. · `packages/opencode/src/tool`
- [Permissions](https://github.com/ixi-enki/opencode/tree/dev/packages/opencode/src/permission): policy gate · Permission evaluation and task gating for risky or destructive operations. · `packages/opencode/src/permission`
- [Project/runtime](https://github.com/ixi-enki/opencode/tree/dev/packages/opencode/src/project): workspace lifecycle · Project, worktree, instance, control-plane, and repository bootstrap orchestration. · `packages/opencode/src/project`
- [Storage/sync](https://github.com/ixi-enki/opencode/tree/dev/packages/opencode/src/storage): local persistence · Session, workspace, usage, and sync persistence with migrations and projector-style consistency. · `packages/opencode/src/storage`
- [Plugins/skills](https://github.com/ixi-enki/opencode/tree/dev/packages/opencode/src/plugin): extensibility · Plugin loading, model/provider integrations, skills, and command/template expansion. · `packages/opencode/src/plugin`

### Cloud platform

Hosted console, gateway, and analytics services.

- [Console app](https://github.com/ixi-enki/opencode/tree/dev/packages/console/app): ssr control plane · Hosted SaaS application for auth, billing, providers, workspaces, docs, and Zen routes. · `packages/console/app`
- [Console core](https://github.com/ixi-enki/opencode/tree/dev/packages/console/core): saas domain · Cloud domain logic and schemas for accounts, auth, billing, workspaces, providers, keys, and referrals. · `packages/console/core`
- [Zen gateway](https://github.com/ixi-enki/opencode/tree/dev/packages/console/app/src/routes/zen): api gateway · Hosted model/API gateway with rate limiting, sticky provider routing, and usage tracking. · `packages/console/app/src/routes/zen`
- [Stats pipeline](https://github.com/ixi-enki/opencode/tree/dev/packages/stats): analytics pipeline · Usage/inference stats ingestion, domain model, backfill, and public stats app. · `packages/stats`

### Shared libraries

Common model, session, UI, and effect/runtime packages.

- [Core models](https://github.com/ixi-enki/opencode/tree/dev/packages/core): shared domain · Shared session, event, project, provider, permission, and utility abstractions used by product code. · `packages/core`
- [LLM runtime](https://github.com/ixi-enki/opencode/tree/dev/packages/llm): provider runtime · Protocol adapters and provider implementations for major model APIs and streaming transports. · `packages/llm`
- [UI library](https://github.com/ixi-enki/opencode/tree/dev/packages/ui): design system · Reusable components, themes, markdown/diff/file renderers, and interaction primitives. · `packages/ui`

### External surfaces

Public web, enterprise sharing, SDKs, editor integrations, and desktop packaging.

- [Web app](https://github.com/ixi-enki/opencode/tree/dev/packages/app): solid app · Browser-based local product UI for sessions, settings, file tree, and prompt composition. · `packages/app`
- [Desktop app](https://github.com/ixi-enki/opencode/tree/dev/packages/desktop): electron shell · Electron main, preload, renderer, updater, menus, IPC, and sidecar/server integration. · `packages/desktop`
- [Enterprise share](https://github.com/ixi-enki/opencode/tree/dev/packages/enterprise): share app · Shared-session rendering and enterprise-facing share surface. · `packages/enterprise`
- [Docs site](https://github.com/ixi-enki/opencode/tree/dev/packages/web): astro site · Public marketing and documentation site with localized content collections. · `packages/web`
- [SDKs](https://github.com/ixi-enki/opencode/tree/dev/packages/sdk): generated clients · Generated JavaScript SDKs for the public API and v2 endpoints. · `packages/sdk`

## Connections

- CLI/TUI → Session engine: starts sessions · CLI/TUI entrypoints create or attach to active sessions and stream events.
- Local server → Session engine: drives runtime · HTTP and websocket routes dispatch into the session engine.
- Session engine → Tools: invokes tools · The agent loop calls concrete tools to inspect and modify the workspace.
- Session engine → Permissions: checks policy · Potentially risky operations are gated by permission evaluation.
- Session engine → LLM runtime: calls models · Session execution delegates to the shared model/provider runtime.
- Session engine → Core models: uses types · Session state, events, prompts, and domain objects are shared through core types.
- Project/runtime → Storage/sync: persists state · Project and instance lifecycle state is stored locally with migrations.
- Local server → Project/runtime: loads workspace · Server routes resolve the active project/worktree/instance context.
- Local server → Storage/sync: reads/writes · API handlers access persisted session, sync, and workspace data.
- Plugins/skills → Session engine: extends agent · Plugins and skills augment commands, prompts, and tool behavior.
- Plugins/skills → LLM runtime: registers providers · Plugin layer contributes provider and model integrations.
- Web app → Local server: syncs to local api · The browser UI consumes the local server API for live session state.
- Desktop app → Web app: renders ui · The Electron shell hosts the renderer UI and system integration.
- Desktop app → Local server: hosts sidecar · Desktop process integrates with the local server/sidecar model.
- Web app → UI library: uses components · The web UI is built on shared UI primitives and themes.
- Desktop app → UI library: uses components · Desktop renderer shares the common UI design system.
- Console app → Console core: uses domain · Hosted app routes are backed by shared cloud domain logic and schema.
- Console app → Zen gateway: serves gateway · The hosted app exposes Zen model/chat endpoints and routing utilities.
- Zen gateway → LLM runtime: proxies providers · Gateway logic maps requests to upstream model providers and protocols.
- Stats pipeline → Console core: reads usage · Analytics and backfill jobs depend on shared cloud database/domain concepts.
- Enterprise share → Session engine: renders shares · Enterprise sharing presents exported session/content artifacts.
- Docs site → UI library: reuses styles · Public site and docs reuse shared presentation patterns and assets.
- SDKs → Console app: targets api · Generated SDKs are produced from the hosted API surface.
- SDKs → Local server: targets local api · SDKs also cover the local server API surface exposed by OpenCode.
