Repository navigation
Description
Summary
AG-UI clients aggregate assistant text by messageId. Within one assistant turn (reply), a text → tool → text sequence is a continuation of the same assistant message and should use one stable text messageId, derived from the assistant turn identity the session log already records.
Related: ag-ui-protocol/ag-ui#1580.
This is a bug fix, not a feature request: no opt-in/opt-out configuration. The previous per-model-call text id is incorrect for AG-UI message aggregation.
Current behavior
ReActAgent.buildAgentStream()computes oneturnIdper assistant turn and stores it inRuntimeContext(SessionRecorder.TURN_ID_KEY). It builds anExecutionIdentity(agentId, sessionId, turnId, runId)and attaches it to every non-forwardedAgentEvent, soevent.getExecution().turnId()is available on the live stream. The sameturnIdis recorded on everySessionEvent.ReActAgent.buildIdentifiedAgentStream()generates a separatereplyIdUUID forAgentStartEvent/AgentEndEvent. That id is not the session fact, is not persisted, and is not available on non-AG-UI call paths.modelCallStream()creates a newcallIdper model call and passes it topreparedModelCallStream(...);ModelCallBlockLifecyclethen emitsTextBlockStartEvent/TextBlockDeltaEvent/TextBlockEndEventwith thatcallIdas theirreplyId.- The text converter therefore derives the text messageId from a per-model-call id. Text before and after a tool call gets different ids, and AG-UI clients render two assistant text messages.
- Downstream applications currently work around this by pinning the first text id for the whole run (for example
StableAguiMessageEventConverterin copilot-api).
Expected behavior
- Pin one text
messageIdper assistant turn using the turn identity the session log already records:ExecutionIdentity.turnIdon live events,SessionEvent.turnIdon replay. Do not useAgentStartEvent.replyIdor the per-callTextBlockDeltaEvent.replyId. - Reuse it across
text → tool → text; a tool call in the middle must not change the text message id. - Keep per-segment boundaries: each text segment still emits its own
TEXT_MESSAGE_START/TEXT_MESSAGE_ENDwith the pinned id.- Per the AG-UI spec (1.0 and the current draft), a
TEXT_MESSAGE_STARTfor an already-open id is a malformed sequence and fatal to the run. - A closed message is closed, not sealed: a producer MAY reopen the same
messageIdwith a newTEXT_MESSAGE_START, and further content appends to what is already there. A reopening START MUST agree with the message it reopens — the same owner,roleandname. - Do not rely on a repeated START being a client no-op: either re-START after
ENDwith the pinned id, or deferENDuntil the assistant turn ends.
- Per the AG-UI spec (1.0 and the current draft), a
- Reasoning messages keep an independent namespace: one assistant turn uses
<turnId>:reasoning, reopened across reasoning segments, so live, replay and persisted history agree on a single reasoning message id. Reasoning must never reuse the bare text id. - Sub-agent / forwarded text and text from different roles must not share the parent's pinned id.
- The replay path (
AguiSessionPresentation) must use the same rule, so a client that reconnects sees one stable id across replay and live frames. - No new legacy flag. Applications that need the old behavior can stay on the previous release or provide a custom
AgentEventConverter.
Why turnId
- It already exists:
ExecutionIdentity.turnIdon live events,SessionEvent.turnIdon the log, andchat_message.turnIddownstream. - It is path-independent: A2A, plain GraphQL and AG-UI all go through the same
SessionRecorder, so history export does not depend on an AG-UI-only id. - It requires no schema change, no
Msg.idchange, and no data migration. replyIdis generated independently fromturnIdand never enters the session log; using it would make AG-UI ids and persisted history diverge.
Implementation sketch
AguiStreamContextrecordsevent.getExecution().turnId()from non-forwarded lifecycle events and pins the text id to it. Keep the existingreplyId/blockIdfallback for streams without an execution identity (synthetic or forwarded events).TextBlockEventConverteruses the pinned text id forTEXT_MESSAGE_START/CONTENT/END.AguiSessionPresentationusesSessionEvent.turnId()for text and<turnId>:reasoningfor reasoning, identical to the live path.- Forwarded/sub-agent frames must not inherit the parent id.
- Do not add public opt-in/opt-out configuration.
Acceptance
- Given one
replywithtext -> tool_call -> text, the SSE stream emits the same textmessageIdfor both text segments, and that id equalsevent.getExecution().turnId(). - Each text segment has a matched
TEXT_MESSAGE_START/TEXT_MESSAGE_END. - Reasoning uses
<turnId>:reasoningand never the bare text id. - Replay and live frames for the same segment carry the same
messageId. - Sub-agent/forwarded text does not reuse the parent text id.
- An official
@ag-ui/clientapplied to the run reports one assistant text message, not two. - No custom
AgentEventConverteris needed downstream.
Context
Found while integrating @ag-ui/client 1.0.3 against an AgentScope Java backend. The downstream StableAguiMessageEventConverter workaround can be removed once the framework does this by default, except for the application-level <messageId>:reasoning mapping, which is a persistence convention.
References:
Activity
[-]fix(agui): keep one text messageId across text→tool→text in a run[/-][+]fix(agui): keep one text messageId across text→tool→text in an assistant turn[/+][-]fix(agui): keep one text messageId across text→tool→text in an assistant turn[/-][+][Bug]: assistant text messageId changes across text→tool→text within one reply[/+]