> For the complete documentation index, see [llms.txt](https://www.opencodebook.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.opencodebook.xyz/en/chapter_02_project_structure_and_development_environment/2.2_monorepo_structure_analysis.md). Agents can also search and read this documentation through its [MCP server](https://www.opencodebook.xyz/~gitbook/mcp).

# 2.2 Monorepo Structure Analysis

> **Model**: claude-opus-4-6 (anthropic/claude-opus-4-6) **Generation Date**: 2025-02-17

***

## 2.2.1 Root Directory File Overview

Before diving into the source code, let's first clarify the key files in the root directory:

```
opencode/
├── package.json        # Workspace root configuration
├── tsconfig.json       # TypeScript base configuration
├── turbo.json          # Turborepo task orchestration
├── sst.config.ts       # SST (cloud infrastructure) configuration
├── bunfig.toml         # Bun configuration
├── flake.nix           # Nix development environment
├── bun.lock            # Dependency lock file
├── packages/           # All sub-packages
├── sdks/               # IDE SDKs
├── infra/              # Infrastructure code
├── specs/              # Design specification documents
├── script/             # Global scripts
├── patches/            # Third-party package patches
└── nix/                # Nix-related configuration
```

**Key information in `package.json`**:

```json
{
  "name": "opencode",
  "private": true,
  "type": "module",
  "packageManager": "bun@1.3.8",
  "workspaces": {
    "packages": [
      "packages/*",
      "packages/console/*",
      "packages/sdk/js",
      "packages/slack"
    ]
  }
}
```

The `workspaces` field defines the locations of all sub-packages in the Monorepo. Bun scans these paths for all `package.json` files and manages them as workspace members.

> **Extended Explanation: What is a Monorepo?**
>
> A Monorepo (Monolithic Repository) is a strategy of managing multiple related projects within a single Git repository. The opposite approach is a Polyrepo (one repository per project).
>
> **Advantages of a Monorepo**:
>
> * **Simple code sharing**: Sub-packages can directly reference each other without publishing to npm
> * **Atomic commits**: Changes spanning multiple sub-packages can be completed in a single commit
> * **Unified build process**: All sub-packages' builds and tests are managed uniformly through Turborepo
> * **Consistent dependency versions**: All sub-packages use the same versions of third-party dependencies
>
> **How Turborepo works**: Turborepo is a Monorepo build system developed by Vercel. Its core capabilities are:
>
> 1. **Task orchestration**: Determines build order based on dependency relationships between sub-packages
> 2. **Parallel execution**: Tasks without dependencies run in parallel
> 3. **Intelligent caching**: Determines whether a rebuild is needed based on source file hashes
>
> For example, when you run `bun turbo typecheck`, Turborepo analyzes the dependency relationships of all sub-packages, performs type checking in parallel according to topological sort order, and caches the results.

**`tsconfig.json`**:

```json
{
  "extends": "@tsconfig/bun/tsconfig.json",
  "compilerOptions": {}
}
```

Very concise -- it inherits Bun's official TypeScript configuration. Each sub-package has its own more detailed `tsconfig.json`.

## 2.2.2 `packages/` Directory Overview -- Responsibilities of 17 Sub-Packages

`packages/` is the core of the entire project, containing 17 sub-packages. Their responsibilities and relationships are shown in the following diagram:

```
                          ┌─────────────┐
                          │   desktop   │ (Tauri Desktop App)
                          └──────┬──────┘
                                 │ embeds
                          ┌──────▼──────┐
                ┌────────►│     app     │◄────────┐
                │         │ (SolidJS    │         │
                │         │  Web Front) │         │
                │         └──────┬──────┘         │
                │                │ uses            │
                │         ┌──────▼──────┐         │
                │         │     ui      │         │
                │         │ (Shared     │         │
                │         │  Components)│         │
                │         └─────────────┘         │
                │                                 │
         ┌──────┴──────┐                   ┌──────┴──────┐
         │     sdk     │ ◄─────────────── │   opencode   │
         │ (TS SDK,    │   OpenAPI Gen    │(Core Engine) │
         │  Auto-Gen)  │                   │             │
         └─────────────┘                   └──────┬──────┘
                                                  │ uses
                                           ┌──────▼──────┐
                                           │    plugin    │
                                           │ (Plugin API  │
                                           │  Type Defs)  │
                                           └─────────────┘
```

Detailed description of each sub-package:

| Sub-Package    | Path                   | Responsibility                                                                                           | Tech Stack         |
| -------------- | ---------------------- | -------------------------------------------------------------------------------------------------------- | ------------------ |
| **opencode**   | `packages/opencode/`   | **Core engine**. Contains the CLI, Server, and all business logic (Session, Tool, Agent, Provider, etc.) | TypeScript, Bun    |
| **app**        | `packages/app/`        | Web front-end application. Provides a browser interface for using OpenCode                               | SolidJS, Vite      |
| **ui**         | `packages/ui/`         | Shared UI component library. Used by `app` and other front-end projects                                  | SolidJS            |
| **sdk**        | `packages/sdk/`        | TypeScript client SDK. Auto-generated from the OpenAPI specification                                     | OpenAPI Codegen    |
| **plugin**     | `packages/plugin/`     | Type definitions for the Plugin interface. Plugin developers depend on this package for type support     | TypeScript         |
| **web**        | `packages/web/`        | Official website (opencode.ai)                                                                           | Astro              |
| **console**    | `packages/console/`    | Cloud console (includes app, core, function, mail, resource sub-modules)                                 | --                 |
| **enterprise** | `packages/enterprise/` | Enterprise features (SSO, audit logs, etc.)                                                              | TypeScript         |
| **containers** | `packages/containers/` | Docker containerization support                                                                          | Dockerfile         |
| **desktop**    | `packages/desktop/`    | Desktop application                                                                                      | Tauri (Rust + Web) |
| **function**   | `packages/function/`   | Serverless functions (for cloud-based features)                                                          | TypeScript         |
| **identity**   | `packages/identity/`   | Brand assets (Logo SVG/PNG)                                                                              | --                 |
| **script**     | `packages/script/`     | Build scripts and development tools                                                                      | TypeScript         |
| **slack**      | `packages/slack/`      | Slack integration                                                                                        | TypeScript         |
| **extensions** | `packages/extensions/` | IDE extensions (currently includes the Zed extension)                                                    | TOML               |
| **docs**       | `packages/docs/`       | Documentation                                                                                            | Markdown           |
| **util**       | `packages/util/`       | Cross-package shared utility functions                                                                   | TypeScript         |

> **Advice for readers**: The source code analysis in this book primarily focuses on `packages/opencode/` (the core engine). Other sub-packages are only covered when discussing topics such as the front-end, plugin interfaces, or the SDK. If you only want to understand OpenCode's core architecture, you only need to focus on `packages/opencode/src/`.

## 2.2.3 `sdks/vscode/` -- VSCode Extension

Outside of `packages/`, there is a separate `sdks/vscode/` directory containing OpenCode's VSCode extension:

```
sdks/vscode/
├── src/            # Extension source code
├── images/         # Extension icons
├── package.json    # VSCode extension manifest
├── esbuild.js      # Build configuration
└── README.md       # Extension documentation
```

The VSCode extension communicates with OpenCode's embedded HTTP Server to provide AI features within the editor. It is not a required component for running OpenCode, but rather an optional enhancement.

## 2.2.4 `infra/` -- SST Infrastructure as Code

```
infra/
├── app.ts          # Web App deployment
├── console.ts      # Console deployment
├── enterprise.ts   # Enterprise infrastructure
├── secret.ts       # Secret management
└── stage.ts        # Environment stage definitions
```

> **Extended Explanation: SST (Serverless Stack)**
>
> SST is a framework for deploying full-stack applications on AWS. It allows developers to define cloud infrastructure using TypeScript code (Infrastructure as Code, IaC), rather than manually configuring it in the AWS console.
>
> The `sst.config.ts` file in the root directory is the SST entry configuration file. This part is unrelated to the core functionality of the OpenCode CLI and is primarily used for deploying OpenCode's web version and cloud services.

For readers of this book, the `infra/` directory is not a focus area. Its presence demonstrates that OpenCode is not merely a CLI tool -- it also has a complete cloud infrastructure supporting its online services.
