@ai-sdk/amazon-bedrock 3.0.55 → 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/CHANGELOG.md +34 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +95 -70
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +95 -70
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
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.
|
|
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";
|
|
@@ -82,7 +82,11 @@ var bedrockProviderOptions = z.object({
|
|
|
82
82
|
reasoningConfig: z.object({
|
|
83
83
|
type: z.union([z.literal("enabled"), z.literal("disabled")]).optional(),
|
|
84
84
|
budgetTokens: z.number().optional()
|
|
85
|
-
}).optional()
|
|
85
|
+
}).optional(),
|
|
86
|
+
/**
|
|
87
|
+
* Anthropic beta features to enable
|
|
88
|
+
*/
|
|
89
|
+
anthropicBeta: z.array(z.string()).optional()
|
|
86
90
|
});
|
|
87
91
|
|
|
88
92
|
// src/bedrock-error.ts
|
|
@@ -641,7 +645,7 @@ function groupIntoBlocks(prompt) {
|
|
|
641
645
|
}
|
|
642
646
|
|
|
643
647
|
// src/map-bedrock-finish-reason.ts
|
|
644
|
-
function mapBedrockFinishReason(finishReason) {
|
|
648
|
+
function mapBedrockFinishReason(finishReason, isJsonResponseFromTool) {
|
|
645
649
|
switch (finishReason) {
|
|
646
650
|
case "stop_sequence":
|
|
647
651
|
case "end_turn":
|
|
@@ -652,7 +656,7 @@ function mapBedrockFinishReason(finishReason) {
|
|
|
652
656
|
case "guardrail_intervened":
|
|
653
657
|
return "content-filter";
|
|
654
658
|
case "tool_use":
|
|
655
|
-
return "tool-calls";
|
|
659
|
+
return isJsonResponseFromTool ? "stop" : "tool-calls";
|
|
656
660
|
default:
|
|
657
661
|
return "unknown";
|
|
658
662
|
}
|
|
@@ -684,7 +688,7 @@ var BedrockChatLanguageModel = class {
|
|
|
684
688
|
toolChoice,
|
|
685
689
|
providerOptions
|
|
686
690
|
}) {
|
|
687
|
-
var _a, _b, _c, _d, _e, _f;
|
|
691
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
688
692
|
const bedrockOptions = (_a = await parseProviderOptions2({
|
|
689
693
|
provider: "bedrock",
|
|
690
694
|
providerOptions,
|
|
@@ -709,6 +713,21 @@ var BedrockChatLanguageModel = class {
|
|
|
709
713
|
setting: "seed"
|
|
710
714
|
});
|
|
711
715
|
}
|
|
716
|
+
if (temperature != null && temperature > 1) {
|
|
717
|
+
warnings.push({
|
|
718
|
+
type: "unsupported-setting",
|
|
719
|
+
setting: "temperature",
|
|
720
|
+
details: `${temperature} exceeds bedrock maximum of 1.0. clamped to 1.0`
|
|
721
|
+
});
|
|
722
|
+
temperature = 1;
|
|
723
|
+
} else if (temperature != null && temperature < 0) {
|
|
724
|
+
warnings.push({
|
|
725
|
+
type: "unsupported-setting",
|
|
726
|
+
setting: "temperature",
|
|
727
|
+
details: `${temperature} is below bedrock minimum of 0. clamped to 0`
|
|
728
|
+
});
|
|
729
|
+
temperature = 0;
|
|
730
|
+
}
|
|
712
731
|
if (responseFormat != null && responseFormat.type !== "text" && responseFormat.type !== "json") {
|
|
713
732
|
warnings.push({
|
|
714
733
|
type: "unsupported-setting",
|
|
@@ -716,14 +735,6 @@ var BedrockChatLanguageModel = class {
|
|
|
716
735
|
details: "Only text and json response formats are supported."
|
|
717
736
|
});
|
|
718
737
|
}
|
|
719
|
-
if (tools != null && (responseFormat == null ? void 0 : responseFormat.type) === "json") {
|
|
720
|
-
if (tools.length > 0) {
|
|
721
|
-
warnings.push({
|
|
722
|
-
type: "other",
|
|
723
|
-
message: "JSON response format does not support tools. The provided tools are ignored."
|
|
724
|
-
});
|
|
725
|
-
}
|
|
726
|
-
}
|
|
727
738
|
const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
|
|
728
739
|
type: "function",
|
|
729
740
|
name: "json",
|
|
@@ -731,8 +742,8 @@ var BedrockChatLanguageModel = class {
|
|
|
731
742
|
inputSchema: responseFormat.schema
|
|
732
743
|
} : void 0;
|
|
733
744
|
const { toolConfig, additionalTools, toolWarnings, betas } = await prepareTools({
|
|
734
|
-
tools: jsonResponseTool ? [
|
|
735
|
-
toolChoice: jsonResponseTool != null ? { type: "
|
|
745
|
+
tools: jsonResponseTool ? [...tools != null ? tools : [], jsonResponseTool] : tools,
|
|
746
|
+
toolChoice: jsonResponseTool != null ? { type: "required" } : toolChoice,
|
|
736
747
|
modelId: this.modelId
|
|
737
748
|
});
|
|
738
749
|
warnings.push(...toolWarnings);
|
|
@@ -742,8 +753,16 @@ var BedrockChatLanguageModel = class {
|
|
|
742
753
|
...additionalTools
|
|
743
754
|
};
|
|
744
755
|
}
|
|
745
|
-
|
|
746
|
-
|
|
756
|
+
if (betas.size > 0 || bedrockOptions.anthropicBeta) {
|
|
757
|
+
const existingBetas = (_b = bedrockOptions.anthropicBeta) != null ? _b : [];
|
|
758
|
+
const mergedBetas = betas.size > 0 ? [...existingBetas, ...Array.from(betas)] : existingBetas;
|
|
759
|
+
bedrockOptions.additionalModelRequestFields = {
|
|
760
|
+
...bedrockOptions.additionalModelRequestFields,
|
|
761
|
+
anthropic_beta: mergedBetas
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
const isThinking = ((_c = bedrockOptions.reasoningConfig) == null ? void 0 : _c.type) === "enabled";
|
|
765
|
+
const thinkingBudget = (_d = bedrockOptions.reasoningConfig) == null ? void 0 : _d.budgetTokens;
|
|
747
766
|
const inferenceConfig = {
|
|
748
767
|
...maxOutputTokens != null && { maxTokens: maxOutputTokens },
|
|
749
768
|
...temperature != null && { temperature },
|
|
@@ -760,7 +779,7 @@ var BedrockChatLanguageModel = class {
|
|
|
760
779
|
bedrockOptions.additionalModelRequestFields = {
|
|
761
780
|
...bedrockOptions.additionalModelRequestFields,
|
|
762
781
|
thinking: {
|
|
763
|
-
type: (
|
|
782
|
+
type: (_e = bedrockOptions.reasoningConfig) == null ? void 0 : _e.type,
|
|
764
783
|
budget_tokens: thinkingBudget
|
|
765
784
|
}
|
|
766
785
|
};
|
|
@@ -789,7 +808,7 @@ var BedrockChatLanguageModel = class {
|
|
|
789
808
|
details: "topK is not supported when thinking is enabled"
|
|
790
809
|
});
|
|
791
810
|
}
|
|
792
|
-
const hasAnyTools = ((
|
|
811
|
+
const hasAnyTools = ((_g = (_f = toolConfig.tools) == null ? void 0 : _f.length) != null ? _g : 0) > 0 || additionalTools;
|
|
793
812
|
let filteredPrompt = prompt;
|
|
794
813
|
if (!hasAnyTools) {
|
|
795
814
|
const hasToolContent = prompt.some(
|
|
@@ -838,27 +857,21 @@ var BedrockChatLanguageModel = class {
|
|
|
838
857
|
};
|
|
839
858
|
}
|
|
840
859
|
async getHeaders({
|
|
841
|
-
betas,
|
|
842
860
|
headers
|
|
843
861
|
}) {
|
|
844
|
-
return combineHeaders(
|
|
845
|
-
await resolve(this.config.headers),
|
|
846
|
-
betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {},
|
|
847
|
-
headers
|
|
848
|
-
);
|
|
862
|
+
return combineHeaders(await resolve(this.config.headers), headers);
|
|
849
863
|
}
|
|
850
864
|
async doGenerate(options) {
|
|
851
865
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
852
866
|
const {
|
|
853
867
|
command: args,
|
|
854
868
|
warnings,
|
|
855
|
-
usesJsonResponseTool
|
|
856
|
-
betas
|
|
869
|
+
usesJsonResponseTool
|
|
857
870
|
} = await this.getArgs(options);
|
|
858
871
|
const url = `${this.getUrl(this.modelId)}/converse`;
|
|
859
872
|
const { value: response, responseHeaders } = await postJsonToApi({
|
|
860
873
|
url,
|
|
861
|
-
headers: await this.getHeaders({
|
|
874
|
+
headers: await this.getHeaders({ headers: options.headers }),
|
|
862
875
|
body: args,
|
|
863
876
|
failedResponseHandler: createJsonErrorResponseHandler({
|
|
864
877
|
errorSchema: BedrockErrorSchema,
|
|
@@ -874,11 +887,10 @@ var BedrockChatLanguageModel = class {
|
|
|
874
887
|
fetch: this.config.fetch
|
|
875
888
|
});
|
|
876
889
|
const content = [];
|
|
890
|
+
let isJsonResponseFromTool = false;
|
|
877
891
|
for (const part of response.output.message.content) {
|
|
878
892
|
if (part.text) {
|
|
879
|
-
|
|
880
|
-
content.push({ type: "text", text: part.text });
|
|
881
|
-
}
|
|
893
|
+
content.push({ type: "text", text: part.text });
|
|
882
894
|
}
|
|
883
895
|
if (part.reasoningContent) {
|
|
884
896
|
if ("reasoningText" in part.reasoningContent) {
|
|
@@ -907,21 +919,24 @@ var BedrockChatLanguageModel = class {
|
|
|
907
919
|
}
|
|
908
920
|
}
|
|
909
921
|
if (part.toolUse) {
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
922
|
+
const isJsonResponseTool = usesJsonResponseTool && part.toolUse.name === "json";
|
|
923
|
+
if (isJsonResponseTool) {
|
|
924
|
+
isJsonResponseFromTool = true;
|
|
925
|
+
content.push({
|
|
913
926
|
type: "text",
|
|
914
927
|
text: JSON.stringify(part.toolUse.input)
|
|
915
|
-
}
|
|
928
|
+
});
|
|
929
|
+
} else {
|
|
930
|
+
content.push({
|
|
916
931
|
type: "tool-call",
|
|
917
932
|
toolCallId: (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId(),
|
|
918
933
|
toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
|
|
919
934
|
input: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : "")
|
|
920
|
-
}
|
|
921
|
-
|
|
935
|
+
});
|
|
936
|
+
}
|
|
922
937
|
}
|
|
923
938
|
}
|
|
924
|
-
const providerMetadata = response.trace || response.usage ||
|
|
939
|
+
const providerMetadata = response.trace || response.usage || isJsonResponseFromTool ? {
|
|
925
940
|
bedrock: {
|
|
926
941
|
...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
|
|
927
942
|
...((_h = response.usage) == null ? void 0 : _h.cacheWriteInputTokens) != null && {
|
|
@@ -929,13 +944,14 @@ var BedrockChatLanguageModel = class {
|
|
|
929
944
|
cacheWriteInputTokens: response.usage.cacheWriteInputTokens
|
|
930
945
|
}
|
|
931
946
|
},
|
|
932
|
-
...
|
|
947
|
+
...isJsonResponseFromTool && { isJsonResponseFromTool: true }
|
|
933
948
|
}
|
|
934
949
|
} : void 0;
|
|
935
950
|
return {
|
|
936
951
|
content,
|
|
937
952
|
finishReason: mapBedrockFinishReason(
|
|
938
|
-
response.stopReason
|
|
953
|
+
response.stopReason,
|
|
954
|
+
isJsonResponseFromTool
|
|
939
955
|
),
|
|
940
956
|
usage: {
|
|
941
957
|
inputTokens: (_i = response.usage) == null ? void 0 : _i.inputTokens,
|
|
@@ -955,13 +971,12 @@ var BedrockChatLanguageModel = class {
|
|
|
955
971
|
const {
|
|
956
972
|
command: args,
|
|
957
973
|
warnings,
|
|
958
|
-
usesJsonResponseTool
|
|
959
|
-
betas
|
|
974
|
+
usesJsonResponseTool
|
|
960
975
|
} = await this.getArgs(options);
|
|
961
976
|
const url = `${this.getUrl(this.modelId)}/converse-stream`;
|
|
962
977
|
const { value: response, responseHeaders } = await postJsonToApi({
|
|
963
978
|
url,
|
|
964
|
-
headers: await this.getHeaders({
|
|
979
|
+
headers: await this.getHeaders({ headers: options.headers }),
|
|
965
980
|
body: args,
|
|
966
981
|
failedResponseHandler: createJsonErrorResponseHandler({
|
|
967
982
|
errorSchema: BedrockErrorSchema,
|
|
@@ -978,6 +993,7 @@ var BedrockChatLanguageModel = class {
|
|
|
978
993
|
totalTokens: void 0
|
|
979
994
|
};
|
|
980
995
|
let providerMetadata = void 0;
|
|
996
|
+
let isJsonResponseFromTool = false;
|
|
981
997
|
const contentBlocks = {};
|
|
982
998
|
return {
|
|
983
999
|
stream: response.pipeThrough(
|
|
@@ -1017,7 +1033,8 @@ var BedrockChatLanguageModel = class {
|
|
|
1017
1033
|
}
|
|
1018
1034
|
if (value.messageStop) {
|
|
1019
1035
|
finishReason = mapBedrockFinishReason(
|
|
1020
|
-
value.messageStop.stopReason
|
|
1036
|
+
value.messageStop.stopReason,
|
|
1037
|
+
isJsonResponseFromTool
|
|
1021
1038
|
);
|
|
1022
1039
|
}
|
|
1023
1040
|
if (value.metadata) {
|
|
@@ -1033,14 +1050,11 @@ var BedrockChatLanguageModel = class {
|
|
|
1033
1050
|
const trace = value.metadata.trace ? {
|
|
1034
1051
|
trace: value.metadata.trace
|
|
1035
1052
|
} : void 0;
|
|
1036
|
-
if (cacheUsage || trace
|
|
1053
|
+
if (cacheUsage || trace) {
|
|
1037
1054
|
providerMetadata = {
|
|
1038
1055
|
bedrock: {
|
|
1039
1056
|
...cacheUsage,
|
|
1040
|
-
...trace
|
|
1041
|
-
...usesJsonResponseTool && {
|
|
1042
|
-
isJsonResponseFromTool: true
|
|
1043
|
-
}
|
|
1057
|
+
...trace
|
|
1044
1058
|
}
|
|
1045
1059
|
};
|
|
1046
1060
|
}
|
|
@@ -1057,20 +1071,16 @@ var BedrockChatLanguageModel = class {
|
|
|
1057
1071
|
const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
|
|
1058
1072
|
if (contentBlocks[blockIndex] == null) {
|
|
1059
1073
|
contentBlocks[blockIndex] = { type: "text" };
|
|
1060
|
-
if (!usesJsonResponseTool) {
|
|
1061
|
-
controller.enqueue({
|
|
1062
|
-
type: "text-start",
|
|
1063
|
-
id: String(blockIndex)
|
|
1064
|
-
});
|
|
1065
|
-
}
|
|
1066
|
-
}
|
|
1067
|
-
if (!usesJsonResponseTool) {
|
|
1068
1074
|
controller.enqueue({
|
|
1069
|
-
type: "text-
|
|
1070
|
-
id: String(blockIndex)
|
|
1071
|
-
delta: value.contentBlockDelta.delta.text
|
|
1075
|
+
type: "text-start",
|
|
1076
|
+
id: String(blockIndex)
|
|
1072
1077
|
});
|
|
1073
1078
|
}
|
|
1079
|
+
controller.enqueue({
|
|
1080
|
+
type: "text-delta",
|
|
1081
|
+
id: String(blockIndex),
|
|
1082
|
+
delta: value.contentBlockDelta.delta.text
|
|
1083
|
+
});
|
|
1074
1084
|
}
|
|
1075
1085
|
if (((_n = value.contentBlockStop) == null ? void 0 : _n.contentBlockIndex) != null) {
|
|
1076
1086
|
const blockIndex = value.contentBlockStop.contentBlockIndex;
|
|
@@ -1082,14 +1092,13 @@ var BedrockChatLanguageModel = class {
|
|
|
1082
1092
|
id: String(blockIndex)
|
|
1083
1093
|
});
|
|
1084
1094
|
} else if (contentBlock.type === "text") {
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
});
|
|
1090
|
-
}
|
|
1095
|
+
controller.enqueue({
|
|
1096
|
+
type: "text-end",
|
|
1097
|
+
id: String(blockIndex)
|
|
1098
|
+
});
|
|
1091
1099
|
} else if (contentBlock.type === "tool-call") {
|
|
1092
|
-
if (
|
|
1100
|
+
if (contentBlock.isJsonResponseTool) {
|
|
1101
|
+
isJsonResponseFromTool = true;
|
|
1093
1102
|
controller.enqueue({
|
|
1094
1103
|
type: "text-start",
|
|
1095
1104
|
id: String(blockIndex)
|
|
@@ -1163,13 +1172,15 @@ var BedrockChatLanguageModel = class {
|
|
|
1163
1172
|
if (((_p = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _p.toolUse) != null) {
|
|
1164
1173
|
const toolUse = contentBlockStart.start.toolUse;
|
|
1165
1174
|
const blockIndex = contentBlockStart.contentBlockIndex;
|
|
1175
|
+
const isJsonResponseTool = usesJsonResponseTool && toolUse.name === "json";
|
|
1166
1176
|
contentBlocks[blockIndex] = {
|
|
1167
1177
|
type: "tool-call",
|
|
1168
1178
|
toolCallId: toolUse.toolUseId,
|
|
1169
1179
|
toolName: toolUse.name,
|
|
1170
|
-
jsonText: ""
|
|
1180
|
+
jsonText: "",
|
|
1181
|
+
isJsonResponseTool
|
|
1171
1182
|
};
|
|
1172
|
-
if (!
|
|
1183
|
+
if (!isJsonResponseTool) {
|
|
1173
1184
|
controller.enqueue({
|
|
1174
1185
|
type: "tool-input-start",
|
|
1175
1186
|
id: toolUse.toolUseId,
|
|
@@ -1183,7 +1194,7 @@ var BedrockChatLanguageModel = class {
|
|
|
1183
1194
|
const contentBlock = contentBlocks[blockIndex];
|
|
1184
1195
|
if ((contentBlock == null ? void 0 : contentBlock.type) === "tool-call") {
|
|
1185
1196
|
const delta = (_q = contentBlockDelta.delta.toolUse.input) != null ? _q : "";
|
|
1186
|
-
if (!
|
|
1197
|
+
if (!contentBlock.isJsonResponseTool) {
|
|
1187
1198
|
controller.enqueue({
|
|
1188
1199
|
type: "tool-input-delta",
|
|
1189
1200
|
id: contentBlock.toolCallId,
|
|
@@ -1195,6 +1206,20 @@ var BedrockChatLanguageModel = class {
|
|
|
1195
1206
|
}
|
|
1196
1207
|
},
|
|
1197
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
|
+
}
|
|
1198
1223
|
controller.enqueue({
|
|
1199
1224
|
type: "finish",
|
|
1200
1225
|
finishReason,
|