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
12 changes: 10 additions & 2 deletions internal/transformer/convert/claude_gemini.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -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)
Expand Down
65 changes: 65 additions & 0 deletions internal/transformer/convert/claude_gemini_tool_ids_test.go
Original file line number Diff line number Diff line change
@@ -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])
}
}
Loading