AI 与大模型 · Copilot Chat 子页

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。

Snapshot: package 0.44.0 9 chat participants 37 contributed LM tools Client extension open source

0. 真实边界

Copilot Chat 的 VS Code 扩展客户端已经开源,本文的类名、文件路径、工具数量和工具描述来自本次抓取的 main 分支快照。GitHub Copilot 的服务端模型路由、鉴权、配额、远端索引和推理基础设施仍不是该仓库公开内容。

所以本页能深入到 ToolRegistryToolsServiceToolCallingLoop 和 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.defaultGitHub CopilotUnknown / AskAgent 条件切换默认聊天入口;根据配置和模型 tool calling 能力可转 AskAgent。
github.copilot.editingSessionGitHub CopilotEdit编辑会话入口,强调对当前编辑上下文的修改。
github.copilot.editingSessionEditorGitHub CopilotInlineChat编辑器内联聊天入口,和当前 editor selection 强绑定。
github.copilot.editsAgentGitHub CopilotAgentAgent Mode 入口,允许多轮工具调用、编辑、执行和验证。
github.copilot.notebookGitHub CopilotEditorNotebook 默认入口。
github.copilot.notebookEditorAgentGitHub CopilotnotebookEditorNotebook 编辑器 agent。
github.copilot.vscodeVS CodeVSCode专门回答 VS Code 和扩展开发相关问题。
github.copilot.terminalTerminalTerminal终端聊天入口。
github.copilot.terminalPanelTerminalTerminal终端面板入口,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.tscopilot_* contributed name 映射为内部 ToolName,prompt 和 telemetry 使用内部名。
实现层:每个工具类通过 ToolRegistry.registerTool 注册,再由 ToolsContribution 调用 vscode.lm.registerTool
执行层:ToolsService.invokeTool 统一记录 OTel span、参数、结果、成功/失败指标,再调用 vscode.lm.invokeTool

3. 37 个工具逐项明细

Contributed tool内部 ToolName实现文件职能实现细节 / 编排影响
copilot_searchCodebasesemantic_searchtools/node/codebaseTool.tsx语义搜索当前 workspace 的代码和文档注释。使用 code search 服务/索引返回相关片段;小 workspace 可返回更完整内容。Prompt 规则要求不确定关键词时优先使用。
search_subagentsearch_subagenttools/node/searchSubagentTool.ts启动专门探索代码库的轻量子 agent。源码注册 SearchSubagentTool;DefaultAgentPrompt 明确要求上下文搜索优先用它,而不是直接多次 grep/file search。
execution_subagentexecution_subagenttools/node/executionSubagentTool.ts启动执行型子 agent,运行命令并摘要关键输出。ExecutionSubagentPrompt 只面向终端执行任务;源码中有独立 invocation id,并限制成执行导向循环。
copilot_searchWorkspaceSymbolssearch_workspace_symbolstools/node/searchWorkspaceSymbolsTool.tsx通过语言服务搜索 workspace symbol。适合用户明确找类、函数、接口等符号;依赖 VS Code/workspace language service。
copilot_getVSCodeAPIget_vscode_apitools/node/vscodeAPITool.ts检索 VS Code API 官方文档和扩展开发参考。仅用于 VS Code extension development 场景;package 描述包含明确的 use / not-use 条件。
copilot_findFilesfile_searchtools/node/findFilesTool.tsx按 glob 搜索文件路径。返回路径而非内容;多根 workspace 可用绝对路径限定 root。
copilot_findTextInFilesgrep_searchtools/node/findTextInFilesTool.tsx快速文本/正则搜索文件内容。支持 includePattern 与 includeIgnoredFiles;prompt 提醒可用它获取单文件概览。
copilot_readFileread_filetools/node/readFileTool.tsx读取指定文件的指定行范围。要求给出 1-indexed line range;二进制文件使用 byte offset;prompt 提醒优先读较大、有意义的块。
copilot_viewImageview_imagetools/node/viewImageTool.tsx读取 png/jpg/jpeg/gif/webp 等图像内容。返回 multimodal data 而非文本行;用于模型查看图像文件。
copilot_listDirectorylist_dirtools/node/listDirTool.tsx列出目录直接子项。文件夹以 `/` 标识;用于建立结构视图,不读取文件内容。
copilot_readProjectStructureread_project_structuretools/node/readProjectStructureTool.ts返回 workspace 文件树结构。作为全局结构上下文,避免 agent 先盲目搜索。
copilot_getErrorsget_errorstools/node/getErrorsTool.tsx读取编译、lint 或 diagnostics 问题。从 VS Code diagnostics 获取用户实际看到的问题;编辑后也用于验证。
copilot_getChangedFilesget_changed_filestools/node/scmChangesTool.ts读取当前 git/SCM 变更 diff。依赖 VS Code SCM/Git 服务;用于总结未提交修改或避免覆盖用户改动。
copilot_testFailuretest_failuretools/node/testFailureTool.tsx把测试失败信息加入 prompt。测试相关 intent 可用它补充失败上下文。
copilot_findTestFilestest_searchtools/node/findTestsFilesTool.tsx在源码文件和测试文件之间互相定位。用来找 code under test 或对应 test file。
copilot_applyPatchapply_patchtools/node/applyPatchTool.tsx + applyPatch/parser.ts按 V4A patch 格式修改文本文件。自带 parser 和 corpus 测试;执行后进入 edit result/diagnostics 流程,适合多处明确补丁。
copilot_insertEditinsert_edit_into_filetools/node/insertEditTool.tsx对已有文件做高层次插入/编辑。Prompt 要求用 `// ...existing code...` 表示省略区域;比 replaceString 更智能但更依赖模型表达。
copilot_replaceStringreplace_string_in_filetools/node/replaceStringTool.tsx + abstractReplaceStringTool.tsx用精确 oldString/newString 替换单处文本。要求 oldString 唯一且包含上下文;源码共享 AbstractReplaceStringTool 的 edit/diagnostics/healing 流程。
copilot_multiReplaceStringmulti_replace_string_in_filetools/node/multiReplaceStringTool.tsx + abstractReplaceStringTool.tsx一次调用执行多个 replaceString 操作。DefaultReminderInstructions 要求多处独立编辑时优先使用它以降低轮次和成本。
copilot_createFilecreate_filetools/node/createFileTool.tsx创建新文件并写入内容。目录不存在时可创建;package 明确不要用它编辑已存在文件。
copilot_createDirectorycreate_directorytools/node/createDirectoryTool.tsx递归创建目录结构。类似 mkdir -p;createFile 本身也会创建所需目录。
copilot_editFilesedit_filespackage.json placeholder占位工具。package 描述为 placeholder tool, do not use;当前 allTools 注册清单未见具体 ToolRegistry 实现。
copilot_createNewWorkspacecreate_new_workspacetools/node/newWorkspace/newWorkspaceTool.tsx为新项目生成完整 workspace 搭建步骤。用于完整 project initialization,不用于单文件创建或修改既有代码。
copilot_getProjectSetupInfoget_project_setup_infotools/node/newWorkspace/projectSetupInfoTool.tsx获取项目类型和语言对应的 setup 信息。package 明确要求先调用 create_new_workspace,再使用本工具。
copilot_installExtensioninstall_extensiontools/node/installExtensionTool.tsx安装 VS Code 扩展。仅作为新 workspace 创建流程的一部分使用,不是通用扩展管理工具。
copilot_runVscodeCommandrun_vscode_commandtools/node/vscodeCmdTool.tsx运行 VS Code command。只应作为新 workspace 创建流程的一部分使用;不是任意命令执行的主路径。
copilot_createNewJupyterNotebookcreate_new_jupyter_notebooktools/node/newNotebookTool.tsx生成新的 Jupyter Notebook。package 提醒除非用户明确要求或已有 notebook,否则优先创建普通 Python 等文本文件。
copilot_editNotebookedit_notebook_filetools/node/editNotebookTool.tsx编辑现有 notebook cell。保留 cell 结构;更新 cell 内容时必须保留空白和缩进,不能使用 existing-code marker。
copilot_runNotebookCellrun_notebook_celltools/node/runNotebookCellTool.tsx运行 notebook 代码单元并返回输出。不运行 markdown cell;常用于数据分析任务中让 kernel 状态保持最新。
copilot_getNotebookSummarycopilot_getNotebookSummarytools/node/notebookSummaryTool.tsx获取 notebook cell 列表、类型、行范围、语言、执行信息和输出 mime。用于先定位 cell id 和 line range,再用 readFile 或 runNotebookCell 操作。
copilot_readNotebookCellOutputread_notebook_cell_outputtools/node/getNotebookCellOutputTool.tsx读取某个 notebook cell 的最近输出。输出 token 上限高于 runNotebookCell 的即时输出,适合查看持久化结果。
copilot_fetchWebPagefetch_webpagetools/vscode-node/fetchWebPageTool.tsx抓取网页主要内容。用于用户明确要分析某个 URL;注册在 vscode-node 层,依赖 VS Code/网络环境。
copilot_githubRepogithub_repotools/node/githubRepoTool.tsx搜索指定 GitHub 仓库片段。package 要求仅当用户非常明确询问某个 GitHub repo,且该 repo 未在 workspace 中打开时使用。
copilot_getSearchResultsget_search_view_resultstools/node/getSearchViewResultsTool.tsx读取 VS Code Search View 当前结果。复用用户已经在搜索面板得到的上下文。
copilot_switchAgentswitch_agenttools/vscode-node/switchAgentTool.ts切换到 Plan agent。package 详细列出适合 switch 的场景:新增功能、多方案、架构决策、多文件变更、需求不清等。
copilot_memorymemorytools/node/memoryTool.tsx管理持久记忆。支持 user/session/repo 三个 scope;命令包含 view、create、str_replace、insert、delete、rename。
copilot_resolveMemoryFileUriresolve_memory_file_uritools/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.tsxAgentPrompt 主装配器,组合 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 编排文件的细节。

返回 Copilot Chat 架构总览

资料来源