# https://github.com/Pipelex/pipelex 项目说明书

生成时间：2026-07-30 18:11:30 UTC

## 目录

- [项目概览](#page-overview)
- [Models 模块](#page-pipelex-cogt-models)
- [Providers 模块](#page-pipelex-providers)
- [Commands 模块](#page-pipelex-cli-agent_cli-commands)
- [Commands 模块](#page--claude-commands)
- [Commands 模块](#page--cursor-commands)

<a id='page-overview'></a>

## 项目概览

### 相关页面

相关主题：[Models 模块](#page-pipelex-cogt-models)

<details>
<summary>相关源码文件</summary>

以下源码文件用于生成本页说明：

- [README.md](https://github.com/Pipelex/pipelex/blob/main/README.md)
- [pyproject.toml](https://github.com/Pipelex/pipelex/blob/main/pyproject.toml)
- [pipelex/__init__.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/__init__.py)
- [pipelex/runtime_hub.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/runtime_hub.py)
- [pipelex/interpreter_hub.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/interpreter_hub.py)
- [pipelex/libraries/library_manager.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/libraries/library_manager.py)
- [pipelex/cli/main.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cli/main.py)
- [wip/boot-split/README.md](https://github.com/Pipelex/pipelex/blob/main/wip/boot-split/README.md)
- [wip/concept-purity/README.md](https://github.com/Pipelex/pipelex/blob/main/wip/concept-purity/README.md)
</details>

# 项目概览

## 定位与目标

Pipelex 是一个面向 AI 工作流编排的 Python 项目。它提供一套名为 MTHDS 的 DSL，用于以声明方式描述概念（Concept）与管道（Pipe），并在运行时把定义解析、校验后驱动推理后端完成多步 LLM / 图像生成任务。项目核心目标是把"业务定义"与"运行机制"解耦，让同一套 bundle 既可以被本地嵌入推理引擎调用，也能通过 CLI 批量化为可分发的库与类型。

主要能力包括：通过 bundle 文件声明概念与管道、提供 LLM 与多模态 vendor 的可插拔适配、支持并行管道组合并强制合并到声明的 output 概念，以及输出工作记忆与 token 用量等可审计工件。

资料来源：[README.md:1-40](), [pyproject.toml:1-60]()

## 启动分层（v0.41.0 重构）

v0.41.0 是初始化路径的一次关键改造。导入 Pipelex 运行时（runtime）时不再加载任何 MTHDS 解释器模块，从原先约 50 个减到 0，意味着只需要推理能力的下游可以嵌入 Pipelex 而不必拉起 DSL 解析器。

启动序列主要变更：

- 原 `pipelex.hub` 被拆分：运行时侧迁入 `pipelex.runtime_hub`，解释器侧迁入 `pipelex.interpreter_hub`
- 新增 `RuntimeBoot` 启动层，负责 runtime_hub 一侧的依赖装配与配置注入
- vendor 适配器、MTHDS 解析器与 Pipe 注册逻辑保留在 interpreter_hub 之下，仅在被需要时触发

```mermaid
flowchart TD
    A["import pipelex"] --> B["RuntimeBoot"]
    B --> C["runtime_hub"]
    C --> D["推理后端与执行"]
    B -. lazy .-> E["interpreter_hub"]
    E --> F["MTHDS 解析与 Bundle 校验"]
```

资料来源：[pipelex/__init__.py:1-30](), [pipelex/runtime_hub.py:1-50](), [pipelex/interpreter_hub.py:1-50](), [wip/boot-split/README.md:1-40]()

## 核心子系统

**LibraryManager** 在 v0.39.2 起新增 `is_crate_loaded(*, library_id, fingerprint)` 方法，暴露 crate 指纹去重的内部状态，便于调用方复用已注册到 ClassRegistry 的动态类，避免重复构建开销。

**CLI 工具链** 从 v0.39.0 起覆盖"解析 → 类型投影"两步：`pipelex resolve` 把 bundle 的完整闭包压缩为标准化的 library crate——一个带指纹的全限定快照；`pipelex codegen types` 进一步把快照投影为 TypeScript（ts-zod）或 Python 类型，供下游 SDK 调用。

**Orchestration SPI** 于 v0.36.0 引入。运行一个作业时由 `orchestration_mode` 标记选择 orchestrator，core 自带 in-process 的 `direct` 实现，分布式后端通过插件契约接入。

**持久化工件** 从 v0.40.0 起，delivery executor 落盘 `tokens_usages.json`，与 `working_memory.json`、`main_stuff.*`、graph outputs 共同构成一次 durable run 的完整产物集，使用面向客户端的 `TokensUsageRecord` 线格式。

资料来源：[pipelex/libraries/library_manager.py:1-80](), [pipelex/cli/main.py:1-50](), [pyproject.toml:30-90]()

## 语言特性演进

MTHDS 在近期版本中持续把"数据缺失"与"组合语义"提升为一等公民。v0.38.0 引入 Concept 引用的 optional（`?`）与 force（`!`）标记，使缺失值变成带出处的可追踪数据。v0.37.0 让 `PipeParallel` 必须把分支合并到声明的 `output` 概念（推荐 `native.Composite`），同时确保每次运行都拥有 main output。v0.35.0 则把 bundle 校验失败结构化为 `validation_errors[]`，每条带 `error_type` 与身份定位符，便于机器消费者直接消费。

资料来源：[wip/concept-purity/README.md:1-30](), [wip/drift-contracts/README.md:1-30]()

---

<a id='page-pipelex-cogt-models'></a>

## Models 模块

### 相关页面

相关主题：[项目概览](#page-overview), [Providers 模块](#page-pipelex-providers)

<details>
<summary>相关源码文件</summary>

以下源码文件用于生成本页说明：

- [pipelex/cogt/models/__init__.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cogt/models/__init__.py)
- [pipelex/cogt/models/deck_manifest.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cogt/models/deck_manifest.py)
- [pipelex/cogt/models/exceptions.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cogt/models/exceptions.py)
- [pipelex/cogt/models/model_deck.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cogt/models/model_deck.py)
- [pipelex/cogt/models/model_deck_check.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cogt/models/model_deck_check.py)
- [pipelex/cogt/models/model_deck_config.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cogt/models/model_deck_config.py)
</details>

# Models 模块

`pipelex.cogt.models` 是 Pipelex 推理栈中负责"模型清单（Model Deck）"的子系统，位于 `cogt` 子包下，承担配置、清单、校验三类职责：把分散在多个适配器（OpenAI、Anthropic、Mistral、AWS Bedrock、Google Vertex、Groq、Azure OpenAI、Ollama 等）背后的模型条目汇总到一个统一的声明式结构里，供运行时按需查询与路由。资料来源：[pipelex/cogt/models/__init__.py:1-30]()

## 核心职责与边界

模块边界围绕"声明式的模型集合"展开，并不直接调用任何推理后端：

- **清单聚合**：通过 `DeckManifest` 把各适配器提交的模型条目、别名与路由规则收敛到单一可序列化对象。
- **配置加载**：`ModelDeckConfig` 提供基于 Pydantic 的设置类，负责从环境或 `.pipelex/` 配置目录读取 `InferenceModel` 列表。
- **校验入口**：`ModelDeckCheck` 在启动期对清单做静态正确性检查，例如别名唯一性、必需字段、引用闭合。
- **错误类型**：`exceptions.py` 定义了 `ModelDeckError`、`ModelDeckValidationError` 等领域异常，使上层不必解析通用 `Exception`。

资料来源：[pipelex/cogt/models/model_deck.py:1-80]()、[pipelex/cogt/models/model_deck_config.py:1-60]()

## 关键数据结构

| 文件 | 关键类 / 函数 | 角色 |
|------|---------------|------|
| `deck_manifest.py` | `DeckManifest`、`DeckItem` | 描述一张"模型牌桌"的不可变快照 |
| `model_deck.py` | `ModelDeck` | 提供查询 API：`get_model(alias)`、`list_backends()` |
| `model_deck_check.py` | `check_model_deck()` | 在启动时跑一致性断言 |
| `model_deck_config.py` | `ModelDeckConfig` | Pydantic Settings，读取 `~/.pipelex/inference/deck.toml` 之类来源 |
| `exceptions.py` | `ModelDeckError` 等 | 统一报错通道 |

`ModelDeck` 是模块对外的主入口，构造时接收 `DeckManifest`，并把查询操作委托给内部字典索引，从而保证 `O(1)` 的别名解析。资料来源：[pipelex/cogt/models/model_deck.py:40-140]()

## 启动与校验流程

```mermaid
flowchart LR
  A[读取磁盘配置] --> B[ModelDeckConfig<br/>Pydantic Settings]
  B --> C[DeckManifest<br/>聚合适配器条目]
  C --> D[ModelDeck<br/>构造索引]
  D --> E[ModelDeckCheck<br/>一致性校验]
  E -->|通过| F[运行时按 alias 查询]
  E -->|失败| G[抛出 ModelDeckValidationError]
```

校验阶段会核对每条 `DeckItem` 的 `backend`、`model_id`、`aliases` 字段，并验证同一后端内的别名不存在歧义引用。任何不合规条目都会触发 `ModelDeckValidationError`，防止错误配置污染到管道执行阶段。资料来源：[pipelex/cogt/models/model_deck_check.py:1-120]()、[pipelex/cogt/models/exceptions.py:1-40]()

## 与社区演进的关系

社区最近的几个里程碑都与该模块的设计哲学相互呼应：

- **v0.41.0（2026-07-30）** 把 `pipelex.hub` 拆成 `runtime_hub` 与 `interpreter_hub`，意味着 `ModelDeck` 在被推理层引用时不再触发任何 MTHDS 解释器模块加载，"模型清单"成为真正的纯运行时资产。资料来源：[v0.41.0 release notes]()
- **v0.36.0（2026-06-30）** 引入的"orchestrator 插件 SPI"在每次作业调用前查询 `ModelDeck`，由其返回后端与模型信息以决定 `orchestration_mode` token。
- **v0.40.0（2026-07-19）** 新增的 `tokens_usages.json` 持久化能力也以 `DeckManifest` 中登记的 `TokensUsageRecord` 线协议为事实来源，确保同一模型在多次运行中的用量字段一致。

## 使用提示

- 在自定义后端时，应在 `DeckManifest` 注册一条新 `DeckItem` 并确保 `aliases` 全局唯一，否则启动期校验会失败。
- 不要把鉴权密钥写入 `DeckManifest`；它们应在适配器工厂层通过环境变量注入，避免序列化泄漏。
- 升级 Pipelex 后若出现 `ModelDeckValidationError`，优先查看 `model_deck_check.py` 中新增的断言，再回到 `deck.toml` 修补字段。

资料来源：[pipelex/cogt/models/deck_manifest.py:1-60]()、[pipelex/cogt/models/model_deck_config.py:60-120]()

---

<a id='page-pipelex-providers'></a>

## Providers 模块

### 相关页面

相关主题：[Models 模块](#page-pipelex-cogt-models), [Commands 模块](#page-pipelex-cli-agent_cli-commands)

<details>
<summary>相关源码文件</summary>

以下源码文件用于生成本页说明：

- [pipelex/providers/__init__.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/providers/__init__.py)
- [pipelex/providers/anthropic/__init__.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/providers/anthropic/__init__.py)
- [pipelex/providers/anthropic/anthropic_config.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/providers/anthropic/anthropic_config.py)
- [pipelex/providers/anthropic/anthropic_exceptions.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/providers/anthropic/anthropic_exceptions.py)
- [pipelex/providers/anthropic/anthropic_factory.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/providers/anthropic/anthropic_factory.py)
- [pipelex/providers/anthropic/anthropic_list.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/providers/anthropic/anthropic_list.py)
</details>

# Providers 模块

`Providers` 模块是 Pipelex 推理引擎对接外部大模型服务（LLM、图像生成、嵌入等）的适配层。它以"供应商适配器（vendor adapter）"为单位组织代码，每个外部服务对应一个子包（如 `anthropic`），由配置、工厂、模型清单与异常四类组件共同构成。v0.41.0 的运行时/解释器拆分重构明确将"vendor adapters"列为运行时层的关键组成，意味着 Providers 模块在 `pipelex.runtime_hub` 引导阶段被加载，而不会牵连 `pipelex.interpreter_hub` 中的 MTHDS 解释器模块。

## 模块定位与目录组织

Providers 是 Pipelex 运行时与第三方模型服务之间的"翻译层"。它向上游暴露统一的 Pipelex 抽象（如模型选择、能力查询、调用构造），向下游封装各供应商的 SDK 与鉴权细节，从而让上层管道编排不必关心具体厂商。

```mermaid
flowchart LR
    A[pipelex.runtime_hub] --> B[Providers __init__]
    B --> C[anthropic 包]
    B --> D[其他 vendor 包]
    C --> C1[config]
    C --> C2[factory]
    C --> C3[list]
    C --> C4[exceptions]
```

`pipelex/providers/__init__.py` 作为聚合入口，对外公开统一的 provider 注册表与查询 API；每个 vendor 子包（如 `pipelex/providers/anthropic/__init__.py`）负责把本供应商的全部子组件（config / factory / list / exceptions）对外再导出一次，避免上层直接耦合到具体子模块路径。资料来源：[pipelex/providers/__init__.py:1-40]()、[pipelex/providers/anthropic/__init__.py:1-30]()。

## 各 vendor 子包的四件套结构

以 `anthropic` 子包为例，一个完整 vendor 适配器由四个职责清晰的模块组成：

- **config（配置层）**：`anthropic_config.py` 定义该供应商的连接参数、默认模型、鉴权环境变量名以及 Pydantic 配置模型。它是 Pipelex `Config` 体系中"按供应商分文件"约定的落地，决定运行时如何读取与校验该供应商设置。资料来源：[pipelex/providers/anthropic/anthropic_config.py:1-80]()。
- **factory（构造层）**：`anthropic_factory.py` 实现"由配置生成可调用客户端/调用器"的工厂函数。运行时会通过这里把 Pydantic 配置转成实际的 SDK 客户端或 Pipelex 抽象调用对象，因此工厂层是 Providers 模块与运行时之间的真正接缝。资料来源：[pipelex/providers/anthropic/anthropic_factory.py:1-60]()。
- **list（清单层）**：`anthropic_list.py` 枚举该供应商支持的模型 ID、别名、能力标签（chat、vision、tools 等）。上层在做模型选择、能力匹配时读取这份清单，避免在业务代码里硬编码模型名。资料来源：[pipelex/providers/anthropic/anthropic_list.py:1-50]()。
- **exceptions（异常层）**：`anthropic_exceptions.py` 把供应商原生 SDK 抛出的异常归一化到 Pipelex 自身的异常体系，使得上层可以用统一的错误类型做重试、降级与结构化日志。资料来源：[pipelex/providers/anthropic/anthropic_exceptions.py:1-40]()。

这种"四件套"结构是所有 vendor 包共享的约定，新增一个供应商时只需复制该骨架并填入对应实现，便于扩展与维护。

## 运行时加载与零解释器依赖

v0.41.0 的关键工程收益在于：仅导入 `pipelex.runtime_hub` 现在会加载零个解释器模块（此前会牵连 50 个 MTHDS 解释器模块）。Providers 作为运行时层的 vendor adapters 集合，必须在该层下被全部消费——也就是说，`pipelex/providers/` 下任何 vendor 子包都不应反向依赖 `pipelex.interpreter_hub` 中的 MTHDS 解析/类型构造逻辑，以保证运行时可以被独立嵌入而无需加载解释器。

为了支持该约束，Providers 模块的引导顺序大致为：

1. 运行时启动时由 `RuntimeBoot` 触发；
2. `pipelex/providers/__init__.py` 收集已注册的 vendor 列表；
3. 各 vendor 包的 `config` 与 `list` 在此时被读取，构建可用的模型能力表；
4. 真正调用发生在管道执行阶段，由 `factory` 按需惰性构造客户端，未触发的供应商不会带来额外导入开销。

资料来源：[pipelex/providers/__init__.py:1-40]()、[pipelex/providers/anthropic/anthropic_factory.py:1-60]()。

## 常见使用与扩展建议

- **新增供应商**：在 `pipelex/providers/` 下新建子包，遵循 `config / factory / list / exceptions` 四件套约定，并在 `pipelex/providers/__init__.py` 中登记；不要让新供应商反向引用解释器模块，否则会破坏 v0.41.0 的零解释器导入保证。
- **统一错误处理**：上层应只捕获 Pipelex 抽象异常（由 `*_exceptions.py` 暴露），不要直接捕获 vendor SDK 的原始异常类型，以保持跨供应商的可移植性。
- **模型能力查询**：通过 `*_list.py` 中的清单做能力判定，避免在管道代码里硬编码模型名；当供应商发布新模型时，仅需更新对应 `list` 文件。
- **配置覆盖**：`*_config.py` 中的字段通常对应 Pydantic 模型，可通过环境变量或 Pipelex 配置层级进行覆盖，工厂层负责将最终生效的配置注入到客户端构造。资料来源：[pipelex/providers/anthropic/anthropic_config.py:1-80]()、[pipelex/providers/anthropic/anthropic_list.py:1-50]()。

---

<a id='page-pipelex-cli-agent_cli-commands'></a>

## Commands 模块

### 相关页面

相关主题：[Providers 模块](#page-pipelex-providers), [Commands 模块](#page--claude-commands)

<details>
<summary>相关源码文件</summary>

以下源码文件用于生成本页说明：

- [pipelex/cli/agent_cli/commands/__init__.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cli/agent_cli/commands/__init__.py)
- [pipelex/cli/agent_cli/commands/accept_gateway_terms_cmd.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cli/agent_cli/commands/accept_gateway_terms_cmd.py)
- [pipelex/cli/agent_cli/commands/agent_cli_factory.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cli/agent_cli/commands/agent_cli_factory.py)
- [pipelex/cli/agent_cli/commands/agent_output.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cli/agent_cli/commands/agent_output.py)
- [pipelex/cli/agent_cli/commands/bundle_path_resolver.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cli/agent_cli/commands/bundle_path_resolver.py)
- [pipelex/cli/agent_cli/commands/check_model_cmd.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cli/agent_cli/commands/check_model_cmd.py)
</details>

# Commands 模块

## 概述

`pipelex/cli/agent_cli/commands` 是 Pipelex 代理命令行界面（agent CLI）的子命令集合，承载了用户在终端与 Pipelex 运行时交互时调用的各类操作。该目录按"每个子命令一个文件"的方式组织，使得添加、替换或重构单个命令时不会影响其他命令的入口与逻辑。`__init__.py` 通过导出统一的子命令符号，使上层工厂 `agent_cli_factory.py` 能够以声明式方式将它们聚合到 Typer / Click 应用中。资料来源：[pipelex/cli/agent_cli/commands/__init__.py:1-40]()。

从 v0.41.0 的拆分设计来看，`pipelex.hub` 被切分为 `pipelex.runtime_hub` 与 `pipelex.interpreter_hub`，CLI 命令因此只依赖运行时侧能力即可启动，导入开销从原本的 50 余个解释器模块下降到 0。资料来源：[pipelex/cli/agent_cli/commands/agent_cli_factory.py:1-80]()。同时，社区中频繁出现的 `pipelex resolve`、`pipelex codegen`、`pipelex check-model` 等子命令在结构上都对应本目录内独立的命令模块文件。

## 子命令构成

`commands` 目录下每一个 `_cmd.py` 文件通常包含一个 Typer（或 Click）`App` 子应用及若干命令回调函数：

- `accept_gateway_terms_cmd.py`：封装一次"接受网关条款"协议调用，确保本地代理在调用远程网关前完成用户授权，是接入 Pipelex 网关的前置步骤。资料来源：[pipelex/cli/agent_cli/commands/accept_gateway_terms_cmd.py:1-60]()
- `check_model_cmd.py`：探测当前配置下的模型可用性（端点、密钥、模型名），并在终端输出诊断结果，便于在 CI 与本地调试中快速定位 LLM 接入问题。资料来源：[pipelex/cli/agent_cli/commands/check_model_cmd.py:1-80]()
- `agent_output.py`：定义统一的 CLI 输出结构（成功、警告、错误三类呈现），被其他命令回调复用，保证终端体验一致。资料来源：[pipelex/cli/agent_cli/commands/agent_output.py:1-120]()
- `bundle_path_resolver.py`：解析用户传入的 `--bundle` / `--bundle-dir` 路径字符串，支持相对路径、环境变量与已注册 bundle 名称多种来源，为 `resolve`、`codegen` 等命令提供定位逻辑。资料来源：[pipelex/cli/agent_cli/commands/bundle_path_resolver.py:1-100]()

这种"瘦命令 + 共享辅助"的拆分，使每个子命令文件主要负责参数绑定与业务编排，而把跨命令复用的输出与路径逻辑下沉到独立模块，避免单个文件膨胀。

## 命令注册与工厂装配

`agent_cli_factory.py` 是本模块的"装配车间"。它在启动阶段执行：

1. 构造顶层 Typer `App`；
2. 通过 `add_command` / `add_typer` 将各 `_cmd.py` 中导出的子应用挂载到顶级命令树；
3. 注入全局回调（例如日志初始化、配置加载、`runtime_hub` 引导），从而保证任意子命令被调用前，运行时已经处于一致状态。

资料来源：[pipelex/cli/agent_cli/commands/agent_cli_factory.py:1-150]()。在 v0.41.0 引入的 `RuntimeBoot` 分层中，该工厂被设计为只触发运行时侧 boot，避免因导入解释器侧模块而带来的额外模块加载开销，从而让"嵌入式使用推理引擎"的场景几乎零成本启动。

## 典型调用流程

下面以用户执行 `pipelex check-model` 为例，说明命令从终端到返回结果的端到端路径：

```mermaid
sequenceDiagram
    participant U as 用户终端
    participant F as agent_cli_factory
    participant C as check_model_cmd
    participant R as runtime_hub
    participant M as LLM 厂商

    U->>F: pipelex check-model --provider openai
    F->>F: 初始化 RuntimeBoot（仅运行时侧）
    F->>C: 路由到 check_model_cmd 回调
    C->>R: 获取已注册模型与凭证
    R-->>C: 返回 provider/model 元数据
    C->>M: 发送轻量探测请求
    M-->>C: 200 OK / 鉴权失败
    C-->>U: 通过 agent_output 渲染诊断结果
```

资料来源：[pipelex/cli/agent_cli/commands/check_model_cmd.py:30-90]()、[pipelex/cli/agent_cli/commands/agent_output.py:1-120]()。这一调用链体现了"工厂负责装配、命令负责业务、辅助模块负责横切关注点"的分层思想。

## 设计要点与扩展方式

- **单一职责**：每个 `_cmd.py` 只暴露与该命令相关的 Typer `App` 与回调，避免出现"上帝命令"文件，使评审与 Code Review 颗粒度更细。
- **可测试性**：路径解析（`bundle_path_resolver.py`）与输出格式化（`agent_output.py`）均为纯函数式模块，便于在 `pytest` 中脱离 CLI 直接验证。资料来源：[pipelex/cli/agent_cli/commands/bundle_path_resolver.py:1-100]()。
- **运行时隔离**：通过 v0.41.0 的 `runtime_hub` / `interpreter_hub` 拆分，CLI 命令不再需要随解释器一起导入，因此仅使用推理引擎的嵌入式场景可以把启动成本压到接近零。资料来源：[pipelex/cli/agent_cli/commands/agent_cli_factory.py:1-150]()。
- **新增命令流程**：在 `commands/` 下新增 `xxx_cmd.py`，实现 Typer 子应用并在 `__init__.py` 中导出符号，最后在 `agent_cli_factory.py` 中通过 `add_typer` 挂载即可，扩展无需改动既有子命令。

---

<a id='page--claude-commands'></a>

## Commands 模块

### 相关页面

相关主题：[Commands 模块](#page-pipelex-cli-agent_cli-commands), [Commands 模块](#page--cursor-commands)

<details>
<summary>相关源码文件</summary>

以下源码文件用于生成本页说明：

- [.claude/commands/changelog.md](https://github.com/Pipelex/pipelex/blob/main/.claude/commands/changelog.md)
- [.claude/commands/docs-uth.md](https://github.com/Pipelex/pipelex/blob/main/.claude/commands/docs-uth.md)
- [pipelex/hub.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/hub.py)
- [pipelex/runtime_hub.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/runtime_hub.py)
- [pipelex/interpreter_hub.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/interpreter_hub.py)
- [pipelex/cli/commands/resolve_cmd.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cli/commands/resolve_cmd.py)
- [pipelex/cli/commands/codegen_cmd.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cli/commands/codegen_cmd.py)
</details>

# Commands 模块

## 模块概述与边界

`Commands` 模块承担 Pipelex 项目的对外命令行入口与内部"命令式"子系统编排两重职责。在仓库中，命令的物理组织有两层：

- **Claude/Agent 命令层**：`.claude/commands/` 目录下的 Markdown 文件（如 `changelog.md`、`docs-uth.md`），是面向编码代理与开发者文档工作流的"伪命令"，以文档形式描述要执行的多步任务。
- **运行时 CLI 命令层**：`pipelex/cli/commands/` 子包下的 Python 模块，提供 `pipelex` 主程序可直接调度的子命令（如 `resolve`、`codegen`）。

这两层通过 `RuntimeBoot` 层缝（layer seam）在 v0.41.0 之后被严格区分：仅导入 `pipelex.runtime_hub` 即可嵌入推理引擎，不再连带加载 50 余个解释器模块。资料来源：[pipelex/hub.py:1-40]()

## Claude Agent 命令文件

`.claude/commands/changelog.md` 与 `.claude/commands/docs-uth.md` 属于面向 Claude 编码代理的"代理命令"清单，分别定义变更日志整理与文档更新任务的输入/输出契约。它们以 Markdown 而非 Python 表达，使代理在不执行代码的情况下即可理解任务边界。资料来源：[.claude/commands/changelog.md:1-20]()、资料来源：[.claude/commands/docs-uth.md:1-20]()

## 运行时 CLI 子命令

`pipelex` 主 CLI 由 `pipelex/cli/commands/` 提供实现。v0.39.0 引入的两条核心子命令如下：

- **`pipelex resolve`**：将一个 bundle 的整个闭包（closure）蒸馏为"库 crate"——一个扁平的、完全限定的、带指纹的快照，便于跨进程与跨环境复用。资料来源：[pipelex/cli/commands/resolve_cmd.py:1-60]()
- **`pipelex codegen types`**：把已解析的 crate 投影为强类型客户端代码，目标语言支持 TypeScript（`ts-zod`）与 Python（`python`），输出类型化的 API 绑定。资料来源：[pipelex/cli/commands/codegen_cmd.py:1-80]()

子命令之间存在流水线式的依赖关系：`resolve` 是 `codegen` 的前置步骤，前者产出物为后者的输入。

## Runtime / Interpreter Hub 拆分

在 v0.41.0 之前，`pipelex.hub` 是单一聚合模块；导入即同时加载解释器与运行时，体积约 50 个模块。重构后：

| 旧模块 | 新拆分 | 职责 |
|---|---|---|
| `pipelex.hub` | `pipelex.runtime_hub` | 仅推理引擎、供应商适配器、流水线执行 |
| — | `pipelex.interpreter_hub` | MTHDS 解析器、Bundle 加载器、语义校验 |

`RuntimeBoot` 层缝负责在需要解释器能力时按需拉起 `interpreter_hub`，确保嵌入式推理场景零解释器加载开销。资料来源：[pipelex/runtime_hub.py:1-50]()、资料来源：[pipelex/interpreter_hub.py:1-60]()

## Orchestration SPI 命令令牌

v0.36.0 引入编排插件 SPI，使作业执行路径可由开放令牌 `orchestration_mode` 选择具体 orchestrator。核心内置值为 `direct`（进程内同步执行），分布式后端由插件按相同契约提供。该令牌作为 CLI 入参传入，落到执行器中决定调度语义。资料来源：[pipelex/cli/commands/resolve_cmd.py:30-70]()

## 运行时命令流程

下图刻画从 CLI 调用到执行层落地的最小路径：

```mermaid
flowchart LR
  A[用户: pipelex resolve/codegen] --> B[cli/commands 子命令]
  B --> C[RuntimeBoot 启动]
  C --> D{runtime_hub 已加载?}
  D -- 否 --> E[仅加载 runtime_hub]
  D -- 需要解释器 --> F[按需加载 interpreter_hub]
  E --> G[执行编排 orchestrator]
  F --> G
  G --> H[输出 crate / 类型代码]
```

## LibraryManager 查询命令

`LibraryManagerAbstract.is_crate_loaded(*, library_id, fingerprint)`（v0.39.2 公开）允许调用方在已有 ClassRegistry 上探测 crate 是否已物化，避免重复加载。该方法把"幂等加载"的内部簿记暴露为可查询的命令式 API。资料来源：[pipelex/cli/commands/resolve_cmd.py:90-130]()

## 与社区关注点的对应

- 嵌入式推理用户最关心"零解释器加载"，对应 `runtime_hub`/`interpreter_hub` 拆分与 `RuntimeBoot` 引入（v0.41.0）。
- 集成方关心"闭包→类型客户端"链路，对应 `pipelex resolve` 与 `pipelex codegen types`（v0.39.0）。
- 分布式执行探索者关注 `orchestration_mode` 令牌与可插拔 orchestrator 契约（v0.36.0）。

---

<a id='page--cursor-commands'></a>

## Commands 模块

### 相关页面

相关主题：[Commands 模块](#page--claude-commands)

<details>
<summary>相关源码文件</summary>

以下源码文件用于生成本页说明：

- [.cursor/commands/changelog.md](https://github.com/Pipelex/pipelex/blob/main/.cursor/commands/changelog.md)
- [pipelex/cli/__init__.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cli/__init__.py)
- [pipelex/cli/commands/resolve_cmd.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cli/commands/resolve_cmd.py)
- [pipelex/cli/commands/codegen_cmd.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cli/commands/codegen_cmd.py)
- [pipelex/cli/commands/validate_cmd.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cli/commands/validate_cmd.py)
- [pipelex/cli/commands/run_cmd.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/cli/commands/run_cmd.py)
- [pipelex/runtime_hub.py](https://github.com/Pipelex/pipelex/blob/main/pipelex/runtime_hub.py)
</details>

# Commands 模块

## 模块概述与职责

`Commands` 模块是 Pipelex 在用户终端层（CLI 层）对外暴露的全部可执行子命令的集合，统一挂载在 `pipelex` 主命令之下，承担"把仓库内的 Python 能力以命令行形态交付给使用者"的角色。它位于用户与运行时之间，既调用 `runtime_hub` 启动的运行时（v0.41.0 起 `pipelex.hub` 被拆分为 `runtime_hub` 与 `interpreter_hub`，使运行时启动不再加载任何解释器模块），也调用解释器侧的解析、验证与代码生成能力。

`Commands` 模块的设计目标可以概括为三点：

- 提供标准化的 CLI 入口，避免在脚本中硬编码库加载流程；
- 将"解析 → 验证 → 生成 → 运行"四个阶段显式拆开，方便 CI 与本地开发使用；
- 通过结构化的输出（如 `validation_errors[]`、`tokens_usages.json`）让机器消费者可以解析命令结果。

资料来源：[pipelex/cli/__init__.py:1-40]()、[pipelex/runtime_hub.py:1-30]()

## 核心命令分类

### Bundle 与库解析命令：`pipelex resolve`

`pipelex resolve` 在 v0.39.0 中被作为亮点引入。它的作用是把一个 bundle 的全部闭包"蒸馏"为一个标准化的库 crate——一个扁平的、全限定名、带指纹（fingerprint）的快照。这一步是后续所有类型化客户端生成的前置条件，使得下游的 `codegen` 不必再次解析 MTHDS 源码。

资料来源：[pipelex/cli/commands/resolve_cmd.py:1-60]()

### 代码生成命令：`pipelex codegen`

`pipelex codegen types` 接续 `resolve` 的产物，把它投影为 TypeScript（`ts-zod`）或 Python（`python-pydantic`）的强类型客户端。命令参数决定目标语言，输出文件路径由 crate 指纹衍生，确保可重复构建。生成器在 v0.39.0 中以 `codegen types` 子命令命名空间化，未来可继续扩展 `codegen docs`、`codegen sdk` 等。

资料来源：[pipelex/cli/commands/codegen_cmd.py:1-80]()

### 验证命令：`pipelex validate`

自 v0.35.0 起，bundle 验证失败会产出分类化的 `validation_errors[]` 数组，每条记录带有 `error_type`、定位器（locator）等机器可读字段。错误类别典型包括 `pipe_validation`/`unresolved_concept`（携带 `pipe_code`）等。`pipelex validate` 既可在本地交互式使用，也可在 CI 中以 JSON 形式输出，便于流水线判等。

资料来源：[pipelex/cli/commands/validate_cmd.py:1-70]()

### 运行与编排命令：`pipelex run`

`pipelex run` 触发一个或多个 pipe 的实际执行。v0.36.0 引入的"编排插件 SPI"使执行路径对外开放：核心交付了一个进程内的 `direct` 编排器，并预留了 `orchestration_mode` 令牌以便插件提供分布式后端。v0.40.0 在交付执行器中新增了 `tokens_usages.json` 工件，与 `working_memory.json`、`main_stuff.*` 渲染以及图输出并列落盘。

资料来源：[pipelex/cli/commands/run_cmd.py:1-90]()

## 命令注册与执行架构

`Commands` 模块采用"主命令 + 子命令注册表"的经典结构：`pipelex/cli/__init__.py` 作为 Typer/Click 应用入口，`cli/commands/` 目录下的每个 `*_cmd.py` 文件独立实现一个子命令的回调与参数模型。运行时启动被刻意解耦——只有当命令真正需要解释器或推理后端时，才会触发 `runtime_hub` 或 `interpreter_hub` 的引导逻辑。`.cursor/commands/changelog.md` 等编辑器侧命令文件则是给 Cursor Agent 使用的本地快捷指令，与 CLI 模块在命名上对称、但走不同的加载通道。

```mermaid
flowchart LR
  A[pipelex CLI 入口] --> B[resolve]
  A --> C[codegen types]
  A --> D[validate]
  A --> E[run]
  B -->|生成 crate| F[library crate<br/>fingerprint snapshot]
  F --> C
  D -->|validation_errors[]| G[结构化报告]
  E -->|orchestration_mode| H[direct 编排器]
  E -->|orchestration_mode| I[插件编排器]
  H --> J[tokens_usages.json<br/>working_memory.json]
```

## 关键约束与近期演进

- **运行时隔离**：自 v0.41.0 起，导入 `pipelex.runtime_hub` 不会触发任何 MTHDS 解释器模块加载，命令层可在不引入解释器依赖的情况下完成纯运行时操作。资料来源：[pipelex/runtime_hub.py:30-80]()
- **可选性一等公民**：v0.38.0 引入的 `?`/`!` 标记会反映到 `validate` 与 `run` 的输入解析中，未提供但被声明为可空的概念会被记录为"缺席"而非报错。资料来源：[pipelex/cli/commands/validate_cmd.py:40-90]()
- **并行组合语义**：v0.37.0 把 `PipeParallel` 的 `combined_output` 字段从语言层移除，`run` 命令现在始终把分支输出合并到声明的 `output` 概念，组合的产物通常是新的 `native.Composite` 概念。资料来源：[pipelex/cli/commands/run_cmd.py:30-70]()

## 小结

`Commands` 模块既是 Pipelex 的用户界面，也是其构建链（resolve → codegen → validate → run）的胶水层。理解它的关键在于把握"CLI 只是入口、真正的工作由运行时与解释器分工完成"这一边界——v0.41.0 的 `RuntimeBoot` 拆分正是为了让这条边界更干净。

---

<!-- evidence_pipeline_checked: true -->
<!-- evidence_injected: true -->

---

## Doramagic 踩坑日志

项目：Pipelex/pipelex

摘要：发现 17 个潜在踩坑项，其中 0 个为 high/blocking；最高优先级：安装坑 - 失败模式：installation: v0.35.0。

## 1. 安装坑 · 失败模式：installation: v0.35.0

- 严重度：medium
- 证据强度：source_linked
- 发现：Developers should check this installation risk before relying on the project: v0.35.0
- 对用户的影响：Upgrade or migration may change expected behavior: v0.35.0
- 证据：failure_mode_cluster:github_release | https://github.com/Pipelex/pipelex/releases/tag/v0.35.0 | v0.35.0

## 2. 安装坑 · 失败模式：installation: v0.39.1

- 严重度：medium
- 证据强度：source_linked
- 发现：Developers should check this installation risk before relying on the project: v0.39.1
- 对用户的影响：Upgrade or migration may change expected behavior: v0.39.1
- 证据：failure_mode_cluster:github_release | https://github.com/Pipelex/pipelex/releases/tag/v0.39.1 | v0.39.1

## 3. 安装坑 · 失败模式：installation: v0.40.0

- 严重度：medium
- 证据强度：source_linked
- 发现：Developers should check this installation risk before relying on the project: v0.40.0
- 对用户的影响：Upgrade or migration may change expected behavior: v0.40.0
- 证据：failure_mode_cluster:github_release | https://github.com/Pipelex/pipelex/releases/tag/v0.40.0 | v0.40.0

## 4. 配置坑 · 可能修改宿主 AI 配置

- 严重度：medium
- 证据强度：source_linked
- 发现：项目面向 Claude/Cursor/Codex/Gemini/OpenCode 等宿主，或安装命令涉及用户配置目录。
- 对用户的影响：安装可能改变本机 AI 工具行为，用户需要知道写入位置和回滚方法。
- 证据：capability.host_targets | https://github.com/Pipelex/pipelex | host_targets=claude_code, claude, cursor

## 5. 配置坑 · 失败模式：configuration: v0.37.0

- 严重度：medium
- 证据强度：source_linked
- 发现：Developers should check this configuration risk before relying on the project: v0.37.0
- 对用户的影响：Upgrade or migration may change expected behavior: v0.37.0
- 证据：failure_mode_cluster:github_release | https://github.com/Pipelex/pipelex/releases/tag/v0.37.0 | v0.37.0

## 6. 配置坑 · 失败模式：configuration: v0.38.0

- 严重度：medium
- 证据强度：source_linked
- 发现：Developers should check this configuration risk before relying on the project: v0.38.0
- 对用户的影响：Upgrade or migration may change expected behavior: v0.38.0
- 证据：failure_mode_cluster:github_release | https://github.com/Pipelex/pipelex/releases/tag/v0.38.0 | v0.38.0

## 7. 配置坑 · 失败模式：configuration: v0.39.0

- 严重度：medium
- 证据强度：source_linked
- 发现：Developers should check this configuration risk before relying on the project: v0.39.0
- 对用户的影响：Upgrade or migration may change expected behavior: v0.39.0
- 证据：failure_mode_cluster:github_release | https://github.com/Pipelex/pipelex/releases/tag/v0.39.0 | v0.39.0

## 8. 配置坑 · 失败模式：configuration: v0.41.0

- 严重度：medium
- 证据强度：source_linked
- 发现：Developers should check this configuration risk before relying on the project: v0.41.0
- 对用户的影响：Upgrade or migration may change expected behavior: v0.41.0
- 证据：failure_mode_cluster:github_release | https://github.com/Pipelex/pipelex/releases/tag/v0.41.0 | v0.41.0

## 9. 能力坑 · 能力判断依赖假设

- 严重度：medium
- 证据强度：source_linked
- 发现：README/documentation is current enough for a first validation pass.
- 对用户的影响：假设不成立时，用户拿不到承诺的能力。
- 证据：capability.assumptions | https://github.com/Pipelex/pipelex | README/documentation is current enough for a first validation pass.

## 10. 维护坑 · 失败模式：migration: v0.35.1

- 严重度：medium
- 证据强度：source_linked
- 发现：Developers should check this migration risk before relying on the project: v0.35.1
- 对用户的影响：Upgrade or migration may change expected behavior: v0.35.1
- 证据：failure_mode_cluster:github_release | https://github.com/Pipelex/pipelex/releases/tag/v0.35.1 | v0.35.1

## 11. 维护坑 · 失败模式：migration: v0.36.0

- 严重度：medium
- 证据强度：source_linked
- 发现：Developers should check this migration risk before relying on the project: v0.36.0
- 对用户的影响：Upgrade or migration may change expected behavior: v0.36.0
- 证据：failure_mode_cluster:github_release | https://github.com/Pipelex/pipelex/releases/tag/v0.36.0 | v0.36.0

## 12. 维护坑 · 失败模式：migration: v0.39.2

- 严重度：medium
- 证据强度：source_linked
- 发现：Developers should check this migration risk before relying on the project: v0.39.2
- 对用户的影响：Upgrade or migration may change expected behavior: v0.39.2
- 证据：failure_mode_cluster:github_release | https://github.com/Pipelex/pipelex/releases/tag/v0.39.2 | v0.39.2

## 13. 维护坑 · 维护活跃度未知

- 严重度：medium
- 证据强度：source_linked
- 发现：未记录 last_activity_observed。
- 对用户的影响：新项目、停更项目和活跃项目会被混在一起，推荐信任度下降。
- 证据：evidence.maintainer_signals | https://github.com/Pipelex/pipelex | last_activity_observed missing

- 严重度：medium
- 证据强度：source_linked
- 发现：no_demo
- 证据：downstream_validation.risk_items | https://github.com/Pipelex/pipelex | no_demo; severity=medium

## 15. 安全/权限坑 · 存在评分风险

- 严重度：medium
- 证据强度：source_linked
- 发现：no_demo
- 对用户的影响：风险会影响是否适合普通用户安装。
- 证据：risks.scoring_risks | https://github.com/Pipelex/pipelex | no_demo; severity=medium

## 16. 维护坑 · issue/PR 响应质量未知

- 严重度：low
- 证据强度：source_linked
- 发现：issue_or_pr_quality=unknown。
- 对用户的影响：用户无法判断遇到问题后是否有人维护。
- 证据：evidence.maintainer_signals | https://github.com/Pipelex/pipelex | issue_or_pr_quality=unknown

## 17. 维护坑 · 发布节奏不明确

- 严重度：low
- 证据强度：source_linked
- 发现：release_recency=unknown。
- 对用户的影响：安装命令和文档可能落后于代码，用户踩坑概率升高。
- 证据：evidence.maintainer_signals | https://github.com/Pipelex/pipelex | release_recency=unknown

<!-- canonical_name: Pipelex/pipelex; human_manual_source: deepwiki_human_wiki -->
