| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658 |
- package antigravity
- import (
- "encoding/json"
- "strings"
- "testing"
- "github.com/stretchr/testify/require"
- )
- // TestBuildParts_ThinkingBlockWithoutSignature 测试thinking block无signature时的处理
- func TestBuildParts_ThinkingBlockWithoutSignature(t *testing.T) {
- tests := []struct {
- name string
- content string
- allowDummyThought bool
- expectedParts int
- description string
- }{
- {
- name: "Claude model - downgrade thinking to text without signature",
- content: `[
- {"type": "text", "text": "Hello"},
- {"type": "thinking", "thinking": "Let me think...", "signature": ""},
- {"type": "text", "text": "World"}
- ]`,
- allowDummyThought: false,
- expectedParts: 3, // thinking 内容降级为普通 text part
- description: "Claude模型缺少signature时应将thinking降级为text,并在上层禁用thinking mode",
- },
- {
- name: "Claude model - preserve thinking block with signature",
- content: `[
- {"type": "text", "text": "Hello"},
- {"type": "thinking", "thinking": "Let me think...", "signature": "sig_real_123"},
- {"type": "text", "text": "World"}
- ]`,
- allowDummyThought: false,
- expectedParts: 3,
- description: "Claude模型应透传带 signature 的 thinking block(用于 Vertex 签名链路)",
- },
- {
- name: "Gemini model - use dummy signature",
- content: `[
- {"type": "text", "text": "Hello"},
- {"type": "thinking", "thinking": "Let me think...", "signature": ""},
- {"type": "text", "text": "World"}
- ]`,
- allowDummyThought: true,
- expectedParts: 3, // 三个block都保留,thinking使用dummy signature
- description: "Gemini模型应该为无signature的thinking block使用dummy signature",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- toolIDToName := make(map[string]string)
- parts, _, err := buildParts(json.RawMessage(tt.content), toolIDToName, tt.allowDummyThought)
- if err != nil {
- t.Fatalf("buildParts() error = %v", err)
- }
- if len(parts) != tt.expectedParts {
- t.Errorf("%s: got %d parts, want %d parts", tt.description, len(parts), tt.expectedParts)
- }
- switch tt.name {
- case "Claude model - preserve thinking block with signature":
- if len(parts) != 3 {
- t.Fatalf("expected 3 parts, got %d", len(parts))
- }
- if !parts[1].Thought || parts[1].ThoughtSignature != "sig_real_123" {
- t.Fatalf("expected thought part with signature sig_real_123, got thought=%v signature=%q",
- parts[1].Thought, parts[1].ThoughtSignature)
- }
- case "Claude model - downgrade thinking to text without signature":
- if len(parts) != 3 {
- t.Fatalf("expected 3 parts, got %d", len(parts))
- }
- if parts[1].Thought {
- t.Fatalf("expected downgraded text part, got thought=%v signature=%q",
- parts[1].Thought, parts[1].ThoughtSignature)
- }
- if parts[1].Text != "Let me think..." {
- t.Fatalf("expected downgraded text %q, got %q", "Let me think...", parts[1].Text)
- }
- case "Gemini model - use dummy signature":
- if len(parts) != 3 {
- t.Fatalf("expected 3 parts, got %d", len(parts))
- }
- if !parts[1].Thought || parts[1].ThoughtSignature != DummyThoughtSignature {
- t.Fatalf("expected dummy thought signature, got thought=%v signature=%q",
- parts[1].Thought, parts[1].ThoughtSignature)
- }
- }
- })
- }
- }
- func TestBuildParts_ToolUseSignatureHandling(t *testing.T) {
- content := `[
- {"type": "tool_use", "id": "t1", "name": "Bash", "input": {"command": "ls"}, "signature": "sig_tool_abc"}
- ]`
- t.Run("Gemini preserves provided tool_use signature", func(t *testing.T) {
- toolIDToName := make(map[string]string)
- parts, _, err := buildParts(json.RawMessage(content), toolIDToName, true)
- if err != nil {
- t.Fatalf("buildParts() error = %v", err)
- }
- if len(parts) != 1 || parts[0].FunctionCall == nil {
- t.Fatalf("expected 1 functionCall part, got %+v", parts)
- }
- if parts[0].ThoughtSignature != "sig_tool_abc" {
- t.Fatalf("expected preserved tool signature %q, got %q", "sig_tool_abc", parts[0].ThoughtSignature)
- }
- })
- t.Run("Gemini falls back to dummy tool_use signature when missing", func(t *testing.T) {
- contentNoSig := `[
- {"type": "tool_use", "id": "t1", "name": "Bash", "input": {"command": "ls"}}
- ]`
- toolIDToName := make(map[string]string)
- parts, _, err := buildParts(json.RawMessage(contentNoSig), toolIDToName, true)
- if err != nil {
- t.Fatalf("buildParts() error = %v", err)
- }
- if len(parts) != 1 || parts[0].FunctionCall == nil {
- t.Fatalf("expected 1 functionCall part, got %+v", parts)
- }
- if parts[0].ThoughtSignature != DummyThoughtSignature {
- t.Fatalf("expected dummy tool signature %q, got %q", DummyThoughtSignature, parts[0].ThoughtSignature)
- }
- })
- t.Run("Claude model - preserve valid signature for tool_use", func(t *testing.T) {
- toolIDToName := make(map[string]string)
- parts, _, err := buildParts(json.RawMessage(content), toolIDToName, false)
- if err != nil {
- t.Fatalf("buildParts() error = %v", err)
- }
- if len(parts) != 1 || parts[0].FunctionCall == nil {
- t.Fatalf("expected 1 functionCall part, got %+v", parts)
- }
- // Claude 模型应透传有效的 signature(Vertex/Google 需要完整签名链路)
- if parts[0].ThoughtSignature != "sig_tool_abc" {
- t.Fatalf("expected preserved tool signature %q, got %q", "sig_tool_abc", parts[0].ThoughtSignature)
- }
- })
- }
- // TestBuildTools_CustomTypeTools 测试custom类型工具转换
- func TestBuildTools_CustomTypeTools(t *testing.T) {
- tests := []struct {
- name string
- tools []ClaudeTool
- expectedLen int
- description string
- }{
- {
- name: "Standard tool format",
- tools: []ClaudeTool{
- {
- Name: "get_weather",
- Description: "Get weather information",
- InputSchema: map[string]any{
- "type": "object",
- "properties": map[string]any{
- "location": map[string]any{"type": "string"},
- },
- },
- },
- },
- expectedLen: 1,
- description: "标准工具格式应该正常转换",
- },
- {
- name: "Custom type tool (MCP format)",
- tools: []ClaudeTool{
- {
- Type: "custom",
- Name: "mcp_tool",
- Custom: &ClaudeCustomToolSpec{
- Description: "MCP tool description",
- InputSchema: map[string]any{
- "type": "object",
- "properties": map[string]any{
- "param": map[string]any{"type": "string"},
- },
- },
- },
- },
- },
- expectedLen: 1,
- description: "Custom类型工具应该从Custom字段读取description和input_schema",
- },
- {
- name: "Mixed standard and custom tools",
- tools: []ClaudeTool{
- {
- Name: "standard_tool",
- Description: "Standard tool",
- InputSchema: map[string]any{"type": "object"},
- },
- {
- Type: "custom",
- Name: "custom_tool",
- Custom: &ClaudeCustomToolSpec{
- Description: "Custom tool",
- InputSchema: map[string]any{"type": "object"},
- },
- },
- },
- expectedLen: 1, // 返回一个GeminiToolDeclaration,包含2个function declarations
- description: "混合标准和custom工具应该都能正确转换",
- },
- {
- name: "Invalid custom tool - nil Custom field",
- tools: []ClaudeTool{
- {
- Type: "custom",
- Name: "invalid_custom",
- // Custom 为 nil
- },
- },
- expectedLen: 0, // 应该被跳过
- description: "Custom字段为nil的custom工具应该被跳过",
- },
- {
- name: "Invalid custom tool - nil InputSchema",
- tools: []ClaudeTool{
- {
- Type: "custom",
- Name: "invalid_custom",
- Custom: &ClaudeCustomToolSpec{
- Description: "Invalid",
- // InputSchema 为 nil
- },
- },
- },
- expectedLen: 0, // 应该被跳过
- description: "InputSchema为nil的custom工具应该被跳过",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result := buildTools(tt.tools)
- if len(result) != tt.expectedLen {
- t.Errorf("%s: got %d tool declarations, want %d", tt.description, len(result), tt.expectedLen)
- }
- // 验证function declarations存在
- if len(result) > 0 && result[0].FunctionDeclarations != nil {
- if len(result[0].FunctionDeclarations) != len(tt.tools) {
- t.Errorf("%s: got %d function declarations, want %d",
- tt.description, len(result[0].FunctionDeclarations), len(tt.tools))
- }
- }
- })
- }
- }
- func TestBuildTools_PreservesWebSearchAlongsideFunctions(t *testing.T) {
- tools := []ClaudeTool{
- {
- Name: "get_weather",
- Description: "Get weather information",
- InputSchema: map[string]any{"type": "object"},
- },
- {
- Type: "web_search_20250305",
- Name: "web_search",
- },
- }
- result := buildTools(tools)
- require.Len(t, result, 2)
- require.Len(t, result[0].FunctionDeclarations, 1)
- require.Equal(t, "get_weather", result[0].FunctionDeclarations[0].Name)
- require.NotNil(t, result[1].GoogleSearch)
- require.NotNil(t, result[1].GoogleSearch.EnhancedContent)
- require.NotNil(t, result[1].GoogleSearch.EnhancedContent.ImageSearch)
- require.Equal(t, 5, result[1].GoogleSearch.EnhancedContent.ImageSearch.MaxResultCount)
- }
- func TestBuildGenerationConfig_ThinkingDynamicBudget(t *testing.T) {
- tests := []struct {
- name string
- model string
- thinking *ThinkingConfig
- wantBudget int
- wantPresent bool
- }{
- {
- name: "enabled without budget defaults to dynamic (-1)",
- model: "claude-opus-4-6-thinking",
- thinking: &ThinkingConfig{Type: "enabled"},
- wantBudget: -1,
- wantPresent: true,
- },
- {
- name: "enabled with budget uses the provided value",
- model: "claude-opus-4-6-thinking",
- thinking: &ThinkingConfig{Type: "enabled", BudgetTokens: 1024},
- wantBudget: 1024,
- wantPresent: true,
- },
- {
- name: "enabled with -1 budget uses dynamic (-1)",
- model: "claude-opus-4-6-thinking",
- thinking: &ThinkingConfig{Type: "enabled", BudgetTokens: -1},
- wantBudget: -1,
- wantPresent: true,
- },
- {
- name: "adaptive on opus4.6 maps to high budget (24576)",
- model: "claude-opus-4-6-thinking",
- thinking: &ThinkingConfig{Type: "adaptive", BudgetTokens: 20000},
- wantBudget: ClaudeAdaptiveHighThinkingBudgetTokens,
- wantPresent: true,
- },
- {
- name: "adaptive on non-opus model keeps default dynamic (-1)",
- model: "claude-sonnet-4-5-thinking",
- thinking: &ThinkingConfig{Type: "adaptive"},
- wantBudget: -1,
- wantPresent: true,
- },
- {
- name: "disabled does not emit thinkingConfig",
- model: "claude-opus-4-6-thinking",
- thinking: &ThinkingConfig{Type: "disabled", BudgetTokens: 1024},
- wantBudget: 0,
- wantPresent: false,
- },
- {
- name: "nil thinking does not emit thinkingConfig",
- model: "claude-opus-4-6-thinking",
- thinking: nil,
- wantBudget: 0,
- wantPresent: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- req := &ClaudeRequest{
- Model: tt.model,
- Thinking: tt.thinking,
- }
- cfg := buildGenerationConfig(req)
- if cfg == nil {
- t.Fatalf("expected non-nil generationConfig")
- }
- if tt.wantPresent {
- if cfg.ThinkingConfig == nil {
- t.Fatalf("expected thinkingConfig to be present")
- }
- if !cfg.ThinkingConfig.IncludeThoughts {
- t.Fatalf("expected includeThoughts=true")
- }
- if cfg.ThinkingConfig.ThinkingBudget != tt.wantBudget {
- t.Fatalf("expected thinkingBudget=%d, got %d", tt.wantBudget, cfg.ThinkingConfig.ThinkingBudget)
- }
- return
- }
- if cfg.ThinkingConfig != nil {
- t.Fatalf("expected thinkingConfig to be nil, got %+v", cfg.ThinkingConfig)
- }
- })
- }
- }
- func TestTransformClaudeToGeminiWithOptions_PreservesBillingHeaderSystemBlock(t *testing.T) {
- tests := []struct {
- name string
- system json.RawMessage
- }{
- {
- name: "system array",
- system: json.RawMessage(`[{"type":"text","text":"x-anthropic-billing-header keep"}]`),
- },
- {
- name: "system string",
- system: json.RawMessage(`"x-anthropic-billing-header keep"`),
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- claudeReq := &ClaudeRequest{
- Model: "claude-3-5-sonnet-latest",
- System: tt.system,
- Messages: []ClaudeMessage{
- {
- Role: "user",
- Content: json.RawMessage(`[{"type":"text","text":"hello"}]`),
- },
- },
- }
- body, err := TransformClaudeToGeminiWithOptions(claudeReq, "project-1", "gemini-2.5-flash", DefaultTransformOptions())
- require.NoError(t, err)
- var req V1InternalRequest
- require.NoError(t, json.Unmarshal(body, &req))
- require.NotNil(t, req.Request.SystemInstruction)
- found := false
- for _, part := range req.Request.SystemInstruction.Parts {
- if strings.Contains(part.Text, "x-anthropic-billing-header keep") {
- found = true
- break
- }
- }
- require.True(t, found, "转换后的 systemInstruction 应保留 x-anthropic-billing-header 内容")
- })
- }
- }
- func TestTransformClaudeToGeminiWithOptions_MessageRoles(t *testing.T) {
- transform := func(t *testing.T, claudeReq *ClaudeRequest) V1InternalRequest {
- t.Helper()
- body, err := TransformClaudeToGeminiWithOptions(claudeReq, "project-1", "gemini-2.5-flash", DefaultTransformOptions())
- require.NoError(t, err)
- var req V1InternalRequest
- require.NoError(t, json.Unmarshal(body, &req))
- return req
- }
- systemText := func(content *GeminiContent) string {
- if content == nil {
- return ""
- }
- var texts []string
- for _, part := range content.Parts {
- texts = append(texts, part.Text)
- }
- return strings.Join(texts, "\n")
- }
- t.Run("message system role moves to system instruction", func(t *testing.T) {
- req := transform(t, &ClaudeRequest{
- Model: "claude-3-5-sonnet-latest",
- Messages: []ClaudeMessage{
- {
- Role: "system",
- Content: json.RawMessage(`[{"type":"text","text":"skills context"}]`),
- },
- {
- Role: "user",
- Content: json.RawMessage(`"hello"`),
- },
- },
- })
- require.Len(t, req.Request.Contents, 1)
- require.Equal(t, "user", req.Request.Contents[0].Role)
- require.Contains(t, systemText(req.Request.SystemInstruction), "skills context")
- for _, content := range req.Request.Contents {
- require.NotEqual(t, "system", content.Role)
- }
- })
- t.Run("assistant role still maps to model", func(t *testing.T) {
- req := transform(t, &ClaudeRequest{
- Model: "claude-3-5-sonnet-latest",
- Messages: []ClaudeMessage{
- {
- Role: "assistant",
- Content: json.RawMessage(`"hello from assistant"`),
- },
- },
- })
- require.Len(t, req.Request.Contents, 1)
- require.Equal(t, "model", req.Request.Contents[0].Role)
- require.Equal(t, "hello from assistant", req.Request.Contents[0].Parts[0].Text)
- })
- t.Run("top level and message system instructions are merged", func(t *testing.T) {
- req := transform(t, &ClaudeRequest{
- Model: "claude-3-5-sonnet-latest",
- System: json.RawMessage(`"top level system"`),
- Messages: []ClaudeMessage{
- {
- Role: "system",
- Content: json.RawMessage(`"message system"`),
- },
- {
- Role: "user",
- Content: json.RawMessage(`"hello"`),
- },
- },
- })
- mergedSystem := systemText(req.Request.SystemInstruction)
- require.Contains(t, mergedSystem, "top level system")
- require.Contains(t, mergedSystem, "message system")
- require.Less(t, strings.Index(mergedSystem, "top level system"), strings.Index(mergedSystem, "message system"))
- require.Len(t, req.Request.Contents, 1)
- require.Equal(t, "user", req.Request.Contents[0].Role)
- })
- t.Run("ordinary user assistant conversation is unchanged", func(t *testing.T) {
- req := transform(t, &ClaudeRequest{
- Model: "claude-3-5-sonnet-latest",
- Messages: []ClaudeMessage{
- {
- Role: "user",
- Content: json.RawMessage(`"question"`),
- },
- {
- Role: "assistant",
- Content: json.RawMessage(`"answer"`),
- },
- },
- })
- require.Len(t, req.Request.Contents, 2)
- require.Equal(t, "user", req.Request.Contents[0].Role)
- require.Equal(t, "question", req.Request.Contents[0].Parts[0].Text)
- require.Equal(t, "model", req.Request.Contents[1].Role)
- require.Equal(t, "answer", req.Request.Contents[1].Parts[0].Text)
- })
- }
- func TestTransformClaudeToGeminiWithOptions_PreservesWebSearchAlongsideFunctions(t *testing.T) {
- claudeReq := &ClaudeRequest{
- Model: "claude-3-5-sonnet-latest",
- Messages: []ClaudeMessage{
- {
- Role: "user",
- Content: json.RawMessage(`[{"type":"text","text":"hello"}]`),
- },
- },
- Tools: []ClaudeTool{
- {
- Name: "get_weather",
- Description: "Get weather information",
- InputSchema: map[string]any{"type": "object"},
- },
- {
- Type: "web_search_20250305",
- Name: "web_search",
- },
- },
- }
- body, err := TransformClaudeToGeminiWithOptions(claudeReq, "project-1", "gemini-2.5-flash", DefaultTransformOptions())
- require.NoError(t, err)
- var req V1InternalRequest
- require.NoError(t, json.Unmarshal(body, &req))
- require.Len(t, req.Request.Tools, 2)
- require.Len(t, req.Request.Tools[0].FunctionDeclarations, 1)
- require.Equal(t, "get_weather", req.Request.Tools[0].FunctionDeclarations[0].Name)
- require.NotNil(t, req.Request.Tools[1].GoogleSearch)
- }
- func TestGeminiToolConfig_IncludeServerSideToolInvocations(t *testing.T) {
- functionTool := ClaudeTool{
- Name: "get_weather",
- Description: "Get weather information",
- InputSchema: map[string]any{"type": "object"},
- }
- webSearchTool := ClaudeTool{
- Type: "web_search_20250305",
- Name: "web_search",
- }
- transform := func(t *testing.T, tools []ClaudeTool) (V1InternalRequest, string) {
- t.Helper()
- body, err := TransformClaudeToGeminiWithOptions(&ClaudeRequest{
- Model: "claude-3-5-sonnet-latest",
- Messages: []ClaudeMessage{
- {
- Role: "user",
- Content: json.RawMessage(`[{"type":"text","text":"hello"}]`),
- },
- },
- Tools: tools,
- }, "project-1", "gemini-2.5-flash", DefaultTransformOptions())
- require.NoError(t, err)
- var req V1InternalRequest
- require.NoError(t, json.Unmarshal(body, &req))
- return req, string(body)
- }
- t.Run("mixed builtin and function tools enable server-side tool invocations", func(t *testing.T) {
- req, raw := transform(t, []ClaudeTool{functionTool, webSearchTool})
- require.NotNil(t, req.Request.ToolConfig)
- require.NotNil(t, req.Request.ToolConfig.IncludeServerSideToolInvocations)
- require.True(t, *req.Request.ToolConfig.IncludeServerSideToolInvocations)
- require.Contains(t, raw, `"includeServerSideToolInvocations":true`)
- })
- t.Run("function tools only leave the flag unset", func(t *testing.T) {
- req, raw := transform(t, []ClaudeTool{functionTool})
- require.NotNil(t, req.Request.ToolConfig)
- require.Nil(t, req.Request.ToolConfig.IncludeServerSideToolInvocations)
- require.NotContains(t, raw, "includeServerSideToolInvocations")
- })
- t.Run("web search only leaves the flag unset", func(t *testing.T) {
- req, raw := transform(t, []ClaudeTool{webSearchTool})
- require.NotNil(t, req.Request.ToolConfig)
- require.Nil(t, req.Request.ToolConfig.IncludeServerSideToolInvocations)
- require.NotContains(t, raw, "includeServerSideToolInvocations")
- })
- }
- // TestToolConfigAlwaysPresent ensures toolConfig is always emitted, including for
- // reasoning models without any tools: upstream rejects requests that omit it.
- func TestToolConfigAlwaysPresent(t *testing.T) {
- cases := []struct {
- name string
- model string
- tools []ClaudeTool
- }{
- {name: "reasoning model without tools", model: "gemini-3.1-pro-high"},
- {name: "reasoning model with tools", model: "gemini-3.1-pro-high", tools: []ClaudeTool{{
- Name: "web_search",
- Description: "Search the web",
- InputSchema: map[string]any{"type": "object", "properties": map[string]any{}},
- }}},
- {name: "non-reasoning model without tools", model: "gemini-3.1-pro"},
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- claudeReq := &ClaudeRequest{
- Model: tc.model,
- Messages: []ClaudeMessage{{Role: "user", Content: json.RawMessage(`"Hello"`)}},
- Tools: tc.tools,
- }
- body, err := TransformClaudeToGeminiWithOptions(claudeReq, "project-1", tc.model, DefaultTransformOptions())
- require.NoError(t, err)
- var req V1InternalRequest
- require.NoError(t, json.Unmarshal(body, &req))
- require.NotNil(t, req.Request.ToolConfig, "toolConfig must be present")
- require.NotNil(t, req.Request.ToolConfig.FunctionCallingConfig)
- require.Equal(t, "VALIDATED", req.Request.ToolConfig.FunctionCallingConfig.Mode)
- })
- }
- }
|