model-output-tokens-wire.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { createAnthropic } from "@ai-sdk/anthropic";
  2. import { generateText } from "ai";
  3. import { describe, expect, it } from "vitest";
  4. import { createProviderBusinessErrorFetch } from "../src/model/model-execution.js";
  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. const OUTPUT_TOKEN_CONTINUE_PROMPT =
  9. "Output token limit hit. Resume directly — no apology, no recap of what you were doing. Pick up mid-thought if that is where the cut happened. Break remaining work into smaller pieces.";
  10. describe("Anthropic Continue request body", () => {
  11. // 恢复 v0.16.6 的 wire 验收:通过真实 SDK 序列化检查相邻 user 合并后的准确文案和次数。
  12. it.each([1, 3])(
  13. "keeps %i exact Continue prompts when Anthropic merges adjacent users",
  14. async (count) => {
  15. let capturedBody: Record<string, unknown> | undefined;
  16. const provider = createAnthropic({
  17. apiKey: "fake-key",
  18. baseURL: "https://api.example.test",
  19. fetch: async (_input, init) => {
  20. if (typeof init?.body !== "string") throw new Error("Expected a JSON request body");
  21. capturedBody = JSON.parse(init.body) as Record<string, unknown>;
  22. return new Response(
  23. JSON.stringify({ error: { message: "wire capture", type: "test" } }),
  24. {
  25. headers: { "content-type": "application/json" },
  26. status: 400,
  27. },
  28. );
  29. },
  30. });
  31. const options = createGenerateTextOptions({
  32. includeModelIO: false,
  33. request: {
  34. messages: [
  35. { role: "user", content: "original user request" },
  36. ...Array.from({ length: count }, () => ({
  37. role: "user" as const,
  38. content: OUTPUT_TOKEN_CONTINUE_PROMPT,
  39. })),
  40. ],
  41. },
  42. resolved: {
  43. properties: createTestModelProperties(),
  44. model: provider("anthropic-model"),
  45. providerKind: "anthropic",
  46. providerId: "wire-provider",
  47. modelId: "anthropic-model",
  48. } as ResolvedAiSdkModel,
  49. statusContext: {
  50. providerId: "wire-provider",
  51. modelId: "anthropic-model",
  52. providerKind: "anthropic",
  53. requestId: "req_output_continue_wire",
  54. sessionId: "sess_output_continue_wire",
  55. traceId: "trace_output_continue_wire",
  56. } as never,
  57. });
  58. await expect(generateText(options)).rejects.toBeDefined();
  59. expect(capturedBody?.messages).toEqual([expect.objectContaining({ role: "user" })]);
  60. const bodyText = JSON.stringify(capturedBody);
  61. expect(bodyText).toContain("original user request");
  62. expect(bodyText.split(OUTPUT_TOKEN_CONTINUE_PROMPT)).toHaveLength(count + 1);
  63. expect(bodyText).not.toContain("<system-reminder>");
  64. },
  65. );
  66. });
  67. describe("provider business request body", () => {
  68. it.each([
  69. {
  70. body: JSON.stringify({ model: "reasoning-model", max_tokens: 12_345 }),
  71. name: "a provider name that used to trigger the implicit rewrite",
  72. },
  73. {
  74. body: JSON.stringify({ model: "reasoning-model", temperature: 0.7 }),
  75. name: "a JSON body without max_tokens",
  76. },
  77. { body: "not-json", name: "a non-JSON body" },
  78. ])("preserves $name for Snowflake Cortex", async ({ body }) => {
  79. let capturedBody: BodyInit | null | undefined;
  80. const fetch = createProviderBusinessErrorFetch({
  81. fetch: async (_input, init) => {
  82. capturedBody = init?.body;
  83. return new Response("{}", {
  84. headers: { "content-type": "application/json" },
  85. status: 200,
  86. });
  87. },
  88. providerId: "snowflake-cortex",
  89. providerKind: "openai-compatible",
  90. });
  91. await fetch("https://example.test/v1/chat/completions", { body, method: "POST" });
  92. expect(capturedBody).toBe(body);
  93. });
  94. });