@ai-sdk/amazon-bedrock 4.0.0-beta.61 → 4.0.0-beta.62

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
@@ -650,7 +650,7 @@ function groupIntoBlocks(prompt) {
650
650
  }
651
651
 
652
652
  // src/map-bedrock-finish-reason.ts
653
- function mapBedrockFinishReason(finishReason) {
653
+ function mapBedrockFinishReason(finishReason, isJsonResponseFromTool) {
654
654
  switch (finishReason) {
655
655
  case "stop_sequence":
656
656
  case "end_turn":
@@ -661,7 +661,7 @@ function mapBedrockFinishReason(finishReason) {
661
661
  case "guardrail_intervened":
662
662
  return "content-filter";
663
663
  case "tool_use":
664
- return "tool-calls";
664
+ return isJsonResponseFromTool ? "stop" : "tool-calls";
665
665
  default:
666
666
  return "unknown";
667
667
  }
@@ -740,14 +740,6 @@ var BedrockChatLanguageModel = class {
740
740
  details: "Only text and json response formats are supported."
741
741
  });
742
742
  }
743
- if (tools != null && (responseFormat == null ? void 0 : responseFormat.type) === "json") {
744
- if (tools.length > 0) {
745
- warnings.push({
746
- type: "other",
747
- message: "JSON response format does not support tools. The provided tools are ignored."
748
- });
749
- }
750
- }
751
743
  const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
752
744
  type: "function",
753
745
  name: "json",
@@ -755,8 +747,8 @@ var BedrockChatLanguageModel = class {
755
747
  inputSchema: responseFormat.schema
756
748
  } : void 0;
757
749
  const { toolConfig, additionalTools, toolWarnings, betas } = await prepareTools({
758
- tools: jsonResponseTool ? [jsonResponseTool, ...tools != null ? tools : []] : tools,
759
- toolChoice: jsonResponseTool != null ? { type: "tool", toolName: jsonResponseTool.name } : toolChoice,
750
+ tools: jsonResponseTool ? [...tools != null ? tools : [], jsonResponseTool] : tools,
751
+ toolChoice: jsonResponseTool != null ? { type: "required" } : toolChoice,
760
752
  modelId: this.modelId
761
753
  });
762
754
  warnings.push(...toolWarnings);
@@ -900,11 +892,10 @@ var BedrockChatLanguageModel = class {
900
892
  fetch: this.config.fetch
901
893
  });
902
894
  const content = [];
895
+ let isJsonResponseFromTool = false;
903
896
  for (const part of response.output.message.content) {
904
897
  if (part.text) {
905
- if (!usesJsonResponseTool) {
906
- content.push({ type: "text", text: part.text });
907
- }
898
+ content.push({ type: "text", text: part.text });
908
899
  }
909
900
  if (part.reasoningContent) {
910
901
  if ("reasoningText" in part.reasoningContent) {
@@ -933,21 +924,24 @@ var BedrockChatLanguageModel = class {
933
924
  }
934
925
  }
935
926
  if (part.toolUse) {
936
- content.push(
937
- // when a json response tool is used, the tool call becomes the text:
938
- usesJsonResponseTool ? {
927
+ const isJsonResponseTool = usesJsonResponseTool && part.toolUse.name === "json";
928
+ if (isJsonResponseTool) {
929
+ isJsonResponseFromTool = true;
930
+ content.push({
939
931
  type: "text",
940
932
  text: JSON.stringify(part.toolUse.input)
941
- } : {
933
+ });
934
+ } else {
935
+ content.push({
942
936
  type: "tool-call",
943
937
  toolCallId: (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId(),
944
938
  toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
945
939
  input: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : "")
946
- }
947
- );
940
+ });
941
+ }
948
942
  }
949
943
  }
950
- const providerMetadata = response.trace || response.usage || usesJsonResponseTool ? {
944
+ const providerMetadata = response.trace || response.usage || isJsonResponseFromTool ? {
951
945
  bedrock: {
952
946
  ...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
953
947
  ...((_h = response.usage) == null ? void 0 : _h.cacheWriteInputTokens) != null && {
@@ -955,13 +949,14 @@ var BedrockChatLanguageModel = class {
955
949
  cacheWriteInputTokens: response.usage.cacheWriteInputTokens
956
950
  }
957
951
  },
958
- ...usesJsonResponseTool && { isJsonResponseFromTool: true }
952
+ ...isJsonResponseFromTool && { isJsonResponseFromTool: true }
959
953
  }
960
954
  } : void 0;
961
955
  return {
962
956
  content,
963
957
  finishReason: mapBedrockFinishReason(
964
- response.stopReason
958
+ response.stopReason,
959
+ isJsonResponseFromTool
965
960
  ),
966
961
  usage: {
967
962
  inputTokens: (_i = response.usage) == null ? void 0 : _i.inputTokens,
@@ -1003,6 +998,7 @@ var BedrockChatLanguageModel = class {
1003
998
  totalTokens: void 0
1004
999
  };
1005
1000
  let providerMetadata = void 0;
1001
+ let isJsonResponseFromTool = false;
1006
1002
  const contentBlocks = {};
1007
1003
  return {
1008
1004
  stream: response.pipeThrough(
@@ -1042,7 +1038,8 @@ var BedrockChatLanguageModel = class {
1042
1038
  }
1043
1039
  if (value.messageStop) {
1044
1040
  finishReason = mapBedrockFinishReason(
1045
- value.messageStop.stopReason
1041
+ value.messageStop.stopReason,
1042
+ isJsonResponseFromTool
1046
1043
  );
1047
1044
  }
1048
1045
  if (value.metadata) {
@@ -1058,14 +1055,11 @@ var BedrockChatLanguageModel = class {
1058
1055
  const trace = value.metadata.trace ? {
1059
1056
  trace: value.metadata.trace
1060
1057
  } : void 0;
1061
- if (cacheUsage || trace || usesJsonResponseTool) {
1058
+ if (cacheUsage || trace) {
1062
1059
  providerMetadata = {
1063
1060
  bedrock: {
1064
1061
  ...cacheUsage,
1065
- ...trace,
1066
- ...usesJsonResponseTool && {
1067
- isJsonResponseFromTool: true
1068
- }
1062
+ ...trace
1069
1063
  }
1070
1064
  };
1071
1065
  }
@@ -1082,20 +1076,16 @@ var BedrockChatLanguageModel = class {
1082
1076
  const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
1083
1077
  if (contentBlocks[blockIndex] == null) {
1084
1078
  contentBlocks[blockIndex] = { type: "text" };
1085
- if (!usesJsonResponseTool) {
1086
- controller.enqueue({
1087
- type: "text-start",
1088
- id: String(blockIndex)
1089
- });
1090
- }
1091
- }
1092
- if (!usesJsonResponseTool) {
1093
1079
  controller.enqueue({
1094
- type: "text-delta",
1095
- id: String(blockIndex),
1096
- delta: value.contentBlockDelta.delta.text
1080
+ type: "text-start",
1081
+ id: String(blockIndex)
1097
1082
  });
1098
1083
  }
1084
+ controller.enqueue({
1085
+ type: "text-delta",
1086
+ id: String(blockIndex),
1087
+ delta: value.contentBlockDelta.delta.text
1088
+ });
1099
1089
  }
1100
1090
  if (((_n = value.contentBlockStop) == null ? void 0 : _n.contentBlockIndex) != null) {
1101
1091
  const blockIndex = value.contentBlockStop.contentBlockIndex;
@@ -1107,14 +1097,13 @@ var BedrockChatLanguageModel = class {
1107
1097
  id: String(blockIndex)
1108
1098
  });
1109
1099
  } else if (contentBlock.type === "text") {
1110
- if (!usesJsonResponseTool) {
1111
- controller.enqueue({
1112
- type: "text-end",
1113
- id: String(blockIndex)
1114
- });
1115
- }
1100
+ controller.enqueue({
1101
+ type: "text-end",
1102
+ id: String(blockIndex)
1103
+ });
1116
1104
  } else if (contentBlock.type === "tool-call") {
1117
- if (usesJsonResponseTool) {
1105
+ if (contentBlock.isJsonResponseTool) {
1106
+ isJsonResponseFromTool = true;
1118
1107
  controller.enqueue({
1119
1108
  type: "text-start",
1120
1109
  id: String(blockIndex)
@@ -1188,13 +1177,15 @@ var BedrockChatLanguageModel = class {
1188
1177
  if (((_p = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _p.toolUse) != null) {
1189
1178
  const toolUse = contentBlockStart.start.toolUse;
1190
1179
  const blockIndex = contentBlockStart.contentBlockIndex;
1180
+ const isJsonResponseTool = usesJsonResponseTool && toolUse.name === "json";
1191
1181
  contentBlocks[blockIndex] = {
1192
1182
  type: "tool-call",
1193
1183
  toolCallId: toolUse.toolUseId,
1194
1184
  toolName: toolUse.name,
1195
- jsonText: ""
1185
+ jsonText: "",
1186
+ isJsonResponseTool
1196
1187
  };
1197
- if (!usesJsonResponseTool) {
1188
+ if (!isJsonResponseTool) {
1198
1189
  controller.enqueue({
1199
1190
  type: "tool-input-start",
1200
1191
  id: toolUse.toolUseId,
@@ -1208,7 +1199,7 @@ var BedrockChatLanguageModel = class {
1208
1199
  const contentBlock = contentBlocks[blockIndex];
1209
1200
  if ((contentBlock == null ? void 0 : contentBlock.type) === "tool-call") {
1210
1201
  const delta = (_q = contentBlockDelta.delta.toolUse.input) != null ? _q : "";
1211
- if (!usesJsonResponseTool) {
1202
+ if (!contentBlock.isJsonResponseTool) {
1212
1203
  controller.enqueue({
1213
1204
  type: "tool-input-delta",
1214
1205
  id: contentBlock.toolCallId,
@@ -1220,6 +1211,20 @@ var BedrockChatLanguageModel = class {
1220
1211
  }
1221
1212
  },
1222
1213
  flush(controller) {
1214
+ if (isJsonResponseFromTool) {
1215
+ if (providerMetadata) {
1216
+ providerMetadata.bedrock = {
1217
+ ...providerMetadata.bedrock,
1218
+ isJsonResponseFromTool: true
1219
+ };
1220
+ } else {
1221
+ providerMetadata = {
1222
+ bedrock: {
1223
+ isJsonResponseFromTool: true
1224
+ }
1225
+ };
1226
+ }
1227
+ }
1223
1228
  controller.enqueue({
1224
1229
  type: "finish",
1225
1230
  finishReason,
@@ -1547,7 +1552,7 @@ import {
1547
1552
  import { AwsV4Signer } from "aws4fetch";
1548
1553
 
1549
1554
  // src/version.ts
1550
- var VERSION = true ? "4.0.0-beta.61" : "0.0.0-test";
1555
+ var VERSION = true ? "4.0.0-beta.62" : "0.0.0-test";
1551
1556
 
1552
1557
  // src/bedrock-sigv4-fetch.ts
1553
1558
  function createSigV4FetchFunction(getCredentials, fetch = globalThis.fetch) {