近期,微软于 Build 2025 开发者大会上开源了 GitHub Copilot Extension for VS Code 项目,采用 MIT 许可证,这一举措在开发者社区引起了不小的轰动。作为 Vibe Coding 的核心利器,宝子们让我们一起深入探究其代码架构和核心功能,理解 Agentic coding 的实现原理,从中汲取养分。
知己知彼,百战不殆;道高一尺,我高一丈!
简介
Copilot Chat extension for VS Code 是 Visual Studio Code 的一个扩展,为编码提供由 AI 驱动的对话式辅助功能。这是 GitHub Copilot 的配套扩展,增加了聊天功能和 Agent Coding 能力。接下来我们试图分析其架构、关键组件和工作原理。
- 项目主要功能特点:
- 聊天界面:提供对话式 AI 辅助、聊天参与者、变量和斜杠命令
- Inline 聊天:直接在编辑器中进行 AI 辅助编辑(使用
Ctrl+I) - 代理(Agent)模式:多步骤自主编码任务执行
- 编辑(Edit)模式:自然语言到代码转换
- 代码补全:下一步编辑建议和内联补全
- 语言模型集成:支持多种 AI 模型(GPT-4, Claude, Gemini 等)
- 上下文感知:工作区理解、语义搜索和代码分析
- 技术栈:
- 主要语言:TypeScript(遵循 VS Code 编码标准)
- 前端框架:TSX(使用 @vscode/prompt-tsx 库构建 prompts)
- 运行时环境:Node.js(扩展宿主和语言服务器功能)
- 性能优化:WebAssembly(用于性能关键的解析和标记化)
- API 集成:VS Code Extension API(大量使用提议的 API)
- 构建工具:ESBuild(打包和编译)
- 测试框架:Vitest(单元测试)
- 其他语言:Python(用于笔记本集成和 ML 评估脚本)
- 目录结构:
src/extension/: 主要扩展实现,按功能组织src/platform/: 共享平台服务和实用程序src/util/: 通用工具、VS Code API 抽象和服务基础架构
核心提示系统
项目的核心提示工程内容位于 src/extension/prompts/ 目录下。我们来看看专业的复杂提示词是怎么写的:他的提示词基于 TSX 的声明式UX,充斥着各种专业提示词工程技巧,俗称专业AI PUA。核心文件位于:
src/extension/prompts/node/agent/agentPrompt.tsx: 代理模式提示的主要入口点/src/extension/prompts/node/agent/agentInstructions.tsx: 代理模式提示模板
代理模式提示包括几个关键组件:
AgentPrompt类:主要的提示协调器,处理整体的代理提示渲染DefaultAgentPrompt类:标准代理行为指令SweBenchAgentPrompt类:高级问题解决模式指令AgentUserMessage类:处理用户输入和环境上下文GlobalAgentContext类:提供环境和工作区信息
此外还有CopilotIdentityRules: When asked for your name, you must respond with "GitHub Copilot"...; 以及SafetyRules: Avoid content that violates copyrights.. 云云。
提示系统使用 TSX 标签来构建,这使得提示更加结构化和可维护:
async render(state: void, sizing: PromptSizing) {
const instructions = this.configurationService.getConfig(ConfigKey.Internal.SweBenchAgentPrompt) ?
<SweBenchAgentPrompt availableTools={this.props.promptContext.tools?.availableTools} modelFamily={this.props.endpoint.family} codesearchMode={undefined} /> :
<DefaultAgentPrompt
availableTools={this.props.promptContext.tools?.availableTools}
modelFamily={this.props.endpoint.family}
codesearchMode={this.props.codesearchMode}
/>;
const baseInstructions = <>
<SystemMessage>
You are an expert AI programming assistant, working with a user in the VS Code editor.<br />
<CopilotIdentityRules />
<SafetyRules />
</SystemMessage>
// ...其他提示组件
</>;
}
SweBenchAgentPrompt中是一系列的繁文缛节,例如:
- Make sure you fully understand the issue described by user and can confidently reproduce it.
...
以及:
- MUST DO: Before making your final summary, you must use
git diffcommand to review all files you have edited to verify that the final successful fix validated by reproducing script has been correctly applied to all the corresponding files.
可见Agent模式名不虚传,是有能力自己看diff。以上位于mostImportantInstructions定义中。此外其中还有agentInstructions和searchInstructions、editFileInstructions等等,十分耗费token,跟不要钱似的。一般的prompt参数模板已经不足以概括,他们整体形成了一个复杂的prompt模板网络,包含诸多分支。
以下类图展示了Agent和核心提示词组件及其关系:
classDiagram
class AgentPrompt {
+render()
-getGlobalAgentContext()
}
class DefaultAgentPrompt {
+render()
}
class AgentUserMessage {
+render()
}
class GlobalAgentContext {
+render()
}
class ToolCallingLoop {
+runToolCallingLoop()
-parseToolCalls()
-executeToolCalls()
}
class ChatAgentService {
+register()
}
class ToolsService {
+getTools()
+executeToolCall()
+registerTool()
}
class AgentIntentInvocation {
+buildPrompt()
}
AgentPrompt --|> PromptElement
DefaultAgentPrompt --|> PromptElement
AgentUserMessage --|> PromptElement
GlobalAgentContext --|> PromptElement
AgentPrompt --> DefaultAgentPrompt : uses
AgentPrompt --> AgentUserMessage : uses
AgentPrompt --> GlobalAgentContext : uses
AgentIntentInvocation --> AgentPrompt : uses
AgentIntentInvocation --> ToolCallingLoop : uses
ChatAgentService --> AgentIntentInvocation : creates
ToolCallingLoop --> ToolsService : uses
class PromptElement {
<<abstract>>
+render()
}
AgentUserMessage 才是承载用户指令信息的核心。同时当然也要支持聊天历史,AgentUserMessageInHistory就承载了这样的角色,turn会把会话的参数信息、编辑文件的历史、信息内容带进来作为上下文参考,由AgentConversationHistory 管理。
第一讲我们先看到这里,内容非常多,下一讲我们继续深入探索 prompt的构建和tool calling loop,试图展示代理模式的整体概况。
- 第一讲:核心提示系统 <-
Where We Are Now - 第二讲:代理模式的实现
- 第三讲:大模型的调用
- 第四讲:MCP的实现