bedrock-thinking-payload.test.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import { describe, expect, it } from "vitest";
  2. import { getModel } from "../src/models.ts";
  3. import { type BedrockOptions, streamBedrock } from "../src/providers/amazon-bedrock.ts";
  4. import type { Context, Model } from "../src/types.ts";
  5. import { hasBedrockCredentials } from "./bedrock-utils.ts";
  6. interface BedrockThinkingPayload {
  7. additionalModelRequestFields?: {
  8. thinking?: { type: string; budget_tokens?: number; display?: string };
  9. output_config?: { effort?: string };
  10. anthropic_beta?: string[];
  11. };
  12. }
  13. class PayloadCaptured extends Error {
  14. constructor() {
  15. super("payload captured");
  16. this.name = "PayloadCaptured";
  17. }
  18. }
  19. function makeContext(): Context {
  20. return {
  21. messages: [{ role: "user", content: "Hello", timestamp: Date.now() }],
  22. };
  23. }
  24. async function capturePayload(
  25. model: Model<"bedrock-converse-stream">,
  26. options?: BedrockOptions,
  27. ): Promise<BedrockThinkingPayload> {
  28. let capturedPayload: BedrockThinkingPayload | undefined;
  29. const s = streamBedrock(model, makeContext(), {
  30. ...options,
  31. reasoning: options?.reasoning ?? "high",
  32. onPayload: (payload) => {
  33. capturedPayload = payload as BedrockThinkingPayload;
  34. throw new PayloadCaptured();
  35. },
  36. });
  37. for await (const event of s) {
  38. if (event.type === "error") {
  39. break;
  40. }
  41. }
  42. if (!capturedPayload) {
  43. throw new Error("Expected Bedrock payload to be captured before request abort");
  44. }
  45. return capturedPayload;
  46. }
  47. describe("Bedrock thinking payload", () => {
  48. it("uses adaptive thinking for Claude Opus 4.8 when reasoning is enabled", async () => {
  49. const baseModel = getModel("amazon-bedrock", "global.anthropic.claude-opus-4-6-v1");
  50. const model: Model<"bedrock-converse-stream"> = {
  51. ...baseModel,
  52. id: "global.anthropic.claude-opus-4-8-v1",
  53. name: "Claude Opus 4.8 (Global)",
  54. };
  55. const payload = await capturePayload(model);
  56. expect(payload.additionalModelRequestFields?.thinking).toEqual({ type: "adaptive", display: "summarized" });
  57. expect(payload.additionalModelRequestFields?.output_config).toEqual({ effort: "high" });
  58. expect(payload.additionalModelRequestFields?.anthropic_beta).toBeUndefined();
  59. });
  60. it("maps xhigh reasoning to effort=xhigh for Claude Opus 4.8", async () => {
  61. const baseModel = getModel("amazon-bedrock", "global.anthropic.claude-opus-4-6-v1");
  62. const model: Model<"bedrock-converse-stream"> = {
  63. ...baseModel,
  64. id: "global.anthropic.claude-opus-4-8-v1",
  65. name: "Claude Opus 4.8 (Global)",
  66. };
  67. const payload = await capturePayload(model, { reasoning: "xhigh" });
  68. expect(payload.additionalModelRequestFields?.thinking).toEqual({ type: "adaptive", display: "summarized" });
  69. expect(payload.additionalModelRequestFields?.output_config).toEqual({ effort: "xhigh" });
  70. expect(payload.additionalModelRequestFields?.anthropic_beta).toBeUndefined();
  71. });
  72. it("uses adaptive thinking for Claude Fable 5 when reasoning is enabled", async () => {
  73. const model = getModel("amazon-bedrock", "global.anthropic.claude-fable-5");
  74. const payload = await capturePayload(model);
  75. expect(payload.additionalModelRequestFields?.thinking).toEqual({ type: "adaptive", display: "summarized" });
  76. expect(payload.additionalModelRequestFields?.output_config).toEqual({ effort: "high" });
  77. expect(payload.additionalModelRequestFields?.anthropic_beta).toBeUndefined();
  78. });
  79. it("maps xhigh reasoning to effort=xhigh for Claude Fable 5", async () => {
  80. const model = getModel("amazon-bedrock", "global.anthropic.claude-fable-5");
  81. const payload = await capturePayload(model, { reasoning: "xhigh" });
  82. expect(payload.additionalModelRequestFields?.thinking).toEqual({ type: "adaptive", display: "summarized" });
  83. expect(payload.additionalModelRequestFields?.output_config).toEqual({ effort: "xhigh" });
  84. });
  85. it("omits display for GovCloud model ids on non-adaptive Claude thinking", async () => {
  86. const baseModel = getModel("amazon-bedrock", "us.anthropic.claude-sonnet-4-5-20250929-v1:0");
  87. const model: Model<"bedrock-converse-stream"> = {
  88. ...baseModel,
  89. id: "us-gov.anthropic.claude-sonnet-4-5-20250929-v1:0",
  90. name: "Claude Sonnet 4.5 (GovCloud)",
  91. };
  92. const payload = await capturePayload(model);
  93. expect(payload.additionalModelRequestFields?.thinking).toEqual({ type: "enabled", budget_tokens: 16384 });
  94. expect(payload.additionalModelRequestFields?.anthropic_beta).toEqual(["interleaved-thinking-2025-05-14"]);
  95. });
  96. it("omits display for GovCloud regions on adaptive Claude thinking", async () => {
  97. const baseModel = getModel("amazon-bedrock", "global.anthropic.claude-opus-4-6-v1");
  98. const model: Model<"bedrock-converse-stream"> = {
  99. ...baseModel,
  100. id: "global.anthropic.claude-opus-4-8-v1",
  101. name: "Claude Opus 4.8 (Global)",
  102. };
  103. const payload = await capturePayload(model, { region: "us-gov-west-1" });
  104. expect(payload.additionalModelRequestFields?.thinking).toEqual({ type: "adaptive" });
  105. expect(payload.additionalModelRequestFields?.output_config).toEqual({ effort: "high" });
  106. expect(payload.additionalModelRequestFields?.anthropic_beta).toBeUndefined();
  107. });
  108. });
  109. describe.skipIf(!hasBedrockCredentials())("Bedrock Claude max tokens E2E", () => {
  110. it(
  111. "uses the model maxTokens cap instead of Bedrock's 4096-token default for adaptive Claude models",
  112. { retry: 2, timeout: 180000 },
  113. async () => {
  114. const baseModel = getModel("amazon-bedrock", "global.anthropic.claude-sonnet-4-6");
  115. const model: Model<"bedrock-converse-stream"> = {
  116. ...baseModel,
  117. maxTokens: 6000,
  118. };
  119. const response = await streamBedrock(
  120. model,
  121. {
  122. systemPrompt: "You are a deterministic text generator. Follow the requested output format exactly.",
  123. messages: [
  124. {
  125. role: "user",
  126. content:
  127. "Output exactly 5200 repetitions of the token alpha, separated by single spaces. Do not number them. Do not use markdown. Do not add any other text.",
  128. timestamp: Date.now(),
  129. },
  130. ],
  131. },
  132. { reasoning: "low" },
  133. ).result();
  134. expect(response.stopReason, response.errorMessage).not.toBe("error");
  135. expect(response.usage.output).toBeGreaterThan(4096);
  136. },
  137. );
  138. });
  139. describe("Application inference profile support", () => {
  140. it("uses adaptive thinking when model.name contains the model name but ARN does not", async () => {
  141. const baseModel = getModel("amazon-bedrock", "global.anthropic.claude-opus-4-6-v1");
  142. const model: Model<"bedrock-converse-stream"> = {
  143. ...baseModel,
  144. id: "arn:aws:bedrock:us-east-1:123456789012:application-inference-profile/my-profile",
  145. name: "Claude Opus 4.6",
  146. };
  147. const payload = await capturePayload(model);
  148. expect(payload.additionalModelRequestFields?.thinking).toEqual({ type: "adaptive", display: "summarized" });
  149. expect(payload.additionalModelRequestFields?.output_config).toEqual({ effort: "high" });
  150. });
  151. it("injects cache points when model.name identifies a supported Claude model", async () => {
  152. const baseModel = getModel("amazon-bedrock", "global.anthropic.claude-opus-4-6-v1");
  153. const model: Model<"bedrock-converse-stream"> = {
  154. ...baseModel,
  155. id: "arn:aws:bedrock:us-east-1:123456789012:application-inference-profile/my-profile",
  156. name: "Claude Sonnet 4.6",
  157. };
  158. let capturedPayload: any;
  159. const s = streamBedrock(
  160. model,
  161. {
  162. systemPrompt: "You are helpful.",
  163. messages: [{ role: "user", content: "Hello", timestamp: Date.now() }],
  164. },
  165. {
  166. onPayload: (payload) => {
  167. capturedPayload = payload;
  168. throw new PayloadCaptured();
  169. },
  170. },
  171. );
  172. for await (const event of s) {
  173. if (event.type === "error") break;
  174. }
  175. // System prompt should have a cache point
  176. expect(capturedPayload.system).toHaveLength(2);
  177. expect(capturedPayload.system[1]).toHaveProperty("cachePoint");
  178. // Last user message should have a cache point
  179. const lastMsg = capturedPayload.messages[capturedPayload.messages.length - 1];
  180. const lastContent = lastMsg.content[lastMsg.content.length - 1];
  181. expect(lastContent).toHaveProperty("cachePoint");
  182. });
  183. it("falls back to fixed-budget thinking for non-adaptive Claude via model.name", async () => {
  184. const baseModel = getModel("amazon-bedrock", "us.anthropic.claude-sonnet-4-5-20250929-v1:0");
  185. const model: Model<"bedrock-converse-stream"> = {
  186. ...baseModel,
  187. id: "arn:aws:bedrock:us-east-1:123456789012:application-inference-profile/my-profile",
  188. name: "Claude Sonnet 4.5",
  189. };
  190. const payload = await capturePayload(model);
  191. expect(payload.additionalModelRequestFields?.thinking).toMatchObject({
  192. type: "enabled",
  193. budget_tokens: expect.any(Number),
  194. });
  195. expect(payload.additionalModelRequestFields?.anthropic_beta).toEqual(["interleaved-thinking-2025-05-14"]);
  196. });
  197. });