@ai-sdk/amazon-bedrock 2.0.6 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @ai-sdk/amazon-bedrock
2
2
 
3
+ ## 2.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - a841484: fix (provider/bedrock): support budgetTokens
8
+
9
+ ## 2.1.0
10
+
11
+ ### Minor Changes
12
+
13
+ - cf7d818: feat (providers/amazon-bedrock): Add reasoning support to amazon-bedrock
14
+
3
15
  ## 2.0.6
4
16
 
5
17
  ### Patch Changes
package/dist/index.js CHANGED
@@ -31,6 +31,7 @@ var import_provider_utils7 = require("@ai-sdk/provider-utils");
31
31
  // src/bedrock-chat-language-model.ts
32
32
  var import_provider4 = require("@ai-sdk/provider");
33
33
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
34
+ var import_zod2 = require("zod");
34
35
 
35
36
  // src/bedrock-api-types.ts
36
37
  var BEDROCK_CACHE_POINT = {
@@ -326,11 +327,45 @@ function convertToBedrockChatMessages(prompt) {
326
327
  // trim the last text part if it's the last message in the block
327
328
  // because Bedrock does not allow trailing whitespace
328
329
  // in pre-filled assistant responses
329
- isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
330
+ trimIfLast(
331
+ isLastBlock,
332
+ isLastMessage,
333
+ isLastContentPart,
334
+ part.text
335
+ )
330
336
  )
331
337
  });
332
338
  break;
333
339
  }
340
+ case "reasoning": {
341
+ bedrockContent.push({
342
+ reasoningContent: {
343
+ reasoningText: {
344
+ // trim the last text part if it's the last message in the block
345
+ // because Bedrock does not allow trailing whitespace
346
+ // in pre-filled assistant responses
347
+ text: trimIfLast(
348
+ isLastBlock,
349
+ isLastMessage,
350
+ isLastContentPart,
351
+ part.text
352
+ ),
353
+ signature: part.signature
354
+ }
355
+ }
356
+ });
357
+ break;
358
+ }
359
+ case "redacted-reasoning": {
360
+ bedrockContent.push({
361
+ reasoningContent: {
362
+ redactedReasoning: {
363
+ data: part.data
364
+ }
365
+ }
366
+ });
367
+ break;
368
+ }
334
369
  case "tool-call": {
335
370
  bedrockContent.push({
336
371
  toolUse: {
@@ -358,6 +393,9 @@ function convertToBedrockChatMessages(prompt) {
358
393
  }
359
394
  return { system, messages };
360
395
  }
396
+ function trimIfLast(isLastBlock, isLastMessage, isLastContentPart, text) {
397
+ return isLastBlock && isLastMessage && isLastContentPart ? text.trim() : text;
398
+ }
361
399
  function groupIntoBlocks(prompt) {
362
400
  const blocks = [];
363
401
  let currentBlock = void 0;
@@ -424,7 +462,6 @@ function mapBedrockFinishReason(finishReason) {
424
462
  }
425
463
 
426
464
  // src/bedrock-chat-language-model.ts
427
- var import_zod2 = require("zod");
428
465
  var BedrockChatLanguageModel = class {
429
466
  constructor(modelId, settings, config) {
430
467
  this.modelId = modelId;
@@ -447,10 +484,9 @@ var BedrockChatLanguageModel = class {
447
484
  stopSequences,
448
485
  responseFormat,
449
486
  seed,
450
- providerMetadata,
451
- headers
487
+ providerMetadata
452
488
  }) {
453
- var _a;
489
+ var _a, _b, _c, _d, _e, _f, _g;
454
490
  const type = mode.type;
455
491
  const warnings = [];
456
492
  if (frequencyPenalty != null) {
@@ -485,12 +521,54 @@ var BedrockChatLanguageModel = class {
485
521
  });
486
522
  }
487
523
  const { system, messages } = convertToBedrockChatMessages(prompt);
524
+ const reasoningConfigOptions = BedrockReasoningConfigOptionsSchema.safeParse(
525
+ (_a = providerMetadata == null ? void 0 : providerMetadata.bedrock) == null ? void 0 : _a.reasoning_config
526
+ );
527
+ if (!reasoningConfigOptions.success) {
528
+ throw new import_provider4.InvalidArgumentError({
529
+ argument: "providerOptions.bedrock.reasoning_config",
530
+ message: "invalid reasoning configuration options",
531
+ cause: reasoningConfigOptions.error
532
+ });
533
+ }
534
+ const isThinking = ((_b = reasoningConfigOptions.data) == null ? void 0 : _b.type) === "enabled";
535
+ const thinkingBudget = (_e = (_c = reasoningConfigOptions.data) == null ? void 0 : _c.budgetTokens) != null ? _e : (_d = reasoningConfigOptions.data) == null ? void 0 : _d.budget_tokens;
488
536
  const inferenceConfig = {
489
537
  ...maxTokens != null && { maxTokens },
490
538
  ...temperature != null && { temperature },
491
539
  ...topP != null && { topP },
492
540
  ...stopSequences != null && { stopSequences }
493
541
  };
542
+ if (isThinking && thinkingBudget != null) {
543
+ if (inferenceConfig.maxTokens != null) {
544
+ inferenceConfig.maxTokens += thinkingBudget;
545
+ } else {
546
+ inferenceConfig.maxTokens = thinkingBudget + 4096;
547
+ }
548
+ this.settings.additionalModelRequestFields = {
549
+ ...this.settings.additionalModelRequestFields,
550
+ reasoning_config: {
551
+ type: (_f = reasoningConfigOptions.data) == null ? void 0 : _f.type,
552
+ budget_tokens: thinkingBudget
553
+ }
554
+ };
555
+ }
556
+ if (isThinking && inferenceConfig.temperature != null) {
557
+ delete inferenceConfig.temperature;
558
+ warnings.push({
559
+ type: "unsupported-setting",
560
+ setting: "temperature",
561
+ details: "temperature is not supported when thinking is enabled"
562
+ });
563
+ }
564
+ if (isThinking && inferenceConfig.topP != null) {
565
+ delete inferenceConfig.topP;
566
+ warnings.push({
567
+ type: "unsupported-setting",
568
+ setting: "topP",
569
+ details: "topP is not supported when thinking is enabled"
570
+ });
571
+ }
494
572
  const baseArgs = {
495
573
  system,
496
574
  additionalModelRequestFields: this.settings.additionalModelRequestFields,
@@ -506,7 +584,7 @@ var BedrockChatLanguageModel = class {
506
584
  return {
507
585
  command: {
508
586
  ...baseArgs,
509
- ...((_a = toolConfig.tools) == null ? void 0 : _a.length) ? { toolConfig } : {}
587
+ ...((_g = toolConfig.tools) == null ? void 0 : _g.length) ? { toolConfig } : {}
510
588
  },
511
589
  warnings: [...warnings, ...toolWarnings]
512
590
  };
@@ -580,6 +658,25 @@ var BedrockChatLanguageModel = class {
580
658
  }
581
659
  }
582
660
  } : void 0;
661
+ const reasoning = response.output.message.content.filter((content) => content.reasoningContent).map((content) => {
662
+ var _a2;
663
+ if (content.reasoningContent && "reasoningText" in content.reasoningContent) {
664
+ return {
665
+ type: "text",
666
+ text: content.reasoningContent.reasoningText.text,
667
+ ...content.reasoningContent.reasoningText.signature && {
668
+ signature: content.reasoningContent.reasoningText.signature
669
+ }
670
+ };
671
+ } else if (content.reasoningContent && "redactedReasoning" in content.reasoningContent) {
672
+ return {
673
+ type: "redacted",
674
+ data: (_a2 = content.reasoningContent.redactedReasoning.data) != null ? _a2 : ""
675
+ };
676
+ } else {
677
+ return void 0;
678
+ }
679
+ }).filter((item) => item !== void 0);
583
680
  return {
584
681
  text: (_h = (_g = (_f = (_e = response.output) == null ? void 0 : _e.message) == null ? void 0 : _f.content) == null ? void 0 : _g.map((part) => {
585
682
  var _a2;
@@ -604,6 +701,7 @@ var BedrockChatLanguageModel = class {
604
701
  rawCall: { rawPrompt, rawSettings },
605
702
  rawResponse: { headers: responseHeaders },
606
703
  warnings,
704
+ reasoning: reasoning.length > 0 ? reasoning : void 0,
607
705
  ...providerMetadata && { providerMetadata }
608
706
  };
609
707
  }
@@ -637,7 +735,7 @@ var BedrockChatLanguageModel = class {
637
735
  stream: response.pipeThrough(
638
736
  new TransformStream({
639
737
  transform(chunk, controller) {
640
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
738
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
641
739
  function enqueueError(bedrockError) {
642
740
  finishReason = "error";
643
741
  controller.enqueue({ type: "error", error: bedrockError });
@@ -697,8 +795,27 @@ var BedrockChatLanguageModel = class {
697
795
  textDelta: value.contentBlockDelta.delta.text
698
796
  });
699
797
  }
798
+ if (((_l = value.contentBlockDelta) == null ? void 0 : _l.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
799
+ const reasoningContent = value.contentBlockDelta.delta.reasoningContent;
800
+ if ("text" in reasoningContent && reasoningContent.text) {
801
+ controller.enqueue({
802
+ type: "reasoning",
803
+ textDelta: reasoningContent.text
804
+ });
805
+ } else if ("signature" in reasoningContent && reasoningContent.signature) {
806
+ controller.enqueue({
807
+ type: "reasoning-signature",
808
+ signature: reasoningContent.signature
809
+ });
810
+ } else if ("data" in reasoningContent && reasoningContent.data) {
811
+ controller.enqueue({
812
+ type: "redacted-reasoning",
813
+ data: reasoningContent.data
814
+ });
815
+ }
816
+ }
700
817
  const contentBlockStart = value.contentBlockStart;
701
- if (((_l = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _l.toolUse) != null) {
818
+ if (((_m = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _m.toolUse) != null) {
702
819
  const toolUse = contentBlockStart.start.toolUse;
703
820
  toolCallContentBlocks[contentBlockStart.contentBlockIndex] = {
704
821
  toolCallId: toolUse.toolUseId,
@@ -709,7 +826,7 @@ var BedrockChatLanguageModel = class {
709
826
  const contentBlockDelta = value.contentBlockDelta;
710
827
  if ((contentBlockDelta == null ? void 0 : contentBlockDelta.delta) && "toolUse" in contentBlockDelta.delta && contentBlockDelta.delta.toolUse) {
711
828
  const contentBlock = toolCallContentBlocks[contentBlockDelta.contentBlockIndex];
712
- const delta = (_m = contentBlockDelta.delta.toolUse.input) != null ? _m : "";
829
+ const delta = (_n = contentBlockDelta.delta.toolUse.input) != null ? _n : "";
713
830
  controller.enqueue({
714
831
  type: "tool-call-delta",
715
832
  toolCallType: "function",
@@ -755,6 +872,11 @@ var BedrockChatLanguageModel = class {
755
872
  return `${this.config.baseUrl()}/model/${encodedModelId}`;
756
873
  }
757
874
  };
875
+ var BedrockReasoningConfigOptionsSchema = import_zod2.z.object({
876
+ type: import_zod2.z.union([import_zod2.z.literal("enabled"), import_zod2.z.literal("disabled")]).nullish(),
877
+ budget_tokens: import_zod2.z.number().nullish(),
878
+ budgetTokens: import_zod2.z.number().nullish()
879
+ }).nullish();
758
880
  var BedrockStopReasonSchema = import_zod2.z.union([
759
881
  import_zod2.z.enum(BEDROCK_STOP_REASONS),
760
882
  import_zod2.z.string()
@@ -764,6 +886,13 @@ var BedrockToolUseSchema = import_zod2.z.object({
764
886
  name: import_zod2.z.string(),
765
887
  input: import_zod2.z.unknown()
766
888
  });
889
+ var BedrockReasoningTextSchema = import_zod2.z.object({
890
+ signature: import_zod2.z.string().nullish(),
891
+ text: import_zod2.z.string()
892
+ });
893
+ var BedrockRedactedReasoningSchema = import_zod2.z.object({
894
+ data: import_zod2.z.string()
895
+ });
767
896
  var BedrockResponseSchema = import_zod2.z.object({
768
897
  metrics: import_zod2.z.object({
769
898
  latencyMs: import_zod2.z.number()
@@ -773,7 +902,15 @@ var BedrockResponseSchema = import_zod2.z.object({
773
902
  content: import_zod2.z.array(
774
903
  import_zod2.z.object({
775
904
  text: import_zod2.z.string().nullish(),
776
- toolUse: BedrockToolUseSchema.nullish()
905
+ toolUse: BedrockToolUseSchema.nullish(),
906
+ reasoningContent: import_zod2.z.union([
907
+ import_zod2.z.object({
908
+ reasoningText: BedrockReasoningTextSchema
909
+ }),
910
+ import_zod2.z.object({
911
+ redactedReasoning: BedrockRedactedReasoningSchema
912
+ })
913
+ ]).nullish()
777
914
  })
778
915
  ),
779
916
  role: import_zod2.z.string()
@@ -794,7 +931,18 @@ var BedrockStreamSchema = import_zod2.z.object({
794
931
  contentBlockIndex: import_zod2.z.number(),
795
932
  delta: import_zod2.z.union([
796
933
  import_zod2.z.object({ text: import_zod2.z.string() }),
797
- import_zod2.z.object({ toolUse: import_zod2.z.object({ input: import_zod2.z.string() }) })
934
+ import_zod2.z.object({ toolUse: import_zod2.z.object({ input: import_zod2.z.string() }) }),
935
+ import_zod2.z.object({
936
+ reasoningContent: import_zod2.z.object({ text: import_zod2.z.string() })
937
+ }),
938
+ import_zod2.z.object({
939
+ reasoningContent: import_zod2.z.object({
940
+ signature: import_zod2.z.string()
941
+ })
942
+ }),
943
+ import_zod2.z.object({
944
+ reasoningContent: import_zod2.z.object({ data: import_zod2.z.string() })
945
+ })
798
946
  ]).nullish()
799
947
  }).nullish(),
800
948
  contentBlockStart: import_zod2.z.object({