reasoning-provider-compatibility.test.ts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { describe, expect, it } from "vitest";
  2. import { createModelId, createModelProviderId, type ModelInputMessage } from "@zcode/contracts";
  3. import { normalizeReasoningHistory } from "../src/model/reasoning-history-normalization.js";
  4. const MODEL_ID = createModelId("GLM-5.3");
  5. const PROVIDER_GROUPS = [
  6. ["builtin:zai-coding-plan", "account:zai-individual-coding-plan", "account:zai-team-coding-plan"],
  7. [
  8. "builtin:bigmodel-coding-plan",
  9. "account:bigmodel-individual-coding-plan",
  10. "account:bigmodel-team-coding-plan",
  11. ],
  12. ];
  13. const compatiblePairs = PROVIDER_GROUPS.flatMap((group) =>
  14. group.flatMap((source) => group.map((target) => [source, target] as const)),
  15. );
  16. describe("reasoning replay provider identity", () => {
  17. it.each(compatiblePairs)("preserves history and cache metadata: %s -> %s", (source, target) => {
  18. const messages = history(source);
  19. const before = structuredClone(messages);
  20. expect(normalizeReasoningHistory(messages, identity(target))).toBe(messages);
  21. expect(messages).toEqual(before);
  22. });
  23. it.each([
  24. ["builtin:zai-coding-plan", "account:bigmodel-individual-coding-plan"],
  25. ["builtin:bigmodel-coding-plan", "account:zai-team-coding-plan"],
  26. ["account:zai-team-coding-plan", "account:bigmodel-team-coding-plan"],
  27. ["custom-a", "custom-b"],
  28. ["custom-zai-coding-plan", "account:zai-individual-coding-plan"],
  29. ["account:zai-team-coding-plan-extra", "account:zai-team-coding-plan"],
  30. ["builtin:zai-unknown-plan", "account:zai-team-coding-plan"],
  31. ["account:zai-start-plan", "account:zai-team-coding-plan"],
  32. ["account:zai-off-peak", "account:zai-team-coding-plan"],
  33. ["zai-api", "account:zai-team-coding-plan"],
  34. ])("keeps distinct providers isolated: %s -> %s", (source, target) => {
  35. expectStripped(history(source), identity(target));
  36. expectStripped(history(target), identity(source));
  37. });
  38. it.each([["builtin:zai-coding-plan", "builtin:zai-coding-plan"]] as const)(
  39. "still strips across model IDs: %s -> %s",
  40. (source, target) => {
  41. expectStripped(history(source), { ...identity(target), modelId: createModelId("GLM-5.2") });
  42. },
  43. );
  44. it("keeps identical custom provider identities compatible", () => {
  45. const messages = history("custom-a");
  46. expect(normalizeReasoningHistory(messages, identity("custom-a"))).toBe(messages);
  47. });
  48. });
  49. function identity(providerId: string) {
  50. return { providerId: createModelProviderId(providerId), modelId: MODEL_ID };
  51. }
  52. function history(providerId: string): ModelInputMessage[] {
  53. return [
  54. { role: "user", content: "Read the file" },
  55. {
  56. role: "assistant",
  57. ...identity(providerId),
  58. cacheControl: { type: "ephemeral" },
  59. content: [
  60. {
  61. type: "reasoning",
  62. text: "signed",
  63. providerOptions: { anthropic: { signature: " sig\n" } },
  64. },
  65. { type: "reasoning", text: "", providerOptions: { anthropic: { redactedData: "opaque" } } },
  66. { type: "reasoning", text: "unsigned" },
  67. { type: "text", text: "Reading" },
  68. ],
  69. toolCalls: [{ id: "read", name: "Read", input: { file_path: "file.txt" } }],
  70. },
  71. { role: "tool", toolCallId: "read", toolName: "Read", content: "File contents" },
  72. { role: "user", content: "Continue" },
  73. ];
  74. }
  75. function expectStripped(messages: ModelInputMessage[], target: ReturnType<typeof identity>) {
  76. const before = structuredClone(messages);
  77. const projected = normalizeReasoningHistory(messages, target);
  78. expect(projected).toEqual([
  79. messages[0],
  80. {
  81. ...messages[1],
  82. content: [
  83. { type: "reasoning", text: "unsigned" },
  84. { type: "text", text: "Reading" },
  85. ],
  86. },
  87. ...messages.slice(2),
  88. ]);
  89. expect(messages).toEqual(before);
  90. }