refactor: standardize dataTag processing across response translators
docker-image / docker (push) Has been cancelled
goreleaser / goreleaser (push) Has been cancelled

- Unified `dataTag` initialization by removing spaces after `data:`.
- Replaced manual slicing with `bytes.TrimSpace` for consistent and robust handling of JSON payloads.
This commit is contained in:
Luis Pater
2025-09-21 11:16:03 +08:00
parent 83a1fa618d
commit e5a6fd2d4f
11 changed files with 38 additions and 51 deletions
+6 -6
View File
@@ -215,8 +215,8 @@ func (c *QwenClient) SendRawMessageStream(ctx context.Context, modelName string,
handlerType := handler.HandlerType()
rawJSON = translator.Request(handlerType, c.Type(), modelName, rawJSON, true)
dataTag := []byte("data: ")
doneTag := []byte("data: [DONE]")
dataTag := []byte("data:")
doneTag := []byte("[DONE]")
errChan := make(chan *interfaces.ErrorMessage)
dataChan := make(chan []byte)
@@ -264,7 +264,7 @@ func (c *QwenClient) SendRawMessageStream(ctx context.Context, modelName string,
for scanner.Scan() {
line := scanner.Bytes()
if bytes.HasPrefix(line, dataTag) {
lines := translator.Response(handlerType, c.Type(), ctx, modelName, originalRequestRawJSON, rawJSON, line[6:], &param)
lines := translator.Response(handlerType, c.Type(), ctx, modelName, originalRequestRawJSON, rawJSON, bytes.TrimSpace(line[5:]), &param)
for i := 0; i < len(lines); i++ {
dataChan <- []byte(lines[i])
}
@@ -274,9 +274,9 @@ func (c *QwenClient) SendRawMessageStream(ctx context.Context, modelName string,
} else {
for scanner.Scan() {
line := scanner.Bytes()
if !bytes.HasPrefix(line, doneTag) {
if bytes.HasPrefix(line, dataTag) {
dataChan <- line[6:]
if bytes.HasPrefix(line, dataTag) {
if !bytes.Equal(bytes.TrimSpace(line[5:]), doneTag) {
dataChan <- bytes.TrimSpace(line[5:])
}
}
c.AddAPIResponseData(ctx, line)