diff --git a/internal/transformer/convert/claude_gemini.go b/internal/transformer/convert/claude_gemini.go index a7fc3c4..7d11f87 100644 --- a/internal/transformer/convert/claude_gemini.go +++ b/internal/transformer/convert/claude_gemini.go @@ -5,9 +5,17 @@ import ( "fmt" "strings" + "github.com/google/uuid" "github.com/lich0821/ccNexus/internal/transformer" ) +// newGeminiToolUseID returns a unique id for a tool_use block built from a Gemini +// functionCall. Gemini does not send call ids, and an id derived from the function +// name alone collides when the model calls the same tool twice in one turn. +func newGeminiToolUseID() string { + return "call_" + uuid.NewString() +} + // ClaudeReqToGemini converts Claude request to Gemini request func ClaudeReqToGemini(claudeReq []byte, model string) ([]byte, error) { var req transformer.ClaudeRequest @@ -275,7 +283,7 @@ func GeminiRespToClaude(geminiResp []byte) ([]byte, error) { if part.FunctionCall != nil { content = append(content, map[string]interface{}{ "type": "tool_use", - "id": fmt.Sprintf("call_%s", part.FunctionCall.Name), + "id": newGeminiToolUseID(), "name": part.FunctionCall.Name, "input": part.FunctionCall.Args, }) @@ -419,7 +427,7 @@ func GeminiStreamToClaude(event []byte, ctx *transformer.StreamContext) ([]byte, result = append(result, buildClaudeEvent("content_block_start", map[string]interface{}{ "index": ctx.ContentIndex, "content_block": map[string]interface{}{ - "type": "tool_use", "id": fmt.Sprintf("call_%s", part.FunctionCall.Name), "name": part.FunctionCall.Name, + "type": "tool_use", "id": newGeminiToolUseID(), "name": part.FunctionCall.Name, }, })...) args, _ := json.Marshal(part.FunctionCall.Args) diff --git a/internal/transformer/convert/claude_gemini_tool_ids_test.go b/internal/transformer/convert/claude_gemini_tool_ids_test.go new file mode 100644 index 0000000..9bc961b --- /dev/null +++ b/internal/transformer/convert/claude_gemini_tool_ids_test.go @@ -0,0 +1,65 @@ +package convert + +import ( + "encoding/json" + "regexp" + "testing" + + "github.com/lich0821/ccNexus/internal/transformer" +) + +const geminiParallelSameToolResp = `{"candidates":[{"content":{"role":"model","parts":[` + + `{"functionCall":{"name":"read_file","args":{"path":"a.txt"}}},` + + `{"functionCall":{"name":"read_file","args":{"path":"b.txt"}}}]},"finishReason":"STOP"}]}` + +func TestGeminiRespToClaudeGivesParallelSameToolCallsDistinctIDs(t *testing.T) { + out, err := GeminiRespToClaude([]byte(geminiParallelSameToolResp)) + if err != nil { + t.Fatalf("GeminiRespToClaude failed: %v", err) + } + + var resp struct { + Content []struct { + Type string `json:"type"` + ID string `json:"id"` + } `json:"content"` + } + if err := json.Unmarshal(out, &resp); err != nil { + t.Fatalf("invalid output: %v", err) + } + + var ids []string + for _, block := range resp.Content { + if block.Type == "tool_use" { + ids = append(ids, block.ID) + } + } + if len(ids) != 2 { + t.Fatalf("expected 2 tool_use blocks, got %d", len(ids)) + } + if ids[0] == "" || ids[0] == ids[1] { + t.Errorf("tool_use ids must be non-empty and distinct, got %q and %q", ids[0], ids[1]) + } +} + +func TestGeminiStreamToClaudeGivesParallelSameToolCallsDistinctIDs(t *testing.T) { + ctx := transformer.NewStreamContext() + ctx.ModelName = "gemini-3.8-flash" + + out, err := GeminiStreamToClaude([]byte("data: "+geminiParallelSameToolResp+"\n\n"), ctx) + if err != nil { + t.Fatalf("GeminiStreamToClaude failed: %v", err) + } + + matches := regexp.MustCompile(`"id":"(call_[^"]*)"`).FindAllStringSubmatch(string(out), -1) + var ids []string + for _, m := range matches { + ids = append(ids, m[1]) + } + if len(ids) != 2 { + t.Fatalf("expected 2 tool_use ids in the stream, got %d: %s", len(ids), out) + } + if ids[0] == "" || ids[0] == ids[1] { + t.Errorf("tool_use ids must be non-empty and distinct, got %q and %q", ids[0], ids[1]) + } +}