@ai-sdk/amazon-bedrock 3.0.0-canary.1 → 3.0.0-canary.3
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 +21 -0
- package/dist/index.js +50 -97
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +52 -103
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
|
@@ -8,8 +8,7 @@ import {
|
|
|
8
8
|
|
|
9
9
|
// src/bedrock-chat-language-model.ts
|
|
10
10
|
import {
|
|
11
|
-
InvalidArgumentError
|
|
12
|
-
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
11
|
+
InvalidArgumentError
|
|
13
12
|
} from "@ai-sdk/provider";
|
|
14
13
|
import {
|
|
15
14
|
combineHeaders,
|
|
@@ -123,9 +122,11 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
|
|
|
123
122
|
import {
|
|
124
123
|
UnsupportedFunctionalityError
|
|
125
124
|
} from "@ai-sdk/provider";
|
|
126
|
-
function prepareTools(
|
|
127
|
-
|
|
128
|
-
|
|
125
|
+
function prepareTools({
|
|
126
|
+
tools,
|
|
127
|
+
toolChoice
|
|
128
|
+
}) {
|
|
129
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
129
130
|
if (tools == null) {
|
|
130
131
|
return {
|
|
131
132
|
toolConfig: { tools: void 0, toolChoice: void 0 },
|
|
@@ -149,7 +150,6 @@ function prepareTools(mode) {
|
|
|
149
150
|
});
|
|
150
151
|
}
|
|
151
152
|
}
|
|
152
|
-
const toolChoice = mode.toolChoice;
|
|
153
153
|
if (toolChoice == null) {
|
|
154
154
|
return {
|
|
155
155
|
toolConfig: { tools: bedrockTools, toolChoice: void 0 },
|
|
@@ -184,7 +184,7 @@ function prepareTools(mode) {
|
|
|
184
184
|
default: {
|
|
185
185
|
const _exhaustiveCheck = type;
|
|
186
186
|
throw new UnsupportedFunctionalityError({
|
|
187
|
-
functionality: `
|
|
187
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
188
188
|
});
|
|
189
189
|
}
|
|
190
190
|
}
|
|
@@ -194,17 +194,14 @@ function prepareTools(mode) {
|
|
|
194
194
|
import {
|
|
195
195
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
196
196
|
} from "@ai-sdk/provider";
|
|
197
|
-
import {
|
|
198
|
-
convertUint8ArrayToBase64,
|
|
199
|
-
createIdGenerator
|
|
200
|
-
} from "@ai-sdk/provider-utils";
|
|
197
|
+
import { createIdGenerator } from "@ai-sdk/provider-utils";
|
|
201
198
|
var generateFileId = createIdGenerator({ prefix: "file", size: 16 });
|
|
202
199
|
function getCachePoint(providerMetadata) {
|
|
203
200
|
var _a;
|
|
204
201
|
return (_a = providerMetadata == null ? void 0 : providerMetadata.bedrock) == null ? void 0 : _a.cachePoint;
|
|
205
202
|
}
|
|
206
203
|
function convertToBedrockChatMessages(prompt) {
|
|
207
|
-
var _a, _b, _c, _d
|
|
204
|
+
var _a, _b, _c, _d;
|
|
208
205
|
const blocks = groupIntoBlocks(prompt);
|
|
209
206
|
let system = [];
|
|
210
207
|
const messages = [];
|
|
@@ -221,7 +218,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
221
218
|
}
|
|
222
219
|
for (const message of block.messages) {
|
|
223
220
|
system.push({ text: message.content });
|
|
224
|
-
if (getCachePoint(message.
|
|
221
|
+
if (getCachePoint(message.providerOptions)) {
|
|
225
222
|
system.push(BEDROCK_CACHE_POINT);
|
|
226
223
|
}
|
|
227
224
|
}
|
|
@@ -230,7 +227,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
230
227
|
case "user": {
|
|
231
228
|
const bedrockContent = [];
|
|
232
229
|
for (const message of block.messages) {
|
|
233
|
-
const { role, content,
|
|
230
|
+
const { role, content, providerOptions } = message;
|
|
234
231
|
switch (role) {
|
|
235
232
|
case "user": {
|
|
236
233
|
for (let j = 0; j < content.length; j++) {
|
|
@@ -242,43 +239,33 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
242
239
|
});
|
|
243
240
|
break;
|
|
244
241
|
}
|
|
245
|
-
case "image": {
|
|
246
|
-
if (part.image instanceof URL) {
|
|
247
|
-
throw new UnsupportedFunctionalityError2({
|
|
248
|
-
functionality: "Image URLs in user messages"
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
|
-
bedrockContent.push({
|
|
252
|
-
image: {
|
|
253
|
-
format: (_b = (_a = part.mimeType) == null ? void 0 : _a.split(
|
|
254
|
-
"/"
|
|
255
|
-
)) == null ? void 0 : _b[1],
|
|
256
|
-
source: {
|
|
257
|
-
bytes: convertUint8ArrayToBase64(
|
|
258
|
-
(_c = part.image) != null ? _c : part.image
|
|
259
|
-
)
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
});
|
|
263
|
-
break;
|
|
264
|
-
}
|
|
265
242
|
case "file": {
|
|
266
243
|
if (part.data instanceof URL) {
|
|
267
244
|
throw new UnsupportedFunctionalityError2({
|
|
268
|
-
functionality: "File
|
|
245
|
+
functionality: "File URL data"
|
|
269
246
|
});
|
|
270
247
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
source: {
|
|
278
|
-
bytes: part.data
|
|
248
|
+
if (part.mediaType.startsWith("image/")) {
|
|
249
|
+
const bedrockImageFormat = part.mediaType === "image/*" ? void 0 : (_b = (_a = part.mediaType) == null ? void 0 : _a.split("/")) == null ? void 0 : _b[1];
|
|
250
|
+
bedrockContent.push({
|
|
251
|
+
image: {
|
|
252
|
+
format: bedrockImageFormat,
|
|
253
|
+
source: { bytes: part.data }
|
|
279
254
|
}
|
|
280
|
-
}
|
|
281
|
-
}
|
|
255
|
+
});
|
|
256
|
+
} else {
|
|
257
|
+
bedrockContent.push({
|
|
258
|
+
document: {
|
|
259
|
+
format: (_d = (_c = part.mediaType) == null ? void 0 : _c.split(
|
|
260
|
+
"/"
|
|
261
|
+
)) == null ? void 0 : _d[1],
|
|
262
|
+
name: generateFileId(),
|
|
263
|
+
source: {
|
|
264
|
+
bytes: part.data
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
}
|
|
282
269
|
break;
|
|
283
270
|
}
|
|
284
271
|
}
|
|
@@ -295,12 +282,12 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
295
282
|
text: part2.text
|
|
296
283
|
};
|
|
297
284
|
case "image":
|
|
298
|
-
if (!part2.
|
|
285
|
+
if (!part2.mediaType) {
|
|
299
286
|
throw new Error(
|
|
300
287
|
"Image mime type is required in tool result part content"
|
|
301
288
|
);
|
|
302
289
|
}
|
|
303
|
-
const format = part2.
|
|
290
|
+
const format = part2.mediaType.split("/")[1];
|
|
304
291
|
if (!isBedrockImageFormat(format)) {
|
|
305
292
|
throw new Error(
|
|
306
293
|
`Unsupported image format: ${format}`
|
|
@@ -330,7 +317,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
330
317
|
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
331
318
|
}
|
|
332
319
|
}
|
|
333
|
-
if (getCachePoint(
|
|
320
|
+
if (getCachePoint(providerOptions)) {
|
|
334
321
|
bedrockContent.push(BEDROCK_CACHE_POINT);
|
|
335
322
|
}
|
|
336
323
|
}
|
|
@@ -404,7 +391,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
404
391
|
}
|
|
405
392
|
}
|
|
406
393
|
}
|
|
407
|
-
if (getCachePoint(message.
|
|
394
|
+
if (getCachePoint(message.providerOptions)) {
|
|
408
395
|
bedrockContent.push(BEDROCK_CACHE_POINT);
|
|
409
396
|
}
|
|
410
397
|
}
|
|
@@ -502,7 +489,6 @@ var BedrockChatLanguageModel = class {
|
|
|
502
489
|
this.supportsImageUrls = false;
|
|
503
490
|
}
|
|
504
491
|
getArgs({
|
|
505
|
-
mode,
|
|
506
492
|
prompt,
|
|
507
493
|
maxTokens,
|
|
508
494
|
temperature,
|
|
@@ -513,10 +499,11 @@ var BedrockChatLanguageModel = class {
|
|
|
513
499
|
stopSequences,
|
|
514
500
|
responseFormat,
|
|
515
501
|
seed,
|
|
516
|
-
|
|
502
|
+
tools,
|
|
503
|
+
toolChoice,
|
|
504
|
+
providerOptions
|
|
517
505
|
}) {
|
|
518
506
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
519
|
-
const type = mode.type;
|
|
520
507
|
const warnings = [];
|
|
521
508
|
if (frequencyPenalty != null) {
|
|
522
509
|
warnings.push({
|
|
@@ -551,7 +538,7 @@ var BedrockChatLanguageModel = class {
|
|
|
551
538
|
}
|
|
552
539
|
const { system, messages } = convertToBedrockChatMessages(prompt);
|
|
553
540
|
const reasoningConfigOptions = BedrockReasoningConfigOptionsSchema.safeParse(
|
|
554
|
-
(_a =
|
|
541
|
+
(_a = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _a.reasoning_config
|
|
555
542
|
);
|
|
556
543
|
if (!reasoningConfigOptions.success) {
|
|
557
544
|
throw new InvalidArgumentError({
|
|
@@ -598,58 +585,20 @@ var BedrockChatLanguageModel = class {
|
|
|
598
585
|
details: "topP is not supported when thinking is enabled"
|
|
599
586
|
});
|
|
600
587
|
}
|
|
601
|
-
const
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
588
|
+
const { toolConfig, toolWarnings } = prepareTools({ tools, toolChoice });
|
|
589
|
+
return {
|
|
590
|
+
command: {
|
|
591
|
+
system,
|
|
592
|
+
messages,
|
|
593
|
+
additionalModelRequestFields: this.settings.additionalModelRequestFields,
|
|
594
|
+
...Object.keys(inferenceConfig).length > 0 && {
|
|
595
|
+
inferenceConfig
|
|
596
|
+
},
|
|
597
|
+
...providerOptions == null ? void 0 : providerOptions.bedrock,
|
|
598
|
+
...((_g = toolConfig.tools) == null ? void 0 : _g.length) ? { toolConfig } : {}
|
|
606
599
|
},
|
|
607
|
-
|
|
608
|
-
...providerMetadata == null ? void 0 : providerMetadata.bedrock
|
|
600
|
+
warnings: [...warnings, ...toolWarnings]
|
|
609
601
|
};
|
|
610
|
-
switch (type) {
|
|
611
|
-
case "regular": {
|
|
612
|
-
const { toolConfig, toolWarnings } = prepareTools(mode);
|
|
613
|
-
return {
|
|
614
|
-
command: {
|
|
615
|
-
...baseArgs,
|
|
616
|
-
...((_g = toolConfig.tools) == null ? void 0 : _g.length) ? { toolConfig } : {}
|
|
617
|
-
},
|
|
618
|
-
warnings: [...warnings, ...toolWarnings]
|
|
619
|
-
};
|
|
620
|
-
}
|
|
621
|
-
case "object-json": {
|
|
622
|
-
throw new UnsupportedFunctionalityError3({
|
|
623
|
-
functionality: "json-mode object generation"
|
|
624
|
-
});
|
|
625
|
-
}
|
|
626
|
-
case "object-tool": {
|
|
627
|
-
return {
|
|
628
|
-
command: {
|
|
629
|
-
...baseArgs,
|
|
630
|
-
toolConfig: {
|
|
631
|
-
tools: [
|
|
632
|
-
{
|
|
633
|
-
toolSpec: {
|
|
634
|
-
name: mode.tool.name,
|
|
635
|
-
description: mode.tool.description,
|
|
636
|
-
inputSchema: {
|
|
637
|
-
json: mode.tool.parameters
|
|
638
|
-
}
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
],
|
|
642
|
-
toolChoice: { tool: { name: mode.tool.name } }
|
|
643
|
-
}
|
|
644
|
-
},
|
|
645
|
-
warnings
|
|
646
|
-
};
|
|
647
|
-
}
|
|
648
|
-
default: {
|
|
649
|
-
const _exhaustiveCheck = type;
|
|
650
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
602
|
}
|
|
654
603
|
async doGenerate(options) {
|
|
655
604
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
|