reasoning-provider-compatibility.test.ts 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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(compatiblePairs)("still strips across model IDs: %s -> %s", (source, target) => {
  39. expectStripped(history(source), { ...identity(target), modelId: createModelId("GLM-5.2") });
  40. });
  41. it("keeps identical custom provider identities compatible", () => {
  42. const messages = history("custom-a");
  43. expect(normalizeReasoningHistory(messages, identity("custom-a"))).toBe(messages);
  44. });
  45. });
  46. function identity(providerId: string) {
  47. return { providerId: createModelProviderId(providerId), modelId: MODEL_ID };
  48. }
  49. function history(providerId: string): ModelInputMessage[] {
  50. return [
  51. { role: "user", content: "Read the file" },
  52. {
  53. role: "assistant",
  54. ...identity(providerId),
  55. cacheControl: { type: "ephemeral" },
  56. content: [
  57. {
  58. type: "reasoning",
  59. text: "signed",
  60. providerOptions: { anthropic: { signature: " sig\n" } },
  61. },
  62. { type: "reasoning", text: "", providerOptions: { anthropic: { redactedData: "opaque" } } },
  63. { type: "reasoning", text: "unsigned" },
  64. { type: "text", text: "Reading" },
  65. ],
  66. toolCalls: [{ id: "read", name: "Read", input: { file_path: "file.txt" } }],
  67. },
  68. { role: "tool", toolCallId: "read", toolName: "Read", content: "File contents" },
  69. { role: "user", content: "Continue" },
  70. ];
  71. }
  72. function expectStripped(messages: ModelInputMessage[], target: ReturnType<typeof identity>) {
  73. const before = structuredClone(messages);
  74. const projected = normalizeReasoningHistory(messages, target);
  75. expect(projected).toEqual([
  76. messages[0],
  77. {
  78. ...messages[1],
  79. content: [
  80. { type: "reasoning", text: "unsigned" },
  81. { type: "text", text: "Reading" },
  82. ],
  83. },
  84. ...messages.slice(2),
  85. ]);
  86. expect(messages).toEqual(before);
  87. }