基于 @anthropic-ai/claude-code@2.1.88 npm 包通过 source map 还原的 TypeScript 源码,深度分析 Claude Code 的核心实现机制,整理为 7 页交互式 HTML 报告。
源码来源:https://github.com/ChinaSiro/claude-code-sourcemap
- 直接打开:https://skyexu.github.io/claude-source-learning/
- clone 项目浏览器打开
index.html即可
页面间通过顶部导航栏跳转,纯静态 HTML + CSS,无需任何构建或依赖
| 页面 | 内容 |
|---|---|
总览 (index.html) |
架构 SVG 图、关键数字统计、模块导航入口 |
Agent 循环 (agent-loop.html) |
query.ts 主循环流程图、三条错误恢复路径、Task 状态机 |
系统提示词 (system-prompt.html) |
14 段提示词逐段中英对照解析、BOUNDARY 缓存分隔、4 层优先级覆盖链 |
工具控制 (tool-control.html) |
5 种权限模式、12 步决策管道、规则引擎、AI 分类器与拒绝跟踪 |
多 Agent (multi-agent.html) |
Coordinator 编排架构图、三种消息传递机制、MCP 工具动态集成、Fork 子 Agent |
内置 Agent (built-in-agents.html) |
Explore / Plan / Verification / General Purpose / Guide 五大 Agent 的完整提示词与设计分析 |
借鉴思路 (insights.html) |
10 个可复用的 Agent 工程设计模式、关键源文件速查表 |
npm pack @anthropic-ai/claude-code@2.1.88
↓ 解压获得 package/cli.js.map
↓ node extract-sources.js
↓ restored-src/src/ ← 1884 个 .ts/.tsx 源文件
报告重点分析的源文件:
| 文件 | 行数 | 角色 |
|---|---|---|
src/query.ts |
1729 | Agent 主循环(while true) |
src/QueryEngine.ts |
1295 | SDK 会话管理层 |
src/constants/prompts.ts |
914 | 系统提示词构建 |
src/utils/permissions/permissions.ts |
1500+ | 权限决策管道 |
src/tools/AgentTool/built-in/*.ts |
— | 5 个内置 Agent 定义 |
src/services/tools/toolOrchestration.ts |
188 | 工具并发/串行执行 |
src/coordinator/coordinatorMode.ts |
300+ | 多 Agent 编排模式 |
src/services/mcp/client.ts |
3348 | MCP 工具发现与注册 |
本报告基于公开发布的 npm 包逆向分析,仅用于技术学习和 Agent 架构研究。
