跳到主要内容

上下文注入

源码: 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 → 发送给模型
↑ │
└──────── 工具结果回灌 ← 工具执行 ←─────────────┘

上一节:动态 Prompt | 下一节:03 核心引擎