diff --git a/internal/runtime/executor/antigravity_executor_execute.go b/internal/runtime/executor/antigravity_executor_execute.go index 2541796178..88ea229532 100644 --- a/internal/runtime/executor/antigravity_executor_execute.go +++ b/internal/runtime/executor/antigravity_executor_execute.go @@ -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 != "" { diff --git a/internal/runtime/executor/antigravity_grounding_nonstream_test.go b/internal/runtime/executor/antigravity_grounding_nonstream_test.go new file mode 100644 index 0000000000..32981c5a93 --- /dev/null +++ b/internal/runtime/executor/antigravity_grounding_nonstream_test.go @@ -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) + } +}