fix(antigravity): normalize tool call/response adjacency

This commit is contained in:
Finessse
2025-12-16 20:00:33 +07:00
parent d7eacf9524
commit 52f4d23c85
2 changed files with 281 additions and 164 deletions
@@ -9,10 +9,10 @@ import (
"bytes"
"encoding/json"
"fmt"
"unicode/utf8"
"github.com/router-for-me/CLIProxyAPI/v6/internal/translator/gemini/common"
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
@@ -43,7 +43,8 @@ func ConvertGeminiRequestToAntigravity(_ string, inputRawJSON []byte, _ bool) []
template, errFixCLIToolResponse := fixCLIToolResponse(template)
if errFixCLIToolResponse != nil {
return []byte{}
// Fail open: tool-call normalization is a best-effort resilience feature.
// Returning an empty request here causes hard failures upstream.
}
systemInstructionResult := gjson.Get(template, "request.system_instruction")
@@ -115,179 +116,194 @@ func ConvertGeminiRequestToAntigravity(_ string, inputRawJSON []byte, _ bool) []
return common.AttachDefaultSafetySettings(rawJSON, "request.safetySettings")
}
// FunctionCallGroup represents a group of function calls and their responses
type FunctionCallGroup struct {
ModelContent map[string]interface{}
FunctionCalls []gjson.Result
ResponsesNeeded int
}
// fixCLIToolResponse performs sophisticated tool response format conversion and grouping.
// This function transforms the CLI tool response format by intelligently grouping function calls
// with their corresponding responses, ensuring proper conversation flow and API compatibility.
// It converts from a linear format (1.json) to a grouped format (2.json) where function calls
// and their responses are properly associated and structured.
// fixCLIToolResponse normalizes Gemini CLI-style tool calling so that any model content containing
// one or more `functionCall` parts is immediately followed by a user content containing the matching
// `functionResponse` parts (matched by `id` when present).
//
// Parameters:
// - input: The input JSON string to be processed
//
// Returns:
// - string: The processed JSON string with grouped function calls and responses
// - error: An error if the processing fails
// This is required for downstream Claude-style validation that enforces tool result adjacency, and
// prevents orphan tool calls that would otherwise yield 400s.
func fixCLIToolResponse(input string) (string, error) {
// Parse the input JSON to extract the conversation structure
parsed := gjson.Parse(input)
// Extract the contents array which contains the conversation messages
contents := parsed.Get("request.contents")
if !contents.Exists() {
// log.Debugf(input)
return input, fmt.Errorf("contents not found in input")
// Normalize to valid UTF-8 so encoding/json can safely parse even if upstream/client
// accidentally emits invalid bytes inside JSON strings.
inBytes := []byte(input)
if !utf8.Valid(inBytes) {
inBytes = bytes.ToValidUTF8(inBytes, []byte("\uFFFD"))
}
// Initialize data structures for processing and grouping
var newContents []interface{} // Final processed contents array
var pendingGroups []*FunctionCallGroup // Groups awaiting completion with responses
var collectedResponses []gjson.Result // Standalone responses to be matched
var root map[string]any
if err := json.Unmarshal(inBytes, &root); err != nil {
return input, nil
}
req, _ := root["request"].(map[string]any)
if req == nil {
return input, nil
}
rawContents, _ := req["contents"].([]any)
if rawContents == nil {
return input, nil
}
// Process each content object in the conversation
// This iterates through messages and groups function calls with their responses
contents.ForEach(func(key, value gjson.Result) bool {
role := value.Get("role").String()
parts := value.Get("parts")
type callInfo struct {
id string
name string
}
// Check if this content has function responses
var responsePartsInThisContent []gjson.Result
parts.ForEach(func(_, part gjson.Result) bool {
if part.Get("functionResponse").Exists() {
responsePartsInThisContent = append(responsePartsInThisContent, part)
}
return true
})
// If this content has function responses, collect them
if len(responsePartsInThisContent) > 0 {
collectedResponses = append(collectedResponses, responsePartsInThisContent...)
// Check if any pending groups can be satisfied
for i := len(pendingGroups) - 1; i >= 0; i-- {
group := pendingGroups[i]
if len(collectedResponses) >= group.ResponsesNeeded {
// Take the needed responses for this group
groupResponses := collectedResponses[:group.ResponsesNeeded]
collectedResponses = collectedResponses[group.ResponsesNeeded:]
// Create merged function response content
var responseParts []interface{}
for _, response := range groupResponses {
var responseMap map[string]interface{}
errUnmarshal := json.Unmarshal([]byte(response.Raw), &responseMap)
if errUnmarshal != nil {
log.Warnf("failed to unmarshal function response: %v\n", errUnmarshal)
continue
}
responseParts = append(responseParts, responseMap)
}
if len(responseParts) > 0 {
functionResponseContent := map[string]interface{}{
"parts": responseParts,
"role": "function",
}
newContents = append(newContents, functionResponseContent)
}
// Remove this group as it's been satisfied
pendingGroups = append(pendingGroups[:i], pendingGroups[i+1:]...)
break
}
}
return true // Skip adding this content, responses are merged
// Collect all functionCall ids (so we only relocate matching responses).
callIDs := make(map[string]struct{})
for _, cAny := range rawContents {
c, _ := cAny.(map[string]any)
if c == nil {
continue
}
// If this is a model with function calls, create a new group
if role == "model" {
var functionCallsInThisModel []gjson.Result
parts.ForEach(func(_, part gjson.Result) bool {
if part.Get("functionCall").Exists() {
functionCallsInThisModel = append(functionCallsInThisModel, part)
}
return true
})
if len(functionCallsInThisModel) > 0 {
// Add the model content
var contentMap map[string]interface{}
errUnmarshal := json.Unmarshal([]byte(value.Raw), &contentMap)
if errUnmarshal != nil {
log.Warnf("failed to unmarshal model content: %v\n", errUnmarshal)
return true
}
newContents = append(newContents, contentMap)
// Create a new group for tracking responses
group := &FunctionCallGroup{
ModelContent: contentMap,
FunctionCalls: functionCallsInThisModel,
ResponsesNeeded: len(functionCallsInThisModel),
}
pendingGroups = append(pendingGroups, group)
} else {
// Regular model content without function calls
var contentMap map[string]interface{}
errUnmarshal := json.Unmarshal([]byte(value.Raw), &contentMap)
if errUnmarshal != nil {
log.Warnf("failed to unmarshal content: %v\n", errUnmarshal)
return true
}
newContents = append(newContents, contentMap)
parts, _ := c["parts"].([]any)
for _, pAny := range parts {
p, _ := pAny.(map[string]any)
if p == nil {
continue
}
fc, _ := p["functionCall"].(map[string]any)
if fc == nil {
continue
}
if id, _ := fc["id"].(string); id != "" {
callIDs[id] = struct{}{}
}
}
}
// Index functionResponse parts by id (keep in encountered order).
responsesByID := make(map[string][]map[string]any)
for _, cAny := range rawContents {
c, _ := cAny.(map[string]any)
if c == nil {
continue
}
parts, _ := c["parts"].([]any)
for _, pAny := range parts {
p, _ := pAny.(map[string]any)
if p == nil {
continue
}
fr, _ := p["functionResponse"].(map[string]any)
if fr == nil {
continue
}
id, _ := fr["id"].(string)
if id == "" {
continue
}
if _, ok := callIDs[id]; !ok {
continue
}
responsesByID[id] = append(responsesByID[id], p)
}
}
// Remove the indexed response parts from their original locations so we don't duplicate them.
for idx := 0; idx < len(rawContents); idx++ {
c, _ := rawContents[idx].(map[string]any)
if c == nil {
continue
}
parts, _ := c["parts"].([]any)
if len(parts) == 0 {
continue
}
kept := make([]any, 0, len(parts))
for _, pAny := range parts {
p, _ := pAny.(map[string]any)
fr, _ := p["functionResponse"].(map[string]any)
if fr != nil {
id, _ := fr["id"].(string)
if id != "" {
if _, ok := callIDs[id]; ok {
continue // relocated
}
}
}
kept = append(kept, pAny)
}
if len(kept) == 0 {
// Drop content entries that were only tool responses.
delete(c, "parts")
rawContents[idx] = c
} else {
// Non-model content (user, etc.)
var contentMap map[string]interface{}
errUnmarshal := json.Unmarshal([]byte(value.Raw), &contentMap)
if errUnmarshal != nil {
log.Warnf("failed to unmarshal content: %v\n", errUnmarshal)
return true
}
newContents = append(newContents, contentMap)
}
return true
})
// Handle any remaining pending groups with remaining responses
for _, group := range pendingGroups {
if len(collectedResponses) >= group.ResponsesNeeded {
groupResponses := collectedResponses[:group.ResponsesNeeded]
collectedResponses = collectedResponses[group.ResponsesNeeded:]
var responseParts []interface{}
for _, response := range groupResponses {
var responseMap map[string]interface{}
errUnmarshal := json.Unmarshal([]byte(response.Raw), &responseMap)
if errUnmarshal != nil {
log.Warnf("failed to unmarshal function response: %v\n", errUnmarshal)
continue
}
responseParts = append(responseParts, responseMap)
}
if len(responseParts) > 0 {
functionResponseContent := map[string]interface{}{
"parts": responseParts,
"role": "function",
}
newContents = append(newContents, functionResponseContent)
}
c["parts"] = kept
rawContents[idx] = c
}
}
// Update the original JSON with the new contents
result := input
newContentsJSON, _ := json.Marshal(newContents)
result, _ = sjson.Set(result, "request.contents", json.RawMessage(newContentsJSON))
// Build new contents, inserting a tool-response user message after each model functionCall turn.
outContents := make([]any, 0, len(rawContents))
for _, cAny := range rawContents {
c, _ := cAny.(map[string]any)
if c == nil {
continue
}
partsAny, _ := c["parts"].([]any)
if partsAny == nil {
// Empty content (e.g. removed response-only). Skip.
continue
}
return result, nil
outContents = append(outContents, c)
if c["role"] != "model" {
continue
}
// Extract ordered functionCall parts in this model content.
calls := make([]callInfo, 0, 4)
for _, pAny := range partsAny {
p, _ := pAny.(map[string]any)
if p == nil {
continue
}
fc, _ := p["functionCall"].(map[string]any)
if fc == nil {
continue
}
id, _ := fc["id"].(string)
name, _ := fc["name"].(string)
if id == "" {
continue
}
calls = append(calls, callInfo{id: id, name: name})
}
if len(calls) == 0 {
continue
}
respParts := make([]any, 0, len(calls))
for _, call := range calls {
if bucket := responsesByID[call.id]; len(bucket) > 0 {
respParts = append(respParts, bucket[0])
responsesByID[call.id] = bucket[1:]
continue
}
// Fallback: synthesize a placeholder result so upstream validators are satisfied.
respParts = append(respParts, map[string]any{
"functionResponse": map[string]any{
"id": call.id,
"name": call.name,
"response": map[string]any{
"result": fmt.Sprintf("tool_result missing for %s", call.id),
},
},
})
}
outContents = append(outContents, map[string]any{
"role": "user",
"parts": respParts,
})
}
req["contents"] = outContents
root["request"] = req
updated, err := json.Marshal(root)
if err != nil {
return input, err
}
return string(updated), nil
}
@@ -0,0 +1,101 @@
package gemini
import (
"fmt"
"testing"
"github.com/tidwall/gjson"
)
func TestFixCLIToolResponse_DoesNotDropEarlyResponses(t *testing.T) {
in := `{
"request": {
"contents": [
{"role":"model","parts":[{"functionCall":{"id":"a","name":"A","args":{}}}]},
{"role":"function","parts":[{"functionResponse":{"id":"a","name":"A","response":{"result":"outA"}}}]},
{"role":"function","parts":[{"functionResponse":{"id":"b","name":"B","response":{"result":"outB"}}}]},
{"role":"model","parts":[{"functionCall":{"id":"b","name":"B","args":{}}}]}
]
}
}`
out, err := fixCLIToolResponse(in)
if err != nil {
t.Fatalf("fixCLIToolResponse error: %v", err)
}
contents := gjson.Get(out, "request.contents")
if !contents.Exists() || !contents.IsArray() {
t.Fatalf("expected request.contents array, got body=%s", out)
}
var callBIndex int64 = -1
contents.ForEach(func(k, v gjson.Result) bool {
if v.Get("role").String() != "model" {
return true
}
v.Get("parts").ForEach(func(_, p gjson.Result) bool {
if p.Get("functionCall.id").String() == "b" {
callBIndex = k.Int()
return false
}
return true
})
return callBIndex == -1
})
if callBIndex < 0 {
t.Fatalf("expected to find functionCall id=b in output body=%s", out)
}
next := gjson.Get(out, fmt.Sprintf("request.contents.%d.parts", callBIndex+1))
if !next.Exists() || !next.IsArray() {
t.Fatalf("expected tool response content after call b body=%s", out)
}
found := false
next.ForEach(func(_, p gjson.Result) bool {
if p.Get("functionResponse.id").String() == "b" {
found = true
return false
}
return true
})
if !found {
t.Fatalf("expected functionResponse id=b immediately after call b body=%s", out)
}
}
func TestFixCLIToolResponse_PullsResponsesBeforeUserText(t *testing.T) {
in := `{
"request": {
"contents": [
{"role":"model","parts":[{"functionCall":{"id":"a","name":"A","args":{}}}]},
{"role":"user","parts":[{"text":"(some user text that must not split tool call/result)"}]},
{"role":"function","parts":[{"functionResponse":{"id":"a","name":"A","response":{"result":"outA"}}}]}
]
}
}`
out, err := fixCLIToolResponse(in)
if err != nil {
t.Fatalf("fixCLIToolResponse error: %v", err)
}
contents := gjson.Get(out, "request.contents")
if !contents.Exists() || !contents.IsArray() {
t.Fatalf("expected request.contents array, got body=%s", out)
}
if contents.Get("0.role").String() != "model" {
t.Fatalf("expected first role=model, got %s body=%s", contents.Get("0.role").String(), out)
}
if contents.Get("1.role").String() != "user" {
t.Fatalf("expected second role=user, got %s body=%s", contents.Get("1.role").String(), out)
}
if contents.Get("1.parts.0.functionResponse.id").String() != "a" {
t.Fatalf("expected second message to contain functionResponse id=a body=%s", out)
}
if contents.Get("2.parts.0.text").String() == "" {
t.Fatalf("expected user text to follow tool result body=%s", out)
}
}