anthropic-tool-name-normalization.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { Type } from "typebox";
  2. import { describe, expect, it } from "vitest";
  3. import { getModel } from "../src/models.ts";
  4. import { stream } from "../src/stream.ts";
  5. import type { Context, Tool } from "../src/types.ts";
  6. import { resolveApiKey } from "./oauth.ts";
  7. const oauthToken = await resolveApiKey("anthropic");
  8. /**
  9. * Tests for Anthropic OAuth tool name normalization.
  10. *
  11. * When using Claude Code OAuth, tool names must match CC's canonical casing.
  12. * The normalization should:
  13. * 1. Convert tool names that match CC tools (case-insensitive) to CC casing on outbound
  14. * 2. Convert tool names back to the original casing on inbound
  15. *
  16. * This is a simple case-insensitive lookup, NOT a mapping of different names.
  17. * e.g., "todowrite" -> "TodoWrite" -> "todowrite" (round-trip works)
  18. *
  19. * The old `find -> Glob` mapping was WRONG because:
  20. * - Outbound: "find" -> "Glob"
  21. * - Inbound: "Glob" -> ??? (no tool named "glob" in context.tools, only "find")
  22. * - Result: tool call has name "Glob" but no tool exists with that name
  23. */
  24. describe.skipIf(!oauthToken)("Anthropic OAuth tool name normalization", () => {
  25. const model = getModel("anthropic", "claude-sonnet-4-6");
  26. it("should normalize user-defined tool matching CC name (todowrite -> TodoWrite -> todowrite)", async () => {
  27. // User defines a tool named "todowrite" (lowercase)
  28. // CC has "TodoWrite" - this should round-trip correctly
  29. const todoTool: Tool = {
  30. name: "todowrite",
  31. description: "Write a todo item",
  32. parameters: Type.Object({
  33. task: Type.String({ description: "The task to add" }),
  34. }),
  35. };
  36. const context: Context = {
  37. systemPrompt: "You are a helpful assistant. Use the todowrite tool when asked to add todos.",
  38. messages: [
  39. {
  40. role: "user",
  41. content: "Add a todo: buy milk. Use the todowrite tool.",
  42. timestamp: Date.now(),
  43. },
  44. ],
  45. tools: [todoTool],
  46. };
  47. const s = stream(model, context, { apiKey: oauthToken });
  48. let toolCallName: string | undefined;
  49. for await (const event of s) {
  50. if (event.type === "toolcall_end") {
  51. const toolCall = event.partial.content[event.contentIndex];
  52. if (toolCall.type === "toolCall") {
  53. toolCallName = toolCall.name;
  54. }
  55. }
  56. }
  57. const response = await s.result();
  58. expect(response.stopReason, `Error: ${response.errorMessage}`).toBe("toolUse");
  59. // The tool call should come back with the ORIGINAL name "todowrite", not "TodoWrite"
  60. expect(toolCallName).toBe("todowrite");
  61. });
  62. it("should handle pi's built-in tools (read, write, edit, bash)", async () => {
  63. // Pi's tools use lowercase names, CC uses PascalCase
  64. const readTool: Tool = {
  65. name: "read",
  66. description: "Read a file",
  67. parameters: Type.Object({
  68. path: Type.String({ description: "File path" }),
  69. }),
  70. };
  71. const context: Context = {
  72. systemPrompt: "You are a helpful assistant. Use the read tool to read files.",
  73. messages: [
  74. {
  75. role: "user",
  76. content: "Read the file /tmp/test.txt using the read tool.",
  77. timestamp: Date.now(),
  78. },
  79. ],
  80. tools: [readTool],
  81. };
  82. const s = stream(model, context, { apiKey: oauthToken });
  83. let toolCallName: string | undefined;
  84. for await (const event of s) {
  85. if (event.type === "toolcall_end") {
  86. const toolCall = event.partial.content[event.contentIndex];
  87. if (toolCall.type === "toolCall") {
  88. toolCallName = toolCall.name;
  89. }
  90. }
  91. }
  92. const response = await s.result();
  93. expect(response.stopReason, `Error: ${response.errorMessage}`).toBe("toolUse");
  94. // The tool call should come back with the ORIGINAL name "read", not "Read"
  95. expect(toolCallName).toBe("read");
  96. });
  97. it("should NOT map find to Glob - find is not a CC tool name", async () => {
  98. // Pi has a "find" tool, CC has "Glob" - these are DIFFERENT tools
  99. // The old code incorrectly mapped find -> Glob, which broke the round-trip
  100. // because there's no tool named "glob" in context.tools
  101. const findTool: Tool = {
  102. name: "find",
  103. description: "Find files by pattern",
  104. parameters: Type.Object({
  105. pattern: Type.String({ description: "Glob pattern" }),
  106. }),
  107. };
  108. const context: Context = {
  109. systemPrompt: "You are a helpful assistant. Use the find tool to search for files.",
  110. messages: [
  111. {
  112. role: "user",
  113. content: "Find all .ts files using the find tool.",
  114. timestamp: Date.now(),
  115. },
  116. ],
  117. tools: [findTool],
  118. };
  119. const s = stream(model, context, { apiKey: oauthToken });
  120. let toolCallName: string | undefined;
  121. for await (const event of s) {
  122. if (event.type === "toolcall_end") {
  123. const toolCall = event.partial.content[event.contentIndex];
  124. if (toolCall.type === "toolCall") {
  125. toolCallName = toolCall.name;
  126. }
  127. }
  128. }
  129. const response = await s.result();
  130. expect(response.stopReason, `Error: ${response.errorMessage}`).toBe("toolUse");
  131. // With the BROKEN find -> Glob mapping:
  132. // - Sent as "Glob" to Anthropic
  133. // - Received back as "Glob"
  134. // - fromClaudeCodeName("Glob", tools) looks for tool.name.toLowerCase() === "glob"
  135. // - No match (tool is named "find"), returns "Glob"
  136. // - Test fails: toolCallName is "Glob" instead of "find"
  137. //
  138. // With the CORRECT implementation (no find->Glob mapping):
  139. // - Sent as "find" to Anthropic (no CC tool named "Find")
  140. // - Received back as "find"
  141. // - Test passes: toolCallName is "find"
  142. expect(toolCallName).toBe("find");
  143. });
  144. it("should handle custom tools that don't match any CC tool names", async () => {
  145. // A completely custom tool should pass through unchanged
  146. const customTool: Tool = {
  147. name: "my_custom_tool",
  148. description: "A custom tool",
  149. parameters: Type.Object({
  150. input: Type.String({ description: "Input value" }),
  151. }),
  152. };
  153. const context: Context = {
  154. systemPrompt: "You are a helpful assistant. Use my_custom_tool when asked.",
  155. messages: [
  156. {
  157. role: "user",
  158. content: "Use my_custom_tool with input 'hello'.",
  159. timestamp: Date.now(),
  160. },
  161. ],
  162. tools: [customTool],
  163. };
  164. const s = stream(model, context, { apiKey: oauthToken });
  165. let toolCallName: string | undefined;
  166. for await (const event of s) {
  167. if (event.type === "toolcall_end") {
  168. const toolCall = event.partial.content[event.contentIndex];
  169. if (toolCall.type === "toolCall") {
  170. toolCallName = toolCall.name;
  171. }
  172. }
  173. }
  174. const response = await s.result();
  175. expect(response.stopReason, `Error: ${response.errorMessage}`).toBe("toolUse");
  176. // Custom tool names should pass through unchanged
  177. expect(toolCallName).toBe("my_custom_tool");
  178. });
  179. });