anthropic-empty-thinking-signature-compat.test.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { describe, expect, it } from "vitest";
  2. import { streamSimple } from "../src/stream.ts";
  3. import type { AssistantMessage, Context, Model } from "../src/types.ts";
  4. interface AnthropicPayload {
  5. messages?: Array<{
  6. role: string;
  7. content: Array<{ type: string; text?: string; thinking?: string; signature?: string }>;
  8. }>;
  9. }
  10. class PayloadCaptured extends Error {
  11. constructor() {
  12. super("payload captured");
  13. this.name = "PayloadCaptured";
  14. }
  15. }
  16. function makeModel(allowEmptySignature?: boolean): Model<"anthropic-messages"> {
  17. return {
  18. id: "mimo-v2.5-pro",
  19. name: "MiMo-V2.5-Pro",
  20. api: "anthropic-messages",
  21. provider: "xiaomi-token-plan-ams",
  22. baseUrl: "http://127.0.0.1:9/anthropic",
  23. reasoning: true,
  24. input: ["text"],
  25. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
  26. contextWindow: 1048576,
  27. maxTokens: 1024,
  28. ...(allowEmptySignature === undefined ? {} : { compat: { allowEmptySignature } }),
  29. };
  30. }
  31. function makeContext(thinkingSignature: string, thinking = "internal reasoning"): Context {
  32. const assistant: AssistantMessage = {
  33. role: "assistant",
  34. content: [{ type: "thinking", thinking, thinkingSignature }],
  35. provider: "xiaomi-token-plan-ams",
  36. api: "anthropic-messages",
  37. model: "mimo-v2.5-pro",
  38. timestamp: Date.now(),
  39. usage: {
  40. input: 0,
  41. output: 0,
  42. cacheRead: 0,
  43. cacheWrite: 0,
  44. totalTokens: 0,
  45. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  46. },
  47. stopReason: "stop",
  48. };
  49. return {
  50. messages: [
  51. { role: "user", content: "first", timestamp: Date.now() },
  52. assistant,
  53. { role: "user", content: "second", timestamp: Date.now() },
  54. ],
  55. };
  56. }
  57. async function capturePayload(model: Model<"anthropic-messages">, context: Context): Promise<AnthropicPayload> {
  58. let capturedPayload: AnthropicPayload | undefined;
  59. const stream = streamSimple(model, context, {
  60. apiKey: "fake-key",
  61. onPayload: (payload) => {
  62. capturedPayload = payload as AnthropicPayload;
  63. throw new PayloadCaptured();
  64. },
  65. });
  66. await stream.result();
  67. if (!capturedPayload) throw new Error("Expected payload capture before request");
  68. return capturedPayload;
  69. }
  70. describe("Anthropic empty thinking signature compat", () => {
  71. it("preserves empty thinking text when the signature is present", async () => {
  72. const payload = await capturePayload(makeModel(), makeContext("signed-thinking", ""));
  73. const assistant = payload.messages?.find((message) => message.role === "assistant");
  74. expect(assistant?.content).toEqual([{ type: "thinking", thinking: "", signature: "signed-thinking" }]);
  75. });
  76. it("converts empty-signature thinking to text by default", async () => {
  77. const payload = await capturePayload(makeModel(), makeContext(""));
  78. const assistant = payload.messages?.find((message) => message.role === "assistant");
  79. expect(assistant?.content).toEqual([{ type: "text", text: "internal reasoning" }]);
  80. });
  81. it("preserves empty-signature thinking when allowEmptySignature is enabled", async () => {
  82. const payload = await capturePayload(makeModel(true), makeContext(" "));
  83. const assistant = payload.messages?.find((message) => message.role === "assistant");
  84. expect(assistant?.content).toEqual([{ type: "thinking", thinking: "internal reasoning", signature: "" }]);
  85. });
  86. });