mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-29 13:23:04 +08:00
fix(antigravity): preserve non-empty groundingMetadata when trailing SSE chunk carries empty object
- Track whether captured groundingMetadata contains any fields via ojson ForEach so trailing empty objects cannot overwrite earlier non-empty metadata while preserving explicit empty objects when no non-empty metadata was seen. - Write preserved non-empty groundingMetadata back into templateDoc unconditionally so trailing empty groundingMetadata objects are replaced. - Add non-streaming assembly unit tests covering trailing empty objects, latest non-empty precedence, and explicit empty-object compatibility. Closes: #8160
This commit is contained in:
@@ -492,21 +492,22 @@ func (e *AntigravityExecutor) convertStreamToNonStream(stream []byte) []byte {
|
||||
|
||||
func (e *AntigravityExecutor) convertStreamChunksToNonStream(chunks [][]byte) []byte {
|
||||
var (
|
||||
responseNodeRaw string
|
||||
responsePartsRaw string
|
||||
responsePartsIndex int
|
||||
traceID string
|
||||
finishReason string
|
||||
modelVersion string
|
||||
responseID string
|
||||
role string
|
||||
usageRaw string
|
||||
groundingMetadataRaw string
|
||||
parts []string
|
||||
pendingKind string
|
||||
pendingText strings.Builder
|
||||
pendingThoughtSig string
|
||||
pendingFromCurrentChunk bool
|
||||
responseNodeRaw string
|
||||
responsePartsRaw string
|
||||
responsePartsIndex int
|
||||
traceID string
|
||||
finishReason string
|
||||
modelVersion string
|
||||
responseID string
|
||||
role string
|
||||
usageRaw string
|
||||
groundingMetadataRaw string
|
||||
groundingMetadataNonEmpty bool
|
||||
parts []string
|
||||
pendingKind string
|
||||
pendingText strings.Builder
|
||||
pendingThoughtSig string
|
||||
pendingFromCurrentChunk bool
|
||||
)
|
||||
|
||||
flushPending := func() {
|
||||
@@ -623,7 +624,17 @@ func (e *AntigravityExecutor) convertStreamChunksToNonStream(chunks [][]byte) []
|
||||
}
|
||||
case "groundingMetadata":
|
||||
if cv.IsObject() && cv.Raw != "" {
|
||||
groundingMetadataRaw = cv.Raw
|
||||
hasFields := false
|
||||
cv.ForEach(func(_, _ ojson.Result) bool {
|
||||
hasFields = true
|
||||
return false
|
||||
})
|
||||
if hasFields {
|
||||
groundingMetadataRaw = cv.Raw
|
||||
groundingMetadataNonEmpty = true
|
||||
} else if !groundingMetadataNonEmpty {
|
||||
groundingMetadataRaw = cv.Raw
|
||||
}
|
||||
}
|
||||
case "content":
|
||||
if cv.IsObject() {
|
||||
@@ -762,7 +773,7 @@ func (e *AntigravityExecutor) convertStreamChunksToNonStream(chunks [][]byte) []
|
||||
if finishReason != "" {
|
||||
_ = templateDoc.Set("candidates.0.finishReason", finishReason)
|
||||
}
|
||||
if groundingMetadataRaw != "" && !templateDoc.Get("candidates.0.groundingMetadata").Exists() {
|
||||
if groundingMetadataRaw != "" && (groundingMetadataNonEmpty || !templateDoc.Get("candidates.0.groundingMetadata").Exists()) {
|
||||
_ = templateDoc.SetRawBytes("candidates.0.groundingMetadata", []byte(groundingMetadataRaw))
|
||||
}
|
||||
if modelVersion != "" {
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/MoeForge/moego/ojson"
|
||||
)
|
||||
|
||||
func TestAntigravityConvertStreamChunksToNonStream_PreservesGroundingMetadataWhenTrailingChunkEmpty(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
trailing string
|
||||
}{
|
||||
{
|
||||
name: "compact empty object",
|
||||
trailing: `{"response":{"candidates":[{"content":{"role":"model","parts":[]},"finishReason":"STOP","groundingMetadata":{}}],"usageMetadata":{"promptTokenCount":12,"candidatesTokenCount":5,"totalTokenCount":17}}}`,
|
||||
},
|
||||
{
|
||||
name: "whitespace empty object",
|
||||
trailing: `{"response":{"candidates":[{"content":{"role":"model","parts":[]},"finishReason":"STOP","groundingMetadata":{ }}],"usageMetadata":{"promptTokenCount":12,"candidatesTokenCount":5,"totalTokenCount":17}}}`,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
exec := &AntigravityExecutor{}
|
||||
chunks := [][]byte{
|
||||
[]byte(`{"response":{"candidates":[{"content":{"role":"model","parts":[{"text":"Search result answer."}]},"groundingMetadata":{"webSearchQueries":["cli proxy api"],"groundingChunks":[{"web":{"uri":"https://example.com","title":"Example"}}],"groundingSupports":[{"segment":{"startIndex":0,"endIndex":6},"groundingChunkIndices":[0]}]}}],"modelVersion":"gemini-3-pro","responseId":"resp-ground-empty-trailing"}}`),
|
||||
[]byte(tc.trailing),
|
||||
}
|
||||
|
||||
out := exec.convertStreamChunksToNonStream(chunks)
|
||||
grounding := ojson.GetBytes(out, "response.candidates.0.groundingMetadata")
|
||||
if got := grounding.Get("webSearchQueries.0").String(); got != "cli proxy api" {
|
||||
t.Fatalf("expected webSearchQueries.0 = %q, got %q (raw=%s)", "cli proxy api", got, grounding.Raw)
|
||||
}
|
||||
if got := grounding.Get("groundingChunks.0.web.uri").String(); got != "https://example.com" {
|
||||
t.Fatalf("expected groundingChunks.0.web.uri = %q, got %q (raw=%s)", "https://example.com", got, grounding.Raw)
|
||||
}
|
||||
if got := grounding.Get("groundingSupports.0.groundingChunkIndices.0").Raw; got != "0" {
|
||||
t.Fatalf("expected groundingSupports.0.groundingChunkIndices.0 = 0, got %q (raw=%s)", got, grounding.Raw)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAntigravityConvertStreamChunksToNonStream_PreservesEarlierEmptyGroundingMetadataWhenTrailingChunkOmitted(t *testing.T) {
|
||||
exec := &AntigravityExecutor{}
|
||||
chunks := [][]byte{
|
||||
[]byte(`{"response":{"candidates":[{"content":{"role":"model","parts":[{"text":"Plain answer."}]},"groundingMetadata":{}}],"modelVersion":"gemini-3-pro","responseId":"resp-ground-early-empty"}}`),
|
||||
[]byte(`{"response":{"candidates":[{"content":{"role":"model","parts":[]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":10,"candidatesTokenCount":3,"totalTokenCount":13}}}`),
|
||||
}
|
||||
|
||||
out := exec.convertStreamChunksToNonStream(chunks)
|
||||
grounding := ojson.GetBytes(out, "response.candidates.0.groundingMetadata")
|
||||
if !grounding.Exists() || grounding.Raw != "{}" {
|
||||
t.Fatalf("expected groundingMetadata = {}, got exists=%v raw=%q", grounding.Exists(), grounding.Raw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAntigravityConvertStreamChunksToNonStream_UsesLatestNonEmptyGroundingMetadata(t *testing.T) {
|
||||
exec := &AntigravityExecutor{}
|
||||
chunks := [][]byte{
|
||||
[]byte(`{"response":{"candidates":[{"content":{"role":"model","parts":[{"text":"First "}]},"groundingMetadata":{"webSearchQueries":["initial query"],"groundingChunks":[{"web":{"uri":"https://first.example","title":"First"}}]}}],"modelVersion":"gemini-3-pro","responseId":"resp-ground-latest"}}`),
|
||||
[]byte(`{"response":{"candidates":[{"content":{"role":"model","parts":[{"text":"Second"}]},"groundingMetadata":{"webSearchQueries":["cli proxy api"],"groundingChunks":[{"web":{"uri":"https://latest.example","title":"Latest"}}]}}],"modelVersion":"gemini-3-pro","responseId":"resp-ground-latest"}}`),
|
||||
[]byte(`{"response":{"candidates":[{"content":{"role":"model","parts":[]},"finishReason":"STOP","groundingMetadata":{}}],"usageMetadata":{"promptTokenCount":12,"candidatesTokenCount":5,"totalTokenCount":17}}}`),
|
||||
}
|
||||
|
||||
out := exec.convertStreamChunksToNonStream(chunks)
|
||||
grounding := ojson.GetBytes(out, "response.candidates.0.groundingMetadata")
|
||||
if got := grounding.Get("webSearchQueries.0").String(); got != "cli proxy api" {
|
||||
t.Fatalf("expected webSearchQueries.0 = %q, got %q (raw=%s)", "cli proxy api", got, grounding.Raw)
|
||||
}
|
||||
if got := grounding.Get("groundingChunks.0.web.uri").String(); got != "https://latest.example" {
|
||||
t.Fatalf("expected groundingChunks.0.web.uri = %q, got %q (raw=%s)", "https://latest.example", got, grounding.Raw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAntigravityConvertStreamChunksToNonStream_PreservesEmptyGroundingMetadataWhenNoNonEmptySeen(t *testing.T) {
|
||||
exec := &AntigravityExecutor{}
|
||||
chunks := [][]byte{
|
||||
[]byte(`{"response":{"candidates":[{"content":{"role":"model","parts":[{"text":"Plain answer."}]}}],"modelVersion":"gemini-3-pro","responseId":"resp-ground-only-empty"}}`),
|
||||
[]byte(`{"response":{"candidates":[{"content":{"role":"model","parts":[]},"finishReason":"STOP","groundingMetadata":{}}],"usageMetadata":{"promptTokenCount":10,"candidatesTokenCount":3,"totalTokenCount":13}}}`),
|
||||
}
|
||||
|
||||
out := exec.convertStreamChunksToNonStream(chunks)
|
||||
grounding := ojson.GetBytes(out, "response.candidates.0.groundingMetadata")
|
||||
if !grounding.Exists() || grounding.Raw != "{}" {
|
||||
t.Fatalf("expected groundingMetadata = {}, got exists=%v raw=%q", grounding.Exists(), grounding.Raw)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user