mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-18 04:10:51 +08:00
**Handle data: without trailing space in streaming responses**
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
This commit is contained in:
@@ -279,6 +279,7 @@ func (c *OpenAICompatibilityClient) SendRawMessageStream(ctx context.Context, mo
|
|||||||
rawJSON = translator.Request(handlerType, c.Type(), modelName, rawJSON, true)
|
rawJSON = translator.Request(handlerType, c.Type(), modelName, rawJSON, true)
|
||||||
|
|
||||||
dataTag := []byte("data: ")
|
dataTag := []byte("data: ")
|
||||||
|
dataUglyTag := []byte("data:") // Some APIs providers don't add space after "data:", fuck for them all
|
||||||
doneTag := []byte("data: [DONE]")
|
doneTag := []byte("data: [DONE]")
|
||||||
errChan := make(chan *interfaces.ErrorMessage)
|
errChan := make(chan *interfaces.ErrorMessage)
|
||||||
dataChan := make(chan []byte)
|
dataChan := make(chan []byte)
|
||||||
@@ -325,6 +326,14 @@ func (c *OpenAICompatibilityClient) SendRawMessageStream(ctx context.Context, mo
|
|||||||
for i := 0; i < len(lines); i++ {
|
for i := 0; i < len(lines); i++ {
|
||||||
dataChan <- []byte(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 {
|
} else {
|
||||||
@@ -337,6 +346,9 @@ func (c *OpenAICompatibilityClient) SendRawMessageStream(ctx context.Context, mo
|
|||||||
}
|
}
|
||||||
c.AddAPIResponseData(newCtx, line[6:])
|
c.AddAPIResponseData(newCtx, line[6:])
|
||||||
dataChan <- line[6:]
|
dataChan <- line[6:]
|
||||||
|
} else if bytes.HasPrefix(line, dataUglyTag) {
|
||||||
|
c.AddAPIResponseData(newCtx, line[5:])
|
||||||
|
dataChan <- line[5:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user