{"meta":{"title":"为 GitHub Copilot CLI 创建和使用自定义智能体","intro":"为特定开发任务创建具有定制专业知识的专用代理。","product":"GitHub Copilot","breadcrumbs":[{"href":"/zh/copilot","title":"GitHub Copilot"},{"href":"/zh/copilot/how-tos","title":"操作方法"},{"href":"/zh/copilot/how-tos/copilot-cli","title":"Copilot CLI"},{"href":"/zh/copilot/how-tos/copilot-cli/customize-copilot","title":"自定义 Copilot CLI"},{"href":"/zh/copilot/how-tos/copilot-cli/customize-copilot/create-custom-agents-for-cli","title":"创建自定义智能体"}],"documentType":"article"},"body":"# 为 GitHub Copilot CLI 创建和使用自定义智能体\n\n为特定开发任务创建具有定制专业知识的专用代理。\n\n## 介绍\n\nCustom agents 使你能够为特定任务定制 Copilot 的专业知识。\n\n当您提示 Copilot 执行任务时，如果 custom agents 确定代理的专业知识非常适合该任务，那么它可能会选择使用您的 Copilot。\n\n由 custom agent 执行的工作通过一个子代理来完成，该子代理是一个临时代理，用于完成任务。 子代理有自己的上下文窗口，可以通过与主代理无关的信息来填充该窗口。 通过这种方式，尤其是对于较大的任务，可以将部分工作移交给custom agents，从而避免扰乱主代理的上下文窗口。 然后，主代理可以专注于更高级别的规划和协调。\n\n有关详细信息，请参阅“[关于自定义代理](/zh/copilot/concepts/agents/copilot-cli/about-custom-agents)”。\n\n## 创建 custom agent\n\n每个 custom agent 文件都由扩展名为 `.agent.md` Markdown 文件定义。 可以自行创建这些文件，也可以从 CLI 中添加这些文件，如以下步骤中所述。\n\n1. 在交互模式下，输入 `/agent`。\n\n2. 从选项列表中选择 **“创建新代理** ”。\n\n3. 选择在存储库或主目录中创建 custom agent 的选项：\n\n   * **Project** （`.github/agents/`）\n\n   * **用户** （`~/.copilot/agents/`）\n\n   > \\[!NOTE]\n   > 如果两个位置都有同名的 custom agents，则使用主目录中的那个，而非存储库中的。\n\n4. 选择是否让 Copilot 创建 custom agent 文件，或自行创建。\n\n**选项 1：使用 Copilot**\n\n输入要创建的代理的详细信息。 描述代理的专业知识以及何时应使用代理。\nCopilot 将采用你输入的说明，并使用它为你编写代理配置文件。\n\n例如，可以输入：\n\n```text\nI am a security expert. I check code files thoroughly for potential security issues. Use me whenever a security review/check/audit is requested for one or more code files, or when the word \"seccheck\" is used in a prompt in reference to code files.\n\nI will identify potential problems, such as code that:\n\n- Exposes secrets or credentials\n- Allows cross-site scripting\n- Allows SQL injection\n- Contains vulnerable dependencies\n- Allows authentication to be bypassed\n\nIf any problems are identified, create a single GitHub issue in this repository on GitHub.com with details of problems, giving full details of each issue, including, but not limited to, risk level and recommended fix.\n```\n\n生成完初始代理配置文件后 Copilot ，会显示以下选项：\n\n* 继续\n* 查看内容\n* 重试\n* 退出\n\n如果选择查看内容，则会在默认编辑器中打开代理文件。 在 CLI 中继续执行代理创建过程之前，可以根据需要查看和进行更改。\n\n若要完成创建过程，请选择 **“继续**”。\n\n**选项 2：手动创建代理配置文件**\n\n选择自行创建代理文件时，将引导你完成一系列提示，以填写创建代理配置文件所需的信息。\n\n1. 输入代理的名称。 输入的名称是列出可用代理时显示的名称。 此版本的版本将用作代理文件的名称，例如，如果输入“安全专家”，代理文件将命名 `security-expert.agent.md`。\n\n   > \\[!TIP]\n   > 为了便于在以编程方式使用 custom agent 时使用，建议选择一个名称，只包含小写字母和连字符。\n\n2. 输入说明，说明此代理具有哪些专业知识，以及何时应使用它。\n\n3. 输入代理的操作说明，包括任何特定的准则、应执行的操作或应遵循的约束。\n\n4. 选择你 custom agent 应有权访问的工具。\n\n   默认情况下， custom agents 有权访问所有工具。 如果限制代理的访问，代理文件中将添加一个`tools`规范。\n\n5. 请重启 CLI，以加载新的 custom agent。\n\n## 使用 custom agent\n\nCustom agents 可通过以下方式使用：\n\n* **斜杠命令**\n\n  以交互模式输入 `/agent` ，然后从可用 custom agents列表中进行选择。 然后输入一个提示，将其传递给所选代理。\n\n  > \\[!NOTE]\n  > 此列表中不包括 CLI 的默认代理。 有关默认代理的详细信息，请参阅 [关于自定义代理](/zh/copilot/concepts/agents/copilot-cli/about-custom-agents#built-in-agents)。\n\n* **显式指令**\n\n  告知 Copilot 使用特定代理。 例如：\n\n  ```copilot\n  Use the security-auditor agent on all files in the /src/app directory\n  ```\n\n* **按推理**\n\n  使用提示符根据智能体文件中的描述触发特定智能体的使用。 例如：\n\n  ```copilot\n  Check all TypeScript files in or under the src directory for potential security problems\n  ```\n\n  或 （其中“seccheck”定义为代理配置文件中的触发词）：\n\n  ```copilot\n  seccheck /src/app/validator.go\n  ```\n\nCopilot 将自动推断要使用的代理。\n\n* **通过编程**\n\n  通过命令行选项指定要使用的 custom agent。 例如：\n\n  ```shell\n  copilot --agent security-auditor --prompt \"Check /src/app/validator.go\"\n  ```\n\n  其中 `security-auditor` 是 custom agent 配置文件的文件名，不含 `.agent.md` 扩展名。 通常（但不一定）这与 `name` 代理配置文件中的值相同。\n\n## 延伸阅读\n\n* [比较 GitHub Copilot CLI 自定义功能](/zh/copilot/concepts/agents/copilot-cli/comparing-cli-features)\n* [自定义代理配置](/zh/copilot/reference/custom-agents-configuration)\n* [GitHub Copilot CLI 命令参考](/zh/copilot/reference/copilot-cli-reference/cli-command-reference#custom-agents-reference)\n* [自定义智能体](/zh/copilot/tutorials/customization-library/custom-agents) — 精选的示例集合"}