@ai-sdk/amazon-bedrock 2.0.6 → 2.1.0

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,11 @@
1
1
  # @ai-sdk/amazon-bedrock
2
2
 
3
+ ## 2.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - cf7d818: feat (providers/amazon-bedrock): Add reasoning support to amazon-bedrock
8
+
3
9
  ## 2.0.6
4
10
 
5
11
  ### Patch Changes
package/dist/index.js CHANGED
@@ -326,11 +326,45 @@ function convertToBedrockChatMessages(prompt) {
326
326
  // trim the last text part if it's the last message in the block
327
327
  // because Bedrock does not allow trailing whitespace
328
328
  // in pre-filled assistant responses
329
- isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
329
+ trimIfLast(
330
+ isLastBlock,
331
+ isLastMessage,
332
+ isLastContentPart,
333
+ part.text
334
+ )
330
335
  )
331
336
  });
332
337
  break;
333
338
  }
339
+ case "reasoning": {
340
+ bedrockContent.push({
341
+ reasoningContent: {
342
+ reasoningText: {
343
+ // trim the last text part if it's the last message in the block
344
+ // because Bedrock does not allow trailing whitespace
345
+ // in pre-filled assistant responses
346
+ text: trimIfLast(
347
+ isLastBlock,
348
+ isLastMessage,
349
+ isLastContentPart,
350
+ part.text
351
+ ),
352
+ signature: part.signature
353
+ }
354
+ }
355
+ });
356
+ break;
357
+ }
358
+ case "redacted-reasoning": {
359
+ bedrockContent.push({
360
+ reasoningContent: {
361
+ redactedReasoning: {
362
+ data: part.data
363
+ }
364
+ }
365
+ });
366
+ break;
367
+ }
334
368
  case "tool-call": {
335
369
  bedrockContent.push({
336
370
  toolUse: {
@@ -358,6 +392,9 @@ function convertToBedrockChatMessages(prompt) {
358
392
  }
359
393
  return { system, messages };
360
394
  }
395
+ function trimIfLast(isLastBlock, isLastMessage, isLastContentPart, text) {
396
+ return isLastBlock && isLastMessage && isLastContentPart ? text.trim() : text;
397
+ }
361
398
  function groupIntoBlocks(prompt) {
362
399
  const blocks = [];
363
400
  let currentBlock = void 0;
@@ -450,7 +487,7 @@ var BedrockChatLanguageModel = class {
450
487
  providerMetadata,
451
488
  headers
452
489
  }) {
453
- var _a;
490
+ var _a, _b, _c, _d;
454
491
  const type = mode.type;
455
492
  const warnings = [];
456
493
  if (frequencyPenalty != null) {
@@ -485,12 +522,51 @@ var BedrockChatLanguageModel = class {
485
522
  });
486
523
  }
487
524
  const { system, messages } = convertToBedrockChatMessages(prompt);
525
+ const reasoningConfigOptions = BedrockReasoningConfigOptionsSchema.safeParse(
526
+ (_a = providerMetadata == null ? void 0 : providerMetadata.bedrock) == null ? void 0 : _a.reasoning_config
527
+ );
528
+ if (!reasoningConfigOptions.success) {
529
+ throw new import_provider4.InvalidArgumentError({
530
+ argument: "providerOptions.bedrock.reasoning_config",
531
+ message: "invalid reasoning configuration options",
532
+ cause: reasoningConfigOptions.error
533
+ });
534
+ }
535
+ const isThinking = ((_b = reasoningConfigOptions.data) == null ? void 0 : _b.type) === "enabled";
536
+ const thinkingBudget = (_c = reasoningConfigOptions.data) == null ? void 0 : _c.budget_tokens;
488
537
  const inferenceConfig = {
489
538
  ...maxTokens != null && { maxTokens },
490
539
  ...temperature != null && { temperature },
491
540
  ...topP != null && { topP },
492
541
  ...stopSequences != null && { stopSequences }
493
542
  };
543
+ if (isThinking && thinkingBudget != null) {
544
+ if (inferenceConfig.maxTokens != null) {
545
+ inferenceConfig.maxTokens += thinkingBudget;
546
+ } else {
547
+ inferenceConfig.maxTokens = thinkingBudget + 4096;
548
+ }
549
+ this.settings.additionalModelRequestFields = {
550
+ ...this.settings.additionalModelRequestFields,
551
+ reasoning_config: { ...reasoningConfigOptions.data }
552
+ };
553
+ }
554
+ if (isThinking && inferenceConfig.temperature != null) {
555
+ delete inferenceConfig.temperature;
556
+ warnings.push({
557
+ type: "unsupported-setting",
558
+ setting: "temperature",
559
+ details: "temperature is not supported when thinking is enabled"
560
+ });
561
+ }
562
+ if (isThinking && inferenceConfig.topP != null) {
563
+ delete inferenceConfig.topP;
564
+ warnings.push({
565
+ type: "unsupported-setting",
566
+ setting: "topP",
567
+ details: "topP is not supported when thinking is enabled"
568
+ });
569
+ }
494
570
  const baseArgs = {
495
571
  system,
496
572
  additionalModelRequestFields: this.settings.additionalModelRequestFields,
@@ -506,7 +582,7 @@ var BedrockChatLanguageModel = class {
506
582
  return {
507
583
  command: {
508
584
  ...baseArgs,
509
- ...((_a = toolConfig.tools) == null ? void 0 : _a.length) ? { toolConfig } : {}
585
+ ...((_d = toolConfig.tools) == null ? void 0 : _d.length) ? { toolConfig } : {}
510
586
  },
511
587
  warnings: [...warnings, ...toolWarnings]
512
588
  };
@@ -580,6 +656,25 @@ var BedrockChatLanguageModel = class {
580
656
  }
581
657
  }
582
658
  } : void 0;
659
+ const reasoning = response.output.message.content.filter((content) => content.reasoningContent).map((content) => {
660
+ var _a2;
661
+ if (content.reasoningContent && "reasoningText" in content.reasoningContent) {
662
+ return {
663
+ type: "text",
664
+ text: content.reasoningContent.reasoningText.text,
665
+ ...content.reasoningContent.reasoningText.signature && {
666
+ signature: content.reasoningContent.reasoningText.signature
667
+ }
668
+ };
669
+ } else if (content.reasoningContent && "redactedReasoning" in content.reasoningContent) {
670
+ return {
671
+ type: "redacted",
672
+ data: (_a2 = content.reasoningContent.redactedReasoning.data) != null ? _a2 : ""
673
+ };
674
+ } else {
675
+ return void 0;
676
+ }
677
+ }).filter((item) => item !== void 0);
583
678
  return {
584
679
  text: (_h = (_g = (_f = (_e = response.output) == null ? void 0 : _e.message) == null ? void 0 : _f.content) == null ? void 0 : _g.map((part) => {
585
680
  var _a2;
@@ -604,6 +699,7 @@ var BedrockChatLanguageModel = class {
604
699
  rawCall: { rawPrompt, rawSettings },
605
700
  rawResponse: { headers: responseHeaders },
606
701
  warnings,
702
+ reasoning: reasoning.length > 0 ? reasoning : void 0,
607
703
  ...providerMetadata && { providerMetadata }
608
704
  };
609
705
  }
@@ -637,7 +733,7 @@ var BedrockChatLanguageModel = class {
637
733
  stream: response.pipeThrough(
638
734
  new TransformStream({
639
735
  transform(chunk, controller) {
640
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
736
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
641
737
  function enqueueError(bedrockError) {
642
738
  finishReason = "error";
643
739
  controller.enqueue({ type: "error", error: bedrockError });
@@ -697,8 +793,27 @@ var BedrockChatLanguageModel = class {
697
793
  textDelta: value.contentBlockDelta.delta.text
698
794
  });
699
795
  }
796
+ if (((_l = value.contentBlockDelta) == null ? void 0 : _l.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
797
+ const reasoningContent = value.contentBlockDelta.delta.reasoningContent;
798
+ if ("text" in reasoningContent && reasoningContent.text) {
799
+ controller.enqueue({
800
+ type: "reasoning",
801
+ textDelta: reasoningContent.text
802
+ });
803
+ } else if ("signature" in reasoningContent && reasoningContent.signature) {
804
+ controller.enqueue({
805
+ type: "reasoning-signature",
806
+ signature: reasoningContent.signature
807
+ });
808
+ } else if ("data" in reasoningContent && reasoningContent.data) {
809
+ controller.enqueue({
810
+ type: "redacted-reasoning",
811
+ data: reasoningContent.data
812
+ });
813
+ }
814
+ }
700
815
  const contentBlockStart = value.contentBlockStart;
701
- if (((_l = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _l.toolUse) != null) {
816
+ if (((_m = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _m.toolUse) != null) {
702
817
  const toolUse = contentBlockStart.start.toolUse;
703
818
  toolCallContentBlocks[contentBlockStart.contentBlockIndex] = {
704
819
  toolCallId: toolUse.toolUseId,
@@ -709,7 +824,7 @@ var BedrockChatLanguageModel = class {
709
824
  const contentBlockDelta = value.contentBlockDelta;
710
825
  if ((contentBlockDelta == null ? void 0 : contentBlockDelta.delta) && "toolUse" in contentBlockDelta.delta && contentBlockDelta.delta.toolUse) {
711
826
  const contentBlock = toolCallContentBlocks[contentBlockDelta.contentBlockIndex];
712
- const delta = (_m = contentBlockDelta.delta.toolUse.input) != null ? _m : "";
827
+ const delta = (_n = contentBlockDelta.delta.toolUse.input) != null ? _n : "";
713
828
  controller.enqueue({
714
829
  type: "tool-call-delta",
715
830
  toolCallType: "function",
@@ -755,6 +870,10 @@ var BedrockChatLanguageModel = class {
755
870
  return `${this.config.baseUrl()}/model/${encodedModelId}`;
756
871
  }
757
872
  };
873
+ var BedrockReasoningConfigOptionsSchema = import_zod2.z.object({
874
+ type: import_zod2.z.union([import_zod2.z.literal("enabled"), import_zod2.z.literal("disabled")]),
875
+ budget_tokens: import_zod2.z.number().nullish()
876
+ }).nullish();
758
877
  var BedrockStopReasonSchema = import_zod2.z.union([
759
878
  import_zod2.z.enum(BEDROCK_STOP_REASONS),
760
879
  import_zod2.z.string()
@@ -764,6 +883,13 @@ var BedrockToolUseSchema = import_zod2.z.object({
764
883
  name: import_zod2.z.string(),
765
884
  input: import_zod2.z.unknown()
766
885
  });
886
+ var BedrockReasoningTextSchema = import_zod2.z.object({
887
+ signature: import_zod2.z.string().nullish(),
888
+ text: import_zod2.z.string()
889
+ });
890
+ var BedrockRedactedReasoningSchema = import_zod2.z.object({
891
+ data: import_zod2.z.string()
892
+ });
767
893
  var BedrockResponseSchema = import_zod2.z.object({
768
894
  metrics: import_zod2.z.object({
769
895
  latencyMs: import_zod2.z.number()
@@ -773,7 +899,15 @@ var BedrockResponseSchema = import_zod2.z.object({
773
899
  content: import_zod2.z.array(
774
900
  import_zod2.z.object({
775
901
  text: import_zod2.z.string().nullish(),
776
- toolUse: BedrockToolUseSchema.nullish()
902
+ toolUse: BedrockToolUseSchema.nullish(),
903
+ reasoningContent: import_zod2.z.union([
904
+ import_zod2.z.object({
905
+ reasoningText: BedrockReasoningTextSchema
906
+ }),
907
+ import_zod2.z.object({
908
+ redactedReasoning: BedrockRedactedReasoningSchema
909
+ })
910
+ ]).nullish()
777
911
  })
778
912
  ),
779
913
  role: import_zod2.z.string()
@@ -794,7 +928,18 @@ var BedrockStreamSchema = import_zod2.z.object({
794
928
  contentBlockIndex: import_zod2.z.number(),
795
929
  delta: import_zod2.z.union([
796
930
  import_zod2.z.object({ text: import_zod2.z.string() }),
797
- import_zod2.z.object({ toolUse: import_zod2.z.object({ input: import_zod2.z.string() }) })
931
+ import_zod2.z.object({ toolUse: import_zod2.z.object({ input: import_zod2.z.string() }) }),
932
+ import_zod2.z.object({
933
+ reasoningContent: import_zod2.z.object({ text: import_zod2.z.string() })
934
+ }),
935
+ import_zod2.z.object({
936
+ reasoningContent: import_zod2.z.object({
937
+ signature: import_zod2.z.string()
938
+ })
939
+ }),
940
+ import_zod2.z.object({
941
+ reasoningContent: import_zod2.z.object({ data: import_zod2.z.string() })
942
+ })
798
943
  ]).nullish()
799
944
  }).nullish(),
800
945
  contentBlockStart: import_zod2.z.object({