上下文注入
源码:
context.ts|context/|constants/prompts.ts
1. 上下文收集
context.ts 负责收集所有需要注入到 System Prompt 中的运行时信息:
// source/src/context.ts
export async function collectContext(tools: Tools, commands: Command[]): Promise<string[]> {
return [
getCwdContext(), // 工作目录
getGitContext(), // Git 状态
getMemoryContext(), // 记忆摘要
getMCPContext(), // MCP 连接状态
getSkillsContext(), // 可用技能
getSettingsContext(), // 用户配置
]
}
2. 上下文类型
| 类型 | 来源 | 更新频率 |
|---|---|---|
| 工作目录 | process.cwd() | 每次 |
| Git 状态 | git branch | 每次 |
| 工作树 | git worktree list | 每次 |
| 记忆摘要 | ~/.claude/projects/*/memory/ | /clear 后 |
| MCP 连接 | services/mcp/ | 连接变化时 |
| 技能列表 | skills/ 目录 | 启动时 |
3. 上下文注入流程
用户输入 → 收集上下文 → 构建 System Prompt → 发送给模型
↑ │
└──────── 工具结果回灌 ← 工具执行 ←─────────────┘