Skip to content

[Feature]: 支持流式输出中的过程文本与最终答案分流 #2975

Open

Description

@dargoner

背景
在多轮 ReAct 或工具调用场景中,模型通常会先输出一段规划、分析或决策文本,然后调用工具,最后再生成面向用户的最终答案。
目前流式事件中的文本增量缺少明确的语义区分。调用方只能看到连续的文本事件,无法可靠判断某段文本属于:

  • 面向用户的最终答案;
  • 工具调用前的中间过程;
  • 工具调用后的补充说明;
  • 尚未确定是否为最终答案的临时内容。
    当前问题
  1. 中间文本可能被误展示给最终用户
    模型在调用工具前输出的规划内容,可能会被前端或上层应用直接展示。
    这会导致:
  • 内部决策过程暴露给用户;
  • 中间结论与最终结论混杂;
  • 不同模型的输出风格导致展示效果不一致;
  • 应用方需要自行编写复杂的过滤逻辑。
  1. 直接等待最终结果会损失流式体验
    一种简单处理方式是等待 AgentResultEvent,确认本轮没有工具调用后,再一次性输出文本。
    但这种方式会:
  • 增加用户等待时间;
  • 失去文本逐步到达的体验;
  • 使长文本响应无法及时显示;
  • 降低 AG-UI、WebUI 等交互界面的实时性。
  1. 文本是否为最终答案,需要在轮次结束后才能确认 PR feat(middleware): add final answer filter for ReAct streams #2926
    在模型文本刚开始流出时,调用方通常无法立即判断它是否会紧接着触发工具调用。
    只有当本轮模型输出结束后,才能确认:
  • 本轮是否产生工具调用;
  • 本轮文本应被视为中间过程还是最终答案;
  • 是否需要向最终用户展示这段文本。
    因此,该问题不适合完全由前端或业务层单独解决,需要框架提供统一的语义表达。
  1. 各适配层的行为可能不一致
    目前不同输出通道对中间文本和最终文本的处理方式可能存在差异,例如:
  • Agent 原生事件流;
  • Agent Protocol;
  • AG-UI;
  • Web/DataPlane;
  • 前端状态管理。
    这会导致相同一次 Agent 执行,在不同客户端中出现不同的展示结果。
    改进建议
    建议在框架层明确表达流式文本的输出语义,使调用方能够根据使用场景选择不同的展示策略。
  1. 提供文本语义分类
    流式文本至少应能区分:
  • 中间过程文本;
  • 最终答案文本;
  • 尚未确定归属的文本。
    分类结果应随事件流传递,并保持跨适配层的一致性。
  1. 同时支持两种消费模式
    框架应支持以下两类使用方式:
    完整流模式
    调用方可以获取全部文本,包括:
  • 中间过程;
  • 工具相关过程;
  • 最终答案。
    适用于调试界面、内部观测、Agent 可视化和过程追踪。
    最终答案模式
    调用方只接收确认属于最终答案的文本,不需要自行处理工具调用和中间过程。
    适用于:
  • 面向最终用户的聊天界面;
  • 对外 API;
  • 需要隐藏内部过程的产品场景。
  1. 保持流式体验
    在能够确认文本语义后,应尽可能继续以增量方式输出最终答案,而不是强制等待整个 Agent 执行结束后再一次性返回。

Activity

  1. added
    enhancement
    on Sep 4, 2026
  2. CryoThrust commented on Sep 4, 2026

    @CryoThrust
    Contributor

    A backward-compatible API shape could keep the existing text delta event and add an explicit semantic envelope rather than asking adapters to infer intent. For example, each stream item could carry phase (MODEL_OUTPUT, TOOL_CALL, TOOL_RESULT, FINAL), turnId, and visibility (INTERNAL, USER), while preserving raw provider events for observability. The framework should buffer only the current model turn until its stop reason is known; if the turn contains a tool call, reclassify its text as intermediate, otherwise emit it as final text incrementally. Adapters such as AG-UI and Agent Protocol can then map the same envelope consistently, and consumers can choose a full-trace or final-answer projection. It would be useful to specify whether text emitted after a tool result starts a new turn or remains part of the same assistant message.

  3. wxh2010zd commented on Sep 4, 2026

    @wxh2010zd

    如果打造Agent Ui,这是必须的。感觉可以参考类似于市面上Trae,workBuddy等输出包装形式,增加语义层面的内容。

  4. oss-maintainer commented on Sep 13, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: enhancement | Status: Evaluated — needs discussion | Feasibility: needs-discussion

    Assessed against main @ 39cd304a.

    Feasibility. The classification you want is expressible in the framework, but requirement 2 (a final-answer-only mode) and requirement 3 (stay incremental) cannot both hold unconditionally. Text is only known to be "not the final answer" once a tool call appears in the same round, which happens after the deltas have already been emitted. The existing implementation states this explicitly: agentscope-core/src/main/java/io/agentscope/core/middleware/FinalAnswerFilterMiddleware.java:36-38 documents that text events are buffered until the model call completes and are suppressed if a tool call was produced, and the mechanism is in handle() at :80-84 (buffer deltas), :93-98 (a ToolCallStartEvent clears the buffer) and :100-108 (ModelCallEndEvent either drops or replays the buffered text). So a strict final-only stream inherently costs latency; the way to keep incremental delivery is to emit provisional text plus a later reclassification/retraction signal, which pushes complexity into the consumer.

    Scope. agentscope-core event model and stream orchestration (agentscope-core/src/main/java/io/agentscope/core/agent/EventType.java:40,52,91 expose REASONING / TOOL_RESULT / SUMMARY, and message/ThinkingBlock.java distinguishes provider thinking but not intermediate vs. final plain text; the round loop lives in agent/ReActAgent.java), plus the agent-protocol / AGUI serializers and RemoteAgentEvent in the harness.

    Minimal seam. Attach an immutable per-round classification to the text block lifecycle events (TextBlockStartEvent / TextBlockDeltaEvent / TextBlockEndEvent) — e.g. PROCESS / FINAL / PROVISIONAL — decided at the end of each reasoning round, and propagate it through the protocol adapters so every channel agrees. Additive field with a legacy default keeps full-stream behaviour for existing consumers, which is why the compatibility risk is manageable once the client contract is fixed.

    Recommendation. Decide the client contract first, then implement: either (1) allow provisional text plus a round-finalization/retraction event, which buys true incremental streaming, or (2) keep buffering for a strict final-only mode and accept the added latency. Those two are the real tradeoff; "incremental and never shows process text" is not achievable at the framework layer. A full-stream mode (requirement 2, first variant) needs no new mechanism at all — it is today's behaviour, it just needs to be an explicit, documented option rather than an accident.

    Prior work: the open PR #3013 ("feat(streaming): add semantic text output disposition events") is already an attempt at this same seam, and #2926 shipped the buffering FinalAnswerFilterMiddleware that this request would generalize. Reviewing and converging on #3013 is preferable to a parallel implementation, so no new pull request is proposed here — the design question to settle on this issue is which of the two contracts above maintainers want.


    Automated evaluation by github-manager-bot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions