@ai-sdk/amazon-bedrock 3.0.0-alpha.9 → 3.0.0-beta.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 +142 -0
- package/README.md +75 -0
- package/dist/index.d.mts +28 -27
- package/dist/index.d.ts +28 -27
- package/dist/index.js +379 -204
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +305 -134
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.mjs
CHANGED
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
postJsonToApi,
|
|
16
16
|
resolve
|
|
17
17
|
} from "@ai-sdk/provider-utils";
|
|
18
|
-
import { z as z3 } from "zod";
|
|
18
|
+
import { z as z3 } from "zod/v4";
|
|
19
19
|
|
|
20
20
|
// src/bedrock-api-types.ts
|
|
21
21
|
var BEDROCK_CACHE_POINT = {
|
|
@@ -33,16 +33,33 @@ var BEDROCK_STOP_REASONS = [
|
|
|
33
33
|
"tool-calls",
|
|
34
34
|
"tool_use"
|
|
35
35
|
];
|
|
36
|
+
var BEDROCK_IMAGE_MIME_TYPES = {
|
|
37
|
+
"image/jpeg": "jpeg",
|
|
38
|
+
"image/png": "png",
|
|
39
|
+
"image/gif": "gif",
|
|
40
|
+
"image/webp": "webp"
|
|
41
|
+
};
|
|
42
|
+
var BEDROCK_DOCUMENT_MIME_TYPES = {
|
|
43
|
+
"application/pdf": "pdf",
|
|
44
|
+
"text/csv": "csv",
|
|
45
|
+
"application/msword": "doc",
|
|
46
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
|
|
47
|
+
"application/vnd.ms-excel": "xls",
|
|
48
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
|
|
49
|
+
"text/html": "html",
|
|
50
|
+
"text/plain": "txt",
|
|
51
|
+
"text/markdown": "md"
|
|
52
|
+
};
|
|
36
53
|
|
|
37
54
|
// src/bedrock-chat-options.ts
|
|
38
|
-
import { z } from "zod";
|
|
55
|
+
import { z } from "zod/v4";
|
|
39
56
|
var bedrockProviderOptions = z.object({
|
|
40
57
|
/**
|
|
41
58
|
* Additional inference parameters that the model supports,
|
|
42
59
|
* beyond the base set of inference parameters that Converse
|
|
43
60
|
* supports in the inferenceConfig field
|
|
44
61
|
*/
|
|
45
|
-
additionalModelRequestFields: z.record(z.any()).optional(),
|
|
62
|
+
additionalModelRequestFields: z.record(z.string(), z.any()).optional(),
|
|
46
63
|
reasoningConfig: z.object({
|
|
47
64
|
type: z.union([z.literal("enabled"), z.literal("disabled")]).optional(),
|
|
48
65
|
budgetTokens: z.number().optional()
|
|
@@ -50,7 +67,7 @@ var bedrockProviderOptions = z.object({
|
|
|
50
67
|
});
|
|
51
68
|
|
|
52
69
|
// src/bedrock-error.ts
|
|
53
|
-
import { z as z2 } from "zod";
|
|
70
|
+
import { z as z2 } from "zod/v4";
|
|
54
71
|
var BedrockErrorSchema = z2.object({
|
|
55
72
|
message: z2.string(),
|
|
56
73
|
type: z2.string().nullish()
|
|
@@ -135,14 +152,29 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
|
|
|
135
152
|
import {
|
|
136
153
|
UnsupportedFunctionalityError
|
|
137
154
|
} from "@ai-sdk/provider";
|
|
155
|
+
function promptContainsToolContent(prompt) {
|
|
156
|
+
return prompt.some((message) => {
|
|
157
|
+
if ("content" in message && Array.isArray(message.content)) {
|
|
158
|
+
return message.content.some(
|
|
159
|
+
(part) => part.type === "tool-call" || part.type === "tool-result"
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
return false;
|
|
163
|
+
});
|
|
164
|
+
}
|
|
138
165
|
function prepareTools({
|
|
139
166
|
tools,
|
|
140
|
-
toolChoice
|
|
167
|
+
toolChoice,
|
|
168
|
+
prompt
|
|
141
169
|
}) {
|
|
142
170
|
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
171
|
+
const hasToolContent = promptContainsToolContent(prompt);
|
|
143
172
|
if (tools == null) {
|
|
144
173
|
return {
|
|
145
|
-
toolConfig: {
|
|
174
|
+
toolConfig: {
|
|
175
|
+
tools: hasToolContent ? [] : void 0,
|
|
176
|
+
toolChoice: void 0
|
|
177
|
+
},
|
|
146
178
|
toolWarnings: []
|
|
147
179
|
};
|
|
148
180
|
}
|
|
@@ -157,7 +189,7 @@ function prepareTools({
|
|
|
157
189
|
name: tool.name,
|
|
158
190
|
description: tool.description,
|
|
159
191
|
inputSchema: {
|
|
160
|
-
json: tool.
|
|
192
|
+
json: tool.inputSchema
|
|
161
193
|
}
|
|
162
194
|
}
|
|
163
195
|
});
|
|
@@ -183,7 +215,10 @@ function prepareTools({
|
|
|
183
215
|
};
|
|
184
216
|
case "none":
|
|
185
217
|
return {
|
|
186
|
-
toolConfig: {
|
|
218
|
+
toolConfig: {
|
|
219
|
+
tools: hasToolContent ? [] : void 0,
|
|
220
|
+
toolChoice: void 0
|
|
221
|
+
},
|
|
187
222
|
toolWarnings
|
|
188
223
|
};
|
|
189
224
|
case "tool":
|
|
@@ -207,21 +242,17 @@ function prepareTools({
|
|
|
207
242
|
import {
|
|
208
243
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
209
244
|
} from "@ai-sdk/provider";
|
|
210
|
-
import {
|
|
211
|
-
convertToBase64,
|
|
212
|
-
createIdGenerator,
|
|
213
|
-
parseProviderOptions
|
|
214
|
-
} from "@ai-sdk/provider-utils";
|
|
215
|
-
var generateFileId = createIdGenerator({ prefix: "file", size: 16 });
|
|
245
|
+
import { convertToBase64, parseProviderOptions } from "@ai-sdk/provider-utils";
|
|
216
246
|
function getCachePoint(providerMetadata) {
|
|
217
247
|
var _a;
|
|
218
248
|
return (_a = providerMetadata == null ? void 0 : providerMetadata.bedrock) == null ? void 0 : _a.cachePoint;
|
|
219
249
|
}
|
|
220
250
|
async function convertToBedrockChatMessages(prompt) {
|
|
221
|
-
var _a, _b, _c, _d;
|
|
222
251
|
const blocks = groupIntoBlocks(prompt);
|
|
223
252
|
let system = [];
|
|
224
253
|
const messages = [];
|
|
254
|
+
let documentCounter = 0;
|
|
255
|
+
const generateDocumentName = () => `document-${++documentCounter}`;
|
|
225
256
|
for (let i = 0; i < blocks.length; i++) {
|
|
226
257
|
const block = blocks[i];
|
|
227
258
|
const isLastBlock = i === blocks.length - 1;
|
|
@@ -263,20 +294,23 @@ async function convertToBedrockChatMessages(prompt) {
|
|
|
263
294
|
});
|
|
264
295
|
}
|
|
265
296
|
if (part.mediaType.startsWith("image/")) {
|
|
266
|
-
const bedrockImageFormat = part.mediaType === "image/*" ? void 0 : (_b = (_a = part.mediaType) == null ? void 0 : _a.split("/")) == null ? void 0 : _b[1];
|
|
267
297
|
bedrockContent.push({
|
|
268
298
|
image: {
|
|
269
|
-
format:
|
|
299
|
+
format: getBedrockImageFormat(part.mediaType),
|
|
270
300
|
source: { bytes: convertToBase64(part.data) }
|
|
271
301
|
}
|
|
272
302
|
});
|
|
273
303
|
} else {
|
|
304
|
+
if (!part.mediaType) {
|
|
305
|
+
throw new UnsupportedFunctionalityError2({
|
|
306
|
+
functionality: "file without mime type",
|
|
307
|
+
message: "File mime type is required in user message part content"
|
|
308
|
+
});
|
|
309
|
+
}
|
|
274
310
|
bedrockContent.push({
|
|
275
311
|
document: {
|
|
276
|
-
format: (
|
|
277
|
-
|
|
278
|
-
)) == null ? void 0 : _d[1],
|
|
279
|
-
name: generateFileId(),
|
|
312
|
+
format: getBedrockDocumentFormat(part.mediaType),
|
|
313
|
+
name: generateDocumentName(),
|
|
280
314
|
source: { bytes: convertToBase64(part.data) }
|
|
281
315
|
}
|
|
282
316
|
});
|
|
@@ -288,36 +322,46 @@ async function convertToBedrockChatMessages(prompt) {
|
|
|
288
322
|
break;
|
|
289
323
|
}
|
|
290
324
|
case "tool": {
|
|
291
|
-
for (
|
|
292
|
-
|
|
293
|
-
const
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
const format = part2.mediaType.split("/")[1];
|
|
306
|
-
if (!isBedrockImageFormat(format)) {
|
|
307
|
-
throw new Error(
|
|
308
|
-
`Unsupported image format: ${format}`
|
|
309
|
-
);
|
|
310
|
-
}
|
|
311
|
-
return {
|
|
312
|
-
image: {
|
|
313
|
-
format,
|
|
314
|
-
source: {
|
|
315
|
-
bytes: part2.data
|
|
325
|
+
for (const part of content) {
|
|
326
|
+
let toolResultContent;
|
|
327
|
+
const output = part.output;
|
|
328
|
+
switch (output.type) {
|
|
329
|
+
case "content": {
|
|
330
|
+
toolResultContent = output.value.map((contentPart) => {
|
|
331
|
+
switch (contentPart.type) {
|
|
332
|
+
case "text":
|
|
333
|
+
return { text: contentPart.text };
|
|
334
|
+
case "media":
|
|
335
|
+
if (!contentPart.mediaType.startsWith("image/")) {
|
|
336
|
+
throw new UnsupportedFunctionalityError2({
|
|
337
|
+
functionality: `media type: ${contentPart.mediaType}`
|
|
338
|
+
});
|
|
316
339
|
}
|
|
317
|
-
|
|
318
|
-
|
|
340
|
+
const format = getBedrockImageFormat(
|
|
341
|
+
contentPart.mediaType
|
|
342
|
+
);
|
|
343
|
+
return {
|
|
344
|
+
image: {
|
|
345
|
+
format,
|
|
346
|
+
source: { bytes: contentPart.data }
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
break;
|
|
319
352
|
}
|
|
320
|
-
|
|
353
|
+
case "text":
|
|
354
|
+
case "error-text":
|
|
355
|
+
toolResultContent = [{ text: output.value }];
|
|
356
|
+
break;
|
|
357
|
+
case "json":
|
|
358
|
+
case "error-json":
|
|
359
|
+
default:
|
|
360
|
+
toolResultContent = [
|
|
361
|
+
{ text: JSON.stringify(output.value) }
|
|
362
|
+
];
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
321
365
|
bedrockContent.push({
|
|
322
366
|
toolResult: {
|
|
323
367
|
toolUseId: part.toolCallId,
|
|
@@ -406,7 +450,7 @@ async function convertToBedrockChatMessages(prompt) {
|
|
|
406
450
|
toolUse: {
|
|
407
451
|
toolUseId: part.toolCallId,
|
|
408
452
|
name: part.toolName,
|
|
409
|
-
input: part.
|
|
453
|
+
input: part.input
|
|
410
454
|
}
|
|
411
455
|
});
|
|
412
456
|
break;
|
|
@@ -428,8 +472,31 @@ async function convertToBedrockChatMessages(prompt) {
|
|
|
428
472
|
}
|
|
429
473
|
return { system, messages };
|
|
430
474
|
}
|
|
431
|
-
function
|
|
432
|
-
|
|
475
|
+
function getBedrockImageFormat(mimeType) {
|
|
476
|
+
if (!mimeType) {
|
|
477
|
+
throw new UnsupportedFunctionalityError2({
|
|
478
|
+
functionality: "image without mime type",
|
|
479
|
+
message: "Image mime type is required in user message part content"
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
const format = BEDROCK_IMAGE_MIME_TYPES[mimeType];
|
|
483
|
+
if (!format) {
|
|
484
|
+
throw new UnsupportedFunctionalityError2({
|
|
485
|
+
functionality: `image mime type: ${mimeType}`,
|
|
486
|
+
message: `Unsupported image mime type: ${mimeType}, expected one of: ${Object.keys(BEDROCK_IMAGE_MIME_TYPES).join(", ")}`
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
return format;
|
|
490
|
+
}
|
|
491
|
+
function getBedrockDocumentFormat(mimeType) {
|
|
492
|
+
const format = BEDROCK_DOCUMENT_MIME_TYPES[mimeType];
|
|
493
|
+
if (!format) {
|
|
494
|
+
throw new UnsupportedFunctionalityError2({
|
|
495
|
+
functionality: `file mime type: ${mimeType}`,
|
|
496
|
+
message: `Unsupported file mime type: ${mimeType}, expected one of: ${Object.keys(BEDROCK_DOCUMENT_MIME_TYPES).join(", ")}`
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
return format;
|
|
433
500
|
}
|
|
434
501
|
function trimIfLast(isLastBlock, isLastMessage, isLastContentPart, text) {
|
|
435
502
|
return isLastBlock && isLastMessage && isLastContentPart ? text.trim() : text;
|
|
@@ -525,7 +592,7 @@ var BedrockChatLanguageModel = class {
|
|
|
525
592
|
toolChoice,
|
|
526
593
|
providerOptions
|
|
527
594
|
}) {
|
|
528
|
-
var _a, _b, _c, _d
|
|
595
|
+
var _a, _b, _c, _d;
|
|
529
596
|
const bedrockOptions = (_a = await parseProviderOptions2({
|
|
530
597
|
provider: "bedrock",
|
|
531
598
|
providerOptions,
|
|
@@ -580,7 +647,7 @@ var BedrockChatLanguageModel = class {
|
|
|
580
647
|
}
|
|
581
648
|
bedrockOptions.additionalModelRequestFields = {
|
|
582
649
|
...bedrockOptions.additionalModelRequestFields,
|
|
583
|
-
|
|
650
|
+
thinking: {
|
|
584
651
|
type: (_d = bedrockOptions.reasoningConfig) == null ? void 0 : _d.type,
|
|
585
652
|
budget_tokens: thinkingBudget
|
|
586
653
|
}
|
|
@@ -602,7 +669,12 @@ var BedrockChatLanguageModel = class {
|
|
|
602
669
|
details: "topP is not supported when thinking is enabled"
|
|
603
670
|
});
|
|
604
671
|
}
|
|
605
|
-
const { toolConfig, toolWarnings } = prepareTools({
|
|
672
|
+
const { toolConfig, toolWarnings } = prepareTools({
|
|
673
|
+
tools,
|
|
674
|
+
toolChoice,
|
|
675
|
+
prompt
|
|
676
|
+
});
|
|
677
|
+
const { reasoningConfig: _, ...filteredBedrockOptions } = (providerOptions == null ? void 0 : providerOptions.bedrock) || {};
|
|
606
678
|
return {
|
|
607
679
|
command: {
|
|
608
680
|
system,
|
|
@@ -611,8 +683,8 @@ var BedrockChatLanguageModel = class {
|
|
|
611
683
|
...Object.keys(inferenceConfig).length > 0 && {
|
|
612
684
|
inferenceConfig
|
|
613
685
|
},
|
|
614
|
-
...
|
|
615
|
-
...
|
|
686
|
+
...filteredBedrockOptions,
|
|
687
|
+
...toolConfig.tools !== void 0 ? { toolConfig } : {}
|
|
616
688
|
},
|
|
617
689
|
warnings: [...warnings, ...toolWarnings]
|
|
618
690
|
};
|
|
@@ -675,10 +747,9 @@ var BedrockChatLanguageModel = class {
|
|
|
675
747
|
if (part.toolUse) {
|
|
676
748
|
content.push({
|
|
677
749
|
type: "tool-call",
|
|
678
|
-
toolCallType: "function",
|
|
679
750
|
toolCallId: (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId(),
|
|
680
751
|
toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
|
|
681
|
-
|
|
752
|
+
input: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : "")
|
|
682
753
|
});
|
|
683
754
|
}
|
|
684
755
|
}
|
|
@@ -736,7 +807,7 @@ var BedrockChatLanguageModel = class {
|
|
|
736
807
|
totalTokens: void 0
|
|
737
808
|
};
|
|
738
809
|
let providerMetadata = void 0;
|
|
739
|
-
const
|
|
810
|
+
const contentBlocks = {};
|
|
740
811
|
return {
|
|
741
812
|
stream: response.pipeThrough(
|
|
742
813
|
new TransformStream({
|
|
@@ -744,11 +815,14 @@ var BedrockChatLanguageModel = class {
|
|
|
744
815
|
controller.enqueue({ type: "stream-start", warnings });
|
|
745
816
|
},
|
|
746
817
|
transform(chunk, controller) {
|
|
747
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
818
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
|
|
748
819
|
function enqueueError(bedrockError) {
|
|
749
820
|
finishReason = "error";
|
|
750
821
|
controller.enqueue({ type: "error", error: bedrockError });
|
|
751
822
|
}
|
|
823
|
+
if (options.includeRawChunks) {
|
|
824
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
825
|
+
}
|
|
752
826
|
if (!chunk.success) {
|
|
753
827
|
enqueueError(chunk.error);
|
|
754
828
|
return;
|
|
@@ -797,78 +871,126 @@ var BedrockChatLanguageModel = class {
|
|
|
797
871
|
};
|
|
798
872
|
}
|
|
799
873
|
}
|
|
800
|
-
if (((_j = value.
|
|
874
|
+
if (((_j = value.contentBlockStart) == null ? void 0 : _j.contentBlockIndex) != null && !((_l = (_k = value.contentBlockStart) == null ? void 0 : _k.start) == null ? void 0 : _l.toolUse)) {
|
|
875
|
+
const blockIndex = value.contentBlockStart.contentBlockIndex;
|
|
876
|
+
contentBlocks[blockIndex] = { type: "text" };
|
|
801
877
|
controller.enqueue({
|
|
802
|
-
type: "text",
|
|
803
|
-
|
|
878
|
+
type: "text-start",
|
|
879
|
+
id: String(blockIndex)
|
|
804
880
|
});
|
|
805
881
|
}
|
|
806
|
-
if (((
|
|
882
|
+
if (((_m = value.contentBlockDelta) == null ? void 0 : _m.delta) && "text" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.text) {
|
|
883
|
+
const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
|
|
884
|
+
if (contentBlocks[blockIndex] == null) {
|
|
885
|
+
contentBlocks[blockIndex] = { type: "text" };
|
|
886
|
+
controller.enqueue({
|
|
887
|
+
type: "text-start",
|
|
888
|
+
id: String(blockIndex)
|
|
889
|
+
});
|
|
890
|
+
}
|
|
891
|
+
controller.enqueue({
|
|
892
|
+
type: "text-delta",
|
|
893
|
+
id: String(blockIndex),
|
|
894
|
+
delta: value.contentBlockDelta.delta.text
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
if (((_n = value.contentBlockStop) == null ? void 0 : _n.contentBlockIndex) != null) {
|
|
898
|
+
const blockIndex = value.contentBlockStop.contentBlockIndex;
|
|
899
|
+
const contentBlock = contentBlocks[blockIndex];
|
|
900
|
+
if (contentBlock != null) {
|
|
901
|
+
if (contentBlock.type === "reasoning") {
|
|
902
|
+
controller.enqueue({
|
|
903
|
+
type: "reasoning-end",
|
|
904
|
+
id: String(blockIndex)
|
|
905
|
+
});
|
|
906
|
+
} else if (contentBlock.type === "text") {
|
|
907
|
+
controller.enqueue({
|
|
908
|
+
type: "text-end",
|
|
909
|
+
id: String(blockIndex)
|
|
910
|
+
});
|
|
911
|
+
} else if (contentBlock.type === "tool-call") {
|
|
912
|
+
controller.enqueue({
|
|
913
|
+
type: "tool-input-end",
|
|
914
|
+
id: contentBlock.toolCallId
|
|
915
|
+
});
|
|
916
|
+
controller.enqueue({
|
|
917
|
+
type: "tool-call",
|
|
918
|
+
toolCallId: contentBlock.toolCallId,
|
|
919
|
+
toolName: contentBlock.toolName,
|
|
920
|
+
input: contentBlock.jsonText
|
|
921
|
+
});
|
|
922
|
+
}
|
|
923
|
+
delete contentBlocks[blockIndex];
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
if (((_o = value.contentBlockDelta) == null ? void 0 : _o.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
|
|
927
|
+
const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
|
|
807
928
|
const reasoningContent = value.contentBlockDelta.delta.reasoningContent;
|
|
808
929
|
if ("text" in reasoningContent && reasoningContent.text) {
|
|
930
|
+
if (contentBlocks[blockIndex] == null) {
|
|
931
|
+
contentBlocks[blockIndex] = { type: "reasoning" };
|
|
932
|
+
controller.enqueue({
|
|
933
|
+
type: "reasoning-start",
|
|
934
|
+
id: String(blockIndex)
|
|
935
|
+
});
|
|
936
|
+
}
|
|
809
937
|
controller.enqueue({
|
|
810
|
-
type: "reasoning",
|
|
811
|
-
|
|
938
|
+
type: "reasoning-delta",
|
|
939
|
+
id: String(blockIndex),
|
|
940
|
+
delta: reasoningContent.text
|
|
812
941
|
});
|
|
813
942
|
} else if ("signature" in reasoningContent && reasoningContent.signature) {
|
|
814
943
|
controller.enqueue({
|
|
815
|
-
type: "reasoning",
|
|
816
|
-
|
|
944
|
+
type: "reasoning-delta",
|
|
945
|
+
id: String(blockIndex),
|
|
946
|
+
delta: "",
|
|
817
947
|
providerMetadata: {
|
|
818
948
|
bedrock: {
|
|
819
949
|
signature: reasoningContent.signature
|
|
820
950
|
}
|
|
821
951
|
}
|
|
822
952
|
});
|
|
823
|
-
controller.enqueue({ type: "reasoning-part-finish" });
|
|
824
953
|
} else if ("data" in reasoningContent && reasoningContent.data) {
|
|
825
954
|
controller.enqueue({
|
|
826
|
-
type: "reasoning",
|
|
827
|
-
|
|
955
|
+
type: "reasoning-delta",
|
|
956
|
+
id: String(blockIndex),
|
|
957
|
+
delta: "",
|
|
828
958
|
providerMetadata: {
|
|
829
959
|
bedrock: {
|
|
830
960
|
redactedData: reasoningContent.data
|
|
831
961
|
}
|
|
832
962
|
}
|
|
833
963
|
});
|
|
834
|
-
controller.enqueue({ type: "reasoning-part-finish" });
|
|
835
964
|
}
|
|
836
965
|
}
|
|
837
966
|
const contentBlockStart = value.contentBlockStart;
|
|
838
|
-
if (((
|
|
967
|
+
if (((_p = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _p.toolUse) != null) {
|
|
839
968
|
const toolUse = contentBlockStart.start.toolUse;
|
|
840
|
-
|
|
969
|
+
const blockIndex = contentBlockStart.contentBlockIndex;
|
|
970
|
+
contentBlocks[blockIndex] = {
|
|
971
|
+
type: "tool-call",
|
|
841
972
|
toolCallId: toolUse.toolUseId,
|
|
842
973
|
toolName: toolUse.name,
|
|
843
974
|
jsonText: ""
|
|
844
975
|
};
|
|
845
|
-
}
|
|
846
|
-
const contentBlockDelta = value.contentBlockDelta;
|
|
847
|
-
if ((contentBlockDelta == null ? void 0 : contentBlockDelta.delta) && "toolUse" in contentBlockDelta.delta && contentBlockDelta.delta.toolUse) {
|
|
848
|
-
const contentBlock = toolCallContentBlocks[contentBlockDelta.contentBlockIndex];
|
|
849
|
-
const delta = (_m = contentBlockDelta.delta.toolUse.input) != null ? _m : "";
|
|
850
976
|
controller.enqueue({
|
|
851
|
-
type: "tool-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
toolName: contentBlock.toolName,
|
|
855
|
-
argsTextDelta: delta
|
|
977
|
+
type: "tool-input-start",
|
|
978
|
+
id: toolUse.toolUseId,
|
|
979
|
+
toolName: toolUse.name
|
|
856
980
|
});
|
|
857
|
-
contentBlock.jsonText += delta;
|
|
858
981
|
}
|
|
859
|
-
const
|
|
860
|
-
if (
|
|
861
|
-
const
|
|
862
|
-
const contentBlock =
|
|
863
|
-
if (contentBlock
|
|
982
|
+
const contentBlockDelta = value.contentBlockDelta;
|
|
983
|
+
if ((contentBlockDelta == null ? void 0 : contentBlockDelta.delta) && "toolUse" in contentBlockDelta.delta && contentBlockDelta.delta.toolUse) {
|
|
984
|
+
const blockIndex = contentBlockDelta.contentBlockIndex;
|
|
985
|
+
const contentBlock = contentBlocks[blockIndex];
|
|
986
|
+
if ((contentBlock == null ? void 0 : contentBlock.type) === "tool-call") {
|
|
987
|
+
const delta = (_q = contentBlockDelta.delta.toolUse.input) != null ? _q : "";
|
|
864
988
|
controller.enqueue({
|
|
865
|
-
type: "tool-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
toolName: contentBlock.toolName,
|
|
869
|
-
args: contentBlock.jsonText
|
|
989
|
+
type: "tool-input-delta",
|
|
990
|
+
id: contentBlock.toolCallId,
|
|
991
|
+
delta
|
|
870
992
|
});
|
|
871
|
-
|
|
993
|
+
contentBlock.jsonText += delta;
|
|
872
994
|
}
|
|
873
995
|
}
|
|
874
996
|
},
|
|
@@ -968,9 +1090,9 @@ var BedrockStreamSchema = z3.object({
|
|
|
968
1090
|
contentBlockStop: z3.object({
|
|
969
1091
|
contentBlockIndex: z3.number()
|
|
970
1092
|
}).nullish(),
|
|
971
|
-
internalServerException: z3.record(z3.unknown()).nullish(),
|
|
1093
|
+
internalServerException: z3.record(z3.string(), z3.unknown()).nullish(),
|
|
972
1094
|
messageStop: z3.object({
|
|
973
|
-
additionalModelResponseFields: z3.record(z3.unknown()).nullish(),
|
|
1095
|
+
additionalModelResponseFields: z3.record(z3.string(), z3.unknown()).nullish(),
|
|
974
1096
|
stopReason: BedrockStopReasonSchema
|
|
975
1097
|
}).nullish(),
|
|
976
1098
|
metadata: z3.object({
|
|
@@ -982,9 +1104,9 @@ var BedrockStreamSchema = z3.object({
|
|
|
982
1104
|
outputTokens: z3.number()
|
|
983
1105
|
}).nullish()
|
|
984
1106
|
}).nullish(),
|
|
985
|
-
modelStreamErrorException: z3.record(z3.unknown()).nullish(),
|
|
986
|
-
throttlingException: z3.record(z3.unknown()).nullish(),
|
|
987
|
-
validationException: z3.record(z3.unknown()).nullish()
|
|
1107
|
+
modelStreamErrorException: z3.record(z3.string(), z3.unknown()).nullish(),
|
|
1108
|
+
throttlingException: z3.record(z3.string(), z3.unknown()).nullish(),
|
|
1109
|
+
validationException: z3.record(z3.string(), z3.unknown()).nullish()
|
|
988
1110
|
});
|
|
989
1111
|
var bedrockReasoningMetadataSchema = z3.object({
|
|
990
1112
|
signature: z3.string().optional(),
|
|
@@ -1005,7 +1127,7 @@ import {
|
|
|
1005
1127
|
} from "@ai-sdk/provider-utils";
|
|
1006
1128
|
|
|
1007
1129
|
// src/bedrock-embedding-options.ts
|
|
1008
|
-
import { z as z4 } from "zod";
|
|
1130
|
+
import { z as z4 } from "zod/v4";
|
|
1009
1131
|
var bedrockEmbeddingProviderOptions = z4.object({
|
|
1010
1132
|
/**
|
|
1011
1133
|
The number of dimensions the resulting output embeddings should have (defaults to 1024).
|
|
@@ -1020,7 +1142,7 @@ var bedrockEmbeddingProviderOptions = z4.object({
|
|
|
1020
1142
|
});
|
|
1021
1143
|
|
|
1022
1144
|
// src/bedrock-embedding-model.ts
|
|
1023
|
-
import { z as z5 } from "zod";
|
|
1145
|
+
import { z as z5 } from "zod/v4";
|
|
1024
1146
|
var BedrockEmbeddingModel = class {
|
|
1025
1147
|
constructor(modelId, config) {
|
|
1026
1148
|
this.modelId = modelId;
|
|
@@ -1102,7 +1224,7 @@ var modelMaxImagesPerCall = {
|
|
|
1102
1224
|
};
|
|
1103
1225
|
|
|
1104
1226
|
// src/bedrock-image-model.ts
|
|
1105
|
-
import { z as z6 } from "zod";
|
|
1227
|
+
import { z as z6 } from "zod/v4";
|
|
1106
1228
|
var BedrockImageModel = class {
|
|
1107
1229
|
constructor(modelId, config) {
|
|
1108
1230
|
this.modelId = modelId;
|
|
@@ -1128,7 +1250,7 @@ var BedrockImageModel = class {
|
|
|
1128
1250
|
headers,
|
|
1129
1251
|
abortSignal
|
|
1130
1252
|
}) {
|
|
1131
|
-
var _a, _b, _c, _d, _e, _f;
|
|
1253
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
1132
1254
|
const warnings = [];
|
|
1133
1255
|
const [width, height] = size ? size.split("x").map(Number) : [];
|
|
1134
1256
|
const args = {
|
|
@@ -1137,6 +1259,9 @@ var BedrockImageModel = class {
|
|
|
1137
1259
|
text: prompt,
|
|
1138
1260
|
...((_a = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _a.negativeText) ? {
|
|
1139
1261
|
negativeText: providerOptions.bedrock.negativeText
|
|
1262
|
+
} : {},
|
|
1263
|
+
...((_b = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _b.style) ? {
|
|
1264
|
+
style: providerOptions.bedrock.style
|
|
1140
1265
|
} : {}
|
|
1141
1266
|
},
|
|
1142
1267
|
imageGenerationConfig: {
|
|
@@ -1144,8 +1269,8 @@ var BedrockImageModel = class {
|
|
|
1144
1269
|
...height ? { height } : {},
|
|
1145
1270
|
...seed ? { seed } : {},
|
|
1146
1271
|
...n ? { numberOfImages: n } : {},
|
|
1147
|
-
...((
|
|
1148
|
-
...((
|
|
1272
|
+
...((_c = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _c.quality) ? { quality: providerOptions.bedrock.quality } : {},
|
|
1273
|
+
...((_d = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _d.cfgScale) ? { cfgScale: providerOptions.bedrock.cfgScale } : {}
|
|
1149
1274
|
}
|
|
1150
1275
|
};
|
|
1151
1276
|
if (aspectRatio != void 0) {
|
|
@@ -1155,7 +1280,7 @@ var BedrockImageModel = class {
|
|
|
1155
1280
|
details: "This model does not support aspect ratio. Use `size` instead."
|
|
1156
1281
|
});
|
|
1157
1282
|
}
|
|
1158
|
-
const currentDate = (
|
|
1283
|
+
const currentDate = (_g = (_f = (_e = this.config._internal) == null ? void 0 : _e.currentDate) == null ? void 0 : _f.call(_e)) != null ? _g : /* @__PURE__ */ new Date();
|
|
1159
1284
|
const { value: response, responseHeaders } = await postJsonToApi3({
|
|
1160
1285
|
url: this.getUrl(this.modelId),
|
|
1161
1286
|
headers: await resolve3(
|
|
@@ -1258,10 +1383,28 @@ function prepareBodyString(body) {
|
|
|
1258
1383
|
return JSON.stringify(body);
|
|
1259
1384
|
}
|
|
1260
1385
|
}
|
|
1386
|
+
function createApiKeyFetchFunction(apiKey, fetch = globalThis.fetch) {
|
|
1387
|
+
return async (input, init) => {
|
|
1388
|
+
const originalHeaders = extractHeaders(init == null ? void 0 : init.headers);
|
|
1389
|
+
return fetch(input, {
|
|
1390
|
+
...init,
|
|
1391
|
+
headers: removeUndefinedEntries(
|
|
1392
|
+
combineHeaders4(originalHeaders, {
|
|
1393
|
+
Authorization: `Bearer ${apiKey}`
|
|
1394
|
+
})
|
|
1395
|
+
)
|
|
1396
|
+
});
|
|
1397
|
+
};
|
|
1398
|
+
}
|
|
1261
1399
|
|
|
1262
1400
|
// src/bedrock-provider.ts
|
|
1263
1401
|
function createAmazonBedrock(options = {}) {
|
|
1264
|
-
const
|
|
1402
|
+
const rawApiKey = loadOptionalSetting({
|
|
1403
|
+
settingValue: options.apiKey,
|
|
1404
|
+
environmentVariableName: "AWS_BEARER_TOKEN_BEDROCK"
|
|
1405
|
+
});
|
|
1406
|
+
const apiKey = rawApiKey && rawApiKey.trim().length > 0 ? rawApiKey.trim() : void 0;
|
|
1407
|
+
const fetchFunction = apiKey ? createApiKeyFetchFunction(apiKey, options.fetch) : createSigV4FetchFunction(async () => {
|
|
1265
1408
|
const region = loadSetting({
|
|
1266
1409
|
settingValue: options.region,
|
|
1267
1410
|
settingName: "region",
|
|
@@ -1269,30 +1412,58 @@ function createAmazonBedrock(options = {}) {
|
|
|
1269
1412
|
description: "AWS region"
|
|
1270
1413
|
});
|
|
1271
1414
|
if (options.credentialProvider) {
|
|
1415
|
+
try {
|
|
1416
|
+
return {
|
|
1417
|
+
...await options.credentialProvider(),
|
|
1418
|
+
region
|
|
1419
|
+
};
|
|
1420
|
+
} catch (error) {
|
|
1421
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
1422
|
+
throw new Error(
|
|
1423
|
+
`AWS credential provider failed: ${errorMessage}. Please ensure your credential provider returns valid AWS credentials with accessKeyId and secretAccessKey properties.`
|
|
1424
|
+
);
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
try {
|
|
1272
1428
|
return {
|
|
1273
|
-
|
|
1274
|
-
|
|
1429
|
+
region,
|
|
1430
|
+
accessKeyId: loadSetting({
|
|
1431
|
+
settingValue: options.accessKeyId,
|
|
1432
|
+
settingName: "accessKeyId",
|
|
1433
|
+
environmentVariableName: "AWS_ACCESS_KEY_ID",
|
|
1434
|
+
description: "AWS access key ID"
|
|
1435
|
+
}),
|
|
1436
|
+
secretAccessKey: loadSetting({
|
|
1437
|
+
settingValue: options.secretAccessKey,
|
|
1438
|
+
settingName: "secretAccessKey",
|
|
1439
|
+
environmentVariableName: "AWS_SECRET_ACCESS_KEY",
|
|
1440
|
+
description: "AWS secret access key"
|
|
1441
|
+
}),
|
|
1442
|
+
sessionToken: loadOptionalSetting({
|
|
1443
|
+
settingValue: options.sessionToken,
|
|
1444
|
+
environmentVariableName: "AWS_SESSION_TOKEN"
|
|
1445
|
+
})
|
|
1275
1446
|
};
|
|
1447
|
+
} catch (error) {
|
|
1448
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
1449
|
+
if (errorMessage.includes("AWS_ACCESS_KEY_ID") || errorMessage.includes("accessKeyId")) {
|
|
1450
|
+
throw new Error(
|
|
1451
|
+
`AWS SigV4 authentication requires AWS credentials. Please provide either:
|
|
1452
|
+
1. Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables
|
|
1453
|
+
2. Provide accessKeyId and secretAccessKey in options
|
|
1454
|
+
3. Use a credentialProvider function
|
|
1455
|
+
4. Use API key authentication with AWS_BEARER_TOKEN_BEDROCK or apiKey option
|
|
1456
|
+
Original error: ${errorMessage}`
|
|
1457
|
+
);
|
|
1458
|
+
}
|
|
1459
|
+
if (errorMessage.includes("AWS_SECRET_ACCESS_KEY") || errorMessage.includes("secretAccessKey")) {
|
|
1460
|
+
throw new Error(
|
|
1461
|
+
`AWS SigV4 authentication requires both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. Please ensure both credentials are provided.
|
|
1462
|
+
Original error: ${errorMessage}`
|
|
1463
|
+
);
|
|
1464
|
+
}
|
|
1465
|
+
throw error;
|
|
1276
1466
|
}
|
|
1277
|
-
return {
|
|
1278
|
-
region,
|
|
1279
|
-
accessKeyId: loadSetting({
|
|
1280
|
-
settingValue: options.accessKeyId,
|
|
1281
|
-
settingName: "accessKeyId",
|
|
1282
|
-
environmentVariableName: "AWS_ACCESS_KEY_ID",
|
|
1283
|
-
description: "AWS access key ID"
|
|
1284
|
-
}),
|
|
1285
|
-
secretAccessKey: loadSetting({
|
|
1286
|
-
settingValue: options.secretAccessKey,
|
|
1287
|
-
settingName: "secretAccessKey",
|
|
1288
|
-
environmentVariableName: "AWS_SECRET_ACCESS_KEY",
|
|
1289
|
-
description: "AWS secret access key"
|
|
1290
|
-
}),
|
|
1291
|
-
sessionToken: loadOptionalSetting({
|
|
1292
|
-
settingValue: options.sessionToken,
|
|
1293
|
-
environmentVariableName: "AWS_SESSION_TOKEN"
|
|
1294
|
-
})
|
|
1295
|
-
};
|
|
1296
1467
|
}, options.fetch);
|
|
1297
1468
|
const getBaseUrl = () => {
|
|
1298
1469
|
var _a, _b;
|
|
@@ -1310,7 +1481,7 @@ function createAmazonBedrock(options = {}) {
|
|
|
1310
1481
|
return new BedrockChatLanguageModel(modelId, {
|
|
1311
1482
|
baseUrl: getBaseUrl,
|
|
1312
1483
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1313
|
-
fetch:
|
|
1484
|
+
fetch: fetchFunction,
|
|
1314
1485
|
generateId
|
|
1315
1486
|
});
|
|
1316
1487
|
};
|
|
@@ -1327,7 +1498,7 @@ function createAmazonBedrock(options = {}) {
|
|
|
1327
1498
|
return new BedrockEmbeddingModel(modelId, {
|
|
1328
1499
|
baseUrl: getBaseUrl,
|
|
1329
1500
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1330
|
-
fetch:
|
|
1501
|
+
fetch: fetchFunction
|
|
1331
1502
|
});
|
|
1332
1503
|
};
|
|
1333
1504
|
const createImageModel = (modelId) => {
|
|
@@ -1335,7 +1506,7 @@ function createAmazonBedrock(options = {}) {
|
|
|
1335
1506
|
return new BedrockImageModel(modelId, {
|
|
1336
1507
|
baseUrl: getBaseUrl,
|
|
1337
1508
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1338
|
-
fetch:
|
|
1509
|
+
fetch: fetchFunction
|
|
1339
1510
|
});
|
|
1340
1511
|
};
|
|
1341
1512
|
provider.languageModel = createChatModel;
|