通过


将内置代理和自定义代理用于 GitHub Copilot

Visual Studio包括一组特选的内置代理,这些代理与 IDE 功能(如调试、分析和测试)深度集成。 还可以创建自定义代理,以满足团队的工作方式。

先决条件

访问自定义代理

可以通过两种方式访问自定义代理:

  • Agent 选取器:在Copilot Chat窗口中,选择代理选取器下拉列表以查看可用的代理。 目前,此选项仅在 Visual Studio 2026 预览体验成员版本中可用。
  • @ 语法:在聊天输入中键入 @ 后跟智能体名称(例如,@debugger)。

可以使用 @ 语法访问自定义代理:键入 @ 后跟聊天输入中的代理名称(例如, @profiler)。

此屏幕截图显示 Visual Studio 中带有自定义智能体的智能体选择器。

显示具有自定义代理的代理选取器的屏幕截图。

内置代理

每个内置代理都侧重于特定的开发人员工作流。 这些程序与Visual Studio的本机工具深度集成,展现了通用助理无法实现的功能。

代理人 说明
@debugger 不止是读取错误消息那么简单。 使用调用堆栈、变量状态和诊断工具在整个解决方案中系统地完成错误诊断。
@profiler 连接到Visual Studio的性能分析基础结构,以识别代码库中的瓶颈并建议针对性的优化,这些建议都是基于你的代码库,而非一般性建议。
@test 生成针对项目框架和模式调优的单元测试,而非 CI 会拒绝的样板代码。
@modernize (仅限 .NET 和 C++)处理框架和依赖项升级,并感知实际的项目图。 标记重大变更、生成迁移代码,并保持与现有模式一致。
代理人 说明
@profiler 连接到Visual Studio的性能分析基础设施,以识别瓶颈并提供针对代码库的优化建议,并非泛泛之谈。

使用 @debugger 代理

代理程序@debugger通过分析调试上下文来系统地帮助您诊断错误。 它还支持端到端的代理式工作流,可重现 bug、使用跟踪点和条件断点检测应用,并利用实时运行时数据验证修复。 有关详细信息,请参阅使用调试程序智能体以智能体方式解决 bug

示例提示

  • @debugger Why is this exception being thrown?
  • @debugger Analyze the current call stack and explain what went wrong
  • @debugger What's causing the null reference in this method?

使用 @profiler 代理

@profiler 代理连接到Visual Studio的分析工具,以帮助识别和修复性能问题。

示例提示

  • @profiler Find the performance bottlenecks in my application
  • @profiler Why is this method taking so long to execute?
  • @profiler Suggest optimizations for the hot path

使用 @test 代理

代理 @test 生成与项目测试框架和约定匹配的单元测试。

示例提示

  • @test Generate unit tests for the selected method
  • @test Create tests that cover edge cases for this class
  • @test Write integration tests for this API endpoint

有关更全面的 .NET 测试支持,请参阅 GitHub Copilot .NET 测试

使用 @modernize 代理

@modernize 代理可帮助.NET和 C++ 项目的框架迁移和依赖项升级。

对于.NET现代化工作流,代理支持三个阶段的过程:

  • 评估:评审包版本、目标框架选项、项目清单和 API 兼容性风险。
  • 计划:生成与当前评估和更新优先级一致的迁移计划。
  • 任务执行:使用动态任务文件完成现代化任务,可以在工作进行时进行编辑。

示例提示

  • @modernize Upgrade this project to .NET 8
  • @modernize What breaking changes should I expect when migrating?
  • @modernize Update deprecated API calls in this file
  • @modernize Assess this solution, generate a migration plan, and create execution tasks

有关 GitHub Copilot 应用程序在 .NET 上现代化的端到端指南,请参阅 GitHub Copilot 应用程序现代化概述

自定义代理

注释

自定义代理需要Visual Studio 2026 18.4 或更高版本。

内置代理涵盖常见工作流,但团队最了解工作流。 使用自定义代理可以使用相同的基础构建自己的代理:工作区感知、代码理解、首选 AI 模型和自己的工具。

MCP(模型上下文协议)结合使用时,自定义代理变得特别强大。 可以将代理连接到外部知识源,例如内部文档、设计系统、API 和数据库,因此代理不限于存储库中的内容。

创建自定义代理

将自定义代理定义为存储库中的 .agent.md 文件夹内的 .github/agents/ 文件:

your-repo/
└── .github/
    └── agents/
        └── code-reviewer.agent.md

代理文件格式

每个代理文件都使用一个简单的模板,其中包含 YAML 前置数据,接着是 Markdown 指令:

---
name: Code Reviewer
description: Reviews PRs against our team's coding standards
model: claude-opus-4-6
tools: ["code_search", "readfile", "find_references"]
---

You are a code reviewer for our team. When reviewing changes, check for:

- Naming conventions: PascalCase for public methods, camelCase for private
- Error handling: all async calls must have try/catch with structured logging
- Test coverage: every public method needs at least one unit test

Flag violations clearly and suggest fixes inline.

Frontmatter 属性

资产 必选 说明
name 智能体在智能体选取器中显示的名称。 如果未指定此属性,则代理名称来自文件名(例如, code-reviewer.agent.md 变为 code-reviewer)。
description Yes 将鼠标悬停在智能体上时显示的简要说明
model 要使用的 AI 模型。 如果未指定此属性,则使用模型选取器中选择的模型。
tools 代理可以使用的工具名称数组。 如果未指定此属性,则启用所有可用的工具。

指定工具

工具可以扩展您的自定义代理能够执行的功能。 可以指定代理应在数组中使用的 tools 工具。

重要

工具名称因GitHub Copilot平台而异。 检查Visual Studio中可用的工具,以确保代理按预期工作。 在聊天窗口中选择 “工具” 图标以查看可用的工具名称。

使用 MCP 连接到外部源

通过使用 MCP 服务器,自定义代理可以访问外部知识源,例如:

  • 内部文档和 Wiki
  • 设计系统和组件库
  • API 和数据库
  • 样式指南和 ADR 存储库

例如,代码审查智能体可以通过 MCP 连接到你的风格指南,从而根据你的实际约定检查 PR。

自定义代理示例

代码评审代理

---
name: Code Reviewer
description: Reviews code against our team's coding standards
tools: ["code_search", "readfile"]
---

You are a code reviewer for our team. Review changes for:

1. **Naming conventions**: PascalCase for public methods, camelCase for private fields
2. **Error handling**: All async calls must have try/catch with structured logging
3. **Test coverage**: Every public method needs at least one unit test
4. **Documentation**: Public APIs must have XML documentation comments

Flag violations clearly and suggest fixes inline.

规划代理

---
name: Feature Planner
description: Helps plan features before writing code
tools: ["code_search", "readfile", "find_references"]
---

You are a planning assistant. When asked about a feature:

1. Gather requirements by asking clarifying questions
2. Identify affected files and components in the codebase
3. Break down the work into discrete tasks
4. Flag potential risks or dependencies
5. Create a structured plan that can be handed off for implementation

Focus on understanding scope before suggesting solutions.

设计系统代理

---
name: Design System
description: Enforces UI design patterns and component usage
tools: ["code_search", "readfile"]
---

You are a design system expert. When reviewing UI code:

1. Check that standard components are used instead of custom implementations
2. Verify spacing and layout follow the design token system
3. Ensure accessibility requirements are met (ARIA labels, keyboard navigation)
4. Flag any UI drift from established patterns

Reference the component library documentation when suggesting fixes.

使用 Visual Studio 工具的全堆栈开发代理

以下示例使用特定于Visual Studio的工具名称:

---
name: Full Stack Dev
description: Full-stack development assistant with search, file editing, and terminal access
tools: ["code_search", "readfile", "editfiles", "find_references", "runcommandinterminal", "getwebpages"]
---

You are a full-stack development assistant. Help with:

1. Searching the codebase to understand existing patterns
2. Reading and editing files to implement changes
3. Running build and test commands to verify your work
4. Looking up documentation when needed

Always check existing code conventions before making changes.

小窍门

选择Copilot Chat窗口中的 Tools 图标以查看Visual Studio版本中的所有可用工具名称。

.NET开发代理

.NET团队在 awesome-copilot 存储库中维护 C# 和Windows Forms开发的特选自定义代理。 开始之前:

  1. 下载 CSharpExpert.agent.mdWinFormsExpert.agent.md
  2. 将文件添加到存储库 .github/agents/ 的文件夹。
  3. 在代理模式下打开Copilot Chat,并从代理选取器中选择代理。

小窍门

选择 Tools>Options>GitHub>Copilot,然后启用 项目特定的 .NET 指令(如 Windows Forms 开发适用时)来自动为代码库添加适当的自定义代理。

C# 专家

C# 专家代理将新式 C# 约定应用于Copilot的代码生成:

  • 语法和性能:遵循当前最佳做法,同时匹配存储库的现有约定。
  • 最小更改:仅生成所需的代码,使用 async/await 进行适当的取消和异常处理。 避免未使用的接口、方法或参数。
  • 测试:支持行为驱动的单元测试、集成测试和 TDD 工作流。

WinForms 专家

WinForms 专家智能体面向 .NET 8 至 .NET 10 上的 Windows Forms 开发:

  • Designer 代码保护:防止.Designer.cs损坏,使Windows Forms设计器在Copilot编辑后继续工作。
  • UI 设计模式:MVVM 和 MVP 模式,包括社区工具包数据绑定。
  • Modern .NET:正确的 InvokeAsync 重载、深色模式、高分辨率屏幕感知和可为空引用类型。
  • 布局:用于响应式、支持 DPI 的布局的 TableLayoutPanelFlowLayoutPanel
  • CodeDOM 序列化:正确处理设计器属性的[DefaultValue] 特性和 ShouldSerialize*() 方法。
  • 异常处理:异步事件处理程序模式和应用程序级异常处理。

社区配置

awesome-copilot 代码库拥有由社区贡献的代理配置,你可以将它们用作起点。 使用此存储库中的配置时,请在部署到团队之前验证工具名称在Visual Studio中正常工作。

限制和说明

  • 如果未指定模型,代理将使用模型选取器中选择的任何模型。
  • 工具名称因GitHub Copilot平台而异。 在部署到团队之前,请验证工具名称在Visual Studio中正常工作。

共享反馈

通过在 awesome-copilot 存储库分享您的自定义代理配置,或在 Visual Studio 开发者社区提交反馈。 工作流有助于塑造未来的功能。