Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions internal/transformer/convert/claude_gemini.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,43 @@ func ClaudeReqToGemini(claudeReq []byte, model string) ([]byte, error) {
geminiReq["tools"] = []map[string]interface{}{{"functionDeclarations": funcDecls}}
// Add toolConfig to enable function calling
geminiReq["toolConfig"] = map[string]interface{}{
"functionCallingConfig": map[string]interface{}{
"mode": "AUTO",
},
"functionCallingConfig": claudeToolChoiceToGeminiConfig(req.ToolChoice),
}
}

return json.Marshal(geminiReq)
}

// claudeToolChoiceToGeminiConfig maps a Claude tool_choice to a Gemini
// functionCallingConfig. A missing or unknown choice stays AUTO.
func claudeToolChoiceToGeminiConfig(toolChoice interface{}) map[string]interface{} {
config := map[string]interface{}{"mode": "AUTO"}

var choiceType string
var name string
switch tc := toolChoice.(type) {
case map[string]interface{}:
choiceType, _ = tc["type"].(string)
name, _ = tc["name"].(string)
case string:
choiceType = tc
}

switch choiceType {
case "any", "required":
config["mode"] = "ANY"
case "tool":
config["mode"] = "ANY"
if name != "" {
config["allowedFunctionNames"] = []string{name}
}
case "none":
config["mode"] = "NONE"
}

return config
}

// GeminiReqToClaude converts Gemini request to Claude request
func GeminiReqToClaude(geminiReq []byte, model string) ([]byte, error) {
var req transformer.GeminiRequest
Expand Down
49 changes: 49 additions & 0 deletions internal/transformer/convert/claude_gemini_tool_choice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package convert

import (
"encoding/json"
"reflect"
"testing"
)

func TestClaudeReqToGeminiToolChoice(t *testing.T) {
tests := []struct {
name string
toolChoice string
wantMode string
wantNames []interface{}
}{
{"omitted", ``, "AUTO", nil},
{"auto", `,"tool_choice":{"type":"auto"}`, "AUTO", nil},
{"any", `,"tool_choice":{"type":"any"}`, "ANY", nil},
{"tool", `,"tool_choice":{"type":"tool","name":"get_weather"}`, "ANY", []interface{}{"get_weather"}},
{"none", `,"tool_choice":{"type":"none"}`, "NONE", nil},
{"string any", `,"tool_choice":"any"`, "ANY", nil},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
claudeReq := `{"model":"claude","max_tokens":100,"messages":[{"role":"user","content":"hi"}],
"tools":[{"name":"get_weather","description":"d","input_schema":{"type":"object","properties":{}}}]` + tt.toolChoice + `}`

out, err := ClaudeReqToGemini([]byte(claudeReq), "gemini-2.5-pro")
if err != nil {
t.Fatalf("ClaudeReqToGemini failed: %v", err)
}

var got map[string]interface{}
if err := json.Unmarshal(out, &got); err != nil {
t.Fatalf("invalid output: %v", err)
}
toolConfig, _ := got["toolConfig"].(map[string]interface{})
fcc, _ := toolConfig["functionCallingConfig"].(map[string]interface{})
if fcc["mode"] != tt.wantMode {
t.Errorf("mode = %v, want %v", fcc["mode"], tt.wantMode)
}
gotNames, _ := fcc["allowedFunctionNames"].([]interface{})
if !reflect.DeepEqual(gotNames, tt.wantNames) {
t.Errorf("allowedFunctionNames = %v, want %v", gotNames, tt.wantNames)
}
})
}
}
Loading