@ai-sdk/amazon-bedrock 2.0.5 → 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/dist/index.mjs CHANGED
@@ -8,6 +8,7 @@ import {
8
8
 
9
9
  // src/bedrock-chat-language-model.ts
10
10
  import {
11
+ InvalidArgumentError,
11
12
  UnsupportedFunctionalityError as UnsupportedFunctionalityError3
12
13
  } from "@ai-sdk/provider";
13
14
  import {
@@ -323,11 +324,45 @@ function convertToBedrockChatMessages(prompt) {
323
324
  // trim the last text part if it's the last message in the block
324
325
  // because Bedrock does not allow trailing whitespace
325
326
  // in pre-filled assistant responses
326
- isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
327
+ trimIfLast(
328
+ isLastBlock,
329
+ isLastMessage,
330
+ isLastContentPart,
331
+ part.text
332
+ )
327
333
  )
328
334
  });
329
335
  break;
330
336
  }
337
+ case "reasoning": {
338
+ bedrockContent.push({
339
+ reasoningContent: {
340
+ reasoningText: {
341
+ // trim the last text part if it's the last message in the block
342
+ // because Bedrock does not allow trailing whitespace
343
+ // in pre-filled assistant responses
344
+ text: trimIfLast(
345
+ isLastBlock,
346
+ isLastMessage,
347
+ isLastContentPart,
348
+ part.text
349
+ ),
350
+ signature: part.signature
351
+ }
352
+ }
353
+ });
354
+ break;
355
+ }
356
+ case "redacted-reasoning": {
357
+ bedrockContent.push({
358
+ reasoningContent: {
359
+ redactedReasoning: {
360
+ data: part.data
361
+ }
362
+ }
363
+ });
364
+ break;
365
+ }
331
366
  case "tool-call": {
332
367
  bedrockContent.push({
333
368
  toolUse: {
@@ -355,6 +390,9 @@ function convertToBedrockChatMessages(prompt) {
355
390
  }
356
391
  return { system, messages };
357
392
  }
393
+ function trimIfLast(isLastBlock, isLastMessage, isLastContentPart, text) {
394
+ return isLastBlock && isLastMessage && isLastContentPart ? text.trim() : text;
395
+ }
358
396
  function groupIntoBlocks(prompt) {
359
397
  const blocks = [];
360
398
  let currentBlock = void 0;
@@ -447,7 +485,7 @@ var BedrockChatLanguageModel = class {
447
485
  providerMetadata,
448
486
  headers
449
487
  }) {
450
- var _a;
488
+ var _a, _b, _c, _d;
451
489
  const type = mode.type;
452
490
  const warnings = [];
453
491
  if (frequencyPenalty != null) {
@@ -482,12 +520,51 @@ var BedrockChatLanguageModel = class {
482
520
  });
483
521
  }
484
522
  const { system, messages } = convertToBedrockChatMessages(prompt);
523
+ const reasoningConfigOptions = BedrockReasoningConfigOptionsSchema.safeParse(
524
+ (_a = providerMetadata == null ? void 0 : providerMetadata.bedrock) == null ? void 0 : _a.reasoning_config
525
+ );
526
+ if (!reasoningConfigOptions.success) {
527
+ throw new InvalidArgumentError({
528
+ argument: "providerOptions.bedrock.reasoning_config",
529
+ message: "invalid reasoning configuration options",
530
+ cause: reasoningConfigOptions.error
531
+ });
532
+ }
533
+ const isThinking = ((_b = reasoningConfigOptions.data) == null ? void 0 : _b.type) === "enabled";
534
+ const thinkingBudget = (_c = reasoningConfigOptions.data) == null ? void 0 : _c.budget_tokens;
485
535
  const inferenceConfig = {
486
536
  ...maxTokens != null && { maxTokens },
487
537
  ...temperature != null && { temperature },
488
538
  ...topP != null && { topP },
489
539
  ...stopSequences != null && { stopSequences }
490
540
  };
541
+ if (isThinking && thinkingBudget != null) {
542
+ if (inferenceConfig.maxTokens != null) {
543
+ inferenceConfig.maxTokens += thinkingBudget;
544
+ } else {
545
+ inferenceConfig.maxTokens = thinkingBudget + 4096;
546
+ }
547
+ this.settings.additionalModelRequestFields = {
548
+ ...this.settings.additionalModelRequestFields,
549
+ reasoning_config: { ...reasoningConfigOptions.data }
550
+ };
551
+ }
552
+ if (isThinking && inferenceConfig.temperature != null) {
553
+ delete inferenceConfig.temperature;
554
+ warnings.push({
555
+ type: "unsupported-setting",
556
+ setting: "temperature",
557
+ details: "temperature is not supported when thinking is enabled"
558
+ });
559
+ }
560
+ if (isThinking && inferenceConfig.topP != null) {
561
+ delete inferenceConfig.topP;
562
+ warnings.push({
563
+ type: "unsupported-setting",
564
+ setting: "topP",
565
+ details: "topP is not supported when thinking is enabled"
566
+ });
567
+ }
491
568
  const baseArgs = {
492
569
  system,
493
570
  additionalModelRequestFields: this.settings.additionalModelRequestFields,
@@ -503,7 +580,7 @@ var BedrockChatLanguageModel = class {
503
580
  return {
504
581
  command: {
505
582
  ...baseArgs,
506
- ...((_a = toolConfig.tools) == null ? void 0 : _a.length) ? { toolConfig } : {}
583
+ ...((_d = toolConfig.tools) == null ? void 0 : _d.length) ? { toolConfig } : {}
507
584
  },
508
585
  warnings: [...warnings, ...toolWarnings]
509
586
  };
@@ -577,6 +654,25 @@ var BedrockChatLanguageModel = class {
577
654
  }
578
655
  }
579
656
  } : void 0;
657
+ const reasoning = response.output.message.content.filter((content) => content.reasoningContent).map((content) => {
658
+ var _a2;
659
+ if (content.reasoningContent && "reasoningText" in content.reasoningContent) {
660
+ return {
661
+ type: "text",
662
+ text: content.reasoningContent.reasoningText.text,
663
+ ...content.reasoningContent.reasoningText.signature && {
664
+ signature: content.reasoningContent.reasoningText.signature
665
+ }
666
+ };
667
+ } else if (content.reasoningContent && "redactedReasoning" in content.reasoningContent) {
668
+ return {
669
+ type: "redacted",
670
+ data: (_a2 = content.reasoningContent.redactedReasoning.data) != null ? _a2 : ""
671
+ };
672
+ } else {
673
+ return void 0;
674
+ }
675
+ }).filter((item) => item !== void 0);
580
676
  return {
581
677
  text: (_h = (_g = (_f = (_e = response.output) == null ? void 0 : _e.message) == null ? void 0 : _f.content) == null ? void 0 : _g.map((part) => {
582
678
  var _a2;
@@ -601,6 +697,7 @@ var BedrockChatLanguageModel = class {
601
697
  rawCall: { rawPrompt, rawSettings },
602
698
  rawResponse: { headers: responseHeaders },
603
699
  warnings,
700
+ reasoning: reasoning.length > 0 ? reasoning : void 0,
604
701
  ...providerMetadata && { providerMetadata }
605
702
  };
606
703
  }
@@ -634,7 +731,7 @@ var BedrockChatLanguageModel = class {
634
731
  stream: response.pipeThrough(
635
732
  new TransformStream({
636
733
  transform(chunk, controller) {
637
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
734
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
638
735
  function enqueueError(bedrockError) {
639
736
  finishReason = "error";
640
737
  controller.enqueue({ type: "error", error: bedrockError });
@@ -694,8 +791,27 @@ var BedrockChatLanguageModel = class {
694
791
  textDelta: value.contentBlockDelta.delta.text
695
792
  });
696
793
  }
794
+ if (((_l = value.contentBlockDelta) == null ? void 0 : _l.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
795
+ const reasoningContent = value.contentBlockDelta.delta.reasoningContent;
796
+ if ("text" in reasoningContent && reasoningContent.text) {
797
+ controller.enqueue({
798
+ type: "reasoning",
799
+ textDelta: reasoningContent.text
800
+ });
801
+ } else if ("signature" in reasoningContent && reasoningContent.signature) {
802
+ controller.enqueue({
803
+ type: "reasoning-signature",
804
+ signature: reasoningContent.signature
805
+ });
806
+ } else if ("data" in reasoningContent && reasoningContent.data) {
807
+ controller.enqueue({
808
+ type: "redacted-reasoning",
809
+ data: reasoningContent.data
810
+ });
811
+ }
812
+ }
697
813
  const contentBlockStart = value.contentBlockStart;
698
- if (((_l = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _l.toolUse) != null) {
814
+ if (((_m = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _m.toolUse) != null) {
699
815
  const toolUse = contentBlockStart.start.toolUse;
700
816
  toolCallContentBlocks[contentBlockStart.contentBlockIndex] = {
701
817
  toolCallId: toolUse.toolUseId,
@@ -706,7 +822,7 @@ var BedrockChatLanguageModel = class {
706
822
  const contentBlockDelta = value.contentBlockDelta;
707
823
  if ((contentBlockDelta == null ? void 0 : contentBlockDelta.delta) && "toolUse" in contentBlockDelta.delta && contentBlockDelta.delta.toolUse) {
708
824
  const contentBlock = toolCallContentBlocks[contentBlockDelta.contentBlockIndex];
709
- const delta = (_m = contentBlockDelta.delta.toolUse.input) != null ? _m : "";
825
+ const delta = (_n = contentBlockDelta.delta.toolUse.input) != null ? _n : "";
710
826
  controller.enqueue({
711
827
  type: "tool-call-delta",
712
828
  toolCallType: "function",
@@ -752,6 +868,10 @@ var BedrockChatLanguageModel = class {
752
868
  return `${this.config.baseUrl()}/model/${encodedModelId}`;
753
869
  }
754
870
  };
871
+ var BedrockReasoningConfigOptionsSchema = z2.object({
872
+ type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
873
+ budget_tokens: z2.number().nullish()
874
+ }).nullish();
755
875
  var BedrockStopReasonSchema = z2.union([
756
876
  z2.enum(BEDROCK_STOP_REASONS),
757
877
  z2.string()
@@ -761,6 +881,13 @@ var BedrockToolUseSchema = z2.object({
761
881
  name: z2.string(),
762
882
  input: z2.unknown()
763
883
  });
884
+ var BedrockReasoningTextSchema = z2.object({
885
+ signature: z2.string().nullish(),
886
+ text: z2.string()
887
+ });
888
+ var BedrockRedactedReasoningSchema = z2.object({
889
+ data: z2.string()
890
+ });
764
891
  var BedrockResponseSchema = z2.object({
765
892
  metrics: z2.object({
766
893
  latencyMs: z2.number()
@@ -770,7 +897,15 @@ var BedrockResponseSchema = z2.object({
770
897
  content: z2.array(
771
898
  z2.object({
772
899
  text: z2.string().nullish(),
773
- toolUse: BedrockToolUseSchema.nullish()
900
+ toolUse: BedrockToolUseSchema.nullish(),
901
+ reasoningContent: z2.union([
902
+ z2.object({
903
+ reasoningText: BedrockReasoningTextSchema
904
+ }),
905
+ z2.object({
906
+ redactedReasoning: BedrockRedactedReasoningSchema
907
+ })
908
+ ]).nullish()
774
909
  })
775
910
  ),
776
911
  role: z2.string()
@@ -791,7 +926,18 @@ var BedrockStreamSchema = z2.object({
791
926
  contentBlockIndex: z2.number(),
792
927
  delta: z2.union([
793
928
  z2.object({ text: z2.string() }),
794
- z2.object({ toolUse: z2.object({ input: z2.string() }) })
929
+ z2.object({ toolUse: z2.object({ input: z2.string() }) }),
930
+ z2.object({
931
+ reasoningContent: z2.object({ text: z2.string() })
932
+ }),
933
+ z2.object({
934
+ reasoningContent: z2.object({
935
+ signature: z2.string()
936
+ })
937
+ }),
938
+ z2.object({
939
+ reasoningContent: z2.object({ data: z2.string() })
940
+ })
795
941
  ]).nullish()
796
942
  }).nullish(),
797
943
  contentBlockStart: z2.object({