request_transformer_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. package antigravity
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "testing"
  6. "github.com/stretchr/testify/require"
  7. )
  8. // TestBuildParts_ThinkingBlockWithoutSignature 测试thinking block无signature时的处理
  9. func TestBuildParts_ThinkingBlockWithoutSignature(t *testing.T) {
  10. tests := []struct {
  11. name string
  12. content string
  13. allowDummyThought bool
  14. expectedParts int
  15. description string
  16. }{
  17. {
  18. name: "Claude model - downgrade thinking to text without signature",
  19. content: `[
  20. {"type": "text", "text": "Hello"},
  21. {"type": "thinking", "thinking": "Let me think...", "signature": ""},
  22. {"type": "text", "text": "World"}
  23. ]`,
  24. allowDummyThought: false,
  25. expectedParts: 3, // thinking 内容降级为普通 text part
  26. description: "Claude模型缺少signature时应将thinking降级为text,并在上层禁用thinking mode",
  27. },
  28. {
  29. name: "Claude model - preserve thinking block with signature",
  30. content: `[
  31. {"type": "text", "text": "Hello"},
  32. {"type": "thinking", "thinking": "Let me think...", "signature": "sig_real_123"},
  33. {"type": "text", "text": "World"}
  34. ]`,
  35. allowDummyThought: false,
  36. expectedParts: 3,
  37. description: "Claude模型应透传带 signature 的 thinking block(用于 Vertex 签名链路)",
  38. },
  39. {
  40. name: "Gemini model - use dummy signature",
  41. content: `[
  42. {"type": "text", "text": "Hello"},
  43. {"type": "thinking", "thinking": "Let me think...", "signature": ""},
  44. {"type": "text", "text": "World"}
  45. ]`,
  46. allowDummyThought: true,
  47. expectedParts: 3, // 三个block都保留,thinking使用dummy signature
  48. description: "Gemini模型应该为无signature的thinking block使用dummy signature",
  49. },
  50. }
  51. for _, tt := range tests {
  52. t.Run(tt.name, func(t *testing.T) {
  53. toolIDToName := make(map[string]string)
  54. parts, _, err := buildParts(json.RawMessage(tt.content), toolIDToName, tt.allowDummyThought)
  55. if err != nil {
  56. t.Fatalf("buildParts() error = %v", err)
  57. }
  58. if len(parts) != tt.expectedParts {
  59. t.Errorf("%s: got %d parts, want %d parts", tt.description, len(parts), tt.expectedParts)
  60. }
  61. switch tt.name {
  62. case "Claude model - preserve thinking block with signature":
  63. if len(parts) != 3 {
  64. t.Fatalf("expected 3 parts, got %d", len(parts))
  65. }
  66. if !parts[1].Thought || parts[1].ThoughtSignature != "sig_real_123" {
  67. t.Fatalf("expected thought part with signature sig_real_123, got thought=%v signature=%q",
  68. parts[1].Thought, parts[1].ThoughtSignature)
  69. }
  70. case "Claude model - downgrade thinking to text without signature":
  71. if len(parts) != 3 {
  72. t.Fatalf("expected 3 parts, got %d", len(parts))
  73. }
  74. if parts[1].Thought {
  75. t.Fatalf("expected downgraded text part, got thought=%v signature=%q",
  76. parts[1].Thought, parts[1].ThoughtSignature)
  77. }
  78. if parts[1].Text != "Let me think..." {
  79. t.Fatalf("expected downgraded text %q, got %q", "Let me think...", parts[1].Text)
  80. }
  81. case "Gemini model - use dummy signature":
  82. if len(parts) != 3 {
  83. t.Fatalf("expected 3 parts, got %d", len(parts))
  84. }
  85. if !parts[1].Thought || parts[1].ThoughtSignature != DummyThoughtSignature {
  86. t.Fatalf("expected dummy thought signature, got thought=%v signature=%q",
  87. parts[1].Thought, parts[1].ThoughtSignature)
  88. }
  89. }
  90. })
  91. }
  92. }
  93. func TestBuildParts_ToolUseSignatureHandling(t *testing.T) {
  94. content := `[
  95. {"type": "tool_use", "id": "t1", "name": "Bash", "input": {"command": "ls"}, "signature": "sig_tool_abc"}
  96. ]`
  97. t.Run("Gemini preserves provided tool_use signature", func(t *testing.T) {
  98. toolIDToName := make(map[string]string)
  99. parts, _, err := buildParts(json.RawMessage(content), toolIDToName, true)
  100. if err != nil {
  101. t.Fatalf("buildParts() error = %v", err)
  102. }
  103. if len(parts) != 1 || parts[0].FunctionCall == nil {
  104. t.Fatalf("expected 1 functionCall part, got %+v", parts)
  105. }
  106. if parts[0].ThoughtSignature != "sig_tool_abc" {
  107. t.Fatalf("expected preserved tool signature %q, got %q", "sig_tool_abc", parts[0].ThoughtSignature)
  108. }
  109. })
  110. t.Run("Gemini falls back to dummy tool_use signature when missing", func(t *testing.T) {
  111. contentNoSig := `[
  112. {"type": "tool_use", "id": "t1", "name": "Bash", "input": {"command": "ls"}}
  113. ]`
  114. toolIDToName := make(map[string]string)
  115. parts, _, err := buildParts(json.RawMessage(contentNoSig), toolIDToName, true)
  116. if err != nil {
  117. t.Fatalf("buildParts() error = %v", err)
  118. }
  119. if len(parts) != 1 || parts[0].FunctionCall == nil {
  120. t.Fatalf("expected 1 functionCall part, got %+v", parts)
  121. }
  122. if parts[0].ThoughtSignature != DummyThoughtSignature {
  123. t.Fatalf("expected dummy tool signature %q, got %q", DummyThoughtSignature, parts[0].ThoughtSignature)
  124. }
  125. })
  126. t.Run("Claude model - preserve valid signature for tool_use", func(t *testing.T) {
  127. toolIDToName := make(map[string]string)
  128. parts, _, err := buildParts(json.RawMessage(content), toolIDToName, false)
  129. if err != nil {
  130. t.Fatalf("buildParts() error = %v", err)
  131. }
  132. if len(parts) != 1 || parts[0].FunctionCall == nil {
  133. t.Fatalf("expected 1 functionCall part, got %+v", parts)
  134. }
  135. // Claude 模型应透传有效的 signature(Vertex/Google 需要完整签名链路)
  136. if parts[0].ThoughtSignature != "sig_tool_abc" {
  137. t.Fatalf("expected preserved tool signature %q, got %q", "sig_tool_abc", parts[0].ThoughtSignature)
  138. }
  139. })
  140. }
  141. // TestBuildTools_CustomTypeTools 测试custom类型工具转换
  142. func TestBuildTools_CustomTypeTools(t *testing.T) {
  143. tests := []struct {
  144. name string
  145. tools []ClaudeTool
  146. expectedLen int
  147. description string
  148. }{
  149. {
  150. name: "Standard tool format",
  151. tools: []ClaudeTool{
  152. {
  153. Name: "get_weather",
  154. Description: "Get weather information",
  155. InputSchema: map[string]any{
  156. "type": "object",
  157. "properties": map[string]any{
  158. "location": map[string]any{"type": "string"},
  159. },
  160. },
  161. },
  162. },
  163. expectedLen: 1,
  164. description: "标准工具格式应该正常转换",
  165. },
  166. {
  167. name: "Custom type tool (MCP format)",
  168. tools: []ClaudeTool{
  169. {
  170. Type: "custom",
  171. Name: "mcp_tool",
  172. Custom: &ClaudeCustomToolSpec{
  173. Description: "MCP tool description",
  174. InputSchema: map[string]any{
  175. "type": "object",
  176. "properties": map[string]any{
  177. "param": map[string]any{"type": "string"},
  178. },
  179. },
  180. },
  181. },
  182. },
  183. expectedLen: 1,
  184. description: "Custom类型工具应该从Custom字段读取description和input_schema",
  185. },
  186. {
  187. name: "Mixed standard and custom tools",
  188. tools: []ClaudeTool{
  189. {
  190. Name: "standard_tool",
  191. Description: "Standard tool",
  192. InputSchema: map[string]any{"type": "object"},
  193. },
  194. {
  195. Type: "custom",
  196. Name: "custom_tool",
  197. Custom: &ClaudeCustomToolSpec{
  198. Description: "Custom tool",
  199. InputSchema: map[string]any{"type": "object"},
  200. },
  201. },
  202. },
  203. expectedLen: 1, // 返回一个GeminiToolDeclaration,包含2个function declarations
  204. description: "混合标准和custom工具应该都能正确转换",
  205. },
  206. {
  207. name: "Invalid custom tool - nil Custom field",
  208. tools: []ClaudeTool{
  209. {
  210. Type: "custom",
  211. Name: "invalid_custom",
  212. // Custom 为 nil
  213. },
  214. },
  215. expectedLen: 0, // 应该被跳过
  216. description: "Custom字段为nil的custom工具应该被跳过",
  217. },
  218. {
  219. name: "Invalid custom tool - nil InputSchema",
  220. tools: []ClaudeTool{
  221. {
  222. Type: "custom",
  223. Name: "invalid_custom",
  224. Custom: &ClaudeCustomToolSpec{
  225. Description: "Invalid",
  226. // InputSchema 为 nil
  227. },
  228. },
  229. },
  230. expectedLen: 0, // 应该被跳过
  231. description: "InputSchema为nil的custom工具应该被跳过",
  232. },
  233. }
  234. for _, tt := range tests {
  235. t.Run(tt.name, func(t *testing.T) {
  236. result := buildTools(tt.tools)
  237. if len(result) != tt.expectedLen {
  238. t.Errorf("%s: got %d tool declarations, want %d", tt.description, len(result), tt.expectedLen)
  239. }
  240. // 验证function declarations存在
  241. if len(result) > 0 && result[0].FunctionDeclarations != nil {
  242. if len(result[0].FunctionDeclarations) != len(tt.tools) {
  243. t.Errorf("%s: got %d function declarations, want %d",
  244. tt.description, len(result[0].FunctionDeclarations), len(tt.tools))
  245. }
  246. }
  247. })
  248. }
  249. }
  250. func TestBuildTools_PreservesWebSearchAlongsideFunctions(t *testing.T) {
  251. tools := []ClaudeTool{
  252. {
  253. Name: "get_weather",
  254. Description: "Get weather information",
  255. InputSchema: map[string]any{"type": "object"},
  256. },
  257. {
  258. Type: "web_search_20250305",
  259. Name: "web_search",
  260. },
  261. }
  262. result := buildTools(tools)
  263. require.Len(t, result, 2)
  264. require.Len(t, result[0].FunctionDeclarations, 1)
  265. require.Equal(t, "get_weather", result[0].FunctionDeclarations[0].Name)
  266. require.NotNil(t, result[1].GoogleSearch)
  267. require.NotNil(t, result[1].GoogleSearch.EnhancedContent)
  268. require.NotNil(t, result[1].GoogleSearch.EnhancedContent.ImageSearch)
  269. require.Equal(t, 5, result[1].GoogleSearch.EnhancedContent.ImageSearch.MaxResultCount)
  270. }
  271. func TestBuildGenerationConfig_ThinkingDynamicBudget(t *testing.T) {
  272. tests := []struct {
  273. name string
  274. model string
  275. thinking *ThinkingConfig
  276. wantBudget int
  277. wantPresent bool
  278. }{
  279. {
  280. name: "enabled without budget defaults to dynamic (-1)",
  281. model: "claude-opus-4-6-thinking",
  282. thinking: &ThinkingConfig{Type: "enabled"},
  283. wantBudget: -1,
  284. wantPresent: true,
  285. },
  286. {
  287. name: "enabled with budget uses the provided value",
  288. model: "claude-opus-4-6-thinking",
  289. thinking: &ThinkingConfig{Type: "enabled", BudgetTokens: 1024},
  290. wantBudget: 1024,
  291. wantPresent: true,
  292. },
  293. {
  294. name: "enabled with -1 budget uses dynamic (-1)",
  295. model: "claude-opus-4-6-thinking",
  296. thinking: &ThinkingConfig{Type: "enabled", BudgetTokens: -1},
  297. wantBudget: -1,
  298. wantPresent: true,
  299. },
  300. {
  301. name: "adaptive on opus4.6 maps to high budget (24576)",
  302. model: "claude-opus-4-6-thinking",
  303. thinking: &ThinkingConfig{Type: "adaptive", BudgetTokens: 20000},
  304. wantBudget: ClaudeAdaptiveHighThinkingBudgetTokens,
  305. wantPresent: true,
  306. },
  307. {
  308. name: "adaptive on non-opus model keeps default dynamic (-1)",
  309. model: "claude-sonnet-4-5-thinking",
  310. thinking: &ThinkingConfig{Type: "adaptive"},
  311. wantBudget: -1,
  312. wantPresent: true,
  313. },
  314. {
  315. name: "disabled does not emit thinkingConfig",
  316. model: "claude-opus-4-6-thinking",
  317. thinking: &ThinkingConfig{Type: "disabled", BudgetTokens: 1024},
  318. wantBudget: 0,
  319. wantPresent: false,
  320. },
  321. {
  322. name: "nil thinking does not emit thinkingConfig",
  323. model: "claude-opus-4-6-thinking",
  324. thinking: nil,
  325. wantBudget: 0,
  326. wantPresent: false,
  327. },
  328. }
  329. for _, tt := range tests {
  330. t.Run(tt.name, func(t *testing.T) {
  331. req := &ClaudeRequest{
  332. Model: tt.model,
  333. Thinking: tt.thinking,
  334. }
  335. cfg := buildGenerationConfig(req)
  336. if cfg == nil {
  337. t.Fatalf("expected non-nil generationConfig")
  338. }
  339. if tt.wantPresent {
  340. if cfg.ThinkingConfig == nil {
  341. t.Fatalf("expected thinkingConfig to be present")
  342. }
  343. if !cfg.ThinkingConfig.IncludeThoughts {
  344. t.Fatalf("expected includeThoughts=true")
  345. }
  346. if cfg.ThinkingConfig.ThinkingBudget != tt.wantBudget {
  347. t.Fatalf("expected thinkingBudget=%d, got %d", tt.wantBudget, cfg.ThinkingConfig.ThinkingBudget)
  348. }
  349. return
  350. }
  351. if cfg.ThinkingConfig != nil {
  352. t.Fatalf("expected thinkingConfig to be nil, got %+v", cfg.ThinkingConfig)
  353. }
  354. })
  355. }
  356. }
  357. func TestTransformClaudeToGeminiWithOptions_PreservesBillingHeaderSystemBlock(t *testing.T) {
  358. tests := []struct {
  359. name string
  360. system json.RawMessage
  361. }{
  362. {
  363. name: "system array",
  364. system: json.RawMessage(`[{"type":"text","text":"x-anthropic-billing-header keep"}]`),
  365. },
  366. {
  367. name: "system string",
  368. system: json.RawMessage(`"x-anthropic-billing-header keep"`),
  369. },
  370. }
  371. for _, tt := range tests {
  372. t.Run(tt.name, func(t *testing.T) {
  373. claudeReq := &ClaudeRequest{
  374. Model: "claude-3-5-sonnet-latest",
  375. System: tt.system,
  376. Messages: []ClaudeMessage{
  377. {
  378. Role: "user",
  379. Content: json.RawMessage(`[{"type":"text","text":"hello"}]`),
  380. },
  381. },
  382. }
  383. body, err := TransformClaudeToGeminiWithOptions(claudeReq, "project-1", "gemini-2.5-flash", DefaultTransformOptions())
  384. require.NoError(t, err)
  385. var req V1InternalRequest
  386. require.NoError(t, json.Unmarshal(body, &req))
  387. require.NotNil(t, req.Request.SystemInstruction)
  388. found := false
  389. for _, part := range req.Request.SystemInstruction.Parts {
  390. if strings.Contains(part.Text, "x-anthropic-billing-header keep") {
  391. found = true
  392. break
  393. }
  394. }
  395. require.True(t, found, "转换后的 systemInstruction 应保留 x-anthropic-billing-header 内容")
  396. })
  397. }
  398. }
  399. func TestTransformClaudeToGeminiWithOptions_MessageRoles(t *testing.T) {
  400. transform := func(t *testing.T, claudeReq *ClaudeRequest) V1InternalRequest {
  401. t.Helper()
  402. body, err := TransformClaudeToGeminiWithOptions(claudeReq, "project-1", "gemini-2.5-flash", DefaultTransformOptions())
  403. require.NoError(t, err)
  404. var req V1InternalRequest
  405. require.NoError(t, json.Unmarshal(body, &req))
  406. return req
  407. }
  408. systemText := func(content *GeminiContent) string {
  409. if content == nil {
  410. return ""
  411. }
  412. var texts []string
  413. for _, part := range content.Parts {
  414. texts = append(texts, part.Text)
  415. }
  416. return strings.Join(texts, "\n")
  417. }
  418. t.Run("message system role moves to system instruction", func(t *testing.T) {
  419. req := transform(t, &ClaudeRequest{
  420. Model: "claude-3-5-sonnet-latest",
  421. Messages: []ClaudeMessage{
  422. {
  423. Role: "system",
  424. Content: json.RawMessage(`[{"type":"text","text":"skills context"}]`),
  425. },
  426. {
  427. Role: "user",
  428. Content: json.RawMessage(`"hello"`),
  429. },
  430. },
  431. })
  432. require.Len(t, req.Request.Contents, 1)
  433. require.Equal(t, "user", req.Request.Contents[0].Role)
  434. require.Contains(t, systemText(req.Request.SystemInstruction), "skills context")
  435. for _, content := range req.Request.Contents {
  436. require.NotEqual(t, "system", content.Role)
  437. }
  438. })
  439. t.Run("assistant role still maps to model", func(t *testing.T) {
  440. req := transform(t, &ClaudeRequest{
  441. Model: "claude-3-5-sonnet-latest",
  442. Messages: []ClaudeMessage{
  443. {
  444. Role: "assistant",
  445. Content: json.RawMessage(`"hello from assistant"`),
  446. },
  447. },
  448. })
  449. require.Len(t, req.Request.Contents, 1)
  450. require.Equal(t, "model", req.Request.Contents[0].Role)
  451. require.Equal(t, "hello from assistant", req.Request.Contents[0].Parts[0].Text)
  452. })
  453. t.Run("top level and message system instructions are merged", func(t *testing.T) {
  454. req := transform(t, &ClaudeRequest{
  455. Model: "claude-3-5-sonnet-latest",
  456. System: json.RawMessage(`"top level system"`),
  457. Messages: []ClaudeMessage{
  458. {
  459. Role: "system",
  460. Content: json.RawMessage(`"message system"`),
  461. },
  462. {
  463. Role: "user",
  464. Content: json.RawMessage(`"hello"`),
  465. },
  466. },
  467. })
  468. mergedSystem := systemText(req.Request.SystemInstruction)
  469. require.Contains(t, mergedSystem, "top level system")
  470. require.Contains(t, mergedSystem, "message system")
  471. require.Less(t, strings.Index(mergedSystem, "top level system"), strings.Index(mergedSystem, "message system"))
  472. require.Len(t, req.Request.Contents, 1)
  473. require.Equal(t, "user", req.Request.Contents[0].Role)
  474. })
  475. t.Run("ordinary user assistant conversation is unchanged", func(t *testing.T) {
  476. req := transform(t, &ClaudeRequest{
  477. Model: "claude-3-5-sonnet-latest",
  478. Messages: []ClaudeMessage{
  479. {
  480. Role: "user",
  481. Content: json.RawMessage(`"question"`),
  482. },
  483. {
  484. Role: "assistant",
  485. Content: json.RawMessage(`"answer"`),
  486. },
  487. },
  488. })
  489. require.Len(t, req.Request.Contents, 2)
  490. require.Equal(t, "user", req.Request.Contents[0].Role)
  491. require.Equal(t, "question", req.Request.Contents[0].Parts[0].Text)
  492. require.Equal(t, "model", req.Request.Contents[1].Role)
  493. require.Equal(t, "answer", req.Request.Contents[1].Parts[0].Text)
  494. })
  495. }
  496. func TestTransformClaudeToGeminiWithOptions_PreservesWebSearchAlongsideFunctions(t *testing.T) {
  497. claudeReq := &ClaudeRequest{
  498. Model: "claude-3-5-sonnet-latest",
  499. Messages: []ClaudeMessage{
  500. {
  501. Role: "user",
  502. Content: json.RawMessage(`[{"type":"text","text":"hello"}]`),
  503. },
  504. },
  505. Tools: []ClaudeTool{
  506. {
  507. Name: "get_weather",
  508. Description: "Get weather information",
  509. InputSchema: map[string]any{"type": "object"},
  510. },
  511. {
  512. Type: "web_search_20250305",
  513. Name: "web_search",
  514. },
  515. },
  516. }
  517. body, err := TransformClaudeToGeminiWithOptions(claudeReq, "project-1", "gemini-2.5-flash", DefaultTransformOptions())
  518. require.NoError(t, err)
  519. var req V1InternalRequest
  520. require.NoError(t, json.Unmarshal(body, &req))
  521. require.Len(t, req.Request.Tools, 2)
  522. require.Len(t, req.Request.Tools[0].FunctionDeclarations, 1)
  523. require.Equal(t, "get_weather", req.Request.Tools[0].FunctionDeclarations[0].Name)
  524. require.NotNil(t, req.Request.Tools[1].GoogleSearch)
  525. }
  526. func TestGeminiToolConfig_IncludeServerSideToolInvocations(t *testing.T) {
  527. functionTool := ClaudeTool{
  528. Name: "get_weather",
  529. Description: "Get weather information",
  530. InputSchema: map[string]any{"type": "object"},
  531. }
  532. webSearchTool := ClaudeTool{
  533. Type: "web_search_20250305",
  534. Name: "web_search",
  535. }
  536. transform := func(t *testing.T, tools []ClaudeTool) (V1InternalRequest, string) {
  537. t.Helper()
  538. body, err := TransformClaudeToGeminiWithOptions(&ClaudeRequest{
  539. Model: "claude-3-5-sonnet-latest",
  540. Messages: []ClaudeMessage{
  541. {
  542. Role: "user",
  543. Content: json.RawMessage(`[{"type":"text","text":"hello"}]`),
  544. },
  545. },
  546. Tools: tools,
  547. }, "project-1", "gemini-2.5-flash", DefaultTransformOptions())
  548. require.NoError(t, err)
  549. var req V1InternalRequest
  550. require.NoError(t, json.Unmarshal(body, &req))
  551. return req, string(body)
  552. }
  553. t.Run("mixed builtin and function tools enable server-side tool invocations", func(t *testing.T) {
  554. req, raw := transform(t, []ClaudeTool{functionTool, webSearchTool})
  555. require.NotNil(t, req.Request.ToolConfig)
  556. require.NotNil(t, req.Request.ToolConfig.IncludeServerSideToolInvocations)
  557. require.True(t, *req.Request.ToolConfig.IncludeServerSideToolInvocations)
  558. require.Contains(t, raw, `"includeServerSideToolInvocations":true`)
  559. })
  560. t.Run("function tools only leave the flag unset", func(t *testing.T) {
  561. req, raw := transform(t, []ClaudeTool{functionTool})
  562. require.NotNil(t, req.Request.ToolConfig)
  563. require.Nil(t, req.Request.ToolConfig.IncludeServerSideToolInvocations)
  564. require.NotContains(t, raw, "includeServerSideToolInvocations")
  565. })
  566. t.Run("web search only leaves the flag unset", func(t *testing.T) {
  567. req, raw := transform(t, []ClaudeTool{webSearchTool})
  568. require.NotNil(t, req.Request.ToolConfig)
  569. require.Nil(t, req.Request.ToolConfig.IncludeServerSideToolInvocations)
  570. require.NotContains(t, raw, "includeServerSideToolInvocations")
  571. })
  572. }
  573. // TestToolConfigAlwaysPresent ensures toolConfig is always emitted, including for
  574. // reasoning models without any tools: upstream rejects requests that omit it.
  575. func TestToolConfigAlwaysPresent(t *testing.T) {
  576. cases := []struct {
  577. name string
  578. model string
  579. tools []ClaudeTool
  580. }{
  581. {name: "reasoning model without tools", model: "gemini-3.1-pro-high"},
  582. {name: "reasoning model with tools", model: "gemini-3.1-pro-high", tools: []ClaudeTool{{
  583. Name: "web_search",
  584. Description: "Search the web",
  585. InputSchema: map[string]any{"type": "object", "properties": map[string]any{}},
  586. }}},
  587. {name: "non-reasoning model without tools", model: "gemini-3.1-pro"},
  588. }
  589. for _, tc := range cases {
  590. t.Run(tc.name, func(t *testing.T) {
  591. claudeReq := &ClaudeRequest{
  592. Model: tc.model,
  593. Messages: []ClaudeMessage{{Role: "user", Content: json.RawMessage(`"Hello"`)}},
  594. Tools: tc.tools,
  595. }
  596. body, err := TransformClaudeToGeminiWithOptions(claudeReq, "project-1", tc.model, DefaultTransformOptions())
  597. require.NoError(t, err)
  598. var req V1InternalRequest
  599. require.NoError(t, json.Unmarshal(body, &req))
  600. require.NotNil(t, req.Request.ToolConfig, "toolConfig must be present")
  601. require.NotNil(t, req.Request.ToolConfig.FunctionCallingConfig)
  602. require.Equal(t, "VALIDATED", req.Request.ToolConfig.FunctionCallingConfig.Mode)
  603. })
  604. }
  605. }