@ai-sdk/amazon-bedrock 1.1.6 → 2.0.0
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 +11 -0
- package/dist/index.d.mts +27 -5
- package/dist/index.d.ts +27 -5
- package/dist/index.js +421 -123
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +433 -119
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -26,15 +26,106 @@ __export(src_exports, {
|
|
|
26
26
|
module.exports = __toCommonJS(src_exports);
|
|
27
27
|
|
|
28
28
|
// src/bedrock-provider.ts
|
|
29
|
-
var
|
|
30
|
-
var import_client_bedrock_runtime3 = require("@aws-sdk/client-bedrock-runtime");
|
|
29
|
+
var import_provider_utils6 = require("@ai-sdk/provider-utils");
|
|
31
30
|
|
|
32
31
|
// src/bedrock-chat-language-model.ts
|
|
33
|
-
var
|
|
34
|
-
var
|
|
32
|
+
var import_provider4 = require("@ai-sdk/provider");
|
|
33
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
35
34
|
|
|
36
|
-
// src/bedrock-
|
|
35
|
+
// src/bedrock-api-types.ts
|
|
36
|
+
var BEDROCK_STOP_REASONS = [
|
|
37
|
+
"stop",
|
|
38
|
+
"stop_sequence",
|
|
39
|
+
"end_turn",
|
|
40
|
+
"length",
|
|
41
|
+
"max_tokens",
|
|
42
|
+
"content-filter",
|
|
43
|
+
"content_filtered",
|
|
44
|
+
"guardrail_intervened",
|
|
45
|
+
"tool-calls",
|
|
46
|
+
"tool_use"
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
// src/bedrock-error.ts
|
|
50
|
+
var import_zod = require("zod");
|
|
51
|
+
var BedrockErrorSchema = import_zod.z.object({
|
|
52
|
+
message: import_zod.z.string(),
|
|
53
|
+
type: import_zod.z.string().nullish()
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// src/bedrock-event-stream-response-handler.ts
|
|
37
57
|
var import_provider = require("@ai-sdk/provider");
|
|
58
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
59
|
+
var import_eventstream_codec = require("@smithy/eventstream-codec");
|
|
60
|
+
var import_util_utf8 = require("@smithy/util-utf8");
|
|
61
|
+
var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response }) => {
|
|
62
|
+
const responseHeaders = (0, import_provider_utils.extractResponseHeaders)(response);
|
|
63
|
+
if (response.body == null) {
|
|
64
|
+
throw new import_provider.EmptyResponseBodyError({});
|
|
65
|
+
}
|
|
66
|
+
const codec = new import_eventstream_codec.EventStreamCodec(import_util_utf8.toUtf8, import_util_utf8.fromUtf8);
|
|
67
|
+
let buffer = new Uint8Array(0);
|
|
68
|
+
const textDecoder = new TextDecoder();
|
|
69
|
+
return {
|
|
70
|
+
responseHeaders,
|
|
71
|
+
value: response.body.pipeThrough(
|
|
72
|
+
new TransformStream({
|
|
73
|
+
transform(chunk, controller) {
|
|
74
|
+
var _a, _b;
|
|
75
|
+
const newBuffer = new Uint8Array(buffer.length + chunk.length);
|
|
76
|
+
newBuffer.set(buffer);
|
|
77
|
+
newBuffer.set(chunk, buffer.length);
|
|
78
|
+
buffer = newBuffer;
|
|
79
|
+
while (buffer.length >= 4) {
|
|
80
|
+
const totalLength = new DataView(
|
|
81
|
+
buffer.buffer,
|
|
82
|
+
buffer.byteOffset,
|
|
83
|
+
buffer.byteLength
|
|
84
|
+
).getUint32(0, false);
|
|
85
|
+
if (buffer.length < totalLength) {
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
const subView = buffer.subarray(0, totalLength);
|
|
90
|
+
const decoded = codec.decode(subView);
|
|
91
|
+
buffer = buffer.slice(totalLength);
|
|
92
|
+
if (((_a = decoded.headers[":message-type"]) == null ? void 0 : _a.value) === "event") {
|
|
93
|
+
const data = textDecoder.decode(decoded.body);
|
|
94
|
+
const parsedDataResult = (0, import_provider_utils.safeParseJSON)({ text: data });
|
|
95
|
+
if (!parsedDataResult.success) {
|
|
96
|
+
controller.enqueue(parsedDataResult);
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
delete parsedDataResult.value.p;
|
|
100
|
+
let wrappedData = {
|
|
101
|
+
[(_b = decoded.headers[":event-type"]) == null ? void 0 : _b.value]: parsedDataResult.value
|
|
102
|
+
};
|
|
103
|
+
const validatedWrappedData = (0, import_provider_utils.safeValidateTypes)({
|
|
104
|
+
value: wrappedData,
|
|
105
|
+
schema: chunkSchema
|
|
106
|
+
});
|
|
107
|
+
if (!validatedWrappedData.success) {
|
|
108
|
+
controller.enqueue(validatedWrappedData);
|
|
109
|
+
} else {
|
|
110
|
+
controller.enqueue({
|
|
111
|
+
success: true,
|
|
112
|
+
value: validatedWrappedData.value,
|
|
113
|
+
rawValue: wrappedData
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
} catch (e) {
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
})
|
|
123
|
+
)
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// src/bedrock-prepare-tools.ts
|
|
128
|
+
var import_provider2 = require("@ai-sdk/provider");
|
|
38
129
|
function prepareTools(mode) {
|
|
39
130
|
var _a;
|
|
40
131
|
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
|
@@ -95,7 +186,7 @@ function prepareTools(mode) {
|
|
|
95
186
|
};
|
|
96
187
|
default: {
|
|
97
188
|
const _exhaustiveCheck = type;
|
|
98
|
-
throw new
|
|
189
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
99
190
|
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
|
|
100
191
|
});
|
|
101
192
|
}
|
|
@@ -103,9 +194,9 @@ function prepareTools(mode) {
|
|
|
103
194
|
}
|
|
104
195
|
|
|
105
196
|
// src/convert-to-bedrock-chat-messages.ts
|
|
106
|
-
var
|
|
107
|
-
var
|
|
108
|
-
var generateFileId = (0,
|
|
197
|
+
var import_provider3 = require("@ai-sdk/provider");
|
|
198
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
199
|
+
var generateFileId = (0, import_provider_utils2.createIdGenerator)({ prefix: "file", size: 16 });
|
|
109
200
|
function convertToBedrockChatMessages(prompt) {
|
|
110
201
|
var _a, _b, _c, _d, _e;
|
|
111
202
|
const blocks = groupIntoBlocks(prompt);
|
|
@@ -118,7 +209,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
118
209
|
switch (type) {
|
|
119
210
|
case "system": {
|
|
120
211
|
if (messages.length > 0) {
|
|
121
|
-
throw new
|
|
212
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
|
122
213
|
functionality: "Multiple system messages that are separated by user/assistant messages"
|
|
123
214
|
});
|
|
124
215
|
}
|
|
@@ -142,15 +233,19 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
142
233
|
}
|
|
143
234
|
case "image": {
|
|
144
235
|
if (part.image instanceof URL) {
|
|
145
|
-
throw new
|
|
236
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
|
146
237
|
functionality: "Image URLs in user messages"
|
|
147
238
|
});
|
|
148
239
|
}
|
|
149
240
|
bedrockContent.push({
|
|
150
241
|
image: {
|
|
151
|
-
format: (_b = (_a = part.mimeType) == null ? void 0 : _a.split(
|
|
242
|
+
format: (_b = (_a = part.mimeType) == null ? void 0 : _a.split(
|
|
243
|
+
"/"
|
|
244
|
+
)) == null ? void 0 : _b[1],
|
|
152
245
|
source: {
|
|
153
|
-
bytes: (
|
|
246
|
+
bytes: (0, import_provider_utils2.convertUint8ArrayToBase64)(
|
|
247
|
+
(_c = part.image) != null ? _c : part.image
|
|
248
|
+
)
|
|
154
249
|
}
|
|
155
250
|
}
|
|
156
251
|
});
|
|
@@ -158,7 +253,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
158
253
|
}
|
|
159
254
|
case "file": {
|
|
160
255
|
if (part.data instanceof URL) {
|
|
161
|
-
throw new
|
|
256
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
|
162
257
|
functionality: "File URLs in user messages"
|
|
163
258
|
});
|
|
164
259
|
}
|
|
@@ -169,7 +264,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
169
264
|
)) == null ? void 0 : _e[1],
|
|
170
265
|
name: generateFileId(),
|
|
171
266
|
source: {
|
|
172
|
-
bytes:
|
|
267
|
+
bytes: part.data
|
|
173
268
|
}
|
|
174
269
|
}
|
|
175
270
|
});
|
|
@@ -314,15 +409,16 @@ function mapBedrockFinishReason(finishReason) {
|
|
|
314
409
|
}
|
|
315
410
|
|
|
316
411
|
// src/bedrock-chat-language-model.ts
|
|
412
|
+
var import_zod2 = require("zod");
|
|
317
413
|
var BedrockChatLanguageModel = class {
|
|
318
414
|
constructor(modelId, settings, config) {
|
|
415
|
+
this.modelId = modelId;
|
|
416
|
+
this.settings = settings;
|
|
417
|
+
this.config = config;
|
|
319
418
|
this.specificationVersion = "v1";
|
|
320
419
|
this.provider = "amazon-bedrock";
|
|
321
420
|
this.defaultObjectGenerationMode = "tool";
|
|
322
421
|
this.supportsImageUrls = false;
|
|
323
|
-
this.modelId = modelId;
|
|
324
|
-
this.settings = settings;
|
|
325
|
-
this.config = config;
|
|
326
422
|
}
|
|
327
423
|
getArgs({
|
|
328
424
|
mode,
|
|
@@ -339,7 +435,7 @@ var BedrockChatLanguageModel = class {
|
|
|
339
435
|
providerMetadata,
|
|
340
436
|
headers
|
|
341
437
|
}) {
|
|
342
|
-
var _a
|
|
438
|
+
var _a;
|
|
343
439
|
const type = mode.type;
|
|
344
440
|
const warnings = [];
|
|
345
441
|
if (frequencyPenalty != null) {
|
|
@@ -360,12 +456,6 @@ var BedrockChatLanguageModel = class {
|
|
|
360
456
|
setting: "seed"
|
|
361
457
|
});
|
|
362
458
|
}
|
|
363
|
-
if (headers != null) {
|
|
364
|
-
warnings.push({
|
|
365
|
-
type: "unsupported-setting",
|
|
366
|
-
setting: "headers"
|
|
367
|
-
});
|
|
368
|
-
}
|
|
369
459
|
if (topK != null) {
|
|
370
460
|
warnings.push({
|
|
371
461
|
type: "unsupported-setting",
|
|
@@ -380,18 +470,20 @@ var BedrockChatLanguageModel = class {
|
|
|
380
470
|
});
|
|
381
471
|
}
|
|
382
472
|
const { system, messages } = convertToBedrockChatMessages(prompt);
|
|
473
|
+
const inferenceConfig = {
|
|
474
|
+
...maxTokens != null && { maxTokens },
|
|
475
|
+
...temperature != null && { temperature },
|
|
476
|
+
...topP != null && { topP },
|
|
477
|
+
...stopSequences != null && { stopSequences }
|
|
478
|
+
};
|
|
383
479
|
const baseArgs = {
|
|
384
|
-
modelId: this.modelId,
|
|
385
480
|
system: system ? [{ text: system }] : void 0,
|
|
386
481
|
additionalModelRequestFields: this.settings.additionalModelRequestFields,
|
|
387
|
-
inferenceConfig
|
|
388
|
-
|
|
389
|
-
temperature,
|
|
390
|
-
topP,
|
|
391
|
-
stopSequences
|
|
482
|
+
...Object.keys(inferenceConfig).length > 0 && {
|
|
483
|
+
inferenceConfig
|
|
392
484
|
},
|
|
393
485
|
messages,
|
|
394
|
-
|
|
486
|
+
...providerMetadata == null ? void 0 : providerMetadata.bedrock
|
|
395
487
|
};
|
|
396
488
|
switch (type) {
|
|
397
489
|
case "regular": {
|
|
@@ -399,13 +491,13 @@ var BedrockChatLanguageModel = class {
|
|
|
399
491
|
return {
|
|
400
492
|
command: {
|
|
401
493
|
...baseArgs,
|
|
402
|
-
...((
|
|
494
|
+
...((_a = toolConfig.tools) == null ? void 0 : _a.length) ? { toolConfig } : {}
|
|
403
495
|
},
|
|
404
496
|
warnings: [...warnings, ...toolWarnings]
|
|
405
497
|
};
|
|
406
498
|
}
|
|
407
499
|
case "object-json": {
|
|
408
|
-
throw new
|
|
500
|
+
throw new import_provider4.UnsupportedFunctionalityError({
|
|
409
501
|
functionality: "json-mode object generation"
|
|
410
502
|
});
|
|
411
503
|
}
|
|
@@ -439,11 +531,29 @@ var BedrockChatLanguageModel = class {
|
|
|
439
531
|
}
|
|
440
532
|
async doGenerate(options) {
|
|
441
533
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
442
|
-
const { command, warnings } = this.getArgs(options);
|
|
443
|
-
const
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
534
|
+
const { command: args, warnings } = this.getArgs(options);
|
|
535
|
+
const url = `${this.getUrl(this.modelId)}/converse`;
|
|
536
|
+
const { value: response, responseHeaders } = await (0, import_provider_utils3.postJsonToApi)({
|
|
537
|
+
url,
|
|
538
|
+
headers: (0, import_provider_utils3.combineHeaders)(
|
|
539
|
+
await (0, import_provider_utils3.resolve)(this.config.headers),
|
|
540
|
+
options.headers
|
|
541
|
+
),
|
|
542
|
+
body: args,
|
|
543
|
+
failedResponseHandler: (0, import_provider_utils3.createJsonErrorResponseHandler)({
|
|
544
|
+
errorSchema: BedrockErrorSchema,
|
|
545
|
+
errorToMessage: (error) => {
|
|
546
|
+
var _a2;
|
|
547
|
+
return `${(_a2 = error.message) != null ? _a2 : "Unknown error"}`;
|
|
548
|
+
}
|
|
549
|
+
}),
|
|
550
|
+
successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
|
|
551
|
+
BedrockResponseSchema
|
|
552
|
+
),
|
|
553
|
+
abortSignal: options.abortSignal,
|
|
554
|
+
fetch: this.config.fetch
|
|
555
|
+
});
|
|
556
|
+
const { messages: rawPrompt, ...rawSettings } = args;
|
|
447
557
|
const providerMetadata = response.trace ? { bedrock: { trace: response.trace } } : void 0;
|
|
448
558
|
return {
|
|
449
559
|
text: (_d = (_c = (_b = (_a = response.output) == null ? void 0 : _a.message) == null ? void 0 : _b.content) == null ? void 0 : _c.map((part) => {
|
|
@@ -459,48 +569,53 @@ var BedrockChatLanguageModel = class {
|
|
|
459
569
|
args: JSON.stringify((_f2 = (_e2 = part.toolUse) == null ? void 0 : _e2.input) != null ? _f2 : "")
|
|
460
570
|
};
|
|
461
571
|
}),
|
|
462
|
-
finishReason: mapBedrockFinishReason(
|
|
572
|
+
finishReason: mapBedrockFinishReason(
|
|
573
|
+
response.stopReason
|
|
574
|
+
),
|
|
463
575
|
usage: {
|
|
464
576
|
promptTokens: (_j = (_i = response.usage) == null ? void 0 : _i.inputTokens) != null ? _j : Number.NaN,
|
|
465
577
|
completionTokens: (_l = (_k = response.usage) == null ? void 0 : _k.outputTokens) != null ? _l : Number.NaN
|
|
466
578
|
},
|
|
467
579
|
rawCall: { rawPrompt, rawSettings },
|
|
580
|
+
rawResponse: { headers: responseHeaders },
|
|
468
581
|
warnings,
|
|
469
|
-
providerMetadata
|
|
582
|
+
...providerMetadata && { providerMetadata }
|
|
470
583
|
};
|
|
471
584
|
}
|
|
472
585
|
async doStream(options) {
|
|
473
|
-
const { command, warnings } = this.getArgs(options);
|
|
474
|
-
const
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
586
|
+
const { command: args, warnings } = this.getArgs(options);
|
|
587
|
+
const url = `${this.getUrl(this.modelId)}/converse-stream`;
|
|
588
|
+
const { value: response, responseHeaders } = await (0, import_provider_utils3.postJsonToApi)({
|
|
589
|
+
url,
|
|
590
|
+
headers: (0, import_provider_utils3.combineHeaders)(
|
|
591
|
+
await (0, import_provider_utils3.resolve)(this.config.headers),
|
|
592
|
+
options.headers
|
|
593
|
+
),
|
|
594
|
+
body: args,
|
|
595
|
+
failedResponseHandler: (0, import_provider_utils3.createJsonErrorResponseHandler)({
|
|
596
|
+
errorSchema: BedrockErrorSchema,
|
|
597
|
+
errorToMessage: (error) => `${error.type}: ${error.message}`
|
|
598
|
+
}),
|
|
599
|
+
successfulResponseHandler: createBedrockEventStreamResponseHandler(BedrockStreamSchema),
|
|
600
|
+
abortSignal: options.abortSignal,
|
|
601
|
+
fetch: this.config.fetch
|
|
602
|
+
});
|
|
603
|
+
const { messages: rawPrompt, ...rawSettings } = args;
|
|
478
604
|
let finishReason = "unknown";
|
|
479
605
|
let usage = {
|
|
480
606
|
promptTokens: Number.NaN,
|
|
481
607
|
completionTokens: Number.NaN
|
|
482
608
|
};
|
|
483
609
|
let providerMetadata = void 0;
|
|
484
|
-
if (!response.stream) {
|
|
485
|
-
throw new Error("No stream found");
|
|
486
|
-
}
|
|
487
|
-
const stream = new ReadableStream({
|
|
488
|
-
async start(controller) {
|
|
489
|
-
for await (const chunk of response.stream) {
|
|
490
|
-
controller.enqueue({ success: true, value: chunk });
|
|
491
|
-
}
|
|
492
|
-
controller.close();
|
|
493
|
-
}
|
|
494
|
-
});
|
|
495
610
|
const toolCallContentBlocks = {};
|
|
496
611
|
return {
|
|
497
|
-
stream:
|
|
612
|
+
stream: response.pipeThrough(
|
|
498
613
|
new TransformStream({
|
|
499
614
|
transform(chunk, controller) {
|
|
500
|
-
var _a, _b, _c, _d, _e, _f, _g
|
|
501
|
-
function enqueueError(
|
|
615
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
616
|
+
function enqueueError(bedrockError) {
|
|
502
617
|
finishReason = "error";
|
|
503
|
-
controller.enqueue({ type: "error", error });
|
|
618
|
+
controller.enqueue({ type: "error", error: bedrockError });
|
|
504
619
|
}
|
|
505
620
|
if (!chunk.success) {
|
|
506
621
|
enqueueError(chunk.error);
|
|
@@ -541,14 +656,14 @@ var BedrockChatLanguageModel = class {
|
|
|
541
656
|
};
|
|
542
657
|
}
|
|
543
658
|
}
|
|
544
|
-
if ((
|
|
659
|
+
if (((_e = value.contentBlockDelta) == null ? void 0 : _e.delta) && "text" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.text) {
|
|
545
660
|
controller.enqueue({
|
|
546
661
|
type: "text-delta",
|
|
547
662
|
textDelta: value.contentBlockDelta.delta.text
|
|
548
663
|
});
|
|
549
664
|
}
|
|
550
665
|
const contentBlockStart = value.contentBlockStart;
|
|
551
|
-
if (((
|
|
666
|
+
if (((_f = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _f.toolUse) != null) {
|
|
552
667
|
const toolUse = contentBlockStart.start.toolUse;
|
|
553
668
|
toolCallContentBlocks[contentBlockStart.contentBlockIndex] = {
|
|
554
669
|
toolCallId: toolUse.toolUseId,
|
|
@@ -557,9 +672,9 @@ var BedrockChatLanguageModel = class {
|
|
|
557
672
|
};
|
|
558
673
|
}
|
|
559
674
|
const contentBlockDelta = value.contentBlockDelta;
|
|
560
|
-
if ((
|
|
675
|
+
if ((contentBlockDelta == null ? void 0 : contentBlockDelta.delta) && "toolUse" in contentBlockDelta.delta && contentBlockDelta.delta.toolUse) {
|
|
561
676
|
const contentBlock = toolCallContentBlocks[contentBlockDelta.contentBlockIndex];
|
|
562
|
-
const delta = (
|
|
677
|
+
const delta = (_g = contentBlockDelta.delta.toolUse.input) != null ? _g : "";
|
|
563
678
|
controller.enqueue({
|
|
564
679
|
type: "tool-call-delta",
|
|
565
680
|
toolCallType: "function",
|
|
@@ -590,97 +705,275 @@ var BedrockChatLanguageModel = class {
|
|
|
590
705
|
type: "finish",
|
|
591
706
|
finishReason,
|
|
592
707
|
usage,
|
|
593
|
-
providerMetadata
|
|
708
|
+
...providerMetadata && { providerMetadata }
|
|
594
709
|
});
|
|
595
710
|
}
|
|
596
711
|
})
|
|
597
712
|
),
|
|
598
713
|
rawCall: { rawPrompt, rawSettings },
|
|
714
|
+
rawResponse: { headers: responseHeaders },
|
|
599
715
|
warnings
|
|
600
716
|
};
|
|
601
717
|
}
|
|
718
|
+
getUrl(modelId) {
|
|
719
|
+
const encodedModelId = encodeURIComponent(modelId);
|
|
720
|
+
return `${this.config.baseUrl()}/model/${encodedModelId}`;
|
|
721
|
+
}
|
|
602
722
|
};
|
|
723
|
+
var BedrockStopReasonSchema = import_zod2.z.union([
|
|
724
|
+
import_zod2.z.enum(BEDROCK_STOP_REASONS),
|
|
725
|
+
import_zod2.z.string()
|
|
726
|
+
]);
|
|
727
|
+
var BedrockToolUseSchema = import_zod2.z.object({
|
|
728
|
+
toolUseId: import_zod2.z.string(),
|
|
729
|
+
name: import_zod2.z.string(),
|
|
730
|
+
input: import_zod2.z.unknown()
|
|
731
|
+
});
|
|
732
|
+
var BedrockResponseSchema = import_zod2.z.object({
|
|
733
|
+
metrics: import_zod2.z.object({
|
|
734
|
+
latencyMs: import_zod2.z.number()
|
|
735
|
+
}).nullish(),
|
|
736
|
+
output: import_zod2.z.object({
|
|
737
|
+
message: import_zod2.z.object({
|
|
738
|
+
content: import_zod2.z.array(
|
|
739
|
+
import_zod2.z.object({
|
|
740
|
+
text: import_zod2.z.string().nullish(),
|
|
741
|
+
toolUse: BedrockToolUseSchema.nullish()
|
|
742
|
+
})
|
|
743
|
+
),
|
|
744
|
+
role: import_zod2.z.string()
|
|
745
|
+
})
|
|
746
|
+
}),
|
|
747
|
+
stopReason: BedrockStopReasonSchema,
|
|
748
|
+
trace: import_zod2.z.unknown().nullish(),
|
|
749
|
+
usage: import_zod2.z.object({
|
|
750
|
+
inputTokens: import_zod2.z.number(),
|
|
751
|
+
outputTokens: import_zod2.z.number(),
|
|
752
|
+
totalTokens: import_zod2.z.number()
|
|
753
|
+
})
|
|
754
|
+
});
|
|
755
|
+
var BedrockStreamSchema = import_zod2.z.object({
|
|
756
|
+
contentBlockDelta: import_zod2.z.object({
|
|
757
|
+
contentBlockIndex: import_zod2.z.number(),
|
|
758
|
+
delta: import_zod2.z.union([
|
|
759
|
+
import_zod2.z.object({ text: import_zod2.z.string() }),
|
|
760
|
+
import_zod2.z.object({ toolUse: import_zod2.z.object({ input: import_zod2.z.string() }) })
|
|
761
|
+
]).nullish()
|
|
762
|
+
}).nullish(),
|
|
763
|
+
contentBlockStart: import_zod2.z.object({
|
|
764
|
+
contentBlockIndex: import_zod2.z.number(),
|
|
765
|
+
start: import_zod2.z.object({
|
|
766
|
+
toolUse: BedrockToolUseSchema.nullish()
|
|
767
|
+
}).nullish()
|
|
768
|
+
}).nullish(),
|
|
769
|
+
contentBlockStop: import_zod2.z.object({
|
|
770
|
+
contentBlockIndex: import_zod2.z.number()
|
|
771
|
+
}).nullish(),
|
|
772
|
+
internalServerException: import_zod2.z.record(import_zod2.z.unknown()).nullish(),
|
|
773
|
+
messageStop: import_zod2.z.object({
|
|
774
|
+
additionalModelResponseFields: import_zod2.z.record(import_zod2.z.unknown()).nullish(),
|
|
775
|
+
stopReason: BedrockStopReasonSchema
|
|
776
|
+
}).nullish(),
|
|
777
|
+
metadata: import_zod2.z.object({
|
|
778
|
+
trace: import_zod2.z.unknown().nullish(),
|
|
779
|
+
usage: import_zod2.z.object({
|
|
780
|
+
inputTokens: import_zod2.z.number(),
|
|
781
|
+
outputTokens: import_zod2.z.number()
|
|
782
|
+
}).nullish()
|
|
783
|
+
}).nullish(),
|
|
784
|
+
modelStreamErrorException: import_zod2.z.record(import_zod2.z.unknown()).nullish(),
|
|
785
|
+
throttlingException: import_zod2.z.record(import_zod2.z.unknown()).nullish(),
|
|
786
|
+
validationException: import_zod2.z.record(import_zod2.z.unknown()).nullish()
|
|
787
|
+
});
|
|
603
788
|
|
|
604
789
|
// src/bedrock-embedding-model.ts
|
|
605
|
-
var
|
|
790
|
+
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
|
791
|
+
var import_zod3 = require("zod");
|
|
606
792
|
var BedrockEmbeddingModel = class {
|
|
607
793
|
constructor(modelId, settings, config) {
|
|
794
|
+
this.modelId = modelId;
|
|
795
|
+
this.settings = settings;
|
|
796
|
+
this.config = config;
|
|
608
797
|
this.specificationVersion = "v1";
|
|
609
798
|
this.provider = "amazon-bedrock";
|
|
610
799
|
this.maxEmbeddingsPerCall = void 0;
|
|
611
800
|
this.supportsParallelCalls = true;
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
801
|
+
}
|
|
802
|
+
getUrl(modelId) {
|
|
803
|
+
const encodedModelId = encodeURIComponent(modelId);
|
|
804
|
+
return `${this.config.baseUrl()}/model/${encodedModelId}/invoke`;
|
|
615
805
|
}
|
|
616
806
|
async doEmbed({
|
|
617
|
-
values
|
|
807
|
+
values,
|
|
808
|
+
headers,
|
|
809
|
+
abortSignal
|
|
618
810
|
}) {
|
|
619
|
-
const
|
|
620
|
-
const
|
|
811
|
+
const embedSingleText = async (inputText) => {
|
|
812
|
+
const args = {
|
|
621
813
|
inputText,
|
|
622
814
|
dimensions: this.settings.dimensions,
|
|
623
815
|
normalize: this.settings.normalize
|
|
624
816
|
};
|
|
625
|
-
const
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
817
|
+
const url = this.getUrl(this.modelId);
|
|
818
|
+
const { value: response } = await (0, import_provider_utils4.postJsonToApi)({
|
|
819
|
+
url,
|
|
820
|
+
headers: await (0, import_provider_utils4.resolve)(
|
|
821
|
+
(0, import_provider_utils4.combineHeaders)(await (0, import_provider_utils4.resolve)(this.config.headers), headers)
|
|
822
|
+
),
|
|
823
|
+
body: args,
|
|
824
|
+
failedResponseHandler: (0, import_provider_utils4.createJsonErrorResponseHandler)({
|
|
825
|
+
errorSchema: BedrockErrorSchema,
|
|
826
|
+
errorToMessage: (error) => `${error.type}: ${error.message}`
|
|
827
|
+
}),
|
|
828
|
+
successfulResponseHandler: (0, import_provider_utils4.createJsonResponseHandler)(
|
|
829
|
+
BedrockEmbeddingResponseSchema
|
|
830
|
+
),
|
|
831
|
+
fetch: this.config.fetch,
|
|
832
|
+
abortSignal
|
|
629
833
|
});
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
834
|
+
return {
|
|
835
|
+
embedding: response.embedding,
|
|
836
|
+
inputTextTokenCount: response.inputTextTokenCount
|
|
837
|
+
};
|
|
633
838
|
};
|
|
634
|
-
const responses = await Promise.all(values.map(
|
|
635
|
-
|
|
636
|
-
(
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
return
|
|
839
|
+
const responses = await Promise.all(values.map(embedSingleText));
|
|
840
|
+
return responses.reduce(
|
|
841
|
+
(accumulated, response) => {
|
|
842
|
+
accumulated.embeddings.push(response.embedding);
|
|
843
|
+
accumulated.usage.tokens += response.inputTextTokenCount;
|
|
844
|
+
return accumulated;
|
|
640
845
|
},
|
|
641
846
|
{ embeddings: [], usage: { tokens: 0 } }
|
|
642
847
|
);
|
|
643
|
-
return response;
|
|
644
848
|
}
|
|
645
849
|
};
|
|
850
|
+
var BedrockEmbeddingResponseSchema = import_zod3.z.object({
|
|
851
|
+
embedding: import_zod3.z.array(import_zod3.z.number()),
|
|
852
|
+
inputTextTokenCount: import_zod3.z.number()
|
|
853
|
+
});
|
|
854
|
+
|
|
855
|
+
// src/headers-utils.ts
|
|
856
|
+
function extractHeaders(headers) {
|
|
857
|
+
let originalHeaders = {};
|
|
858
|
+
if (headers) {
|
|
859
|
+
if (headers instanceof Headers) {
|
|
860
|
+
originalHeaders = convertHeadersToRecord(headers);
|
|
861
|
+
} else if (Array.isArray(headers)) {
|
|
862
|
+
for (const [k, v] of headers) {
|
|
863
|
+
originalHeaders[k.toLowerCase()] = v;
|
|
864
|
+
}
|
|
865
|
+
} else {
|
|
866
|
+
originalHeaders = Object.fromEntries(
|
|
867
|
+
Object.entries(headers).map(([k, v]) => [k.toLowerCase(), v])
|
|
868
|
+
);
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
return originalHeaders;
|
|
872
|
+
}
|
|
873
|
+
function convertHeadersToRecord(headers) {
|
|
874
|
+
const record = {};
|
|
875
|
+
headers.forEach((value, key) => {
|
|
876
|
+
record[key.toLowerCase()] = value;
|
|
877
|
+
});
|
|
878
|
+
return record;
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
// src/bedrock-sigv4-fetch.ts
|
|
882
|
+
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
|
883
|
+
var import_aws4fetch = require("aws4fetch");
|
|
884
|
+
function createSigV4FetchFunction(getCredentials, fetch = globalThis.fetch) {
|
|
885
|
+
return async (input, init) => {
|
|
886
|
+
var _a;
|
|
887
|
+
if (((_a = init == null ? void 0 : init.method) == null ? void 0 : _a.toUpperCase()) !== "POST" || !(init == null ? void 0 : init.body)) {
|
|
888
|
+
return fetch(input, init);
|
|
889
|
+
}
|
|
890
|
+
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
|
|
891
|
+
const originalHeaders = extractHeaders(init.headers);
|
|
892
|
+
const body = prepareBodyString(init.body);
|
|
893
|
+
const credentials = getCredentials();
|
|
894
|
+
const signer = new import_aws4fetch.AwsV4Signer({
|
|
895
|
+
url,
|
|
896
|
+
method: "POST",
|
|
897
|
+
headers: Object.entries((0, import_provider_utils5.removeUndefinedEntries)(originalHeaders)),
|
|
898
|
+
body,
|
|
899
|
+
region: credentials.region,
|
|
900
|
+
accessKeyId: credentials.accessKeyId,
|
|
901
|
+
secretAccessKey: credentials.secretAccessKey,
|
|
902
|
+
sessionToken: credentials.sessionToken,
|
|
903
|
+
service: "bedrock"
|
|
904
|
+
});
|
|
905
|
+
const signingResult = await signer.sign();
|
|
906
|
+
const signedHeaders = convertHeadersToRecord(signingResult.headers);
|
|
907
|
+
return fetch(input, {
|
|
908
|
+
...init,
|
|
909
|
+
body,
|
|
910
|
+
headers: (0, import_provider_utils5.removeUndefinedEntries)(
|
|
911
|
+
(0, import_provider_utils5.combineHeaders)(originalHeaders, signedHeaders)
|
|
912
|
+
)
|
|
913
|
+
});
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
function prepareBodyString(body) {
|
|
917
|
+
if (typeof body === "string") {
|
|
918
|
+
return body;
|
|
919
|
+
} else if (body instanceof Uint8Array) {
|
|
920
|
+
return new TextDecoder().decode(body);
|
|
921
|
+
} else if (body instanceof ArrayBuffer) {
|
|
922
|
+
return new TextDecoder().decode(new Uint8Array(body));
|
|
923
|
+
} else {
|
|
924
|
+
return JSON.stringify(body);
|
|
925
|
+
}
|
|
926
|
+
}
|
|
646
927
|
|
|
647
928
|
// src/bedrock-provider.ts
|
|
648
929
|
function createAmazonBedrock(options = {}) {
|
|
649
|
-
const
|
|
930
|
+
const sigv4Fetch = createSigV4FetchFunction(
|
|
931
|
+
() => ({
|
|
932
|
+
region: (0, import_provider_utils6.loadSetting)({
|
|
933
|
+
settingValue: options.region,
|
|
934
|
+
settingName: "region",
|
|
935
|
+
environmentVariableName: "AWS_REGION",
|
|
936
|
+
description: "AWS region"
|
|
937
|
+
}),
|
|
938
|
+
accessKeyId: (0, import_provider_utils6.loadSetting)({
|
|
939
|
+
settingValue: options.accessKeyId,
|
|
940
|
+
settingName: "accessKeyId",
|
|
941
|
+
environmentVariableName: "AWS_ACCESS_KEY_ID",
|
|
942
|
+
description: "AWS access key ID"
|
|
943
|
+
}),
|
|
944
|
+
secretAccessKey: (0, import_provider_utils6.loadSetting)({
|
|
945
|
+
settingValue: options.secretAccessKey,
|
|
946
|
+
settingName: "secretAccessKey",
|
|
947
|
+
environmentVariableName: "AWS_SECRET_ACCESS_KEY",
|
|
948
|
+
description: "AWS secret access key"
|
|
949
|
+
}),
|
|
950
|
+
sessionToken: (0, import_provider_utils6.loadOptionalSetting)({
|
|
951
|
+
settingValue: options.sessionToken,
|
|
952
|
+
environmentVariableName: "AWS_SESSION_TOKEN"
|
|
953
|
+
})
|
|
954
|
+
}),
|
|
955
|
+
options.fetch
|
|
956
|
+
);
|
|
957
|
+
const getBaseUrl = () => {
|
|
958
|
+
var _a, _b;
|
|
959
|
+
return (_b = (0, import_provider_utils6.withoutTrailingSlash)(
|
|
960
|
+
(_a = options.baseURL) != null ? _a : `https://bedrock-runtime.${(0, import_provider_utils6.loadSetting)({
|
|
961
|
+
settingValue: options.region,
|
|
962
|
+
settingName: "region",
|
|
963
|
+
environmentVariableName: "AWS_REGION",
|
|
964
|
+
description: "AWS region"
|
|
965
|
+
})}.amazonaws.com`
|
|
966
|
+
)) != null ? _b : `https://bedrock-runtime.us-east-1.amazonaws.com`;
|
|
967
|
+
};
|
|
968
|
+
const createChatModel = (modelId, settings = {}) => {
|
|
650
969
|
var _a;
|
|
651
|
-
return new
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
description: "AWS region"
|
|
658
|
-
}),
|
|
659
|
-
credentials: {
|
|
660
|
-
accessKeyId: (0, import_provider_utils2.loadSetting)({
|
|
661
|
-
settingValue: options.accessKeyId,
|
|
662
|
-
settingName: "accessKeyId",
|
|
663
|
-
environmentVariableName: "AWS_ACCESS_KEY_ID",
|
|
664
|
-
description: "AWS access key ID"
|
|
665
|
-
}),
|
|
666
|
-
secretAccessKey: (0, import_provider_utils2.loadSetting)({
|
|
667
|
-
settingValue: options.secretAccessKey,
|
|
668
|
-
settingName: "secretAccessKey",
|
|
669
|
-
environmentVariableName: "AWS_SECRET_ACCESS_KEY",
|
|
670
|
-
description: "AWS secret access key"
|
|
671
|
-
}),
|
|
672
|
-
sessionToken: (0, import_provider_utils2.loadOptionalSetting)({
|
|
673
|
-
settingValue: options.sessionToken,
|
|
674
|
-
environmentVariableName: "AWS_SESSION_TOKEN"
|
|
675
|
-
})
|
|
676
|
-
}
|
|
677
|
-
}
|
|
678
|
-
);
|
|
970
|
+
return new BedrockChatLanguageModel(modelId, settings, {
|
|
971
|
+
baseUrl: getBaseUrl,
|
|
972
|
+
headers: (_a = options.headers) != null ? _a : {},
|
|
973
|
+
fetch: sigv4Fetch,
|
|
974
|
+
generateId: import_provider_utils6.generateId
|
|
975
|
+
});
|
|
679
976
|
};
|
|
680
|
-
const createChatModel = (modelId, settings = {}) => new BedrockChatLanguageModel(modelId, settings, {
|
|
681
|
-
client: createBedrockRuntimeClient(),
|
|
682
|
-
generateId: import_provider_utils2.generateId
|
|
683
|
-
});
|
|
684
977
|
const provider = function(modelId, settings) {
|
|
685
978
|
if (new.target) {
|
|
686
979
|
throw new Error(
|
|
@@ -689,9 +982,14 @@ function createAmazonBedrock(options = {}) {
|
|
|
689
982
|
}
|
|
690
983
|
return createChatModel(modelId, settings);
|
|
691
984
|
};
|
|
692
|
-
const createEmbeddingModel = (modelId, settings = {}) =>
|
|
693
|
-
|
|
694
|
-
|
|
985
|
+
const createEmbeddingModel = (modelId, settings = {}) => {
|
|
986
|
+
var _a;
|
|
987
|
+
return new BedrockEmbeddingModel(modelId, settings, {
|
|
988
|
+
baseUrl: getBaseUrl,
|
|
989
|
+
headers: (_a = options.headers) != null ? _a : {},
|
|
990
|
+
fetch: sigv4Fetch
|
|
991
|
+
});
|
|
992
|
+
};
|
|
695
993
|
provider.languageModel = createChatModel;
|
|
696
994
|
provider.embedding = createEmbeddingModel;
|
|
697
995
|
provider.textEmbedding = createEmbeddingModel;
|