@ai-sdk/amazon-bedrock 3.0.0-beta.10 → 3.0.0-beta.12

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
@@ -172,7 +172,7 @@ function prepareTools({
172
172
  if (tools == null) {
173
173
  return {
174
174
  toolConfig: {
175
- tools: hasToolContent ? [] : void 0,
175
+ tools: void 0,
176
176
  toolChoice: void 0
177
177
  },
178
178
  toolWarnings: []
@@ -216,7 +216,7 @@ function prepareTools({
216
216
  case "none":
217
217
  return {
218
218
  toolConfig: {
219
- tools: hasToolContent ? [] : void 0,
219
+ tools: void 0,
220
220
  toolChoice: void 0
221
221
  },
222
222
  toolWarnings
@@ -623,14 +623,27 @@ var BedrockChatLanguageModel = class {
623
623
  setting: "topK"
624
624
  });
625
625
  }
626
- if (responseFormat != null && responseFormat.type !== "text") {
626
+ if (responseFormat != null && responseFormat.type !== "text" && responseFormat.type !== "json") {
627
627
  warnings.push({
628
628
  type: "unsupported-setting",
629
629
  setting: "responseFormat",
630
- details: "JSON response format is not supported."
630
+ details: "Only text and json response formats are supported."
631
631
  });
632
632
  }
633
- const { system, messages } = await convertToBedrockChatMessages(prompt);
633
+ if (tools != null && (responseFormat == null ? void 0 : responseFormat.type) === "json") {
634
+ if (tools.length > 0) {
635
+ warnings.push({
636
+ type: "other",
637
+ message: "JSON response format does not support tools. The provided tools are ignored."
638
+ });
639
+ }
640
+ }
641
+ const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
642
+ type: "function",
643
+ name: "json",
644
+ description: "Respond with a JSON object.",
645
+ inputSchema: responseFormat.schema
646
+ } : void 0;
634
647
  const isThinking = ((_b = bedrockOptions.reasoningConfig) == null ? void 0 : _b.type) === "enabled";
635
648
  const thinkingBudget = (_c = bedrockOptions.reasoningConfig) == null ? void 0 : _c.budgetTokens;
636
649
  const inferenceConfig = {
@@ -669,10 +682,37 @@ var BedrockChatLanguageModel = class {
669
682
  details: "topP is not supported when thinking is enabled"
670
683
  });
671
684
  }
685
+ const activeTools = jsonResponseTool != null ? [jsonResponseTool] : tools != null ? tools : [];
686
+ let filteredPrompt = prompt;
687
+ if (activeTools.length === 0) {
688
+ const hasToolContent = prompt.some(
689
+ (message) => "content" in message && Array.isArray(message.content) && message.content.some(
690
+ (part) => part.type === "tool-call" || part.type === "tool-result"
691
+ )
692
+ );
693
+ if (hasToolContent) {
694
+ filteredPrompt = prompt.map(
695
+ (message) => message.role === "system" ? message : {
696
+ ...message,
697
+ content: message.content.filter(
698
+ (part) => part.type !== "tool-call" && part.type !== "tool-result"
699
+ )
700
+ }
701
+ ).filter(
702
+ (message) => message.role === "system" || message.content.length > 0
703
+ );
704
+ warnings.push({
705
+ type: "unsupported-setting",
706
+ setting: "toolContent",
707
+ details: "Tool calls and results removed from conversation because Bedrock does not support tool content without active tools."
708
+ });
709
+ }
710
+ }
711
+ const { system, messages } = await convertToBedrockChatMessages(filteredPrompt);
672
712
  const { toolConfig, toolWarnings } = prepareTools({
673
- tools,
674
- toolChoice,
675
- prompt
713
+ tools: activeTools,
714
+ toolChoice: jsonResponseTool != null ? { type: "tool", toolName: jsonResponseTool.name } : toolChoice,
715
+ prompt: filteredPrompt
676
716
  });
677
717
  const { reasoningConfig: _, ...filteredBedrockOptions } = (providerOptions == null ? void 0 : providerOptions.bedrock) || {};
678
718
  return {
@@ -686,12 +726,17 @@ var BedrockChatLanguageModel = class {
686
726
  ...filteredBedrockOptions,
687
727
  ...toolConfig.tools !== void 0 ? { toolConfig } : {}
688
728
  },
689
- warnings: [...warnings, ...toolWarnings]
729
+ warnings: [...warnings, ...toolWarnings],
730
+ usesJsonResponseTool: jsonResponseTool != null
690
731
  };
691
732
  }
692
733
  async doGenerate(options) {
693
734
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
694
- const { command: args, warnings } = await this.getArgs(options);
735
+ const {
736
+ command: args,
737
+ warnings,
738
+ usesJsonResponseTool
739
+ } = await this.getArgs(options);
695
740
  const url = `${this.getUrl(this.modelId)}/converse`;
696
741
  const { value: response, responseHeaders } = await postJsonToApi({
697
742
  url,
@@ -716,7 +761,9 @@ var BedrockChatLanguageModel = class {
716
761
  const content = [];
717
762
  for (const part of response.output.message.content) {
718
763
  if (part.text) {
719
- content.push({ type: "text", text: part.text });
764
+ if (!usesJsonResponseTool) {
765
+ content.push({ type: "text", text: part.text });
766
+ }
720
767
  }
721
768
  if (part.reasoningContent) {
722
769
  if ("reasoningText" in part.reasoningContent) {
@@ -745,22 +792,29 @@ var BedrockChatLanguageModel = class {
745
792
  }
746
793
  }
747
794
  if (part.toolUse) {
748
- content.push({
749
- type: "tool-call",
750
- toolCallId: (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId(),
751
- toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
752
- input: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : "")
753
- });
795
+ content.push(
796
+ // when a json response tool is used, the tool call becomes the text:
797
+ usesJsonResponseTool ? {
798
+ type: "text",
799
+ text: JSON.stringify(part.toolUse.input)
800
+ } : {
801
+ type: "tool-call",
802
+ toolCallId: (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId(),
803
+ toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
804
+ input: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : "")
805
+ }
806
+ );
754
807
  }
755
808
  }
756
- const providerMetadata = response.trace || response.usage ? {
809
+ const providerMetadata = response.trace || response.usage || usesJsonResponseTool ? {
757
810
  bedrock: {
758
811
  ...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
759
812
  ...((_h = response.usage) == null ? void 0 : _h.cacheWriteInputTokens) != null && {
760
813
  usage: {
761
814
  cacheWriteInputTokens: response.usage.cacheWriteInputTokens
762
815
  }
763
- }
816
+ },
817
+ ...usesJsonResponseTool && { isJsonResponseFromTool: true }
764
818
  }
765
819
  } : void 0;
766
820
  return {
@@ -783,7 +837,11 @@ var BedrockChatLanguageModel = class {
783
837
  };
784
838
  }
785
839
  async doStream(options) {
786
- const { command: args, warnings } = await this.getArgs(options);
840
+ const {
841
+ command: args,
842
+ warnings,
843
+ usesJsonResponseTool
844
+ } = await this.getArgs(options);
787
845
  const url = `${this.getUrl(this.modelId)}/converse-stream`;
788
846
  const { value: response, responseHeaders } = await postJsonToApi({
789
847
  url,
@@ -862,11 +920,14 @@ var BedrockChatLanguageModel = class {
862
920
  const trace = value.metadata.trace ? {
863
921
  trace: value.metadata.trace
864
922
  } : void 0;
865
- if (cacheUsage || trace) {
923
+ if (cacheUsage || trace || usesJsonResponseTool) {
866
924
  providerMetadata = {
867
925
  bedrock: {
868
926
  ...cacheUsage,
869
- ...trace
927
+ ...trace,
928
+ ...usesJsonResponseTool && {
929
+ isJsonResponseFromTool: true
930
+ }
870
931
  }
871
932
  };
872
933
  }
@@ -883,16 +944,20 @@ var BedrockChatLanguageModel = class {
883
944
  const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
884
945
  if (contentBlocks[blockIndex] == null) {
885
946
  contentBlocks[blockIndex] = { type: "text" };
947
+ if (!usesJsonResponseTool) {
948
+ controller.enqueue({
949
+ type: "text-start",
950
+ id: String(blockIndex)
951
+ });
952
+ }
953
+ }
954
+ if (!usesJsonResponseTool) {
886
955
  controller.enqueue({
887
- type: "text-start",
888
- id: String(blockIndex)
956
+ type: "text-delta",
957
+ id: String(blockIndex),
958
+ delta: value.contentBlockDelta.delta.text
889
959
  });
890
960
  }
891
- controller.enqueue({
892
- type: "text-delta",
893
- id: String(blockIndex),
894
- delta: value.contentBlockDelta.delta.text
895
- });
896
961
  }
897
962
  if (((_n = value.contentBlockStop) == null ? void 0 : _n.contentBlockIndex) != null) {
898
963
  const blockIndex = value.contentBlockStop.contentBlockIndex;
@@ -904,21 +969,39 @@ var BedrockChatLanguageModel = class {
904
969
  id: String(blockIndex)
905
970
  });
906
971
  } else if (contentBlock.type === "text") {
907
- controller.enqueue({
908
- type: "text-end",
909
- id: String(blockIndex)
910
- });
972
+ if (!usesJsonResponseTool) {
973
+ controller.enqueue({
974
+ type: "text-end",
975
+ id: String(blockIndex)
976
+ });
977
+ }
911
978
  } else if (contentBlock.type === "tool-call") {
912
- controller.enqueue({
913
- type: "tool-input-end",
914
- id: contentBlock.toolCallId
915
- });
916
- controller.enqueue({
917
- type: "tool-call",
918
- toolCallId: contentBlock.toolCallId,
919
- toolName: contentBlock.toolName,
920
- input: contentBlock.jsonText
921
- });
979
+ if (usesJsonResponseTool) {
980
+ controller.enqueue({
981
+ type: "text-start",
982
+ id: String(blockIndex)
983
+ });
984
+ controller.enqueue({
985
+ type: "text-delta",
986
+ id: String(blockIndex),
987
+ delta: contentBlock.jsonText
988
+ });
989
+ controller.enqueue({
990
+ type: "text-end",
991
+ id: String(blockIndex)
992
+ });
993
+ } else {
994
+ controller.enqueue({
995
+ type: "tool-input-end",
996
+ id: contentBlock.toolCallId
997
+ });
998
+ controller.enqueue({
999
+ type: "tool-call",
1000
+ toolCallId: contentBlock.toolCallId,
1001
+ toolName: contentBlock.toolName,
1002
+ input: contentBlock.jsonText
1003
+ });
1004
+ }
922
1005
  }
923
1006
  delete contentBlocks[blockIndex];
924
1007
  }
@@ -973,11 +1056,13 @@ var BedrockChatLanguageModel = class {
973
1056
  toolName: toolUse.name,
974
1057
  jsonText: ""
975
1058
  };
976
- controller.enqueue({
977
- type: "tool-input-start",
978
- id: toolUse.toolUseId,
979
- toolName: toolUse.name
980
- });
1059
+ if (!usesJsonResponseTool) {
1060
+ controller.enqueue({
1061
+ type: "tool-input-start",
1062
+ id: toolUse.toolUseId,
1063
+ toolName: toolUse.name
1064
+ });
1065
+ }
981
1066
  }
982
1067
  const contentBlockDelta = value.contentBlockDelta;
983
1068
  if ((contentBlockDelta == null ? void 0 : contentBlockDelta.delta) && "toolUse" in contentBlockDelta.delta && contentBlockDelta.delta.toolUse) {
@@ -985,11 +1070,13 @@ var BedrockChatLanguageModel = class {
985
1070
  const contentBlock = contentBlocks[blockIndex];
986
1071
  if ((contentBlock == null ? void 0 : contentBlock.type) === "tool-call") {
987
1072
  const delta = (_q = contentBlockDelta.delta.toolUse.input) != null ? _q : "";
988
- controller.enqueue({
989
- type: "tool-input-delta",
990
- id: contentBlock.toolCallId,
991
- delta
992
- });
1073
+ if (!usesJsonResponseTool) {
1074
+ controller.enqueue({
1075
+ type: "tool-input-delta",
1076
+ id: contentBlock.toolCallId,
1077
+ delta
1078
+ });
1079
+ }
993
1080
  contentBlock.jsonText += delta;
994
1081
  }
995
1082
  }