refactor: standardize dataTag processing across response translators

- 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

View File

@@ -18,7 +18,7 @@ import (
)
var (
dataTag = []byte("data: ")
dataTag = []byte("data:")
)
// ConvertAnthropicResponseToOpenAIParams holds parameters for response conversion
@@ -62,7 +62,7 @@ func ConvertClaudeResponseToOpenAI(_ context.Context, modelName string, original
if !bytes.HasPrefix(rawJSON, dataTag) {
return []string{}
}
rawJSON = rawJSON[6:]
rawJSON = bytes.TrimSpace(rawJSON[5:])
root := gjson.ParseBytes(rawJSON)
eventType := root.Get("type").String()
@@ -289,7 +289,7 @@ func ConvertClaudeResponseToOpenAINonStream(_ context.Context, _ string, origina
if !bytes.HasPrefix(line, dataTag) {
continue
}
chunks = append(chunks, line[6:])
chunks = append(chunks, bytes.TrimSpace(rawJSON[5:]))
}
// Base OpenAI non-streaming response template

View File

@@ -34,7 +34,7 @@ type claudeToResponsesState struct {
ReasoningIndex int
}
var dataTag = []byte("data: ")
var dataTag = []byte("data:")
func emitEvent(event string, payload string) string {
return fmt.Sprintf("event: %s\ndata: %s\n\n", event, payload)
@@ -51,7 +51,7 @@ func ConvertClaudeResponseToOpenAIResponses(ctx context.Context, modelName strin
if !bytes.HasPrefix(rawJSON, dataTag) {
return []string{}
}
rawJSON = rawJSON[6:]
rawJSON = bytes.TrimSpace(rawJSON[5:])
root := gjson.ParseBytes(rawJSON)
ev := root.Get("type").String()
var out []string