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

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
@@ -623,13 +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
+ 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;
633
647
  const { system, messages } = await convertToBedrockChatMessages(prompt);
634
648
  const isThinking = ((_b = bedrockOptions.reasoningConfig) == null ? void 0 : _b.type) === "enabled";
635
649
  const thinkingBudget = (_c = bedrockOptions.reasoningConfig) == null ? void 0 : _c.budgetTokens;
@@ -670,8 +684,8 @@ var BedrockChatLanguageModel = class {
670
684
  });
671
685
  }
672
686
  const { toolConfig, toolWarnings } = prepareTools({
673
- tools,
674
- toolChoice,
687
+ tools: jsonResponseTool != null ? [jsonResponseTool] : tools != null ? tools : [],
688
+ toolChoice: jsonResponseTool != null ? { type: "tool", toolName: jsonResponseTool.name } : toolChoice,
675
689
  prompt
676
690
  });
677
691
  const { reasoningConfig: _, ...filteredBedrockOptions } = (providerOptions == null ? void 0 : providerOptions.bedrock) || {};
@@ -686,12 +700,17 @@ var BedrockChatLanguageModel = class {
686
700
  ...filteredBedrockOptions,
687
701
  ...toolConfig.tools !== void 0 ? { toolConfig } : {}
688
702
  },
689
- warnings: [...warnings, ...toolWarnings]
703
+ warnings: [...warnings, ...toolWarnings],
704
+ usesJsonResponseTool: jsonResponseTool != null
690
705
  };
691
706
  }
692
707
  async doGenerate(options) {
693
708
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
694
- const { command: args, warnings } = await this.getArgs(options);
709
+ const {
710
+ command: args,
711
+ warnings,
712
+ usesJsonResponseTool
713
+ } = await this.getArgs(options);
695
714
  const url = `${this.getUrl(this.modelId)}/converse`;
696
715
  const { value: response, responseHeaders } = await postJsonToApi({
697
716
  url,
@@ -716,7 +735,9 @@ var BedrockChatLanguageModel = class {
716
735
  const content = [];
717
736
  for (const part of response.output.message.content) {
718
737
  if (part.text) {
719
- content.push({ type: "text", text: part.text });
738
+ if (!usesJsonResponseTool) {
739
+ content.push({ type: "text", text: part.text });
740
+ }
720
741
  }
721
742
  if (part.reasoningContent) {
722
743
  if ("reasoningText" in part.reasoningContent) {
@@ -745,22 +766,29 @@ var BedrockChatLanguageModel = class {
745
766
  }
746
767
  }
747
768
  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
- });
769
+ content.push(
770
+ // when a json response tool is used, the tool call becomes the text:
771
+ usesJsonResponseTool ? {
772
+ type: "text",
773
+ text: JSON.stringify(part.toolUse.input)
774
+ } : {
775
+ type: "tool-call",
776
+ toolCallId: (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId(),
777
+ toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
778
+ input: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : "")
779
+ }
780
+ );
754
781
  }
755
782
  }
756
- const providerMetadata = response.trace || response.usage ? {
783
+ const providerMetadata = response.trace || response.usage || usesJsonResponseTool ? {
757
784
  bedrock: {
758
785
  ...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
759
786
  ...((_h = response.usage) == null ? void 0 : _h.cacheWriteInputTokens) != null && {
760
787
  usage: {
761
788
  cacheWriteInputTokens: response.usage.cacheWriteInputTokens
762
789
  }
763
- }
790
+ },
791
+ ...usesJsonResponseTool && { isJsonResponseFromTool: true }
764
792
  }
765
793
  } : void 0;
766
794
  return {
@@ -783,7 +811,11 @@ var BedrockChatLanguageModel = class {
783
811
  };
784
812
  }
785
813
  async doStream(options) {
786
- const { command: args, warnings } = await this.getArgs(options);
814
+ const {
815
+ command: args,
816
+ warnings,
817
+ usesJsonResponseTool
818
+ } = await this.getArgs(options);
787
819
  const url = `${this.getUrl(this.modelId)}/converse-stream`;
788
820
  const { value: response, responseHeaders } = await postJsonToApi({
789
821
  url,
@@ -862,11 +894,14 @@ var BedrockChatLanguageModel = class {
862
894
  const trace = value.metadata.trace ? {
863
895
  trace: value.metadata.trace
864
896
  } : void 0;
865
- if (cacheUsage || trace) {
897
+ if (cacheUsage || trace || usesJsonResponseTool) {
866
898
  providerMetadata = {
867
899
  bedrock: {
868
900
  ...cacheUsage,
869
- ...trace
901
+ ...trace,
902
+ ...usesJsonResponseTool && {
903
+ isJsonResponseFromTool: true
904
+ }
870
905
  }
871
906
  };
872
907
  }
@@ -883,16 +918,20 @@ var BedrockChatLanguageModel = class {
883
918
  const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
884
919
  if (contentBlocks[blockIndex] == null) {
885
920
  contentBlocks[blockIndex] = { type: "text" };
921
+ if (!usesJsonResponseTool) {
922
+ controller.enqueue({
923
+ type: "text-start",
924
+ id: String(blockIndex)
925
+ });
926
+ }
927
+ }
928
+ if (!usesJsonResponseTool) {
886
929
  controller.enqueue({
887
- type: "text-start",
888
- id: String(blockIndex)
930
+ type: "text-delta",
931
+ id: String(blockIndex),
932
+ delta: value.contentBlockDelta.delta.text
889
933
  });
890
934
  }
891
- controller.enqueue({
892
- type: "text-delta",
893
- id: String(blockIndex),
894
- delta: value.contentBlockDelta.delta.text
895
- });
896
935
  }
897
936
  if (((_n = value.contentBlockStop) == null ? void 0 : _n.contentBlockIndex) != null) {
898
937
  const blockIndex = value.contentBlockStop.contentBlockIndex;
@@ -904,21 +943,39 @@ var BedrockChatLanguageModel = class {
904
943
  id: String(blockIndex)
905
944
  });
906
945
  } else if (contentBlock.type === "text") {
907
- controller.enqueue({
908
- type: "text-end",
909
- id: String(blockIndex)
910
- });
946
+ if (!usesJsonResponseTool) {
947
+ controller.enqueue({
948
+ type: "text-end",
949
+ id: String(blockIndex)
950
+ });
951
+ }
911
952
  } 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
- });
953
+ if (usesJsonResponseTool) {
954
+ controller.enqueue({
955
+ type: "text-start",
956
+ id: String(blockIndex)
957
+ });
958
+ controller.enqueue({
959
+ type: "text-delta",
960
+ id: String(blockIndex),
961
+ delta: contentBlock.jsonText
962
+ });
963
+ controller.enqueue({
964
+ type: "text-end",
965
+ id: String(blockIndex)
966
+ });
967
+ } else {
968
+ controller.enqueue({
969
+ type: "tool-input-end",
970
+ id: contentBlock.toolCallId
971
+ });
972
+ controller.enqueue({
973
+ type: "tool-call",
974
+ toolCallId: contentBlock.toolCallId,
975
+ toolName: contentBlock.toolName,
976
+ input: contentBlock.jsonText
977
+ });
978
+ }
922
979
  }
923
980
  delete contentBlocks[blockIndex];
924
981
  }
@@ -973,11 +1030,13 @@ var BedrockChatLanguageModel = class {
973
1030
  toolName: toolUse.name,
974
1031
  jsonText: ""
975
1032
  };
976
- controller.enqueue({
977
- type: "tool-input-start",
978
- id: toolUse.toolUseId,
979
- toolName: toolUse.name
980
- });
1033
+ if (!usesJsonResponseTool) {
1034
+ controller.enqueue({
1035
+ type: "tool-input-start",
1036
+ id: toolUse.toolUseId,
1037
+ toolName: toolUse.name
1038
+ });
1039
+ }
981
1040
  }
982
1041
  const contentBlockDelta = value.contentBlockDelta;
983
1042
  if ((contentBlockDelta == null ? void 0 : contentBlockDelta.delta) && "toolUse" in contentBlockDelta.delta && contentBlockDelta.delta.toolUse) {
@@ -985,11 +1044,13 @@ var BedrockChatLanguageModel = class {
985
1044
  const contentBlock = contentBlocks[blockIndex];
986
1045
  if ((contentBlock == null ? void 0 : contentBlock.type) === "tool-call") {
987
1046
  const delta = (_q = contentBlockDelta.delta.toolUse.input) != null ? _q : "";
988
- controller.enqueue({
989
- type: "tool-input-delta",
990
- id: contentBlock.toolCallId,
991
- delta
992
- });
1047
+ if (!usesJsonResponseTool) {
1048
+ controller.enqueue({
1049
+ type: "tool-input-delta",
1050
+ id: contentBlock.toolCallId,
1051
+ delta
1052
+ });
1053
+ }
993
1054
  contentBlock.jsonText += delta;
994
1055
  }
995
1056
  }