{"meta":{"title":"使用GitHub Copilot处理任务的最佳做法","intro":"了解如何从 Copilot云代理中获取最佳结果。","product":"GitHub Copilot","breadcrumbs":[{"href":"/zh/copilot","title":"GitHub Copilot"},{"href":"/zh/copilot/tutorials","title":"教程"},{"href":"/zh/copilot/tutorials/cloud-agent","title":"云代理"},{"href":"/zh/copilot/tutorials/cloud-agent/get-the-best-results","title":"获得最优结果"}],"documentType":"article"},"body":"# 使用GitHub Copilot处理任务的最佳做法\n\n了解如何从 Copilot云代理中获取最佳结果。\n\n> \\[!NOTE]\n> 关于Copilot云代理的介绍，请查看[关于 GitHub Copilot 云代理](/zh/copilot/concepts/agents/cloud-agent/about-cloud-agent)。\n\n## 确保问题范围明确\n\n```\n          GitHub Copilot 在被分配明确且范围清晰的任务时能提供更好的结果。 理想的任务包括：\n```\n\n* 清晰描述要解决的问题或所需的工作。\n* 关于什么样的解决方案才算好的完整验收标准（例如，是否应该有单元测试？）。\n* 关于需要更改哪些文件的指示。\n\n> \\[!TIP]\n\n```\n          Copilot云代理 能够搜索代码库，包括语义代码搜索，这有助于基于含义而不是完全文本匹配来查找相关代码。 即使未在任务中指定确切的文件路径，代理也可以自行发现正确的代码。\n```\n\n如果通过分配一个问题将任务传递给 Copilot，那么此问题对 Copilot 来说可以看作是一个提示。 考虑问题说明是否可能有效作为 AI 提示，以便 Copilot 进行所需的代码更改。\n\n## 选择授予Copilot的正确任务类型\n\n当你使用Copilot时，你将逐渐了解它最适合处理哪些类型的任务。 最初，你可能希望首先提供 Copilot 更简单的任务，看看它如何作为云代理工作。 例如，你可以首先要求 Copilot 修复 bug、更改用户界面功能、改进测试覆盖率、更新文档、改进辅助功能或解决技术债务。\n\n你可能选择自行处理问题而非分配给 Copilot，包括：\n\n* **复杂、范围广泛的任务**\n  * 需要跨仓库知识和测试的范围广泛、上下文丰富的重构问题\n  * 需要了解依赖项和旧代码的复杂问题\n  * 需要深入域知识的任务\n  * 涉及大量业务逻辑的任务\n  * 对代码库的大规模更改需要保持设计一致性\n\n* **敏感和关键任务**\n  * 生产关键问题\n  * 涉及安全性、个人身份信息、身份验证反响的任务\n  * 事件响应\n\n* **不明确的任务**\n  * 缺少清晰定义的任务：要求不明确的任务、开放式任务、需要从不确定性中寻找解决方案的任务\n\n* **学习任务**\n  * 开发人员希望通过学习实现更深入理解的任务\n\n## 在打开拉取请求之前，先研究、规划和迭代\n\n与其要求 Copilot 立即打开拉取请求，不如先研究 Copilot云代理 存储库、创建实施计划，并在分支上进行迭代代码更改。 这让你在决定打开拉取请求前查看差异并完善工作。\n\n如果想要以下目的，此工作流非常有用：\n\n* 了解代码库在进行更改之前的工作原理。\n* 编写任何代码前，与 Copilot 商定方案。\n* 打开拉取请求评审前，查看并迭代更改。\n\n请参阅“[使用 Copilot云代理 进行代码更改的研究、计划和迭代](/zh/copilot/how-tos/use-copilot-agents/cloud-agent/research-plan-iterate)”。\n\n## 使用评论来迭代拉取请求\n\n使用 Copilot 处理拉取请求时，就像与人类开发人员合作一样：通常在合并之前，拉取请求需要进一步完善。 由 Copilot 创建的拉取请求与人工创建的拉取请求，达到可合并状态的流程完全相同。\n\n此外，您可以：\n\n* 在拉取请求的评论中提及 `@copilot`，说明你认为不正确或可改进的地方，Copilot 会直接将提交推送到拉取请求分支。\n* 请求 Copilot 解决 Pull Request 上的合并冲突。 请参阅“[查看来自 Copilot 的输出](/zh/copilot/how-tos/use-copilot-agents/cloud-agent/make-changes-to-an-existing-pr#resolving-merge-conflicts)”。\n* 自行处理功能分支并将更改推送到拉取请求。\n\n拥有写入权限的用户在评论中提及 `@copilot` 后，Copilot 将开始进行所有必要更改，完成后更新拉取请求。 由于 Copilot 在提交批注后立即开始查看批注，因此如果你可能对拉取请求进行多个批注，最好通过单击“ **开始审阅**”而不是单击“ **添加单个批注**”对其进行批处理。 然后，可以一次性提交所有批注，从而触发 Copilot 处理整个评论，而不是单独处理单个批注。\n\n> \\[!NOTE]\n> Copilot 仅响应对存储库具有写入权限的人的评论。\n\n由于 Copilot 对拉取请求进行更改，它将使标题和正文保持最新状态，以便它们反映当前的更改。\n\n## 将自定义说明添加到代码库\n\n通过向存储库添加自定义说明，可以指导 Copilot 如何了解项目以及如何生成、测试和验证其更改。\n\n如果 Copilot 能够在自己的开发环境中生成、测试和验证其更改，则更有可能生成可快速合并的良好拉取请求。\n\n```\n          Copilot云代理 支持多种不同类型的自定义指令文件：\n```\n\n* `/.github/copilot-instructions.md`\n* `/.github/instructions/**/*.instructions.md`\n* `**/AGENTS.md`\n* `/CLAUDE.md`\n* `/GEMINI.md`\n\n有关详细信息，请参阅“[为GitHub Copilot添加存储库自定义说明](/zh/copilot/customizing-copilot/adding-repository-custom-instructions-for-github-copilot?tool=webui)”。\n\n### 仓库范围的说明\n\n若要添加适用于存储库中分配 Copilot 的所有任务的说明，请在存储库的根目录中创建一个 `.github/copilot-instructions.md` 文件。 此文件应包含有关项目的信息，例如如何生成和测试项目，以及要 Copilot 遵循的任何编码标准或约定。 请注意，这些说明也适用于 副驾驶聊天 和 Copilot 代码评审。\n\n首次要求 Copilot 在给定存储库中创建拉取请求时， Copilot 会留下一条注释，其中包含自动生成自定义说明的链接。 您还可以随时让 Copilot 使用我们推荐的提示为您生成自定义说明。 请参阅“[为GitHub Copilot添加存储库自定义说明](/zh/copilot/how-tos/configure-custom-instructions/add-repository-instructions?tool=webui#asking-copilot-cloud-agent-to-generate-a-copilot-instructionsmd-file)”。\n\n还可以选择随时编写自己的自定义指令。 下面是有效 `copilot-instructions.md` 文件的示例：\n\n```markdown\nThis is a Go based repository with a Ruby client for certain API endpoints. It is primarily responsible for ingesting metered usage for GitHub and recording that usage. Please follow these guidelines when contributing:\n\n## Code Standards\n\n### Required Before Each Commit\n- Run `make fmt` before committing any changes to ensure proper code formatting\n- This will run gofmt on all Go files to maintain consistent style\n\n### Development Flow\n- Build: `make build`\n- Test: `make test`\n- Full CI check: `make ci` (includes build, fmt, lint, test)\n\n## Repository Structure\n- `cmd/`: Main service entry points and executables\n- `internal/`: Logic related to interactions with other GitHub services\n- `lib/`: Core Go packages for billing logic\n- `admin/`: Admin interface components\n- `config/`: Configuration files and templates\n- `docs/`: Documentation\n- `proto/`: Protocol buffer definitions. Run `make proto` after making updates here.\n- `ruby/`: Ruby implementation components. Updates to this folder should include incrementing this version file using semantic versioning: `ruby/lib/billing-platform/version.rb`\n- `testing/`: Test helpers and fixtures\n\n## Key Guidelines\n1. Follow Go best practices and idiomatic patterns\n2. Maintain existing code structure and organization\n3. Use dependency injection patterns where appropriate\n4. Write unit tests for new functionality. Use table-driven unit tests when possible.\n5. Document public APIs and complex logic. Suggest changes to the `docs/` folder when appropriate\n```\n\n### 路径特定说明\n\n若要添加适用于特定类型文件 Copilot 的说明，如单元测试或 React 组件，请在存储库中创建一个或多个 `.github/instructions/**/*.instructions.md` 文件。\n在这些文件中，包括有关文件类型的信息，例如如何生成和测试它们，以及要 Copilot 遵循的任何编码标准或约定。\n\n可以使用说明文件前言中的 glob 模式，指定这些说明应适用于哪些文件类型。 例如，若要为 Playwright 测试创建说明，可以创建一个名为 `.github/instructions/playwright-tests.instructions.md` 的说明文件，其中包含以下内容：\n\n```markdown\n---\napplyTo: \"**/tests/*.spec.ts\"\n---\n\n## Playwright test requirements\n\nWhen writing Playwright tests, please follow these guidelines to ensure consistency and maintainability:\n\n1. **Use stable locators** - Prefer `getByRole()`, `getByText()`, and `getByTestId()` over CSS selectors or XPath\n1. **Write isolated tests** - Each test should be independent and not rely on other tests' state\n1. **Follow naming conventions** - Use descriptive test names and `*.spec.ts` file naming\n1. **Implement proper assertions** - Use Playwright's `expect()` with specific matchers like `toHaveText()`, `toBeVisible()`\n1. **Leverage auto-wait** - Avoid manual `setTimeout()` and rely on Playwright's built-in waiting mechanisms\n1. **Configure cross-browser testing** - Test across Chromium, Firefox, and WebKit browsers\n1. **Use Page Object Model** - Organize selectors and actions into reusable page classes for maintainability\n1. **Handle dynamic content** - Properly wait for elements to load and handle loading states\n1. **Set up proper test data** - Use beforeEach/afterEach hooks for test setup and cleanup\n1. **Configure CI/CD integration** - Set up headless mode, screenshots on failure, and parallel execution\n```\n\n## 组织范围的自定义说明\n\n```\n          Copilot云代理 将组织的自定义指令作为工作的一部分。 \n          Copilot云代理 首先确定存储库范围的自定义说明的优先级。 有关如何配置组织自定义说明的详细信息，请参阅 [AUTOTITLE](/copilot/how-tos/configure-custom-instructions/add-organization-instructions)。\n```\n\n## 使用模型上下文协议 (MCP)\n\n可以使用 MCP 扩展 Copilot云代理 的功能。 这允许 Copilot云代理 使用本地和远程 MCP 服务器提供的工具。\nGitHub MCP 服务器和 [Playwright MCP 服务器](https://github.com/microsoft/playwright-mcp)默认处于启用状态。 有关详细信息，请参阅“[将代理连接到外部工具](/zh/copilot/how-tos/use-copilot-agents/cloud-agent/extend-cloud-agent-with-mcp)”。\n\n## 创建 自定义代理\n\n自定义指令帮助指导 Copilot 在整个存储库中的常规行为，而 自定义代理 创建具有专注专业知识和定制工具配置的完全专用智能体。 这些代理专为域专业知识和一致行为至关重要的特定定期工作流而设计。\n自定义智能体 被定义为名为 代理资料 的 Markdown 文件。\n\n以下是您可以创建的一些示例 自定义代理：\n\n* **测试专家**：使用特定测试框架配置的代理，侧重于测试覆盖率、测试质量和测试最佳做法。 它可能仅限于读取、搜索和编辑工具，以防止对生产代码进行意外更改，同时确保全面的测试覆盖率。\n* **文档专家**：专门创建和维护项目文档的代理，深入了解文档标准、样式指南以及分析代码以生成准确的 API 文档和用户指南的能力。\n* **Python 专家**：了解 Python 约定、常用框架（如 Django 或 Flask）并遵循 PEP 标准的特定于语言的代理。 它将对 Python 工具、虚拟环境和测试框架（如 pytest）具有专业知识。\n\n默认情况下， 自定义代理 继承存储库中配置的任何 MCP 服务器工具，但也可以配置为 自定义代理 仅有权访问特定工具。\n\n可以在使用自定义代理的任何位置使用Copilot云代理，包括分配问题或提示任务时。\n\n有关创建和配置 自定义代理的详细信息，请参阅 [为Copilot云代理创建自定义代理](/zh/copilot/how-tos/use-copilot-agents/cloud-agent/create-custom-agents)。\n\n## 在 GitHub Copilot环境中预安装依赖项\n\n在执行任务期间，Copilot 可以访问自己的临时开发环境（由 GitHub Actions 提供支持），在该环境中，它可以探索你的代码、进行更改、执行自动化测试和 Linter 等操作。\n\n如果 Copilot 能够在自己的开发环境中生成、测试和验证其更改，则更有可能生成可快速合并的良好拉取请求。\n\n为此，其需要项目的依赖项。\nCopilot 可以通过试验和错误过程发现和安装这些依赖项本身 ，但考虑到大型语言模型（LLM）的不确定性质，这可能很慢且不可靠。\n\n可以配置一个 `copilot-setup-steps.yml` 文件，以在代理开始工作前预装这些依赖项，确保其能正常运行。 有关详细信息，请参阅“[配置开发环境](/zh/copilot/how-tos/use-copilot-agents/cloud-agent/customize-the-agent-environment#preinstalling-tools-or-dependencies-in-copilots-environment)”。"}