From 56c8297f6bd287a1120ab9367bcdc77266ff677b Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Mon, 1 Sep 2025 17:38:24 +0800 Subject: [PATCH] **Handle `data:` without trailing space in streaming responses** MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add support for API providers that emit `data:` (no space) in Server‑Sent Events. Introduces a new `dataUglyTag` and corresponding parsing logic to correctly process and forward these lines, ensuring compatibility with non‑standard streaming formats. Fuck for them all --- internal/client/openai-compatibility_client.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/internal/client/openai-compatibility_client.go b/internal/client/openai-compatibility_client.go index 7e859d0c..5afefda5 100644 --- a/internal/client/openai-compatibility_client.go +++ b/internal/client/openai-compatibility_client.go @@ -279,6 +279,7 @@ func (c *OpenAICompatibilityClient) SendRawMessageStream(ctx context.Context, mo rawJSON = translator.Request(handlerType, c.Type(), modelName, rawJSON, true) dataTag := []byte("data: ") + dataUglyTag := []byte("data:") // Some APIs providers don't add space after "data:", fuck for them all doneTag := []byte("data: [DONE]") errChan := make(chan *interfaces.ErrorMessage) dataChan := make(chan []byte) @@ -325,6 +326,14 @@ func (c *OpenAICompatibilityClient) SendRawMessageStream(ctx context.Context, mo for i := 0; i < len(lines); i++ { dataChan <- []byte(lines[i]) } + } else if bytes.HasPrefix(line, dataUglyTag) { + if bytes.Equal(line, doneTag) { + break + } + lines := translator.Response(handlerType, c.Type(), newCtx, modelName, line[5:], ¶m) + for i := 0; i < len(lines); i++ { + dataChan <- []byte(lines[i]) + } } } } else { @@ -337,6 +346,9 @@ func (c *OpenAICompatibilityClient) SendRawMessageStream(ctx context.Context, mo } c.AddAPIResponseData(newCtx, line[6:]) dataChan <- line[6:] + } else if bytes.HasPrefix(line, dataUglyTag) { + c.AddAPIResponseData(newCtx, line[5:]) + dataChan <- line[5:] } } }