anthropic-effort-wire.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { createAnthropic } from "@ai-sdk/anthropic";
  2. import { generateText } from "ai";
  3. import { describe, expect, it } from "vitest";
  4. import { createGenerateTextOptions } from "../src/model/runner-options.js";
  5. import type { ResolvedAiSdkModel } from "../src/model/runner-runtime.js";
  6. import { createTestModelProperties } from "./test-model-format.js";
  7. describe("Anthropic effort wire shape", () => {
  8. it.each(["low", "high", "max"] as const)(
  9. "sends Kimi K3 %s effort through output_config without K2 thinking",
  10. async (effort) => {
  11. const capturedBody = await captureAnthropicEffortRequest({
  12. effort,
  13. modelId: "kimi-k3",
  14. providerId: "moonshot-kimi",
  15. });
  16. expect(capturedBody).toMatchObject({ output_config: { effort } });
  17. expect(capturedBody).not.toHaveProperty("thinking");
  18. },
  19. );
  20. it.each(["low", "high", "max"] as const)(
  21. "maps canonical %s config through to adaptive wire fields",
  22. async (effort) => {
  23. const modelId = "opaque-model";
  24. const canonicalOptions = { effort, thinking: { type: "adaptive" } };
  25. const capturedBody = await captureAnthropicEffortRequest({
  26. effort,
  27. modelId,
  28. providerId: "openrouter-anthropic",
  29. providerOptions: canonicalOptions,
  30. });
  31. expect(capturedBody).toMatchObject({
  32. model: modelId,
  33. output_config: { effort },
  34. thinking: { type: "adaptive" },
  35. });
  36. expect(capturedBody).not.toHaveProperty("thinking.budget_tokens");
  37. expect(capturedBody).not.toHaveProperty("reasoning_effort");
  38. },
  39. );
  40. it("sends GPT xhigh effort through Anthropic output_config", async () => {
  41. const capturedBody = await captureAnthropicEffortRequest({
  42. effort: "xhigh",
  43. modelId: "gpt-5.6-sol",
  44. providerId: "custom-anthropic-gpt",
  45. });
  46. expect(capturedBody).toMatchObject({ output_config: { effort: "xhigh" } });
  47. expect(capturedBody).not.toHaveProperty("reasoning_effort");
  48. });
  49. });
  50. async function captureAnthropicEffortRequest(input: {
  51. effort: "low" | "medium" | "high" | "xhigh" | "max";
  52. modelId: string;
  53. providerId: string;
  54. providerOptions?: Record<string, unknown>;
  55. }): Promise<Record<string, unknown> | undefined> {
  56. let capturedBody: Record<string, unknown> | undefined;
  57. const provider = createAnthropic({
  58. apiKey: "fake-ak",
  59. baseURL: "https://example.com/v1",
  60. fetch: async (_request, init) => {
  61. if (typeof init?.body !== "string") {
  62. throw new Error("Expected a JSON request body");
  63. }
  64. capturedBody = JSON.parse(init.body) as Record<string, unknown>;
  65. return new Response(
  66. JSON.stringify({
  67. content: [{ text: "ok", type: "text" }],
  68. id: "msg_anthropic_effort",
  69. model: input.modelId,
  70. role: "assistant",
  71. stop_reason: "end_turn",
  72. stop_sequence: null,
  73. type: "message",
  74. usage: { input_tokens: 1, output_tokens: 1 },
  75. }),
  76. { headers: { "content-type": "application/json" } },
  77. );
  78. },
  79. });
  80. const options = createGenerateTextOptions({
  81. includeModelIO: false,
  82. request: {
  83. messages: [{ content: "hi", role: "user" }],
  84. providerOptions: { anthropic: input.providerOptions ?? { effort: input.effort } },
  85. },
  86. resolved: {
  87. properties: createTestModelProperties(),
  88. baseURL: "https://example.com/v1",
  89. model: provider(input.modelId),
  90. providerKind: "anthropic",
  91. providerId: input.providerId,
  92. modelId: input.modelId,
  93. } as ResolvedAiSdkModel,
  94. statusContext: {
  95. providerId: input.providerId,
  96. modelId: input.modelId,
  97. providerKind: "anthropic",
  98. requestId: `req_${input.providerId}_${input.effort}`,
  99. sessionId: `sess_${input.providerId}_${input.effort}`,
  100. traceId: `trace_${input.providerId}_${input.effort}`,
  101. } as never,
  102. });
  103. await generateText(options);
  104. return capturedBody;
  105. }