@ai-sdk/amazon-bedrock 4.0.0-beta.60 → 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
@@ -77,7 +77,11 @@ var bedrockProviderOptions = z.object({
77
77
  reasoningConfig: z.object({
78
78
  type: z.union([z.literal("enabled"), z.literal("disabled")]).optional(),
79
79
  budgetTokens: z.number().optional()
80
- }).optional()
80
+ }).optional(),
81
+ /**
82
+ * Anthropic beta features to enable
83
+ */
84
+ anthropicBeta: z.array(z.string()).optional()
81
85
  });
82
86
 
83
87
  // src/bedrock-error.ts
@@ -646,7 +650,7 @@ function groupIntoBlocks(prompt) {
646
650
  }
647
651
 
648
652
  // src/map-bedrock-finish-reason.ts
649
- function mapBedrockFinishReason(finishReason) {
653
+ function mapBedrockFinishReason(finishReason, isJsonResponseFromTool) {
650
654
  switch (finishReason) {
651
655
  case "stop_sequence":
652
656
  case "end_turn":
@@ -657,7 +661,7 @@ function mapBedrockFinishReason(finishReason) {
657
661
  case "guardrail_intervened":
658
662
  return "content-filter";
659
663
  case "tool_use":
660
- return "tool-calls";
664
+ return isJsonResponseFromTool ? "stop" : "tool-calls";
661
665
  default:
662
666
  return "unknown";
663
667
  }
@@ -689,7 +693,7 @@ var BedrockChatLanguageModel = class {
689
693
  toolChoice,
690
694
  providerOptions
691
695
  }) {
692
- var _a, _b, _c, _d, _e, _f;
696
+ var _a, _b, _c, _d, _e, _f, _g;
693
697
  const bedrockOptions = (_a = await parseProviderOptions2({
694
698
  provider: "bedrock",
695
699
  providerOptions,
@@ -736,14 +740,6 @@ var BedrockChatLanguageModel = class {
736
740
  details: "Only text and json response formats are supported."
737
741
  });
738
742
  }
739
- if (tools != null && (responseFormat == null ? void 0 : responseFormat.type) === "json") {
740
- if (tools.length > 0) {
741
- warnings.push({
742
- type: "other",
743
- message: "JSON response format does not support tools. The provided tools are ignored."
744
- });
745
- }
746
- }
747
743
  const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
748
744
  type: "function",
749
745
  name: "json",
@@ -751,8 +747,8 @@ var BedrockChatLanguageModel = class {
751
747
  inputSchema: responseFormat.schema
752
748
  } : void 0;
753
749
  const { toolConfig, additionalTools, toolWarnings, betas } = await prepareTools({
754
- tools: jsonResponseTool ? [jsonResponseTool, ...tools != null ? tools : []] : tools,
755
- toolChoice: jsonResponseTool != null ? { type: "tool", toolName: jsonResponseTool.name } : toolChoice,
750
+ tools: jsonResponseTool ? [...tools != null ? tools : [], jsonResponseTool] : tools,
751
+ toolChoice: jsonResponseTool != null ? { type: "required" } : toolChoice,
756
752
  modelId: this.modelId
757
753
  });
758
754
  warnings.push(...toolWarnings);
@@ -762,8 +758,16 @@ var BedrockChatLanguageModel = class {
762
758
  ...additionalTools
763
759
  };
764
760
  }
765
- const isThinking = ((_b = bedrockOptions.reasoningConfig) == null ? void 0 : _b.type) === "enabled";
766
- const thinkingBudget = (_c = bedrockOptions.reasoningConfig) == null ? void 0 : _c.budgetTokens;
761
+ if (betas.size > 0 || bedrockOptions.anthropicBeta) {
762
+ const existingBetas = (_b = bedrockOptions.anthropicBeta) != null ? _b : [];
763
+ const mergedBetas = betas.size > 0 ? [...existingBetas, ...Array.from(betas)] : existingBetas;
764
+ bedrockOptions.additionalModelRequestFields = {
765
+ ...bedrockOptions.additionalModelRequestFields,
766
+ anthropic_beta: mergedBetas
767
+ };
768
+ }
769
+ const isThinking = ((_c = bedrockOptions.reasoningConfig) == null ? void 0 : _c.type) === "enabled";
770
+ const thinkingBudget = (_d = bedrockOptions.reasoningConfig) == null ? void 0 : _d.budgetTokens;
767
771
  const inferenceConfig = {
768
772
  ...maxOutputTokens != null && { maxTokens: maxOutputTokens },
769
773
  ...temperature != null && { temperature },
@@ -780,7 +784,7 @@ var BedrockChatLanguageModel = class {
780
784
  bedrockOptions.additionalModelRequestFields = {
781
785
  ...bedrockOptions.additionalModelRequestFields,
782
786
  thinking: {
783
- type: (_d = bedrockOptions.reasoningConfig) == null ? void 0 : _d.type,
787
+ type: (_e = bedrockOptions.reasoningConfig) == null ? void 0 : _e.type,
784
788
  budget_tokens: thinkingBudget
785
789
  }
786
790
  };
@@ -809,7 +813,7 @@ var BedrockChatLanguageModel = class {
809
813
  details: "topK is not supported when thinking is enabled"
810
814
  });
811
815
  }
812
- const hasAnyTools = ((_f = (_e = toolConfig.tools) == null ? void 0 : _e.length) != null ? _f : 0) > 0 || additionalTools;
816
+ const hasAnyTools = ((_g = (_f = toolConfig.tools) == null ? void 0 : _f.length) != null ? _g : 0) > 0 || additionalTools;
813
817
  let filteredPrompt = prompt;
814
818
  if (!hasAnyTools) {
815
819
  const hasToolContent = prompt.some(
@@ -858,27 +862,21 @@ var BedrockChatLanguageModel = class {
858
862
  };
859
863
  }
860
864
  async getHeaders({
861
- betas,
862
865
  headers
863
866
  }) {
864
- return combineHeaders(
865
- await resolve(this.config.headers),
866
- betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {},
867
- headers
868
- );
867
+ return combineHeaders(await resolve(this.config.headers), headers);
869
868
  }
870
869
  async doGenerate(options) {
871
870
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
872
871
  const {
873
872
  command: args,
874
873
  warnings,
875
- usesJsonResponseTool,
876
- betas
874
+ usesJsonResponseTool
877
875
  } = await this.getArgs(options);
878
876
  const url = `${this.getUrl(this.modelId)}/converse`;
879
877
  const { value: response, responseHeaders } = await postJsonToApi({
880
878
  url,
881
- headers: await this.getHeaders({ betas, headers: options.headers }),
879
+ headers: await this.getHeaders({ headers: options.headers }),
882
880
  body: args,
883
881
  failedResponseHandler: createJsonErrorResponseHandler({
884
882
  errorSchema: BedrockErrorSchema,
@@ -894,11 +892,10 @@ var BedrockChatLanguageModel = class {
894
892
  fetch: this.config.fetch
895
893
  });
896
894
  const content = [];
895
+ let isJsonResponseFromTool = false;
897
896
  for (const part of response.output.message.content) {
898
897
  if (part.text) {
899
- if (!usesJsonResponseTool) {
900
- content.push({ type: "text", text: part.text });
901
- }
898
+ content.push({ type: "text", text: part.text });
902
899
  }
903
900
  if (part.reasoningContent) {
904
901
  if ("reasoningText" in part.reasoningContent) {
@@ -927,21 +924,24 @@ var BedrockChatLanguageModel = class {
927
924
  }
928
925
  }
929
926
  if (part.toolUse) {
930
- content.push(
931
- // when a json response tool is used, the tool call becomes the text:
932
- usesJsonResponseTool ? {
927
+ const isJsonResponseTool = usesJsonResponseTool && part.toolUse.name === "json";
928
+ if (isJsonResponseTool) {
929
+ isJsonResponseFromTool = true;
930
+ content.push({
933
931
  type: "text",
934
932
  text: JSON.stringify(part.toolUse.input)
935
- } : {
933
+ });
934
+ } else {
935
+ content.push({
936
936
  type: "tool-call",
937
937
  toolCallId: (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId(),
938
938
  toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
939
939
  input: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : "")
940
- }
941
- );
940
+ });
941
+ }
942
942
  }
943
943
  }
944
- const providerMetadata = response.trace || response.usage || usesJsonResponseTool ? {
944
+ const providerMetadata = response.trace || response.usage || isJsonResponseFromTool ? {
945
945
  bedrock: {
946
946
  ...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
947
947
  ...((_h = response.usage) == null ? void 0 : _h.cacheWriteInputTokens) != null && {
@@ -949,13 +949,14 @@ var BedrockChatLanguageModel = class {
949
949
  cacheWriteInputTokens: response.usage.cacheWriteInputTokens
950
950
  }
951
951
  },
952
- ...usesJsonResponseTool && { isJsonResponseFromTool: true }
952
+ ...isJsonResponseFromTool && { isJsonResponseFromTool: true }
953
953
  }
954
954
  } : void 0;
955
955
  return {
956
956
  content,
957
957
  finishReason: mapBedrockFinishReason(
958
- response.stopReason
958
+ response.stopReason,
959
+ isJsonResponseFromTool
959
960
  ),
960
961
  usage: {
961
962
  inputTokens: (_i = response.usage) == null ? void 0 : _i.inputTokens,
@@ -975,13 +976,12 @@ var BedrockChatLanguageModel = class {
975
976
  const {
976
977
  command: args,
977
978
  warnings,
978
- usesJsonResponseTool,
979
- betas
979
+ usesJsonResponseTool
980
980
  } = await this.getArgs(options);
981
981
  const url = `${this.getUrl(this.modelId)}/converse-stream`;
982
982
  const { value: response, responseHeaders } = await postJsonToApi({
983
983
  url,
984
- headers: await this.getHeaders({ betas, headers: options.headers }),
984
+ headers: await this.getHeaders({ headers: options.headers }),
985
985
  body: args,
986
986
  failedResponseHandler: createJsonErrorResponseHandler({
987
987
  errorSchema: BedrockErrorSchema,
@@ -998,6 +998,7 @@ var BedrockChatLanguageModel = class {
998
998
  totalTokens: void 0
999
999
  };
1000
1000
  let providerMetadata = void 0;
1001
+ let isJsonResponseFromTool = false;
1001
1002
  const contentBlocks = {};
1002
1003
  return {
1003
1004
  stream: response.pipeThrough(
@@ -1037,7 +1038,8 @@ var BedrockChatLanguageModel = class {
1037
1038
  }
1038
1039
  if (value.messageStop) {
1039
1040
  finishReason = mapBedrockFinishReason(
1040
- value.messageStop.stopReason
1041
+ value.messageStop.stopReason,
1042
+ isJsonResponseFromTool
1041
1043
  );
1042
1044
  }
1043
1045
  if (value.metadata) {
@@ -1053,14 +1055,11 @@ var BedrockChatLanguageModel = class {
1053
1055
  const trace = value.metadata.trace ? {
1054
1056
  trace: value.metadata.trace
1055
1057
  } : void 0;
1056
- if (cacheUsage || trace || usesJsonResponseTool) {
1058
+ if (cacheUsage || trace) {
1057
1059
  providerMetadata = {
1058
1060
  bedrock: {
1059
1061
  ...cacheUsage,
1060
- ...trace,
1061
- ...usesJsonResponseTool && {
1062
- isJsonResponseFromTool: true
1063
- }
1062
+ ...trace
1064
1063
  }
1065
1064
  };
1066
1065
  }
@@ -1077,20 +1076,16 @@ var BedrockChatLanguageModel = class {
1077
1076
  const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
1078
1077
  if (contentBlocks[blockIndex] == null) {
1079
1078
  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
1079
  controller.enqueue({
1089
- type: "text-delta",
1090
- id: String(blockIndex),
1091
- delta: value.contentBlockDelta.delta.text
1080
+ type: "text-start",
1081
+ id: String(blockIndex)
1092
1082
  });
1093
1083
  }
1084
+ controller.enqueue({
1085
+ type: "text-delta",
1086
+ id: String(blockIndex),
1087
+ delta: value.contentBlockDelta.delta.text
1088
+ });
1094
1089
  }
1095
1090
  if (((_n = value.contentBlockStop) == null ? void 0 : _n.contentBlockIndex) != null) {
1096
1091
  const blockIndex = value.contentBlockStop.contentBlockIndex;
@@ -1102,14 +1097,13 @@ var BedrockChatLanguageModel = class {
1102
1097
  id: String(blockIndex)
1103
1098
  });
1104
1099
  } else if (contentBlock.type === "text") {
1105
- if (!usesJsonResponseTool) {
1106
- controller.enqueue({
1107
- type: "text-end",
1108
- id: String(blockIndex)
1109
- });
1110
- }
1100
+ controller.enqueue({
1101
+ type: "text-end",
1102
+ id: String(blockIndex)
1103
+ });
1111
1104
  } else if (contentBlock.type === "tool-call") {
1112
- if (usesJsonResponseTool) {
1105
+ if (contentBlock.isJsonResponseTool) {
1106
+ isJsonResponseFromTool = true;
1113
1107
  controller.enqueue({
1114
1108
  type: "text-start",
1115
1109
  id: String(blockIndex)
@@ -1183,13 +1177,15 @@ var BedrockChatLanguageModel = class {
1183
1177
  if (((_p = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _p.toolUse) != null) {
1184
1178
  const toolUse = contentBlockStart.start.toolUse;
1185
1179
  const blockIndex = contentBlockStart.contentBlockIndex;
1180
+ const isJsonResponseTool = usesJsonResponseTool && toolUse.name === "json";
1186
1181
  contentBlocks[blockIndex] = {
1187
1182
  type: "tool-call",
1188
1183
  toolCallId: toolUse.toolUseId,
1189
1184
  toolName: toolUse.name,
1190
- jsonText: ""
1185
+ jsonText: "",
1186
+ isJsonResponseTool
1191
1187
  };
1192
- if (!usesJsonResponseTool) {
1188
+ if (!isJsonResponseTool) {
1193
1189
  controller.enqueue({
1194
1190
  type: "tool-input-start",
1195
1191
  id: toolUse.toolUseId,
@@ -1203,7 +1199,7 @@ var BedrockChatLanguageModel = class {
1203
1199
  const contentBlock = contentBlocks[blockIndex];
1204
1200
  if ((contentBlock == null ? void 0 : contentBlock.type) === "tool-call") {
1205
1201
  const delta = (_q = contentBlockDelta.delta.toolUse.input) != null ? _q : "";
1206
- if (!usesJsonResponseTool) {
1202
+ if (!contentBlock.isJsonResponseTool) {
1207
1203
  controller.enqueue({
1208
1204
  type: "tool-input-delta",
1209
1205
  id: contentBlock.toolCallId,
@@ -1215,6 +1211,20 @@ var BedrockChatLanguageModel = class {
1215
1211
  }
1216
1212
  },
1217
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
+ }
1218
1228
  controller.enqueue({
1219
1229
  type: "finish",
1220
1230
  finishReason,
@@ -1542,7 +1552,7 @@ import {
1542
1552
  import { AwsV4Signer } from "aws4fetch";
1543
1553
 
1544
1554
  // src/version.ts
1545
- var VERSION = true ? "4.0.0-beta.60" : "0.0.0-test";
1555
+ var VERSION = true ? "4.0.0-beta.62" : "0.0.0-test";
1546
1556
 
1547
1557
  // src/bedrock-sigv4-fetch.ts
1548
1558
  function createSigV4FetchFunction(getCredentials, fetch = globalThis.fetch) {