词元之母TOK.MOM - 平台充值汇率 1:1 即 1 人民币充值到账 1 美元,支持一个 Key 调用近 600+ 海内外模型,限时特价模型低至 1 折,欢迎上岸!
run_agent.py 中的 AIAgent 类 ——这是一个大型文件(15k+ 行),负责处理从 prompt(提示词)组装到工具分发再到 provider 故障转移的所有逻辑。AIAgent 负责:prompt_builder.py 组装有效的系统 prompt 和工具 schemachat_completions、codex_responses、anthropic_messages)chat() 是对 run_conversation() 的轻量封装,从结果 dict 中提取 final_response 字段。| API 模式 | 用途 | 客户端类型 |
|---|---|---|
chat_completions | 兼容 OpenAI 的端点(OpenRouter、自定义及大多数 provider) | openai.OpenAI |
codex_responses | OpenAI Codex / Responses API | openai.OpenAI(使用 Responses 格式) |
anthropic_messages | 原生 Anthropic Messages API | 通过适配器使用 anthropic.Anthropic |
role/content/tool_calls dict)。api_mode 构造函数参数(最高优先级)anthropic provider → anthropic_messages)api.anthropic.com → anthropic_messages)chat_completionsrun_conversation()
1. 若未提供则生成 task_id
2. 将用户消息追加到对话历史
3. 构建或复用已缓存的系统 prompt(prompt_builder.py)
4. 检查是否需要预检压缩(上下文超过 50%)
5. 从对话历史构建 API 消息
- chat_completions:直接使用 OpenAI 格式
- codex_responses:转换为 Responses API 输入项
- anthropic_messages:通过 anthropic_adapter.py 转换
6. 注入临时 prompt 层(预算警告、上下文压力提示)
7. 若使用 Anthropic,应用 prompt 缓存标记
8. 发起可中断的 API 调用(_interruptible_api_call)
9. 解析响应:
- 若有 tool_calls:执行工具,追加结果,回到步骤 5
- 若为文本响应:持久化 session,按需刷写内存,返回assistant_msg["reasoning"] 中,并可选择通过 reasoning_callback 展示。User → Assistant → User → Assistant → ...Assistant(含 tool_calls)→ Tool → Tool → ... → Assistanttool 角色可以连续出现(并行工具结果)_interruptible_api_call() 中,该方法在后台线程中执行实际的 HTTP 调用,同时监听中断事件:┌────────────────────────────────────────────────────┐
│ 主线程 API 线程 │
│ │
│ 等待: HTTP POST │
│ - 响应就绪 ───▶ 发送至 provider │
│ - 中断事件 │
│ - 超时 │
└────────────────────────────────────────────────────┘/stop 命令或信号)时:ThreadPoolExecutor 并发执行clarify)强制顺序执行for each tool_call in response.tool_calls:
1. 从 tools/registry.py 解析处理器
2. 触发 pre_tool_call 插件 hook
3. 检查是否为危险命令(tools/approval.py)
- 若危险:调用 approval_callback,等待用户确认
4. 使用参数 + task_id 执行处理器
5. 触发 post_tool_call 插件 hook
6. 将 {"role": "tool", "content": result} 追加到历史handle_function_call() 之前,由 run_agent.py 提前拦截:| 工具 | 拦截原因 |
|---|---|
todo | 读写 agent 本地任务状态 |
memory | 向持久化内存文件写入内容(有字符限制) |
session_search | 通过 agent 的 session DB 查询 session 历史 |
delegate_task | 以隔离上下文生成子 agent |
AIAgent 支持平台特定的回调,用于在 CLI、gateway 和 ACP 集成中实现实时进度展示:| 回调 | 触发时机 | 使用方 |
|---|---|---|
tool_progress_callback | 每次工具执行前后 | CLI spinner、gateway 进度消息 |
thinking_callback | 模型开始/停止思考时 | CLI "thinking..." 指示器 |
reasoning_callback | 模型返回推理内容时 | CLI 推理展示、gateway 推理块 |
clarify_callback | 调用 clarify 工具时 | CLI 输入提示、gateway 交互消息 |
step_callback | 每次完整 agent 轮次结束后 | Gateway 步骤追踪、ACP 进度 |
stream_delta_callback | 每个流式 token(启用时) | CLI 流式展示 |
tool_gen_callback | 从流中解析出工具调用时 | CLI spinner 中的工具预览 |
status_callback | 状态变更时(思考、执行等) | ACP 状态更新 |
IterationBudget 追踪迭代次数:agent.max_turns 配置)delegation.max_iterations(默认 50)——父 agent 与子 agent 的总迭代次数可超过父 agent 的上限fallback_providers 列表auxiliary.* 配置节进行配置。compression.protect_last_n,默认:20)hermes_state.py 使用 SQLite)MEMORY.md / USER.md/resume 或 hermes chat --resume 恢复 session| 文件 | 用途 |
|---|---|
run_agent.py | AIAgent 类——完整的 agent loop |
agent/prompt_builder.py | 从内存、技能、上下文文件和个性组装系统 prompt |
agent/context_engine.py | ContextEngine ABC——可插拔的上下文管理 |
agent/context_compressor.py | 默认引擎——有损摘要算法 |
agent/prompt_caching.py | Anthropic prompt 缓存标记和缓存指标 |
agent/auxiliary_client.py | 用于辅助任务的辅助 LLM 客户端(视觉、摘要) |
model_tools.py | 工具 schema 集合,handle_function_call() 分发 |