|
| 1 | +# Claude Code — 编译修复版 |
| 2 | + |
| 3 | +**[English](README.md)** | **[中文](README_CN.md)** |
| 4 | + |
| 5 | +基于 2026-03-31 泄露的 Claude Code CLI 源码,修复所有缺失文件、断裂引用和运行时错误,使其可以正常编译运行。 |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 项目背景 |
| 10 | + |
| 11 | +2026 年 3 月 31 日,Anthropic 的 Claude Code CLI 完整源码通过 npm 注册表中的 `.map` 文件泄露。原始泄露版本**无法编译** — 缺少 22+ 个源文件、存在断裂的内部引用、依赖 Anthropic 内部包、并使用了未发布的 Bun 特性(`bun:bundle`)。 |
| 12 | + |
| 13 | +本仓库修复了以上所有问题。最终效果:一条 `bun build` 命令即可生成可用的 ~23MB 单文件 bundle。 |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## 相比原始泄露版本的修改 |
| 18 | + |
| 19 | +### 补充的缺失源文件(22 个 stub) |
| 20 | + |
| 21 | +泄露的源码引用了不存在的文件。已创建对应的 stub: |
| 22 | + |
| 23 | +| 文件 | 用途 | |
| 24 | +|------|------| |
| 25 | +| `src/global.d.ts` | TypeScript 全局类型声明 | |
| 26 | +| `src/utils/protectedNamespace.ts` | 命名空间保护 | |
| 27 | +| `src/utils/useEffectEvent.ts` | React `useEffectEvent` polyfill | |
| 28 | +| `src/entrypoints/sdk/coreTypes.generated.ts` | SDK 生成类型 | |
| 29 | +| `src/entrypoints/sdk/runtimeTypes.ts` | SDK 运行时类型 | |
| 30 | +| `src/entrypoints/sdk/toolTypes.ts` | SDK 工具类型 | |
| 31 | +| `src/tools/REPLTool/REPLTool.ts` | REPL 工具 stub | |
| 32 | +| `src/tools/SuggestBackgroundPRTool/` | PR 建议工具 stub | |
| 33 | +| `src/tools/VerifyPlanExecutionTool/` | 计划验证工具 stub | |
| 34 | +| `src/tools/WorkflowTool/` | 工作流工具 stub | |
| 35 | +| `src/tools/TungstenTool/TungstenLiveMonitor.tsx` | Tungsten 监控 stub | |
| 36 | +| `src/commands/agents-platform/` | Agent 平台命令 stub | |
| 37 | +| `src/commands/assistant/` | 助手命令 stub | |
| 38 | +| `src/components/agents/SnapshotUpdateDialog.tsx` | 快照对话框 stub | |
| 39 | +| `src/assistant/AssistantSessionChooser.tsx` | 会话选择器 stub | |
| 40 | +| `src/services/compact/snipCompact.ts` | 剪裁压缩 stub | |
| 41 | +| `src/services/compact/cachedMicrocompact.ts` | 微压缩 stub | |
| 42 | +| `src/services/contextCollapse/` | 上下文折叠 stub | |
| 43 | +| `src/ink/devtools.ts` | 开发工具 stub | |
| 44 | +| `src/skills/bundled/verify/` | 验证 skill stub | |
| 45 | +| `src/utils/filePersistence/types.ts` | 文件持久化类型 stub | |
| 46 | + |
| 47 | +### 源码修复 |
| 48 | + |
| 49 | +| 修复项 | 文件 | 说明 | |
| 50 | +|--------|------|------| |
| 51 | +| `useEffectEvent` 导入 | `src/components/tasks/BackgroundTasksDialog.tsx`, `src/state/AppState.tsx` | React 19 实验性 Hook 在 `react-reconciler@0.31` 中不可用 — 改为本地 polyfill | |
| 52 | +| 版本检查跳过 | `src/utils/autoUpdater.ts` | `assertMinVersion()` 会访问 Anthropic 服务器 — 直接跳过 | |
| 53 | +| 组织验证跳过 | `src/main.tsx` | `validateForceLoginOrg()` 需要 Anthropic 认证 — 注释掉 | |
| 54 | +| 认证检查跳过 | `src/main.tsx` | 登录流程依赖 Anthropic OAuth — 启用自动执行 | |
| 55 | +| `SandboxManager` 替换 | `node_modules/@anthropic-ai/sandbox-runtime/` | 将 14 个方法的 stub 替换为 [anthropic-experimental/sandbox-runtime](https://github.com/anthropic-experimental/sandbox-runtime) 的真实实现 | |
| 56 | + |
| 57 | +### Bun 兼容 Shim |
| 58 | + |
| 59 | +| 文件 | 用途 | |
| 60 | +|------|------| |
| 61 | +| `shims/macro.ts` | 提供 `MACRO` 全局变量(VERSION、BUILD_TIME 等)— 原本由 Anthropic 内部 Bun 构建注入 | |
| 62 | +| `shims/bun-bundle.ts` | 提供 `feature()` 函数 — 替代 Anthropic 内部的 `bun:bundle` | |
| 63 | + |
| 64 | +### 缺失依赖(补充 28 个包) |
| 65 | + |
| 66 | +原始 `package.json` 不完整。已补充 28 个缺失依赖,包括 `@anthropic-ai/` 系列 SDK、OpenTelemetry 包和其他必要模块。 |
| 67 | + |
| 68 | +### 内部包处理 |
| 69 | + |
| 70 | +两个 Anthropic 内部包无法从 npm 安装: |
| 71 | + |
| 72 | +- `@anthropic-ai/sandbox-runtime` — 已替换为[开源实现](https://github.com/anthropic-experimental/sandbox-runtime) |
| 73 | +- `@ant/claude-for-chrome-mcp` — 已在 `node_modules/` 中创建 stub |
| 74 | + |
| 75 | +--- |
| 76 | + |
| 77 | +## 快速开始 |
| 78 | + |
| 79 | +### 前置要求 |
| 80 | + |
| 81 | +- **Bun** 1.3+ — `curl -fsSL https://bun.sh/install | bash` |
| 82 | +- **Node.js** 18+(可选,Bun 自带 npm) |
| 83 | + |
| 84 | +### 编译 |
| 85 | + |
| 86 | +```bash |
| 87 | +git clone git@github.com:roger2ai/Claude-Code-Compiled.git |
| 88 | +cd Claude-Code-Compiled |
| 89 | + |
| 90 | +# 安装依赖 |
| 91 | +bun install |
| 92 | + |
| 93 | +# 修补 Commander.js(多字符短标志不支持) |
| 94 | +# 详见 docs/BUILD.md §4.1 — 每次 bun install 后需重新应用 |
| 95 | + |
| 96 | +# 编译 |
| 97 | +bun build shims/macro.ts src/main.tsx --target=bun --outdir=./dist |
| 98 | + |
| 99 | +# 合并为单文件 |
| 100 | +cat dist/shims/macro.js dist/src/main.js > dist/bundle.js |
| 101 | +echo 'if (typeof main === "function") main().catch(e => { console.error(e); process.exit(1); });' >> dist/bundle.js |
| 102 | +``` |
| 103 | + |
| 104 | +输出:`dist/bundle.js`(~23 MB,~5,750 模块,~300ms 编译时间) |
| 105 | + |
| 106 | +### 运行 |
| 107 | + |
| 108 | +```bash |
| 109 | +# 查看帮助(不需要 API key) |
| 110 | +bun dist/bundle.js --help |
| 111 | + |
| 112 | +# 交互式 REPL(需要真实终端 + API key) |
| 113 | +export ANTHROPIC_API_KEY=你的密钥 |
| 114 | +bun dist/bundle.js |
| 115 | + |
| 116 | +# 单次执行模式 |
| 117 | +bun dist/bundle.js -p "say hello" |
| 118 | +``` |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | +## 项目结构 |
| 123 | + |
| 124 | +``` |
| 125 | +claude-code/ |
| 126 | +├── src/ # 源码(~1,900 个 TypeScript 文件,512K+ 行) |
| 127 | +│ ├── main.tsx # CLI 入口 |
| 128 | +│ ├── QueryEngine.ts # LLM 查询引擎 |
| 129 | +│ ├── Tool.ts # 工具类型定义 |
| 130 | +│ ├── tools/ # 43 个工具实现 |
| 131 | +│ ├── commands/ # 80+ 个斜杠命令 |
| 132 | +│ ├── components/ # 346 个 React/Ink UI 组件 |
| 133 | +│ ├── services/ # 21 个服务模块 |
| 134 | +│ ├── screens/ # 全屏 UI(REPL、Doctor 等) |
| 135 | +│ └── utils/ # 290+ 个工具函数文件 |
| 136 | +├── shims/ # Bun 兼容 shim |
| 137 | +├── docs/ # 架构与编译文档 |
| 138 | +├── dist/ # 编译输出(gitignored) |
| 139 | +└── package.json # 依赖声明(574 个包) |
| 140 | +``` |
| 141 | + |
| 142 | +--- |
| 143 | + |
| 144 | +## 文档 |
| 145 | + |
| 146 | +| 文档 | 内容 | |
| 147 | +|------|------| |
| 148 | +| [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) | 全景架构总览 | |
| 149 | +| [docs/ARCHITECTURE-TOOLS.md](docs/ARCHITECTURE-TOOLS.md) | 43 个工具详细分析 | |
| 150 | +| [docs/ARCHITECTURE-SERVICES.md](docs/ARCHITECTURE-SERVICES.md) | 21 个服务模块 | |
| 151 | +| [docs/ARCHITECTURE-COMPONENTS.md](docs/ARCHITECTURE-COMPONENTS.md) | 346 个 UI 组件 | |
| 152 | +| [docs/ARCHITECTURE-COMMANDS.md](docs/ARCHITECTURE-COMMANDS.md) | 命令、Skill、Plugin | |
| 153 | +| [docs/ARCHITECTURE-UTILS.md](docs/ARCHITECTURE-UTILS.md) | 工具函数层 | |
| 154 | +| [docs/ARCHITECTURE-BRIDGE-REMOTE.md](docs/ARCHITECTURE-BRIDGE-REMOTE.md) | IDE 桥接与远程会话 | |
| 155 | +| [docs/API-CONFIG.md](docs/API-CONFIG.md) | API 配置(环境变量、认证、代理) | |
| 156 | +| [docs/BUILD.md](docs/BUILD.md) | 详细编译指南与所有补丁 | |
| 157 | +| [docs/REFACTORING-ASSESSMENT.md](docs/REFACTORING-ASSESSMENT.md) | 重构可行性评估 | |
| 158 | + |
| 159 | +--- |
| 160 | + |
| 161 | +## 已知限制 |
| 162 | + |
| 163 | +1. **TUI 需要真实终端** — 管道或非 TTY 环境下静默退出 |
| 164 | +2. **需要 API key** — 实际对话必须设置 `ANTHROPIC_API_KEY` |
| 165 | +3. **部分工具是 stub** — REPLTool、WorkflowTool 等为空实现 |
| 166 | +4. **macOS Keychain** — Linux 上回退到明文文件存储 |
| 167 | +5. **WSL2 沙箱** — 需要 `apt install bubblewrap socat` 才能使用沙箱功能 |
| 168 | +6. **Commander.js 补丁** — 多字符短标志(`-d2e`)每次 `bun install` 后需手动修补 `node_modules` |
| 169 | + |
| 170 | +--- |
| 171 | + |
| 172 | +## 原始来源 |
| 173 | + |
| 174 | +源码由 [@Fried_rice](https://x.com/Fried_rice) 于 2026-03-31 泄露。所有原始源码归 [Anthropic](https://www.anthropic.com) 所有。本仓库仅作为可编译参考,不用于生产环境。 |
0 commit comments