@ai-sdk/amazon-bedrock 3.0.56 → 3.0.57

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,7 +8,7 @@ import {
8
8
  } from "@ai-sdk/provider-utils";
9
9
 
10
10
  // src/version.ts
11
- var VERSION = true ? "3.0.56" : "0.0.0-test";
11
+ var VERSION = true ? "3.0.57" : "0.0.0-test";
12
12
 
13
13
  // src/bedrock-provider.ts
14
14
  import { anthropicTools as anthropicTools2 } from "@ai-sdk/anthropic/internal";
@@ -645,7 +645,7 @@ function groupIntoBlocks(prompt) {
645
645
  }
646
646
 
647
647
  // src/map-bedrock-finish-reason.ts
648
- function mapBedrockFinishReason(finishReason) {
648
+ function mapBedrockFinishReason(finishReason, isJsonResponseFromTool) {
649
649
  switch (finishReason) {
650
650
  case "stop_sequence":
651
651
  case "end_turn":
@@ -656,7 +656,7 @@ function mapBedrockFinishReason(finishReason) {
656
656
  case "guardrail_intervened":
657
657
  return "content-filter";
658
658
  case "tool_use":
659
- return "tool-calls";
659
+ return isJsonResponseFromTool ? "stop" : "tool-calls";
660
660
  default:
661
661
  return "unknown";
662
662
  }
@@ -735,14 +735,6 @@ var BedrockChatLanguageModel = class {
735
735
  details: "Only text and json response formats are supported."
736
736
  });
737
737
  }
738
- if (tools != null && (responseFormat == null ? void 0 : responseFormat.type) === "json") {
739
- if (tools.length > 0) {
740
- warnings.push({
741
- type: "other",
742
- message: "JSON response format does not support tools. The provided tools are ignored."
743
- });
744
- }
745
- }
746
738
  const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
747
739
  type: "function",
748
740
  name: "json",
@@ -750,8 +742,8 @@ var BedrockChatLanguageModel = class {
750
742
  inputSchema: responseFormat.schema
751
743
  } : void 0;
752
744
  const { toolConfig, additionalTools, toolWarnings, betas } = await prepareTools({
753
- tools: jsonResponseTool ? [jsonResponseTool, ...tools != null ? tools : []] : tools,
754
- toolChoice: jsonResponseTool != null ? { type: "tool", toolName: jsonResponseTool.name } : toolChoice,
745
+ tools: jsonResponseTool ? [...tools != null ? tools : [], jsonResponseTool] : tools,
746
+ toolChoice: jsonResponseTool != null ? { type: "required" } : toolChoice,
755
747
  modelId: this.modelId
756
748
  });
757
749
  warnings.push(...toolWarnings);
@@ -895,11 +887,10 @@ var BedrockChatLanguageModel = class {
895
887
  fetch: this.config.fetch
896
888
  });
897
889
  const content = [];
890
+ let isJsonResponseFromTool = false;
898
891
  for (const part of response.output.message.content) {
899
892
  if (part.text) {
900
- if (!usesJsonResponseTool) {
901
- content.push({ type: "text", text: part.text });
902
- }
893
+ content.push({ type: "text", text: part.text });
903
894
  }
904
895
  if (part.reasoningContent) {
905
896
  if ("reasoningText" in part.reasoningContent) {
@@ -928,21 +919,24 @@ var BedrockChatLanguageModel = class {
928
919
  }
929
920
  }
930
921
  if (part.toolUse) {
931
- content.push(
932
- // when a json response tool is used, the tool call becomes the text:
933
- usesJsonResponseTool ? {
922
+ const isJsonResponseTool = usesJsonResponseTool && part.toolUse.name === "json";
923
+ if (isJsonResponseTool) {
924
+ isJsonResponseFromTool = true;
925
+ content.push({
934
926
  type: "text",
935
927
  text: JSON.stringify(part.toolUse.input)
936
- } : {
928
+ });
929
+ } else {
930
+ content.push({
937
931
  type: "tool-call",
938
932
  toolCallId: (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId(),
939
933
  toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
940
934
  input: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : "")
941
- }
942
- );
935
+ });
936
+ }
943
937
  }
944
938
  }
945
- const providerMetadata = response.trace || response.usage || usesJsonResponseTool ? {
939
+ const providerMetadata = response.trace || response.usage || isJsonResponseFromTool ? {
946
940
  bedrock: {
947
941
  ...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
948
942
  ...((_h = response.usage) == null ? void 0 : _h.cacheWriteInputTokens) != null && {
@@ -950,13 +944,14 @@ var BedrockChatLanguageModel = class {
950
944
  cacheWriteInputTokens: response.usage.cacheWriteInputTokens
951
945
  }
952
946
  },
953
- ...usesJsonResponseTool && { isJsonResponseFromTool: true }
947
+ ...isJsonResponseFromTool && { isJsonResponseFromTool: true }
954
948
  }
955
949
  } : void 0;
956
950
  return {
957
951
  content,
958
952
  finishReason: mapBedrockFinishReason(
959
- response.stopReason
953
+ response.stopReason,
954
+ isJsonResponseFromTool
960
955
  ),
961
956
  usage: {
962
957
  inputTokens: (_i = response.usage) == null ? void 0 : _i.inputTokens,
@@ -998,6 +993,7 @@ var BedrockChatLanguageModel = class {
998
993
  totalTokens: void 0
999
994
  };
1000
995
  let providerMetadata = void 0;
996
+ let isJsonResponseFromTool = false;
1001
997
  const contentBlocks = {};
1002
998
  return {
1003
999
  stream: response.pipeThrough(
@@ -1037,7 +1033,8 @@ var BedrockChatLanguageModel = class {
1037
1033
  }
1038
1034
  if (value.messageStop) {
1039
1035
  finishReason = mapBedrockFinishReason(
1040
- value.messageStop.stopReason
1036
+ value.messageStop.stopReason,
1037
+ isJsonResponseFromTool
1041
1038
  );
1042
1039
  }
1043
1040
  if (value.metadata) {
@@ -1053,14 +1050,11 @@ var BedrockChatLanguageModel = class {
1053
1050
  const trace = value.metadata.trace ? {
1054
1051
  trace: value.metadata.trace
1055
1052
  } : void 0;
1056
- if (cacheUsage || trace || usesJsonResponseTool) {
1053
+ if (cacheUsage || trace) {
1057
1054
  providerMetadata = {
1058
1055
  bedrock: {
1059
1056
  ...cacheUsage,
1060
- ...trace,
1061
- ...usesJsonResponseTool && {
1062
- isJsonResponseFromTool: true
1063
- }
1057
+ ...trace
1064
1058
  }
1065
1059
  };
1066
1060
  }
@@ -1077,20 +1071,16 @@ var BedrockChatLanguageModel = class {
1077
1071
  const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
1078
1072
  if (contentBlocks[blockIndex] == null) {
1079
1073
  contentBlocks[blockIndex] = { type: "text" };
1080
- if (!usesJsonResponseTool) {
1081
- controller.enqueue({
1082
- type: "text-start",
1083
- id: String(blockIndex)
1084
- });
1085
- }
1086
- }
1087
- if (!usesJsonResponseTool) {
1088
1074
  controller.enqueue({
1089
- type: "text-delta",
1090
- id: String(blockIndex),
1091
- delta: value.contentBlockDelta.delta.text
1075
+ type: "text-start",
1076
+ id: String(blockIndex)
1092
1077
  });
1093
1078
  }
1079
+ controller.enqueue({
1080
+ type: "text-delta",
1081
+ id: String(blockIndex),
1082
+ delta: value.contentBlockDelta.delta.text
1083
+ });
1094
1084
  }
1095
1085
  if (((_n = value.contentBlockStop) == null ? void 0 : _n.contentBlockIndex) != null) {
1096
1086
  const blockIndex = value.contentBlockStop.contentBlockIndex;
@@ -1102,14 +1092,13 @@ var BedrockChatLanguageModel = class {
1102
1092
  id: String(blockIndex)
1103
1093
  });
1104
1094
  } else if (contentBlock.type === "text") {
1105
- if (!usesJsonResponseTool) {
1106
- controller.enqueue({
1107
- type: "text-end",
1108
- id: String(blockIndex)
1109
- });
1110
- }
1095
+ controller.enqueue({
1096
+ type: "text-end",
1097
+ id: String(blockIndex)
1098
+ });
1111
1099
  } else if (contentBlock.type === "tool-call") {
1112
- if (usesJsonResponseTool) {
1100
+ if (contentBlock.isJsonResponseTool) {
1101
+ isJsonResponseFromTool = true;
1113
1102
  controller.enqueue({
1114
1103
  type: "text-start",
1115
1104
  id: String(blockIndex)
@@ -1183,13 +1172,15 @@ var BedrockChatLanguageModel = class {
1183
1172
  if (((_p = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _p.toolUse) != null) {
1184
1173
  const toolUse = contentBlockStart.start.toolUse;
1185
1174
  const blockIndex = contentBlockStart.contentBlockIndex;
1175
+ const isJsonResponseTool = usesJsonResponseTool && toolUse.name === "json";
1186
1176
  contentBlocks[blockIndex] = {
1187
1177
  type: "tool-call",
1188
1178
  toolCallId: toolUse.toolUseId,
1189
1179
  toolName: toolUse.name,
1190
- jsonText: ""
1180
+ jsonText: "",
1181
+ isJsonResponseTool
1191
1182
  };
1192
- if (!usesJsonResponseTool) {
1183
+ if (!isJsonResponseTool) {
1193
1184
  controller.enqueue({
1194
1185
  type: "tool-input-start",
1195
1186
  id: toolUse.toolUseId,
@@ -1203,7 +1194,7 @@ var BedrockChatLanguageModel = class {
1203
1194
  const contentBlock = contentBlocks[blockIndex];
1204
1195
  if ((contentBlock == null ? void 0 : contentBlock.type) === "tool-call") {
1205
1196
  const delta = (_q = contentBlockDelta.delta.toolUse.input) != null ? _q : "";
1206
- if (!usesJsonResponseTool) {
1197
+ if (!contentBlock.isJsonResponseTool) {
1207
1198
  controller.enqueue({
1208
1199
  type: "tool-input-delta",
1209
1200
  id: contentBlock.toolCallId,
@@ -1215,6 +1206,20 @@ var BedrockChatLanguageModel = class {
1215
1206
  }
1216
1207
  },
1217
1208
  flush(controller) {
1209
+ if (isJsonResponseFromTool) {
1210
+ if (providerMetadata) {
1211
+ providerMetadata.bedrock = {
1212
+ ...providerMetadata.bedrock,
1213
+ isJsonResponseFromTool: true
1214
+ };
1215
+ } else {
1216
+ providerMetadata = {
1217
+ bedrock: {
1218
+ isJsonResponseFromTool: true
1219
+ }
1220
+ };
1221
+ }
1222
+ }
1218
1223
  controller.enqueue({
1219
1224
  type: "finish",
1220
1225
  finishReason,