empty-reasoning-wire.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { createAnthropic } from "@ai-sdk/anthropic";
  2. import { generateText } from "ai";
  3. import { describe, expect, it } from "vitest";
  4. import type { ModelInputMessage } from "@zcode/contracts";
  5. import { createGenerateTextOptions } from "../src/model/runner-options.js";
  6. import type { ResolvedAiSdkModel } from "../src/model/runner-runtime.js";
  7. import { createTestModelProperties } from "./test-model-format.js";
  8. describe("empty reasoning history wire shape", () => {
  9. it("omits an exact-empty metadata-free reasoning stub from Anthropic history", async () => {
  10. const capturedBody = await captureAnthropicBody({
  11. messages: [
  12. { role: "user", content: "start" },
  13. {
  14. role: "assistant",
  15. content: [
  16. { type: "reasoning", text: "" },
  17. { type: "text", text: "done" },
  18. ],
  19. },
  20. { role: "user", content: "finish" },
  21. ],
  22. modelId: "claude-sonnet-4",
  23. providerId: "anthropic",
  24. });
  25. const messages = capturedBody.messages as Record<string, unknown>[];
  26. expect(messages[1]).toEqual({
  27. role: "assistant",
  28. content: [{ type: "text", text: "done" }],
  29. });
  30. });
  31. it("does not synthesize a DeepSeek empty thinking placeholder before a tool call", async () => {
  32. const toolCallId = "call_deepseek_empty_thinking";
  33. const capturedBody = await captureAnthropicBody({
  34. messages: [
  35. { role: "user", content: "start" },
  36. {
  37. role: "assistant",
  38. content: [{ type: "reasoning", text: "" }],
  39. toolCalls: [{ id: toolCallId, name: "Read", input: { file_path: "README.md" } }],
  40. },
  41. {
  42. role: "tool",
  43. content: "tool result",
  44. toolCallId,
  45. toolName: "Read",
  46. },
  47. { role: "user", content: "finish" },
  48. ],
  49. modelId: "deepseek-v4-pro",
  50. providerId: "deepseek",
  51. });
  52. const messages = capturedBody.messages as Array<{ content?: unknown }>;
  53. expect(messages[1]?.content).toEqual([
  54. {
  55. type: "tool_use",
  56. id: toolCallId,
  57. name: "Read",
  58. input: { file_path: "README.md" },
  59. },
  60. ]);
  61. });
  62. });
  63. async function captureAnthropicBody(input: {
  64. messages: ModelInputMessage[];
  65. modelId: string;
  66. providerId: string;
  67. }): Promise<Record<string, unknown>> {
  68. let capturedBody: Record<string, unknown> | undefined;
  69. const provider = createAnthropic({
  70. apiKey: "fake-ak",
  71. baseURL: "https://api.anthropic.example.test/v1",
  72. fetch: async (_request, init) => {
  73. if (typeof init?.body !== "string") {
  74. throw new Error("Expected a JSON request body");
  75. }
  76. capturedBody = JSON.parse(init.body) as Record<string, unknown>;
  77. return new Response(
  78. JSON.stringify({
  79. content: [{ text: "ok", type: "text" }],
  80. id: "msg_empty_reasoning_history",
  81. model: input.modelId,
  82. role: "assistant",
  83. stop_reason: "end_turn",
  84. stop_sequence: null,
  85. type: "message",
  86. usage: { input_tokens: 1, output_tokens: 1 },
  87. }),
  88. { headers: { "content-type": "application/json" } },
  89. );
  90. },
  91. });
  92. const options = createGenerateTextOptions({
  93. includeModelIO: false,
  94. request: {
  95. messages: input.messages,
  96. providerOptions: {
  97. anthropic: { thinking: { type: "enabled", budgetTokens: 1024 } },
  98. },
  99. },
  100. resolved: {
  101. properties: createTestModelProperties(),
  102. baseURL: "https://api.anthropic.example.test/v1",
  103. model: provider(input.modelId),
  104. providerKind: "anthropic",
  105. providerId: input.providerId,
  106. modelId: input.modelId,
  107. } as ResolvedAiSdkModel,
  108. statusContext: {
  109. providerId: input.providerId,
  110. modelId: input.modelId,
  111. providerKind: "anthropic",
  112. requestId: `req_${input.providerId}_empty_reasoning`,
  113. sessionId: `sess_${input.providerId}_empty_reasoning`,
  114. traceId: `trace_${input.providerId}_empty_reasoning`,
  115. } as never,
  116. });
  117. await generateText(options);
  118. if (!capturedBody) {
  119. throw new Error("Anthropic reasoning history request body was not captured");
  120. }
  121. return capturedBody;
  122. }