anthropic-force-adaptive-thinking.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { describe, expect, it } from "vitest";
  2. import { getModel } from "../src/models.ts";
  3. import { streamSimple } from "../src/stream.ts";
  4. import type { Context, Model, SimpleStreamOptions } from "../src/types.ts";
  5. interface AnthropicThinkingPayload {
  6. model?: string;
  7. thinking?: { type: string; budget_tokens?: number; display?: string };
  8. output_config?: { effort?: string };
  9. }
  10. class PayloadCaptured extends Error {
  11. constructor() {
  12. super("payload captured");
  13. this.name = "PayloadCaptured";
  14. }
  15. }
  16. function makeContext(): Context {
  17. return {
  18. messages: [{ role: "user", content: "Hello", timestamp: Date.now() }],
  19. };
  20. }
  21. function makeCustomModel(compat?: Model<"anthropic-messages">["compat"]): Model<"anthropic-messages"> {
  22. return {
  23. // Id intentionally does not match any built-in adaptive substring. This
  24. // mirrors corporate proxy schemes such as `anthropic--claude-opus-latest`.
  25. id: "vendor--claude-opus-latest",
  26. name: "Vendor Proxy Opus Latest",
  27. api: "anthropic-messages",
  28. provider: "vendor-proxy",
  29. baseUrl: "http://127.0.0.1:9",
  30. reasoning: true,
  31. input: ["text"],
  32. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
  33. contextWindow: 200000,
  34. maxTokens: 32000,
  35. compat,
  36. };
  37. }
  38. async function capturePayload(
  39. model: Model<"anthropic-messages">,
  40. options?: SimpleStreamOptions,
  41. ): Promise<AnthropicThinkingPayload> {
  42. let capturedPayload: AnthropicThinkingPayload | undefined;
  43. const payloadCaptureModel: Model<"anthropic-messages"> = {
  44. ...model,
  45. baseUrl: "http://127.0.0.1:9",
  46. };
  47. const s = streamSimple(payloadCaptureModel, makeContext(), {
  48. ...options,
  49. apiKey: "fake-key",
  50. onPayload: (payload) => {
  51. capturedPayload = payload as AnthropicThinkingPayload;
  52. throw new PayloadCaptured();
  53. },
  54. });
  55. await s.result();
  56. if (!capturedPayload) {
  57. throw new Error("Expected payload to be captured before request failure");
  58. }
  59. return capturedPayload;
  60. }
  61. describe("Anthropic forceAdaptiveThinking compat override", () => {
  62. it("sends legacy thinking payload for custom model ids by default", async () => {
  63. const payload = await capturePayload(makeCustomModel(), { reasoning: "medium" });
  64. expect(payload.thinking?.type).toBe("enabled");
  65. expect(payload.output_config).toBeUndefined();
  66. });
  67. it("sends adaptive thinking payload when compat.forceAdaptiveThinking is true", async () => {
  68. const payload = await capturePayload(makeCustomModel({ forceAdaptiveThinking: true }), { reasoning: "medium" });
  69. expect(payload.thinking).toEqual({ type: "adaptive", display: "summarized" });
  70. expect(payload.output_config).toEqual({ effort: "medium" });
  71. });
  72. it.each([
  73. "claude-opus-5[1m]",
  74. "claude-sonnet-5",
  75. "claude-fable-5",
  76. "claude-mythos-5",
  77. "claude-mythos-preview",
  78. ])("omits thinking while preserving effort for default-thinking model %s", async (modelId) => {
  79. const model = {
  80. ...makeCustomModel(),
  81. id: modelId,
  82. };
  83. const payload = await capturePayload(model, { reasoning: "medium" });
  84. expect(payload.model).toBe(model.id);
  85. expect(payload).not.toHaveProperty("thinking");
  86. expect(payload.output_config).toEqual({ effort: "medium" });
  87. });
  88. it("does not treat an arbitrary bracket suffix as a default-thinking model", async () => {
  89. const model = { ...makeCustomModel(), id: "claude-opus-5[foo]" };
  90. const payload = await capturePayload(model, { reasoning: "medium" });
  91. expect(payload.thinking?.type).toBe("enabled");
  92. expect(payload.output_config).toBeUndefined();
  93. });
  94. it("keeps an explicit disabled thinking parameter for models that allow it", async () => {
  95. const model = { ...makeCustomModel(), id: "claude-opus-5" };
  96. const payload = await capturePayload(model);
  97. expect(payload.thinking).toEqual({ type: "disabled" });
  98. expect(payload.output_config).toBeUndefined();
  99. });
  100. it("omits thinking with native xhigh effort for Claude Fable 5", async () => {
  101. const payload = await capturePayload(getModel("anthropic", "claude-fable-5"), { reasoning: "xhigh" });
  102. expect(payload).not.toHaveProperty("thinking");
  103. expect(payload.output_config).toEqual({ effort: "xhigh" });
  104. });
  105. it("allows built-in adaptive models to opt out with compat.forceAdaptiveThinking false", async () => {
  106. const model: Model<"anthropic-messages"> = {
  107. ...getModel("anthropic", "claude-opus-4-8"),
  108. compat: { forceAdaptiveThinking: false },
  109. };
  110. const payload = await capturePayload(model, { reasoning: "medium" });
  111. expect(payload.thinking?.type).toBe("enabled");
  112. expect(payload.output_config).toBeUndefined();
  113. });
  114. it("preserves thinking.type=disabled when reasoning is off regardless of override", async () => {
  115. const payload = await capturePayload(makeCustomModel({ forceAdaptiveThinking: true }));
  116. expect(payload.thinking).toEqual({ type: "disabled" });
  117. expect(payload.output_config).toBeUndefined();
  118. });
  119. });