前置阅读:流式输出基础用法 —当父stream()API、EventType、StreamOptions、SSE 集成。本页专注HarnessAgent的子 agent 事件转发机制。
HarnessAgent.stream() 期间调用了 agent_spawn 或 agent_send,子 agent 产生的所有中间事件会被实时注入父 Flux<Event>,并携带 EventSource 标识来源,无需任何额外配置。
工作原理
stream() 模式有 bus;call() 模式 bus 缺席,自动降级为阻塞 invokeAgent,行为不变。
事件时序
source == null;子 agent 事件 source != null。
通过 EventSource 区分来源
EventSource 字段一览
path 规则:格式为
<parentSessionId>/<agentId>,多级嵌套时自然叠加,如 sess-001/planner/executor。
多级嵌套(孙 Agent)
子HarnessAgent 在自己的 stream() 入口也会注入一个新 bus。孙 agent 的事件先被子 agent 的 bus 捕获(depth+1),再随子 agent 的 Flux<Event> 出口流出,被祖父的 AgentSpawnTool 再次转发到祖父 bus。path 按深度自动拼接。
source.getDepth() 或 source.getPath() 前缀即可定位任意层级:
常用消费模式
1. 实时流式 UI 渲染
按事件类型分流,分别送入不同 UI 组件:2. 等待子 agent 最终结果
如果只关心子 agent 的回复文本(不关心中间事件):3. 收集并按层级分组
4. SSE 转发(含来源元数据)
错误处理
子 agent 内部发生异常时,框架捕获后写入TOOL_RESULT 错误文本,不向父 Flux 传播 onError,父流不受影响。
若整条父流发生致命错误(如模型调用失败),则遵循标准 Reactor 语义:
行为边界
相关文档
- 流式输出基础 —
stream()API、EventType、StreamOptions完整参数 - 子 Agent(Subagent) — 子 agent 声明、
agent_spawn/agent_send参数 - 架构(Architecture) —
SubagentEventBus、Reactor Context 注入与StreamingHook时序