fix: backfill empty functionResponse.name from preceding functionCall
when Amp or Claude Code sends functionResponse with an empty name in Gemini conversation history, the Gemini API rejects the request with 400 "Name cannot be empty". this fix backfills empty names from the corresponding preceding functionCall parts using positional matching. covers all three Gemini translator paths: - gemini/gemini (direct API key) - antigravity/gemini (OAuth) - gemini-cli/gemini (Gemini CLI) also switches fixCLIToolResponse pending group matching from LIFO to FIFO to correctly handle multiple sequential tool call groups. fixes #1903
This commit is contained in:
@@ -5,9 +5,11 @@ package gemini
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
)
|
||||
@@ -95,6 +97,71 @@ func ConvertGeminiRequestToGemini(_ string, inputRawJSON []byte, _ bool) []byte
|
||||
out = []byte(strJson)
|
||||
}
|
||||
|
||||
// Backfill empty functionResponse.name from the preceding functionCall.name.
|
||||
// Amp may send function responses with empty names; the Gemini API rejects these.
|
||||
out = backfillEmptyFunctionResponseNames(out)
|
||||
|
||||
out = common.AttachDefaultSafetySettings(out, "safetySettings")
|
||||
return out
|
||||
}
|
||||
|
||||
// backfillEmptyFunctionResponseNames walks the contents array and for each
|
||||
// model turn containing functionCall parts, records the call names in order.
|
||||
// For the immediately following user/function turn containing functionResponse
|
||||
// parts, any empty name is replaced with the corresponding call name.
|
||||
func backfillEmptyFunctionResponseNames(data []byte) []byte {
|
||||
contents := gjson.GetBytes(data, "contents")
|
||||
if !contents.Exists() {
|
||||
return data
|
||||
}
|
||||
|
||||
out := data
|
||||
var pendingCallNames []string
|
||||
|
||||
contents.ForEach(func(contentIdx, content gjson.Result) bool {
|
||||
role := content.Get("role").String()
|
||||
|
||||
// Collect functionCall names from model turns
|
||||
if role == "model" {
|
||||
var names []string
|
||||
content.Get("parts").ForEach(func(_, part gjson.Result) bool {
|
||||
if part.Get("functionCall").Exists() {
|
||||
names = append(names, part.Get("functionCall.name").String())
|
||||
}
|
||||
return true
|
||||
})
|
||||
if len(names) > 0 {
|
||||
pendingCallNames = names
|
||||
} else {
|
||||
pendingCallNames = nil
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Backfill empty functionResponse names from pending call names
|
||||
if len(pendingCallNames) > 0 {
|
||||
ri := 0
|
||||
content.Get("parts").ForEach(func(partIdx, part gjson.Result) bool {
|
||||
if part.Get("functionResponse").Exists() {
|
||||
name := part.Get("functionResponse.name").String()
|
||||
if strings.TrimSpace(name) == "" {
|
||||
if ri < len(pendingCallNames) {
|
||||
out, _ = sjson.SetBytes(out,
|
||||
fmt.Sprintf("contents.%d.parts.%d.functionResponse.name", contentIdx.Int(), partIdx.Int()),
|
||||
pendingCallNames[ri])
|
||||
} else {
|
||||
log.Debugf("more function responses than calls at contents[%d], skipping name backfill", contentIdx.Int())
|
||||
}
|
||||
}
|
||||
ri++
|
||||
}
|
||||
return true
|
||||
})
|
||||
pendingCallNames = nil
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
package gemini
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func TestBackfillEmptyFunctionResponseNames_Single(t *testing.T) {
|
||||
input := []byte(`{
|
||||
"contents": [
|
||||
{
|
||||
"role": "model",
|
||||
"parts": [
|
||||
{"functionCall": {"name": "Bash", "args": {"cmd": "ls"}}}
|
||||
]
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"parts": [
|
||||
{"functionResponse": {"name": "", "response": {"output": "file1.txt"}}}
|
||||
]
|
||||
}
|
||||
]
|
||||
}`)
|
||||
|
||||
out := backfillEmptyFunctionResponseNames(input)
|
||||
|
||||
name := gjson.GetBytes(out, "contents.1.parts.0.functionResponse.name").String()
|
||||
if name != "Bash" {
|
||||
t.Errorf("Expected backfilled name 'Bash', got '%s'", name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackfillEmptyFunctionResponseNames_Parallel(t *testing.T) {
|
||||
input := []byte(`{
|
||||
"contents": [
|
||||
{
|
||||
"role": "model",
|
||||
"parts": [
|
||||
{"functionCall": {"name": "Read", "args": {"path": "/a"}}},
|
||||
{"functionCall": {"name": "Grep", "args": {"pattern": "x"}}}
|
||||
]
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"parts": [
|
||||
{"functionResponse": {"name": "", "response": {"result": "content a"}}},
|
||||
{"functionResponse": {"name": "", "response": {"result": "match x"}}}
|
||||
]
|
||||
}
|
||||
]
|
||||
}`)
|
||||
|
||||
out := backfillEmptyFunctionResponseNames(input)
|
||||
|
||||
name0 := gjson.GetBytes(out, "contents.1.parts.0.functionResponse.name").String()
|
||||
name1 := gjson.GetBytes(out, "contents.1.parts.1.functionResponse.name").String()
|
||||
if name0 != "Read" {
|
||||
t.Errorf("Expected first name 'Read', got '%s'", name0)
|
||||
}
|
||||
if name1 != "Grep" {
|
||||
t.Errorf("Expected second name 'Grep', got '%s'", name1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackfillEmptyFunctionResponseNames_PreservesExisting(t *testing.T) {
|
||||
input := []byte(`{
|
||||
"contents": [
|
||||
{
|
||||
"role": "model",
|
||||
"parts": [
|
||||
{"functionCall": {"name": "Bash", "args": {}}}
|
||||
]
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"parts": [
|
||||
{"functionResponse": {"name": "Bash", "response": {"result": "ok"}}}
|
||||
]
|
||||
}
|
||||
]
|
||||
}`)
|
||||
|
||||
out := backfillEmptyFunctionResponseNames(input)
|
||||
|
||||
name := gjson.GetBytes(out, "contents.1.parts.0.functionResponse.name").String()
|
||||
if name != "Bash" {
|
||||
t.Errorf("Expected preserved name 'Bash', got '%s'", name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertGeminiRequestToGemini_BackfillsEmptyName(t *testing.T) {
|
||||
input := []byte(`{
|
||||
"contents": [
|
||||
{
|
||||
"role": "model",
|
||||
"parts": [
|
||||
{"functionCall": {"name": "Bash", "args": {"cmd": "ls"}}}
|
||||
]
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"parts": [
|
||||
{"functionResponse": {"name": "", "response": {"output": "file1.txt"}}}
|
||||
]
|
||||
}
|
||||
]
|
||||
}`)
|
||||
|
||||
out := ConvertGeminiRequestToGemini("", input, false)
|
||||
|
||||
name := gjson.GetBytes(out, "contents.1.parts.0.functionResponse.name").String()
|
||||
if name != "Bash" {
|
||||
t.Errorf("Expected backfilled name 'Bash', got '%s'", name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackfillEmptyFunctionResponseNames_MoreResponsesThanCalls(t *testing.T) {
|
||||
// Extra responses beyond the call count should not panic and should be left unchanged.
|
||||
input := []byte(`{
|
||||
"contents": [
|
||||
{
|
||||
"role": "model",
|
||||
"parts": [
|
||||
{"functionCall": {"name": "Bash", "args": {}}}
|
||||
]
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"parts": [
|
||||
{"functionResponse": {"name": "", "response": {"result": "ok"}}},
|
||||
{"functionResponse": {"name": "", "response": {"result": "extra"}}}
|
||||
]
|
||||
}
|
||||
]
|
||||
}`)
|
||||
|
||||
out := backfillEmptyFunctionResponseNames(input)
|
||||
|
||||
name0 := gjson.GetBytes(out, "contents.1.parts.0.functionResponse.name").String()
|
||||
if name0 != "Bash" {
|
||||
t.Errorf("Expected first name 'Bash', got '%s'", name0)
|
||||
}
|
||||
// Second response has no matching call, should remain empty
|
||||
name1 := gjson.GetBytes(out, "contents.1.parts.1.functionResponse.name").String()
|
||||
if name1 != "" {
|
||||
t.Errorf("Expected second name to remain empty, got '%s'", name1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackfillEmptyFunctionResponseNames_MultipleGroups(t *testing.T) {
|
||||
// Two sequential call/response groups should each get correct names.
|
||||
input := []byte(`{
|
||||
"contents": [
|
||||
{
|
||||
"role": "model",
|
||||
"parts": [
|
||||
{"functionCall": {"name": "Read", "args": {}}}
|
||||
]
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"parts": [
|
||||
{"functionResponse": {"name": "", "response": {"result": "content"}}}
|
||||
]
|
||||
},
|
||||
{
|
||||
"role": "model",
|
||||
"parts": [
|
||||
{"functionCall": {"name": "Grep", "args": {}}}
|
||||
]
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"parts": [
|
||||
{"functionResponse": {"name": "", "response": {"result": "match"}}}
|
||||
]
|
||||
}
|
||||
]
|
||||
}`)
|
||||
|
||||
out := backfillEmptyFunctionResponseNames(input)
|
||||
|
||||
name0 := gjson.GetBytes(out, "contents.1.parts.0.functionResponse.name").String()
|
||||
name1 := gjson.GetBytes(out, "contents.3.parts.0.functionResponse.name").String()
|
||||
if name0 != "Read" {
|
||||
t.Errorf("Expected first group name 'Read', got '%s'", name0)
|
||||
}
|
||||
if name1 != "Grep" {
|
||||
t.Errorf("Expected second group name 'Grep', got '%s'", name1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user