Claude Code 源码分析Claude Code 源码分析
首页
源码统计
系统架构
UML 图表
工具系统
CodeGraph
首页
源码统计
系统架构
UML 图表
工具系统
CodeGraph
  • 概览

    • Claude Code 源码分析
    • 源码统计
    • CodeGraph 图谱
  • 架构

    • 系统架构
    • UML 图表索引
    • 查询引擎
    • 核心流程
    • 消息系统
    • 状态管理
  • 功能模块

    • 工具系统
    • 斜杠命令
    • 服务层
    • MCP 协议
    • Skills 技能
    • 子代理系统
  • 分层深度

    • 入口层
    • UI / Ink 层
    • utils 基础设施
    • 桥接 / 远程
    • 上下文压缩
  • 原理与安全

    • 底层原理
    • 技术难点
    • 权限与安全
    • 内部机制
    • 遥测与分析
  • 深度专题

    • Hooks 系统
    • 插件系统
    • 记忆系统
    • API 通信层
    • Ink 终端 UI
    • 认证系统
    • 构建与发布
    • 术语表
  • 调用分析

    • 调用链分析
    • 核心文件索引
  • 模块详解

    • utils

      • 模块: utils
      • messages · 消息工厂与规范化
      • session-storage · JSONL 会话持久化
      • permissions · 工具权限决策
      • shell-hooks · 用户 Shell Hook 系统
    • components

      • 模块: components
      • REPL · 主屏编排
      • messages · 消息行渲染
      • PermissionRequest · 权限弹窗
      • PromptInput · 底部输入
    • services

      • 模块: services
      • api-claude · Anthropic API 流式与重试
      • mcp-client · MCP 连接与工具调用
      • compact · 上下文压缩与自动触发
      • analytics · GrowthBook、Datadog 与 1P 事件
    • tools

      • 模块: tools
      • tool-interface · Tool 契约与注册表
      • bash-tool · Shell 执行与权限
      • streaming-executor · 流式工具并发调度
      • agent-tool · 子 Agent 委派
    • commands

      • 模块: commands
      • command-registry · commands.ts 注册与分派
      • model-command · /model 模型选择
      • mcp-commands · /mcp 服务器管理
      • compact-memory-commands · /compact 与 /memory
    • ink

      • 模块: ink
      • Ink 渲染管线 · Screen 与终端输出
      • 终端事件 · resize、paste、stdin
      • Ink Hooks · 输入、搜索、终端状态
      • Ink 组件 · Box、Text、ScrollBox 原语
    • hooks

      • 模块: hooks
      • useCanUseTool · 权限 UI 接缝
      • 输入与快捷键 Hook
      • 合并态 Hook(MCP + 本地)
      • notifs 通知 Hook
    • bridge

      • 模块: bridge
      • repl-bridge · REPL 桥初始化与传输
      • bridge-messaging · 桥消息路由与入站处理
      • remote-bridge-core · env-less 核心与守护主循环
      • bridge-permissions-ui · 权限、API 与 TUI
    • cli

      • 模块: cli
      • Structured IO · NDJSON SDK 协议
      • CLI Transports · Session Ingress 传输层
      • CLI Handlers · 子命令懒加载实现
      • Update & Upload · 自更新与串行上传原语
    • screens

      • 模块: screens
      • REPL 屏 · Screen 类型与顶层路由
      • ResumeConversation · 会话恢复选择器
      • Doctor · 安装诊断全屏
    • entrypoints

      • 模块: entrypoints
      • cli-entrypoint · Bootstrap 与快路径
      • sdk-types · core / control / runtime 类型体系
      • mcp-entrypoint · MCP stdio 服务器
      • sandbox-types · 沙箱配置单一真相源
    • skills

      • 模块: skills
      • skills-loading · 磁盘加载与 bundled 注册表
      • bundled-skills · 内置 skill 与 initBundledSkills
      • mcp-skills · MCP prompt 转 skill
      • skill-tool-integration · SkillTool 与命令注册
    • types

      • 模块: types
      • message-types · Message 联合与 content blocks
      • tool-permission-types · Tool、Permission、Command 类型
      • api-sdk-types · API 与 Hooks 协议类型
      • misc-types · ids、plugin、generated 与其余类型
    • tasks

      • 模块: tasks
      • local-agent-task · 本地 Agent 与主会话后台化
      • remote-agent-task · 远程 CCR 与 In-Process Teammate
      • shell-workflow-tasks · Bash 后台、Workflow 与 stopTask
      • dream-monitor-tasks · Dream、Monitor MCP 与 pill 文案
    • keybindings

      • 模块: keybindings
      • keybinding-registry · 注册、Provider 与 useKeybinding
      • default-bindings · 默认键位表与平台差异
      • command-bindings · command:* 动态斜杠命令绑定
      • vim-bindings · Vim 模式与 keybindings 边界
    • memdir

      • 模块: memdir
      • memdir-core · 路径、加载与 MEMORY.md
      • memory-extraction · extractMemories 与 SessionMemory
      • memdir-commands · /memory、/remember 与命令集成
    • state

      • 模块: state
      • app-state-core · store、AppState 类型与 Provider
      • app-state-selectors · selectors 与 onChangeAppState
      • teammate-state · 队友视图与 swarm 状态
      • state-boundaries · bootstrap、sessionStorage、FileStateCache
    • query

      • 模块: query
      • query config 与 deps · 配置快照与依赖注入
      • query tokenBudget · +500k 自动续跑
      • query transitions · Continue / Terminal 状态机
      • query stopHooks · Stop 事件与 turn 结束编排
  • 模块详解(扩展)

    • messages · 消息工厂与规范化
    • session-storage · JSONL 会话持久化
    • permissions · 工具权限决策
    • shell-hooks · 用户 Shell Hook 系统
    • REPL · 主屏编排
    • messages · 消息行渲染
    • PermissionRequest · 权限弹窗
    • PromptInput · 底部输入
    • api-claude · Anthropic API 流式与重试
    • mcp-client · MCP 连接与工具调用
    • compact · 上下文压缩与自动触发
    • analytics · GrowthBook、Datadog 与 1P 事件
    • tool-interface · Tool 契约与注册表
    • bash-tool · Shell 执行与权限
    • streaming-executor · 流式工具并发调度
    • agent-tool · 子 Agent 委派
    • command-registry · commands.ts 注册与分派
    • model-command · /model 模型选择
    • mcp-commands · /mcp 服务器管理
    • compact-memory-commands · /compact 与 /memory
    • Ink 渲染管线 · Screen 与终端输出
    • 终端事件 · resize、paste、stdin
    • Ink Hooks · 输入、搜索、终端状态
    • Ink 组件 · Box、Text、ScrollBox 原语
    • useCanUseTool · 权限 UI 接缝
    • 输入与快捷键 Hook
    • 合并态 Hook(MCP + 本地)
    • notifs 通知 Hook
    • repl-bridge · REPL 桥初始化与传输
    • bridge-messaging · 桥消息路由与入站处理
    • remote-bridge-core · env-less 核心与守护主循环
    • bridge-permissions-ui · 权限、API 与 TUI
    • Structured IO · NDJSON SDK 协议
    • CLI Transports · Session Ingress 传输层
    • CLI Handlers · 子命令懒加载实现
    • Update & Upload · 自更新与串行上传原语
    • REPL 屏 · Screen 类型与顶层路由
    • ResumeConversation · 会话恢复选择器
    • Doctor · 安装诊断全屏
    • cli-entrypoint · Bootstrap 与快路径
    • sdk-types · core / control / runtime 类型体系
    • mcp-entrypoint · MCP stdio 服务器
    • sandbox-types · 沙箱配置单一真相源
    • skills-loading · 磁盘加载与 bundled 注册表
    • bundled-skills · 内置 skill 与 initBundledSkills
    • mcp-skills · MCP prompt 转 skill
    • skill-tool-integration · SkillTool 与命令注册
    • message-types · Message 联合与 content blocks
    • tool-permission-types · Tool、Permission、Command 类型
    • api-sdk-types · API 与 Hooks 协议类型
    • misc-types · ids、plugin、generated 与其余类型
    • local-agent-task · 本地 Agent 与主会话后台化
    • remote-agent-task · 远程 CCR 与 In-Process Teammate
    • shell-workflow-tasks · Bash 后台、Workflow 与 stopTask
    • dream-monitor-tasks · Dream、Monitor MCP 与 pill 文案
    • keybinding-registry · 注册、Provider 与 useKeybinding
    • default-bindings · 默认键位表与平台差异
    • command-bindings · command:* 动态斜杠命令绑定
    • vim-bindings · Vim 模式与 keybindings 边界
    • memdir-core · 路径、加载与 MEMORY.md
    • memory-extraction · extractMemories 与 SessionMemory
    • memdir-commands · /memory、/remember 与命令集成
    • app-state-core · store、AppState 类型与 Provider
    • app-state-selectors · selectors 与 onChangeAppState
    • teammate-state · 队友视图与 swarm 状态
    • state-boundaries · bootstrap、sessionStorage、FileStateCache
    • query config 与 deps · 配置快照与依赖注入
    • query tokenBudget · +500k 自动续跑
    • query transitions · Continue / Terminal 状态机
    • query stopHooks · Stop 事件与 turn 结束编排
  • 工具详解

    • tool-interface · Tool 契约与注册表
    • tool-permission-types · Tool、Permission、Command 类型
    • 工具: Bash
    • 工具: PowerShell
    • 工具: Agent
    • 工具: LSP
    • 工具: FileEdit
    • 工具: FileRead
    • 工具: Skill
    • 工具: WebFetch
    • 工具: MCP
    • 工具: SendMessage
    • 工具: FileWrite
    • 工具: Config
    • 工具: Grep
    • 工具: Brief
    • 工具: ExitPlanMode
    • 工具: ToolSearch
    • 工具: NotebookEdit
    • 工具: TaskOutput
    • 工具: WebSearch
    • 工具: ScheduleCron

本章总览

src/commands/ 是 Claude Code 的「斜杠命令实现层」:每个 /clear、/model、/mcp 等用户输入对应一个 Command 对象,由根目录 commands.ts 聚合注册,经 processSlashCommand 分派到 local / local-jsx / prompt 三种执行路径(213 个文件,约 26692 行)。务必区分:commands.ts(注册表与 getCommands)≠ commands/ 目录(各命令实现)。

总览图

渲染图表中…

学完本章你应该能

  • 建立 command-registry → model / mcp / compact-memory 的阅读顺序
  • 理解 Command 三种 type 与 lazy load 模式
  • 能在源码树中从本页跳转到子章节与高亮文件
  • 区分内置命令、plugin/skill 命令与 MCP prompt 命令的来源

建议学习步骤

  1. 浏览下方子章节导航表,选择入口主题
  2. 点击 SourceTree 中的 commands.ts 或 commands/ 子目录跳转到对应讲解
  3. 建议顺序:command-registry → model-command → mcp-commands → compact-memory-commands

模块在架构中的位置

commands 是 src/ 下的一级目录,共 213 个文件、26,692 行。一句话概括:87 个 /command:/model、/compact、/mcp、/memory、/doctor 等,用户可直接操控 Agent 行为。

概览

指标数值
行数26,692
文件213

子章节导航

子章节主题核心路径
command-registrycommands.ts 注册、getCommands、分派commands.ts、types/command.ts
model-command/model 模型选择与校验commands/model/
mcp-commands/mcp 服务器管理commands/mcp/
compact-memory-commands/compact /memory 上下文与记忆commands/compact/、commands/memory/

Command 三种类型

Claude Code 斜杠命令在 types/command.ts 中分为三类:

type执行形态典型命令load 返回
local同步函数,直接改 messages 或返回 compact 结果/compact、/clear{ call(args, context) }
local-jsxInk React 组件,弹窗或 picker/model、/mcp、/memory{ call(onDone, context, args) }
prompt展开为 prompt 块发给模型(skills)/commit(skill)、plugin 命令getPromptForCommand

lazy load:index.ts 只导出元数据(name、description、type),load: () => import('./xxx.js') 推迟重模块(如 insights 113KB)直到用户真正输入命令。REPL typeahead 调用 getCommands(cwd) 时只读 description,不触发 load。

immediate 命令(如 /mcp、/model 在部分配置下)绕过输入队列,在 stop point 之外立即执行——见 shouldInferenceConfigCommandBeImmediate。

命令来源与优先级

loadAllCommands(cwd) 合并顺序(后者覆盖同名前者需 dedupe):

bundledSkills → builtinPluginSkills → skillDirCommands
  → workflowCommands → pluginCommands → pluginSkills
  → COMMANDS() 内置数组

getCommands 在此基础上过滤 meetsAvailabilityRequirement(claude-ai / console)与 isCommandEnabled()(GrowthBook、env)。动态 skill(文件操作中发现)插入在内置命令之前。

INTERNAL_ONLY_COMMANDS 仅 USER_TYPE === 'ant' 且非 demo 时加入 COMMANDS,外部构建通过 dead code elimination 剥离。REMOTE_SAFE_COMMANDS 与 BRIDGE_SAFE_COMMANDS 限制 remote / mobile bridge 可执行的命令子集。

目录树展示 src/commands/ 下全部子目录(clear、compact、config、doctor 等)。根目录 commands.ts 不在树中但 command-registry 子章节通过 highlightFiles 与高亮锚点跳转。

与其他模块的边界

协作方关系
processSlashCommand.tsx解析 /name args,findCommand,按 type 分派
skills/loadSkillsDir.js从 ~/.claude/skills 加载 prompt 命令
utils/plugins/loadPluginCommands.js插件 manifest 中的 commands
services/compact/*/compact 命令调用 compactConversation
services/mcp/*/mcp 与 MCPSettings 组件、ConnectionManager
components/ModelPicker/model 菜单 UI
state/AppStatemainLoopModel、fastMode、mcp.clients

调试「命令找不到」:先 getCommands(cwd) 列表是否含该名,再查 alias、availability、isEnabled;调试「命令无反应」:查 processSlashCommand 的 type 分支与 load() 是否抛错。

本章小结与延伸

commands 模块 = 用户斜杠输入的产品化入口。先懂 commands.ts 如何组装命令池,再按主题读 model / mcp / compact / memory 四条高频路径。 继续学习:

  • command-registry
Next
command-registry · commands.ts 注册与分派