@ai-sdk/amazon-bedrock 4.0.161 → 4.0.165

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
@@ -239,7 +239,8 @@ import {
239
239
  async function prepareTools({
240
240
  tools,
241
241
  toolChoice,
242
- modelId
242
+ modelId,
243
+ disableParallelToolUse
243
244
  }) {
244
245
  var _a;
245
246
  const toolWarnings = [];
@@ -285,6 +286,7 @@ async function prepareTools({
285
286
  } = await prepareAnthropicTools({
286
287
  tools: ProviderTools,
287
288
  toolChoice,
289
+ disableParallelToolUse,
288
290
  supportsStructuredOutput: false,
289
291
  supportsStrictTools: false
290
292
  });
@@ -340,8 +342,17 @@ async function prepareTools({
340
342
  }
341
343
  });
342
344
  }
345
+ if (isAnthropicModel && !usingAnthropicTools && disableParallelToolUse && bedrockTools.length > 0 && (toolChoice == null ? void 0 : toolChoice.type) !== "none") {
346
+ additionalTools = {
347
+ tool_choice: (toolChoice == null ? void 0 : toolChoice.type) === "required" ? { type: "any", disable_parallel_tool_use: true } : (toolChoice == null ? void 0 : toolChoice.type) === "tool" ? {
348
+ type: "tool",
349
+ name: toolChoice.toolName,
350
+ disable_parallel_tool_use: true
351
+ } : { type: "auto", disable_parallel_tool_use: true }
352
+ };
353
+ }
343
354
  let bedrockToolChoice = void 0;
344
- if (!usingAnthropicTools && bedrockTools.length > 0 && toolChoice) {
355
+ if (!usingAnthropicTools && (additionalTools == null ? void 0 : additionalTools.tool_choice) == null && bedrockTools.length > 0 && toolChoice) {
345
356
  const type = toolChoice.type;
346
357
  switch (type) {
347
358
  case "auto":
@@ -692,7 +703,12 @@ async function convertToBedrockChatMessages(prompt, isMistral = false) {
692
703
  }
693
704
  pushCachePoint(bedrockContent, providerOptions);
694
705
  }
695
- messages.push({ role: "user", content: bedrockContent });
706
+ const previousMessage = messages.at(-1);
707
+ if ((previousMessage == null ? void 0 : previousMessage.role) === "user") {
708
+ previousMessage.content.push(...bedrockContent);
709
+ } else {
710
+ messages.push({ role: "user", content: bedrockContent });
711
+ }
696
712
  break;
697
713
  }
698
714
  case "assistant": {
@@ -701,15 +717,56 @@ async function convertToBedrockChatMessages(prompt, isMistral = false) {
701
717
  const message = block.messages[j];
702
718
  const isLastMessage = j === block.messages.length - 1;
703
719
  const { content } = message;
704
- const hasReasoningBlocks = content.some(
705
- (part) => part.type === "reasoning"
720
+ const convertedReasoningContent = await Promise.all(
721
+ content.map(async (part) => {
722
+ if (part.type !== "reasoning") {
723
+ return void 0;
724
+ }
725
+ const metadata = await parseProviderOptions({
726
+ provider: "bedrock",
727
+ providerOptions: part.providerOptions,
728
+ schema: bedrockReasoningMetadataSchema
729
+ });
730
+ if ((metadata == null ? void 0 : metadata.signature) != null) {
731
+ return {
732
+ reasoningContent: {
733
+ reasoningText: {
734
+ // do not trim reasoning text when a signature is present:
735
+ // the signature validates the exact original bytes
736
+ text: part.text,
737
+ signature: metadata.signature
738
+ }
739
+ }
740
+ };
741
+ }
742
+ if ((metadata == null ? void 0 : metadata.redactedContent) != null) {
743
+ return {
744
+ reasoningContent: {
745
+ redactedContent: metadata.redactedContent
746
+ }
747
+ };
748
+ }
749
+ if ((metadata == null ? void 0 : metadata.redactedData) != null) {
750
+ return {
751
+ reasoningContent: {
752
+ redactedReasoning: {
753
+ data: metadata.redactedData
754
+ }
755
+ }
756
+ };
757
+ }
758
+ return void 0;
759
+ })
760
+ );
761
+ const hasReplayableReasoningBlocks = convertedReasoningContent.some(
762
+ (part) => part != null
706
763
  );
707
764
  for (let k = 0; k < content.length; k++) {
708
765
  const part = content[k];
709
766
  const isLastContentPart = k === content.length - 1;
710
767
  switch (part.type) {
711
768
  case "text": {
712
- if (!part.text.trim() && !hasReasoningBlocks) {
769
+ if (!part.text.trim() && !hasReplayableReasoningBlocks) {
713
770
  break;
714
771
  }
715
772
  bedrockContent.push({
@@ -728,34 +785,9 @@ async function convertToBedrockChatMessages(prompt, isMistral = false) {
728
785
  break;
729
786
  }
730
787
  case "reasoning": {
731
- const reasoningMetadata = await parseProviderOptions({
732
- provider: "bedrock",
733
- providerOptions: part.providerOptions,
734
- schema: bedrockReasoningMetadataSchema
735
- });
736
- if ((reasoningMetadata == null ? void 0 : reasoningMetadata.signature) != null) {
737
- bedrockContent.push({
738
- reasoningContent: {
739
- reasoningText: {
740
- text: part.text,
741
- signature: reasoningMetadata.signature
742
- }
743
- }
744
- });
745
- } else if ((reasoningMetadata == null ? void 0 : reasoningMetadata.redactedContent) != null) {
746
- bedrockContent.push({
747
- reasoningContent: {
748
- redactedContent: reasoningMetadata.redactedContent
749
- }
750
- });
751
- } else if ((reasoningMetadata == null ? void 0 : reasoningMetadata.redactedData) != null) {
752
- bedrockContent.push({
753
- reasoningContent: {
754
- redactedReasoning: {
755
- data: reasoningMetadata.redactedData
756
- }
757
- }
758
- });
788
+ const convertedPart = convertedReasoningContent[k];
789
+ if (convertedPart != null) {
790
+ bedrockContent.push(convertedPart);
759
791
  }
760
792
  break;
761
793
  }
@@ -774,7 +806,9 @@ async function convertToBedrockChatMessages(prompt, isMistral = false) {
774
806
  }
775
807
  pushCachePoint(bedrockContent, message.providerOptions);
776
808
  }
777
- messages.push({ role: "assistant", content: bedrockContent });
809
+ if (bedrockContent.length > 0) {
810
+ messages.push({ role: "assistant", content: bedrockContent });
811
+ }
778
812
  break;
779
813
  }
780
814
  default: {
@@ -883,6 +917,9 @@ function mapBedrockFinishReason(finishReason, isJsonResponseFromTool) {
883
917
  }
884
918
 
885
919
  // src/bedrock-chat-language-model.ts
920
+ var anthropicProviderOptions = z4.object({
921
+ disableParallelToolUse: z4.boolean().optional()
922
+ });
886
923
  var BedrockChatLanguageModel = class {
887
924
  constructor(modelId, config) {
888
925
  this.modelId = modelId;
@@ -908,12 +945,17 @@ var BedrockChatLanguageModel = class {
908
945
  toolChoice,
909
946
  providerOptions
910
947
  }) {
911
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
948
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
912
949
  const bedrockOptions = (_a = await parseProviderOptions2({
913
950
  provider: "bedrock",
914
951
  providerOptions,
915
952
  schema: amazonBedrockLanguageModelOptions
916
953
  })) != null ? _a : {};
954
+ const anthropicOptions = await parseProviderOptions2({
955
+ provider: "anthropic",
956
+ providerOptions,
957
+ schema: anthropicProviderOptions
958
+ });
917
959
  const warnings = [];
918
960
  if (frequencyPenalty != null) {
919
961
  warnings.push({
@@ -956,7 +998,10 @@ var BedrockChatLanguageModel = class {
956
998
  });
957
999
  }
958
1000
  const isAnthropicModel = this.modelId.includes("anthropic");
959
- const isThinkingEnabled = ((_b = bedrockOptions.reasoningConfig) == null ? void 0 : _b.type) === "enabled" || ((_c = bedrockOptions.reasoningConfig) == null ? void 0 : _c.type) === "adaptive";
1001
+ const openAIModelId = (_b = /^(?:[^.]+\.)?(openai\..+)$/.exec(this.modelId)) == null ? void 0 : _b[1];
1002
+ const isOpenAIModel = openAIModelId != null;
1003
+ const isOpenAIGptOssModel = (_c = openAIModelId == null ? void 0 : openAIModelId.startsWith("openai.gpt-oss-")) != null ? _c : false;
1004
+ const isThinkingEnabled = ((_d = bedrockOptions.reasoningConfig) == null ? void 0 : _d.type) === "enabled" || ((_e = bedrockOptions.reasoningConfig) == null ? void 0 : _e.type) === "adaptive";
960
1005
  const { supportsStructuredOutput: modelSupportsStructuredOutput } = getModelCapabilities(this.modelId);
961
1006
  const useNativeStructuredOutput = isAnthropicModel && supportsNativeStructuredOutput(this.modelId) && (modelSupportsStructuredOutput || isThinkingEnabled) && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null;
962
1007
  const useJsonInstructionForStructuredOutput = isAnthropicModel && !supportsStrictTools(this.modelId) && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && tools != null && tools.length > 0;
@@ -969,7 +1014,8 @@ var BedrockChatLanguageModel = class {
969
1014
  const { toolConfig, additionalTools, toolWarnings, betas } = await prepareTools({
970
1015
  tools: jsonResponseTool ? [...tools != null ? tools : [], jsonResponseTool] : tools,
971
1016
  toolChoice: jsonResponseTool != null ? { type: "required" } : toolChoice,
972
- modelId: this.modelId
1017
+ modelId: this.modelId,
1018
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
973
1019
  });
974
1020
  warnings.push(...toolWarnings);
975
1021
  if (additionalTools) {
@@ -979,16 +1025,16 @@ var BedrockChatLanguageModel = class {
979
1025
  };
980
1026
  }
981
1027
  if (betas.size > 0 || bedrockOptions.anthropicBeta) {
982
- const existingBetas = (_d = bedrockOptions.anthropicBeta) != null ? _d : [];
1028
+ const existingBetas = (_f = bedrockOptions.anthropicBeta) != null ? _f : [];
983
1029
  const mergedBetas = betas.size > 0 ? [...existingBetas, ...Array.from(betas)] : existingBetas;
984
1030
  bedrockOptions.additionalModelRequestFields = {
985
1031
  ...bedrockOptions.additionalModelRequestFields,
986
1032
  anthropic_beta: mergedBetas
987
1033
  };
988
1034
  }
989
- const thinkingType = (_e = bedrockOptions.reasoningConfig) == null ? void 0 : _e.type;
990
- const thinkingBudget = thinkingType === "enabled" ? (_f = bedrockOptions.reasoningConfig) == null ? void 0 : _f.budgetTokens : void 0;
991
- const thinkingDisplay = thinkingType === "adaptive" ? (_g = bedrockOptions.reasoningConfig) == null ? void 0 : _g.display : void 0;
1035
+ const thinkingType = (_g = bedrockOptions.reasoningConfig) == null ? void 0 : _g.type;
1036
+ const thinkingBudget = thinkingType === "enabled" ? (_h = bedrockOptions.reasoningConfig) == null ? void 0 : _h.budgetTokens : void 0;
1037
+ const thinkingDisplay = thinkingType === "adaptive" ? (_i = bedrockOptions.reasoningConfig) == null ? void 0 : _i.display : void 0;
992
1038
  const isAnthropicThinkingEnabled = isAnthropicModel && isThinkingEnabled;
993
1039
  const inferenceConfig = {
994
1040
  ...maxOutputTokens != null && { maxTokens: maxOutputTokens },
@@ -1021,7 +1067,7 @@ var BedrockChatLanguageModel = class {
1021
1067
  };
1022
1068
  }
1023
1069
  } else if (!isAnthropicModel) {
1024
- if (((_h = bedrockOptions.reasoningConfig) == null ? void 0 : _h.budgetTokens) != null) {
1070
+ if (((_j = bedrockOptions.reasoningConfig) == null ? void 0 : _j.budgetTokens) != null) {
1025
1071
  warnings.push({
1026
1072
  type: "unsupported",
1027
1073
  feature: "budgetTokens",
@@ -1036,21 +1082,26 @@ var BedrockChatLanguageModel = class {
1036
1082
  });
1037
1083
  }
1038
1084
  }
1039
- const maxReasoningEffort = (_i = bedrockOptions.reasoningConfig) == null ? void 0 : _i.maxReasoningEffort;
1040
- const isOpenAIModel = this.modelId.startsWith("openai.");
1085
+ const maxReasoningEffort = (_k = bedrockOptions.reasoningConfig) == null ? void 0 : _k.maxReasoningEffort;
1041
1086
  if (maxReasoningEffort != null) {
1042
1087
  if (isAnthropicModel) {
1043
1088
  bedrockOptions.additionalModelRequestFields = {
1044
1089
  ...bedrockOptions.additionalModelRequestFields,
1045
1090
  output_config: {
1046
- ...(_j = bedrockOptions.additionalModelRequestFields) == null ? void 0 : _j.output_config,
1091
+ ...(_l = bedrockOptions.additionalModelRequestFields) == null ? void 0 : _l.output_config,
1047
1092
  effort: maxReasoningEffort
1048
1093
  }
1049
1094
  };
1050
1095
  } else if (isOpenAIModel) {
1051
- bedrockOptions.additionalModelRequestFields = {
1096
+ bedrockOptions.additionalModelRequestFields = isOpenAIGptOssModel ? {
1052
1097
  ...bedrockOptions.additionalModelRequestFields,
1053
1098
  reasoning_effort: maxReasoningEffort
1099
+ } : {
1100
+ ...bedrockOptions.additionalModelRequestFields,
1101
+ reasoning: {
1102
+ ...(_m = bedrockOptions.additionalModelRequestFields) == null ? void 0 : _m.reasoning,
1103
+ effort: maxReasoningEffort
1104
+ }
1054
1105
  };
1055
1106
  } else {
1056
1107
  bedrockOptions.additionalModelRequestFields = {
@@ -1067,7 +1118,7 @@ var BedrockChatLanguageModel = class {
1067
1118
  bedrockOptions.additionalModelRequestFields = {
1068
1119
  ...bedrockOptions.additionalModelRequestFields,
1069
1120
  output_config: {
1070
- ...(_k = bedrockOptions.additionalModelRequestFields) == null ? void 0 : _k.output_config,
1121
+ ...(_n = bedrockOptions.additionalModelRequestFields) == null ? void 0 : _n.output_config,
1071
1122
  format: {
1072
1123
  type: "json_schema",
1073
1124
  schema: sanitizeJsonSchema(responseFormat.schema)
@@ -1099,7 +1150,7 @@ var BedrockChatLanguageModel = class {
1099
1150
  details: "topK is not supported when thinking is enabled"
1100
1151
  });
1101
1152
  }
1102
- const hasAnyTools = ((_m = (_l = toolConfig.tools) == null ? void 0 : _l.length) != null ? _m : 0) > 0 || additionalTools;
1153
+ const hasAnyTools = ((_p = (_o = toolConfig.tools) == null ? void 0 : _o.length) != null ? _p : 0) > 0 || additionalTools;
1103
1154
  let filteredPrompt = prompt;
1104
1155
  if (!hasAnyTools) {
1105
1156
  const hasToolContent = prompt.some(
@@ -1729,6 +1780,10 @@ var BedrockReasoningTextSchema = z4.object({
1729
1780
  var BedrockRedactedReasoningSchema = z4.object({
1730
1781
  data: z4.string()
1731
1782
  });
1783
+ var AmazonBedrockCacheDetailSchema = z4.object({
1784
+ inputTokens: z4.number(),
1785
+ ttl: z4.string()
1786
+ }).catchall(z4.json());
1732
1787
  var BedrockResponseSchema = z4.object({
1733
1788
  metrics: z4.object({
1734
1789
  latencyMs: z4.number()
@@ -1770,8 +1825,8 @@ var BedrockResponseSchema = z4.object({
1770
1825
  totalTokens: z4.number(),
1771
1826
  cacheReadInputTokens: z4.number().nullish(),
1772
1827
  cacheWriteInputTokens: z4.number().nullish(),
1773
- cacheDetails: z4.array(z4.object({ inputTokens: z4.number(), ttl: z4.string() })).nullish()
1774
- })
1828
+ cacheDetails: z4.array(AmazonBedrockCacheDetailSchema).nullish()
1829
+ }).catchall(z4.json())
1775
1830
  });
1776
1831
  var BedrockStreamSchema = z4.object({
1777
1832
  contentBlockDelta: z4.object({
@@ -1819,10 +1874,11 @@ var BedrockStreamSchema = z4.object({
1819
1874
  usage: z4.object({
1820
1875
  cacheReadInputTokens: z4.number().nullish(),
1821
1876
  cacheWriteInputTokens: z4.number().nullish(),
1822
- cacheDetails: z4.array(z4.object({ inputTokens: z4.number(), ttl: z4.string() })).nullish(),
1877
+ cacheDetails: z4.array(AmazonBedrockCacheDetailSchema).nullish(),
1823
1878
  inputTokens: z4.number(),
1824
- outputTokens: z4.number()
1825
- }).nullish()
1879
+ outputTokens: z4.number(),
1880
+ totalTokens: z4.number().optional()
1881
+ }).catchall(z4.json()).nullish()
1826
1882
  }).nullish(),
1827
1883
  modelStreamErrorException: z4.record(z4.string(), z4.unknown()).nullish(),
1828
1884
  serviceUnavailableException: z4.record(z4.string(), z4.unknown()).nullish(),
@@ -2255,7 +2311,7 @@ import {
2255
2311
  import { AwsV4Signer } from "aws4fetch";
2256
2312
 
2257
2313
  // src/version.ts
2258
- var VERSION = true ? "4.0.161" : "0.0.0-test";
2314
+ var VERSION = true ? "4.0.165" : "0.0.0-test";
2259
2315
 
2260
2316
  // src/bedrock-sigv4-fetch.ts
2261
2317
  function createSigV4FetchFunction(getCredentials, fetch, service = "bedrock") {