From 72d30a623a107ffa30eade6e1105500f6d731b53 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Fri, 27 Nov 2026 01:46:36 +0000 Subject: [PATCH] fix(messages): build named SSE frames without going through writeData writeEvent now writes the data lines itself, with the same line splitting, so relayed upstream bytes do not reach the capacity computation in writeData. --- messages_stream.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/messages_stream.go b/messages_stream.go index d421a55..3d2800e 100644 --- a/messages_stream.go +++ b/messages_stream.go @@ -407,13 +407,15 @@ func relayAnthropicMessagesStream(w http.ResponseWriter, body io.Reader, model s // writeEvent writes one frame with an "event:" line ahead of the data, the // framing the Anthropic Messages stream uses. event must not contain a line -// terminator; an empty event writes a bare data frame. +// terminator; an empty event writes a bare data frame. Every data line is +// split the way writeData splits it, so no raw CR or LF reaches the wire. func (s *sseWriter) writeEvent(event string, b []byte) error { - if event == "" { - return s.writeData(b) + var buf []byte + if event != "" { + buf = append(buf, sseEventPrefix...) + buf = append(buf, event...) + buf = append(buf, '\n') } - buf := append([]byte(sseEventPrefix), event...) - buf = append(buf, '\n') for _, line := range splitSSELines(b) { buf = append(buf, sseDataPrefix...) buf = append(buf, line...)