diff --git a/internal/translator/antigravity/openai/chat-completions/antigravity_openai_request.go b/internal/translator/antigravity/openai/chat-completions/antigravity_openai_request.go index 51d4a02a..f2cb04d6 100644 --- a/internal/translator/antigravity/openai/chat-completions/antigravity_openai_request.go +++ b/internal/translator/antigravity/openai/chat-completions/antigravity_openai_request.go @@ -305,12 +305,12 @@ func ConvertOpenAIRequestToAntigravity(modelName string, inputRawJSON []byte, _ } } - // tools -> request.tools[0].functionDeclarations + request.tools[0].googleSearch passthrough + // tools -> request.tools[].functionDeclarations + request.tools[].googleSearch passthrough tools := gjson.GetBytes(rawJSON, "tools") if tools.IsArray() && len(tools.Array()) > 0 { - toolNode := []byte(`{}`) - hasTool := false + functionToolNode := []byte(`{}`) hasFunction := false + googleSearchNodes := make([][]byte, 0) for _, t := range tools.Array() { if t.Get("type").String() == "function" { fn := t.Get("function") @@ -349,31 +349,37 @@ func ConvertOpenAIRequestToAntigravity(modelName string, inputRawJSON []byte, _ } fnRaw, _ = sjson.Delete(fnRaw, "strict") if !hasFunction { - toolNode, _ = sjson.SetRawBytes(toolNode, "functionDeclarations", []byte("[]")) + functionToolNode, _ = sjson.SetRawBytes(functionToolNode, "functionDeclarations", []byte("[]")) } - tmp, errSet := sjson.SetRawBytes(toolNode, "functionDeclarations.-1", []byte(fnRaw)) + tmp, errSet := sjson.SetRawBytes(functionToolNode, "functionDeclarations.-1", []byte(fnRaw)) if errSet != nil { log.Warnf("Failed to append tool declaration for '%s': %v", fn.Get("name").String(), errSet) continue } - toolNode = tmp + functionToolNode = tmp hasFunction = true - hasTool = true } } if gs := t.Get("google_search"); gs.Exists() { + googleToolNode := []byte(`{}`) var errSet error - toolNode, errSet = sjson.SetRawBytes(toolNode, "googleSearch", []byte(gs.Raw)) + googleToolNode, errSet = sjson.SetRawBytes(googleToolNode, "googleSearch", []byte(gs.Raw)) if errSet != nil { log.Warnf("Failed to set googleSearch tool: %v", errSet) continue } - hasTool = true + googleSearchNodes = append(googleSearchNodes, googleToolNode) } } - if hasTool { - out, _ = sjson.SetRawBytes(out, "request.tools", []byte("[]")) - out, _ = sjson.SetRawBytes(out, "request.tools.0", toolNode) + if hasFunction || len(googleSearchNodes) > 0 { + toolsNode := []byte("[]") + if hasFunction { + toolsNode, _ = sjson.SetRawBytes(toolsNode, "-1", functionToolNode) + } + for _, googleNode := range googleSearchNodes { + toolsNode, _ = sjson.SetRawBytes(toolsNode, "-1", googleNode) + } + out, _ = sjson.SetRawBytes(out, "request.tools", toolsNode) } } diff --git a/internal/translator/gemini-cli/openai/chat-completions/gemini-cli_openai_request.go b/internal/translator/gemini-cli/openai/chat-completions/gemini-cli_openai_request.go index 85669689..6351fa58 100644 --- a/internal/translator/gemini-cli/openai/chat-completions/gemini-cli_openai_request.go +++ b/internal/translator/gemini-cli/openai/chat-completions/gemini-cli_openai_request.go @@ -283,12 +283,12 @@ func ConvertOpenAIRequestToGeminiCLI(modelName string, inputRawJSON []byte, _ bo } } - // tools -> request.tools[0].functionDeclarations + request.tools[0].googleSearch passthrough + // tools -> request.tools[].functionDeclarations + request.tools[].googleSearch passthrough tools := gjson.GetBytes(rawJSON, "tools") if tools.IsArray() && len(tools.Array()) > 0 { - toolNode := []byte(`{}`) - hasTool := false + functionToolNode := []byte(`{}`) hasFunction := false + googleSearchNodes := make([][]byte, 0) for _, t := range tools.Array() { if t.Get("type").String() == "function" { fn := t.Get("function") @@ -327,31 +327,37 @@ func ConvertOpenAIRequestToGeminiCLI(modelName string, inputRawJSON []byte, _ bo } fnRaw, _ = sjson.Delete(fnRaw, "strict") if !hasFunction { - toolNode, _ = sjson.SetRawBytes(toolNode, "functionDeclarations", []byte("[]")) + functionToolNode, _ = sjson.SetRawBytes(functionToolNode, "functionDeclarations", []byte("[]")) } - tmp, errSet := sjson.SetRawBytes(toolNode, "functionDeclarations.-1", []byte(fnRaw)) + tmp, errSet := sjson.SetRawBytes(functionToolNode, "functionDeclarations.-1", []byte(fnRaw)) if errSet != nil { log.Warnf("Failed to append tool declaration for '%s': %v", fn.Get("name").String(), errSet) continue } - toolNode = tmp + functionToolNode = tmp hasFunction = true - hasTool = true } } if gs := t.Get("google_search"); gs.Exists() { + googleToolNode := []byte(`{}`) var errSet error - toolNode, errSet = sjson.SetRawBytes(toolNode, "googleSearch", []byte(gs.Raw)) + googleToolNode, errSet = sjson.SetRawBytes(googleToolNode, "googleSearch", []byte(gs.Raw)) if errSet != nil { log.Warnf("Failed to set googleSearch tool: %v", errSet) continue } - hasTool = true + googleSearchNodes = append(googleSearchNodes, googleToolNode) } } - if hasTool { - out, _ = sjson.SetRawBytes(out, "request.tools", []byte("[]")) - out, _ = sjson.SetRawBytes(out, "request.tools.0", toolNode) + if hasFunction || len(googleSearchNodes) > 0 { + toolsNode := []byte("[]") + if hasFunction { + toolsNode, _ = sjson.SetRawBytes(toolsNode, "-1", functionToolNode) + } + for _, googleNode := range googleSearchNodes { + toolsNode, _ = sjson.SetRawBytes(toolsNode, "-1", googleNode) + } + out, _ = sjson.SetRawBytes(out, "request.tools", toolsNode) } } diff --git a/internal/translator/gemini/openai/chat-completions/gemini_openai_request.go b/internal/translator/gemini/openai/chat-completions/gemini_openai_request.go index ba8b47e3..0a35cfd0 100644 --- a/internal/translator/gemini/openai/chat-completions/gemini_openai_request.go +++ b/internal/translator/gemini/openai/chat-completions/gemini_openai_request.go @@ -289,12 +289,12 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool) } } - // tools -> tools[0].functionDeclarations + tools[0].googleSearch passthrough + // tools -> tools[].functionDeclarations + tools[].googleSearch passthrough tools := gjson.GetBytes(rawJSON, "tools") if tools.IsArray() && len(tools.Array()) > 0 { - toolNode := []byte(`{}`) - hasTool := false + functionToolNode := []byte(`{}`) hasFunction := false + googleSearchNodes := make([][]byte, 0) for _, t := range tools.Array() { if t.Get("type").String() == "function" { fn := t.Get("function") @@ -333,31 +333,37 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool) } fnRaw, _ = sjson.Delete(fnRaw, "strict") if !hasFunction { - toolNode, _ = sjson.SetRawBytes(toolNode, "functionDeclarations", []byte("[]")) + functionToolNode, _ = sjson.SetRawBytes(functionToolNode, "functionDeclarations", []byte("[]")) } - tmp, errSet := sjson.SetRawBytes(toolNode, "functionDeclarations.-1", []byte(fnRaw)) + tmp, errSet := sjson.SetRawBytes(functionToolNode, "functionDeclarations.-1", []byte(fnRaw)) if errSet != nil { log.Warnf("Failed to append tool declaration for '%s': %v", fn.Get("name").String(), errSet) continue } - toolNode = tmp + functionToolNode = tmp hasFunction = true - hasTool = true } } if gs := t.Get("google_search"); gs.Exists() { + googleToolNode := []byte(`{}`) var errSet error - toolNode, errSet = sjson.SetRawBytes(toolNode, "googleSearch", []byte(gs.Raw)) + googleToolNode, errSet = sjson.SetRawBytes(googleToolNode, "googleSearch", []byte(gs.Raw)) if errSet != nil { log.Warnf("Failed to set googleSearch tool: %v", errSet) continue } - hasTool = true + googleSearchNodes = append(googleSearchNodes, googleToolNode) } } - if hasTool { - out, _ = sjson.SetRawBytes(out, "tools", []byte("[]")) - out, _ = sjson.SetRawBytes(out, "tools.0", toolNode) + if hasFunction || len(googleSearchNodes) > 0 { + toolsNode := []byte("[]") + if hasFunction { + toolsNode, _ = sjson.SetRawBytes(toolsNode, "-1", functionToolNode) + } + for _, googleNode := range googleSearchNodes { + toolsNode, _ = sjson.SetRawBytes(toolsNode, "-1", googleNode) + } + out, _ = sjson.SetRawBytes(out, "tools", toolsNode) } }