How nishal77/opencode works
1. This is a Bun/TypeScript monorepo for OpenCode: a local AI coding agent with multiple user surfaces, shared domain libraries, a typed HTTP API, and separate cloud services for auth, billing, sharing, and analytics. The main architectural center is the agent runtime plus its session/tool/provider model.
2. packages/core is the domain kernel. It owns the canonical definitions for sessions, tools, prompts, permissions, providers/models, filesystem/git/project behavior, config parsing, database schemas/migrations, and effect-based runtime primitives. Most other packages depend on this layer rather than duplicating business logic.
3. packages/opencode is the main product/runtime assembly. It combines CLI commands, the interactive TUI, session processing, LLM orchestration, plugin loading, workspace/bootstrap logic, sync, storage, and the local HTTP API server into the end-user executable.
4. packages/llm is the provider/protocol adapter layer. It normalizes differences between OpenAI, Anthropic, Gemini, Bedrock, Cloudflare, OpenRouter, xAI, GitHub Copilot, and other providers, including streaming and tool-call behavior, into one internal model that OpenCode can consume consistently.
5. The user-facing surfaces are split by interaction mode: packages/tui for terminal UX, packages/desktop for the Electron app, packages/app for the larger SolidJS app, and packages/ui as the shared component/theme library. These clients mostly render state and send actions into the local runtime/API boundary.
6. The core data flow is: user prompt/input → session processor/runner → model provider request → tool execution → persisted events/messages → UI projections. Tool results can mutate files, run shell commands, query LSP/MCP integrations, or emit structured outputs, and those changes are reflected back through session views, timelines, and sync streams.
7. packages/server is a typed HTTP boundary over the domain model. Its route groups are organized by feature area—agent, session, filesystem, model, provider, pty, permission, question, reference, project-copy, integration, and health—with middleware for authorization, schema validation, and session/location scoping.
8. Plugins and integrations are intentionally isolated. packages/plugin exposes the v2 effect-based plugin API, while OpenCode’s own runtime has plugin loaders, host adapters, and workspace adapters. MCP servers, IDE/LSP support, Git integration, and provider-specific extensions are all treated as pluggable capabilities at the edges.
9. Persistence is split between local embedded state and service-side databases. Core/opencode carry Drizzle-backed schemas and migrations for sessions, workspaces, credentials, events, and permissions; the console/stats services have their own databases and ingestion paths for usage and analytics. Long-lived state is therefore shared by schema, but separated by deployment target.
10. The cloud/control-plane side lives mainly in packages/console, packages/enterprise, and packages/stats. Console handles auth, billing, model proxying/rate limiting, and usage batching; enterprise hosts share views and APIs; stats ingests telemetry and serves analytics. The infra/ modules wire these services into SST-managed deployments.
11. packages/web is the documentation and marketing site, built with Astro/Starlight and multilingual content. packages/sdk/js is the generated OpenAPI-based client/server SDK that external consumers and internal tools use to talk to the same APIs as the built-in apps.
12. Cross-cutting tooling is substantial: Storybook supports the UI library, Playwright and recorded integration tests cover the app surfaces, Electron Builder packages desktop, and GitHub Actions plus release scripts drive publishing. These tools reinforce the same package graph rather than introducing separate architectural tiers.
Main entry points: opencode opencode · server server · core core · ui ui
Components 20
Shared Core
Canonical domain, session, tool, plugin, and model abstractions used across runtimes and services.
- core · Canonical session/tool/provider/config/persistence model and effect runtime primitives.
packages/core - llm · Normalizes provider protocols, streaming, and tool-call behavior into one internal model.
packages/llm - plugin · Effect-based plugin surface for agents, commands, providers, skills, and host integration.
packages/plugin - effect-sqlite · Effect-compatible SQLite driver and migrator utilities used by storage layers.
packages/effect-drizzle-sqlite
Local Runtime
The end-user executable assembly: CLI, agent runtime, local server, plugin host, persistence, and workspace control.
- opencode · Main executable assembly combining CLI, TUI, session processing, plugin loading, sync, and local HTTP API.
packages/opencode - server · Standalone typed HTTP boundary exposing agent, session, filesystem, provider, and related routes.
packages/server - cli · Thin command framework for service control, daemon boot, and runtime entrypoints.
packages/cli
User Surfaces
Terminal, desktop, web app, docs site, and shared UI packages that present the runtime state.
- tui · Interactive terminal surface for sessions, prompts, permissions, plugins, and workspace workflows.
packages/tui - desktop · Desktop renderer/main process integration around the shared runtime and web UI.
packages/desktop - app · Web app surface for sessions, settings, timelines, and workspace management.
packages/app - ui · Shared design system, themes, icons, and cross-surface UI primitives.
packages/ui - web · Astro/Starlight documentation and marketing site with multilingual content.
packages/web
Cloud Services
Auth, billing, sharing, analytics, and deployment wiring for the control plane and hosted services.
- console app · Hosted console for auth, billing, provider management, usage, and enterprise workflows.
packages/console/app - console core · Shared control-plane domain models, schemas, and billing/provider/account logic.
packages/console/core - console fn · Background functions for auth and log/stat processing.
packages/console/function - stats core · Stats/telemetry domain, normalization, runtime, and ingestion helpers.
packages/stats/core - stats server · Analytics ingestion and sync server.
packages/stats/server - infra · SST app definitions for local, console, enterprise, lake, monitoring, secrets, and stats deployments.
infra
Connections 27
- llm → core: provider model · LLM adapters consume the canonical model and tool/session contracts from core.
- plugin → core: plugin contracts · The plugin API is built on core abstractions for agents, commands, providers, and skills.
- effect-sqlite → core: persistence runtime · SQLite drivers and migrations support the core database and effect layers.
- opencode → core: domain kernel · The main runtime assembles session, tool, config, and persistence logic from core.
- opencode → llm: model execution · OpenCode uses provider adapters to execute model requests and streaming responses.
- opencode → plugin: plugin host · The local runtime loads and runs plugins through the shared plugin API.
- opencode → server: local api · The executable embeds a typed HTTP boundary for clients and integration surfaces.
- opencode → tui: terminal surface · The runtime powers the interactive terminal experience.
- opencode → cli: entrypoints · CLI commands bootstrap and control the opencode runtime and daemon.
- opencode → ui: shared ui · The runtime and TUI reuse shared UI primitives and theming.
- server → core: typed boundary · HTTP handlers are thin wrappers around the shared domain model.
- server → llm: provider routes · Server routes expose provider/model actions backed by normalized LLM adapters.
- tui → ui: rendering · The terminal UI consumes shared components, icons, and theme primitives.
- app → ui: components · The web app is built on the shared design system and theme layer.
- app → server: api client · The app talks to the local typed HTTP API boundary for sessions and workspace state.
- desktop → opencode: embedded runtime · Electron wraps the local product runtime and session/server lifecycle.
- desktop → app: renderer · Desktop reuses the web app-like renderer and shared UI stack.
- web → ui: brand system · The docs/marketing site shares visual primitives and assets with the product UI.
- console app → console core: control plane · The hosted console uses the shared account, billing, provider, and workspace domain.
- console fn → console core: background jobs · Serverless functions operate on the same control-plane domain and schemas.
- stats core → console core: analytics domain · Stats and console share some business concepts around users, workspaces, and usage.
- stats server → stats core: ingestion · The analytics server persists and serves data through the stats domain layer.
- infra → console app: deploy · Infrastructure defines the deployment topology for the console application.
- infra → stats server: deploy · Infrastructure wires the stats ingestion service into managed deployments.
- sdk js → server: openapi client · Generated SDKs target the same HTTP API exposed by the server package.
- script → opencode: publish · Repository scripts drive builds, releases, and maintenance for the main runtime.
- script → sdk js: release · Automation builds and publishes generated SDK artifacts.