结合Claude的回答我们来看下Claude Code的实现原理和细节
一、诞生故事:从「听歌小工具」到企业级产品 Claude Code 的想法源自一个命令行工具,最初只是用 Claude 来显示工程师在工作时听的音乐,但在获得文件系统访问权限后,它在 Anthropic 内部迅速传播开来。
时间线 :
2024年9月: 第一个原型 2024年11月: 内部狗粮测试版本(第1天20%工程师使用,第5天50%使用) 2025年: 正式发布稳定版 这个发展路径很重要 → 它说明 Claude Code 不是为了炫技而做的产品,而是 Anthropic 工程师真实需求驱动的工具 。
二、技术栈:「自举」的艺术 技术栈采用 TypeScript、React、Ink、Yoga 和 Bun,选择这些技术是为了”符合发行版”并发挥模型的优势。有趣的事实:Claude Code 中 90% 的代码是由它自己编写的!
核心组件 :1 2 3 4 5 6 7 8 9 10 11 ┌─────────────────────────────────────┐ │ Terminal UI (Ink + React) │ ← 终端界面 ├─────────────────────────────────────┤ │ Agent Orchestration Layer │ ← 代理编排层 ├─────────────────────────────────────┤ │ Tool System (File/Shell/Git) │ ← 工具系统 ├─────────────────────────────────────┤ │ MCP Integration Layer │ ← MCP集成层 ├─────────────────────────────────────┤ │ Claude API (Sonnet 4.5/Opus 4) │ ← AI模型层 └─────────────────────────────────────┘
为什么用这些技术?
TypeScript : 类型安全,适合复杂的状态管理React + Ink : 在终端中渲染 React 组件(是的,你没看错!)Yoga : Facebook 的布局引擎,用于终端 UI 布局Bun : 超快的 JavaScript 运行时三、工作原理:AI 代理的「感知-决策-执行」循环 1. 基础流程 1 2 3 用户输入 → Claude分析 → 制定计划 → 调用工具 → 执行操作 → 反馈结果 ↑ ↓ └──────────────────── 循环迭代 ────────────────────────┘
2. 核心机制详解 A. 上下文理解系统
Claude Code 维护对整个项目结构的认知,可以从网上查找最新信息,并通过 MCP 可以从 Google Drive、Figma 和 Slack 等外部数据源提取信息
具体实现:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class ContextManager { buildProjectIndex ( ) { - 扫描文件树 - 识别语言和框架 - 构建依赖关系图 - 标记重要文件(配置、入口点等) } agenticSearch (query ) { - 语义搜索相关文件 - 理解代码关系 - 定位依赖项 - 返回相关上下文 } compactContext (preserveInstructions ) { - 保留核心信息 - 移除已完成的历史 - 压缩冗余内容 } }
B. 工具调用系统
Claude Code 有一套标准化的工具接口:1 2 3 4 5 6 7 8 9 10 interface Tool { read : 读取文件 write : 写入文件 bash : 执行Shell 命令 grep : 代码搜索 diff : 查看差异 git : Git 操作 mcp_*: MCP 服务器提供的工具 }
工具调用流程 :
Claude 决策 : “我需要查看 logging.py 文件”权限检查 : 询问用户或检查白名单执行工具 : read_file("logging.py")返回结果 : 文件内容 → Claude 的上下文继续推理 : 基于内容做下一步决策四、子代理系统 (Subagents):分工协作的艺术 对于复杂问题应该强烈考虑使用子代理。让 Claude 使用子代理来验证细节或调查它可能有的特定问题,尤其是在对话或任务早期,往往能保留上下文可用性,而在效率损失方面几乎没有什么缺点
子代理架构 1 2 3 4 5 6 7 8 9 主 Claude (协调者) ├── 代码审查员 (Code Reviewer) │ └── 专注:逻辑错误、安全漏洞、性能问题 ├── 测试工程师 (Test Engineer) │ └── 专注:编写和运行测试 ├── 文档编写者 (Doc Writer) │ └── 专注:注释、README、变更日志 └── 调试专家 (Debugger) └── 专注:定位和修复 bug
实现示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 --- name: code-reviewer description: 综合代码质量和可维护性分析 tools: read, grep, diff, lint_runner --- 你是一名专家代码审查员: 1 . **逻辑错误和 bug** - 可能导致系统故障 2 . **安全漏洞** - 数据保护问题 3 . **性能问题** - 影响用户体验 4 . **可维护性问题** - 增加技术债务 5 . **代码风格和一致性** - 符合项目标准 - 分析业务逻辑正确性 - 检查错误处理和边界情况覆盖 - 验证适当的输入验证和清理 - 评估对现有功能的影响 - 评估测试覆盖率和质量
使用方式 :1 2 3 4 5 6 7 8 claude> "完成这个功能后,让代码审查员检查它" 主 Claude: "我会让专门的审查员来检查这个..." → 启动 code-reviewer 子代理 → 审查完成,报告返回给主 Claude → 主 Claude 根据反馈修改代码
五、MCP (Model Context Protocol):Claude Code 的「神经系统」 MCP(模型上下文协议)是一个开源标准,用于连接 AI 应用程序到外部系统。使用 MCP,像 Claude 或 ChatGPT 这样的 AI 应用程序可以连接到数据源(例如本地文件、数据库)、工具(例如搜索引擎、计算器)和工作流(例如专门的提示)
MCP 核心概念 把 MCP 想象成 “AI 的 USB-C 接口” → 标准化的连接方式1 2 3 4 5 6 7 8 9 10 11 12 Claude Code (主机) ↓ MCP 客户端 ↓ [MCP 协议] ← 标准化通信 ↓ MCP 服务器们 ├── GitHub MCP ├── Slack MCP ├── Google Drive MCP ├── Database MCP └── 自定义 MCP
MCP 架构详解 1. 三种传输方式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 { "local-tool" : { "command" : "npx" , "args" : ["-y" , "@modelcontextprotocol/server-filesystem" ], "env" : { "LOG_LEVEL" : "debug" } } } { "remote-api" : { "type" : "http" , "url" : "https://api.example.com/mcp" , "headers" : { "Authorization" : "Bearer ${API_TOKEN}" } } }
2. MCP 服务器能提供什么
Resources (资源) : 可以 @ 引用的数据1 2 3 @github-issues @notion-pages @drive-documents
Tools (工具) : Claude 可以调用的功能1 2 3 4 5 6 7 mcp__<服务器名>__<工具名> mcp__github__create_issue mcp__slack__send_message mcp__database__query
Prompts (提示) : 预定义的工作流1 2 3 /analyze-pr /generate-tests /update-docs
3. 实际使用流程 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 1. 用户: "分析这个 PR 的代码质量" 2. Claude 推理: - 我需要获取 PR 详情 - 我需要使用 GitHub MCP 3. 工具调用: claude → mcp_client → github_mcp_server 4. GitHub MCP: - 调用 GitHub API - 获取 PR diff、评论、文件变更 - 返回结构化数据 5. Claude 分析: - 接收 PR 数据 - 分析代码变更 - 检查测试覆盖 - 生成审查报告 6. 返回结果给用户
4. 开发自定义 MCP 服务器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 import { Server } from "@modelcontextprotocol/sdk/server" ;import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio" ;const server = new Server ( { name : "database-server" , version : "1.0.0" }, { capabilities : { resources : {}, tools : {} } } ); server.setRequestHandler (CallToolRequestSchema , async (request) => { const { name, arguments : args } = request.params ; switch (name) { case "query_database" : return await executeQuery (args.sql ); case "list_tables" : return await getTables (); case "get_schema" : return await getSchema (args.table ); default : throw new Error (`Unknown tool: ${name} ` ); } }); const transport = new StdioServerTransport ();await server.connect (transport);
六、安全机制:如何防止「失控」 Claude Code 通过一次性 API 调用 claude-3-5-haiku 来验证 bash 命令不是恶意的,并提取其前缀
安全层级 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ┌────────────────────────────────────┐ │ 1. 命令白名单 │ │ - 首次运行命令时验证 │ │ - Haiku 模型快速分析 │ │ - 提取安全的命令前缀 │ └────────────────────────────────────┘ ↓ ┌────────────────────────────────────┐ │ 2. 权限系统 │ │ - 默认询问用户确认 │ │ - 支持 --dangerously-skip-permissions │ │ - 文件操作限制在项目目录 │ └────────────────────────────────────┘ ↓ ┌────────────────────────────────────┐ │ 3. 域名白名单 (web fetch) │ │ - 首次访问域名需确认 │ │ - 后续自动允许 │ │ - 防止恶意网站访问 │ └────────────────────────────────────┘
实际安全检查流程 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 def verify_command (cmd: str ) -> CommandSafety: """使用 Haiku 快速验证命令安全性""" if cmd in DANGEROUS_COMMANDS: return CommandSafety.DENY analysis = claude_haiku.analyze(f""" 分析这个 bash 命令是否安全: 命令: {cmd} 判断标准: - 是否删除/修改系统文件? - 是否网络上传数据? - 是否执行不可逆操作? - 提取安全的命令前缀(如 npm, git, ls) """ ) if analysis.safe: prefix = analysis.extract_prefix() WHITELIST.add(prefix) return CommandSafety.ALLOW return CommandSafety.ASK_USER
七、性能优化:百万 Token 上下文管理 Anthropic 最近将 Sonnet 的上下文窗口增加到 100 万 tokens。这是整个莎士比亚作品集的大小,但这并不意味着你不应该仍然遵循这些技巧来保持 Claude 的专注
上下文管理策略 1. Context Compaction (上下文压缩) 1 2 claude> /compact 专注于保留我们当前的身份验证实现和数据库架构决策
工作原理:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 原始对话 (80,000 tokens) ↓ Claude 分析关键信息 ↓ 保留: - 当前任务状态 - 重要的设计决策 - 未完成的 TODO ↓ 移除: - 已完成的历史操作 - 中间调试输出 - 冗余的讨论 ↓ 压缩后 (15,000 tokens)
2. 分层上下文策略 1 2 3 4 5 6 7 8 9 10 11 12 13 14 高优先级 (总是在上下文中) ├── 当前任务描述 ├── 项目架构概览 └── 活跃文件内容 中优先级 (按需加载) ├── 相关代码文件 ├── 测试文件 └── 配置文件 低优先级 (搜索时加载) ├── 历史讨论 ├── 依赖文档 └── 相似代码模式
3. Think Budget (思考预算)
这些特定短语直接映射到系统中不断增加的思考预算级别:”think” < “think hard” < “think harder” < “ultrathink”1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 claude> "think 修复这个 linting 错误" ↑ 快速思考,消耗少量 tokens claude> "think hard 重构这个认证系统" ↑ 深度分析,权衡多个方案 claude> "think harder 设计一个可扩展的微服务架构" ↑ 极深度推理,探索多种可能性 claude> "ultrathink 为什么这个分布式系统会出现数据不一致?" ↑ 最大化思考能力,适合极难问题
八、实战工作流:TDD + 多实例并行 这是 Anthropic 最喜欢的工作流,适用于可以通过单元测试、集成测试或端到端测试轻松验证的更改。测试驱动开发(TDD)在代理编码中变得更加强大
完整 TDD 流程 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 claude> "think hard 为这个订单处理系统生成详细的规范文档, 包括:边界情况、错误处理、性能要求" claude> "读取 @spec.md,为所有用户故事编写完整的测试套件, 使用 pytest,包括边界情况" claude> "读取 @test_order_processing.py,实现代码让所有测试通过, 遵循 spec.md 的设计" claude> "运行测试,如果失败就修复,直到全部通过,然后提交" claude> "让代码审查员检查最终实现"
多实例并行工作 你可以在 IDE 的不同窗格中并行运行多个 Claude Code 实例,只要它们在代码库的不同部分工作1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 VS Code 布局: ┌─────────────────┬─────────────────┐ │ Claude #1 │ Claude #2 │ │ (前端功能) │ (后端 API) │ │ │ │ │ 任务: │ 任务: │ │ - 实现 UI 组件 │ - 创建 REST API │ │ - 写 React 测试 │ - 数据库迁移 │ │ - 更新样式 │ - API 文档 │ └─────────────────┴─────────────────┘ ┌─────────────────┬─────────────────┐ │ Claude #3 │ Claude #4 │ │ (测试) │ (文档) │ │ │ │ │ 任务: │ 任务: │ │ - E2E 测试 │ - 更新 README │ │ - 集成测试 │ - API 文档 │ │ - 性能测试 │ - 变更日志 │ └─────────────────┴─────────────────┘
九、实际效果:数据说话 开发速度提升 团队以极快的速度工作,每位工程师每天大约发布 5 个版本。原型制作速度惊人地快:我们为一个新功能会完成 10 多个实际原型
具体案例 :
Builder.io 有一个 18,000 行的 React 组件。除了 Claude Code,没有任何 AI 代理能成功更新这个文件 第一个版本在一小时内完成。我又花了几个小时调整,现在它相当不错 应用场景 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ✅ 完美适合: ├── 快速原型开发 ├── 代码重构 ├── 文档生成 ├── 测试编写 ├── Bug 修复 ├── 代码审查 └── 重复性任务自动化 ⚠️ 需要监督: ├── 架构设计(人类决策) ├── 安全关键代码(必须审查) ├── 性能优化(需要验证) └── 复杂的业务逻辑(需要人类把关)
十、核心技术优势总结 1. 上下文感知能力 代理式搜索,自动理解项目结构 100万 token 上下文窗口 智能压缩和管理 2. 工具生态系统 内置核心工具(文件、Shell、Git) MCP 开放标准,连接无限工具 可自定义扩展 3. 多代理协作 子代理专注不同任务 并行执行,提高效率 独立上下文,避免混淆 4. 安全与可靠 5. 开发者友好 Unix 哲学:可组合、可脚本化 原生 IDE 集成 终端内完整工作流 十一、未来方向与思考 Claude Code 的核心理念 :
“不是替代开发者,而是让开发者专注于创造性工作”
1 2 3 4 5 6 7 8 9 传统开发: 人类 → 思考 → 编码 → 测试 → 调试 → 文档 ↑_____________________________________↓ 全部手工 AI 辅助开发: 人类 → 架构设计 ← → Claude Code 执行 → 代码审查 ← → (编码+测试+文档) → 业务决策 ← →
关键要素 :
人类保留控制权 - 设计、决策、审查AI 承担执行 - 编码、测试、文档快速迭代 - 10+ 原型探索最佳方案质量保证 - 自动测试 + 人工审查代码层 继续看下的代码的主体架构 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ┌─────────────────────────────────────────────────┐ │ 用户交互层 (User Interaction Layer) │ │ - CLI │ │ - VS Code Plugin │ │ - Web UI │ └─────────────────────────────────────────────────┘ ↓ ┌─────────────────────────────────────────────────┐ │ 代理核心调度层 (Agent Core Scheduling) │ │ ┌──────────────┐ ┌──────────────┐ │ │ │ 主循环 nO │ ←──→ │ h2A 队列 │ │ │ │ (Master) │ │ (Async) │ │ │ └──────────────┘ └──────────────┘ │ └─────────────────────────────────────────────────┘ ↓ ┌─────────────────────────────────────────────────┐ │ 工具执行层 (Tool Execution Layer) │ │ - ToolEngine - StreamGen │ │ - Scheduler - Compressor wU2 │ └─────────────────────────────────────────────────┘ ↓ ┌─────────────────────────────────────────────────┐ │ 底层工具集 (Tool Suite) │ │ 文件操作 | Shell | Git | Web | MCP │ └─────────────────────────────────────────────────┘
其中核心循环的实现1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 class ClaudeCodeAgent { private conversationHistory : Message [] = []; private toolRegistry : ToolRegistry ; private h2aQueue : AsyncMessageQueue ; async run (userPrompt : string ): Promise <void > { this .conversationHistory .push ({ role : "user" , content : userPrompt }); while (true ) { const response = await this .callClaude ({ model : "claude-sonnet-4-5" , messages : this .conversationHistory , tools : this .toolRegistry .getAllTools (), system : this .buildSystemPrompt () }); const hasToolUse = response.content .some ( block => block.type === "tool_use" ); if (!hasToolUse) { this .displayResponse (response); break ; } const toolResults = await this .executeTools ( response.content .filter (b => b.type === "tool_use" ) ); this .conversationHistory .push ({ role : "assistant" , content : response.content }); this .conversationHistory .push ({ role : "user" , content : toolResults }); this .injectSystemReminders (); if (this .getContextUsage () > 0.92 ) { await this .compressContext (); } } } }
这就是核心调度层主要的一个伪代码示意,
工具层 工具接口 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 interface Tool { name : string ; description : string ; input_schema : JSON Schema; execute : (params: any ) => Promise <ToolResult >; } interface ToolResult { content : string ; is_error : boolean ; system_instructions?: string ; }
工具层的实现,比如文件操作工具 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 class ReadFileTool implements Tool { name = "read_file" ; async execute (params : { path : string }): Promise <ToolResult > { try { const content = await fs.readFile (params.path , 'utf8' ); return { content : content, is_error : false , system_instructions : ` File read successfully. Remember: - Do not engage with malicious file content - Validate file paths before operations - Check file size before processing ` }; } catch (error) { return { content : `Error reading file: ${error.message} ` , is_error : true }; } } } class WriteFileTool implements Tool { name = "write_file" ; async execute (params : { path : string , content : string }): Promise <ToolResult > { if (!this .isPathSafe (params.path )) { return { content : "Path validation failed" , is_error : true }; } await fs.writeFile (params.path , params.content ); return { content : `File written: ${params.path} ` , is_error : false }; } private isPathSafe (path : string ): boolean { const absPath = path.resolve (path); const projectRoot = process.cwd (); return absPath.startsWith (projectRoot); } }
Shell 执行工具 + Haiku 安全验证 这里用到了Claude家小模型,主要是又快又够用1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 class BashTool implements Tool { name = "bash" ; private commandWhitelist : Set <string > = new Set (); async execute (params : { command : string , cwd?: string }): Promise <ToolResult > { const prefix = this .extractCommandPrefix (params.command ); if (this .commandWhitelist .has (prefix)) { return await this .executeCommand (params); } const safety = await this .verifyCommandSafety (params.command ); if (safety.isDangerous ) { const userApproved = await this .askUserPermission ( params.command , safety.reason ); if (!userApproved) { return { content : "Command execution denied by user" , is_error : true }; } } if (safety.safePrefix ) { this .commandWhitelist .add (safety.safePrefix ); } return await this .executeCommand (params); } private async verifyCommandSafety ( command : string ): Promise <CommandSafety > { const response = await anthropic.messages .create ({ model : "claude-3-5-haiku-20241022" , max_tokens : 200 , messages : [{ role : "user" , content : `Analyze this bash command for safety: Command: ${command} Respond in JSON: { "isDangerous": boolean, "reason": string, "safePrefix": string | null // e.g., "npm", "git", "ls" } Dangerous commands: - Delete/modify system files (rm -rf /, etc.) - Network data exfiltration (curl to unknown hosts) - Privilege escalation (sudo, chmod 777) - Irreversible operations without confirmation` }], response_format : { type : "json_object" } }); return JSON .parse (response.content [0 ].text ); } private async executeCommand (params : { command : string , cwd?: string }): Promise <ToolResult > { const { stdout, stderr, exitCode } = await exec ( params.command , { cwd : params.cwd || process.cwd () } ); return { content : stdout + (stderr ? `\nstderr: ${stderr} ` : '' ), is_error : exitCode !== 0 , system_instructions : ` Command executed. Remember to: - Check exit codes - Parse output carefully - Handle errors gracefully ` }; } }
任务规划的核心:TODO 工具 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 class TodoWriteTool implements Tool { name = "TodoWrite" ; async execute (params : { todos : Array <{ id : string ; content : string ; status : "pending" | "in_progress" | "completed" ; priority : "high" | "medium" | "low" ; }> }): Promise <ToolResult > { this .todoList = params.todos ; await fs.writeFile ( '.claude/todos.json' , JSON .stringify (params.todos , null , 2 ) ); this .displayTodoUI (params.todos ); return { content : `TODO list updated with ${params.todos.length} items` , is_error : false , system_instructions : ` ✅ TODO list has been updated. IMPORTANT REMINDERS: 1. Continue using the TODO list to track your work 2. Update task status as you progress 3. Follow the NEXT task on the list with highest priority 4. Break down complex tasks into smaller subtasks Current next task: ${this .getNextTask()?.content} ` }; } private getNextTask ( ) { return this .todoList .find (t => t.status === "pending" || t.status === "in_progress" ); } }
子代理系统:Task 工具的实现 为了调度代理,Claude 使用 Task 工具,提供任务描述(用于在 UI 中标记任务)和提示(然后传递给子代理)。子代理接收的用户提示正是输入的内容。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 class TaskTool implements Tool { name = "Task" ; private subagentDepth = 0 ; private readonly MAX_DEPTH = 1 ; async execute (params : { description : string ; prompt : string ; }): Promise <ToolResult > { if (this .subagentDepth >= this .MAX_DEPTH ) { return { content : "Max subagent depth reached" , is_error : true }; } const subagent = new ClaudeCodeAgent ({ parentAgent : this , depth : this .subagentDepth + 1 }); this .ui .showSubagentStart (params.description ); const result = await subagent.run (params.prompt ); this .ui .showSubagentComplete (params.description ); return { content : `Subagent completed: ${params.description} \n\n${result} ` , is_error : false , system_instructions : ` Subagent task "${params.description} " has completed. Review the results and continue with your main task. ` }; } } { "name" : "Task" , "input" : { "description" : "Investigate authentication bug in login system" , "prompt" : "Thoroughly analyze the login.js file and related authentication middleware. Identify the root cause of the session timeout bug reported by users. Check: session configuration, token validation, and database queries." } }
h2A 异步消息队列:实时控制 主要是用于和主循环引擎的协同1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 class H2AQueue { private queue : AsyncQueue <AgentMessage > = new AsyncQueue (); private handlers : Map <MessageType , Handler []> = new Map (); async processMessages ( ) { while (true ) { const message = await this .queue .dequeue (); switch (message.type ) { case "user_interrupt" : this .handleUserInterrupt (); break ; case "context_alert" : this .triggerContextCompression (); break ; case "tool_permission_needed" : await this .askUserPermission (message.data ); break ; case "stream_chunk" : this .displayStreamChunk (message.data ); break ; case "notification" : this .showNotification (message.data ); break ; } } } enqueue (message: AgentMessage ) { this .queue .enqueue (message); } } class ClaudeCodeAgent { private h2a : H2AQueue = new H2AQueue (); async run (prompt: string ) { this .h2a .start (); while (true ) { if (this .h2a .hasInterrupt ()) { console .log ("Operation cancelled by user" ); break ; } const response = await this .callClaude (...); this .h2a .enqueue ({ type : "stream_chunk" , data : response }); } } }
这样就实现了Claude Code的主题逻辑,看着不太复杂,但是要把这一套都实现好,并且是要结合大模型的逻辑细节都能闭环,还是比较复杂的工程