> ## Documentation Index
> Fetch the complete documentation index at: https://agent-compass.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 导入自定义组件

无需修改 AgentCompass 源码，即可加载你自己编写的 Benchmark、Harness、Environment、Recipe 或 Analyzer。在配置中列出注册这些组件的 Python 文件、package 或 module，AgentCompass 会在查找组件 ID 之前导入它们。

## 开始之前

本页说明如何加载你已经写好的组件。如果还需要编写组件，请先阅读对应的开发者指南：

| 想添加的组件 | 参考文档 |
| - | - |
| Benchmark | [Benchmark 集成](https://agent-compass.mintlify.site/zh/developer_guide/extensions/benchmark/overview.md) |
| Harness | [Harness 集成](https://agent-compass.mintlify.site/zh/developer_guide/extensions/harness/overview.md) |
| Environment | [Environment 集成](https://agent-compass.mintlify.site/zh/developer_guide/extensions/environment/overview.md) |
| Recipe | [Recipe 集成](https://agent-compass.mintlify.site/zh/developer_guide/extensions/recipe_integration.md) |
| Analyzer | [Analyzer 集成](https://agent-compass.mintlify.site/zh/developer_guide/extensions/analyzer_integration.md) |

这些指南面向贡献者，其中一些步骤（例如把文件放在 `src/agentcompass/` 下）只适用于提交到仓库的组件。编写私有组件时，请遵循相同的组件契约（基类、注册装饰器和参数），把代码保存在自己的项目中，再按本页的步骤加载。

## 快速开始

先编写一个注册组件的 module。下面的示例注册了一个用于标记空最终答案的 Analyzer：

```python theme={"system"}
# components.py
from agentcompass.runtime import (
    ANALYZERS,
    AnalysisResult,
    AnalyzerCategory,
    BaseAnalyzer,
    RunResult,
)


@ANALYZERS.register()
class EmptyAnswerAnalyzer(BaseAnalyzer):
    id = "EmptyAnswerAnalyzer"
    description = "Flag tasks whose final answer is empty."
    category = AnalyzerCategory.BEHAVIOR
    datasets = []
    data_requirements = []

    async def analysis(self, task, prepared, result: RunResult, req, plan) -> AnalysisResult:
        _ = prepared, req, plan
        empty = not str(result.final_answer or "").strip()
        return AnalysisResult(task_id=task.task_id, is_badcase=empty, details={"empty_answer": empty})
```

然后在配置文件中声明这个文件。这里的相对路径以配置文件所在目录为基准：

```yaml theme={"system"}
# config.yaml
runtime:
  custom_imports:
    - ./components.py
```

确认 AgentCompass 能找到该组件：

```bash theme={"system"}
agentcompass list analyzer --config ./config.yaml
```

输出中会包含 `EmptyAnswerAnalyzer`。你也可以不使用配置文件，直接通过 `--custom-import ./components.py` 传入该文件。

## 选择导入什么

`custom_imports` 中的每一项都是普通字符串。AgentCompass 按下列顺序检查它，以决定如何加载：

| 如果该项是 | AgentCompass 将它作为 | 示例 |
| - | - | - |
| 已存在的文件 | 单个 Python 文件 | `./components.py` |
| 已存在的目录 | Python package；目录中必须包含 `__init__.py` | `./component_package` |
| 由合法 Python 标识符组成的点分名称 | 已安装的 module，等同于 `import company_eval.components` | `company_eval.components` |

其他值都会报错。

### 单个文件

适合小型、自包含的组件。AgentCompass 不会把该文件所在目录加入模块搜索路径，因此文件不能按名称导入同目录下的其他文件，`from . import helpers` 这类相对导入也不可用；标准库和已安装的包可以正常导入。如果组件需要多个文件，请改用 package。

### package 目录

组件由多个文件组成时使用 package。AgentCompass 只执行 package 的 `__init__.py`，不会扫描目录，因此请在其中导入每一个注册组件的 module：

```python theme={"system"}
# component_package/__init__.py
from .benchmark import CustomBenchmark
from .harness import CustomHarness
```

package 内部请使用相对导入，如上所示。通过目录名导入 package（例如 `import component_package`），得到的不是 AgentCompass 加载的那个 package。如果两个 package 需要互相引用，请把它们放在同一个上级 package 下，只导入上级 package。

### 已安装的 module

组件以已安装 Python 包的形式发布时（例如来自团队的私有包索引），使用 module 名。该 module 必须能在运行 AgentCompass 的 Python 环境中导入。

## 声明导入

导入可以声明在四个位置。按你运行 AgentCompass 的方式选用即可，也可以组合使用。

| 位置 | 写法 | 相对路径的基准目录 |
| - | - | - |
| 配置文件 | 在 `runtime.custom_imports` 下列出各项 | 该配置文件所在目录 |
| `launch` 的编排文件 | 在顶层 `runtime.custom_imports` 下列出各项 | 编排文件所在目录 |
| 命令行 | 重复使用 `--custom-import <entry>` | 当前工作目录 |
| Python SDK | 调用 `load_custom_imports()` 或传入 `custom_imports` | 当前工作目录 |

SDK 的用法见 [Python SDK](https://agent-compass.mintlify.site/zh/user_guide/using_agentcompass/python_api.md#导入自定义组件)。

配置文件的来源与其他设置相同：用户级 `config.yaml`、最近的项目级 `config.yaml`，以及每个 `--config` 文件。查找位置见[覆盖顺序](https://agent-compass.mintlify.site/zh/user_guide/using_agentcompass/cli/config.md#覆盖顺序)。

### 加载顺序

AgentCompass 按以下顺序加载导入：

1. 全部内置组件，因此自定义组件不能使用已被内置组件占用的 ID。
2. 配置文件中的导入：先用户级文件，再项目级文件，然后按命令行顺序依次处理每个 `--config` 文件。
3. 编排文件中的导入（仅 `launch`）。
4. `--custom-import` 传入的导入，按命令行顺序处理。

解析后指向同一文件、目录或 module 名的条目只执行一次。配置文件中的大多数列表设置会被高优先级文件整体替换，但 `custom_imports` 是例外：各层的条目会合并在一起。

如果导入失败或组件 ID 已被占用，命令会报错并停止，不会跳过该条目。

## 在命令中使用这些选项

以下命令都接受可重复的 `--config <path>` 和 `--custom-import <entry>` 选项：

* `run` 和 `launch`
* `summary` 和 `analysis`
* 所有 `list` 子命令，包括 `list dump`
* `config show` 和 `config docs`
* `env close`

例如，检查来自已安装 module 的 Benchmark 是否已注册：

```bash theme={"system"}
agentcompass list benchmark --config ./config.yaml --custom-import company_eval.components
```

`list` 和 `config docs` 只读取配置文件中的 `runtime.custom_imports`，忽略其中的其他设置。YAML 语法错误、无效的条目或导入失败仍会让命令停止。没有配置自定义导入时，这些命令只加载你所查询类型的内置组件。

## Recipe

在被导入的 module 中，用 `@RECIPES.register()` 装饰的 Recipe 类会进入每个加载了该导入的运行的 Recipe 注册表。随后，AgentCompass 会对其 `matches()` 返回 `True` 的每个任务应用它。若要限制可应用的 Recipe，请传入 `--recipe <id>`，它是一份允许列表。

如果只想在指定的运行中加载某个 Recipe，请改用 `--recipe-dir`。它接收在 `RECIPE_CLASSES` 中列出类的 package，不使用装饰器。详见 [Recipe](https://agent-compass.mintlify.site/zh/user_guide/other_features/recipes.md#可信外部-recipe)。

## 汇总或分析已有结果

每次运行都会把它用到的导入记录在 `run_info.json` 中，方便你确认组件的来源。`summary` 和 `analysis` 不会再次导入这些记录，只使用当前配置和命令行声明的导入，所以打开一个结果目录不会执行其中保存的任何代码。

如果结果用到了自定义组件，请在命令中重新声明：

```bash theme={"system"}
agentcompass summary ./results/example --config ./config.yaml --custom-import ./component_package
agentcompass analysis --input ./results/example --custom-import ./components.py
```

如果没有重新声明，命令会因为找不到组件而报错，`summary --dry-run` 也一样。

使用默认的 `analyzers: null` 时，`analysis` 只会考虑此刻已注册的 Analyzer。要使用自定义 Analyzer，请按上面的方式导入它；仅在 `analysis_params.analyzers` 中写出它的 ID，不会加载它的源代码。

## 只导入可信代码

导入会以你当前的权限执行该文件、package 或 module 中的 Python 代码。所有接受这些选项的命令都是如此，包括 `list`、`config docs` 和 `summary --dry-run`。请只添加你信任的来源，并留意共享配置文件中导入列表的变动。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.