# 为 GitHub Copilot CLI 创建和使用自定义智能体

为特定开发任务创建具有定制专业知识的专用代理。

## 介绍

Custom agents 使你能够为特定任务定制 Copilot 的专业知识。

当您提示 Copilot 执行任务时，如果 custom agents 确定代理的专业知识非常适合该任务，那么它可能会选择使用您的 Copilot。

由 custom agent 执行的工作通过一个子代理来完成，该子代理是一个临时代理，用于完成任务。 子代理有自己的上下文窗口，可以通过与主代理无关的信息来填充该窗口。 通过这种方式，尤其是对于较大的任务，可以将部分工作移交给custom agents，从而避免扰乱主代理的上下文窗口。 然后，主代理可以专注于更高级别的规划和协调。

有关详细信息，请参阅“[关于自定义代理](/zh/copilot/concepts/agents/copilot-cli/about-custom-agents)”。

## 创建 custom agent

每个 custom agent 文件都由扩展名为 `.agent.md` Markdown 文件定义。 可以自行创建这些文件，也可以从 CLI 中添加这些文件，如以下步骤中所述。

1. 在交互模式下，输入 `/agent`。

2. 从选项列表中选择 **“创建新代理** ”。

3. 选择在存储库或主目录中创建 custom agent 的选项：

   * **Project** （`.github/agents/`）

   * **用户** （`~/.copilot/agents/`）

   > \[!NOTE]
   > 如果两个位置都有同名的 custom agents，则使用主目录中的那个，而非存储库中的。

4. 选择是否让 Copilot 创建 custom agent 文件，或自行创建。

**选项 1：使用 Copilot**

输入要创建的代理的详细信息。 描述代理的专业知识以及何时应使用代理。
Copilot 将采用你输入的说明，并使用它为你编写代理配置文件。

例如，可以输入：

```text
I 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.

I will identify potential problems, such as code that:

- Exposes secrets or credentials
- Allows cross-site scripting
- Allows SQL injection
- Contains vulnerable dependencies
- Allows authentication to be bypassed

If 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.
```

生成完初始代理配置文件后 Copilot ，会显示以下选项：

* 继续
* 查看内容
* 重试
* 退出

如果选择查看内容，则会在默认编辑器中打开代理文件。 在 CLI 中继续执行代理创建过程之前，可以根据需要查看和进行更改。

若要完成创建过程，请选择 **“继续**”。

**选项 2：手动创建代理配置文件**

选择自行创建代理文件时，将引导你完成一系列提示，以填写创建代理配置文件所需的信息。

1. 输入代理的名称。 输入的名称是列出可用代理时显示的名称。 此版本的版本将用作代理文件的名称，例如，如果输入“安全专家”，代理文件将命名 `security-expert.agent.md`。

   > \[!TIP]
   > 为了便于在以编程方式使用 custom agent 时使用，建议选择一个名称，只包含小写字母和连字符。

2. 输入说明，说明此代理具有哪些专业知识，以及何时应使用它。

3. 输入代理的操作说明，包括任何特定的准则、应执行的操作或应遵循的约束。

4. 选择你 custom agent 应有权访问的工具。

   默认情况下， custom agents 有权访问所有工具。 如果限制代理的访问，代理文件中将添加一个`tools`规范。

5. 请重启 CLI，以加载新的 custom agent。

## 使用 custom agent

Custom agents 可通过以下方式使用：

* **斜杠命令**

  以交互模式输入 `/agent` ，然后从可用 custom agents列表中进行选择。 然后输入一个提示，将其传递给所选代理。

  > \[!NOTE]
  > 此列表中不包括 CLI 的默认代理。 有关默认代理的详细信息，请参阅 [关于自定义代理](/zh/copilot/concepts/agents/copilot-cli/about-custom-agents#built-in-agents)。

* **显式指令**

  告知 Copilot 使用特定代理。 例如：

  ```copilot
  Use the security-auditor agent on all files in the /src/app directory
  ```

* **按推理**

  使用提示符根据智能体文件中的描述触发特定智能体的使用。 例如：

  ```copilot
  Check all TypeScript files in or under the src directory for potential security problems
  ```

  或 （其中“seccheck”定义为代理配置文件中的触发词）：

  ```copilot
  seccheck /src/app/validator.go
  ```

Copilot 将自动推断要使用的代理。

* **通过编程**

  通过命令行选项指定要使用的 custom agent。 例如：

  ```shell
  copilot --agent security-auditor --prompt "Check /src/app/validator.go"
  ```

  其中 `security-auditor` 是 custom agent 配置文件的文件名，不含 `.agent.md` 扩展名。 通常（但不一定）这与 `name` 代理配置文件中的值相同。

## 延伸阅读

* [比较 GitHub Copilot CLI 自定义功能](/zh/copilot/concepts/agents/copilot-cli/comparing-cli-features)
* [自定义代理配置](/zh/copilot/reference/custom-agents-configuration)
* [GitHub Copilot CLI 命令参考](/zh/copilot/reference/copilot-cli-reference/cli-command-reference#custom-agents-reference)
* [自定义智能体](/zh/copilot/tutorials/customization-library/custom-agents) — 精选的示例集合