Copilot Chat Agent / Tool / Prompt 源码细节
基于 2026-04-25 抓取的 microsoft/vscode-copilot-chat main 分支快照,逐项展开 9 个 Chat Participant、37 个 contributed Language Model Tools、ToolRegistry、ToolsService、AgentPrompt 和 ToolCallingLoop。
0. 真实边界
Copilot Chat 的 VS Code 扩展客户端已经开源,本文的类名、文件路径、工具数量和工具描述来自本次抓取的 main 分支快照。GitHub Copilot 的服务端模型路由、鉴权、配额、远端索引和推理基础设施仍不是该仓库公开内容。
所以本页能深入到 ToolRegistry、ToolsService、ToolCallingLoop 和 prompt-tsx 文件,但不会把服务端模型行为写成源码事实。
1. Chat Participant 与 Intent
Chat surface | +-- github.copilot.default +-- github.copilot.editingSession +-- github.copilot.editsAgent +-- github.copilot.notebook +-- github.copilot.vscode +-- github.copilot.terminal | v chatParticipants.ts | vscode.chat.createChatParticipant(...) v ChatParticipantRequestHandler | sanitize ignored references | rebuild Conversation | infer location and intent v DefaultIntentRequestHandler | confirmation + hooks + intent invocation v DefaultToolCallingLoop | PromptRenderer + AgentPrompt + available tools | model turns + tool rounds + transcript v final markdown / edits / progress / confirmations
| Participant id | 显示名 | 默认 Intent | 职责 |
|---|---|---|---|
github.copilot.default | GitHub Copilot | Unknown / AskAgent 条件切换 | 默认聊天入口;根据配置和模型 tool calling 能力可转 AskAgent。 |
github.copilot.editingSession | GitHub Copilot | Edit | 编辑会话入口,强调对当前编辑上下文的修改。 |
github.copilot.editingSessionEditor | GitHub Copilot | InlineChat | 编辑器内联聊天入口,和当前 editor selection 强绑定。 |
github.copilot.editsAgent | GitHub Copilot | Agent | Agent Mode 入口,允许多轮工具调用、编辑、执行和验证。 |
github.copilot.notebook | GitHub Copilot | Editor | Notebook 默认入口。 |
github.copilot.notebookEditorAgent | GitHub Copilot | notebookEditor | Notebook 编辑器 agent。 |
github.copilot.vscode | VS Code | VSCode | 专门回答 VS Code 和扩展开发相关问题。 |
github.copilot.terminal | Terminal | Terminal | 终端聊天入口。 |
github.copilot.terminalPanel | Terminal | Terminal | 终端面板入口,id 单独贡献。 |
2. 工具注册与调用架构
package.json contributes.languageModelTools | v ToolName / ContributedToolName mapping | src/extension/tools/common/toolNames.ts v ToolRegistry.registerTool(...) | concrete tool classes under tools/node and tools/vscode-node v ToolsContribution | vscode.lm.registerTool(getContributedToolName(name), tool) v VS Code Language Model Tool API | v ToolCallingLoop offers filtered tools to the selected model | v ToolsService.invokeTool(...) | telemetry span + debug args/result + vscode.lm.invokeTool v LanguageModelToolResult2 returns to ChatToolCalls prompt section
package.json 贡献工具给 VS Code,包含 contributed name、描述、schema、tags 和 when 条件。toolNames.ts 把 copilot_* contributed name 映射为内部 ToolName,prompt 和 telemetry 使用内部名。ToolRegistry.registerTool 注册,再由 ToolsContribution 调用 vscode.lm.registerTool。ToolsService.invokeTool 统一记录 OTel span、参数、结果、成功/失败指标,再调用 vscode.lm.invokeTool。3. 37 个工具逐项明细
| Contributed tool | 内部 ToolName | 实现文件 | 职能 | 实现细节 / 编排影响 |
|---|---|---|---|---|
copilot_searchCodebase | semantic_search | tools/node/codebaseTool.tsx | 语义搜索当前 workspace 的代码和文档注释。 | 使用 code search 服务/索引返回相关片段;小 workspace 可返回更完整内容。Prompt 规则要求不确定关键词时优先使用。 |
search_subagent | search_subagent | tools/node/searchSubagentTool.ts | 启动专门探索代码库的轻量子 agent。 | 源码注册 SearchSubagentTool;DefaultAgentPrompt 明确要求上下文搜索优先用它,而不是直接多次 grep/file search。 |
execution_subagent | execution_subagent | tools/node/executionSubagentTool.ts | 启动执行型子 agent,运行命令并摘要关键输出。 | ExecutionSubagentPrompt 只面向终端执行任务;源码中有独立 invocation id,并限制成执行导向循环。 |
copilot_searchWorkspaceSymbols | search_workspace_symbols | tools/node/searchWorkspaceSymbolsTool.tsx | 通过语言服务搜索 workspace symbol。 | 适合用户明确找类、函数、接口等符号;依赖 VS Code/workspace language service。 |
copilot_getVSCodeAPI | get_vscode_api | tools/node/vscodeAPITool.ts | 检索 VS Code API 官方文档和扩展开发参考。 | 仅用于 VS Code extension development 场景;package 描述包含明确的 use / not-use 条件。 |
copilot_findFiles | file_search | tools/node/findFilesTool.tsx | 按 glob 搜索文件路径。 | 返回路径而非内容;多根 workspace 可用绝对路径限定 root。 |
copilot_findTextInFiles | grep_search | tools/node/findTextInFilesTool.tsx | 快速文本/正则搜索文件内容。 | 支持 includePattern 与 includeIgnoredFiles;prompt 提醒可用它获取单文件概览。 |
copilot_readFile | read_file | tools/node/readFileTool.tsx | 读取指定文件的指定行范围。 | 要求给出 1-indexed line range;二进制文件使用 byte offset;prompt 提醒优先读较大、有意义的块。 |
copilot_viewImage | view_image | tools/node/viewImageTool.tsx | 读取 png/jpg/jpeg/gif/webp 等图像内容。 | 返回 multimodal data 而非文本行;用于模型查看图像文件。 |
copilot_listDirectory | list_dir | tools/node/listDirTool.tsx | 列出目录直接子项。 | 文件夹以 `/` 标识;用于建立结构视图,不读取文件内容。 |
copilot_readProjectStructure | read_project_structure | tools/node/readProjectStructureTool.ts | 返回 workspace 文件树结构。 | 作为全局结构上下文,避免 agent 先盲目搜索。 |
copilot_getErrors | get_errors | tools/node/getErrorsTool.tsx | 读取编译、lint 或 diagnostics 问题。 | 从 VS Code diagnostics 获取用户实际看到的问题;编辑后也用于验证。 |
copilot_getChangedFiles | get_changed_files | tools/node/scmChangesTool.ts | 读取当前 git/SCM 变更 diff。 | 依赖 VS Code SCM/Git 服务;用于总结未提交修改或避免覆盖用户改动。 |
copilot_testFailure | test_failure | tools/node/testFailureTool.tsx | 把测试失败信息加入 prompt。 | 测试相关 intent 可用它补充失败上下文。 |
copilot_findTestFiles | test_search | tools/node/findTestsFilesTool.tsx | 在源码文件和测试文件之间互相定位。 | 用来找 code under test 或对应 test file。 |
copilot_applyPatch | apply_patch | tools/node/applyPatchTool.tsx + applyPatch/parser.ts | 按 V4A patch 格式修改文本文件。 | 自带 parser 和 corpus 测试;执行后进入 edit result/diagnostics 流程,适合多处明确补丁。 |
copilot_insertEdit | insert_edit_into_file | tools/node/insertEditTool.tsx | 对已有文件做高层次插入/编辑。 | Prompt 要求用 `// ...existing code...` 表示省略区域;比 replaceString 更智能但更依赖模型表达。 |
copilot_replaceString | replace_string_in_file | tools/node/replaceStringTool.tsx + abstractReplaceStringTool.tsx | 用精确 oldString/newString 替换单处文本。 | 要求 oldString 唯一且包含上下文;源码共享 AbstractReplaceStringTool 的 edit/diagnostics/healing 流程。 |
copilot_multiReplaceString | multi_replace_string_in_file | tools/node/multiReplaceStringTool.tsx + abstractReplaceStringTool.tsx | 一次调用执行多个 replaceString 操作。 | DefaultReminderInstructions 要求多处独立编辑时优先使用它以降低轮次和成本。 |
copilot_createFile | create_file | tools/node/createFileTool.tsx | 创建新文件并写入内容。 | 目录不存在时可创建;package 明确不要用它编辑已存在文件。 |
copilot_createDirectory | create_directory | tools/node/createDirectoryTool.tsx | 递归创建目录结构。 | 类似 mkdir -p;createFile 本身也会创建所需目录。 |
copilot_editFiles | edit_files | package.json placeholder | 占位工具。 | package 描述为 placeholder tool, do not use;当前 allTools 注册清单未见具体 ToolRegistry 实现。 |
copilot_createNewWorkspace | create_new_workspace | tools/node/newWorkspace/newWorkspaceTool.tsx | 为新项目生成完整 workspace 搭建步骤。 | 用于完整 project initialization,不用于单文件创建或修改既有代码。 |
copilot_getProjectSetupInfo | get_project_setup_info | tools/node/newWorkspace/projectSetupInfoTool.tsx | 获取项目类型和语言对应的 setup 信息。 | package 明确要求先调用 create_new_workspace,再使用本工具。 |
copilot_installExtension | install_extension | tools/node/installExtensionTool.tsx | 安装 VS Code 扩展。 | 仅作为新 workspace 创建流程的一部分使用,不是通用扩展管理工具。 |
copilot_runVscodeCommand | run_vscode_command | tools/node/vscodeCmdTool.tsx | 运行 VS Code command。 | 只应作为新 workspace 创建流程的一部分使用;不是任意命令执行的主路径。 |
copilot_createNewJupyterNotebook | create_new_jupyter_notebook | tools/node/newNotebookTool.tsx | 生成新的 Jupyter Notebook。 | package 提醒除非用户明确要求或已有 notebook,否则优先创建普通 Python 等文本文件。 |
copilot_editNotebook | edit_notebook_file | tools/node/editNotebookTool.tsx | 编辑现有 notebook cell。 | 保留 cell 结构;更新 cell 内容时必须保留空白和缩进,不能使用 existing-code marker。 |
copilot_runNotebookCell | run_notebook_cell | tools/node/runNotebookCellTool.tsx | 运行 notebook 代码单元并返回输出。 | 不运行 markdown cell;常用于数据分析任务中让 kernel 状态保持最新。 |
copilot_getNotebookSummary | copilot_getNotebookSummary | tools/node/notebookSummaryTool.tsx | 获取 notebook cell 列表、类型、行范围、语言、执行信息和输出 mime。 | 用于先定位 cell id 和 line range,再用 readFile 或 runNotebookCell 操作。 |
copilot_readNotebookCellOutput | read_notebook_cell_output | tools/node/getNotebookCellOutputTool.tsx | 读取某个 notebook cell 的最近输出。 | 输出 token 上限高于 runNotebookCell 的即时输出,适合查看持久化结果。 |
copilot_fetchWebPage | fetch_webpage | tools/vscode-node/fetchWebPageTool.tsx | 抓取网页主要内容。 | 用于用户明确要分析某个 URL;注册在 vscode-node 层,依赖 VS Code/网络环境。 |
copilot_githubRepo | github_repo | tools/node/githubRepoTool.tsx | 搜索指定 GitHub 仓库片段。 | package 要求仅当用户非常明确询问某个 GitHub repo,且该 repo 未在 workspace 中打开时使用。 |
copilot_getSearchResults | get_search_view_results | tools/node/getSearchViewResultsTool.tsx | 读取 VS Code Search View 当前结果。 | 复用用户已经在搜索面板得到的上下文。 |
copilot_switchAgent | switch_agent | tools/vscode-node/switchAgentTool.ts | 切换到 Plan agent。 | package 详细列出适合 switch 的场景:新增功能、多方案、架构决策、多文件变更、需求不清等。 |
copilot_memory | memory | tools/node/memoryTool.tsx | 管理持久记忆。 | 支持 user/session/repo 三个 scope;命令包含 view、create、str_replace、insert、delete、rename。 |
copilot_resolveMemoryFileUri | resolve_memory_file_uri | tools/node/resolveMemoryFileUriTool.tsx | 把 /memories/... 路径解析成完整 URI。 | 用于需要真实 URI 的后续 API,例如 artifact 引用。 |
4. Prompt 细节:公开源码中的提示词装配
AgentPrompt.tsx
|
+-- base SystemMessage
| +-- expert programming assistant identity
| +-- Copilot identity rules
| +-- SafetyRules
| +-- MemoryInstructionsPrompt
|
+-- model-specific customizations
| +-- default / OpenAI / Anthropic / Gemini / family H / minimax
|
+-- dynamic context
+-- custom instructions
+-- mode instructions
+-- terminal state
+-- workspace structure
+-- notebook summary
+-- memory context
+-- tool call rounds and results
+-- summarized conversation history | 文件 | 作用 |
|---|---|
agentPrompt.tsx | AgentPrompt 主装配器,组合 SystemMessage、custom instructions、memory、history、tool calls 和 summarization。 |
defaultAgentInstructions.tsx | 默认 agent 提示词和工具使用规则,包含搜索子 agent、执行子 agent、并行工具、编辑工具优先级等动态条件。 |
executionSubagentPrompt.tsx | 执行型子 agent 的系统提示词,只要求运行命令并返回 compact final_answer。 |
searchSubagentPrompt.tsx | 搜索型子 agent 的代码库探索提示词。 |
anthropicPrompts.tsx / openai/*.tsx / geminiPrompts.tsx | 按模型家族覆盖身份、工具说明、编辑提示和输出偏好。 |
summarizedConversationHistory.tsx | 长上下文下的历史摘要、cache breakpoint 和 inline summarization 入口。 |
toolCalling.tsx | 把工具调用和工具结果渲染回 prompt,同时处理截断、调试日志和 telemetry。 |
源码中可以直接看到默认 agent prompt 的核心规则:要求收集上下文再行动、读文件时优先大块读取、可并行调用多个工具、搜索优先使用 search_subagent,多数执行任务优先使用 execution_subagent,编辑时优先选择更精确和更省轮次的编辑工具。
5. ToolCallingLoop 的状态机
Start request
|
v
run SessionStart / UserPromptSubmit hooks
|
v
getAvailableTools
| request tools + tool picker + model overrides + MCP + config
v
buildPrompt
| AgentPrompt / history / memory / tools / edited files
v
send model turn
|
+-- text only ----------------------> Stop hooks -> final result
|
+-- tool calls
|
v
execute each tool through ToolsService
|
v
append toolCallRounds and toolCallResults
|
+-- hit limit / cancelled ----> cancellation or confirmation
|
+-- continue -----------------> next model turn
|
+-- autopilot complete -------> task_complete ToolCallingLoop 还维护 transcript、token usage、debug logger、OpenTelemetry span、subagent trace context 和 stop hook reason。它不是一个简单 while 循环,而是 VS Code UI、模型 endpoint、工具系统和可观测性的协调点。
6. 与主页面的关系
主页面解释 Copilot Chat 的总体 VS Code 插件架构;本页按源码快照补足每个工具和 prompt/agent 编排文件的细节。
资料来源
- microsoft/vscode-copilot-chat:本次源码快照、package.json、工具实现和 prompt 文件。
- VS Code Open Source AI Editor First Milestone:Copilot Chat 公开仓库背景。
- VS Code Chat API:Chat Participant 接入方式。
- VS Code Language Model Tool API:工具注册与调用接口。
- VS Code Language Model API:模型访问边界。