xiaomi-token-plan-ams-anthropic-empty-signature-smoke.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { describe, expect, it } from "vitest";
  2. import { completeSimple, getEnvApiKey, streamSimple } from "../src/stream.ts";
  3. import type { AssistantMessage, Context, Model } from "../src/types.ts";
  4. const provider = "xiaomi-token-plan-ams";
  5. const apiKey = getEnvApiKey(provider);
  6. const model: Model<"anthropic-messages"> = {
  7. id: "mimo-v2.5-pro",
  8. name: "MiMo-V2.5-Pro Anthropic smoke",
  9. api: "anthropic-messages",
  10. provider,
  11. baseUrl: "https://token-plan-ams.xiaomimimo.com/anthropic",
  12. reasoning: true,
  13. input: ["text"],
  14. cost: { input: 1, output: 3, cacheRead: 0.2, cacheWrite: 0 },
  15. contextWindow: 1048576,
  16. maxTokens: 1024,
  17. compat: { allowEmptySignature: true },
  18. };
  19. interface AnthropicPayload {
  20. messages?: Array<{
  21. role: string;
  22. content: string | Array<{ type: string; text?: string; thinking?: string; signature?: string }>;
  23. }>;
  24. }
  25. class PayloadCaptured extends Error {
  26. constructor() {
  27. super("payload captured");
  28. this.name = "PayloadCaptured";
  29. }
  30. }
  31. function makeInitialContext(): Context {
  32. return {
  33. systemPrompt: "You are concise. Follow the requested output format exactly.",
  34. messages: [
  35. {
  36. role: "user",
  37. content: "Think internally if you need to, then reply with exactly this text and nothing else: first-ok",
  38. timestamp: Date.now(),
  39. },
  40. ],
  41. };
  42. }
  43. function getThinkingBlocks(message: AssistantMessage) {
  44. return message.content.filter((block) => block.type === "thinking");
  45. }
  46. async function captureReplayPayload(context: Context): Promise<AnthropicPayload> {
  47. let capturedPayload: AnthropicPayload | undefined;
  48. const stream = streamSimple(model, context, {
  49. apiKey,
  50. maxTokens: 512,
  51. reasoning: "high",
  52. onPayload: (payload) => {
  53. capturedPayload = payload as AnthropicPayload;
  54. throw new PayloadCaptured();
  55. },
  56. });
  57. await stream.result();
  58. if (!capturedPayload) {
  59. throw new Error("Expected payload capture before request");
  60. }
  61. return capturedPayload;
  62. }
  63. describe.skipIf(!apiKey)("Xiaomi Token Plan AMS Anthropic empty thinking signature smoke", () => {
  64. it("reproduces empty thinking signatures and preserves them for replay", { timeout: 60000, retry: 1 }, async () => {
  65. const firstContext = makeInitialContext();
  66. const first = await completeSimple(model, firstContext, {
  67. apiKey,
  68. maxTokens: 512,
  69. reasoning: "high",
  70. });
  71. expect(first.stopReason, first.errorMessage).toBe("stop");
  72. const thinkingBlocks = getThinkingBlocks(first);
  73. expect(thinkingBlocks.length).toBeGreaterThan(0);
  74. expect(thinkingBlocks.some((block) => block.thinkingSignature === "")).toBe(true);
  75. const replayContext: Context = {
  76. ...firstContext,
  77. messages: [
  78. ...firstContext.messages,
  79. first,
  80. {
  81. role: "user",
  82. content: "Reply with exactly this text and nothing else: second-ok",
  83. timestamp: Date.now(),
  84. },
  85. ],
  86. };
  87. const replayPayload = await captureReplayPayload(replayContext);
  88. const assistantPayload = replayPayload.messages?.find((message) => message.role === "assistant");
  89. expect(assistantPayload).toBeDefined();
  90. expect(Array.isArray(assistantPayload!.content)).toBe(true);
  91. const replayedThinking = (assistantPayload!.content as Array<{ type: string; text?: string }>).filter(
  92. (block) => block.type === "thinking",
  93. );
  94. const replayedText = (assistantPayload!.content as Array<{ type: string; text?: string }>).filter(
  95. (block) => block.type === "text",
  96. );
  97. expect(replayedThinking).toEqual([{ type: "thinking", thinking: thinkingBlocks[0].thinking, signature: "" }]);
  98. expect(replayedText.some((block) => block.text === thinkingBlocks[0].thinking)).toBe(false);
  99. });
  100. });