@ai-sdk/amazon-bedrock 3.0.0-canary.0 → 3.0.0-canary.10
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 +110 -0
- package/dist/index.d.mts +38 -25
- package/dist/index.d.ts +38 -25
- package/dist/index.js +260 -272
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +263 -279
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -9
package/dist/index.js
CHANGED
|
@@ -29,9 +29,8 @@ module.exports = __toCommonJS(src_exports);
|
|
|
29
29
|
var import_provider_utils7 = require("@ai-sdk/provider-utils");
|
|
30
30
|
|
|
31
31
|
// src/bedrock-chat-language-model.ts
|
|
32
|
-
var import_provider4 = require("@ai-sdk/provider");
|
|
33
32
|
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
34
|
-
var
|
|
33
|
+
var import_zod3 = require("zod");
|
|
35
34
|
|
|
36
35
|
// src/bedrock-api-types.ts
|
|
37
36
|
var BEDROCK_CACHE_POINT = {
|
|
@@ -50,11 +49,26 @@ var BEDROCK_STOP_REASONS = [
|
|
|
50
49
|
"tool_use"
|
|
51
50
|
];
|
|
52
51
|
|
|
53
|
-
// src/bedrock-
|
|
52
|
+
// src/bedrock-chat-options.ts
|
|
54
53
|
var import_zod = require("zod");
|
|
55
|
-
var
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
var bedrockProviderOptions = import_zod.z.object({
|
|
55
|
+
/**
|
|
56
|
+
* Additional inference parameters that the model supports,
|
|
57
|
+
* beyond the base set of inference parameters that Converse
|
|
58
|
+
* supports in the inferenceConfig field
|
|
59
|
+
*/
|
|
60
|
+
additionalModelRequestFields: import_zod.z.record(import_zod.z.any()).optional(),
|
|
61
|
+
reasoningConfig: import_zod.z.object({
|
|
62
|
+
type: import_zod.z.union([import_zod.z.literal("enabled"), import_zod.z.literal("disabled")]).nullish(),
|
|
63
|
+
budgetTokens: import_zod.z.number().nullish()
|
|
64
|
+
}).nullish()
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// src/bedrock-error.ts
|
|
68
|
+
var import_zod2 = require("zod");
|
|
69
|
+
var BedrockErrorSchema = import_zod2.z.object({
|
|
70
|
+
message: import_zod2.z.string(),
|
|
71
|
+
type: import_zod2.z.string().nullish()
|
|
58
72
|
});
|
|
59
73
|
|
|
60
74
|
// src/bedrock-event-stream-response-handler.ts
|
|
@@ -130,9 +144,11 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
|
|
|
130
144
|
|
|
131
145
|
// src/bedrock-prepare-tools.ts
|
|
132
146
|
var import_provider2 = require("@ai-sdk/provider");
|
|
133
|
-
function prepareTools(
|
|
134
|
-
|
|
135
|
-
|
|
147
|
+
function prepareTools({
|
|
148
|
+
tools,
|
|
149
|
+
toolChoice
|
|
150
|
+
}) {
|
|
151
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
136
152
|
if (tools == null) {
|
|
137
153
|
return {
|
|
138
154
|
toolConfig: { tools: void 0, toolChoice: void 0 },
|
|
@@ -156,7 +172,6 @@ function prepareTools(mode) {
|
|
|
156
172
|
});
|
|
157
173
|
}
|
|
158
174
|
}
|
|
159
|
-
const toolChoice = mode.toolChoice;
|
|
160
175
|
if (toolChoice == null) {
|
|
161
176
|
return {
|
|
162
177
|
toolConfig: { tools: bedrockTools, toolChoice: void 0 },
|
|
@@ -191,7 +206,7 @@ function prepareTools(mode) {
|
|
|
191
206
|
default: {
|
|
192
207
|
const _exhaustiveCheck = type;
|
|
193
208
|
throw new import_provider2.UnsupportedFunctionalityError({
|
|
194
|
-
functionality: `
|
|
209
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
195
210
|
});
|
|
196
211
|
}
|
|
197
212
|
}
|
|
@@ -206,7 +221,7 @@ function getCachePoint(providerMetadata) {
|
|
|
206
221
|
return (_a = providerMetadata == null ? void 0 : providerMetadata.bedrock) == null ? void 0 : _a.cachePoint;
|
|
207
222
|
}
|
|
208
223
|
function convertToBedrockChatMessages(prompt) {
|
|
209
|
-
var _a, _b, _c, _d
|
|
224
|
+
var _a, _b, _c, _d;
|
|
210
225
|
const blocks = groupIntoBlocks(prompt);
|
|
211
226
|
let system = [];
|
|
212
227
|
const messages = [];
|
|
@@ -223,7 +238,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
223
238
|
}
|
|
224
239
|
for (const message of block.messages) {
|
|
225
240
|
system.push({ text: message.content });
|
|
226
|
-
if (getCachePoint(message.
|
|
241
|
+
if (getCachePoint(message.providerOptions)) {
|
|
227
242
|
system.push(BEDROCK_CACHE_POINT);
|
|
228
243
|
}
|
|
229
244
|
}
|
|
@@ -232,7 +247,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
232
247
|
case "user": {
|
|
233
248
|
const bedrockContent = [];
|
|
234
249
|
for (const message of block.messages) {
|
|
235
|
-
const { role, content,
|
|
250
|
+
const { role, content, providerOptions } = message;
|
|
236
251
|
switch (role) {
|
|
237
252
|
case "user": {
|
|
238
253
|
for (let j = 0; j < content.length; j++) {
|
|
@@ -244,43 +259,31 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
244
259
|
});
|
|
245
260
|
break;
|
|
246
261
|
}
|
|
247
|
-
case "image": {
|
|
248
|
-
if (part.image instanceof URL) {
|
|
249
|
-
throw new import_provider3.UnsupportedFunctionalityError({
|
|
250
|
-
functionality: "Image URLs in user messages"
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
bedrockContent.push({
|
|
254
|
-
image: {
|
|
255
|
-
format: (_b = (_a = part.mimeType) == null ? void 0 : _a.split(
|
|
256
|
-
"/"
|
|
257
|
-
)) == null ? void 0 : _b[1],
|
|
258
|
-
source: {
|
|
259
|
-
bytes: (0, import_provider_utils2.convertUint8ArrayToBase64)(
|
|
260
|
-
(_c = part.image) != null ? _c : part.image
|
|
261
|
-
)
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
});
|
|
265
|
-
break;
|
|
266
|
-
}
|
|
267
262
|
case "file": {
|
|
268
263
|
if (part.data instanceof URL) {
|
|
269
264
|
throw new import_provider3.UnsupportedFunctionalityError({
|
|
270
|
-
functionality: "File
|
|
265
|
+
functionality: "File URL data"
|
|
271
266
|
});
|
|
272
267
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
source: {
|
|
280
|
-
bytes: part.data
|
|
268
|
+
if (part.mediaType.startsWith("image/")) {
|
|
269
|
+
const bedrockImageFormat = part.mediaType === "image/*" ? void 0 : (_b = (_a = part.mediaType) == null ? void 0 : _a.split("/")) == null ? void 0 : _b[1];
|
|
270
|
+
bedrockContent.push({
|
|
271
|
+
image: {
|
|
272
|
+
format: bedrockImageFormat,
|
|
273
|
+
source: { bytes: (0, import_provider_utils2.convertToBase64)(part.data) }
|
|
281
274
|
}
|
|
282
|
-
}
|
|
283
|
-
}
|
|
275
|
+
});
|
|
276
|
+
} else {
|
|
277
|
+
bedrockContent.push({
|
|
278
|
+
document: {
|
|
279
|
+
format: (_d = (_c = part.mediaType) == null ? void 0 : _c.split(
|
|
280
|
+
"/"
|
|
281
|
+
)) == null ? void 0 : _d[1],
|
|
282
|
+
name: generateFileId(),
|
|
283
|
+
source: { bytes: (0, import_provider_utils2.convertToBase64)(part.data) }
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
}
|
|
284
287
|
break;
|
|
285
288
|
}
|
|
286
289
|
}
|
|
@@ -297,12 +300,12 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
297
300
|
text: part2.text
|
|
298
301
|
};
|
|
299
302
|
case "image":
|
|
300
|
-
if (!part2.
|
|
303
|
+
if (!part2.mediaType) {
|
|
301
304
|
throw new Error(
|
|
302
305
|
"Image mime type is required in tool result part content"
|
|
303
306
|
);
|
|
304
307
|
}
|
|
305
|
-
const format = part2.
|
|
308
|
+
const format = part2.mediaType.split("/")[1];
|
|
306
309
|
if (!isBedrockImageFormat(format)) {
|
|
307
310
|
throw new Error(
|
|
308
311
|
`Unsupported image format: ${format}`
|
|
@@ -332,7 +335,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
332
335
|
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
333
336
|
}
|
|
334
337
|
}
|
|
335
|
-
if (getCachePoint(
|
|
338
|
+
if (getCachePoint(providerOptions)) {
|
|
336
339
|
bedrockContent.push(BEDROCK_CACHE_POINT);
|
|
337
340
|
}
|
|
338
341
|
}
|
|
@@ -406,7 +409,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
406
409
|
}
|
|
407
410
|
}
|
|
408
411
|
}
|
|
409
|
-
if (getCachePoint(message.
|
|
412
|
+
if (getCachePoint(message.providerOptions)) {
|
|
410
413
|
bedrockContent.push(BEDROCK_CACHE_POINT);
|
|
411
414
|
}
|
|
412
415
|
}
|
|
@@ -494,19 +497,15 @@ function mapBedrockFinishReason(finishReason) {
|
|
|
494
497
|
|
|
495
498
|
// src/bedrock-chat-language-model.ts
|
|
496
499
|
var BedrockChatLanguageModel = class {
|
|
497
|
-
constructor(modelId,
|
|
500
|
+
constructor(modelId, config) {
|
|
498
501
|
this.modelId = modelId;
|
|
499
|
-
this.settings = settings;
|
|
500
502
|
this.config = config;
|
|
501
|
-
this.specificationVersion = "
|
|
503
|
+
this.specificationVersion = "v2";
|
|
502
504
|
this.provider = "amazon-bedrock";
|
|
503
|
-
this.defaultObjectGenerationMode = "tool";
|
|
504
|
-
this.supportsImageUrls = false;
|
|
505
505
|
}
|
|
506
506
|
getArgs({
|
|
507
|
-
mode,
|
|
508
507
|
prompt,
|
|
509
|
-
|
|
508
|
+
maxOutputTokens,
|
|
510
509
|
temperature,
|
|
511
510
|
topP,
|
|
512
511
|
topK,
|
|
@@ -515,10 +514,16 @@ var BedrockChatLanguageModel = class {
|
|
|
515
514
|
stopSequences,
|
|
516
515
|
responseFormat,
|
|
517
516
|
seed,
|
|
518
|
-
|
|
517
|
+
tools,
|
|
518
|
+
toolChoice,
|
|
519
|
+
providerOptions
|
|
519
520
|
}) {
|
|
520
|
-
var _a, _b, _c, _d, _e
|
|
521
|
-
const
|
|
521
|
+
var _a, _b, _c, _d, _e;
|
|
522
|
+
const bedrockOptions = (_a = (0, import_provider_utils3.parseProviderOptions)({
|
|
523
|
+
provider: "bedrock",
|
|
524
|
+
providerOptions,
|
|
525
|
+
schema: bedrockProviderOptions
|
|
526
|
+
})) != null ? _a : {};
|
|
522
527
|
const warnings = [];
|
|
523
528
|
if (frequencyPenalty != null) {
|
|
524
529
|
warnings.push({
|
|
@@ -552,34 +557,24 @@ var BedrockChatLanguageModel = class {
|
|
|
552
557
|
});
|
|
553
558
|
}
|
|
554
559
|
const { system, messages } = convertToBedrockChatMessages(prompt);
|
|
555
|
-
const
|
|
556
|
-
|
|
557
|
-
);
|
|
558
|
-
if (!reasoningConfigOptions.success) {
|
|
559
|
-
throw new import_provider4.InvalidArgumentError({
|
|
560
|
-
argument: "providerOptions.bedrock.reasoning_config",
|
|
561
|
-
message: "invalid reasoning configuration options",
|
|
562
|
-
cause: reasoningConfigOptions.error
|
|
563
|
-
});
|
|
564
|
-
}
|
|
565
|
-
const isThinking = ((_b = reasoningConfigOptions.data) == null ? void 0 : _b.type) === "enabled";
|
|
566
|
-
const thinkingBudget = (_e = (_c = reasoningConfigOptions.data) == null ? void 0 : _c.budgetTokens) != null ? _e : (_d = reasoningConfigOptions.data) == null ? void 0 : _d.budget_tokens;
|
|
560
|
+
const isThinking = ((_b = bedrockOptions.reasoningConfig) == null ? void 0 : _b.type) === "enabled";
|
|
561
|
+
const thinkingBudget = (_c = bedrockOptions.reasoningConfig) == null ? void 0 : _c.budgetTokens;
|
|
567
562
|
const inferenceConfig = {
|
|
568
|
-
...
|
|
563
|
+
...maxOutputTokens != null && { maxOutputTokens },
|
|
569
564
|
...temperature != null && { temperature },
|
|
570
565
|
...topP != null && { topP },
|
|
571
566
|
...stopSequences != null && { stopSequences }
|
|
572
567
|
};
|
|
573
568
|
if (isThinking && thinkingBudget != null) {
|
|
574
|
-
if (inferenceConfig.
|
|
575
|
-
inferenceConfig.
|
|
569
|
+
if (inferenceConfig.maxOutputTokens != null) {
|
|
570
|
+
inferenceConfig.maxOutputTokens += thinkingBudget;
|
|
576
571
|
} else {
|
|
577
|
-
inferenceConfig.
|
|
572
|
+
inferenceConfig.maxOutputTokens = thinkingBudget + 4096;
|
|
578
573
|
}
|
|
579
|
-
|
|
580
|
-
...
|
|
581
|
-
|
|
582
|
-
type: (
|
|
574
|
+
bedrockOptions.additionalModelRequestFields = {
|
|
575
|
+
...bedrockOptions.additionalModelRequestFields,
|
|
576
|
+
reasoningConfig: {
|
|
577
|
+
type: (_d = bedrockOptions.reasoningConfig) == null ? void 0 : _d.type,
|
|
583
578
|
budget_tokens: thinkingBudget
|
|
584
579
|
}
|
|
585
580
|
};
|
|
@@ -600,61 +595,28 @@ var BedrockChatLanguageModel = class {
|
|
|
600
595
|
details: "topP is not supported when thinking is enabled"
|
|
601
596
|
});
|
|
602
597
|
}
|
|
603
|
-
const
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
598
|
+
const { toolConfig, toolWarnings } = prepareTools({ tools, toolChoice });
|
|
599
|
+
return {
|
|
600
|
+
command: {
|
|
601
|
+
system,
|
|
602
|
+
messages,
|
|
603
|
+
additionalModelRequestFields: bedrockOptions.additionalModelRequestFields,
|
|
604
|
+
...Object.keys(inferenceConfig).length > 0 && {
|
|
605
|
+
inferenceConfig
|
|
606
|
+
},
|
|
607
|
+
...providerOptions == null ? void 0 : providerOptions.bedrock,
|
|
608
|
+
...((_e = toolConfig.tools) == null ? void 0 : _e.length) ? { toolConfig } : {}
|
|
608
609
|
},
|
|
609
|
-
|
|
610
|
-
|
|
610
|
+
warnings: [...warnings, ...toolWarnings]
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
async getSupportedUrls() {
|
|
614
|
+
return {
|
|
615
|
+
// no supported urls for bedrock
|
|
611
616
|
};
|
|
612
|
-
switch (type) {
|
|
613
|
-
case "regular": {
|
|
614
|
-
const { toolConfig, toolWarnings } = prepareTools(mode);
|
|
615
|
-
return {
|
|
616
|
-
command: {
|
|
617
|
-
...baseArgs,
|
|
618
|
-
...((_g = toolConfig.tools) == null ? void 0 : _g.length) ? { toolConfig } : {}
|
|
619
|
-
},
|
|
620
|
-
warnings: [...warnings, ...toolWarnings]
|
|
621
|
-
};
|
|
622
|
-
}
|
|
623
|
-
case "object-json": {
|
|
624
|
-
throw new import_provider4.UnsupportedFunctionalityError({
|
|
625
|
-
functionality: "json-mode object generation"
|
|
626
|
-
});
|
|
627
|
-
}
|
|
628
|
-
case "object-tool": {
|
|
629
|
-
return {
|
|
630
|
-
command: {
|
|
631
|
-
...baseArgs,
|
|
632
|
-
toolConfig: {
|
|
633
|
-
tools: [
|
|
634
|
-
{
|
|
635
|
-
toolSpec: {
|
|
636
|
-
name: mode.tool.name,
|
|
637
|
-
description: mode.tool.description,
|
|
638
|
-
inputSchema: {
|
|
639
|
-
json: mode.tool.parameters
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
],
|
|
644
|
-
toolChoice: { tool: { name: mode.tool.name } }
|
|
645
|
-
}
|
|
646
|
-
},
|
|
647
|
-
warnings
|
|
648
|
-
};
|
|
649
|
-
}
|
|
650
|
-
default: {
|
|
651
|
-
const _exhaustiveCheck = type;
|
|
652
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
617
|
}
|
|
656
618
|
async doGenerate(options) {
|
|
657
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m
|
|
619
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
658
620
|
const { command: args, warnings } = this.getArgs(options);
|
|
659
621
|
const url = `${this.getUrl(this.modelId)}/converse`;
|
|
660
622
|
const { value: response, responseHeaders } = await (0, import_provider_utils3.postJsonToApi)({
|
|
@@ -677,62 +639,68 @@ var BedrockChatLanguageModel = class {
|
|
|
677
639
|
abortSignal: options.abortSignal,
|
|
678
640
|
fetch: this.config.fetch
|
|
679
641
|
});
|
|
680
|
-
const
|
|
642
|
+
const content = [];
|
|
643
|
+
for (const part of response.output.message.content) {
|
|
644
|
+
if (part.text) {
|
|
645
|
+
content.push({ type: "text", text: part.text });
|
|
646
|
+
}
|
|
647
|
+
if (part.reasoningContent) {
|
|
648
|
+
if ("reasoningText" in part.reasoningContent) {
|
|
649
|
+
content.push({
|
|
650
|
+
type: "reasoning",
|
|
651
|
+
reasoningType: "text",
|
|
652
|
+
text: part.reasoningContent.reasoningText.text
|
|
653
|
+
});
|
|
654
|
+
if (part.reasoningContent.reasoningText.signature) {
|
|
655
|
+
content.push({
|
|
656
|
+
type: "reasoning",
|
|
657
|
+
reasoningType: "signature",
|
|
658
|
+
signature: part.reasoningContent.reasoningText.signature
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
} else if ("redactedReasoning" in part.reasoningContent) {
|
|
662
|
+
content.push({
|
|
663
|
+
type: "reasoning",
|
|
664
|
+
reasoningType: "redacted",
|
|
665
|
+
data: (_a = part.reasoningContent.redactedReasoning.data) != null ? _a : ""
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
if (part.toolUse) {
|
|
670
|
+
content.push({
|
|
671
|
+
type: "tool-call",
|
|
672
|
+
toolCallType: "function",
|
|
673
|
+
toolCallId: (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId(),
|
|
674
|
+
toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
|
|
675
|
+
args: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : "")
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
}
|
|
681
679
|
const providerMetadata = response.trace || response.usage ? {
|
|
682
680
|
bedrock: {
|
|
683
681
|
...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
|
|
684
682
|
...response.usage && {
|
|
685
683
|
usage: {
|
|
686
|
-
cacheReadInputTokens: (
|
|
687
|
-
cacheWriteInputTokens: (
|
|
684
|
+
cacheReadInputTokens: (_i = (_h = response.usage) == null ? void 0 : _h.cacheReadInputTokens) != null ? _i : Number.NaN,
|
|
685
|
+
cacheWriteInputTokens: (_k = (_j = response.usage) == null ? void 0 : _j.cacheWriteInputTokens) != null ? _k : Number.NaN
|
|
688
686
|
}
|
|
689
687
|
}
|
|
690
688
|
}
|
|
691
689
|
} : void 0;
|
|
692
|
-
const reasoning = response.output.message.content.filter((content) => content.reasoningContent).map((content) => {
|
|
693
|
-
var _a2;
|
|
694
|
-
if (content.reasoningContent && "reasoningText" in content.reasoningContent) {
|
|
695
|
-
return {
|
|
696
|
-
type: "text",
|
|
697
|
-
text: content.reasoningContent.reasoningText.text,
|
|
698
|
-
...content.reasoningContent.reasoningText.signature && {
|
|
699
|
-
signature: content.reasoningContent.reasoningText.signature
|
|
700
|
-
}
|
|
701
|
-
};
|
|
702
|
-
} else if (content.reasoningContent && "redactedReasoning" in content.reasoningContent) {
|
|
703
|
-
return {
|
|
704
|
-
type: "redacted",
|
|
705
|
-
data: (_a2 = content.reasoningContent.redactedReasoning.data) != null ? _a2 : ""
|
|
706
|
-
};
|
|
707
|
-
} else {
|
|
708
|
-
return void 0;
|
|
709
|
-
}
|
|
710
|
-
}).filter((item) => item !== void 0);
|
|
711
690
|
return {
|
|
712
|
-
|
|
713
|
-
var _a2;
|
|
714
|
-
return (_a2 = part.text) != null ? _a2 : "";
|
|
715
|
-
}).join("")) != null ? _h : void 0,
|
|
716
|
-
toolCalls: (_l = (_k = (_j = (_i = response.output) == null ? void 0 : _i.message) == null ? void 0 : _j.content) == null ? void 0 : _k.filter((part) => !!part.toolUse)) == null ? void 0 : _l.map((part) => {
|
|
717
|
-
var _a2, _b2, _c2, _d2, _e2, _f2;
|
|
718
|
-
return {
|
|
719
|
-
toolCallType: "function",
|
|
720
|
-
toolCallId: (_b2 = (_a2 = part.toolUse) == null ? void 0 : _a2.toolUseId) != null ? _b2 : this.config.generateId(),
|
|
721
|
-
toolName: (_d2 = (_c2 = part.toolUse) == null ? void 0 : _c2.name) != null ? _d2 : `tool-${this.config.generateId()}`,
|
|
722
|
-
args: JSON.stringify((_f2 = (_e2 = part.toolUse) == null ? void 0 : _e2.input) != null ? _f2 : "")
|
|
723
|
-
};
|
|
724
|
-
}),
|
|
691
|
+
content,
|
|
725
692
|
finishReason: mapBedrockFinishReason(
|
|
726
693
|
response.stopReason
|
|
727
694
|
),
|
|
728
695
|
usage: {
|
|
729
|
-
|
|
730
|
-
|
|
696
|
+
inputTokens: (_l = response.usage) == null ? void 0 : _l.inputTokens,
|
|
697
|
+
outputTokens: (_m = response.usage) == null ? void 0 : _m.outputTokens
|
|
698
|
+
},
|
|
699
|
+
response: {
|
|
700
|
+
// TODO add id, timestamp, etc
|
|
701
|
+
headers: responseHeaders
|
|
731
702
|
},
|
|
732
|
-
rawCall: { rawPrompt, rawSettings },
|
|
733
|
-
rawResponse: { headers: responseHeaders },
|
|
734
703
|
warnings,
|
|
735
|
-
reasoning: reasoning.length > 0 ? reasoning : void 0,
|
|
736
704
|
...providerMetadata && { providerMetadata }
|
|
737
705
|
};
|
|
738
706
|
}
|
|
@@ -754,17 +722,19 @@ var BedrockChatLanguageModel = class {
|
|
|
754
722
|
abortSignal: options.abortSignal,
|
|
755
723
|
fetch: this.config.fetch
|
|
756
724
|
});
|
|
757
|
-
const { messages: rawPrompt, ...rawSettings } = args;
|
|
758
725
|
let finishReason = "unknown";
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
726
|
+
const usage = {
|
|
727
|
+
inputTokens: void 0,
|
|
728
|
+
outputTokens: void 0
|
|
762
729
|
};
|
|
763
730
|
let providerMetadata = void 0;
|
|
764
731
|
const toolCallContentBlocks = {};
|
|
765
732
|
return {
|
|
766
733
|
stream: response.pipeThrough(
|
|
767
734
|
new TransformStream({
|
|
735
|
+
start(controller) {
|
|
736
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
737
|
+
},
|
|
768
738
|
transform(chunk, controller) {
|
|
769
739
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
770
740
|
function enqueueError(bedrockError) {
|
|
@@ -798,10 +768,8 @@ var BedrockChatLanguageModel = class {
|
|
|
798
768
|
);
|
|
799
769
|
}
|
|
800
770
|
if (value.metadata) {
|
|
801
|
-
usage =
|
|
802
|
-
|
|
803
|
-
completionTokens: (_d = (_c = value.metadata.usage) == null ? void 0 : _c.outputTokens) != null ? _d : Number.NaN
|
|
804
|
-
};
|
|
771
|
+
usage.inputTokens = (_b = (_a = value.metadata.usage) == null ? void 0 : _a.inputTokens) != null ? _b : usage.inputTokens;
|
|
772
|
+
usage.outputTokens = (_d = (_c = value.metadata.usage) == null ? void 0 : _c.outputTokens) != null ? _d : usage.outputTokens;
|
|
805
773
|
const cacheUsage = ((_e = value.metadata.usage) == null ? void 0 : _e.cacheReadInputTokens) != null || ((_f = value.metadata.usage) == null ? void 0 : _f.cacheWriteInputTokens) != null ? {
|
|
806
774
|
usage: {
|
|
807
775
|
cacheReadInputTokens: (_h = (_g = value.metadata.usage) == null ? void 0 : _g.cacheReadInputTokens) != null ? _h : Number.NaN,
|
|
@@ -822,8 +790,8 @@ var BedrockChatLanguageModel = class {
|
|
|
822
790
|
}
|
|
823
791
|
if (((_k = value.contentBlockDelta) == null ? void 0 : _k.delta) && "text" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.text) {
|
|
824
792
|
controller.enqueue({
|
|
825
|
-
type: "text
|
|
826
|
-
|
|
793
|
+
type: "text",
|
|
794
|
+
text: value.contentBlockDelta.delta.text
|
|
827
795
|
});
|
|
828
796
|
}
|
|
829
797
|
if (((_l = value.contentBlockDelta) == null ? void 0 : _l.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
|
|
@@ -831,16 +799,19 @@ var BedrockChatLanguageModel = class {
|
|
|
831
799
|
if ("text" in reasoningContent && reasoningContent.text) {
|
|
832
800
|
controller.enqueue({
|
|
833
801
|
type: "reasoning",
|
|
834
|
-
|
|
802
|
+
reasoningType: "text",
|
|
803
|
+
text: reasoningContent.text
|
|
835
804
|
});
|
|
836
805
|
} else if ("signature" in reasoningContent && reasoningContent.signature) {
|
|
837
806
|
controller.enqueue({
|
|
838
|
-
type: "reasoning
|
|
807
|
+
type: "reasoning",
|
|
808
|
+
reasoningType: "signature",
|
|
839
809
|
signature: reasoningContent.signature
|
|
840
810
|
});
|
|
841
811
|
} else if ("data" in reasoningContent && reasoningContent.data) {
|
|
842
812
|
controller.enqueue({
|
|
843
|
-
type: "
|
|
813
|
+
type: "reasoning",
|
|
814
|
+
reasoningType: "redacted",
|
|
844
815
|
data: reasoningContent.data
|
|
845
816
|
});
|
|
846
817
|
}
|
|
@@ -893,9 +864,8 @@ var BedrockChatLanguageModel = class {
|
|
|
893
864
|
}
|
|
894
865
|
})
|
|
895
866
|
),
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
warnings
|
|
867
|
+
// TODO request?
|
|
868
|
+
response: { headers: responseHeaders }
|
|
899
869
|
};
|
|
900
870
|
}
|
|
901
871
|
getUrl(modelId) {
|
|
@@ -903,116 +873,127 @@ var BedrockChatLanguageModel = class {
|
|
|
903
873
|
return `${this.config.baseUrl()}/model/${encodedModelId}`;
|
|
904
874
|
}
|
|
905
875
|
};
|
|
906
|
-
var
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
budgetTokens: import_zod2.z.number().nullish()
|
|
910
|
-
}).nullish();
|
|
911
|
-
var BedrockStopReasonSchema = import_zod2.z.union([
|
|
912
|
-
import_zod2.z.enum(BEDROCK_STOP_REASONS),
|
|
913
|
-
import_zod2.z.string()
|
|
876
|
+
var BedrockStopReasonSchema = import_zod3.z.union([
|
|
877
|
+
import_zod3.z.enum(BEDROCK_STOP_REASONS),
|
|
878
|
+
import_zod3.z.string()
|
|
914
879
|
]);
|
|
915
|
-
var BedrockToolUseSchema =
|
|
916
|
-
toolUseId:
|
|
917
|
-
name:
|
|
918
|
-
input:
|
|
880
|
+
var BedrockToolUseSchema = import_zod3.z.object({
|
|
881
|
+
toolUseId: import_zod3.z.string(),
|
|
882
|
+
name: import_zod3.z.string(),
|
|
883
|
+
input: import_zod3.z.unknown()
|
|
919
884
|
});
|
|
920
|
-
var BedrockReasoningTextSchema =
|
|
921
|
-
signature:
|
|
922
|
-
text:
|
|
885
|
+
var BedrockReasoningTextSchema = import_zod3.z.object({
|
|
886
|
+
signature: import_zod3.z.string().nullish(),
|
|
887
|
+
text: import_zod3.z.string()
|
|
923
888
|
});
|
|
924
|
-
var BedrockRedactedReasoningSchema =
|
|
925
|
-
data:
|
|
889
|
+
var BedrockRedactedReasoningSchema = import_zod3.z.object({
|
|
890
|
+
data: import_zod3.z.string()
|
|
926
891
|
});
|
|
927
|
-
var BedrockResponseSchema =
|
|
928
|
-
metrics:
|
|
929
|
-
latencyMs:
|
|
892
|
+
var BedrockResponseSchema = import_zod3.z.object({
|
|
893
|
+
metrics: import_zod3.z.object({
|
|
894
|
+
latencyMs: import_zod3.z.number()
|
|
930
895
|
}).nullish(),
|
|
931
|
-
output:
|
|
932
|
-
message:
|
|
933
|
-
content:
|
|
934
|
-
|
|
935
|
-
text:
|
|
896
|
+
output: import_zod3.z.object({
|
|
897
|
+
message: import_zod3.z.object({
|
|
898
|
+
content: import_zod3.z.array(
|
|
899
|
+
import_zod3.z.object({
|
|
900
|
+
text: import_zod3.z.string().nullish(),
|
|
936
901
|
toolUse: BedrockToolUseSchema.nullish(),
|
|
937
|
-
reasoningContent:
|
|
938
|
-
|
|
902
|
+
reasoningContent: import_zod3.z.union([
|
|
903
|
+
import_zod3.z.object({
|
|
939
904
|
reasoningText: BedrockReasoningTextSchema
|
|
940
905
|
}),
|
|
941
|
-
|
|
906
|
+
import_zod3.z.object({
|
|
942
907
|
redactedReasoning: BedrockRedactedReasoningSchema
|
|
943
908
|
})
|
|
944
909
|
]).nullish()
|
|
945
910
|
})
|
|
946
911
|
),
|
|
947
|
-
role:
|
|
912
|
+
role: import_zod3.z.string()
|
|
948
913
|
})
|
|
949
914
|
}),
|
|
950
915
|
stopReason: BedrockStopReasonSchema,
|
|
951
|
-
trace:
|
|
952
|
-
usage:
|
|
953
|
-
inputTokens:
|
|
954
|
-
outputTokens:
|
|
955
|
-
totalTokens:
|
|
956
|
-
cacheReadInputTokens:
|
|
957
|
-
cacheWriteInputTokens:
|
|
916
|
+
trace: import_zod3.z.unknown().nullish(),
|
|
917
|
+
usage: import_zod3.z.object({
|
|
918
|
+
inputTokens: import_zod3.z.number(),
|
|
919
|
+
outputTokens: import_zod3.z.number(),
|
|
920
|
+
totalTokens: import_zod3.z.number(),
|
|
921
|
+
cacheReadInputTokens: import_zod3.z.number().nullish(),
|
|
922
|
+
cacheWriteInputTokens: import_zod3.z.number().nullish()
|
|
958
923
|
})
|
|
959
924
|
});
|
|
960
|
-
var BedrockStreamSchema =
|
|
961
|
-
contentBlockDelta:
|
|
962
|
-
contentBlockIndex:
|
|
963
|
-
delta:
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
reasoningContent:
|
|
925
|
+
var BedrockStreamSchema = import_zod3.z.object({
|
|
926
|
+
contentBlockDelta: import_zod3.z.object({
|
|
927
|
+
contentBlockIndex: import_zod3.z.number(),
|
|
928
|
+
delta: import_zod3.z.union([
|
|
929
|
+
import_zod3.z.object({ text: import_zod3.z.string() }),
|
|
930
|
+
import_zod3.z.object({ toolUse: import_zod3.z.object({ input: import_zod3.z.string() }) }),
|
|
931
|
+
import_zod3.z.object({
|
|
932
|
+
reasoningContent: import_zod3.z.object({ text: import_zod3.z.string() })
|
|
968
933
|
}),
|
|
969
|
-
|
|
970
|
-
reasoningContent:
|
|
971
|
-
signature:
|
|
934
|
+
import_zod3.z.object({
|
|
935
|
+
reasoningContent: import_zod3.z.object({
|
|
936
|
+
signature: import_zod3.z.string()
|
|
972
937
|
})
|
|
973
938
|
}),
|
|
974
|
-
|
|
975
|
-
reasoningContent:
|
|
939
|
+
import_zod3.z.object({
|
|
940
|
+
reasoningContent: import_zod3.z.object({ data: import_zod3.z.string() })
|
|
976
941
|
})
|
|
977
942
|
]).nullish()
|
|
978
943
|
}).nullish(),
|
|
979
|
-
contentBlockStart:
|
|
980
|
-
contentBlockIndex:
|
|
981
|
-
start:
|
|
944
|
+
contentBlockStart: import_zod3.z.object({
|
|
945
|
+
contentBlockIndex: import_zod3.z.number(),
|
|
946
|
+
start: import_zod3.z.object({
|
|
982
947
|
toolUse: BedrockToolUseSchema.nullish()
|
|
983
948
|
}).nullish()
|
|
984
949
|
}).nullish(),
|
|
985
|
-
contentBlockStop:
|
|
986
|
-
contentBlockIndex:
|
|
950
|
+
contentBlockStop: import_zod3.z.object({
|
|
951
|
+
contentBlockIndex: import_zod3.z.number()
|
|
987
952
|
}).nullish(),
|
|
988
|
-
internalServerException:
|
|
989
|
-
messageStop:
|
|
990
|
-
additionalModelResponseFields:
|
|
953
|
+
internalServerException: import_zod3.z.record(import_zod3.z.unknown()).nullish(),
|
|
954
|
+
messageStop: import_zod3.z.object({
|
|
955
|
+
additionalModelResponseFields: import_zod3.z.record(import_zod3.z.unknown()).nullish(),
|
|
991
956
|
stopReason: BedrockStopReasonSchema
|
|
992
957
|
}).nullish(),
|
|
993
|
-
metadata:
|
|
994
|
-
trace:
|
|
995
|
-
usage:
|
|
996
|
-
cacheReadInputTokens:
|
|
997
|
-
cacheWriteInputTokens:
|
|
998
|
-
inputTokens:
|
|
999
|
-
outputTokens:
|
|
958
|
+
metadata: import_zod3.z.object({
|
|
959
|
+
trace: import_zod3.z.unknown().nullish(),
|
|
960
|
+
usage: import_zod3.z.object({
|
|
961
|
+
cacheReadInputTokens: import_zod3.z.number().nullish(),
|
|
962
|
+
cacheWriteInputTokens: import_zod3.z.number().nullish(),
|
|
963
|
+
inputTokens: import_zod3.z.number(),
|
|
964
|
+
outputTokens: import_zod3.z.number()
|
|
1000
965
|
}).nullish()
|
|
1001
966
|
}).nullish(),
|
|
1002
|
-
modelStreamErrorException:
|
|
1003
|
-
throttlingException:
|
|
1004
|
-
validationException:
|
|
967
|
+
modelStreamErrorException: import_zod3.z.record(import_zod3.z.unknown()).nullish(),
|
|
968
|
+
throttlingException: import_zod3.z.record(import_zod3.z.unknown()).nullish(),
|
|
969
|
+
validationException: import_zod3.z.record(import_zod3.z.unknown()).nullish()
|
|
1005
970
|
});
|
|
1006
971
|
|
|
1007
972
|
// src/bedrock-embedding-model.ts
|
|
1008
973
|
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
|
1009
|
-
|
|
974
|
+
|
|
975
|
+
// src/bedrock-embedding-options.ts
|
|
976
|
+
var import_zod4 = require("zod");
|
|
977
|
+
var bedrockEmbeddingProviderOptions = import_zod4.z.object({
|
|
978
|
+
/**
|
|
979
|
+
The number of dimensions the resulting output embeddings should have (defaults to 1024).
|
|
980
|
+
Only supported in amazon.titan-embed-text-v2:0.
|
|
981
|
+
*/
|
|
982
|
+
dimensions: import_zod4.z.union([import_zod4.z.literal(1024), import_zod4.z.literal(512), import_zod4.z.literal(256)]).optional(),
|
|
983
|
+
/**
|
|
984
|
+
Flag indicating whether or not to normalize the output embeddings. Defaults to true
|
|
985
|
+
Only supported in amazon.titan-embed-text-v2:0.
|
|
986
|
+
*/
|
|
987
|
+
normalize: import_zod4.z.boolean().optional()
|
|
988
|
+
});
|
|
989
|
+
|
|
990
|
+
// src/bedrock-embedding-model.ts
|
|
991
|
+
var import_zod5 = require("zod");
|
|
1010
992
|
var BedrockEmbeddingModel = class {
|
|
1011
|
-
constructor(modelId,
|
|
993
|
+
constructor(modelId, config) {
|
|
1012
994
|
this.modelId = modelId;
|
|
1013
|
-
this.settings = settings;
|
|
1014
995
|
this.config = config;
|
|
1015
|
-
this.specificationVersion = "
|
|
996
|
+
this.specificationVersion = "v2";
|
|
1016
997
|
this.provider = "amazon-bedrock";
|
|
1017
998
|
this.maxEmbeddingsPerCall = void 0;
|
|
1018
999
|
this.supportsParallelCalls = true;
|
|
@@ -1024,13 +1005,20 @@ var BedrockEmbeddingModel = class {
|
|
|
1024
1005
|
async doEmbed({
|
|
1025
1006
|
values,
|
|
1026
1007
|
headers,
|
|
1027
|
-
abortSignal
|
|
1008
|
+
abortSignal,
|
|
1009
|
+
providerOptions
|
|
1028
1010
|
}) {
|
|
1011
|
+
var _a;
|
|
1012
|
+
const bedrockOptions = (_a = (0, import_provider_utils4.parseProviderOptions)({
|
|
1013
|
+
provider: "bedrock",
|
|
1014
|
+
providerOptions,
|
|
1015
|
+
schema: bedrockEmbeddingProviderOptions
|
|
1016
|
+
})) != null ? _a : {};
|
|
1029
1017
|
const embedSingleText = async (inputText) => {
|
|
1030
1018
|
const args = {
|
|
1031
1019
|
inputText,
|
|
1032
|
-
dimensions:
|
|
1033
|
-
normalize:
|
|
1020
|
+
dimensions: bedrockOptions.dimensions,
|
|
1021
|
+
normalize: bedrockOptions.normalize
|
|
1034
1022
|
};
|
|
1035
1023
|
const url = this.getUrl(this.modelId);
|
|
1036
1024
|
const { value: response } = await (0, import_provider_utils4.postJsonToApi)({
|
|
@@ -1065,9 +1053,9 @@ var BedrockEmbeddingModel = class {
|
|
|
1065
1053
|
);
|
|
1066
1054
|
}
|
|
1067
1055
|
};
|
|
1068
|
-
var BedrockEmbeddingResponseSchema =
|
|
1069
|
-
embedding:
|
|
1070
|
-
inputTextTokenCount:
|
|
1056
|
+
var BedrockEmbeddingResponseSchema = import_zod5.z.object({
|
|
1057
|
+
embedding: import_zod5.z.array(import_zod5.z.number()),
|
|
1058
|
+
inputTextTokenCount: import_zod5.z.number()
|
|
1071
1059
|
});
|
|
1072
1060
|
|
|
1073
1061
|
// src/bedrock-image-model.ts
|
|
@@ -1079,7 +1067,7 @@ var modelMaxImagesPerCall = {
|
|
|
1079
1067
|
};
|
|
1080
1068
|
|
|
1081
1069
|
// src/bedrock-image-model.ts
|
|
1082
|
-
var
|
|
1070
|
+
var import_zod6 = require("zod");
|
|
1083
1071
|
var BedrockImageModel = class {
|
|
1084
1072
|
constructor(modelId, settings, config) {
|
|
1085
1073
|
this.modelId = modelId;
|
|
@@ -1161,8 +1149,8 @@ var BedrockImageModel = class {
|
|
|
1161
1149
|
};
|
|
1162
1150
|
}
|
|
1163
1151
|
};
|
|
1164
|
-
var bedrockImageResponseSchema =
|
|
1165
|
-
images:
|
|
1152
|
+
var bedrockImageResponseSchema = import_zod6.z.object({
|
|
1153
|
+
images: import_zod6.z.array(import_zod6.z.string())
|
|
1166
1154
|
});
|
|
1167
1155
|
|
|
1168
1156
|
// src/headers-utils.ts
|
|
@@ -1284,26 +1272,26 @@ function createAmazonBedrock(options = {}) {
|
|
|
1284
1272
|
})}.amazonaws.com`
|
|
1285
1273
|
)) != null ? _b : `https://bedrock-runtime.us-east-1.amazonaws.com`;
|
|
1286
1274
|
};
|
|
1287
|
-
const createChatModel = (modelId
|
|
1275
|
+
const createChatModel = (modelId) => {
|
|
1288
1276
|
var _a;
|
|
1289
|
-
return new BedrockChatLanguageModel(modelId,
|
|
1277
|
+
return new BedrockChatLanguageModel(modelId, {
|
|
1290
1278
|
baseUrl: getBaseUrl,
|
|
1291
1279
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1292
1280
|
fetch: sigv4Fetch,
|
|
1293
1281
|
generateId: import_provider_utils7.generateId
|
|
1294
1282
|
});
|
|
1295
1283
|
};
|
|
1296
|
-
const provider = function(modelId
|
|
1284
|
+
const provider = function(modelId) {
|
|
1297
1285
|
if (new.target) {
|
|
1298
1286
|
throw new Error(
|
|
1299
1287
|
"The Amazon Bedrock model function cannot be called with the new keyword."
|
|
1300
1288
|
);
|
|
1301
1289
|
}
|
|
1302
|
-
return createChatModel(modelId
|
|
1290
|
+
return createChatModel(modelId);
|
|
1303
1291
|
};
|
|
1304
|
-
const createEmbeddingModel = (modelId
|
|
1292
|
+
const createEmbeddingModel = (modelId) => {
|
|
1305
1293
|
var _a;
|
|
1306
|
-
return new BedrockEmbeddingModel(modelId,
|
|
1294
|
+
return new BedrockEmbeddingModel(modelId, {
|
|
1307
1295
|
baseUrl: getBaseUrl,
|
|
1308
1296
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1309
1297
|
fetch: sigv4Fetch
|