transform-messages-copilot-openai-to-anthropic.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import { describe, expect, it } from "vitest";
  2. import { transformMessages } from "../src/providers/transform-messages.ts";
  3. import type { AssistantMessage, Message, Model, ToolCall } from "../src/types.ts";
  4. // Normalize function matching what anthropic.ts uses
  5. function anthropicNormalizeToolCallId(
  6. id: string,
  7. _model: Model<"anthropic-messages">,
  8. _source: AssistantMessage,
  9. ): string {
  10. return id.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64);
  11. }
  12. function makeCopilotClaudeModel(): Model<"anthropic-messages"> {
  13. return {
  14. id: "claude-sonnet-4.6",
  15. name: "Claude Sonnet 4.6",
  16. api: "anthropic-messages",
  17. provider: "github-copilot",
  18. baseUrl: "https://api.individual.githubcopilot.com",
  19. reasoning: true,
  20. input: ["text", "image"],
  21. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
  22. contextWindow: 128000,
  23. maxTokens: 16000,
  24. };
  25. }
  26. function makeAssistantMessage(content: AssistantMessage["content"]): AssistantMessage {
  27. return {
  28. role: "assistant",
  29. content,
  30. api: "openai-responses",
  31. provider: "github-copilot",
  32. model: "gpt-5",
  33. usage: {
  34. input: 0,
  35. output: 0,
  36. cacheRead: 0,
  37. cacheWrite: 0,
  38. totalTokens: 0,
  39. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  40. },
  41. stopReason: "toolUse",
  42. timestamp: Date.now(),
  43. };
  44. }
  45. describe("OpenAI to Anthropic session migration for Copilot Claude", () => {
  46. it("converts thinking blocks to plain text when source model differs", () => {
  47. const model = makeCopilotClaudeModel();
  48. const messages: Message[] = [
  49. { role: "user", content: "hello", timestamp: Date.now() },
  50. {
  51. role: "assistant",
  52. content: [
  53. {
  54. type: "thinking",
  55. thinking: "Let me think about this...",
  56. thinkingSignature: "reasoning_content",
  57. },
  58. { type: "text", text: "Hi there!" },
  59. ],
  60. api: "openai-completions",
  61. provider: "github-copilot",
  62. model: "gpt-4o",
  63. usage: {
  64. input: 0,
  65. output: 0,
  66. cacheRead: 0,
  67. cacheWrite: 0,
  68. totalTokens: 0,
  69. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  70. },
  71. stopReason: "stop",
  72. timestamp: Date.now(),
  73. },
  74. ];
  75. const result = transformMessages(messages, model, anthropicNormalizeToolCallId);
  76. const assistantMsg = result.find((m) => m.role === "assistant") as AssistantMessage;
  77. // Thinking block should be converted to text since models differ
  78. const textBlocks = assistantMsg.content.filter((b) => b.type === "text");
  79. const thinkingBlocks = assistantMsg.content.filter((b) => b.type === "thinking");
  80. expect(thinkingBlocks).toHaveLength(0);
  81. expect(textBlocks.length).toBeGreaterThanOrEqual(2);
  82. });
  83. it("removes thoughtSignature from tool calls when migrating between models", () => {
  84. const model = makeCopilotClaudeModel();
  85. const messages: Message[] = [
  86. { role: "user", content: "run a command", timestamp: Date.now() },
  87. {
  88. role: "assistant",
  89. content: [
  90. {
  91. type: "toolCall",
  92. id: "call_123",
  93. name: "bash",
  94. arguments: { command: "ls" },
  95. thoughtSignature: JSON.stringify({ type: "reasoning.encrypted", id: "call_123", data: "encrypted" }),
  96. },
  97. ],
  98. api: "openai-responses",
  99. provider: "github-copilot",
  100. model: "gpt-5",
  101. usage: {
  102. input: 0,
  103. output: 0,
  104. cacheRead: 0,
  105. cacheWrite: 0,
  106. totalTokens: 0,
  107. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  108. },
  109. stopReason: "toolUse",
  110. timestamp: Date.now(),
  111. },
  112. {
  113. role: "toolResult",
  114. toolCallId: "call_123",
  115. toolName: "bash",
  116. content: [{ type: "text", text: "output" }],
  117. isError: false,
  118. timestamp: Date.now(),
  119. },
  120. ];
  121. const result = transformMessages(messages, model, anthropicNormalizeToolCallId);
  122. const assistantMsg = result.find((m) => m.role === "assistant") as AssistantMessage;
  123. const toolCall = assistantMsg.content.find((b) => b.type === "toolCall") as ToolCall;
  124. expect(toolCall.thoughtSignature).toBeUndefined();
  125. });
  126. it("adds synthetic tool results for trailing orphaned tool calls", () => {
  127. const model = makeCopilotClaudeModel();
  128. const messages: Message[] = [
  129. { role: "user", content: "read the file", timestamp: Date.now() },
  130. makeAssistantMessage([
  131. {
  132. type: "toolCall",
  133. id: "call_123|fc_123",
  134. name: "read",
  135. arguments: { path: "README.md" },
  136. },
  137. ]),
  138. ];
  139. const result = transformMessages(messages, model, anthropicNormalizeToolCallId);
  140. const lastMessage = result[result.length - 1];
  141. expect(lastMessage).toMatchObject({
  142. role: "toolResult",
  143. toolCallId: "call_123_fc_123",
  144. toolName: "read",
  145. isError: true,
  146. content: [{ type: "text", text: "No result provided" }],
  147. });
  148. });
  149. it("adds synthetic results only for trailing tool calls that are still missing results", () => {
  150. const model = makeCopilotClaudeModel();
  151. const messages: Message[] = [
  152. { role: "user", content: "run commands", timestamp: Date.now() },
  153. makeAssistantMessage([
  154. { type: "toolCall", id: "call_1|fc_1", name: "read", arguments: { path: "README.md" } },
  155. { type: "toolCall", id: "call_2|fc_2", name: "bash", arguments: { command: "pwd" } },
  156. ]),
  157. {
  158. role: "toolResult",
  159. toolCallId: "call_1|fc_1",
  160. toolName: "read",
  161. content: [{ type: "text", text: "done" }],
  162. isError: false,
  163. timestamp: Date.now(),
  164. },
  165. ];
  166. const result = transformMessages(messages, model, anthropicNormalizeToolCallId);
  167. const syntheticResults = result.filter((message) => message.role === "toolResult" && message.isError);
  168. expect(syntheticResults).toHaveLength(1);
  169. expect(syntheticResults[0]).toMatchObject({
  170. role: "toolResult",
  171. toolCallId: "call_2_fc_2",
  172. toolName: "bash",
  173. content: [{ type: "text", text: "No result provided" }],
  174. });
  175. });
  176. });