Skip to content

voyax/claude-code-from-source

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Claude Code Source Build

A buildable and runnable version of Anthropic's Claude Code CLI, built from an official source snapshot.

Uses a zero-modification Bun.plugin() preload approach to resolve bun:bundle compile-time features and MACRO.* constant injection — no original source files are touched.

Quick Start

Prerequisites

  • Bun >= 1.3.11
  • Anthropic API Key (for actual conversations)

Install

git clone <repo-url> && cd claude-code
bun install

Run

# Development mode (runs source directly, zero modification)
bun run dev

# Or use the launcher script
./bin/claude-dev

# Non-interactive mode
ANTHROPIC_API_KEY=sk-ant-xxx bun run dev -- -p "your prompt"

# Interactive REPL
ANTHROPIC_API_KEY=sk-ant-xxx bun run dev

Production Build

bun run build          # Bundle to dist/ (~24MB, 426 chunks)
bun ./dist/cli.js      # Run the bundled output

The build output is Node.js-compatible — import.meta.require calls are automatically patched with a createRequire fallback.

Project Structure

claude-code/
├── entrypoints/           # Entry points
│   ├── cli.tsx            #   CLI bootstrap
│   ├── init.ts            #   Initialization pipeline
│   ├── mcp.ts             #   MCP Server entry
│   └── sdk/               #   SDK type definitions
├── main.tsx               # Core CLI logic (Commander.js)
├── tools/                 # 48 built-in tools
│   ├── BashTool/          #   Shell execution + security analysis
│   ├── FileEditTool/      #   File editing
│   ├── AgentTool/         #   Sub-agents / Swarm
│   ├── GrepTool/          #   Code search
│   └── ...
├── services/              # Service layer
│   ├── api/               #   Anthropic API client
│   ├── mcp/               #   MCP protocol implementation
│   ├── compact/           #   Context compaction
│   └── analytics/         #   Telemetry & analytics
├── components/            # React/Ink terminal UI components
├── ink/                   # Custom terminal renderer (not npm ink)
├── commands/              # 103 CLI commands
├── utils/                 # Utilities (566 files)
├── hooks/                 # React hooks
├── constants/             # Configuration constants
├── types/                 # TypeScript type declarations
├── native-ts/             # Pure TS ports of native modules
│   ├── yoga-layout/       #   Flexbox layout engine
│   ├── color-diff/        #   Syntax-highlighted diffs
│   └── file-index/        #   Fuzzy file search
├── packages/              # Monorepo workspace packages
│   ├── @ant/              #   Internal package stubs
│   └── color-diff-napi/   #   NAPI module TS implementation
├── shims/                 # Zero-modification build shims
│   ├── preload.ts         #   Bun.plugin() + MACRO globals
│   └── bun-bundle.ts      #   feature() stub (returns false)
├── scripts/               # Automation tools
│   ├── create-type-stubs.mjs
│   ├── fix-missing-exports.mjs
│   └── health-check.ts
├── build.ts               # Production build script
├── package.json           # Dependencies + workspace config
└── tsconfig.json          # TypeScript config

Build Approach

The Problem

The official source depends on Bun bundler's compile-time features and cannot run directly:

Problem Scope Solution
bun:bundle feature() system 212 files Bun.plugin() intercept → redirect to shim
MACRO.* build-time constants 40+ files Preload script injects globalThis.MACRO
src/ path alias 250 files tsconfig.json paths mapping
@ant/* internal packages ~15 files Monorepo workspace stubs
Missing internal modules ~20 files Auto-generated stubs via scripts

Zero-Modification Approach

All source files remain untouched. Build shims are injected at runtime via Bun's preload mechanism:

bun --preload ./shims/preload.ts ./entrypoints/cli.tsx

shims/preload.ts does two things:

  1. Registers a Bun.plugin() that intercepts import { feature } from 'bun:bundle' and redirects to the shim
  2. Defines globalThis.MACRO with build-time constants (VERSION, BUILD_TIME, etc.)

shims/bun-bundle.ts exports feature() returning false for all flags — equivalent to an external (non-Anthropic) build. This means all internal feature-gated code paths (KAIROS, BRIDGE_MODE, DAEMON, etc.) are cleanly disabled.

Production Build

build.ts uses Bun.build() with:

  • define to inline MACRO constants at compile time (replacing runtime globalThis)
  • Plugin to intercept bun:bundle
  • Code splitting for fast startup
  • Post-processing to patch import.meta.require for Node.js compatibility

API Providers

Provider Configuration
Anthropic Direct ANTHROPIC_API_KEY
AWS Bedrock AWS credentials (env vars or ~/.aws/credentials)
Google Vertex AI GCP credentials (gcloud auth or service account)
Azure Foundry Azure credentials

Development Tools

bun run lint           # Biome linting
bun run lint:fix       # Auto-fix lint issues
bun run format         # Code formatting
bun run health         # Health check (code size, lint, build status)
bun run check:unused   # Dead code detection (Knip)
bun run stubs:create   # Auto-generate missing type stubs from tsc errors
bun run stubs:fix-exports  # Fix missing exports in stub files

Key Technical Details

  • Custom Ink: The terminal UI (ink/) is a complete React-to-terminal renderer using react-reconciler, not the npm ink package
  • Custom Yoga: Layout uses a pure TypeScript flexbox engine (native-ts/yoga-layout/), not the npm yoga-layout package
  • Bun Runtime APIs: All Bun.* API calls (Bun.spawn, Bun.hash, Bun.semver, etc.) are guarded with typeof Bun !== 'undefined' and have Node.js fallbacks
  • Feature Flags: 77 compile-time feature flags control dead-code elimination; all return false in this build
  • Security: Multi-layered permission system with dangerous command detection, shell injection prevention, sandbox isolation, and SSRF protection

License

For educational and research purposes only. Claude Code is a product of Anthropic — all rights belong to Anthropic.

About

Buildable Claude Code CLI from official source — zero-modification Bun.plugin() preload approach

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages