diff --git a/src/gateway/server-chat.ts b/src/gateway/server-chat.ts index d54d0a99eeb..64675c8b5b7 100644 --- a/src/gateway/server-chat.ts +++ b/src/gateway/server-chat.ts @@ -352,6 +352,27 @@ export function createAgentEventHandler({ const text = normalizedHeartbeatText.text.trim(); const shouldSuppressSilent = normalizedHeartbeatText.suppress || isSilentReplyText(text, SILENT_REPLY_TOKEN); + // Flush any throttled delta so streaming clients receive the complete text + // before the final event. The 150 ms throttle in emitChatDelta may have + // suppressed the most recent chunk, leaving the client with stale text. + if (text && !shouldSuppressSilent) { + const lastSent = chatRunState.deltaSentAt.get(clientRunId) ?? 0; + if (lastSent > 0) { + const flushPayload = { + runId: clientRunId, + sessionKey, + seq, + state: "delta" as const, + message: { + role: "assistant", + content: [{ type: "text", text }], + timestamp: Date.now(), + }, + }; + broadcast("chat", flushPayload, { dropIfSlow: true }); + nodeSendToSession(sessionKey, "chat", flushPayload); + } + } chatRunState.buffers.delete(clientRunId); chatRunState.deltaSentAt.delete(clientRunId); if (jobState === "done") {