本文基于 VSCode Agent/Prompt.txt 文件,深入分析了 VSCode Agent(其身份被指定为 "GitHub Copilot")的系统提示词设计。我们将详细解读该提示词如何通过明确的身份设定、强调主动上下文收集和任务分解的核心指令、独特的并行与隐藏式工具使用哲学、以 insert_edit_into_file 和 get_errors 为核心的精确编辑规范,以及丰富的工具集,构建一个深度集成于 VS Code、具备强大自主探索和执行能力的 AI 编程助手。文章将大量引用提示词原文,以具体展示其设计策略与实现细节。
引言:深度集成 IDE 的 AI 编程范式
VSCode Agent 的系统提示词 (Prompt.txt) 为我们呈现了一个旨在与 VS Code 开发环境无缝融合的 AI 编程助手的蓝图。这份提示词不仅仅定义了 AI 的角色,更关键的是,它建立了一套详尽的行为规范和交互逻辑,强调主动性、精确性和对开发环境的深度利用。通过剖析这些指令,我们可以更清晰地理解 GitHub Copilot 在 VS Code 中是如何被设计来辅助开发者的。
身份与行为准则:专业的 GitHub Copilot
提示词开篇便在 <identity> 标签中为 Agent 设定了清晰的角色和必须遵守的规则,奠定了其行为基调:
You are an AI programming assistant.
When asked for your name, you must respond with "GitHub Copilot".
Follow the user's requirements carefully & to the letter.
Follow Microsoft content policies.
Avoid content that violates copyrights.
If you are asked to generate content that is harmful, hateful, racist, sexist, lewd, violent, or completely irrelevant to software engineering, only respond with "Sorry, I can't assist with that."
Keep your answers short and impersonal.这部分内容不仅赋予了它“GitHub Copilot”的官方名称,更重要的是确立了几个核心原则:严格遵循指令("carefully & to the letter")、内容安全(遵守微软政策,拒绝有害内容)以及沟通风格("short and impersonal"),旨在塑造一个专业、可靠且专注于任务的形象。
核心指令:主动性、上下文优先与任务分解
<instructions> 部分是驱动 Agent 行为的核心,极大地强调了其主动性和对上下文的依赖:
主动研究与探索被置于首位: "The user will ask a question, or ask you to perform a task, and it may require lots of research to answer correctly. There is a selection of tools that let you perform actions or retrieve helpful context..."
上下文收集是前提: "Don't make assumptions about the situation- gather context first, then perform the task or answer the question."
上下文收集的责任感与彻底性: "It's YOUR RESPONSIBILITY to make sure that you have done all you can to collect necessary context." 并且 "Don't give up unless you are sure the request cannot be fulfilled with the tools you have."
鼓励探索与创新: "Think creatively and explore the workspace in order to make a complete fix."
项目环境感知: "If you can infer the project type (languages, frameworks, and libraries) from the user's query or the context that you have, make sure to keep them in mind when making changes."
结构化任务处理: "If the user wants you to implement a feature and they have not specified the files to edit, first break down the user's request into smaller concepts and think about the kinds of files you need to grasp each concept."
搜索工具偏好: "Prefer using the semantic_search tool to search for context unless you know the exact string or filename pattern you're searching for."
这些指令共同塑造了一个积极主动、不轻易放弃、重视环境理解和结构化思考的 Agent 形象,要求它在行动前必须进行充分的“侦察”和规划。
工具使用哲学:并行、主动、隐藏实现
<toolUseInstructions> 部分详细规定了 Agent 如何与工具交互,体现了效率与用户体验并重的设计思想:
严格的 Schema 遵循: "When using a tool, follow the json schema very carefully and make sure to include ALL required properties. Always output valid JSON when using a tool."
工具优先原则: "If a tool exists to do a task, use the tool instead of asking the user to manually take an action."
行动的自主性: "If you say that you will take an action, then go ahead and use the tool to do it. No need to ask permission."
对用户隐藏实现细节: "Never say the name of a tool to a user. For example, instead of saying that you'll use the run_in_terminal tool, say 'I'll run the command in a terminal'."
有条件的并行处理: "If you think running multiple tools can answer the user's question, prefer calling them in parallel whenever possible, but do not call semantic_search in parallel." 同时对终端操作有特殊限制:"Don't call the run_in_terminal tool multiple times in parallel. Instead, run one command and wait for the output before running the next command."
利用用户反馈进行学习: "After you have performed the user's task, if the user corrected something you did, expressed a coding preference, or communicated a fact that you need to remember, use the update_user_preferences tool to save their preferences." (尽管
update_user_preferences未在<functions>列出,但指令中提到了其用途)。
这些规则旨在让 Agent 的工具调用过程既高效(并行、自主)又顺畅(隐藏细节),同时具备一定的适应性(保存用户偏好)。
文件编辑规范:精确、验证、隐藏细节
对于核心的编码任务——文件编辑,<editFileInstructions> 制定了极为详尽且严格的流程,其核心是 insert_edit_into_file 工具和 get_errors 工具的配合:
强制先读后写: "Don't try to edit an existing file without reading it first, so you can make changes properly."
唯一的编辑入口: "Use the insert_edit_into_file tool to edit files." 并且反复强调:"NEVER print out a codeblock with file changes unless the user asked for it. Use the insert_edit_into_file tool instead."
对用户透明化编辑过程: "NEVER show the changes to the user, just call the tool, and the edits will be applied and shown to the user."
智能且简洁的编辑格式: "The insert_edit_into_file tool is very smart and can understand how to apply your edits to the user's files, you just need to provide minimal hints." 提示词给出了明确的格式要求和示例:
When you use the insert_edit_into_file tool, avoid repeating existing code, instead use comments to represent regions of unchanged code. The tool prefers that you are as concise as possible. For example: // ...existing code... changed code // ...existing code... changed code // ...existing code...Here is an example of how you should format an edit to an existing Person class: class Person { // ...existing code... age: number; // ...existing code... getAge() { return this.age; } }强制的编辑后验证: "After editing a file, you MUST call get_errors to validate the change. Fix the errors if they are relevant to your change or the prompt, and remember to validate that they were actually fixed."
这套规范旨在确保所有文件修改都通过一个可控、智能的接口进行,并且强制进行事后验证,极大地提高了代码修改的可靠性,同时保持了用户交互的简洁性。
丰富的工具集:赋能广泛的开发任务
提示词的 <functions> 部分定义了一个功能强大的工具集,为 Agent 提供了执行各种开发任务的能力。关键工具及其描述摘要如下(基于 Prompt.txt):
semantic_search: "Run a natural language search for relevant code... Returns relevant code snippets... or the full contents... if it is small." (首选搜索方式)list_code_usages: "Request to list all usages (references, definitions, implementations etc) of a symbol..." (用于代码追踪和重构)read_file: "Read the contents of a file... You must specify the line range..." (获取文件内容)insert_edit_into_file: "Insert new code into an existing file... The system is very smart... Avoid repeating existing code, instead use comments..." (核心编辑工具)get_errors: "Get any compile or lint errors in a code file... Also use this tool after editing a file to validate the change." (强制验证步骤)run_in_terminal: "Run a shell command in a terminal. State is persistent... If the command is a long-running background process, you MUST pass isBackground=true." (执行命令)get_terminal_output: "Get the output of a terminal command previous started with run_in_terminal" (获取后台命令输出)file_search: "Search for files... by glob pattern. Use this tool when you know the exact filename pattern..." (精确文件查找)grep_search: "Do a text search... Use this tool when you know the exact string..." (精确文本查找)get_changed_files: "Get git diffs of current file changes..." (集成版本控制)以及用于项目创建 (
create_new_workspace,get_project_setup_info), VS Code API查询 (get_vscode_api), 网页抓取 (fetch_webpage) 等多种辅助工具。
这个全面的工具库是 Agent 实现其“专家级知识”和执行复杂任务的基础。
结论:一个主动、精确且深度集成的 VS Code 伙伴
VSCode Agent (GitHub Copilot) 的系统提示词通过精心设计的指令和规范,成功塑造了一个深度集成于 VS Code、具备高度主动性、强调上下文理解、采用精确编辑流程并拥有强大工具集的 AI 编程伙伴。其设计哲学在赋予 AI 强大能力的同时,处处体现了对效率、可靠性、用户体验和开发环境整合的深度考量。从强制的上下文收集责任,到智能且受控的文件编辑与验证流程,再到灵活的并行工具调用策略,这份提示词为我们展示了构建顶尖 IDE AI 助手的一种先进范式。



评论区