refactor(chat): build SSE payload after the liveness check

In public resource URL mode, building the payload consumes the chunk into the
holdback buffer, so returning between build and write would drop it. Reusing
emitStreamEvent also removes the duplicated flush-before-completion logic.

Co-authored-by: lyingbug <lyingbug@users.noreply.github.com>
This commit is contained in:
lyingbug
2026-08-04 09:30:52 +00:00
committed by lyingbug
parent ef1f0b36c4
commit 061319581d
+5 -12
View File
@@ -393,9 +393,6 @@ func (h *Handler) handleAgentEventsForSSE(
return
}
// Build StreamResponse from StreamEvent
response := buildStreamResponseFor(ctx, evt, requestID, resourceRewriter)
// Check for completion event
if evt.Type == "complete" {
streamCompleted = true
@@ -406,20 +403,16 @@ func (h *Handler) handleAgentEventsForSSE(
titleReceived = true
}
// Check if connection is still alive before writing
// Check if connection is still alive before writing. Build the
// payload only after this check: in public resource URL mode
// building consumes the chunk into the holdback buffer, so an
// early return here would drop it.
if c.Request.Context().Err() != nil {
log.Info("Connection closed during event sending, stopping")
return
}
// Any content still held back must precede the completion marker,
// which clients treat as the end of the message.
if streamCompleted {
flushHeldStreamContent(ctx, c, requestID, resourceRewriter)
}
c.SSEvent("message", response)
c.Writer.Flush()
emitStreamEvent(ctx, c, evt, requestID, resourceRewriter)
}
// Update offset