How ixi-enki/opencode works
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: session Session engine · server Local server · desktop Desktop app · app Web app
Components 20
Local runtime
OpenCode client, agent, and local server execution.
- CLI/TUI · Command-line and terminal UI surfaces for interacting with the local agent.
packages/opencode/src/cli - Local server · Instance server and HTTP/WebSocket API that backs local UI surfaces and remote access.
packages/opencode/src/server - Session engine · Prompting, retries, compaction, tool loops, message/state transitions, and event streaming.
packages/opencode/src/session - Tools · Filesystem, git, shell/PTy, LSP, MCP, patch/edit/write/read/search, web fetch/search, and repository discovery tools.
packages/opencode/src/tool - Permissions · Permission evaluation and task gating for risky or destructive operations.
packages/opencode/src/permission - Project/runtime · Project, worktree, instance, control-plane, and repository bootstrap orchestration.
packages/opencode/src/project - Storage/sync · Session, workspace, usage, and sync persistence with migrations and projector-style consistency.
packages/opencode/src/storage - Plugins/skills · Plugin loading, model/provider integrations, skills, and command/template expansion.
packages/opencode/src/plugin
Cloud platform
Hosted console, gateway, and analytics services.
- Console app · Hosted SaaS application for auth, billing, providers, workspaces, docs, and Zen routes.
packages/console/app - Console core · Cloud domain logic and schemas for accounts, auth, billing, workspaces, providers, keys, and referrals.
packages/console/core - Zen gateway · Hosted model/API gateway with rate limiting, sticky provider routing, and usage tracking.
packages/console/app/src/routes/zen - Stats 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 · Shared session, event, project, provider, permission, and utility abstractions used by product code.
packages/core - LLM runtime · Protocol adapters and provider implementations for major model APIs and streaming transports.
packages/llm - UI library · 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 · Browser-based local product UI for sessions, settings, file tree, and prompt composition.
packages/app - Desktop app · Electron main, preload, renderer, updater, menus, IPC, and sidecar/server integration.
packages/desktop - Enterprise share · Shared-session rendering and enterprise-facing share surface.
packages/enterprise - Docs site · Public marketing and documentation site with localized content collections.
packages/web - SDKs · Generated JavaScript SDKs for the public API and v2 endpoints.
packages/sdk
Connections 24
- 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.