在Visual Studio(预览版)中使用Copilot代理模式
Use Copilot Agent Mode in Visual Studio (Preview)

原始链接: https://learn.microsoft.com/en-us/visualstudio/ide/copilot-agent-mode?view=vs-2022

Visual Studio 中的 GitHub Copilot 代理模式允许你使用自然语言指示 AI 修改你的代码库。它会推断你的请求,规划必要的工作,并通过结合代码编辑和工具调用来执行它。这与 Copilot Chat 不同,因为代理模式可以运行命令,迭代错误(构建失败、测试结果)并自主解决问题。 要使用它,请在 Visual Studio 的设置中启用代理模式(工具 > 选项 > GitHub > Copilot > Copilot Chat)。然后,在 Copilot Chat 窗口中,选择“代理”模式并输入你的任务。Copilot 会确定上下文和文件,可能使用数据库迁移或 dotnet restore 等工具。系统会提示你确认工具的使用。 代理模式会直接将代码编辑流式传输到编辑器中,允许你逐个或整体地查看并接受或放弃更改。它会监控编辑结果并迭代以解决出现的任何问题,如有必要,可以多次迭代。你可以中断请求,将编辑还原到之前的检查点,并通过聊天窗口中的“工具”图标管理授权工具。

这个 Hacker News 帖子讨论了 Visual Studio 预览版中新的 Copilot Agent 模式。用户们将它与 Cursor、Windsurf、Roocode 和 Claude Code 等替代方案进行了比较,偏好各不相同。一些人发现 Copilot 有助于自动化诸如 bug 修复和测试编写等任务,通过减少更改所需的工作量来提高代码质量。然而,另一些人则提到了一些担忧,例如大型项目中的 token 使用量以及与 VS Code 及其与 Claude 4 集成的速度相比显得滞后。 一个主要的争议点是隐私。一位用户批评 Copilot 在未购买企业版的情况下隐式地使用私有仓库代码进行训练,而另一些用户则指出个人许可证文档声明,除非在设置中明确启用,否则数据不会用于训练。一些人认为,当包含所有源代码时,Claude 执行类似 grep 操作的能力非常有用,但在使用 Nuget 包时则不然,这表明需要更好地与智能感知工具集成。
相关文章

原文

With GitHub Copilot's agent mode in Visual Studio, you can use natural language to specify a high-level task. AI will then autonomously reason through the request, plan the work needed, and apply the changes to your codebase. Agent mode combines code editing and tool invocation to accomplish the task you specified. As it processes your request, it monitors the outcome of edits and tools, and iterates to resolve any issues that arise.

The key difference from Copilot Chat is that agent mode can:

  • Run commands and builds to interpret the environment or execute a task (for example, database migrations, dotnet restore, etc.).
  • Iterate on errors, failed builds, or unit test results until it either requires additional input or considers the task complete.

Prerequisites

Get started with agent mode

To get started with Copilot agent mode in Visual Studio, enable the feature in Tools > Options > GitHub > Copilot > Copilot Chat > Enable agent mode in the chat pane.

Use agent mode

In agent mode, Copilot operates autonomously and determines the relevant context for your prompt.

Follow these steps to get started:

  1. Ensure agent mode is enabled by selecting Enable agent mode in the chat pane in Tools > Options > GitHub > Copilot > Copilot Chat.

    Screenshot that shows the enable agent mode setting in Options.

  2. Open the Copilot Chat window, select Ask to expand the mode dropdown, and then select Agent.

    Screenshot that shows Copilot agent mode selector.

  3. Enter your prompt for making edits in the chat input field and select Send or press Enter to submit it. You can specify a high-level requirement, and you don't have to specify which files to work on. In agent mode, Copilot determines the relevant context and the files to edit autonomously.

  4. Agent mode might invoke multiple tools to accomplish different tasks. Optionally, select the Tools icon to configure which additional tools can be used for responding to your request.

    Screenshot that shows additional tools used by agent mode.

  5. Confirm tool invocations and terminal commands. Before running a terminal command or a non-builtin tool, Copilot requests confirmation to continue. This is because tools might run locally on your machine and perform actions that modify files or data.

    Screenshot that shows agent command approval.

  6. Copilot detects issues and problems in code edits and terminal commands, and then iterates and performs additional actions to resolve them. For example, agent mode might run unit tests as a result of a code edit. If the tests fail, it uses the test outcome to resolve the issue. Copilot agent mode iterates multiple times to resolve issues and problems.

  7. As Copilot processes your request, notice that Copilot streams the suggested code edits directly in the editor. Review the suggested edits and either keep or discard the suggested edits as a whole in Total Changes in the chat window, or individually by clicking on a file and reviewing the code diffs presented in the editor.

    Screenshot that shows the list of suggested edits.

  8. If you want to review individual code changes made by the agent, you can either review the specific change made at each step, or review the cumulative changes from the last time changes were kept or undone.

    Screenshot that shows accessing individual edit diffs with Copilot agent.

    Screenshot that shows accessing cumulative edit diffs with Copilot agent.

  9. Continue to iterate on the code changes to refine the edits or implement additional features.

Agent mode can use the following tools:

You can view and manage the tools that can be used for responding to a request. Select the Tools icon in the chat window to view and manage the tools that are available in agent mode.

Screenshot that shows Copilot agent tool selector.

Based on the outcome of a tool, Copilot might invoke other tools to accomplish the overall request. For example, if a code edit results in syntax errors in the file, Copilot might explore another approach and suggest different code changes.

Additional tools added by running MCP servers are not automatically enabled, they are unchecked by default and must be checked to be activated.

When a tool is invoked, Copilot requests confirmation to run the tool. This is because tools might run locally on your machine and perform actions that modify files or data.

Screenshot that shows tool confirmation request.

In the chat window, after a tool invocation, use the Allow dropdown options to automatically confirm the specific tool for the current session, solution, or all future invocations.

You can reset tool confirmation selections in Tools > Options > GitHub > Copilot > Tools.

Screenshot that shows tool confirmation options.

Accept or discard edits

Copilot lists the files that were edited in the list of Total Changes in the Chat window.

Screenshot that shows the Total Changes list.

Click on each file to review changes individually, where you can Keep or Undo edits made to each chunk of code.

Alternatively, in the Total Changes list, select Keep or Undo for all edits made since the last time you clicked Keep or Undo.

Revert edits

As you're sending requests to make edits to your code, you might want to roll back some of these changes, for example, when you want to use another implementation strategy or if Copilot starts walking down the wrong path when generating edits. To do so, select Restore next to the checkpoint prior to the prompt that included changes you didn't want.

Screenshot that shows reverting edits.

Currently, Visual Studio Copilot Agent doesn't support stepwise undo/redo.

Interrupt an agent mode request

To interrupt an ongoing request, you can cancel it. This stops all running tools and terminal commands.

To stop a build, select Build in the top toolbar, and then select Cancel or use the Ctrl + Break keyboard shortcut.

Frequently asked questions

I don't see Ask and Agent mode in the GitHub Copilot Chat window.

Take the following troubleshooting steps in the order specified:

  • Make sure you're using Visual Studio 17.14 or later: check the version at Help > About Visual Studio. If you're not using version 17.14 or later, launch the Visual Studio Installer and update your build.
  • Make sure you've selected the Enable agent mode in the chat pane setting in Tools > Options > GitHub > Copilot Chat.
  • Try restarting Visual Studio.

When should I use Ask mode vs. Agent mode?

  • Ask mode is excellent when you want 100% confidence that no code edits are made unless you explicitly select Apply or copy and paste the code yourself.
  • Agent mode can handle the same conceptual questions, generate code examples without applying them, along with its agent capabilities of editing code.
  • If you are looking to use MCP capabilities, you must have agent mode selected.

What happened to Copilot Edits in Visual Studio?

  • We perceive agent mode to be an evolution of Edits, with greater ability to iterate on errors, use tools, and automatically apply code changes.
  • For the initial releases of Visual Studio 2022 version 17.14, Edits mode is still available if you uncheck the Enable agent mode in the chat pane setting in Tools > Options > GitHub > Copilot > Copilot Chat.

As an administrator, how do I control use of agent mode for Visual Studio users?

Agent mode in Visual Studio is governed by the Editor preview features flag in the GitHub Copilot dashboard for administrators. If the administrator turns off this setting, users under that subscription won’t be able to use agent mode in Visual Studio.

For more information, see managing policies and features for copilot in your enterprise.

联系我们 contact @ memedata.com