tool-call-id-normalization.test.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /**
  2. * Tool Call ID Normalization Tests
  3. *
  4. * Tests that tool call IDs from OpenAI Responses API (github-copilot, openai-codex, opencode)
  5. * are properly normalized when sent to other providers.
  6. *
  7. * OpenAI Responses API generates IDs in format: {call_id}|{id}
  8. * where {id} can be 400+ chars with special characters (+, /, =).
  9. *
  10. * Regression test for: https://github.com/earendil-works/pi-mono/issues/1022
  11. */
  12. import { Type } from "typebox";
  13. import { describe, expect, it } from "vitest";
  14. import { getModel } from "../src/models.ts";
  15. import { completeSimple, getEnvApiKey } from "../src/stream.ts";
  16. import type { AssistantMessage, Message, Tool, ToolResultMessage } from "../src/types.ts";
  17. import { resolveApiKey } from "./oauth.ts";
  18. // Resolve API keys
  19. const copilotToken = await resolveApiKey("github-copilot");
  20. const openrouterKey = getEnvApiKey("openrouter");
  21. const codexToken = await resolveApiKey("openai-codex");
  22. // Simple echo tool for testing
  23. const echoToolSchema = Type.Object({
  24. message: Type.String({ description: "Message to echo back" }),
  25. });
  26. const echoTool: Tool<typeof echoToolSchema> = {
  27. name: "echo",
  28. description: "Echoes the message back",
  29. parameters: echoToolSchema,
  30. };
  31. /**
  32. * Test 1: Live cross-provider handoff
  33. *
  34. * 1. Use github-copilot gpt-5.2-codex to generate a tool call
  35. * 2. Switch to openrouter openai/gpt-5.2-codex and complete
  36. * 3. Switch to openai-codex gpt-5.5 and complete
  37. *
  38. * Both should succeed without "call_id too long" errors.
  39. */
  40. describe("Tool Call ID Normalization - Live Handoff", () => {
  41. it.skipIf(!copilotToken || !openrouterKey)(
  42. "github-copilot -> openrouter should normalize pipe-separated IDs",
  43. async () => {
  44. const copilotModel = getModel("github-copilot", "gpt-5.2-codex");
  45. const openrouterModel = getModel("openrouter", "openai/gpt-5.2-codex");
  46. // Step 1: Generate tool call with github-copilot
  47. const userMessage: Message = {
  48. role: "user",
  49. content: "Use the echo tool to echo 'hello world'",
  50. timestamp: Date.now(),
  51. };
  52. const assistantResponse = await completeSimple(
  53. copilotModel,
  54. {
  55. systemPrompt: "You are a helpful assistant. Use the echo tool when asked.",
  56. messages: [userMessage],
  57. tools: [echoTool],
  58. },
  59. { apiKey: copilotToken },
  60. );
  61. expect(assistantResponse.stopReason, `Copilot error: ${assistantResponse.errorMessage}`).toBe("toolUse");
  62. const toolCall = assistantResponse.content.find((c) => c.type === "toolCall");
  63. expect(toolCall).toBeDefined();
  64. expect(toolCall!.type).toBe("toolCall");
  65. // Verify it's a pipe-separated ID (OpenAI Responses format)
  66. if (toolCall?.type === "toolCall") {
  67. expect(toolCall.id).toContain("|");
  68. console.log(`Tool call ID from github-copilot: ${toolCall.id.slice(0, 80)}...`);
  69. }
  70. // Create tool result
  71. const toolResult: ToolResultMessage = {
  72. role: "toolResult",
  73. toolCallId: (toolCall as any).id,
  74. toolName: "echo",
  75. content: [{ type: "text", text: "hello world" }],
  76. isError: false,
  77. timestamp: Date.now(),
  78. };
  79. // Step 2: Complete with openrouter (uses openai-completions API)
  80. const openrouterResponse = await completeSimple(
  81. openrouterModel,
  82. {
  83. systemPrompt: "You are a helpful assistant.",
  84. messages: [
  85. userMessage,
  86. assistantResponse,
  87. toolResult,
  88. { role: "user", content: "Say hi", timestamp: Date.now() },
  89. ],
  90. tools: [echoTool],
  91. },
  92. { apiKey: openrouterKey },
  93. );
  94. // Should NOT fail with "call_id too long" error
  95. expect(openrouterResponse.stopReason, `OpenRouter error: ${openrouterResponse.errorMessage}`).not.toBe(
  96. "error",
  97. );
  98. expect(openrouterResponse.errorMessage).toBeUndefined();
  99. },
  100. 60000,
  101. );
  102. it.skipIf(!copilotToken || !codexToken)(
  103. "github-copilot -> openai-codex should normalize pipe-separated IDs",
  104. async () => {
  105. const copilotModel = getModel("github-copilot", "gpt-5.2-codex");
  106. const codexModel = getModel("openai-codex", "gpt-5.5");
  107. // Step 1: Generate tool call with github-copilot
  108. const userMessage: Message = {
  109. role: "user",
  110. content: "Use the echo tool to echo 'test message'",
  111. timestamp: Date.now(),
  112. };
  113. const assistantResponse = await completeSimple(
  114. copilotModel,
  115. {
  116. systemPrompt: "You are a helpful assistant. Use the echo tool when asked.",
  117. messages: [userMessage],
  118. tools: [echoTool],
  119. },
  120. { apiKey: copilotToken },
  121. );
  122. expect(assistantResponse.stopReason, `Copilot error: ${assistantResponse.errorMessage}`).toBe("toolUse");
  123. const toolCall = assistantResponse.content.find((c) => c.type === "toolCall");
  124. expect(toolCall).toBeDefined();
  125. // Create tool result
  126. const toolResult: ToolResultMessage = {
  127. role: "toolResult",
  128. toolCallId: (toolCall as any).id,
  129. toolName: "echo",
  130. content: [{ type: "text", text: "test message" }],
  131. isError: false,
  132. timestamp: Date.now(),
  133. };
  134. // Step 2: Complete with openai-codex (uses openai-codex-responses API)
  135. const codexResponse = await completeSimple(
  136. codexModel,
  137. {
  138. systemPrompt: "You are a helpful assistant.",
  139. messages: [
  140. userMessage,
  141. assistantResponse,
  142. toolResult,
  143. { role: "user", content: "Say hi", timestamp: Date.now() },
  144. ],
  145. tools: [echoTool],
  146. },
  147. { apiKey: codexToken },
  148. );
  149. // Should NOT fail with ID validation error
  150. expect(codexResponse.stopReason, `Codex error: ${codexResponse.errorMessage}`).not.toBe("error");
  151. expect(codexResponse.errorMessage).toBeUndefined();
  152. },
  153. 60000,
  154. );
  155. });
  156. /**
  157. * Test 2: Prefilled context with exact failing IDs from issue #1022
  158. *
  159. * Uses the exact tool call ID format that caused the error:
  160. * "call_xxx|very_long_base64_with_special_chars+/="
  161. */
  162. describe("Tool Call ID Normalization - Prefilled Context", () => {
  163. // Exact tool call ID from issue #1022 JSONL
  164. const FAILING_TOOL_CALL_ID =
  165. "call_pAYbIr76hXIjncD9UE4eGfnS|t5nnb2qYMFWGSsr13fhCd1CaCu3t3qONEPuOudu4HSVEtA8YJSL6FAZUxvoOoD792VIJWl91g87EdqsCWp9krVsdBysQoDaf9lMCLb8BS4EYi4gQd5kBQBYLlgD71PYwvf+TbMD9J9/5OMD42oxSRj8H+vRf78/l2Xla33LWz4nOgsddBlbvabICRs8GHt5C9PK5keFtzyi3lsyVKNlfduK3iphsZqs4MLv4zyGJnvZo/+QzShyk5xnMSQX/f98+aEoNflEApCdEOXipipgeiNWnpFSHbcwmMkZoJhURNu+JEz3xCh1mrXeYoN5o+trLL3IXJacSsLYXDrYTipZZbJFRPAucgbnjYBC+/ZzJOfkwCs+Gkw7EoZR7ZQgJ8ma+9586n4tT4cI8DEhBSZsWMjrCt8dxKg==";
  166. // Build prefilled context with the failing ID
  167. function buildPrefilledMessages(): Message[] {
  168. const userMessage: Message = {
  169. role: "user",
  170. content: "Use the echo tool to echo 'hello'",
  171. timestamp: Date.now() - 2000,
  172. };
  173. const assistantMessage: AssistantMessage = {
  174. role: "assistant",
  175. content: [
  176. {
  177. type: "toolCall",
  178. id: FAILING_TOOL_CALL_ID,
  179. name: "echo",
  180. arguments: { message: "hello" },
  181. },
  182. ],
  183. api: "openai-responses",
  184. provider: "github-copilot",
  185. model: "gpt-5.2-codex",
  186. usage: {
  187. input: 100,
  188. output: 50,
  189. cacheRead: 0,
  190. cacheWrite: 0,
  191. totalTokens: 150,
  192. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  193. },
  194. stopReason: "toolUse",
  195. timestamp: Date.now() - 1500,
  196. };
  197. const toolResult: ToolResultMessage = {
  198. role: "toolResult",
  199. toolCallId: FAILING_TOOL_CALL_ID,
  200. toolName: "echo",
  201. content: [{ type: "text", text: "hello" }],
  202. isError: false,
  203. timestamp: Date.now() - 1000,
  204. };
  205. const followUpUser: Message = {
  206. role: "user",
  207. content: "Say hi",
  208. timestamp: Date.now(),
  209. };
  210. return [userMessage, assistantMessage, toolResult, followUpUser];
  211. }
  212. it.skipIf(!openrouterKey)(
  213. "openrouter should handle prefilled context with long pipe-separated IDs",
  214. async () => {
  215. const model = getModel("openrouter", "openai/gpt-5.2-codex");
  216. const messages = buildPrefilledMessages();
  217. const response = await completeSimple(
  218. model,
  219. {
  220. systemPrompt: "You are a helpful assistant.",
  221. messages,
  222. tools: [echoTool],
  223. },
  224. { apiKey: openrouterKey },
  225. );
  226. // Should NOT fail with "call_id too long" error
  227. expect(response.stopReason, `OpenRouter error: ${response.errorMessage}`).not.toBe("error");
  228. if (response.errorMessage) {
  229. expect(response.errorMessage).not.toContain("call_id");
  230. expect(response.errorMessage).not.toContain("too long");
  231. }
  232. },
  233. 30000,
  234. );
  235. it.skipIf(!codexToken)(
  236. "openai-codex should handle prefilled context with long pipe-separated IDs",
  237. async () => {
  238. const model = getModel("openai-codex", "gpt-5.5");
  239. const messages = buildPrefilledMessages();
  240. const response = await completeSimple(
  241. model,
  242. {
  243. systemPrompt: "You are a helpful assistant.",
  244. messages,
  245. tools: [echoTool],
  246. },
  247. { apiKey: codexToken },
  248. );
  249. // Should NOT fail with ID validation error
  250. expect(response.stopReason, `Codex error: ${response.errorMessage}`).not.toBe("error");
  251. if (response.errorMessage) {
  252. expect(response.errorMessage).not.toContain("id");
  253. expect(response.errorMessage).not.toContain("additional characters");
  254. }
  255. },
  256. 30000,
  257. );
  258. });