From 061319581d43dd4793305c8d624335deb91ee36e Mon Sep 17 00:00:00 2001 From: lyingbug <[email> Date: Tue, 4 Aug 2026 09:30:52 +0000 Subject: [PATCH] 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 --- internal/handler/session/stream.go | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/internal/handler/session/stream.go b/internal/handler/session/stream.go index 5eee9287..d73b88c3 100644 --- a/internal/handler/session/stream.go +++ b/internal/handler/session/stream.go @@ -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