mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-03-01 01:24:29 +08:00
fix(responses): include model and usage in translated streams
Ensure response.created and response.completed chunks produced by the OpenAI/Gemini/Claude translators always include required fields (response.model and response.usage) so clients validating Responses SSE do not fail schema validation.
This commit is contained in:
@@ -212,6 +212,7 @@ func ConvertGeminiResponseToOpenAIResponses(_ context.Context, modelName string,
|
||||
created, _ = sjson.Set(created, "sequence_number", nextSeq())
|
||||
created, _ = sjson.Set(created, "response.id", st.ResponseID)
|
||||
created, _ = sjson.Set(created, "response.created_at", st.CreatedAt)
|
||||
created, _ = sjson.Set(created, "response.model", modelName)
|
||||
out = append(out, emitEvent("response.created", created))
|
||||
|
||||
inprog := `{"type":"response.in_progress","sequence_number":0,"response":{"id":"","object":"response","created_at":0,"status":"in_progress"}}`
|
||||
@@ -529,31 +530,36 @@ func ConvertGeminiResponseToOpenAIResponses(_ context.Context, modelName string,
|
||||
completed, _ = sjson.SetRaw(completed, "response.output", gjson.Get(outputsWrapper, "arr").Raw)
|
||||
}
|
||||
|
||||
// usage mapping
|
||||
input := int64(0)
|
||||
cached := int64(0)
|
||||
output := int64(0)
|
||||
reasoning := int64(0)
|
||||
total := int64(0)
|
||||
if um := root.Get("usageMetadata"); um.Exists() {
|
||||
// input tokens = prompt + thoughts
|
||||
input := um.Get("promptTokenCount").Int() + um.Get("thoughtsTokenCount").Int()
|
||||
completed, _ = sjson.Set(completed, "response.usage.input_tokens", input)
|
||||
input = um.Get("promptTokenCount").Int() + um.Get("thoughtsTokenCount").Int()
|
||||
// cached token details: align with OpenAI "cached_tokens" semantics.
|
||||
completed, _ = sjson.Set(completed, "response.usage.input_tokens_details.cached_tokens", um.Get("cachedContentTokenCount").Int())
|
||||
cached = um.Get("cachedContentTokenCount").Int()
|
||||
// output tokens
|
||||
if v := um.Get("candidatesTokenCount"); v.Exists() {
|
||||
completed, _ = sjson.Set(completed, "response.usage.output_tokens", v.Int())
|
||||
} else {
|
||||
completed, _ = sjson.Set(completed, "response.usage.output_tokens", 0)
|
||||
output = v.Int()
|
||||
}
|
||||
if v := um.Get("thoughtsTokenCount"); v.Exists() {
|
||||
completed, _ = sjson.Set(completed, "response.usage.output_tokens_details.reasoning_tokens", v.Int())
|
||||
} else {
|
||||
completed, _ = sjson.Set(completed, "response.usage.output_tokens_details.reasoning_tokens", 0)
|
||||
reasoning = v.Int()
|
||||
}
|
||||
if v := um.Get("totalTokenCount"); v.Exists() {
|
||||
completed, _ = sjson.Set(completed, "response.usage.total_tokens", v.Int())
|
||||
total = v.Int()
|
||||
} else {
|
||||
completed, _ = sjson.Set(completed, "response.usage.total_tokens", 0)
|
||||
total = input + output
|
||||
}
|
||||
}
|
||||
|
||||
completed, _ = sjson.Set(completed, "response.usage.input_tokens", input)
|
||||
completed, _ = sjson.Set(completed, "response.usage.input_tokens_details.cached_tokens", cached)
|
||||
completed, _ = sjson.Set(completed, "response.usage.output_tokens", output)
|
||||
completed, _ = sjson.Set(completed, "response.usage.output_tokens_details.reasoning_tokens", reasoning)
|
||||
completed, _ = sjson.Set(completed, "response.usage.total_tokens", total)
|
||||
|
||||
out = append(out, emitEvent("response.completed", completed))
|
||||
}
|
||||
|
||||
|
||||
@@ -53,6 +53,7 @@ func TestConvertGeminiResponseToOpenAIResponses_UnwrapAndAggregateText(t *testin
|
||||
textDone string
|
||||
messageText string
|
||||
responseID string
|
||||
createdModel string
|
||||
instructions string
|
||||
cachedTokens int64
|
||||
|
||||
@@ -68,6 +69,8 @@ func TestConvertGeminiResponseToOpenAIResponses_UnwrapAndAggregateText(t *testin
|
||||
for i, chunk := range out {
|
||||
ev, data := parseSSEEvent(t, chunk)
|
||||
switch ev {
|
||||
case "response.created":
|
||||
createdModel = data.Get("response.model").String()
|
||||
case "response.output_text.done":
|
||||
gotTextDone = true
|
||||
if posTextDone == -1 {
|
||||
@@ -132,6 +135,9 @@ func TestConvertGeminiResponseToOpenAIResponses_UnwrapAndAggregateText(t *testin
|
||||
if responseID != "resp_req_vrtx_1" {
|
||||
t.Fatalf("unexpected response id: got %q", responseID)
|
||||
}
|
||||
if createdModel != "test-model" {
|
||||
t.Fatalf("unexpected response.created model: got %q", createdModel)
|
||||
}
|
||||
if instructions != "test instructions" {
|
||||
t.Fatalf("unexpected instructions echo: got %q", instructions)
|
||||
}
|
||||
@@ -153,6 +159,31 @@ func TestConvertGeminiResponseToOpenAIResponses_UnwrapAndAggregateText(t *testin
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertGeminiResponseToOpenAIResponses_CompletedAlwaysHasUsage(t *testing.T) {
|
||||
in := `data: {"response":{"candidates":[{"content":{"role":"model","parts":[{"text":"hi"}]},"finishReason":"STOP"}],"modelVersion":"test-model","responseId":"req_no_usage"},"traceId":"t1"}`
|
||||
|
||||
var param any
|
||||
out := ConvertGeminiResponseToOpenAIResponses(context.Background(), "test-model", nil, nil, []byte(in), ¶m)
|
||||
|
||||
gotCompleted := false
|
||||
for _, chunk := range out {
|
||||
ev, data := parseSSEEvent(t, chunk)
|
||||
if ev != "response.completed" {
|
||||
continue
|
||||
}
|
||||
gotCompleted = true
|
||||
if !data.Get("response.usage.input_tokens").Exists() {
|
||||
t.Fatalf("response.completed missing usage.input_tokens: %s", data.Raw)
|
||||
}
|
||||
if !data.Get("response.usage.output_tokens").Exists() {
|
||||
t.Fatalf("response.completed missing usage.output_tokens: %s", data.Raw)
|
||||
}
|
||||
}
|
||||
if !gotCompleted {
|
||||
t.Fatalf("missing response.completed event")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertGeminiResponseToOpenAIResponses_ReasoningEncryptedContent(t *testing.T) {
|
||||
sig := "RXE0RENrZ0lDeEFDR0FJcVFOZDdjUzlleGFuRktRdFcvSzNyZ2MvWDNCcDQ4RmxSbGxOWUlOVU5kR1l1UHMrMGdkMVp0Vkg3ekdKU0g4YVljc2JjN3lNK0FrdGpTNUdqamI4T3Z0VVNETzdQd3pmcFhUOGl3U3hXUEJvTVFRQ09mWTFyMEtTWGZxUUlJakFqdmFGWk83RW1XRlBKckJVOVpkYzdDKw=="
|
||||
in := []string{
|
||||
|
||||
Reference in New Issue
Block a user