|
| 1 | +# Architecture Overview |
| 2 | + |
| 3 | +Codebuff is a TypeScript monorepo (Bun workspaces) that provides an AI-powered coding assistant via a CLI, SDK, and web API. |
| 4 | + |
| 5 | +## Package Dependency Graph |
| 6 | + |
| 7 | +``` |
| 8 | + ┌──────────┐ |
| 9 | + │ cli/ │ TUI client (OpenTUI + React) |
| 10 | + └────┬─────┘ |
| 11 | + │ |
| 12 | + ┌────▼─────┐ |
| 13 | + ┌───────│ sdk/ │ JS/TS SDK |
| 14 | + │ └────┬─────┘ |
| 15 | + │ │ |
| 16 | + ┌───────▼────────┐ │ |
| 17 | + │ agent-runtime/ │◄──┘ Agent execution engine |
| 18 | + └───────┬────────┘ |
| 19 | + │ |
| 20 | + ┌───────────────┼───────────────┐ |
| 21 | + │ │ │ |
| 22 | + ┌─────▼─────┐ ┌─────▼─────┐ ┌─────▼─────┐ |
| 23 | + │ agents/ │ │ common/ │ │ internal/ │ |
| 24 | + └───────────┘ └─────┬─────┘ └─────┬─────┘ |
| 25 | + │ │ |
| 26 | + ┌─────┼─────┐ ┌─────┼─────────┐ |
| 27 | + │ │ │ │ │ │ |
| 28 | + billing/ bigquery/ code-map/ web/ |
| 29 | +``` |
| 30 | + |
| 31 | +## Packages |
| 32 | + |
| 33 | +### `cli/` — TUI Client |
| 34 | + |
| 35 | +The user-facing terminal UI, built with [OpenTUI](https://github.com/nickhudkins/opentui) (a React renderer for terminals) and React hooks. |
| 36 | + |
| 37 | +- **Entry point:** `src/index.tsx` → `src/app.tsx` → `src/chat.tsx` |
| 38 | +- **Key responsibilities:** |
| 39 | + - Renders the chat interface, agent output, tool call results, and status indicators |
| 40 | + - Manages user input, slash commands (`/help`, `/usage`), and agent mode selection (DEFAULT, MAX, PLAN) |
| 41 | + - Handles authentication (login polling, OAuth), session persistence, and chat history |
| 42 | + - Calls `client.run()` from the SDK and processes streaming events |
| 43 | +- **Depends on:** `sdk`, `common` |
| 44 | + |
| 45 | +### `sdk/` — JavaScript/TypeScript SDK |
| 46 | + |
| 47 | +The public SDK used by the CLI and available to external users via `@codebuff/sdk` on npm. |
| 48 | + |
| 49 | +- **Entry point:** `src/client.ts` (`CodebuffClient`) → `src/run.ts` (`run()`) |
| 50 | +- **Key responsibilities:** |
| 51 | + - Orchestrates agent runs: initializes session state, registers tool handlers, calls `callMainPrompt()` |
| 52 | + - **Executes tool calls locally** on the user's machine (file edits, terminal commands, code search) |
| 53 | + - Manages model provider selection: Claude OAuth, ChatGPT OAuth, or Codebuff backend |
| 54 | + - Handles credentials, retry logic, and error transformation |
| 55 | +- **Depends on:** `agent-runtime`, `common`, `internal` (for OpenAI-compatible provider) |
| 56 | + |
| 57 | +### `packages/agent-runtime/` — Agent Execution Engine |
| 58 | + |
| 59 | +The core agent loop that drives LLM inference, tool execution, and multi-step reasoning. |
| 60 | + |
| 61 | +- **Entry point:** `src/main-prompt.ts` → `src/run-agent-step.ts` (`loopAgentSteps()`) |
| 62 | +- **Key responsibilities:** |
| 63 | + - Runs the agent loop: LLM call → process response → execute tool calls → repeat |
| 64 | + - Manages agent templates, system prompts, and tool definitions |
| 65 | + - Handles subagent spawning, programmatic agent steps (`handleSteps` generators) |
| 66 | + - Processes the AI SDK stream (`streamText()`) and routes tool calls to the SDK |
| 67 | + - Manages context token counting, cache debugging, and cost tracking |
| 68 | +- **Depends on:** `common`, `agents` (for agent templates) |
| 69 | + |
| 70 | +### `common/` — Shared Library |
| 71 | + |
| 72 | +Shared types, utilities, constants, and tool definitions used across the entire monorepo. |
| 73 | + |
| 74 | +- **Key areas:** |
| 75 | + - `src/types/` — TypeScript types: `SessionState`, `AgentOutput`, `Message`, contracts for DI |
| 76 | + - `src/tools/` — Tool parameter schemas (Zod), tool names, and tool call validation |
| 77 | + - `src/constants/` — Model configs, agent IDs, OAuth settings, billing constants |
| 78 | + - `src/util/` — Error handling (`ErrorOr<T>`), message utilities, string helpers, XML parsing |
| 79 | + - `src/templates/` — Agent definition types, initial `.agents/` directory template |
| 80 | + - `src/testing/` — Mock factories for database, filesystem, analytics, fetch, timers |
| 81 | +- **Depends on:** nothing (leaf package) |
| 82 | + |
| 83 | +### `agents/` — Agent Definitions |
| 84 | + |
| 85 | +Prompt-based and programmatic agent definitions that ship with Codebuff. |
| 86 | + |
| 87 | +- **Key agents:** |
| 88 | + - `base2/` — The default agent (base2, base2-max, base2-free, base2-plan) |
| 89 | + - `editor/` — Code editing specialist with best-of-N selection |
| 90 | + - `file-explorer/` — File picker, code searcher, directory lister, glob matcher |
| 91 | + - `thinker/` — Deep reasoning agent with best-of-N variants |
| 92 | + - `reviewer/` — Code review agent with multi-prompt variant |
| 93 | + - `researcher/` — Web search and docs search agents |
| 94 | + - `general-agent/` — General-purpose agents (opus-agent, gpt-5-agent) |
| 95 | + - `commander.ts` / `commander-lite.ts` — Terminal command execution agents |
| 96 | + - `context-pruner.ts` — Conversation summarization to manage context length |
| 97 | +- **Depends on:** `common` (for agent definition types and tool params) |
| 98 | + |
| 99 | +### `web/` — Next.js Web Application |
| 100 | + |
| 101 | +The Codebuff web server, marketing site, and API. |
| 102 | + |
| 103 | +- **Key areas:** |
| 104 | + - `src/app/api/v1/chat/completions/` — The main LLM proxy endpoint (routes to OpenRouter, Fireworks, OpenAI) |
| 105 | + - `src/app/api/v1/` — REST API: agent runs, feedback, usage, web search, docs search, token count |
| 106 | + - `src/app/api/auth/` — NextAuth.js authentication (GitHub OAuth) |
| 107 | + - `src/app/api/stripe/` — Billing: credit purchases, subscriptions, webhooks |
| 108 | + - `src/app/api/agents/` — Agent registry: publish, validate, fetch |
| 109 | + - `src/app/api/orgs/` — Organization management: teams, billing, repos |
| 110 | + - `src/app/` — Marketing pages, docs (MDX via contentlayer), user profile, pricing |
| 111 | + - `src/llm-api/` — LLM provider integrations (OpenRouter, Fireworks, OpenAI, SiliconFlow, CanopyWave) |
| 112 | +- **Depends on:** `common`, `internal`, `billing`, `bigquery` |
| 113 | + |
| 114 | +### `packages/internal/` — Internal Utilities |
| 115 | + |
| 116 | +Server-side utilities, database schema, and vendor forks shared between `web` and `sdk`. |
| 117 | + |
| 118 | +- **Key areas:** |
| 119 | + - `src/db/` — Drizzle ORM schema (`schema.ts`), migrations, Docker Compose for local Postgres |
| 120 | + - `src/env.ts` — Server environment variable validation (@t3-oss/env-nextjs) |
| 121 | + - `src/loops/` — Loops email service integration (transactional emails) |
| 122 | + - `src/openai-compatible/` — Forked OpenAI-compatible AI SDK provider (used by the SDK to call the Codebuff backend) |
| 123 | + - `src/openrouter-ai-sdk/` — Forked OpenRouter AI SDK provider (used by the web server) |
| 124 | + - `src/templates/` — Agent template fetching and validation |
| 125 | +- **Depends on:** `common` |
| 126 | + |
| 127 | +### `packages/billing/` — Billing & Credits |
| 128 | + |
| 129 | +Credit management, subscription handling, and usage tracking. |
| 130 | + |
| 131 | +- **Key components:** |
| 132 | + - `balance-calculator.ts` — Credit balance calculation (free, purchased, rollover, subscription grants) |
| 133 | + - `subscription.ts` — Subscription plan management, block grants, weekly limits |
| 134 | + - `grant-credits.ts` — Credit grant operations (referral, purchase, admin, free) |
| 135 | + - `auto-topup.ts` — Automatic credit purchases when balance is low |
| 136 | + - `usage-service.ts` — Usage data aggregation |
| 137 | + - `credit-delegation.ts` — Organization credit delegation |
| 138 | +- **Depends on:** `common` (for DB access, Stripe utils, types) |
| 139 | + |
| 140 | +### `packages/bigquery/` — Analytics Data |
| 141 | + |
| 142 | +Google BigQuery integration for storing agent interaction traces and usage analytics. |
| 143 | + |
| 144 | +- **Tables:** `traces` (agent interactions), `relabels` (fine-tuning relabeling data) |
| 145 | +- **Trace types:** file selection calls, file trees, agent responses, training data, model grading |
| 146 | +- **Depends on:** `common` |
| 147 | + |
| 148 | +### `packages/code-map/` — Code Parsing |
| 149 | + |
| 150 | +Tree-sitter based source code parser that extracts function/variable names for file tree display. |
| 151 | + |
| 152 | +- **Supports:** TypeScript, JavaScript, Python, Go, Rust, Java, C, C++, C#, Ruby, PHP |
| 153 | +- **Used by:** The `read_subtree` tool to show parsed variable names alongside the file tree |
| 154 | +- **Depends on:** nothing (leaf package) |
| 155 | + |
| 156 | +### `packages/build-tools/` — Build Utilities |
| 157 | + |
| 158 | +Custom build executors, currently just the Infisical secrets integration. |
| 159 | + |
| 160 | +### `.agents/` — Local Agent Templates |
| 161 | + |
| 162 | +Project-specific agent definitions for this repository. These are loaded automatically by the agent runtime. |
| 163 | + |
| 164 | +- CLI agent templates (claude-code-cli, codex-cli, gemini-cli, codebuff-local-cli) |
| 165 | +- Notion query agents |
| 166 | +- Skills (cleanup, meta, review) |
| 167 | + |
| 168 | +### `evals/` — Evaluation Framework |
| 169 | + |
| 170 | +BuffBench evaluation suite for measuring agent performance on real-world coding tasks. |
| 171 | + |
| 172 | +- **Workflow:** Pick commits → generate eval tasks → run agents → judge results → extract lessons |
| 173 | +- **Runners:** Codebuff, Claude Code, Codex |
| 174 | +- **Depends on:** `common`, `agent-runtime`, `sdk` |
| 175 | + |
| 176 | +### `freebuff/` — Free Tier Product |
| 177 | + |
| 178 | +A separate free-to-use version of Codebuff with its own CLI binary and web app. |
| 179 | + |
| 180 | +- `freebuff/cli/` — Standalone CLI binary and release scripts |
| 181 | +- `freebuff/web/` — Minimal Next.js app for auth (login, onboarding) |
| 182 | +- Uses ChatGPT OAuth for free LLM access (no Codebuff credits required) |
| 183 | + |
| 184 | +### `scripts/` — Development & Operations |
| 185 | + |
| 186 | +Developer tooling, analytics scripts, and service management. |
| 187 | + |
| 188 | +- `start-services.ts` / `stop-services.ts` / `status-services.ts` — Local dev environment management |
| 189 | +- `tmux/` — tmux helper scripts for CLI E2E testing |
| 190 | +- Analytics: DAU calculation, MRR, subscriber profitability, model usage |
| 191 | +- Release: changelog generation, credit grants, worktree management |
| 192 | + |
| 193 | +## Key Architectural Patterns |
| 194 | + |
| 195 | +### Dependency Injection via Contracts |
| 196 | + |
| 197 | +The codebase avoids tight coupling between packages using contract types in `common/src/types/contracts/`: |
| 198 | + |
| 199 | +- `database.ts` — DB access functions (`GetUserInfoFromApiKeyFn`, `StartAgentRunFn`, etc.) |
| 200 | +- `llm.ts` — LLM calling functions (`PromptAiSdkStreamFn`, `PromptAiSdkFn`) |
| 201 | +- `analytics.ts` — Event tracking (`TrackEventFn`) |
| 202 | +- `client.ts` — Client-server communication (`RequestToolCallFn`, `SendActionFn`) |
| 203 | +- `env.ts` — Environment variable access (`BaseEnv`, `ClientEnv`, `CiEnv`) |
| 204 | + |
| 205 | +This allows the agent-runtime to be used by both the SDK (local execution) and the web server (if needed) without direct dependencies. |
| 206 | + |
| 207 | +### ErrorOr Pattern |
| 208 | + |
| 209 | +Prefer `ErrorOr<T>` return values (`success(value)` / `failure(error)`) over throwing exceptions. Defined in `common/src/util/error.ts`. |
| 210 | + |
| 211 | +### Local Tool Execution |
| 212 | + |
| 213 | +Tool calls (file edits, terminal commands, code search) execute **on the user's machine** via the SDK, not on the server. The agent-runtime sends tool call requests through `requestToolCall`, which the SDK handles locally. |
| 214 | + |
| 215 | +### AI SDK Integration |
| 216 | + |
| 217 | +The project uses Vercel's [AI SDK](https://sdk.vercel.ai/) (`ai` package) for LLM interactions: |
| 218 | + |
| 219 | +- `streamText()` for streaming responses |
| 220 | +- `generateText()` / `generateObject()` for non-streaming |
| 221 | +- Custom `OpenAICompatibleChatLanguageModel` provider for the Codebuff backend |
| 222 | +- `APICallError` for HTTP error handling (see [Error Schema](./error-schema.md)) |
| 223 | + |
| 224 | +### Agent Template System |
| 225 | + |
| 226 | +Agents are defined as templates with: |
| 227 | + |
| 228 | +- **Prompt agents** — System prompt + tool list + spawnable subagents |
| 229 | +- **Programmatic agents** — `handleSteps` generator functions that run in a sandbox |
| 230 | +- Templates live in `agents/` (shipped) and `.agents/` (project-local) |
| 231 | +- Users can publish agents to the Codebuff registry |
| 232 | + |
| 233 | +## Development |
| 234 | + |
| 235 | +```bash |
| 236 | +bun up # Start web server + database |
| 237 | +bun start-cli # Start CLI (separate terminal) |
| 238 | +bun ps # Check running services |
| 239 | +bun down # Stop services |
| 240 | +bun typecheck # Run all type checks |
| 241 | +bun test # Run all tests |
| 242 | +``` |
| 243 | + |
| 244 | +See the [Request Flow](./request-flow.md) doc for the detailed path a prompt takes through the system. |
0 commit comments