@ai-sdk/amazon-bedrock 5.0.0-beta.4 → 5.0.0-beta.40

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/dist/index.mjs DELETED
@@ -1,2329 +0,0 @@
1
- // src/bedrock-provider.ts
2
- import { anthropicTools as anthropicTools2 } from "@ai-sdk/anthropic/internal";
3
- import {
4
- generateId,
5
- loadOptionalSetting,
6
- loadSetting,
7
- withoutTrailingSlash,
8
- withUserAgentSuffix as withUserAgentSuffix2
9
- } from "@ai-sdk/provider-utils";
10
-
11
- // src/bedrock-chat-language-model.ts
12
- import {
13
- combineHeaders,
14
- createJsonErrorResponseHandler,
15
- createJsonResponseHandler,
16
- parseProviderOptions as parseProviderOptions2,
17
- postJsonToApi,
18
- resolve
19
- } from "@ai-sdk/provider-utils";
20
- import { z as z3 } from "zod/v4";
21
-
22
- // src/bedrock-api-types.ts
23
- var BEDROCK_STOP_REASONS = [
24
- "stop",
25
- "stop_sequence",
26
- "end_turn",
27
- "length",
28
- "max_tokens",
29
- "content-filter",
30
- "content_filtered",
31
- "guardrail_intervened",
32
- "tool-calls",
33
- "tool_use"
34
- ];
35
- var BEDROCK_IMAGE_MIME_TYPES = {
36
- "image/jpeg": "jpeg",
37
- "image/png": "png",
38
- "image/gif": "gif",
39
- "image/webp": "webp"
40
- };
41
- var BEDROCK_DOCUMENT_MIME_TYPES = {
42
- "application/pdf": "pdf",
43
- "text/csv": "csv",
44
- "application/msword": "doc",
45
- "application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
46
- "application/vnd.ms-excel": "xls",
47
- "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
48
- "text/html": "html",
49
- "text/plain": "txt",
50
- "text/markdown": "md"
51
- };
52
-
53
- // src/bedrock-chat-options.ts
54
- import { z } from "zod/v4";
55
- var bedrockFilePartProviderOptions = z.object({
56
- /**
57
- * Citation configuration for this document.
58
- * When enabled, this document will generate citations in the response.
59
- */
60
- citations: z.object({
61
- /**
62
- * Enable citations for this document
63
- */
64
- enabled: z.boolean()
65
- }).optional()
66
- });
67
- var amazonBedrockLanguageModelOptions = z.object({
68
- /**
69
- * Additional inference parameters that the model supports,
70
- * beyond the base set of inference parameters that Converse
71
- * supports in the inferenceConfig field
72
- */
73
- additionalModelRequestFields: z.record(z.string(), z.any()).optional(),
74
- reasoningConfig: z.object({
75
- type: z.union([
76
- z.literal("enabled"),
77
- z.literal("disabled"),
78
- z.literal("adaptive")
79
- ]).optional(),
80
- budgetTokens: z.number().optional(),
81
- maxReasoningEffort: z.enum(["low", "medium", "high", "max"]).optional()
82
- }).optional(),
83
- /**
84
- * Anthropic beta features to enable
85
- */
86
- anthropicBeta: z.array(z.string()).optional()
87
- });
88
-
89
- // src/bedrock-error.ts
90
- import { z as z2 } from "zod/v4";
91
- var BedrockErrorSchema = z2.object({
92
- message: z2.string(),
93
- type: z2.string().nullish()
94
- });
95
-
96
- // src/bedrock-event-stream-response-handler.ts
97
- import { EmptyResponseBodyError } from "@ai-sdk/provider";
98
- import {
99
- safeParseJSON,
100
- extractResponseHeaders,
101
- safeValidateTypes
102
- } from "@ai-sdk/provider-utils";
103
-
104
- // src/bedrock-event-stream-decoder.ts
105
- import { EventStreamCodec } from "@smithy/eventstream-codec";
106
- import { toUtf8, fromUtf8 } from "@smithy/util-utf8";
107
- function createBedrockEventStreamDecoder(body, processEvent) {
108
- const codec = new EventStreamCodec(toUtf8, fromUtf8);
109
- let buffer = new Uint8Array(0);
110
- const textDecoder = new TextDecoder();
111
- return body.pipeThrough(
112
- new TransformStream({
113
- async transform(chunk, controller) {
114
- var _a, _b;
115
- const newBuffer = new Uint8Array(buffer.length + chunk.length);
116
- newBuffer.set(buffer);
117
- newBuffer.set(chunk, buffer.length);
118
- buffer = newBuffer;
119
- while (buffer.length >= 4) {
120
- const totalLength = new DataView(
121
- buffer.buffer,
122
- buffer.byteOffset,
123
- buffer.byteLength
124
- ).getUint32(0, false);
125
- if (buffer.length < totalLength) {
126
- break;
127
- }
128
- try {
129
- const subView = buffer.subarray(0, totalLength);
130
- const decoded = codec.decode(subView);
131
- buffer = buffer.slice(totalLength);
132
- const messageType = (_a = decoded.headers[":message-type"]) == null ? void 0 : _a.value;
133
- const eventType = (_b = decoded.headers[":event-type"]) == null ? void 0 : _b.value;
134
- const data = textDecoder.decode(decoded.body);
135
- await processEvent({ messageType, eventType, data }, controller);
136
- } catch (e) {
137
- break;
138
- }
139
- }
140
- }
141
- })
142
- );
143
- }
144
-
145
- // src/bedrock-event-stream-response-handler.ts
146
- var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response }) => {
147
- const responseHeaders = extractResponseHeaders(response);
148
- if (response.body == null) {
149
- throw new EmptyResponseBodyError({});
150
- }
151
- return {
152
- responseHeaders,
153
- value: createBedrockEventStreamDecoder(
154
- response.body,
155
- async (event, controller) => {
156
- if (event.messageType === "event") {
157
- const parsedDataResult = await safeParseJSON({ text: event.data });
158
- if (!parsedDataResult.success) {
159
- controller.enqueue(parsedDataResult);
160
- return;
161
- }
162
- delete parsedDataResult.value.p;
163
- const wrappedData = {
164
- [event.eventType]: parsedDataResult.value
165
- };
166
- const validatedWrappedData = await safeValidateTypes({
167
- value: wrappedData,
168
- schema: chunkSchema
169
- });
170
- if (!validatedWrappedData.success) {
171
- controller.enqueue(validatedWrappedData);
172
- } else {
173
- controller.enqueue({
174
- success: true,
175
- value: validatedWrappedData.value,
176
- rawValue: wrappedData
177
- });
178
- }
179
- }
180
- }
181
- )
182
- };
183
- };
184
-
185
- // src/bedrock-prepare-tools.ts
186
- import {
187
- UnsupportedFunctionalityError
188
- } from "@ai-sdk/provider";
189
- import { asSchema } from "@ai-sdk/provider-utils";
190
- import {
191
- anthropicTools,
192
- prepareTools as prepareAnthropicTools
193
- } from "@ai-sdk/anthropic/internal";
194
- async function prepareTools({
195
- tools,
196
- toolChoice,
197
- modelId
198
- }) {
199
- var _a;
200
- const toolWarnings = [];
201
- const betas = /* @__PURE__ */ new Set();
202
- if (tools == null || tools.length === 0) {
203
- return {
204
- toolConfig: {},
205
- additionalTools: void 0,
206
- betas,
207
- toolWarnings
208
- };
209
- }
210
- const supportedTools = tools.filter((tool) => {
211
- if (tool.type === "provider" && tool.id === "anthropic.web_search_20250305") {
212
- toolWarnings.push({
213
- type: "unsupported",
214
- feature: "web_search_20250305 tool",
215
- details: "The web_search_20250305 tool is not supported on Amazon Bedrock."
216
- });
217
- return false;
218
- }
219
- return true;
220
- });
221
- if (supportedTools.length === 0) {
222
- return {
223
- toolConfig: {},
224
- additionalTools: void 0,
225
- betas,
226
- toolWarnings
227
- };
228
- }
229
- const isAnthropicModel = modelId.includes("anthropic.");
230
- const ProviderTools = supportedTools.filter((t) => t.type === "provider");
231
- const functionTools = supportedTools.filter((t) => t.type === "function");
232
- let additionalTools = void 0;
233
- const bedrockTools = [];
234
- const usingAnthropicTools = isAnthropicModel && ProviderTools.length > 0;
235
- if (usingAnthropicTools) {
236
- const {
237
- toolChoice: preparedAnthropicToolChoice,
238
- toolWarnings: anthropicToolWarnings,
239
- betas: anthropicBetas
240
- } = await prepareAnthropicTools({
241
- tools: ProviderTools,
242
- toolChoice,
243
- supportsStructuredOutput: false,
244
- supportsStrictTools: false
245
- });
246
- toolWarnings.push(...anthropicToolWarnings);
247
- anthropicBetas.forEach((beta) => betas.add(beta));
248
- if (preparedAnthropicToolChoice) {
249
- additionalTools = {
250
- tool_choice: preparedAnthropicToolChoice
251
- };
252
- }
253
- for (const tool of ProviderTools) {
254
- const toolFactory = Object.values(anthropicTools).find((factory) => {
255
- const instance = factory({});
256
- return instance.id === tool.id;
257
- });
258
- if (toolFactory != null) {
259
- const fullToolDefinition = toolFactory({});
260
- bedrockTools.push({
261
- toolSpec: {
262
- name: tool.name,
263
- inputSchema: {
264
- json: await asSchema(fullToolDefinition.inputSchema).jsonSchema
265
- }
266
- }
267
- });
268
- } else {
269
- toolWarnings.push({ type: "unsupported", feature: "tool ${tool.id}" });
270
- }
271
- }
272
- } else {
273
- for (const tool of ProviderTools) {
274
- toolWarnings.push({ type: "unsupported", feature: `tool ${tool.id}` });
275
- }
276
- }
277
- const filteredFunctionTools = (toolChoice == null ? void 0 : toolChoice.type) === "tool" ? functionTools.filter((t) => t.name === toolChoice.toolName) : functionTools;
278
- for (const tool of filteredFunctionTools) {
279
- bedrockTools.push({
280
- toolSpec: {
281
- name: tool.name,
282
- ...((_a = tool.description) == null ? void 0 : _a.trim()) !== "" ? { description: tool.description } : {},
283
- ...tool.strict != null ? { strict: tool.strict } : {},
284
- inputSchema: {
285
- json: tool.inputSchema
286
- }
287
- }
288
- });
289
- }
290
- let bedrockToolChoice = void 0;
291
- if (!usingAnthropicTools && bedrockTools.length > 0 && toolChoice) {
292
- const type = toolChoice.type;
293
- switch (type) {
294
- case "auto":
295
- bedrockToolChoice = { auto: {} };
296
- break;
297
- case "required":
298
- bedrockToolChoice = { any: {} };
299
- break;
300
- case "none":
301
- bedrockTools.length = 0;
302
- bedrockToolChoice = void 0;
303
- break;
304
- case "tool":
305
- bedrockToolChoice = { tool: { name: toolChoice.toolName } };
306
- break;
307
- default: {
308
- const _exhaustiveCheck = type;
309
- throw new UnsupportedFunctionalityError({
310
- functionality: `tool choice type: ${_exhaustiveCheck}`
311
- });
312
- }
313
- }
314
- }
315
- const toolConfig = bedrockTools.length > 0 ? { tools: bedrockTools, toolChoice: bedrockToolChoice } : {};
316
- return {
317
- toolConfig,
318
- additionalTools,
319
- betas,
320
- toolWarnings
321
- };
322
- }
323
-
324
- // src/convert-bedrock-usage.ts
325
- function convertBedrockUsage(usage) {
326
- var _a, _b;
327
- if (usage == null) {
328
- return {
329
- inputTokens: {
330
- total: void 0,
331
- noCache: void 0,
332
- cacheRead: void 0,
333
- cacheWrite: void 0
334
- },
335
- outputTokens: {
336
- total: void 0,
337
- text: void 0,
338
- reasoning: void 0
339
- },
340
- raw: void 0
341
- };
342
- }
343
- const inputTokens = usage.inputTokens;
344
- const outputTokens = usage.outputTokens;
345
- const cacheReadTokens = (_a = usage.cacheReadInputTokens) != null ? _a : 0;
346
- const cacheWriteTokens = (_b = usage.cacheWriteInputTokens) != null ? _b : 0;
347
- return {
348
- inputTokens: {
349
- total: inputTokens + cacheReadTokens + cacheWriteTokens,
350
- noCache: inputTokens,
351
- cacheRead: cacheReadTokens,
352
- cacheWrite: cacheWriteTokens
353
- },
354
- outputTokens: {
355
- total: outputTokens,
356
- text: outputTokens,
357
- reasoning: void 0
358
- },
359
- raw: usage
360
- };
361
- }
362
-
363
- // src/convert-to-bedrock-chat-messages.ts
364
- import {
365
- UnsupportedFunctionalityError as UnsupportedFunctionalityError2
366
- } from "@ai-sdk/provider";
367
- import {
368
- convertToBase64,
369
- parseProviderOptions,
370
- stripFileExtension
371
- } from "@ai-sdk/provider-utils";
372
-
373
- // src/normalize-tool-call-id.ts
374
- function isMistralModel(modelId) {
375
- return modelId.includes("mistral.");
376
- }
377
- function normalizeToolCallId(toolCallId, isMistral) {
378
- if (!isMistral) {
379
- return toolCallId;
380
- }
381
- const alphanumericChars = toolCallId.replace(/[^a-zA-Z0-9]/g, "");
382
- return alphanumericChars.slice(0, 9);
383
- }
384
-
385
- // src/convert-to-bedrock-chat-messages.ts
386
- function getCachePoint(providerMetadata) {
387
- var _a;
388
- const cachePointConfig = (_a = providerMetadata == null ? void 0 : providerMetadata.bedrock) == null ? void 0 : _a.cachePoint;
389
- if (!cachePointConfig) {
390
- return void 0;
391
- }
392
- return { cachePoint: cachePointConfig };
393
- }
394
- async function shouldEnableCitations(providerMetadata) {
395
- var _a, _b;
396
- const bedrockOptions = await parseProviderOptions({
397
- provider: "bedrock",
398
- providerOptions: providerMetadata,
399
- schema: bedrockFilePartProviderOptions
400
- });
401
- return (_b = (_a = bedrockOptions == null ? void 0 : bedrockOptions.citations) == null ? void 0 : _a.enabled) != null ? _b : false;
402
- }
403
- async function convertToBedrockChatMessages(prompt, isMistral = false) {
404
- var _a;
405
- const blocks = groupIntoBlocks(prompt);
406
- let system = [];
407
- const messages = [];
408
- let documentCounter = 0;
409
- const generateDocumentName = () => `document-${++documentCounter}`;
410
- for (let i = 0; i < blocks.length; i++) {
411
- const block = blocks[i];
412
- const isLastBlock = i === blocks.length - 1;
413
- const type = block.type;
414
- switch (type) {
415
- case "system": {
416
- if (messages.length > 0) {
417
- throw new UnsupportedFunctionalityError2({
418
- functionality: "Multiple system messages that are separated by user/assistant messages"
419
- });
420
- }
421
- for (const message of block.messages) {
422
- system.push({ text: message.content });
423
- const cachePoint = getCachePoint(message.providerOptions);
424
- if (cachePoint) {
425
- system.push(cachePoint);
426
- }
427
- }
428
- break;
429
- }
430
- case "user": {
431
- const bedrockContent = [];
432
- for (const message of block.messages) {
433
- const { role, content, providerOptions } = message;
434
- switch (role) {
435
- case "user": {
436
- for (let j = 0; j < content.length; j++) {
437
- const part = content[j];
438
- switch (part.type) {
439
- case "text": {
440
- bedrockContent.push({
441
- text: part.text
442
- });
443
- break;
444
- }
445
- case "file": {
446
- if (part.data instanceof URL) {
447
- throw new UnsupportedFunctionalityError2({
448
- functionality: "File URL data"
449
- });
450
- }
451
- if (part.mediaType.startsWith("image/")) {
452
- bedrockContent.push({
453
- image: {
454
- format: getBedrockImageFormat(part.mediaType),
455
- source: { bytes: convertToBase64(part.data) }
456
- }
457
- });
458
- } else {
459
- if (!part.mediaType) {
460
- throw new UnsupportedFunctionalityError2({
461
- functionality: "file without mime type",
462
- message: "File mime type is required in user message part content"
463
- });
464
- }
465
- const enableCitations = await shouldEnableCitations(
466
- part.providerOptions
467
- );
468
- bedrockContent.push({
469
- document: {
470
- format: getBedrockDocumentFormat(part.mediaType),
471
- name: part.filename ? stripFileExtension(part.filename) : generateDocumentName(),
472
- source: { bytes: convertToBase64(part.data) },
473
- ...enableCitations && {
474
- citations: { enabled: true }
475
- }
476
- }
477
- });
478
- }
479
- break;
480
- }
481
- }
482
- }
483
- break;
484
- }
485
- case "tool": {
486
- for (const part of content) {
487
- if (part.type === "tool-approval-response") {
488
- continue;
489
- }
490
- let toolResultContent;
491
- const output = part.output;
492
- switch (output.type) {
493
- case "content": {
494
- toolResultContent = output.value.map((contentPart) => {
495
- switch (contentPart.type) {
496
- case "text":
497
- return { text: contentPart.text };
498
- case "image-data":
499
- if (!contentPart.mediaType.startsWith("image/")) {
500
- throw new UnsupportedFunctionalityError2({
501
- functionality: `media type: ${contentPart.mediaType}`
502
- });
503
- }
504
- const format = getBedrockImageFormat(
505
- contentPart.mediaType
506
- );
507
- return {
508
- image: {
509
- format,
510
- source: { bytes: contentPart.data }
511
- }
512
- };
513
- default: {
514
- throw new UnsupportedFunctionalityError2({
515
- functionality: `unsupported tool content part type: ${contentPart.type}`
516
- });
517
- }
518
- }
519
- });
520
- break;
521
- }
522
- case "text":
523
- case "error-text":
524
- toolResultContent = [{ text: output.value }];
525
- break;
526
- case "execution-denied":
527
- toolResultContent = [
528
- { text: (_a = output.reason) != null ? _a : "Tool execution denied." }
529
- ];
530
- break;
531
- case "json":
532
- case "error-json":
533
- default:
534
- toolResultContent = [
535
- { text: JSON.stringify(output.value) }
536
- ];
537
- break;
538
- }
539
- bedrockContent.push({
540
- toolResult: {
541
- toolUseId: normalizeToolCallId(part.toolCallId, isMistral),
542
- content: toolResultContent
543
- }
544
- });
545
- }
546
- break;
547
- }
548
- default: {
549
- const _exhaustiveCheck = role;
550
- throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
551
- }
552
- }
553
- const cachePoint = getCachePoint(providerOptions);
554
- if (cachePoint) {
555
- bedrockContent.push(cachePoint);
556
- }
557
- }
558
- messages.push({ role: "user", content: bedrockContent });
559
- break;
560
- }
561
- case "assistant": {
562
- const bedrockContent = [];
563
- for (let j = 0; j < block.messages.length; j++) {
564
- const message = block.messages[j];
565
- const isLastMessage = j === block.messages.length - 1;
566
- const { content } = message;
567
- for (let k = 0; k < content.length; k++) {
568
- const part = content[k];
569
- const isLastContentPart = k === content.length - 1;
570
- switch (part.type) {
571
- case "text": {
572
- if (!part.text.trim()) {
573
- break;
574
- }
575
- bedrockContent.push({
576
- text: (
577
- // trim the last text part if it's the last message in the block
578
- // because Bedrock does not allow trailing whitespace
579
- // in pre-filled assistant responses
580
- trimIfLast(
581
- isLastBlock,
582
- isLastMessage,
583
- isLastContentPart,
584
- part.text
585
- )
586
- )
587
- });
588
- break;
589
- }
590
- case "reasoning": {
591
- const reasoningMetadata = await parseProviderOptions({
592
- provider: "bedrock",
593
- providerOptions: part.providerOptions,
594
- schema: bedrockReasoningMetadataSchema
595
- });
596
- if (reasoningMetadata != null) {
597
- if (reasoningMetadata.signature != null) {
598
- bedrockContent.push({
599
- reasoningContent: {
600
- reasoningText: {
601
- // trim the last text part if it's the last message in the block
602
- // because Bedrock does not allow trailing whitespace
603
- // in pre-filled assistant responses
604
- text: trimIfLast(
605
- isLastBlock,
606
- isLastMessage,
607
- isLastContentPart,
608
- part.text
609
- ),
610
- signature: reasoningMetadata.signature
611
- }
612
- }
613
- });
614
- } else if (reasoningMetadata.redactedData != null) {
615
- bedrockContent.push({
616
- reasoningContent: {
617
- redactedReasoning: {
618
- data: reasoningMetadata.redactedData
619
- }
620
- }
621
- });
622
- }
623
- }
624
- break;
625
- }
626
- case "tool-call": {
627
- bedrockContent.push({
628
- toolUse: {
629
- toolUseId: normalizeToolCallId(part.toolCallId, isMistral),
630
- name: part.toolName,
631
- input: part.input
632
- }
633
- });
634
- break;
635
- }
636
- }
637
- }
638
- const cachePoint = getCachePoint(message.providerOptions);
639
- if (cachePoint) {
640
- bedrockContent.push(cachePoint);
641
- }
642
- }
643
- messages.push({ role: "assistant", content: bedrockContent });
644
- break;
645
- }
646
- default: {
647
- const _exhaustiveCheck = type;
648
- throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
649
- }
650
- }
651
- }
652
- return { system, messages };
653
- }
654
- function getBedrockImageFormat(mimeType) {
655
- if (!mimeType) {
656
- throw new UnsupportedFunctionalityError2({
657
- functionality: "image without mime type",
658
- message: "Image mime type is required in user message part content"
659
- });
660
- }
661
- const format = BEDROCK_IMAGE_MIME_TYPES[mimeType];
662
- if (!format) {
663
- throw new UnsupportedFunctionalityError2({
664
- functionality: `image mime type: ${mimeType}`,
665
- message: `Unsupported image mime type: ${mimeType}, expected one of: ${Object.keys(BEDROCK_IMAGE_MIME_TYPES).join(", ")}`
666
- });
667
- }
668
- return format;
669
- }
670
- function getBedrockDocumentFormat(mimeType) {
671
- const format = BEDROCK_DOCUMENT_MIME_TYPES[mimeType];
672
- if (!format) {
673
- throw new UnsupportedFunctionalityError2({
674
- functionality: `file mime type: ${mimeType}`,
675
- message: `Unsupported file mime type: ${mimeType}, expected one of: ${Object.keys(BEDROCK_DOCUMENT_MIME_TYPES).join(", ")}`
676
- });
677
- }
678
- return format;
679
- }
680
- function trimIfLast(isLastBlock, isLastMessage, isLastContentPart, text) {
681
- return isLastBlock && isLastMessage && isLastContentPart ? text.trim() : text;
682
- }
683
- function groupIntoBlocks(prompt) {
684
- const blocks = [];
685
- let currentBlock = void 0;
686
- for (const message of prompt) {
687
- const { role } = message;
688
- switch (role) {
689
- case "system": {
690
- if ((currentBlock == null ? void 0 : currentBlock.type) !== "system") {
691
- currentBlock = { type: "system", messages: [] };
692
- blocks.push(currentBlock);
693
- }
694
- currentBlock.messages.push(message);
695
- break;
696
- }
697
- case "assistant": {
698
- if ((currentBlock == null ? void 0 : currentBlock.type) !== "assistant") {
699
- currentBlock = { type: "assistant", messages: [] };
700
- blocks.push(currentBlock);
701
- }
702
- currentBlock.messages.push(message);
703
- break;
704
- }
705
- case "user": {
706
- if ((currentBlock == null ? void 0 : currentBlock.type) !== "user") {
707
- currentBlock = { type: "user", messages: [] };
708
- blocks.push(currentBlock);
709
- }
710
- currentBlock.messages.push(message);
711
- break;
712
- }
713
- case "tool": {
714
- if ((currentBlock == null ? void 0 : currentBlock.type) !== "user") {
715
- currentBlock = { type: "user", messages: [] };
716
- blocks.push(currentBlock);
717
- }
718
- currentBlock.messages.push(message);
719
- break;
720
- }
721
- default: {
722
- const _exhaustiveCheck = role;
723
- throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
724
- }
725
- }
726
- }
727
- return blocks;
728
- }
729
-
730
- // src/map-bedrock-finish-reason.ts
731
- function mapBedrockFinishReason(finishReason, isJsonResponseFromTool) {
732
- switch (finishReason) {
733
- case "stop_sequence":
734
- case "end_turn":
735
- return "stop";
736
- case "max_tokens":
737
- return "length";
738
- case "content_filtered":
739
- case "guardrail_intervened":
740
- return "content-filter";
741
- case "tool_use":
742
- return isJsonResponseFromTool ? "stop" : "tool-calls";
743
- default:
744
- return "other";
745
- }
746
- }
747
-
748
- // src/bedrock-chat-language-model.ts
749
- var BedrockChatLanguageModel = class {
750
- constructor(modelId, config) {
751
- this.modelId = modelId;
752
- this.config = config;
753
- this.specificationVersion = "v4";
754
- this.provider = "amazon-bedrock";
755
- this.supportedUrls = {
756
- // no supported urls for bedrock
757
- };
758
- }
759
- async getArgs({
760
- prompt,
761
- maxOutputTokens,
762
- temperature,
763
- topP,
764
- topK,
765
- frequencyPenalty,
766
- presencePenalty,
767
- stopSequences,
768
- responseFormat,
769
- seed,
770
- tools,
771
- toolChoice,
772
- providerOptions
773
- }) {
774
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
775
- const bedrockOptions = (_a = await parseProviderOptions2({
776
- provider: "bedrock",
777
- providerOptions,
778
- schema: amazonBedrockLanguageModelOptions
779
- })) != null ? _a : {};
780
- const warnings = [];
781
- if (frequencyPenalty != null) {
782
- warnings.push({
783
- type: "unsupported",
784
- feature: "frequencyPenalty"
785
- });
786
- }
787
- if (presencePenalty != null) {
788
- warnings.push({
789
- type: "unsupported",
790
- feature: "presencePenalty"
791
- });
792
- }
793
- if (seed != null) {
794
- warnings.push({
795
- type: "unsupported",
796
- feature: "seed"
797
- });
798
- }
799
- if (temperature != null && temperature > 1) {
800
- warnings.push({
801
- type: "unsupported",
802
- feature: "temperature",
803
- details: `${temperature} exceeds bedrock maximum of 1.0. clamped to 1.0`
804
- });
805
- temperature = 1;
806
- } else if (temperature != null && temperature < 0) {
807
- warnings.push({
808
- type: "unsupported",
809
- feature: "temperature",
810
- details: `${temperature} is below bedrock minimum of 0. clamped to 0`
811
- });
812
- temperature = 0;
813
- }
814
- if (responseFormat != null && responseFormat.type !== "text" && responseFormat.type !== "json") {
815
- warnings.push({
816
- type: "unsupported",
817
- feature: "responseFormat",
818
- details: "Only text and json response formats are supported."
819
- });
820
- }
821
- const isAnthropicModel = this.modelId.includes("anthropic");
822
- const isThinkingEnabled = ((_b = bedrockOptions.reasoningConfig) == null ? void 0 : _b.type) === "enabled" || ((_c = bedrockOptions.reasoningConfig) == null ? void 0 : _c.type) === "adaptive";
823
- const useNativeStructuredOutput = isAnthropicModel && isThinkingEnabled && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null;
824
- const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !useNativeStructuredOutput ? {
825
- type: "function",
826
- name: "json",
827
- description: "Respond with a JSON object.",
828
- inputSchema: responseFormat.schema
829
- } : void 0;
830
- const { toolConfig, additionalTools, toolWarnings, betas } = await prepareTools({
831
- tools: jsonResponseTool ? [...tools != null ? tools : [], jsonResponseTool] : tools,
832
- toolChoice: jsonResponseTool != null ? { type: "required" } : toolChoice,
833
- modelId: this.modelId
834
- });
835
- warnings.push(...toolWarnings);
836
- if (additionalTools) {
837
- bedrockOptions.additionalModelRequestFields = {
838
- ...bedrockOptions.additionalModelRequestFields,
839
- ...additionalTools
840
- };
841
- }
842
- if (betas.size > 0 || bedrockOptions.anthropicBeta) {
843
- const existingBetas = (_d = bedrockOptions.anthropicBeta) != null ? _d : [];
844
- const mergedBetas = betas.size > 0 ? [...existingBetas, ...Array.from(betas)] : existingBetas;
845
- bedrockOptions.additionalModelRequestFields = {
846
- ...bedrockOptions.additionalModelRequestFields,
847
- anthropic_beta: mergedBetas
848
- };
849
- }
850
- const thinkingType = (_e = bedrockOptions.reasoningConfig) == null ? void 0 : _e.type;
851
- const thinkingBudget = thinkingType === "enabled" ? (_f = bedrockOptions.reasoningConfig) == null ? void 0 : _f.budgetTokens : void 0;
852
- const isAnthropicThinkingEnabled = isAnthropicModel && isThinkingEnabled;
853
- const inferenceConfig = {
854
- ...maxOutputTokens != null && { maxTokens: maxOutputTokens },
855
- ...temperature != null && { temperature },
856
- ...topP != null && { topP },
857
- ...topK != null && { topK },
858
- ...stopSequences != null && { stopSequences }
859
- };
860
- if (isAnthropicThinkingEnabled) {
861
- if (thinkingBudget != null) {
862
- if (inferenceConfig.maxTokens != null) {
863
- inferenceConfig.maxTokens += thinkingBudget;
864
- } else {
865
- inferenceConfig.maxTokens = thinkingBudget + 4096;
866
- }
867
- bedrockOptions.additionalModelRequestFields = {
868
- ...bedrockOptions.additionalModelRequestFields,
869
- thinking: {
870
- type: "enabled",
871
- budget_tokens: thinkingBudget
872
- }
873
- };
874
- } else if (thinkingType === "adaptive") {
875
- bedrockOptions.additionalModelRequestFields = {
876
- ...bedrockOptions.additionalModelRequestFields,
877
- thinking: {
878
- type: "adaptive"
879
- }
880
- };
881
- }
882
- } else if (!isAnthropicModel) {
883
- if (((_g = bedrockOptions.reasoningConfig) == null ? void 0 : _g.budgetTokens) != null) {
884
- warnings.push({
885
- type: "unsupported",
886
- feature: "budgetTokens",
887
- details: "budgetTokens applies only to Anthropic models on Bedrock and will be ignored for this model."
888
- });
889
- }
890
- if (thinkingType === "adaptive") {
891
- warnings.push({
892
- type: "unsupported",
893
- feature: "adaptive thinking",
894
- details: "adaptive thinking type applies only to Anthropic models on Bedrock."
895
- });
896
- }
897
- }
898
- const maxReasoningEffort = (_h = bedrockOptions.reasoningConfig) == null ? void 0 : _h.maxReasoningEffort;
899
- const isOpenAIModel = this.modelId.startsWith("openai.");
900
- if (maxReasoningEffort != null) {
901
- if (isAnthropicModel) {
902
- bedrockOptions.additionalModelRequestFields = {
903
- ...bedrockOptions.additionalModelRequestFields,
904
- output_config: {
905
- ...(_i = bedrockOptions.additionalModelRequestFields) == null ? void 0 : _i.output_config,
906
- effort: maxReasoningEffort
907
- }
908
- };
909
- } else if (isOpenAIModel) {
910
- bedrockOptions.additionalModelRequestFields = {
911
- ...bedrockOptions.additionalModelRequestFields,
912
- reasoning_effort: maxReasoningEffort
913
- };
914
- } else {
915
- bedrockOptions.additionalModelRequestFields = {
916
- ...bedrockOptions.additionalModelRequestFields,
917
- reasoningConfig: {
918
- ...thinkingType != null && thinkingType !== "adaptive" && { type: thinkingType },
919
- ...thinkingBudget != null && { budgetTokens: thinkingBudget },
920
- maxReasoningEffort
921
- }
922
- };
923
- }
924
- }
925
- if (useNativeStructuredOutput) {
926
- bedrockOptions.additionalModelRequestFields = {
927
- ...bedrockOptions.additionalModelRequestFields,
928
- output_config: {
929
- ...(_j = bedrockOptions.additionalModelRequestFields) == null ? void 0 : _j.output_config,
930
- format: {
931
- type: "json_schema",
932
- schema: responseFormat.schema
933
- }
934
- }
935
- };
936
- }
937
- if (isAnthropicThinkingEnabled && inferenceConfig.temperature != null) {
938
- delete inferenceConfig.temperature;
939
- warnings.push({
940
- type: "unsupported",
941
- feature: "temperature",
942
- details: "temperature is not supported when thinking is enabled"
943
- });
944
- }
945
- if (isAnthropicThinkingEnabled && inferenceConfig.topP != null) {
946
- delete inferenceConfig.topP;
947
- warnings.push({
948
- type: "unsupported",
949
- feature: "topP",
950
- details: "topP is not supported when thinking is enabled"
951
- });
952
- }
953
- if (isAnthropicThinkingEnabled && inferenceConfig.topK != null) {
954
- delete inferenceConfig.topK;
955
- warnings.push({
956
- type: "unsupported",
957
- feature: "topK",
958
- details: "topK is not supported when thinking is enabled"
959
- });
960
- }
961
- const hasAnyTools = ((_l = (_k = toolConfig.tools) == null ? void 0 : _k.length) != null ? _l : 0) > 0 || additionalTools;
962
- let filteredPrompt = prompt;
963
- if (!hasAnyTools) {
964
- const hasToolContent = prompt.some(
965
- (message) => "content" in message && Array.isArray(message.content) && message.content.some(
966
- (part) => part.type === "tool-call" || part.type === "tool-result"
967
- )
968
- );
969
- if (hasToolContent) {
970
- filteredPrompt = prompt.map(
971
- (message) => message.role === "system" ? message : {
972
- ...message,
973
- content: message.content.filter(
974
- (part) => part.type !== "tool-call" && part.type !== "tool-result"
975
- )
976
- }
977
- ).filter(
978
- (message) => message.role === "system" || message.content.length > 0
979
- );
980
- warnings.push({
981
- type: "unsupported",
982
- feature: "toolContent",
983
- details: "Tool calls and results removed from conversation because Bedrock does not support tool content without active tools."
984
- });
985
- }
986
- }
987
- const isMistral = isMistralModel(this.modelId);
988
- const { system, messages } = await convertToBedrockChatMessages(
989
- filteredPrompt,
990
- isMistral
991
- );
992
- const {
993
- reasoningConfig: _,
994
- additionalModelRequestFields: __,
995
- ...filteredBedrockOptions
996
- } = (providerOptions == null ? void 0 : providerOptions.bedrock) || {};
997
- const additionalModelResponseFieldPaths = isAnthropicModel ? ["/delta/stop_sequence"] : void 0;
998
- return {
999
- command: {
1000
- system,
1001
- messages,
1002
- additionalModelRequestFields: bedrockOptions.additionalModelRequestFields,
1003
- ...additionalModelResponseFieldPaths && {
1004
- additionalModelResponseFieldPaths
1005
- },
1006
- ...Object.keys(inferenceConfig).length > 0 && {
1007
- inferenceConfig
1008
- },
1009
- ...filteredBedrockOptions,
1010
- ...toolConfig.tools !== void 0 && toolConfig.tools.length > 0 ? { toolConfig } : {}
1011
- },
1012
- warnings,
1013
- usesJsonResponseTool: jsonResponseTool != null,
1014
- betas
1015
- };
1016
- }
1017
- async getHeaders({
1018
- headers
1019
- }) {
1020
- return combineHeaders(await resolve(this.config.headers), headers);
1021
- }
1022
- async doGenerate(options) {
1023
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
1024
- const {
1025
- command: args,
1026
- warnings,
1027
- usesJsonResponseTool
1028
- } = await this.getArgs(options);
1029
- const url = `${this.getUrl(this.modelId)}/converse`;
1030
- const { value: response, responseHeaders } = await postJsonToApi({
1031
- url,
1032
- headers: await this.getHeaders({ headers: options.headers }),
1033
- body: args,
1034
- failedResponseHandler: createJsonErrorResponseHandler({
1035
- errorSchema: BedrockErrorSchema,
1036
- errorToMessage: (error) => {
1037
- var _a2;
1038
- return `${(_a2 = error.message) != null ? _a2 : "Unknown error"}`;
1039
- }
1040
- }),
1041
- successfulResponseHandler: createJsonResponseHandler(
1042
- BedrockResponseSchema
1043
- ),
1044
- abortSignal: options.abortSignal,
1045
- fetch: this.config.fetch
1046
- });
1047
- const content = [];
1048
- let isJsonResponseFromTool = false;
1049
- for (const part of response.output.message.content) {
1050
- if (part.text) {
1051
- content.push({ type: "text", text: part.text });
1052
- }
1053
- if (part.reasoningContent) {
1054
- if ("reasoningText" in part.reasoningContent) {
1055
- const reasoning = {
1056
- type: "reasoning",
1057
- text: part.reasoningContent.reasoningText.text
1058
- };
1059
- if (part.reasoningContent.reasoningText.signature) {
1060
- reasoning.providerMetadata = {
1061
- bedrock: {
1062
- signature: part.reasoningContent.reasoningText.signature
1063
- }
1064
- };
1065
- }
1066
- content.push(reasoning);
1067
- } else if ("redactedReasoning" in part.reasoningContent) {
1068
- content.push({
1069
- type: "reasoning",
1070
- text: "",
1071
- providerMetadata: {
1072
- bedrock: {
1073
- redactedData: (_a = part.reasoningContent.redactedReasoning.data) != null ? _a : ""
1074
- }
1075
- }
1076
- });
1077
- }
1078
- }
1079
- if (part.toolUse) {
1080
- const isJsonResponseTool = usesJsonResponseTool && part.toolUse.name === "json";
1081
- if (isJsonResponseTool) {
1082
- isJsonResponseFromTool = true;
1083
- content.push({
1084
- type: "text",
1085
- text: JSON.stringify(part.toolUse.input)
1086
- });
1087
- } else {
1088
- const isMistral = isMistralModel(this.modelId);
1089
- const rawToolCallId = (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId();
1090
- content.push({
1091
- type: "tool-call",
1092
- toolCallId: normalizeToolCallId(rawToolCallId, isMistral),
1093
- toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
1094
- input: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : {})
1095
- });
1096
- }
1097
- }
1098
- }
1099
- const stopSequence = (_j = (_i = (_h = response.additionalModelResponseFields) == null ? void 0 : _h.delta) == null ? void 0 : _i.stop_sequence) != null ? _j : null;
1100
- const providerMetadata = response.trace || response.usage || response.performanceConfig || response.serviceTier || isJsonResponseFromTool || stopSequence ? {
1101
- bedrock: {
1102
- ...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
1103
- ...response.performanceConfig && {
1104
- performanceConfig: response.performanceConfig
1105
- },
1106
- ...response.serviceTier && {
1107
- serviceTier: response.serviceTier
1108
- },
1109
- ...(((_k = response.usage) == null ? void 0 : _k.cacheWriteInputTokens) != null || ((_l = response.usage) == null ? void 0 : _l.cacheDetails) != null) && {
1110
- usage: {
1111
- ...response.usage.cacheWriteInputTokens != null && {
1112
- cacheWriteInputTokens: response.usage.cacheWriteInputTokens
1113
- },
1114
- ...response.usage.cacheDetails != null && {
1115
- cacheDetails: response.usage.cacheDetails
1116
- }
1117
- }
1118
- },
1119
- ...isJsonResponseFromTool && { isJsonResponseFromTool: true },
1120
- stopSequence
1121
- }
1122
- } : void 0;
1123
- return {
1124
- content,
1125
- finishReason: {
1126
- unified: mapBedrockFinishReason(
1127
- response.stopReason,
1128
- isJsonResponseFromTool
1129
- ),
1130
- raw: (_m = response.stopReason) != null ? _m : void 0
1131
- },
1132
- usage: convertBedrockUsage(response.usage),
1133
- response: {
1134
- id: (_n = responseHeaders == null ? void 0 : responseHeaders["x-amzn-requestid"]) != null ? _n : void 0,
1135
- timestamp: (responseHeaders == null ? void 0 : responseHeaders["date"]) != null ? new Date(responseHeaders["date"]) : void 0,
1136
- modelId: this.modelId,
1137
- headers: responseHeaders
1138
- },
1139
- warnings,
1140
- ...providerMetadata && { providerMetadata }
1141
- };
1142
- }
1143
- async doStream(options) {
1144
- const {
1145
- command: args,
1146
- warnings,
1147
- usesJsonResponseTool
1148
- } = await this.getArgs(options);
1149
- const modelId = this.modelId;
1150
- const isMistral = isMistralModel(modelId);
1151
- const url = `${this.getUrl(modelId)}/converse-stream`;
1152
- const { value: response, responseHeaders } = await postJsonToApi({
1153
- url,
1154
- headers: await this.getHeaders({ headers: options.headers }),
1155
- body: args,
1156
- failedResponseHandler: createJsonErrorResponseHandler({
1157
- errorSchema: BedrockErrorSchema,
1158
- errorToMessage: (error) => `${error.type}: ${error.message}`
1159
- }),
1160
- successfulResponseHandler: createBedrockEventStreamResponseHandler(BedrockStreamSchema),
1161
- abortSignal: options.abortSignal,
1162
- fetch: this.config.fetch
1163
- });
1164
- let finishReason = {
1165
- unified: "other",
1166
- raw: void 0
1167
- };
1168
- let usage = void 0;
1169
- let providerMetadata = void 0;
1170
- let isJsonResponseFromTool = false;
1171
- let stopSequence = null;
1172
- const contentBlocks = {};
1173
- return {
1174
- stream: response.pipeThrough(
1175
- new TransformStream({
1176
- start(controller) {
1177
- var _a;
1178
- controller.enqueue({ type: "stream-start", warnings });
1179
- controller.enqueue({
1180
- type: "response-metadata",
1181
- id: (_a = responseHeaders == null ? void 0 : responseHeaders["x-amzn-requestid"]) != null ? _a : void 0,
1182
- timestamp: (responseHeaders == null ? void 0 : responseHeaders["date"]) != null ? new Date(responseHeaders["date"]) : void 0,
1183
- modelId
1184
- });
1185
- },
1186
- transform(chunk, controller) {
1187
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
1188
- function enqueueError(bedrockError) {
1189
- finishReason = { unified: "error", raw: void 0 };
1190
- controller.enqueue({ type: "error", error: bedrockError });
1191
- }
1192
- if (options.includeRawChunks) {
1193
- controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1194
- }
1195
- if (!chunk.success) {
1196
- enqueueError(chunk.error);
1197
- return;
1198
- }
1199
- const value = chunk.value;
1200
- if (value.internalServerException) {
1201
- enqueueError(value.internalServerException);
1202
- return;
1203
- }
1204
- if (value.modelStreamErrorException) {
1205
- enqueueError(value.modelStreamErrorException);
1206
- return;
1207
- }
1208
- if (value.throttlingException) {
1209
- enqueueError(value.throttlingException);
1210
- return;
1211
- }
1212
- if (value.validationException) {
1213
- enqueueError(value.validationException);
1214
- return;
1215
- }
1216
- if (value.messageStop) {
1217
- finishReason = {
1218
- unified: mapBedrockFinishReason(
1219
- value.messageStop.stopReason,
1220
- isJsonResponseFromTool
1221
- ),
1222
- raw: (_a = value.messageStop.stopReason) != null ? _a : void 0
1223
- };
1224
- stopSequence = (_d = (_c = (_b = value.messageStop.additionalModelResponseFields) == null ? void 0 : _b.delta) == null ? void 0 : _c.stop_sequence) != null ? _d : null;
1225
- }
1226
- if (value.metadata) {
1227
- if (value.metadata.usage) {
1228
- usage = value.metadata.usage;
1229
- }
1230
- const cacheUsage = ((_e = value.metadata.usage) == null ? void 0 : _e.cacheWriteInputTokens) != null || ((_f = value.metadata.usage) == null ? void 0 : _f.cacheDetails) != null ? {
1231
- usage: {
1232
- ...((_g = value.metadata.usage) == null ? void 0 : _g.cacheWriteInputTokens) != null && {
1233
- cacheWriteInputTokens: value.metadata.usage.cacheWriteInputTokens
1234
- },
1235
- ...((_h = value.metadata.usage) == null ? void 0 : _h.cacheDetails) != null && {
1236
- cacheDetails: value.metadata.usage.cacheDetails
1237
- }
1238
- }
1239
- } : void 0;
1240
- const trace = value.metadata.trace ? {
1241
- trace: value.metadata.trace
1242
- } : void 0;
1243
- if (cacheUsage || trace || value.metadata.performanceConfig || value.metadata.serviceTier) {
1244
- providerMetadata = {
1245
- bedrock: {
1246
- ...cacheUsage,
1247
- ...trace,
1248
- ...value.metadata.performanceConfig && {
1249
- performanceConfig: value.metadata.performanceConfig
1250
- },
1251
- ...value.metadata.serviceTier && {
1252
- serviceTier: value.metadata.serviceTier
1253
- }
1254
- }
1255
- };
1256
- }
1257
- }
1258
- if (((_i = value.contentBlockStart) == null ? void 0 : _i.contentBlockIndex) != null && !((_k = (_j = value.contentBlockStart) == null ? void 0 : _j.start) == null ? void 0 : _k.toolUse)) {
1259
- const blockIndex = value.contentBlockStart.contentBlockIndex;
1260
- contentBlocks[blockIndex] = { type: "text" };
1261
- controller.enqueue({
1262
- type: "text-start",
1263
- id: String(blockIndex)
1264
- });
1265
- }
1266
- if (((_l = value.contentBlockDelta) == null ? void 0 : _l.delta) && "text" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.text) {
1267
- const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
1268
- if (contentBlocks[blockIndex] == null) {
1269
- contentBlocks[blockIndex] = { type: "text" };
1270
- controller.enqueue({
1271
- type: "text-start",
1272
- id: String(blockIndex)
1273
- });
1274
- }
1275
- controller.enqueue({
1276
- type: "text-delta",
1277
- id: String(blockIndex),
1278
- delta: value.contentBlockDelta.delta.text
1279
- });
1280
- }
1281
- if (((_m = value.contentBlockStop) == null ? void 0 : _m.contentBlockIndex) != null) {
1282
- const blockIndex = value.contentBlockStop.contentBlockIndex;
1283
- const contentBlock = contentBlocks[blockIndex];
1284
- if (contentBlock != null) {
1285
- if (contentBlock.type === "reasoning") {
1286
- controller.enqueue({
1287
- type: "reasoning-end",
1288
- id: String(blockIndex)
1289
- });
1290
- } else if (contentBlock.type === "text") {
1291
- controller.enqueue({
1292
- type: "text-end",
1293
- id: String(blockIndex)
1294
- });
1295
- } else if (contentBlock.type === "tool-call") {
1296
- if (contentBlock.isJsonResponseTool) {
1297
- isJsonResponseFromTool = true;
1298
- controller.enqueue({
1299
- type: "text-start",
1300
- id: String(blockIndex)
1301
- });
1302
- controller.enqueue({
1303
- type: "text-delta",
1304
- id: String(blockIndex),
1305
- delta: contentBlock.jsonText
1306
- });
1307
- controller.enqueue({
1308
- type: "text-end",
1309
- id: String(blockIndex)
1310
- });
1311
- } else {
1312
- controller.enqueue({
1313
- type: "tool-input-end",
1314
- id: contentBlock.toolCallId
1315
- });
1316
- controller.enqueue({
1317
- type: "tool-call",
1318
- toolCallId: contentBlock.toolCallId,
1319
- toolName: contentBlock.toolName,
1320
- input: contentBlock.jsonText === "" ? "{}" : contentBlock.jsonText
1321
- });
1322
- }
1323
- }
1324
- delete contentBlocks[blockIndex];
1325
- }
1326
- }
1327
- if (((_n = value.contentBlockDelta) == null ? void 0 : _n.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
1328
- const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
1329
- const reasoningContent = value.contentBlockDelta.delta.reasoningContent;
1330
- if ("text" in reasoningContent && reasoningContent.text) {
1331
- if (contentBlocks[blockIndex] == null) {
1332
- contentBlocks[blockIndex] = { type: "reasoning" };
1333
- controller.enqueue({
1334
- type: "reasoning-start",
1335
- id: String(blockIndex)
1336
- });
1337
- }
1338
- controller.enqueue({
1339
- type: "reasoning-delta",
1340
- id: String(blockIndex),
1341
- delta: reasoningContent.text
1342
- });
1343
- } else if ("signature" in reasoningContent && reasoningContent.signature) {
1344
- controller.enqueue({
1345
- type: "reasoning-delta",
1346
- id: String(blockIndex),
1347
- delta: "",
1348
- providerMetadata: {
1349
- bedrock: {
1350
- signature: reasoningContent.signature
1351
- }
1352
- }
1353
- });
1354
- } else if ("data" in reasoningContent && reasoningContent.data) {
1355
- controller.enqueue({
1356
- type: "reasoning-delta",
1357
- id: String(blockIndex),
1358
- delta: "",
1359
- providerMetadata: {
1360
- bedrock: {
1361
- redactedData: reasoningContent.data
1362
- }
1363
- }
1364
- });
1365
- }
1366
- }
1367
- const contentBlockStart = value.contentBlockStart;
1368
- if (((_o = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _o.toolUse) != null) {
1369
- const toolUse = contentBlockStart.start.toolUse;
1370
- const blockIndex = contentBlockStart.contentBlockIndex;
1371
- const isJsonResponseTool = usesJsonResponseTool && toolUse.name === "json";
1372
- const normalizedToolCallId = normalizeToolCallId(
1373
- toolUse.toolUseId,
1374
- isMistral
1375
- );
1376
- contentBlocks[blockIndex] = {
1377
- type: "tool-call",
1378
- toolCallId: normalizedToolCallId,
1379
- toolName: toolUse.name,
1380
- jsonText: "",
1381
- isJsonResponseTool
1382
- };
1383
- if (!isJsonResponseTool) {
1384
- controller.enqueue({
1385
- type: "tool-input-start",
1386
- id: normalizedToolCallId,
1387
- toolName: toolUse.name
1388
- });
1389
- }
1390
- }
1391
- const contentBlockDelta = value.contentBlockDelta;
1392
- if ((contentBlockDelta == null ? void 0 : contentBlockDelta.delta) && "toolUse" in contentBlockDelta.delta && contentBlockDelta.delta.toolUse) {
1393
- const blockIndex = contentBlockDelta.contentBlockIndex;
1394
- const contentBlock = contentBlocks[blockIndex];
1395
- if ((contentBlock == null ? void 0 : contentBlock.type) === "tool-call") {
1396
- const delta = (_p = contentBlockDelta.delta.toolUse.input) != null ? _p : "";
1397
- if (!contentBlock.isJsonResponseTool) {
1398
- controller.enqueue({
1399
- type: "tool-input-delta",
1400
- id: contentBlock.toolCallId,
1401
- delta
1402
- });
1403
- }
1404
- contentBlock.jsonText += delta;
1405
- }
1406
- }
1407
- },
1408
- flush(controller) {
1409
- if (isJsonResponseFromTool || stopSequence != null) {
1410
- if (providerMetadata) {
1411
- providerMetadata.bedrock = {
1412
- ...providerMetadata.bedrock,
1413
- ...isJsonResponseFromTool && {
1414
- isJsonResponseFromTool: true
1415
- },
1416
- stopSequence
1417
- };
1418
- } else {
1419
- providerMetadata = {
1420
- bedrock: {
1421
- ...isJsonResponseFromTool && {
1422
- isJsonResponseFromTool: true
1423
- },
1424
- stopSequence
1425
- }
1426
- };
1427
- }
1428
- }
1429
- controller.enqueue({
1430
- type: "finish",
1431
- finishReason,
1432
- usage: convertBedrockUsage(usage),
1433
- ...providerMetadata && { providerMetadata }
1434
- });
1435
- }
1436
- })
1437
- ),
1438
- // TODO request?
1439
- response: { headers: responseHeaders }
1440
- };
1441
- }
1442
- getUrl(modelId) {
1443
- const encodedModelId = encodeURIComponent(modelId);
1444
- return `${this.config.baseUrl()}/model/${encodedModelId}`;
1445
- }
1446
- };
1447
- var BedrockStopReasonSchema = z3.union([
1448
- z3.enum(BEDROCK_STOP_REASONS),
1449
- z3.string()
1450
- ]);
1451
- var BedrockAdditionalModelResponseFieldsSchema = z3.object({
1452
- delta: z3.object({
1453
- stop_sequence: z3.string().nullish()
1454
- }).nullish()
1455
- }).catchall(z3.unknown());
1456
- var BedrockToolUseSchema = z3.object({
1457
- toolUseId: z3.string(),
1458
- name: z3.string(),
1459
- input: z3.unknown()
1460
- });
1461
- var BedrockReasoningTextSchema = z3.object({
1462
- signature: z3.string().nullish(),
1463
- text: z3.string()
1464
- });
1465
- var BedrockRedactedReasoningSchema = z3.object({
1466
- data: z3.string()
1467
- });
1468
- var BedrockResponseSchema = z3.object({
1469
- metrics: z3.object({
1470
- latencyMs: z3.number()
1471
- }).nullish(),
1472
- output: z3.object({
1473
- message: z3.object({
1474
- content: z3.array(
1475
- z3.object({
1476
- text: z3.string().nullish(),
1477
- toolUse: BedrockToolUseSchema.nullish(),
1478
- reasoningContent: z3.union([
1479
- z3.object({
1480
- reasoningText: BedrockReasoningTextSchema
1481
- }),
1482
- z3.object({
1483
- redactedReasoning: BedrockRedactedReasoningSchema
1484
- })
1485
- ]).nullish()
1486
- })
1487
- ),
1488
- role: z3.string()
1489
- })
1490
- }),
1491
- stopReason: BedrockStopReasonSchema,
1492
- additionalModelResponseFields: BedrockAdditionalModelResponseFieldsSchema.nullish(),
1493
- trace: z3.unknown().nullish(),
1494
- performanceConfig: z3.object({ latency: z3.string() }).nullish(),
1495
- serviceTier: z3.object({ type: z3.string() }).nullish(),
1496
- usage: z3.object({
1497
- inputTokens: z3.number(),
1498
- outputTokens: z3.number(),
1499
- totalTokens: z3.number(),
1500
- cacheReadInputTokens: z3.number().nullish(),
1501
- cacheWriteInputTokens: z3.number().nullish(),
1502
- cacheDetails: z3.array(z3.object({ inputTokens: z3.number(), ttl: z3.string() })).nullish()
1503
- })
1504
- });
1505
- var BedrockStreamSchema = z3.object({
1506
- contentBlockDelta: z3.object({
1507
- contentBlockIndex: z3.number(),
1508
- delta: z3.union([
1509
- z3.object({ text: z3.string() }),
1510
- z3.object({ toolUse: z3.object({ input: z3.string() }) }),
1511
- z3.object({
1512
- reasoningContent: z3.object({ text: z3.string() })
1513
- }),
1514
- z3.object({
1515
- reasoningContent: z3.object({
1516
- signature: z3.string()
1517
- })
1518
- }),
1519
- z3.object({
1520
- reasoningContent: z3.object({ data: z3.string() })
1521
- })
1522
- ]).nullish()
1523
- }).nullish(),
1524
- contentBlockStart: z3.object({
1525
- contentBlockIndex: z3.number(),
1526
- start: z3.object({
1527
- toolUse: BedrockToolUseSchema.nullish()
1528
- }).nullish()
1529
- }).nullish(),
1530
- contentBlockStop: z3.object({
1531
- contentBlockIndex: z3.number()
1532
- }).nullish(),
1533
- internalServerException: z3.record(z3.string(), z3.unknown()).nullish(),
1534
- messageStop: z3.object({
1535
- additionalModelResponseFields: BedrockAdditionalModelResponseFieldsSchema.nullish(),
1536
- stopReason: BedrockStopReasonSchema
1537
- }).nullish(),
1538
- metadata: z3.object({
1539
- trace: z3.unknown().nullish(),
1540
- performanceConfig: z3.object({ latency: z3.string() }).nullish(),
1541
- serviceTier: z3.object({ type: z3.string() }).nullish(),
1542
- usage: z3.object({
1543
- cacheReadInputTokens: z3.number().nullish(),
1544
- cacheWriteInputTokens: z3.number().nullish(),
1545
- cacheDetails: z3.array(z3.object({ inputTokens: z3.number(), ttl: z3.string() })).nullish(),
1546
- inputTokens: z3.number(),
1547
- outputTokens: z3.number()
1548
- }).nullish()
1549
- }).nullish(),
1550
- modelStreamErrorException: z3.record(z3.string(), z3.unknown()).nullish(),
1551
- throttlingException: z3.record(z3.string(), z3.unknown()).nullish(),
1552
- validationException: z3.record(z3.string(), z3.unknown()).nullish()
1553
- });
1554
- var bedrockReasoningMetadataSchema = z3.object({
1555
- signature: z3.string().optional(),
1556
- redactedData: z3.string().optional()
1557
- });
1558
-
1559
- // src/bedrock-embedding-model.ts
1560
- import {
1561
- TooManyEmbeddingValuesForCallError
1562
- } from "@ai-sdk/provider";
1563
- import {
1564
- combineHeaders as combineHeaders2,
1565
- createJsonErrorResponseHandler as createJsonErrorResponseHandler2,
1566
- createJsonResponseHandler as createJsonResponseHandler2,
1567
- parseProviderOptions as parseProviderOptions3,
1568
- postJsonToApi as postJsonToApi2,
1569
- resolve as resolve2
1570
- } from "@ai-sdk/provider-utils";
1571
-
1572
- // src/bedrock-embedding-options.ts
1573
- import { z as z4 } from "zod/v4";
1574
- var amazonBedrockEmbeddingModelOptionsSchema = z4.object({
1575
- /**
1576
- * The number of dimensions the resulting output embeddings should have (defaults to 1024).
1577
- * Only supported in amazon.titan-embed-text-v2:0.
1578
- */
1579
- dimensions: z4.union([z4.literal(1024), z4.literal(512), z4.literal(256)]).optional(),
1580
- /**
1581
- * Flag indicating whether or not to normalize the output embeddings. Defaults to true.
1582
- * Only supported in amazon.titan-embed-text-v2:0.
1583
- */
1584
- normalize: z4.boolean().optional(),
1585
- /**
1586
- * The number of dimensions for Nova embedding models (defaults to 1024).
1587
- * Supported values: 256, 384, 1024, 3072.
1588
- * Only supported in amazon.nova-* embedding models.
1589
- */
1590
- embeddingDimension: z4.union([z4.literal(256), z4.literal(384), z4.literal(1024), z4.literal(3072)]).optional(),
1591
- /**
1592
- * The purpose of the embedding. Defaults to 'GENERIC_INDEX'.
1593
- * Only supported in amazon.nova-* embedding models.
1594
- */
1595
- embeddingPurpose: z4.enum([
1596
- "GENERIC_INDEX",
1597
- "TEXT_RETRIEVAL",
1598
- "IMAGE_RETRIEVAL",
1599
- "VIDEO_RETRIEVAL",
1600
- "DOCUMENT_RETRIEVAL",
1601
- "AUDIO_RETRIEVAL",
1602
- "GENERIC_RETRIEVAL",
1603
- "CLASSIFICATION",
1604
- "CLUSTERING"
1605
- ]).optional(),
1606
- /**
1607
- * Input type for Cohere embedding models on Bedrock.
1608
- * Common values: `search_document`, `search_query`, `classification`, `clustering`.
1609
- * If not set, the provider defaults to `search_query`.
1610
- */
1611
- inputType: z4.enum(["search_document", "search_query", "classification", "clustering"]).optional(),
1612
- /**
1613
- * Truncation behavior when input exceeds the model's context length.
1614
- * Supported in Cohere and Nova embedding models. Defaults to 'END' for Nova models.
1615
- */
1616
- truncate: z4.enum(["NONE", "START", "END"]).optional(),
1617
- /**
1618
- * The number of dimensions the resulting output embeddings should have (defaults to 1536).
1619
- * Only supported in cohere.embed-v4:0 and newer Cohere embedding models.
1620
- */
1621
- outputDimension: z4.union([z4.literal(256), z4.literal(512), z4.literal(1024), z4.literal(1536)]).optional()
1622
- });
1623
-
1624
- // src/bedrock-embedding-model.ts
1625
- import { z as z5 } from "zod/v4";
1626
- var BedrockEmbeddingModel = class {
1627
- constructor(modelId, config) {
1628
- this.modelId = modelId;
1629
- this.config = config;
1630
- this.specificationVersion = "v4";
1631
- this.provider = "amazon-bedrock";
1632
- this.maxEmbeddingsPerCall = 1;
1633
- this.supportsParallelCalls = true;
1634
- }
1635
- getUrl(modelId) {
1636
- const encodedModelId = encodeURIComponent(modelId);
1637
- return `${this.config.baseUrl()}/model/${encodedModelId}/invoke`;
1638
- }
1639
- async doEmbed({
1640
- values,
1641
- headers,
1642
- abortSignal,
1643
- providerOptions
1644
- }) {
1645
- var _a, _b, _c, _d, _e, _f;
1646
- if (values.length > this.maxEmbeddingsPerCall) {
1647
- throw new TooManyEmbeddingValuesForCallError({
1648
- provider: this.provider,
1649
- modelId: this.modelId,
1650
- maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
1651
- values
1652
- });
1653
- }
1654
- const bedrockOptions = (_a = await parseProviderOptions3({
1655
- provider: "bedrock",
1656
- providerOptions,
1657
- schema: amazonBedrockEmbeddingModelOptionsSchema
1658
- })) != null ? _a : {};
1659
- const isNovaModel = this.modelId.startsWith("amazon.nova-") && this.modelId.includes("embed");
1660
- const isCohereModel = this.modelId.startsWith("cohere.embed-");
1661
- const args = isNovaModel ? {
1662
- taskType: "SINGLE_EMBEDDING",
1663
- singleEmbeddingParams: {
1664
- embeddingPurpose: (_b = bedrockOptions.embeddingPurpose) != null ? _b : "GENERIC_INDEX",
1665
- embeddingDimension: (_c = bedrockOptions.embeddingDimension) != null ? _c : 1024,
1666
- text: {
1667
- truncationMode: (_d = bedrockOptions.truncate) != null ? _d : "END",
1668
- value: values[0]
1669
- }
1670
- }
1671
- } : isCohereModel ? {
1672
- // Cohere embedding models on Bedrock require `input_type`.
1673
- // Without it, the service attempts other schema branches and rejects the request.
1674
- input_type: (_e = bedrockOptions.inputType) != null ? _e : "search_query",
1675
- texts: [values[0]],
1676
- truncate: bedrockOptions.truncate,
1677
- output_dimension: bedrockOptions.outputDimension
1678
- } : {
1679
- inputText: values[0],
1680
- dimensions: bedrockOptions.dimensions,
1681
- normalize: bedrockOptions.normalize
1682
- };
1683
- const url = this.getUrl(this.modelId);
1684
- const { value: response } = await postJsonToApi2({
1685
- url,
1686
- headers: await resolve2(
1687
- combineHeaders2(await resolve2(this.config.headers), headers)
1688
- ),
1689
- body: args,
1690
- failedResponseHandler: createJsonErrorResponseHandler2({
1691
- errorSchema: BedrockErrorSchema,
1692
- errorToMessage: (error) => `${error.type}: ${error.message}`
1693
- }),
1694
- successfulResponseHandler: createJsonResponseHandler2(
1695
- BedrockEmbeddingResponseSchema
1696
- ),
1697
- fetch: this.config.fetch,
1698
- abortSignal
1699
- });
1700
- let embedding;
1701
- if ("embedding" in response) {
1702
- embedding = response.embedding;
1703
- } else if (Array.isArray(response.embeddings)) {
1704
- const firstEmbedding = response.embeddings[0];
1705
- if (typeof firstEmbedding === "object" && firstEmbedding !== null && "embeddingType" in firstEmbedding) {
1706
- embedding = firstEmbedding.embedding;
1707
- } else {
1708
- embedding = firstEmbedding;
1709
- }
1710
- } else {
1711
- embedding = response.embeddings.float[0];
1712
- }
1713
- const tokens = "inputTextTokenCount" in response ? response.inputTextTokenCount : "inputTokenCount" in response ? (_f = response.inputTokenCount) != null ? _f : 0 : NaN;
1714
- return {
1715
- embeddings: [embedding],
1716
- usage: { tokens },
1717
- warnings: []
1718
- };
1719
- }
1720
- };
1721
- var BedrockEmbeddingResponseSchema = z5.union([
1722
- // Titan-style response
1723
- z5.object({
1724
- embedding: z5.array(z5.number()),
1725
- inputTextTokenCount: z5.number()
1726
- }),
1727
- // Nova-style response
1728
- z5.object({
1729
- embeddings: z5.array(
1730
- z5.object({
1731
- embeddingType: z5.string(),
1732
- embedding: z5.array(z5.number())
1733
- })
1734
- ),
1735
- inputTokenCount: z5.number().optional()
1736
- }),
1737
- // Cohere v3-style response
1738
- z5.object({
1739
- embeddings: z5.array(z5.array(z5.number()))
1740
- }),
1741
- // Cohere v4-style response
1742
- z5.object({
1743
- embeddings: z5.object({
1744
- float: z5.array(z5.array(z5.number()))
1745
- })
1746
- })
1747
- ]);
1748
-
1749
- // src/bedrock-image-model.ts
1750
- import {
1751
- combineHeaders as combineHeaders3,
1752
- convertUint8ArrayToBase64,
1753
- createJsonErrorResponseHandler as createJsonErrorResponseHandler3,
1754
- createJsonResponseHandler as createJsonResponseHandler3,
1755
- postJsonToApi as postJsonToApi3,
1756
- resolve as resolve3
1757
- } from "@ai-sdk/provider-utils";
1758
-
1759
- // src/bedrock-image-settings.ts
1760
- var modelMaxImagesPerCall = {
1761
- "amazon.nova-canvas-v1:0": 5
1762
- };
1763
-
1764
- // src/bedrock-image-model.ts
1765
- import { z as z6 } from "zod/v4";
1766
- var BedrockImageModel = class {
1767
- constructor(modelId, config) {
1768
- this.modelId = modelId;
1769
- this.config = config;
1770
- this.specificationVersion = "v4";
1771
- this.provider = "amazon-bedrock";
1772
- }
1773
- get maxImagesPerCall() {
1774
- var _a;
1775
- return (_a = modelMaxImagesPerCall[this.modelId]) != null ? _a : 1;
1776
- }
1777
- getUrl(modelId) {
1778
- const encodedModelId = encodeURIComponent(modelId);
1779
- return `${this.config.baseUrl()}/model/${encodedModelId}/invoke`;
1780
- }
1781
- async doGenerate({
1782
- prompt,
1783
- n,
1784
- size,
1785
- aspectRatio,
1786
- seed,
1787
- providerOptions,
1788
- headers,
1789
- abortSignal,
1790
- files,
1791
- mask
1792
- }) {
1793
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
1794
- const warnings = [];
1795
- const [width, height] = size ? size.split("x").map(Number) : [];
1796
- const hasFiles = files != null && files.length > 0;
1797
- const imageGenerationConfig = {
1798
- ...width ? { width } : {},
1799
- ...height ? { height } : {},
1800
- ...seed ? { seed } : {},
1801
- ...n ? { numberOfImages: n } : {},
1802
- ...((_a = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _a.quality) ? { quality: providerOptions.bedrock.quality } : {},
1803
- ...((_b = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _b.cfgScale) ? { cfgScale: providerOptions.bedrock.cfgScale } : {}
1804
- };
1805
- let args;
1806
- if (hasFiles) {
1807
- const hasMask = (mask == null ? void 0 : mask.type) != null;
1808
- const hasMaskPrompt = ((_c = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _c.maskPrompt) != null;
1809
- const taskType = (_e = (_d = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _d.taskType) != null ? _e : hasMask || hasMaskPrompt ? "INPAINTING" : "IMAGE_VARIATION";
1810
- const sourceImageBase64 = getBase64Data(files[0]);
1811
- switch (taskType) {
1812
- case "INPAINTING": {
1813
- const inPaintingParams = {
1814
- image: sourceImageBase64,
1815
- ...prompt ? { text: prompt } : {},
1816
- ...((_f = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _f.negativeText) ? { negativeText: providerOptions.bedrock.negativeText } : {}
1817
- };
1818
- if (hasMask) {
1819
- inPaintingParams.maskImage = getBase64Data(mask);
1820
- } else if (hasMaskPrompt) {
1821
- inPaintingParams.maskPrompt = providerOptions.bedrock.maskPrompt;
1822
- }
1823
- args = {
1824
- taskType: "INPAINTING",
1825
- inPaintingParams,
1826
- imageGenerationConfig
1827
- };
1828
- break;
1829
- }
1830
- case "OUTPAINTING": {
1831
- const outPaintingParams = {
1832
- image: sourceImageBase64,
1833
- ...prompt ? { text: prompt } : {},
1834
- ...((_g = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _g.negativeText) ? { negativeText: providerOptions.bedrock.negativeText } : {},
1835
- ...((_h = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _h.outPaintingMode) ? { outPaintingMode: providerOptions.bedrock.outPaintingMode } : {}
1836
- };
1837
- if (hasMask) {
1838
- outPaintingParams.maskImage = getBase64Data(mask);
1839
- } else if (hasMaskPrompt) {
1840
- outPaintingParams.maskPrompt = providerOptions.bedrock.maskPrompt;
1841
- }
1842
- args = {
1843
- taskType: "OUTPAINTING",
1844
- outPaintingParams,
1845
- imageGenerationConfig
1846
- };
1847
- break;
1848
- }
1849
- case "BACKGROUND_REMOVAL": {
1850
- args = {
1851
- taskType: "BACKGROUND_REMOVAL",
1852
- backgroundRemovalParams: {
1853
- image: sourceImageBase64
1854
- }
1855
- };
1856
- break;
1857
- }
1858
- case "IMAGE_VARIATION": {
1859
- const images = files.map((file) => getBase64Data(file));
1860
- const imageVariationParams = {
1861
- images,
1862
- ...prompt ? { text: prompt } : {},
1863
- ...((_i = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _i.negativeText) ? { negativeText: providerOptions.bedrock.negativeText } : {},
1864
- ...((_j = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _j.similarityStrength) != null ? {
1865
- similarityStrength: providerOptions.bedrock.similarityStrength
1866
- } : {}
1867
- };
1868
- args = {
1869
- taskType: "IMAGE_VARIATION",
1870
- imageVariationParams,
1871
- imageGenerationConfig
1872
- };
1873
- break;
1874
- }
1875
- default:
1876
- throw new Error(`Unsupported task type: ${taskType}`);
1877
- }
1878
- } else {
1879
- args = {
1880
- taskType: "TEXT_IMAGE",
1881
- textToImageParams: {
1882
- text: prompt,
1883
- ...((_k = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _k.negativeText) ? {
1884
- negativeText: providerOptions.bedrock.negativeText
1885
- } : {},
1886
- ...((_l = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _l.style) ? {
1887
- style: providerOptions.bedrock.style
1888
- } : {}
1889
- },
1890
- imageGenerationConfig
1891
- };
1892
- }
1893
- if (aspectRatio != void 0) {
1894
- warnings.push({
1895
- type: "unsupported",
1896
- feature: "aspectRatio",
1897
- details: "This model does not support aspect ratio. Use `size` instead."
1898
- });
1899
- }
1900
- const currentDate = (_o = (_n = (_m = this.config._internal) == null ? void 0 : _m.currentDate) == null ? void 0 : _n.call(_m)) != null ? _o : /* @__PURE__ */ new Date();
1901
- const { value: response, responseHeaders } = await postJsonToApi3({
1902
- url: this.getUrl(this.modelId),
1903
- headers: await resolve3(
1904
- combineHeaders3(await resolve3(this.config.headers), headers)
1905
- ),
1906
- body: args,
1907
- failedResponseHandler: createJsonErrorResponseHandler3({
1908
- errorSchema: BedrockErrorSchema,
1909
- errorToMessage: (error) => `${error.type}: ${error.message}`
1910
- }),
1911
- successfulResponseHandler: createJsonResponseHandler3(
1912
- bedrockImageResponseSchema
1913
- ),
1914
- abortSignal,
1915
- fetch: this.config.fetch
1916
- });
1917
- if (response.status === "Request Moderated") {
1918
- const moderationReasons = (_p = response.details) == null ? void 0 : _p["Moderation Reasons"];
1919
- const reasons = Array.isArray(moderationReasons) ? moderationReasons : ["Unknown"];
1920
- throw new Error(
1921
- `Amazon Bedrock request was moderated: ${reasons.join(", ")}`
1922
- );
1923
- }
1924
- if (!response.images || response.images.length === 0) {
1925
- throw new Error(
1926
- "Amazon Bedrock returned no images. " + (response.status ? `Status: ${response.status}` : "")
1927
- );
1928
- }
1929
- return {
1930
- images: response.images,
1931
- warnings,
1932
- response: {
1933
- timestamp: currentDate,
1934
- modelId: this.modelId,
1935
- headers: responseHeaders
1936
- }
1937
- };
1938
- }
1939
- };
1940
- function getBase64Data(file) {
1941
- if (file.type === "url") {
1942
- throw new Error(
1943
- "URL-based images are not supported for Amazon Bedrock image editing. Please provide the image data directly."
1944
- );
1945
- }
1946
- if (file.data instanceof Uint8Array) {
1947
- return convertUint8ArrayToBase64(file.data);
1948
- }
1949
- return file.data;
1950
- }
1951
- var bedrockImageResponseSchema = z6.object({
1952
- // Normal successful response
1953
- images: z6.array(z6.string()).optional(),
1954
- // Moderation response fields
1955
- id: z6.string().optional(),
1956
- status: z6.string().optional(),
1957
- result: z6.unknown().optional(),
1958
- progress: z6.unknown().optional(),
1959
- details: z6.record(z6.string(), z6.unknown()).optional(),
1960
- preview: z6.unknown().optional()
1961
- });
1962
-
1963
- // src/bedrock-sigv4-fetch.ts
1964
- import {
1965
- combineHeaders as combineHeaders4,
1966
- normalizeHeaders,
1967
- withUserAgentSuffix,
1968
- getRuntimeEnvironmentUserAgent
1969
- } from "@ai-sdk/provider-utils";
1970
- import { AwsV4Signer } from "aws4fetch";
1971
-
1972
- // src/version.ts
1973
- var VERSION = true ? "5.0.0-beta.4" : "0.0.0-test";
1974
-
1975
- // src/bedrock-sigv4-fetch.ts
1976
- function createSigV4FetchFunction(getCredentials, fetch = globalThis.fetch) {
1977
- return async (input, init) => {
1978
- var _a, _b;
1979
- const request = input instanceof Request ? input : void 0;
1980
- const originalHeaders = combineHeaders4(
1981
- normalizeHeaders(request == null ? void 0 : request.headers),
1982
- normalizeHeaders(init == null ? void 0 : init.headers)
1983
- );
1984
- const headersWithUserAgent = withUserAgentSuffix(
1985
- originalHeaders,
1986
- `ai-sdk/amazon-bedrock/${VERSION}`,
1987
- getRuntimeEnvironmentUserAgent()
1988
- );
1989
- let effectiveBody = (_a = init == null ? void 0 : init.body) != null ? _a : void 0;
1990
- if (effectiveBody === void 0 && request && request.body !== null) {
1991
- try {
1992
- effectiveBody = await request.clone().text();
1993
- } catch (e) {
1994
- }
1995
- }
1996
- const effectiveMethod = (_b = init == null ? void 0 : init.method) != null ? _b : request == null ? void 0 : request.method;
1997
- if ((effectiveMethod == null ? void 0 : effectiveMethod.toUpperCase()) !== "POST" || !effectiveBody) {
1998
- return fetch(input, {
1999
- ...init,
2000
- headers: headersWithUserAgent
2001
- });
2002
- }
2003
- const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
2004
- const body = prepareBodyString(effectiveBody);
2005
- const credentials = await getCredentials();
2006
- const signer = new AwsV4Signer({
2007
- url,
2008
- method: "POST",
2009
- headers: Object.entries(headersWithUserAgent),
2010
- body,
2011
- region: credentials.region,
2012
- accessKeyId: credentials.accessKeyId,
2013
- secretAccessKey: credentials.secretAccessKey,
2014
- sessionToken: credentials.sessionToken,
2015
- service: "bedrock"
2016
- });
2017
- const signingResult = await signer.sign();
2018
- const signedHeaders = normalizeHeaders(signingResult.headers);
2019
- const combinedHeaders = combineHeaders4(headersWithUserAgent, signedHeaders);
2020
- return fetch(input, {
2021
- ...init,
2022
- body,
2023
- headers: combinedHeaders
2024
- });
2025
- };
2026
- }
2027
- function prepareBodyString(body) {
2028
- if (typeof body === "string") {
2029
- return body;
2030
- } else if (body instanceof Uint8Array) {
2031
- return new TextDecoder().decode(body);
2032
- } else if (body instanceof ArrayBuffer) {
2033
- return new TextDecoder().decode(new Uint8Array(body));
2034
- } else {
2035
- return JSON.stringify(body);
2036
- }
2037
- }
2038
- function createApiKeyFetchFunction(apiKey, fetch = globalThis.fetch) {
2039
- return async (input, init) => {
2040
- const originalHeaders = normalizeHeaders(init == null ? void 0 : init.headers);
2041
- const headersWithUserAgent = withUserAgentSuffix(
2042
- originalHeaders,
2043
- `ai-sdk/amazon-bedrock/${VERSION}`,
2044
- getRuntimeEnvironmentUserAgent()
2045
- );
2046
- const finalHeaders = combineHeaders4(headersWithUserAgent, {
2047
- Authorization: `Bearer ${apiKey}`
2048
- });
2049
- return fetch(input, {
2050
- ...init,
2051
- headers: finalHeaders
2052
- });
2053
- };
2054
- }
2055
-
2056
- // src/reranking/bedrock-reranking-model.ts
2057
- import {
2058
- combineHeaders as combineHeaders5,
2059
- createJsonErrorResponseHandler as createJsonErrorResponseHandler4,
2060
- createJsonResponseHandler as createJsonResponseHandler4,
2061
- parseProviderOptions as parseProviderOptions4,
2062
- postJsonToApi as postJsonToApi4,
2063
- resolve as resolve4
2064
- } from "@ai-sdk/provider-utils";
2065
-
2066
- // src/reranking/bedrock-reranking-api.ts
2067
- import { lazySchema, zodSchema } from "@ai-sdk/provider-utils";
2068
- import { z as z7 } from "zod/v4";
2069
- var bedrockRerankingResponseSchema = lazySchema(
2070
- () => zodSchema(
2071
- z7.object({
2072
- results: z7.array(
2073
- z7.object({
2074
- index: z7.number(),
2075
- relevanceScore: z7.number()
2076
- })
2077
- ),
2078
- nextToken: z7.string().optional()
2079
- })
2080
- )
2081
- );
2082
-
2083
- // src/reranking/bedrock-reranking-options.ts
2084
- import { lazySchema as lazySchema2, zodSchema as zodSchema2 } from "@ai-sdk/provider-utils";
2085
- import { z as z8 } from "zod/v4";
2086
- var amazonBedrockRerankingModelOptionsSchema = lazySchema2(
2087
- () => zodSchema2(
2088
- z8.object({
2089
- /**
2090
- * If the total number of results was greater than could fit in a response, a token is returned in the nextToken field. You can enter that token in this field to return the next batch of results.
2091
- */
2092
- nextToken: z8.string().optional(),
2093
- /**
2094
- * Additional model request fields to pass to the model.
2095
- */
2096
- additionalModelRequestFields: z8.record(z8.string(), z8.any()).optional()
2097
- })
2098
- )
2099
- );
2100
-
2101
- // src/reranking/bedrock-reranking-model.ts
2102
- var BedrockRerankingModel = class {
2103
- constructor(modelId, config) {
2104
- this.modelId = modelId;
2105
- this.config = config;
2106
- this.specificationVersion = "v4";
2107
- this.provider = "amazon-bedrock";
2108
- }
2109
- async doRerank({
2110
- documents,
2111
- headers,
2112
- query,
2113
- topN,
2114
- abortSignal,
2115
- providerOptions
2116
- }) {
2117
- const bedrockOptions = await parseProviderOptions4({
2118
- provider: "bedrock",
2119
- providerOptions,
2120
- schema: amazonBedrockRerankingModelOptionsSchema
2121
- });
2122
- const {
2123
- value: response,
2124
- responseHeaders,
2125
- rawValue
2126
- } = await postJsonToApi4({
2127
- url: `${this.config.baseUrl()}/rerank`,
2128
- headers: await resolve4(
2129
- combineHeaders5(await resolve4(this.config.headers), headers)
2130
- ),
2131
- body: {
2132
- nextToken: bedrockOptions == null ? void 0 : bedrockOptions.nextToken,
2133
- queries: [
2134
- {
2135
- textQuery: { text: query },
2136
- type: "TEXT"
2137
- }
2138
- ],
2139
- rerankingConfiguration: {
2140
- bedrockRerankingConfiguration: {
2141
- modelConfiguration: {
2142
- modelArn: `arn:aws:bedrock:${this.config.region}::foundation-model/${this.modelId}`,
2143
- additionalModelRequestFields: bedrockOptions == null ? void 0 : bedrockOptions.additionalModelRequestFields
2144
- },
2145
- numberOfResults: topN
2146
- },
2147
- type: "BEDROCK_RERANKING_MODEL"
2148
- },
2149
- sources: documents.values.map((value) => ({
2150
- type: "INLINE",
2151
- inlineDocumentSource: documents.type === "text" ? {
2152
- type: "TEXT",
2153
- textDocument: { text: value }
2154
- } : {
2155
- type: "JSON",
2156
- jsonDocument: value
2157
- }
2158
- }))
2159
- },
2160
- failedResponseHandler: createJsonErrorResponseHandler4({
2161
- errorSchema: BedrockErrorSchema,
2162
- errorToMessage: (error) => `${error.type}: ${error.message}`
2163
- }),
2164
- successfulResponseHandler: createJsonResponseHandler4(
2165
- bedrockRerankingResponseSchema
2166
- ),
2167
- fetch: this.config.fetch,
2168
- abortSignal
2169
- });
2170
- return {
2171
- ranking: response.results,
2172
- response: {
2173
- headers: responseHeaders,
2174
- body: rawValue
2175
- }
2176
- };
2177
- }
2178
- };
2179
-
2180
- // src/bedrock-provider.ts
2181
- function createAmazonBedrock(options = {}) {
2182
- const rawApiKey = loadOptionalSetting({
2183
- settingValue: options.apiKey,
2184
- environmentVariableName: "AWS_BEARER_TOKEN_BEDROCK"
2185
- });
2186
- const apiKey = rawApiKey && rawApiKey.trim().length > 0 ? rawApiKey.trim() : void 0;
2187
- const fetchFunction = apiKey ? createApiKeyFetchFunction(apiKey, options.fetch) : createSigV4FetchFunction(async () => {
2188
- const region = loadSetting({
2189
- settingValue: options.region,
2190
- settingName: "region",
2191
- environmentVariableName: "AWS_REGION",
2192
- description: "AWS region"
2193
- });
2194
- if (options.credentialProvider) {
2195
- try {
2196
- return {
2197
- ...await options.credentialProvider(),
2198
- region
2199
- };
2200
- } catch (error) {
2201
- const errorMessage = error instanceof Error ? error.message : String(error);
2202
- throw new Error(
2203
- `AWS credential provider failed: ${errorMessage}. Please ensure your credential provider returns valid AWS credentials with accessKeyId and secretAccessKey properties.`
2204
- );
2205
- }
2206
- }
2207
- try {
2208
- return {
2209
- region,
2210
- accessKeyId: loadSetting({
2211
- settingValue: options.accessKeyId,
2212
- settingName: "accessKeyId",
2213
- environmentVariableName: "AWS_ACCESS_KEY_ID",
2214
- description: "AWS access key ID"
2215
- }),
2216
- secretAccessKey: loadSetting({
2217
- settingValue: options.secretAccessKey,
2218
- settingName: "secretAccessKey",
2219
- environmentVariableName: "AWS_SECRET_ACCESS_KEY",
2220
- description: "AWS secret access key"
2221
- }),
2222
- sessionToken: loadOptionalSetting({
2223
- settingValue: options.sessionToken,
2224
- environmentVariableName: "AWS_SESSION_TOKEN"
2225
- })
2226
- };
2227
- } catch (error) {
2228
- const errorMessage = error instanceof Error ? error.message : String(error);
2229
- if (errorMessage.includes("AWS_ACCESS_KEY_ID") || errorMessage.includes("accessKeyId")) {
2230
- throw new Error(
2231
- `AWS SigV4 authentication requires AWS credentials. Please provide either:
2232
- 1. Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables
2233
- 2. Provide accessKeyId and secretAccessKey in options
2234
- 3. Use a credentialProvider function
2235
- 4. Use API key authentication with AWS_BEARER_TOKEN_BEDROCK or apiKey option
2236
- Original error: ${errorMessage}`
2237
- );
2238
- }
2239
- if (errorMessage.includes("AWS_SECRET_ACCESS_KEY") || errorMessage.includes("secretAccessKey")) {
2240
- throw new Error(
2241
- `AWS SigV4 authentication requires both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. Please ensure both credentials are provided.
2242
- Original error: ${errorMessage}`
2243
- );
2244
- }
2245
- throw error;
2246
- }
2247
- }, options.fetch);
2248
- const getHeaders = () => {
2249
- var _a;
2250
- const baseHeaders = (_a = options.headers) != null ? _a : {};
2251
- return withUserAgentSuffix2(baseHeaders, `ai-sdk/amazon-bedrock/${VERSION}`);
2252
- };
2253
- const getBedrockRuntimeBaseUrl = () => {
2254
- var _a, _b;
2255
- return (_b = withoutTrailingSlash(
2256
- (_a = options.baseURL) != null ? _a : `https://bedrock-runtime.${loadSetting({
2257
- settingValue: options.region,
2258
- settingName: "region",
2259
- environmentVariableName: "AWS_REGION",
2260
- description: "AWS region"
2261
- })}.amazonaws.com`
2262
- )) != null ? _b : `https://bedrock-runtime.us-east-1.amazonaws.com`;
2263
- };
2264
- const getBedrockAgentRuntimeBaseUrl = () => {
2265
- var _a, _b;
2266
- return (_b = withoutTrailingSlash(
2267
- (_a = options.baseURL) != null ? _a : `https://bedrock-agent-runtime.${loadSetting({
2268
- settingValue: options.region,
2269
- settingName: "region",
2270
- environmentVariableName: "AWS_REGION",
2271
- description: "AWS region"
2272
- })}.amazonaws.com`
2273
- )) != null ? _b : `https://bedrock-agent-runtime.us-west-2.amazonaws.com`;
2274
- };
2275
- const createChatModel = (modelId) => new BedrockChatLanguageModel(modelId, {
2276
- baseUrl: getBedrockRuntimeBaseUrl,
2277
- headers: getHeaders,
2278
- fetch: fetchFunction,
2279
- generateId
2280
- });
2281
- const provider = function(modelId) {
2282
- if (new.target) {
2283
- throw new Error(
2284
- "The Amazon Bedrock model function cannot be called with the new keyword."
2285
- );
2286
- }
2287
- return createChatModel(modelId);
2288
- };
2289
- const createEmbeddingModel = (modelId) => new BedrockEmbeddingModel(modelId, {
2290
- baseUrl: getBedrockRuntimeBaseUrl,
2291
- headers: getHeaders,
2292
- fetch: fetchFunction
2293
- });
2294
- const createImageModel = (modelId) => new BedrockImageModel(modelId, {
2295
- baseUrl: getBedrockRuntimeBaseUrl,
2296
- headers: getHeaders,
2297
- fetch: fetchFunction
2298
- });
2299
- const createRerankingModel = (modelId) => new BedrockRerankingModel(modelId, {
2300
- baseUrl: getBedrockAgentRuntimeBaseUrl,
2301
- region: loadSetting({
2302
- settingValue: options.region,
2303
- settingName: "region",
2304
- environmentVariableName: "AWS_REGION",
2305
- description: "AWS region"
2306
- }),
2307
- headers: getHeaders,
2308
- fetch: fetchFunction
2309
- });
2310
- provider.specificationVersion = "v4";
2311
- provider.languageModel = createChatModel;
2312
- provider.embedding = createEmbeddingModel;
2313
- provider.embeddingModel = createEmbeddingModel;
2314
- provider.textEmbedding = createEmbeddingModel;
2315
- provider.textEmbeddingModel = createEmbeddingModel;
2316
- provider.image = createImageModel;
2317
- provider.imageModel = createImageModel;
2318
- provider.reranking = createRerankingModel;
2319
- provider.rerankingModel = createRerankingModel;
2320
- provider.tools = anthropicTools2;
2321
- return provider;
2322
- }
2323
- var bedrock = createAmazonBedrock();
2324
- export {
2325
- VERSION,
2326
- bedrock,
2327
- createAmazonBedrock
2328
- };
2329
- //# sourceMappingURL=index.mjs.map