.agents/design/core/ai/agent-loop/index.md
状态:当前实现
最后核对:2026-07-16
Agent Loop 为不同 Agent provider 和上层业务提供稳定的模型循环协议。它统一以下语义:
fastAgent 与 piAgent 的业务可见行为。Agent Loop 不负责 Workflow 节点输出、数据库写入、SSE 协议、权限鉴定或具体工具业务。
Workflow Agent / ToolCall
`-- packages/service/core/workflow/dispatch/ai/agentLoopCore
|-- context and runtime adapters
|-- assistantResponses / nodeResponse collectors
|-- interactive and usage adapters
`-- packages/service/core/ai/llm/agentLoop/interface
|-- application: provider selection and usage collection
|-- domain: input/runtime/result/event/tool contracts
`-- provider
|-- fastAgent
`-- piAgent
agentLoop目录:packages/service/core/ai/llm/agentLoop
职责:
runAgentLoop。fastAgent,未知 provider 直接报错。status: error 的标准结果。usagePush 上报的用量,并随结果返回,不二次触发计费。agentLoopCore目录:packages/service/core/workflow/dispatch/ai/agentLoopCore
职责:
assistantResponses、nodeResponse 和 SSE 所需数据。paused 转为 Workflow 的 interactive。Workflow Agent 与 ToolCall 都调用 agentLoopCore,但保留各自节点语义:
AgentLoopInput 只包含模型循环可理解的数据:
messages 和可选 systemPrompt。activePlan。providerState。userAnswer。childrenInteractiveParams。AgentLoopRuntime 由调用方注入运行能力:
executeTool。底层不能从 Workflow 闭包读取额外状态。新增业务能力应先判断它属于通用协议、系统工具还是 Workflow adapter。
AgentLoopResult 使用判别联合表达四种状态:
done:正常完成。paused:等待 ask 回答或子工具继续执行。aborted:用户停止或 provider 控制结束。error:执行失败,同时尽量保留已产生的消息、requestId 和 usage。稳定返回字段包括完整消息、当前轮 assistant 消息、requestId、usage、计划、providerState、Checkpoint 和 finishReason。底层结果不包含 Workflow interactive schema。
系统工具由 Agent Loop 维护统一语义,目前包括:
plan:创建和更新当前计划。ask:暂停本轮并等待用户回答。sandbox:使用准备好的 Sandbox client 执行系统工具。readFile:读取对话上传的文档。datasetSearch:查询当前可用知识库。是否启用以及所需 executor/client 由 Runtime 显式传入。
业务工具通过 toolCatalog.runtimeTools 暴露,由统一 executeTool 执行。执行结果包含:
response。Agent Loop 不解释 metadata 的业务结构;Workflow collector 在边界外消费它。
Provider 通过 AgentLoopEvent 报告模型请求、流式文本、工具运行、计划、ask 和压缩事件。事件有两个独立消费者:
agentLoopCore collector 负责 assistantResponses 和 nodeResponse。assistantResponses 的写入原则是单一来源:调用方传入额外业务响应,collector 只根据标准事件追加 Agent 响应,最终统一压缩重复计划快照。节点外壳不得再根据最终 messages 重复补写同一批工具结果。
agentLoopCore/application/context/reminder.ts 统一构造当前用户消息中的动态上下文:
SKILL.md 路径。这些内容放在 user message 的 `` 中。文档文件通过 read_files 的 { ids } 协议读取;Sandbox 文件操作使用独立的 Sandbox 工具。
历史上下文超过阈值时,压缩模块生成一个 <context_checkpoint> string,保留目标、约束、关键事实、工具结果、资源和下一步。其约束是:
实现位于 packages/service/core/ai/llm/compress,Agent provider 只消费其标准结果。
ask 事件。providerState.pendingMainContext。status: paused 和 pause.type: ask。agentLoopCore 转换为 Workflow interactive。providerState 和 userAnswer,provider 在原工具调用后追加 tool response 并继续。pause.type: tool_child 和 toolCallId。childrenInteractiveParams 回传子流程结果。旧 interactive 快照的兼容读取集中在 agentLoopCore/adapter/memory 和 provider 恢复边界;新写入只使用标准结构。
runtime.usagePush 上报真实 usage。agentLoopCore 将通用 usage 转换为 Workflow 账单类型。inputTokens、outputTokens 和 llmTotalPoints 只汇总 agentCall 项。| 能力 | 路径 |
|---|---|
| 公共入口 | packages/service/core/ai/llm/agentLoop/interface/run.ts |
| 领域协议 | packages/service/core/ai/llm/agentLoop/domain |
| Provider 注册 | packages/service/core/ai/llm/agentLoop/provider/registry.ts |
| Workflow Core | packages/service/core/workflow/dispatch/ai/agentLoopCore |
| Workflow Agent | packages/service/core/workflow/dispatch/ai/agent |
| ToolCall | packages/service/core/workflow/dispatch/ai/toolcall |
| 历史压缩 | packages/service/core/ai/llm/compress |
相关测试主要位于:
packages/service/test/core/ai/llm/agentLooppackages/service/test/core/ai/llm/compresspackages/service/test/core/workflow/dispatch/ai/agentLoopCore协议改动至少需要覆盖 provider contract、collector、ask/child 恢复、Checkpoint 传播和 usage 单次上报。