openai-responses-foreign-toolcall-id.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { describe, expect, it } from "vitest";
  2. import { getModel } from "../src/models.ts";
  3. import { convertResponsesMessages } from "../src/providers/openai-responses-shared.ts";
  4. import type { AssistantMessage, Context, ToolResultMessage, Usage } from "../src/types.ts";
  5. import { shortHash } from "../src/utils/hash.ts";
  6. const COPILOT_RAW_TOOL_CALL_ID =
  7. "call_4VnzVawQXPB9MgYib7CiQFEY|I9b95oN1wD/cHXKTw3PpRkL6KkCtzTJhUxMouMWYwHeTo2j3htzfSk7YPx2vifiIM4g3A8XXyOj8q4Bt6SLUG7gqY1E3ELkrkVQNHglRfUmWj84lqxJY+Puieb3VKyX0FB+83TUzn91cDMF/4gzt990IzqVrc+nIb9RRscRD070Du16q1glydVjWR0SBJsE6TbY/esOjFpqplogQqrajm1eI++f3eLi73R6q7hVusY0QbeFySVxABCjhN0lXB04caBe1rzHjYzul6MAXj7uq+0r17VLq+yrtyYhN12wkmFqHeqTyEei6EFPbMy24Nc+IbJlkP0OCg02W+gOnyBFcbi2ctvJFSOhSjt1CqBdqCnnhwUqXjbWiT0wh3DmLScRgTHmGkaI+oAcQQjfic65nxj+TnEkReA==";
  8. const usage: Usage = {
  9. input: 0,
  10. output: 0,
  11. cacheRead: 0,
  12. cacheWrite: 0,
  13. totalTokens: 0,
  14. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  15. };
  16. describe("OpenAI Responses foreign tool call ID normalization", () => {
  17. it("hashes foreign Copilot tool item IDs into a bounded Codex-safe fc_<hash> shape", () => {
  18. const model = getModel("openai-codex", "gpt-5.5");
  19. const assistant: AssistantMessage = {
  20. role: "assistant",
  21. content: [
  22. {
  23. type: "toolCall",
  24. id: COPILOT_RAW_TOOL_CALL_ID,
  25. name: "edit",
  26. arguments: { path: "src/styles/app.css" },
  27. },
  28. ],
  29. api: "openai-responses",
  30. provider: "github-copilot",
  31. model: "gpt-5.5",
  32. usage,
  33. stopReason: "toolUse",
  34. timestamp: Date.now() - 2000,
  35. };
  36. const toolResult: ToolResultMessage = {
  37. role: "toolResult",
  38. toolCallId: COPILOT_RAW_TOOL_CALL_ID,
  39. toolName: "edit",
  40. content: [{ type: "text", text: "ok" }],
  41. isError: false,
  42. timestamp: Date.now() - 1000,
  43. };
  44. const context: Context = {
  45. systemPrompt: "You are concise.",
  46. messages: [{ role: "user", content: "Use the tool.", timestamp: Date.now() - 3000 }, assistant, toolResult],
  47. };
  48. const input = convertResponsesMessages(model, context, new Set(["openai", "openai-codex", "opencode"]));
  49. const functionCall = input.find((item) => item.type === "function_call");
  50. expect(functionCall).toBeDefined();
  51. expect(functionCall?.type).toBe("function_call");
  52. if (!functionCall || functionCall.type !== "function_call") {
  53. throw new Error("Expected function_call item");
  54. }
  55. const expectedItemId = `fc_${shortHash(COPILOT_RAW_TOOL_CALL_ID.split("|")[1]!)}`;
  56. expect(functionCall.id).toBe(expectedItemId);
  57. expect(functionCall.id?.length ?? 0).toBeLessThanOrEqual(64);
  58. expect(functionCall.id).toMatch(/^fc_[A-Za-z0-9]+$/);
  59. });
  60. });