@ai-sdk/amazon-bedrock 2.2.10 → 3.0.0-alpha.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 +253 -20
- package/dist/index.d.mts +46 -34
- package/dist/index.d.ts +46 -34
- package/dist/index.js +367 -361
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +374 -366
- package/dist/index.mjs.map +1 -1
- package/package.json +13 -11
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")]).optional(),
|
|
63
|
+
budgetTokens: import_zod.z.number().optional()
|
|
64
|
+
}).optional()
|
|
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
|
|
@@ -74,7 +88,7 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
|
|
|
74
88
|
responseHeaders,
|
|
75
89
|
value: response.body.pipeThrough(
|
|
76
90
|
new TransformStream({
|
|
77
|
-
transform(chunk, controller) {
|
|
91
|
+
async transform(chunk, controller) {
|
|
78
92
|
var _a, _b;
|
|
79
93
|
const newBuffer = new Uint8Array(buffer.length + chunk.length);
|
|
80
94
|
newBuffer.set(buffer);
|
|
@@ -95,7 +109,7 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
|
|
|
95
109
|
buffer = buffer.slice(totalLength);
|
|
96
110
|
if (((_a = decoded.headers[":message-type"]) == null ? void 0 : _a.value) === "event") {
|
|
97
111
|
const data = textDecoder.decode(decoded.body);
|
|
98
|
-
const parsedDataResult = (0, import_provider_utils.safeParseJSON)({ text: data });
|
|
112
|
+
const parsedDataResult = await (0, import_provider_utils.safeParseJSON)({ text: data });
|
|
99
113
|
if (!parsedDataResult.success) {
|
|
100
114
|
controller.enqueue(parsedDataResult);
|
|
101
115
|
break;
|
|
@@ -104,7 +118,7 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
|
|
|
104
118
|
let wrappedData = {
|
|
105
119
|
[(_b = decoded.headers[":event-type"]) == null ? void 0 : _b.value]: parsedDataResult.value
|
|
106
120
|
};
|
|
107
|
-
const validatedWrappedData = (0, import_provider_utils.safeValidateTypes)({
|
|
121
|
+
const validatedWrappedData = await (0, import_provider_utils.safeValidateTypes)({
|
|
108
122
|
value: wrappedData,
|
|
109
123
|
schema: chunkSchema
|
|
110
124
|
});
|
|
@@ -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
|
}
|
|
@@ -205,8 +220,8 @@ function getCachePoint(providerMetadata) {
|
|
|
205
220
|
var _a;
|
|
206
221
|
return (_a = providerMetadata == null ? void 0 : providerMetadata.bedrock) == null ? void 0 : _a.cachePoint;
|
|
207
222
|
}
|
|
208
|
-
function convertToBedrockChatMessages(prompt) {
|
|
209
|
-
var _a, _b, _c, _d
|
|
223
|
+
async function convertToBedrockChatMessages(prompt) {
|
|
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
|
}
|
|
@@ -366,32 +369,39 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
366
369
|
break;
|
|
367
370
|
}
|
|
368
371
|
case "reasoning": {
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
// because Bedrock does not allow trailing whitespace
|
|
374
|
-
// in pre-filled assistant responses
|
|
375
|
-
text: trimIfLast(
|
|
376
|
-
isLastBlock,
|
|
377
|
-
isLastMessage,
|
|
378
|
-
isLastContentPart,
|
|
379
|
-
part.text
|
|
380
|
-
),
|
|
381
|
-
signature: part.signature
|
|
382
|
-
}
|
|
383
|
-
}
|
|
372
|
+
const reasoningMetadata = await (0, import_provider_utils2.parseProviderOptions)({
|
|
373
|
+
provider: "bedrock",
|
|
374
|
+
providerOptions: part.providerOptions,
|
|
375
|
+
schema: bedrockReasoningMetadataSchema
|
|
384
376
|
});
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
377
|
+
if (reasoningMetadata != null) {
|
|
378
|
+
if (reasoningMetadata.signature != null) {
|
|
379
|
+
bedrockContent.push({
|
|
380
|
+
reasoningContent: {
|
|
381
|
+
reasoningText: {
|
|
382
|
+
// trim the last text part if it's the last message in the block
|
|
383
|
+
// because Bedrock does not allow trailing whitespace
|
|
384
|
+
// in pre-filled assistant responses
|
|
385
|
+
text: trimIfLast(
|
|
386
|
+
isLastBlock,
|
|
387
|
+
isLastMessage,
|
|
388
|
+
isLastContentPart,
|
|
389
|
+
part.text
|
|
390
|
+
),
|
|
391
|
+
signature: reasoningMetadata.signature
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
} else if (reasoningMetadata.redactedData != null) {
|
|
396
|
+
bedrockContent.push({
|
|
397
|
+
reasoningContent: {
|
|
398
|
+
redactedReasoning: {
|
|
399
|
+
data: reasoningMetadata.redactedData
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
});
|
|
393
403
|
}
|
|
394
|
-
}
|
|
404
|
+
}
|
|
395
405
|
break;
|
|
396
406
|
}
|
|
397
407
|
case "tool-call": {
|
|
@@ -406,7 +416,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
406
416
|
}
|
|
407
417
|
}
|
|
408
418
|
}
|
|
409
|
-
if (getCachePoint(message.
|
|
419
|
+
if (getCachePoint(message.providerOptions)) {
|
|
410
420
|
bedrockContent.push(BEDROCK_CACHE_POINT);
|
|
411
421
|
}
|
|
412
422
|
}
|
|
@@ -494,19 +504,18 @@ function mapBedrockFinishReason(finishReason) {
|
|
|
494
504
|
|
|
495
505
|
// src/bedrock-chat-language-model.ts
|
|
496
506
|
var BedrockChatLanguageModel = class {
|
|
497
|
-
constructor(modelId,
|
|
507
|
+
constructor(modelId, config) {
|
|
498
508
|
this.modelId = modelId;
|
|
499
|
-
this.settings = settings;
|
|
500
509
|
this.config = config;
|
|
501
|
-
this.specificationVersion = "
|
|
510
|
+
this.specificationVersion = "v2";
|
|
502
511
|
this.provider = "amazon-bedrock";
|
|
503
|
-
this.
|
|
504
|
-
|
|
512
|
+
this.supportedUrls = {
|
|
513
|
+
// no supported urls for bedrock
|
|
514
|
+
};
|
|
505
515
|
}
|
|
506
|
-
getArgs({
|
|
507
|
-
mode,
|
|
516
|
+
async getArgs({
|
|
508
517
|
prompt,
|
|
509
|
-
|
|
518
|
+
maxOutputTokens,
|
|
510
519
|
temperature,
|
|
511
520
|
topP,
|
|
512
521
|
topK,
|
|
@@ -515,10 +524,16 @@ var BedrockChatLanguageModel = class {
|
|
|
515
524
|
stopSequences,
|
|
516
525
|
responseFormat,
|
|
517
526
|
seed,
|
|
518
|
-
|
|
527
|
+
tools,
|
|
528
|
+
toolChoice,
|
|
529
|
+
providerOptions
|
|
519
530
|
}) {
|
|
520
|
-
var _a, _b, _c, _d, _e
|
|
521
|
-
const
|
|
531
|
+
var _a, _b, _c, _d, _e;
|
|
532
|
+
const bedrockOptions = (_a = await (0, import_provider_utils3.parseProviderOptions)({
|
|
533
|
+
provider: "bedrock",
|
|
534
|
+
providerOptions,
|
|
535
|
+
schema: bedrockProviderOptions
|
|
536
|
+
})) != null ? _a : {};
|
|
522
537
|
const warnings = [];
|
|
523
538
|
if (frequencyPenalty != null) {
|
|
524
539
|
warnings.push({
|
|
@@ -551,35 +566,25 @@ var BedrockChatLanguageModel = class {
|
|
|
551
566
|
details: "JSON response format is not supported."
|
|
552
567
|
});
|
|
553
568
|
}
|
|
554
|
-
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;
|
|
569
|
+
const { system, messages } = await convertToBedrockChatMessages(prompt);
|
|
570
|
+
const isThinking = ((_b = bedrockOptions.reasoningConfig) == null ? void 0 : _b.type) === "enabled";
|
|
571
|
+
const thinkingBudget = (_c = bedrockOptions.reasoningConfig) == null ? void 0 : _c.budgetTokens;
|
|
567
572
|
const inferenceConfig = {
|
|
568
|
-
...
|
|
573
|
+
...maxOutputTokens != null && { maxOutputTokens },
|
|
569
574
|
...temperature != null && { temperature },
|
|
570
575
|
...topP != null && { topP },
|
|
571
576
|
...stopSequences != null && { stopSequences }
|
|
572
577
|
};
|
|
573
578
|
if (isThinking && thinkingBudget != null) {
|
|
574
|
-
if (inferenceConfig.
|
|
575
|
-
inferenceConfig.
|
|
579
|
+
if (inferenceConfig.maxOutputTokens != null) {
|
|
580
|
+
inferenceConfig.maxOutputTokens += thinkingBudget;
|
|
576
581
|
} else {
|
|
577
|
-
inferenceConfig.
|
|
582
|
+
inferenceConfig.maxOutputTokens = thinkingBudget + 4096;
|
|
578
583
|
}
|
|
579
|
-
|
|
580
|
-
...
|
|
581
|
-
|
|
582
|
-
type: (
|
|
584
|
+
bedrockOptions.additionalModelRequestFields = {
|
|
585
|
+
...bedrockOptions.additionalModelRequestFields,
|
|
586
|
+
reasoningConfig: {
|
|
587
|
+
type: (_d = bedrockOptions.reasoningConfig) == null ? void 0 : _d.type,
|
|
583
588
|
budget_tokens: thinkingBudget
|
|
584
589
|
}
|
|
585
590
|
};
|
|
@@ -600,62 +605,24 @@ var BedrockChatLanguageModel = class {
|
|
|
600
605
|
details: "topP is not supported when thinking is enabled"
|
|
601
606
|
});
|
|
602
607
|
}
|
|
603
|
-
const
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
+
const { toolConfig, toolWarnings } = prepareTools({ tools, toolChoice });
|
|
609
|
+
return {
|
|
610
|
+
command: {
|
|
611
|
+
system,
|
|
612
|
+
messages,
|
|
613
|
+
additionalModelRequestFields: bedrockOptions.additionalModelRequestFields,
|
|
614
|
+
...Object.keys(inferenceConfig).length > 0 && {
|
|
615
|
+
inferenceConfig
|
|
616
|
+
},
|
|
617
|
+
...providerOptions == null ? void 0 : providerOptions.bedrock,
|
|
618
|
+
...((_e = toolConfig.tools) == null ? void 0 : _e.length) ? { toolConfig } : {}
|
|
608
619
|
},
|
|
609
|
-
|
|
610
|
-
...providerMetadata == null ? void 0 : providerMetadata.bedrock
|
|
620
|
+
warnings: [...warnings, ...toolWarnings]
|
|
611
621
|
};
|
|
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
622
|
}
|
|
656
623
|
async doGenerate(options) {
|
|
657
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n
|
|
658
|
-
const { command: args, warnings } = this.getArgs(options);
|
|
624
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
625
|
+
const { command: args, warnings } = await this.getArgs(options);
|
|
659
626
|
const url = `${this.getUrl(this.modelId)}/converse`;
|
|
660
627
|
const { value: response, responseHeaders } = await (0, import_provider_utils3.postJsonToApi)({
|
|
661
628
|
url,
|
|
@@ -677,67 +644,78 @@ var BedrockChatLanguageModel = class {
|
|
|
677
644
|
abortSignal: options.abortSignal,
|
|
678
645
|
fetch: this.config.fetch
|
|
679
646
|
});
|
|
680
|
-
const
|
|
647
|
+
const content = [];
|
|
648
|
+
for (const part of response.output.message.content) {
|
|
649
|
+
if (part.text) {
|
|
650
|
+
content.push({ type: "text", text: part.text });
|
|
651
|
+
}
|
|
652
|
+
if (part.reasoningContent) {
|
|
653
|
+
if ("reasoningText" in part.reasoningContent) {
|
|
654
|
+
const reasoning = {
|
|
655
|
+
type: "reasoning",
|
|
656
|
+
text: part.reasoningContent.reasoningText.text
|
|
657
|
+
};
|
|
658
|
+
if (part.reasoningContent.reasoningText.signature) {
|
|
659
|
+
reasoning.providerMetadata = {
|
|
660
|
+
bedrock: {
|
|
661
|
+
signature: part.reasoningContent.reasoningText.signature
|
|
662
|
+
}
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
content.push(reasoning);
|
|
666
|
+
} else if ("redactedReasoning" in part.reasoningContent) {
|
|
667
|
+
content.push({
|
|
668
|
+
type: "reasoning",
|
|
669
|
+
text: "",
|
|
670
|
+
providerMetadata: {
|
|
671
|
+
bedrock: {
|
|
672
|
+
redactedData: (_a = part.reasoningContent.redactedReasoning.data) != null ? _a : ""
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
});
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
if (part.toolUse) {
|
|
679
|
+
content.push({
|
|
680
|
+
type: "tool-call",
|
|
681
|
+
toolCallType: "function",
|
|
682
|
+
toolCallId: (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId(),
|
|
683
|
+
toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
|
|
684
|
+
args: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : "")
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
}
|
|
681
688
|
const providerMetadata = response.trace || response.usage ? {
|
|
682
689
|
bedrock: {
|
|
683
690
|
...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
|
|
684
|
-
...response.usage && {
|
|
691
|
+
...((_h = response.usage) == null ? void 0 : _h.cacheWriteInputTokens) != null && {
|
|
685
692
|
usage: {
|
|
686
|
-
|
|
687
|
-
cacheWriteInputTokens: (_d = (_c = response.usage) == null ? void 0 : _c.cacheWriteInputTokens) != null ? _d : Number.NaN
|
|
693
|
+
cacheWriteInputTokens: response.usage.cacheWriteInputTokens
|
|
688
694
|
}
|
|
689
695
|
}
|
|
690
696
|
}
|
|
691
697
|
} : 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
698
|
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
|
-
}),
|
|
699
|
+
content,
|
|
725
700
|
finishReason: mapBedrockFinishReason(
|
|
726
701
|
response.stopReason
|
|
727
702
|
),
|
|
728
703
|
usage: {
|
|
729
|
-
|
|
730
|
-
|
|
704
|
+
inputTokens: (_i = response.usage) == null ? void 0 : _i.inputTokens,
|
|
705
|
+
outputTokens: (_j = response.usage) == null ? void 0 : _j.outputTokens,
|
|
706
|
+
totalTokens: ((_k = response.usage) == null ? void 0 : _k.inputTokens) + ((_l = response.usage) == null ? void 0 : _l.outputTokens),
|
|
707
|
+
cachedInputTokens: (_n = (_m = response.usage) == null ? void 0 : _m.cacheReadInputTokens) != null ? _n : void 0
|
|
708
|
+
},
|
|
709
|
+
response: {
|
|
710
|
+
// TODO add id, timestamp, etc
|
|
711
|
+
headers: responseHeaders
|
|
731
712
|
},
|
|
732
|
-
rawCall: { rawPrompt, rawSettings },
|
|
733
|
-
rawResponse: { headers: responseHeaders },
|
|
734
713
|
warnings,
|
|
735
|
-
reasoning: reasoning.length > 0 ? reasoning : void 0,
|
|
736
714
|
...providerMetadata && { providerMetadata }
|
|
737
715
|
};
|
|
738
716
|
}
|
|
739
717
|
async doStream(options) {
|
|
740
|
-
const { command: args, warnings } = this.getArgs(options);
|
|
718
|
+
const { command: args, warnings } = await this.getArgs(options);
|
|
741
719
|
const url = `${this.getUrl(this.modelId)}/converse-stream`;
|
|
742
720
|
const { value: response, responseHeaders } = await (0, import_provider_utils3.postJsonToApi)({
|
|
743
721
|
url,
|
|
@@ -754,19 +732,22 @@ var BedrockChatLanguageModel = class {
|
|
|
754
732
|
abortSignal: options.abortSignal,
|
|
755
733
|
fetch: this.config.fetch
|
|
756
734
|
});
|
|
757
|
-
const { messages: rawPrompt, ...rawSettings } = args;
|
|
758
735
|
let finishReason = "unknown";
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
736
|
+
const usage = {
|
|
737
|
+
inputTokens: void 0,
|
|
738
|
+
outputTokens: void 0,
|
|
739
|
+
totalTokens: void 0
|
|
762
740
|
};
|
|
763
741
|
let providerMetadata = void 0;
|
|
764
742
|
const toolCallContentBlocks = {};
|
|
765
743
|
return {
|
|
766
744
|
stream: response.pipeThrough(
|
|
767
745
|
new TransformStream({
|
|
746
|
+
start(controller) {
|
|
747
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
748
|
+
},
|
|
768
749
|
transform(chunk, controller) {
|
|
769
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m
|
|
750
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
770
751
|
function enqueueError(bedrockError) {
|
|
771
752
|
finishReason = "error";
|
|
772
753
|
controller.enqueue({ type: "error", error: bedrockError });
|
|
@@ -798,14 +779,13 @@ var BedrockChatLanguageModel = class {
|
|
|
798
779
|
);
|
|
799
780
|
}
|
|
800
781
|
if (value.metadata) {
|
|
801
|
-
usage =
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
const cacheUsage = ((
|
|
782
|
+
usage.inputTokens = (_b = (_a = value.metadata.usage) == null ? void 0 : _a.inputTokens) != null ? _b : usage.inputTokens;
|
|
783
|
+
usage.outputTokens = (_d = (_c = value.metadata.usage) == null ? void 0 : _c.outputTokens) != null ? _d : usage.outputTokens;
|
|
784
|
+
usage.totalTokens = ((_e = usage.inputTokens) != null ? _e : 0) + ((_f = usage.outputTokens) != null ? _f : 0);
|
|
785
|
+
usage.cachedInputTokens = (_h = (_g = value.metadata.usage) == null ? void 0 : _g.cacheReadInputTokens) != null ? _h : usage.cachedInputTokens;
|
|
786
|
+
const cacheUsage = ((_i = value.metadata.usage) == null ? void 0 : _i.cacheWriteInputTokens) != null ? {
|
|
806
787
|
usage: {
|
|
807
|
-
|
|
808
|
-
cacheWriteInputTokens: (_j = (_i = value.metadata.usage) == null ? void 0 : _i.cacheWriteInputTokens) != null ? _j : Number.NaN
|
|
788
|
+
cacheWriteInputTokens: value.metadata.usage.cacheWriteInputTokens
|
|
809
789
|
}
|
|
810
790
|
} : void 0;
|
|
811
791
|
const trace = value.metadata.trace ? {
|
|
@@ -820,33 +800,45 @@ var BedrockChatLanguageModel = class {
|
|
|
820
800
|
};
|
|
821
801
|
}
|
|
822
802
|
}
|
|
823
|
-
if (((
|
|
803
|
+
if (((_j = value.contentBlockDelta) == null ? void 0 : _j.delta) && "text" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.text) {
|
|
824
804
|
controller.enqueue({
|
|
825
|
-
type: "text
|
|
826
|
-
|
|
805
|
+
type: "text",
|
|
806
|
+
text: value.contentBlockDelta.delta.text
|
|
827
807
|
});
|
|
828
808
|
}
|
|
829
|
-
if (((
|
|
809
|
+
if (((_k = value.contentBlockDelta) == null ? void 0 : _k.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
|
|
830
810
|
const reasoningContent = value.contentBlockDelta.delta.reasoningContent;
|
|
831
811
|
if ("text" in reasoningContent && reasoningContent.text) {
|
|
832
812
|
controller.enqueue({
|
|
833
813
|
type: "reasoning",
|
|
834
|
-
|
|
814
|
+
text: reasoningContent.text
|
|
835
815
|
});
|
|
836
816
|
} else if ("signature" in reasoningContent && reasoningContent.signature) {
|
|
837
817
|
controller.enqueue({
|
|
838
|
-
type: "reasoning
|
|
839
|
-
|
|
818
|
+
type: "reasoning",
|
|
819
|
+
text: "",
|
|
820
|
+
providerMetadata: {
|
|
821
|
+
bedrock: {
|
|
822
|
+
signature: reasoningContent.signature
|
|
823
|
+
}
|
|
824
|
+
}
|
|
840
825
|
});
|
|
826
|
+
controller.enqueue({ type: "reasoning-part-finish" });
|
|
841
827
|
} else if ("data" in reasoningContent && reasoningContent.data) {
|
|
842
828
|
controller.enqueue({
|
|
843
|
-
type: "
|
|
844
|
-
|
|
829
|
+
type: "reasoning",
|
|
830
|
+
text: "",
|
|
831
|
+
providerMetadata: {
|
|
832
|
+
bedrock: {
|
|
833
|
+
redactedData: reasoningContent.data
|
|
834
|
+
}
|
|
835
|
+
}
|
|
845
836
|
});
|
|
837
|
+
controller.enqueue({ type: "reasoning-part-finish" });
|
|
846
838
|
}
|
|
847
839
|
}
|
|
848
840
|
const contentBlockStart = value.contentBlockStart;
|
|
849
|
-
if (((
|
|
841
|
+
if (((_l = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _l.toolUse) != null) {
|
|
850
842
|
const toolUse = contentBlockStart.start.toolUse;
|
|
851
843
|
toolCallContentBlocks[contentBlockStart.contentBlockIndex] = {
|
|
852
844
|
toolCallId: toolUse.toolUseId,
|
|
@@ -857,7 +849,7 @@ var BedrockChatLanguageModel = class {
|
|
|
857
849
|
const contentBlockDelta = value.contentBlockDelta;
|
|
858
850
|
if ((contentBlockDelta == null ? void 0 : contentBlockDelta.delta) && "toolUse" in contentBlockDelta.delta && contentBlockDelta.delta.toolUse) {
|
|
859
851
|
const contentBlock = toolCallContentBlocks[contentBlockDelta.contentBlockIndex];
|
|
860
|
-
const delta = (
|
|
852
|
+
const delta = (_m = contentBlockDelta.delta.toolUse.input) != null ? _m : "";
|
|
861
853
|
controller.enqueue({
|
|
862
854
|
type: "tool-call-delta",
|
|
863
855
|
toolCallType: "function",
|
|
@@ -893,9 +885,8 @@ var BedrockChatLanguageModel = class {
|
|
|
893
885
|
}
|
|
894
886
|
})
|
|
895
887
|
),
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
warnings
|
|
888
|
+
// TODO request?
|
|
889
|
+
response: { headers: responseHeaders }
|
|
899
890
|
};
|
|
900
891
|
}
|
|
901
892
|
getUrl(modelId) {
|
|
@@ -903,118 +894,134 @@ var BedrockChatLanguageModel = class {
|
|
|
903
894
|
return `${this.config.baseUrl()}/model/${encodedModelId}`;
|
|
904
895
|
}
|
|
905
896
|
};
|
|
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()
|
|
897
|
+
var BedrockStopReasonSchema = import_zod3.z.union([
|
|
898
|
+
import_zod3.z.enum(BEDROCK_STOP_REASONS),
|
|
899
|
+
import_zod3.z.string()
|
|
914
900
|
]);
|
|
915
|
-
var BedrockToolUseSchema =
|
|
916
|
-
toolUseId:
|
|
917
|
-
name:
|
|
918
|
-
input:
|
|
901
|
+
var BedrockToolUseSchema = import_zod3.z.object({
|
|
902
|
+
toolUseId: import_zod3.z.string(),
|
|
903
|
+
name: import_zod3.z.string(),
|
|
904
|
+
input: import_zod3.z.unknown()
|
|
919
905
|
});
|
|
920
|
-
var BedrockReasoningTextSchema =
|
|
921
|
-
signature:
|
|
922
|
-
text:
|
|
906
|
+
var BedrockReasoningTextSchema = import_zod3.z.object({
|
|
907
|
+
signature: import_zod3.z.string().nullish(),
|
|
908
|
+
text: import_zod3.z.string()
|
|
923
909
|
});
|
|
924
|
-
var BedrockRedactedReasoningSchema =
|
|
925
|
-
data:
|
|
910
|
+
var BedrockRedactedReasoningSchema = import_zod3.z.object({
|
|
911
|
+
data: import_zod3.z.string()
|
|
926
912
|
});
|
|
927
|
-
var BedrockResponseSchema =
|
|
928
|
-
metrics:
|
|
929
|
-
latencyMs:
|
|
913
|
+
var BedrockResponseSchema = import_zod3.z.object({
|
|
914
|
+
metrics: import_zod3.z.object({
|
|
915
|
+
latencyMs: import_zod3.z.number()
|
|
930
916
|
}).nullish(),
|
|
931
|
-
output:
|
|
932
|
-
message:
|
|
933
|
-
content:
|
|
934
|
-
|
|
935
|
-
text:
|
|
917
|
+
output: import_zod3.z.object({
|
|
918
|
+
message: import_zod3.z.object({
|
|
919
|
+
content: import_zod3.z.array(
|
|
920
|
+
import_zod3.z.object({
|
|
921
|
+
text: import_zod3.z.string().nullish(),
|
|
936
922
|
toolUse: BedrockToolUseSchema.nullish(),
|
|
937
|
-
reasoningContent:
|
|
938
|
-
|
|
923
|
+
reasoningContent: import_zod3.z.union([
|
|
924
|
+
import_zod3.z.object({
|
|
939
925
|
reasoningText: BedrockReasoningTextSchema
|
|
940
926
|
}),
|
|
941
|
-
|
|
927
|
+
import_zod3.z.object({
|
|
942
928
|
redactedReasoning: BedrockRedactedReasoningSchema
|
|
943
929
|
})
|
|
944
930
|
]).nullish()
|
|
945
931
|
})
|
|
946
932
|
),
|
|
947
|
-
role:
|
|
933
|
+
role: import_zod3.z.string()
|
|
948
934
|
})
|
|
949
935
|
}),
|
|
950
936
|
stopReason: BedrockStopReasonSchema,
|
|
951
|
-
trace:
|
|
952
|
-
usage:
|
|
953
|
-
inputTokens:
|
|
954
|
-
outputTokens:
|
|
955
|
-
totalTokens:
|
|
956
|
-
cacheReadInputTokens:
|
|
957
|
-
cacheWriteInputTokens:
|
|
937
|
+
trace: import_zod3.z.unknown().nullish(),
|
|
938
|
+
usage: import_zod3.z.object({
|
|
939
|
+
inputTokens: import_zod3.z.number(),
|
|
940
|
+
outputTokens: import_zod3.z.number(),
|
|
941
|
+
totalTokens: import_zod3.z.number(),
|
|
942
|
+
cacheReadInputTokens: import_zod3.z.number().nullish(),
|
|
943
|
+
cacheWriteInputTokens: import_zod3.z.number().nullish()
|
|
958
944
|
})
|
|
959
945
|
});
|
|
960
|
-
var BedrockStreamSchema =
|
|
961
|
-
contentBlockDelta:
|
|
962
|
-
contentBlockIndex:
|
|
963
|
-
delta:
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
reasoningContent:
|
|
946
|
+
var BedrockStreamSchema = import_zod3.z.object({
|
|
947
|
+
contentBlockDelta: import_zod3.z.object({
|
|
948
|
+
contentBlockIndex: import_zod3.z.number(),
|
|
949
|
+
delta: import_zod3.z.union([
|
|
950
|
+
import_zod3.z.object({ text: import_zod3.z.string() }),
|
|
951
|
+
import_zod3.z.object({ toolUse: import_zod3.z.object({ input: import_zod3.z.string() }) }),
|
|
952
|
+
import_zod3.z.object({
|
|
953
|
+
reasoningContent: import_zod3.z.object({ text: import_zod3.z.string() })
|
|
968
954
|
}),
|
|
969
|
-
|
|
970
|
-
reasoningContent:
|
|
971
|
-
signature:
|
|
955
|
+
import_zod3.z.object({
|
|
956
|
+
reasoningContent: import_zod3.z.object({
|
|
957
|
+
signature: import_zod3.z.string()
|
|
972
958
|
})
|
|
973
959
|
}),
|
|
974
|
-
|
|
975
|
-
reasoningContent:
|
|
960
|
+
import_zod3.z.object({
|
|
961
|
+
reasoningContent: import_zod3.z.object({ data: import_zod3.z.string() })
|
|
976
962
|
})
|
|
977
963
|
]).nullish()
|
|
978
964
|
}).nullish(),
|
|
979
|
-
contentBlockStart:
|
|
980
|
-
contentBlockIndex:
|
|
981
|
-
start:
|
|
965
|
+
contentBlockStart: import_zod3.z.object({
|
|
966
|
+
contentBlockIndex: import_zod3.z.number(),
|
|
967
|
+
start: import_zod3.z.object({
|
|
982
968
|
toolUse: BedrockToolUseSchema.nullish()
|
|
983
969
|
}).nullish()
|
|
984
970
|
}).nullish(),
|
|
985
|
-
contentBlockStop:
|
|
986
|
-
contentBlockIndex:
|
|
971
|
+
contentBlockStop: import_zod3.z.object({
|
|
972
|
+
contentBlockIndex: import_zod3.z.number()
|
|
987
973
|
}).nullish(),
|
|
988
|
-
internalServerException:
|
|
989
|
-
messageStop:
|
|
990
|
-
additionalModelResponseFields:
|
|
974
|
+
internalServerException: import_zod3.z.record(import_zod3.z.unknown()).nullish(),
|
|
975
|
+
messageStop: import_zod3.z.object({
|
|
976
|
+
additionalModelResponseFields: import_zod3.z.record(import_zod3.z.unknown()).nullish(),
|
|
991
977
|
stopReason: BedrockStopReasonSchema
|
|
992
978
|
}).nullish(),
|
|
993
|
-
metadata:
|
|
994
|
-
trace:
|
|
995
|
-
usage:
|
|
996
|
-
cacheReadInputTokens:
|
|
997
|
-
cacheWriteInputTokens:
|
|
998
|
-
inputTokens:
|
|
999
|
-
outputTokens:
|
|
979
|
+
metadata: import_zod3.z.object({
|
|
980
|
+
trace: import_zod3.z.unknown().nullish(),
|
|
981
|
+
usage: import_zod3.z.object({
|
|
982
|
+
cacheReadInputTokens: import_zod3.z.number().nullish(),
|
|
983
|
+
cacheWriteInputTokens: import_zod3.z.number().nullish(),
|
|
984
|
+
inputTokens: import_zod3.z.number(),
|
|
985
|
+
outputTokens: import_zod3.z.number()
|
|
1000
986
|
}).nullish()
|
|
1001
987
|
}).nullish(),
|
|
1002
|
-
modelStreamErrorException:
|
|
1003
|
-
throttlingException:
|
|
1004
|
-
validationException:
|
|
988
|
+
modelStreamErrorException: import_zod3.z.record(import_zod3.z.unknown()).nullish(),
|
|
989
|
+
throttlingException: import_zod3.z.record(import_zod3.z.unknown()).nullish(),
|
|
990
|
+
validationException: import_zod3.z.record(import_zod3.z.unknown()).nullish()
|
|
991
|
+
});
|
|
992
|
+
var bedrockReasoningMetadataSchema = import_zod3.z.object({
|
|
993
|
+
signature: import_zod3.z.string().optional(),
|
|
994
|
+
redactedData: import_zod3.z.string().optional()
|
|
1005
995
|
});
|
|
1006
996
|
|
|
1007
997
|
// src/bedrock-embedding-model.ts
|
|
998
|
+
var import_provider4 = require("@ai-sdk/provider");
|
|
1008
999
|
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
|
1009
|
-
|
|
1000
|
+
|
|
1001
|
+
// src/bedrock-embedding-options.ts
|
|
1002
|
+
var import_zod4 = require("zod");
|
|
1003
|
+
var bedrockEmbeddingProviderOptions = import_zod4.z.object({
|
|
1004
|
+
/**
|
|
1005
|
+
The number of dimensions the resulting output embeddings should have (defaults to 1024).
|
|
1006
|
+
Only supported in amazon.titan-embed-text-v2:0.
|
|
1007
|
+
*/
|
|
1008
|
+
dimensions: import_zod4.z.union([import_zod4.z.literal(1024), import_zod4.z.literal(512), import_zod4.z.literal(256)]).optional(),
|
|
1009
|
+
/**
|
|
1010
|
+
Flag indicating whether or not to normalize the output embeddings. Defaults to true
|
|
1011
|
+
Only supported in amazon.titan-embed-text-v2:0.
|
|
1012
|
+
*/
|
|
1013
|
+
normalize: import_zod4.z.boolean().optional()
|
|
1014
|
+
});
|
|
1015
|
+
|
|
1016
|
+
// src/bedrock-embedding-model.ts
|
|
1017
|
+
var import_zod5 = require("zod");
|
|
1010
1018
|
var BedrockEmbeddingModel = class {
|
|
1011
|
-
constructor(modelId,
|
|
1019
|
+
constructor(modelId, config) {
|
|
1012
1020
|
this.modelId = modelId;
|
|
1013
|
-
this.settings = settings;
|
|
1014
1021
|
this.config = config;
|
|
1015
|
-
this.specificationVersion = "
|
|
1022
|
+
this.specificationVersion = "v2";
|
|
1016
1023
|
this.provider = "amazon-bedrock";
|
|
1017
|
-
this.maxEmbeddingsPerCall =
|
|
1024
|
+
this.maxEmbeddingsPerCall = 1;
|
|
1018
1025
|
this.supportsParallelCalls = true;
|
|
1019
1026
|
}
|
|
1020
1027
|
getUrl(modelId) {
|
|
@@ -1024,50 +1031,54 @@ var BedrockEmbeddingModel = class {
|
|
|
1024
1031
|
async doEmbed({
|
|
1025
1032
|
values,
|
|
1026
1033
|
headers,
|
|
1027
|
-
abortSignal
|
|
1034
|
+
abortSignal,
|
|
1035
|
+
providerOptions
|
|
1028
1036
|
}) {
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
const { value: response } = await (0, import_provider_utils4.postJsonToApi)({
|
|
1037
|
-
url,
|
|
1038
|
-
headers: await (0, import_provider_utils4.resolve)(
|
|
1039
|
-
(0, import_provider_utils4.combineHeaders)(await (0, import_provider_utils4.resolve)(this.config.headers), headers)
|
|
1040
|
-
),
|
|
1041
|
-
body: args,
|
|
1042
|
-
failedResponseHandler: (0, import_provider_utils4.createJsonErrorResponseHandler)({
|
|
1043
|
-
errorSchema: BedrockErrorSchema,
|
|
1044
|
-
errorToMessage: (error) => `${error.type}: ${error.message}`
|
|
1045
|
-
}),
|
|
1046
|
-
successfulResponseHandler: (0, import_provider_utils4.createJsonResponseHandler)(
|
|
1047
|
-
BedrockEmbeddingResponseSchema
|
|
1048
|
-
),
|
|
1049
|
-
fetch: this.config.fetch,
|
|
1050
|
-
abortSignal
|
|
1037
|
+
var _a;
|
|
1038
|
+
if (values.length > this.maxEmbeddingsPerCall) {
|
|
1039
|
+
throw new import_provider4.TooManyEmbeddingValuesForCallError({
|
|
1040
|
+
provider: this.provider,
|
|
1041
|
+
modelId: this.modelId,
|
|
1042
|
+
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
|
1043
|
+
values
|
|
1051
1044
|
});
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1045
|
+
}
|
|
1046
|
+
const bedrockOptions = (_a = await (0, import_provider_utils4.parseProviderOptions)({
|
|
1047
|
+
provider: "bedrock",
|
|
1048
|
+
providerOptions,
|
|
1049
|
+
schema: bedrockEmbeddingProviderOptions
|
|
1050
|
+
})) != null ? _a : {};
|
|
1051
|
+
const args = {
|
|
1052
|
+
inputText: values[0],
|
|
1053
|
+
dimensions: bedrockOptions.dimensions,
|
|
1054
|
+
normalize: bedrockOptions.normalize
|
|
1055
|
+
};
|
|
1056
|
+
const url = this.getUrl(this.modelId);
|
|
1057
|
+
const { value: response } = await (0, import_provider_utils4.postJsonToApi)({
|
|
1058
|
+
url,
|
|
1059
|
+
headers: await (0, import_provider_utils4.resolve)(
|
|
1060
|
+
(0, import_provider_utils4.combineHeaders)(await (0, import_provider_utils4.resolve)(this.config.headers), headers)
|
|
1061
|
+
),
|
|
1062
|
+
body: args,
|
|
1063
|
+
failedResponseHandler: (0, import_provider_utils4.createJsonErrorResponseHandler)({
|
|
1064
|
+
errorSchema: BedrockErrorSchema,
|
|
1065
|
+
errorToMessage: (error) => `${error.type}: ${error.message}`
|
|
1066
|
+
}),
|
|
1067
|
+
successfulResponseHandler: (0, import_provider_utils4.createJsonResponseHandler)(
|
|
1068
|
+
BedrockEmbeddingResponseSchema
|
|
1069
|
+
),
|
|
1070
|
+
fetch: this.config.fetch,
|
|
1071
|
+
abortSignal
|
|
1072
|
+
});
|
|
1073
|
+
return {
|
|
1074
|
+
embeddings: [response.embedding],
|
|
1075
|
+
usage: { tokens: response.inputTextTokenCount }
|
|
1056
1076
|
};
|
|
1057
|
-
const responses = await Promise.all(values.map(embedSingleText));
|
|
1058
|
-
return responses.reduce(
|
|
1059
|
-
(accumulated, response) => {
|
|
1060
|
-
accumulated.embeddings.push(response.embedding);
|
|
1061
|
-
accumulated.usage.tokens += response.inputTextTokenCount;
|
|
1062
|
-
return accumulated;
|
|
1063
|
-
},
|
|
1064
|
-
{ embeddings: [], usage: { tokens: 0 } }
|
|
1065
|
-
);
|
|
1066
1077
|
}
|
|
1067
1078
|
};
|
|
1068
|
-
var BedrockEmbeddingResponseSchema =
|
|
1069
|
-
embedding:
|
|
1070
|
-
inputTextTokenCount:
|
|
1079
|
+
var BedrockEmbeddingResponseSchema = import_zod5.z.object({
|
|
1080
|
+
embedding: import_zod5.z.array(import_zod5.z.number()),
|
|
1081
|
+
inputTextTokenCount: import_zod5.z.number()
|
|
1071
1082
|
});
|
|
1072
1083
|
|
|
1073
1084
|
// src/bedrock-image-model.ts
|
|
@@ -1079,18 +1090,17 @@ var modelMaxImagesPerCall = {
|
|
|
1079
1090
|
};
|
|
1080
1091
|
|
|
1081
1092
|
// src/bedrock-image-model.ts
|
|
1082
|
-
var
|
|
1093
|
+
var import_zod6 = require("zod");
|
|
1083
1094
|
var BedrockImageModel = class {
|
|
1084
|
-
constructor(modelId,
|
|
1095
|
+
constructor(modelId, config) {
|
|
1085
1096
|
this.modelId = modelId;
|
|
1086
|
-
this.settings = settings;
|
|
1087
1097
|
this.config = config;
|
|
1088
|
-
this.specificationVersion = "
|
|
1098
|
+
this.specificationVersion = "v2";
|
|
1089
1099
|
this.provider = "amazon-bedrock";
|
|
1090
1100
|
}
|
|
1091
1101
|
get maxImagesPerCall() {
|
|
1092
|
-
var _a
|
|
1093
|
-
return (
|
|
1102
|
+
var _a;
|
|
1103
|
+
return (_a = modelMaxImagesPerCall[this.modelId]) != null ? _a : 1;
|
|
1094
1104
|
}
|
|
1095
1105
|
getUrl(modelId) {
|
|
1096
1106
|
const encodedModelId = encodeURIComponent(modelId);
|
|
@@ -1161,8 +1171,8 @@ var BedrockImageModel = class {
|
|
|
1161
1171
|
};
|
|
1162
1172
|
}
|
|
1163
1173
|
};
|
|
1164
|
-
var bedrockImageResponseSchema =
|
|
1165
|
-
images:
|
|
1174
|
+
var bedrockImageResponseSchema = import_zod6.z.object({
|
|
1175
|
+
images: import_zod6.z.array(import_zod6.z.string())
|
|
1166
1176
|
});
|
|
1167
1177
|
|
|
1168
1178
|
// src/headers-utils.ts
|
|
@@ -1184,11 +1194,7 @@ function extractHeaders(headers) {
|
|
|
1184
1194
|
return originalHeaders;
|
|
1185
1195
|
}
|
|
1186
1196
|
function convertHeadersToRecord(headers) {
|
|
1187
|
-
|
|
1188
|
-
headers.forEach((value, key) => {
|
|
1189
|
-
record[key.toLowerCase()] = value;
|
|
1190
|
-
});
|
|
1191
|
-
return record;
|
|
1197
|
+
return Object.fromEntries([...headers]);
|
|
1192
1198
|
}
|
|
1193
1199
|
|
|
1194
1200
|
// src/bedrock-sigv4-fetch.ts
|
|
@@ -1284,34 +1290,34 @@ function createAmazonBedrock(options = {}) {
|
|
|
1284
1290
|
})}.amazonaws.com`
|
|
1285
1291
|
)) != null ? _b : `https://bedrock-runtime.us-east-1.amazonaws.com`;
|
|
1286
1292
|
};
|
|
1287
|
-
const createChatModel = (modelId
|
|
1293
|
+
const createChatModel = (modelId) => {
|
|
1288
1294
|
var _a;
|
|
1289
|
-
return new BedrockChatLanguageModel(modelId,
|
|
1295
|
+
return new BedrockChatLanguageModel(modelId, {
|
|
1290
1296
|
baseUrl: getBaseUrl,
|
|
1291
1297
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1292
1298
|
fetch: sigv4Fetch,
|
|
1293
1299
|
generateId: import_provider_utils7.generateId
|
|
1294
1300
|
});
|
|
1295
1301
|
};
|
|
1296
|
-
const provider = function(modelId
|
|
1302
|
+
const provider = function(modelId) {
|
|
1297
1303
|
if (new.target) {
|
|
1298
1304
|
throw new Error(
|
|
1299
1305
|
"The Amazon Bedrock model function cannot be called with the new keyword."
|
|
1300
1306
|
);
|
|
1301
1307
|
}
|
|
1302
|
-
return createChatModel(modelId
|
|
1308
|
+
return createChatModel(modelId);
|
|
1303
1309
|
};
|
|
1304
|
-
const createEmbeddingModel = (modelId
|
|
1310
|
+
const createEmbeddingModel = (modelId) => {
|
|
1305
1311
|
var _a;
|
|
1306
|
-
return new BedrockEmbeddingModel(modelId,
|
|
1312
|
+
return new BedrockEmbeddingModel(modelId, {
|
|
1307
1313
|
baseUrl: getBaseUrl,
|
|
1308
1314
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1309
1315
|
fetch: sigv4Fetch
|
|
1310
1316
|
});
|
|
1311
1317
|
};
|
|
1312
|
-
const createImageModel = (modelId
|
|
1318
|
+
const createImageModel = (modelId) => {
|
|
1313
1319
|
var _a;
|
|
1314
|
-
return new BedrockImageModel(modelId,
|
|
1320
|
+
return new BedrockImageModel(modelId, {
|
|
1315
1321
|
baseUrl: getBaseUrl,
|
|
1316
1322
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1317
1323
|
fetch: sigv4Fetch
|