@ai-sdk/amazon-bedrock 3.0.0-canary.9 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -5,17 +5,18 @@ import {
5
5
  loadSetting,
6
6
  withoutTrailingSlash
7
7
  } from "@ai-sdk/provider-utils";
8
+ import { anthropicTools as anthropicTools2 } from "@ai-sdk/anthropic/internal";
8
9
 
9
10
  // src/bedrock-chat-language-model.ts
10
11
  import {
11
12
  combineHeaders,
12
13
  createJsonErrorResponseHandler,
13
14
  createJsonResponseHandler,
14
- parseProviderOptions,
15
+ parseProviderOptions as parseProviderOptions2,
15
16
  postJsonToApi,
16
17
  resolve
17
18
  } from "@ai-sdk/provider-utils";
18
- import { z as z3 } from "zod";
19
+ import { z as z3 } from "zod/v4";
19
20
 
20
21
  // src/bedrock-api-types.ts
21
22
  var BEDROCK_CACHE_POINT = {
@@ -33,24 +34,41 @@ var BEDROCK_STOP_REASONS = [
33
34
  "tool-calls",
34
35
  "tool_use"
35
36
  ];
37
+ var BEDROCK_IMAGE_MIME_TYPES = {
38
+ "image/jpeg": "jpeg",
39
+ "image/png": "png",
40
+ "image/gif": "gif",
41
+ "image/webp": "webp"
42
+ };
43
+ var BEDROCK_DOCUMENT_MIME_TYPES = {
44
+ "application/pdf": "pdf",
45
+ "text/csv": "csv",
46
+ "application/msword": "doc",
47
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
48
+ "application/vnd.ms-excel": "xls",
49
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
50
+ "text/html": "html",
51
+ "text/plain": "txt",
52
+ "text/markdown": "md"
53
+ };
36
54
 
37
55
  // src/bedrock-chat-options.ts
38
- import { z } from "zod";
56
+ import { z } from "zod/v4";
39
57
  var bedrockProviderOptions = z.object({
40
58
  /**
41
59
  * Additional inference parameters that the model supports,
42
60
  * beyond the base set of inference parameters that Converse
43
61
  * supports in the inferenceConfig field
44
62
  */
45
- additionalModelRequestFields: z.record(z.any()).optional(),
63
+ additionalModelRequestFields: z.record(z.string(), z.any()).optional(),
46
64
  reasoningConfig: z.object({
47
- type: z.union([z.literal("enabled"), z.literal("disabled")]).nullish(),
48
- budgetTokens: z.number().nullish()
49
- }).nullish()
65
+ type: z.union([z.literal("enabled"), z.literal("disabled")]).optional(),
66
+ budgetTokens: z.number().optional()
67
+ }).optional()
50
68
  });
51
69
 
52
70
  // src/bedrock-error.ts
53
- import { z as z2 } from "zod";
71
+ import { z as z2 } from "zod/v4";
54
72
  var BedrockErrorSchema = z2.object({
55
73
  message: z2.string(),
56
74
  type: z2.string().nullish()
@@ -77,7 +95,7 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
77
95
  responseHeaders,
78
96
  value: response.body.pipeThrough(
79
97
  new TransformStream({
80
- transform(chunk, controller) {
98
+ async transform(chunk, controller) {
81
99
  var _a, _b;
82
100
  const newBuffer = new Uint8Array(buffer.length + chunk.length);
83
101
  newBuffer.set(buffer);
@@ -98,7 +116,7 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
98
116
  buffer = buffer.slice(totalLength);
99
117
  if (((_a = decoded.headers[":message-type"]) == null ? void 0 : _a.value) === "event") {
100
118
  const data = textDecoder.decode(decoded.body);
101
- const parsedDataResult = safeParseJSON({ text: data });
119
+ const parsedDataResult = await safeParseJSON({ text: data });
102
120
  if (!parsedDataResult.success) {
103
121
  controller.enqueue(parsedDataResult);
104
122
  break;
@@ -107,7 +125,7 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
107
125
  let wrappedData = {
108
126
  [(_b = decoded.headers[":event-type"]) == null ? void 0 : _b.value]: parsedDataResult.value
109
127
  };
110
- const validatedWrappedData = safeValidateTypes({
128
+ const validatedWrappedData = await safeValidateTypes({
111
129
  value: wrappedData,
112
130
  schema: chunkSchema
113
131
  });
@@ -135,89 +153,160 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
135
153
  import {
136
154
  UnsupportedFunctionalityError
137
155
  } from "@ai-sdk/provider";
156
+ import { asSchema } from "@ai-sdk/provider-utils";
157
+ import {
158
+ anthropicTools,
159
+ prepareTools as prepareAnthropicTools
160
+ } from "@ai-sdk/anthropic/internal";
138
161
  function prepareTools({
139
162
  tools,
140
- toolChoice
163
+ toolChoice,
164
+ modelId
141
165
  }) {
142
- tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
143
- if (tools == null) {
166
+ const toolWarnings = [];
167
+ const betas = /* @__PURE__ */ new Set();
168
+ if (tools == null || tools.length === 0) {
144
169
  return {
145
- toolConfig: { tools: void 0, toolChoice: void 0 },
146
- toolWarnings: []
170
+ toolConfig: {},
171
+ additionalTools: void 0,
172
+ betas,
173
+ toolWarnings
147
174
  };
148
175
  }
149
- const toolWarnings = [];
150
- const bedrockTools = [];
151
- for (const tool of tools) {
152
- if (tool.type === "provider-defined") {
153
- toolWarnings.push({ type: "unsupported-tool", tool });
154
- } else {
155
- bedrockTools.push({
156
- toolSpec: {
157
- name: tool.name,
158
- description: tool.description,
159
- inputSchema: {
160
- json: tool.parameters
161
- }
162
- }
176
+ const supportedTools = tools.filter((tool) => {
177
+ if (tool.type === "provider-defined" && tool.id === "anthropic.web_search_20250305") {
178
+ toolWarnings.push({
179
+ type: "unsupported-tool",
180
+ tool,
181
+ details: "The web_search_20250305 tool is not supported on Amazon Bedrock."
163
182
  });
183
+ return false;
164
184
  }
165
- }
166
- if (toolChoice == null) {
185
+ return true;
186
+ });
187
+ if (supportedTools.length === 0) {
167
188
  return {
168
- toolConfig: { tools: bedrockTools, toolChoice: void 0 },
189
+ toolConfig: {},
190
+ additionalTools: void 0,
191
+ betas,
169
192
  toolWarnings
170
193
  };
171
194
  }
172
- const type = toolChoice.type;
173
- switch (type) {
174
- case "auto":
175
- return {
176
- toolConfig: { tools: bedrockTools, toolChoice: { auto: {} } },
177
- toolWarnings
178
- };
179
- case "required":
180
- return {
181
- toolConfig: { tools: bedrockTools, toolChoice: { any: {} } },
182
- toolWarnings
183
- };
184
- case "none":
185
- return {
186
- toolConfig: { tools: void 0, toolChoice: void 0 },
187
- toolWarnings
188
- };
189
- case "tool":
190
- return {
191
- toolConfig: {
192
- tools: bedrockTools,
193
- toolChoice: { tool: { name: toolChoice.toolName } }
194
- },
195
- toolWarnings
195
+ const isAnthropicModel = modelId.includes("anthropic.");
196
+ const providerDefinedTools = supportedTools.filter(
197
+ (t) => t.type === "provider-defined"
198
+ );
199
+ const functionTools = supportedTools.filter((t) => t.type === "function");
200
+ let additionalTools = void 0;
201
+ const bedrockTools = [];
202
+ const usingAnthropicTools = isAnthropicModel && providerDefinedTools.length > 0;
203
+ if (usingAnthropicTools) {
204
+ if (functionTools.length > 0) {
205
+ toolWarnings.push({
206
+ type: "unsupported-setting",
207
+ setting: "tools",
208
+ details: "Mixed Anthropic provider-defined tools and standard function tools are not supported in a single call to Bedrock. Only Anthropic tools will be used."
209
+ });
210
+ }
211
+ const {
212
+ toolChoice: preparedAnthropicToolChoice,
213
+ toolWarnings: anthropicToolWarnings,
214
+ betas: anthropicBetas
215
+ } = prepareAnthropicTools({
216
+ tools: providerDefinedTools,
217
+ toolChoice
218
+ });
219
+ toolWarnings.push(...anthropicToolWarnings);
220
+ anthropicBetas.forEach((beta) => betas.add(beta));
221
+ if (preparedAnthropicToolChoice) {
222
+ additionalTools = {
223
+ tool_choice: preparedAnthropicToolChoice
196
224
  };
197
- default: {
198
- const _exhaustiveCheck = type;
199
- throw new UnsupportedFunctionalityError({
200
- functionality: `tool choice type: ${_exhaustiveCheck}`
225
+ }
226
+ for (const tool of providerDefinedTools) {
227
+ const toolFactory = Object.values(anthropicTools).find((factory) => {
228
+ const instance = factory({});
229
+ return instance.id === tool.id;
201
230
  });
231
+ if (toolFactory != null) {
232
+ const fullToolDefinition = toolFactory({});
233
+ bedrockTools.push({
234
+ toolSpec: {
235
+ name: tool.name,
236
+ inputSchema: {
237
+ json: asSchema(fullToolDefinition.inputSchema).jsonSchema
238
+ }
239
+ }
240
+ });
241
+ } else {
242
+ toolWarnings.push({ type: "unsupported-tool", tool });
243
+ }
244
+ }
245
+ } else {
246
+ for (const tool of providerDefinedTools) {
247
+ toolWarnings.push({ type: "unsupported-tool", tool });
248
+ }
249
+ }
250
+ for (const tool of functionTools) {
251
+ bedrockTools.push({
252
+ toolSpec: {
253
+ name: tool.name,
254
+ description: tool.description,
255
+ inputSchema: {
256
+ json: tool.inputSchema
257
+ }
258
+ }
259
+ });
260
+ }
261
+ let bedrockToolChoice = void 0;
262
+ if (!usingAnthropicTools && bedrockTools.length > 0 && toolChoice) {
263
+ const type = toolChoice.type;
264
+ switch (type) {
265
+ case "auto":
266
+ bedrockToolChoice = { auto: {} };
267
+ break;
268
+ case "required":
269
+ bedrockToolChoice = { any: {} };
270
+ break;
271
+ case "none":
272
+ bedrockTools.length = 0;
273
+ bedrockToolChoice = void 0;
274
+ break;
275
+ case "tool":
276
+ bedrockToolChoice = { tool: { name: toolChoice.toolName } };
277
+ break;
278
+ default: {
279
+ const _exhaustiveCheck = type;
280
+ throw new UnsupportedFunctionalityError({
281
+ functionality: `tool choice type: ${_exhaustiveCheck}`
282
+ });
283
+ }
202
284
  }
203
285
  }
286
+ const toolConfig = bedrockTools.length > 0 ? { tools: bedrockTools, toolChoice: bedrockToolChoice } : {};
287
+ return {
288
+ toolConfig,
289
+ additionalTools,
290
+ betas,
291
+ toolWarnings
292
+ };
204
293
  }
205
294
 
206
295
  // src/convert-to-bedrock-chat-messages.ts
207
296
  import {
208
297
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
209
298
  } from "@ai-sdk/provider";
210
- import { convertToBase64, createIdGenerator } from "@ai-sdk/provider-utils";
211
- var generateFileId = createIdGenerator({ prefix: "file", size: 16 });
299
+ import { convertToBase64, parseProviderOptions } from "@ai-sdk/provider-utils";
212
300
  function getCachePoint(providerMetadata) {
213
301
  var _a;
214
302
  return (_a = providerMetadata == null ? void 0 : providerMetadata.bedrock) == null ? void 0 : _a.cachePoint;
215
303
  }
216
- function convertToBedrockChatMessages(prompt) {
217
- var _a, _b, _c, _d;
304
+ async function convertToBedrockChatMessages(prompt) {
218
305
  const blocks = groupIntoBlocks(prompt);
219
306
  let system = [];
220
307
  const messages = [];
308
+ let documentCounter = 0;
309
+ const generateDocumentName = () => `document-${++documentCounter}`;
221
310
  for (let i = 0; i < blocks.length; i++) {
222
311
  const block = blocks[i];
223
312
  const isLastBlock = i === blocks.length - 1;
@@ -259,20 +348,23 @@ function convertToBedrockChatMessages(prompt) {
259
348
  });
260
349
  }
261
350
  if (part.mediaType.startsWith("image/")) {
262
- const bedrockImageFormat = part.mediaType === "image/*" ? void 0 : (_b = (_a = part.mediaType) == null ? void 0 : _a.split("/")) == null ? void 0 : _b[1];
263
351
  bedrockContent.push({
264
352
  image: {
265
- format: bedrockImageFormat,
353
+ format: getBedrockImageFormat(part.mediaType),
266
354
  source: { bytes: convertToBase64(part.data) }
267
355
  }
268
356
  });
269
357
  } else {
358
+ if (!part.mediaType) {
359
+ throw new UnsupportedFunctionalityError2({
360
+ functionality: "file without mime type",
361
+ message: "File mime type is required in user message part content"
362
+ });
363
+ }
270
364
  bedrockContent.push({
271
365
  document: {
272
- format: (_d = (_c = part.mediaType) == null ? void 0 : _c.split(
273
- "/"
274
- )) == null ? void 0 : _d[1],
275
- name: generateFileId(),
366
+ format: getBedrockDocumentFormat(part.mediaType),
367
+ name: generateDocumentName(),
276
368
  source: { bytes: convertToBase64(part.data) }
277
369
  }
278
370
  });
@@ -284,36 +376,46 @@ function convertToBedrockChatMessages(prompt) {
284
376
  break;
285
377
  }
286
378
  case "tool": {
287
- for (let i2 = 0; i2 < content.length; i2++) {
288
- const part = content[i2];
289
- const toolResultContent = part.content != void 0 ? part.content.map((part2) => {
290
- switch (part2.type) {
291
- case "text":
292
- return {
293
- text: part2.text
294
- };
295
- case "image":
296
- if (!part2.mediaType) {
297
- throw new Error(
298
- "Image mime type is required in tool result part content"
299
- );
300
- }
301
- const format = part2.mediaType.split("/")[1];
302
- if (!isBedrockImageFormat(format)) {
303
- throw new Error(
304
- `Unsupported image format: ${format}`
305
- );
306
- }
307
- return {
308
- image: {
309
- format,
310
- source: {
311
- bytes: part2.data
379
+ for (const part of content) {
380
+ let toolResultContent;
381
+ const output = part.output;
382
+ switch (output.type) {
383
+ case "content": {
384
+ toolResultContent = output.value.map((contentPart) => {
385
+ switch (contentPart.type) {
386
+ case "text":
387
+ return { text: contentPart.text };
388
+ case "media":
389
+ if (!contentPart.mediaType.startsWith("image/")) {
390
+ throw new UnsupportedFunctionalityError2({
391
+ functionality: `media type: ${contentPart.mediaType}`
392
+ });
312
393
  }
313
- }
314
- };
394
+ const format = getBedrockImageFormat(
395
+ contentPart.mediaType
396
+ );
397
+ return {
398
+ image: {
399
+ format,
400
+ source: { bytes: contentPart.data }
401
+ }
402
+ };
403
+ }
404
+ });
405
+ break;
315
406
  }
316
- }) : [{ text: JSON.stringify(part.result) }];
407
+ case "text":
408
+ case "error-text":
409
+ toolResultContent = [{ text: output.value }];
410
+ break;
411
+ case "json":
412
+ case "error-json":
413
+ default:
414
+ toolResultContent = [
415
+ { text: JSON.stringify(output.value) }
416
+ ];
417
+ break;
418
+ }
317
419
  bedrockContent.push({
318
420
  toolResult: {
319
421
  toolUseId: part.toolCallId,
@@ -362,32 +464,39 @@ function convertToBedrockChatMessages(prompt) {
362
464
  break;
363
465
  }
364
466
  case "reasoning": {
365
- bedrockContent.push({
366
- reasoningContent: {
367
- reasoningText: {
368
- // trim the last text part if it's the last message in the block
369
- // because Bedrock does not allow trailing whitespace
370
- // in pre-filled assistant responses
371
- text: trimIfLast(
372
- isLastBlock,
373
- isLastMessage,
374
- isLastContentPart,
375
- part.text
376
- ),
377
- signature: part.signature
378
- }
379
- }
467
+ const reasoningMetadata = await parseProviderOptions({
468
+ provider: "bedrock",
469
+ providerOptions: part.providerOptions,
470
+ schema: bedrockReasoningMetadataSchema
380
471
  });
381
- break;
382
- }
383
- case "redacted-reasoning": {
384
- bedrockContent.push({
385
- reasoningContent: {
386
- redactedReasoning: {
387
- data: part.data
388
- }
472
+ if (reasoningMetadata != null) {
473
+ if (reasoningMetadata.signature != null) {
474
+ bedrockContent.push({
475
+ reasoningContent: {
476
+ reasoningText: {
477
+ // trim the last text part if it's the last message in the block
478
+ // because Bedrock does not allow trailing whitespace
479
+ // in pre-filled assistant responses
480
+ text: trimIfLast(
481
+ isLastBlock,
482
+ isLastMessage,
483
+ isLastContentPart,
484
+ part.text
485
+ ),
486
+ signature: reasoningMetadata.signature
487
+ }
488
+ }
489
+ });
490
+ } else if (reasoningMetadata.redactedData != null) {
491
+ bedrockContent.push({
492
+ reasoningContent: {
493
+ redactedReasoning: {
494
+ data: reasoningMetadata.redactedData
495
+ }
496
+ }
497
+ });
389
498
  }
390
- });
499
+ }
391
500
  break;
392
501
  }
393
502
  case "tool-call": {
@@ -395,7 +504,7 @@ function convertToBedrockChatMessages(prompt) {
395
504
  toolUse: {
396
505
  toolUseId: part.toolCallId,
397
506
  name: part.toolName,
398
- input: part.args
507
+ input: part.input
399
508
  }
400
509
  });
401
510
  break;
@@ -417,8 +526,31 @@ function convertToBedrockChatMessages(prompt) {
417
526
  }
418
527
  return { system, messages };
419
528
  }
420
- function isBedrockImageFormat(format) {
421
- return ["jpeg", "png", "gif"].includes(format);
529
+ function getBedrockImageFormat(mimeType) {
530
+ if (!mimeType) {
531
+ throw new UnsupportedFunctionalityError2({
532
+ functionality: "image without mime type",
533
+ message: "Image mime type is required in user message part content"
534
+ });
535
+ }
536
+ const format = BEDROCK_IMAGE_MIME_TYPES[mimeType];
537
+ if (!format) {
538
+ throw new UnsupportedFunctionalityError2({
539
+ functionality: `image mime type: ${mimeType}`,
540
+ message: `Unsupported image mime type: ${mimeType}, expected one of: ${Object.keys(BEDROCK_IMAGE_MIME_TYPES).join(", ")}`
541
+ });
542
+ }
543
+ return format;
544
+ }
545
+ function getBedrockDocumentFormat(mimeType) {
546
+ const format = BEDROCK_DOCUMENT_MIME_TYPES[mimeType];
547
+ if (!format) {
548
+ throw new UnsupportedFunctionalityError2({
549
+ functionality: `file mime type: ${mimeType}`,
550
+ message: `Unsupported file mime type: ${mimeType}, expected one of: ${Object.keys(BEDROCK_DOCUMENT_MIME_TYPES).join(", ")}`
551
+ });
552
+ }
553
+ return format;
422
554
  }
423
555
  function trimIfLast(isLastBlock, isLastMessage, isLastContentPart, text) {
424
556
  return isLastBlock && isLastMessage && isLastContentPart ? text.trim() : text;
@@ -495,8 +627,11 @@ var BedrockChatLanguageModel = class {
495
627
  this.config = config;
496
628
  this.specificationVersion = "v2";
497
629
  this.provider = "amazon-bedrock";
630
+ this.supportedUrls = {
631
+ // no supported urls for bedrock
632
+ };
498
633
  }
499
- getArgs({
634
+ async getArgs({
500
635
  prompt,
501
636
  maxOutputTokens,
502
637
  temperature,
@@ -511,8 +646,8 @@ var BedrockChatLanguageModel = class {
511
646
  toolChoice,
512
647
  providerOptions
513
648
  }) {
514
- var _a, _b, _c, _d, _e;
515
- const bedrockOptions = (_a = parseProviderOptions({
649
+ var _a, _b, _c, _d, _e, _f;
650
+ const bedrockOptions = (_a = await parseProviderOptions2({
516
651
  provider: "bedrock",
517
652
  providerOptions,
518
653
  schema: bedrockProviderOptions
@@ -542,14 +677,39 @@ var BedrockChatLanguageModel = class {
542
677
  setting: "topK"
543
678
  });
544
679
  }
545
- if (responseFormat != null && responseFormat.type !== "text") {
680
+ if (responseFormat != null && responseFormat.type !== "text" && responseFormat.type !== "json") {
546
681
  warnings.push({
547
682
  type: "unsupported-setting",
548
683
  setting: "responseFormat",
549
- details: "JSON response format is not supported."
684
+ details: "Only text and json response formats are supported."
550
685
  });
551
686
  }
552
- const { system, messages } = convertToBedrockChatMessages(prompt);
687
+ if (tools != null && (responseFormat == null ? void 0 : responseFormat.type) === "json") {
688
+ if (tools.length > 0) {
689
+ warnings.push({
690
+ type: "other",
691
+ message: "JSON response format does not support tools. The provided tools are ignored."
692
+ });
693
+ }
694
+ }
695
+ const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
696
+ type: "function",
697
+ name: "json",
698
+ description: "Respond with a JSON object.",
699
+ inputSchema: responseFormat.schema
700
+ } : void 0;
701
+ const { toolConfig, additionalTools, toolWarnings, betas } = prepareTools({
702
+ tools: jsonResponseTool ? [jsonResponseTool, ...tools != null ? tools : []] : tools,
703
+ toolChoice: jsonResponseTool != null ? { type: "tool", toolName: jsonResponseTool.name } : toolChoice,
704
+ modelId: this.modelId
705
+ });
706
+ warnings.push(...toolWarnings);
707
+ if (additionalTools) {
708
+ bedrockOptions.additionalModelRequestFields = {
709
+ ...bedrockOptions.additionalModelRequestFields,
710
+ ...additionalTools
711
+ };
712
+ }
553
713
  const isThinking = ((_b = bedrockOptions.reasoningConfig) == null ? void 0 : _b.type) === "enabled";
554
714
  const thinkingBudget = (_c = bedrockOptions.reasoningConfig) == null ? void 0 : _c.budgetTokens;
555
715
  const inferenceConfig = {
@@ -566,7 +726,7 @@ var BedrockChatLanguageModel = class {
566
726
  }
567
727
  bedrockOptions.additionalModelRequestFields = {
568
728
  ...bedrockOptions.additionalModelRequestFields,
569
- reasoningConfig: {
729
+ thinking: {
570
730
  type: (_d = bedrockOptions.reasoningConfig) == null ? void 0 : _d.type,
571
731
  budget_tokens: thinkingBudget
572
732
  }
@@ -588,7 +748,34 @@ var BedrockChatLanguageModel = class {
588
748
  details: "topP is not supported when thinking is enabled"
589
749
  });
590
750
  }
591
- const { toolConfig, toolWarnings } = prepareTools({ tools, toolChoice });
751
+ const hasAnyTools = ((_f = (_e = toolConfig.tools) == null ? void 0 : _e.length) != null ? _f : 0) > 0 || additionalTools;
752
+ let filteredPrompt = prompt;
753
+ if (!hasAnyTools) {
754
+ const hasToolContent = prompt.some(
755
+ (message) => "content" in message && Array.isArray(message.content) && message.content.some(
756
+ (part) => part.type === "tool-call" || part.type === "tool-result"
757
+ )
758
+ );
759
+ if (hasToolContent) {
760
+ filteredPrompt = prompt.map(
761
+ (message) => message.role === "system" ? message : {
762
+ ...message,
763
+ content: message.content.filter(
764
+ (part) => part.type !== "tool-call" && part.type !== "tool-result"
765
+ )
766
+ }
767
+ ).filter(
768
+ (message) => message.role === "system" || message.content.length > 0
769
+ );
770
+ warnings.push({
771
+ type: "unsupported-setting",
772
+ setting: "toolContent",
773
+ details: "Tool calls and results removed from conversation because Bedrock does not support tool content without active tools."
774
+ });
775
+ }
776
+ }
777
+ const { system, messages } = await convertToBedrockChatMessages(filteredPrompt);
778
+ const { reasoningConfig: _, ...filteredBedrockOptions } = (providerOptions == null ? void 0 : providerOptions.bedrock) || {};
592
779
  return {
593
780
  command: {
594
781
  system,
@@ -597,27 +784,36 @@ var BedrockChatLanguageModel = class {
597
784
  ...Object.keys(inferenceConfig).length > 0 && {
598
785
  inferenceConfig
599
786
  },
600
- ...providerOptions == null ? void 0 : providerOptions.bedrock,
601
- ...((_e = toolConfig.tools) == null ? void 0 : _e.length) ? { toolConfig } : {}
787
+ ...filteredBedrockOptions,
788
+ ...toolConfig.tools !== void 0 && toolConfig.tools.length > 0 ? { toolConfig } : {}
602
789
  },
603
- warnings: [...warnings, ...toolWarnings]
790
+ warnings,
791
+ usesJsonResponseTool: jsonResponseTool != null,
792
+ betas
604
793
  };
605
794
  }
606
- async getSupportedUrls() {
607
- return {
608
- // no supported urls for bedrock
609
- };
795
+ async getHeaders({
796
+ betas,
797
+ headers
798
+ }) {
799
+ return combineHeaders(
800
+ await resolve(this.config.headers),
801
+ betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {},
802
+ headers
803
+ );
610
804
  }
611
805
  async doGenerate(options) {
612
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
613
- const { command: args, warnings } = this.getArgs(options);
806
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
807
+ const {
808
+ command: args,
809
+ warnings,
810
+ usesJsonResponseTool,
811
+ betas
812
+ } = await this.getArgs(options);
614
813
  const url = `${this.getUrl(this.modelId)}/converse`;
615
814
  const { value: response, responseHeaders } = await postJsonToApi({
616
815
  url,
617
- headers: combineHeaders(
618
- await resolve(this.config.headers),
619
- options.headers
620
- ),
816
+ headers: await this.getHeaders({ betas, headers: options.headers }),
621
817
  body: args,
622
818
  failedResponseHandler: createJsonErrorResponseHandler({
623
819
  errorSchema: BedrockErrorSchema,
@@ -635,49 +831,60 @@ var BedrockChatLanguageModel = class {
635
831
  const content = [];
636
832
  for (const part of response.output.message.content) {
637
833
  if (part.text) {
638
- content.push({ type: "text", text: part.text });
834
+ if (!usesJsonResponseTool) {
835
+ content.push({ type: "text", text: part.text });
836
+ }
639
837
  }
640
838
  if (part.reasoningContent) {
641
839
  if ("reasoningText" in part.reasoningContent) {
642
- content.push({
840
+ const reasoning = {
643
841
  type: "reasoning",
644
- reasoningType: "text",
645
842
  text: part.reasoningContent.reasoningText.text
646
- });
843
+ };
647
844
  if (part.reasoningContent.reasoningText.signature) {
648
- content.push({
649
- type: "reasoning",
650
- reasoningType: "signature",
651
- signature: part.reasoningContent.reasoningText.signature
652
- });
845
+ reasoning.providerMetadata = {
846
+ bedrock: {
847
+ signature: part.reasoningContent.reasoningText.signature
848
+ }
849
+ };
653
850
  }
851
+ content.push(reasoning);
654
852
  } else if ("redactedReasoning" in part.reasoningContent) {
655
853
  content.push({
656
854
  type: "reasoning",
657
- reasoningType: "redacted",
658
- data: (_a = part.reasoningContent.redactedReasoning.data) != null ? _a : ""
855
+ text: "",
856
+ providerMetadata: {
857
+ bedrock: {
858
+ redactedData: (_a = part.reasoningContent.redactedReasoning.data) != null ? _a : ""
859
+ }
860
+ }
659
861
  });
660
862
  }
661
863
  }
662
864
  if (part.toolUse) {
663
- content.push({
664
- type: "tool-call",
665
- toolCallType: "function",
666
- toolCallId: (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId(),
667
- toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
668
- args: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : "")
669
- });
865
+ content.push(
866
+ // when a json response tool is used, the tool call becomes the text:
867
+ usesJsonResponseTool ? {
868
+ type: "text",
869
+ text: JSON.stringify(part.toolUse.input)
870
+ } : {
871
+ type: "tool-call",
872
+ toolCallId: (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId(),
873
+ toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
874
+ input: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : "")
875
+ }
876
+ );
670
877
  }
671
878
  }
672
- const providerMetadata = response.trace || response.usage ? {
879
+ const providerMetadata = response.trace || response.usage || usesJsonResponseTool ? {
673
880
  bedrock: {
674
881
  ...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
675
- ...response.usage && {
882
+ ...((_h = response.usage) == null ? void 0 : _h.cacheWriteInputTokens) != null && {
676
883
  usage: {
677
- cacheReadInputTokens: (_i = (_h = response.usage) == null ? void 0 : _h.cacheReadInputTokens) != null ? _i : Number.NaN,
678
- cacheWriteInputTokens: (_k = (_j = response.usage) == null ? void 0 : _j.cacheWriteInputTokens) != null ? _k : Number.NaN
884
+ cacheWriteInputTokens: response.usage.cacheWriteInputTokens
679
885
  }
680
- }
886
+ },
887
+ ...usesJsonResponseTool && { isJsonResponseFromTool: true }
681
888
  }
682
889
  } : void 0;
683
890
  return {
@@ -686,8 +893,10 @@ var BedrockChatLanguageModel = class {
686
893
  response.stopReason
687
894
  ),
688
895
  usage: {
689
- inputTokens: (_l = response.usage) == null ? void 0 : _l.inputTokens,
690
- outputTokens: (_m = response.usage) == null ? void 0 : _m.outputTokens
896
+ inputTokens: (_i = response.usage) == null ? void 0 : _i.inputTokens,
897
+ outputTokens: (_j = response.usage) == null ? void 0 : _j.outputTokens,
898
+ totalTokens: ((_k = response.usage) == null ? void 0 : _k.inputTokens) + ((_l = response.usage) == null ? void 0 : _l.outputTokens),
899
+ cachedInputTokens: (_n = (_m = response.usage) == null ? void 0 : _m.cacheReadInputTokens) != null ? _n : void 0
691
900
  },
692
901
  response: {
693
902
  // TODO add id, timestamp, etc
@@ -698,14 +907,16 @@ var BedrockChatLanguageModel = class {
698
907
  };
699
908
  }
700
909
  async doStream(options) {
701
- const { command: args, warnings } = this.getArgs(options);
910
+ const {
911
+ command: args,
912
+ warnings,
913
+ usesJsonResponseTool,
914
+ betas
915
+ } = await this.getArgs(options);
702
916
  const url = `${this.getUrl(this.modelId)}/converse-stream`;
703
917
  const { value: response, responseHeaders } = await postJsonToApi({
704
918
  url,
705
- headers: combineHeaders(
706
- await resolve(this.config.headers),
707
- options.headers
708
- ),
919
+ headers: await this.getHeaders({ betas, headers: options.headers }),
709
920
  body: args,
710
921
  failedResponseHandler: createJsonErrorResponseHandler({
711
922
  errorSchema: BedrockErrorSchema,
@@ -718,10 +929,11 @@ var BedrockChatLanguageModel = class {
718
929
  let finishReason = "unknown";
719
930
  const usage = {
720
931
  inputTokens: void 0,
721
- outputTokens: void 0
932
+ outputTokens: void 0,
933
+ totalTokens: void 0
722
934
  };
723
935
  let providerMetadata = void 0;
724
- const toolCallContentBlocks = {};
936
+ const contentBlocks = {};
725
937
  return {
726
938
  stream: response.pipeThrough(
727
939
  new TransformStream({
@@ -729,11 +941,14 @@ var BedrockChatLanguageModel = class {
729
941
  controller.enqueue({ type: "stream-start", warnings });
730
942
  },
731
943
  transform(chunk, controller) {
732
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
944
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
733
945
  function enqueueError(bedrockError) {
734
946
  finishReason = "error";
735
947
  controller.enqueue({ type: "error", error: bedrockError });
736
948
  }
949
+ if (options.includeRawChunks) {
950
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
951
+ }
737
952
  if (!chunk.success) {
738
953
  enqueueError(chunk.error);
739
954
  return;
@@ -763,87 +978,174 @@ var BedrockChatLanguageModel = class {
763
978
  if (value.metadata) {
764
979
  usage.inputTokens = (_b = (_a = value.metadata.usage) == null ? void 0 : _a.inputTokens) != null ? _b : usage.inputTokens;
765
980
  usage.outputTokens = (_d = (_c = value.metadata.usage) == null ? void 0 : _c.outputTokens) != null ? _d : usage.outputTokens;
766
- const cacheUsage = ((_e = value.metadata.usage) == null ? void 0 : _e.cacheReadInputTokens) != null || ((_f = value.metadata.usage) == null ? void 0 : _f.cacheWriteInputTokens) != null ? {
981
+ usage.totalTokens = ((_e = usage.inputTokens) != null ? _e : 0) + ((_f = usage.outputTokens) != null ? _f : 0);
982
+ usage.cachedInputTokens = (_h = (_g = value.metadata.usage) == null ? void 0 : _g.cacheReadInputTokens) != null ? _h : usage.cachedInputTokens;
983
+ const cacheUsage = ((_i = value.metadata.usage) == null ? void 0 : _i.cacheWriteInputTokens) != null ? {
767
984
  usage: {
768
- cacheReadInputTokens: (_h = (_g = value.metadata.usage) == null ? void 0 : _g.cacheReadInputTokens) != null ? _h : Number.NaN,
769
- cacheWriteInputTokens: (_j = (_i = value.metadata.usage) == null ? void 0 : _i.cacheWriteInputTokens) != null ? _j : Number.NaN
985
+ cacheWriteInputTokens: value.metadata.usage.cacheWriteInputTokens
770
986
  }
771
987
  } : void 0;
772
988
  const trace = value.metadata.trace ? {
773
989
  trace: value.metadata.trace
774
990
  } : void 0;
775
- if (cacheUsage || trace) {
991
+ if (cacheUsage || trace || usesJsonResponseTool) {
776
992
  providerMetadata = {
777
993
  bedrock: {
778
994
  ...cacheUsage,
779
- ...trace
995
+ ...trace,
996
+ ...usesJsonResponseTool && {
997
+ isJsonResponseFromTool: true
998
+ }
780
999
  }
781
1000
  };
782
1001
  }
783
1002
  }
784
- if (((_k = value.contentBlockDelta) == null ? void 0 : _k.delta) && "text" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.text) {
1003
+ if (((_j = value.contentBlockStart) == null ? void 0 : _j.contentBlockIndex) != null && !((_l = (_k = value.contentBlockStart) == null ? void 0 : _k.start) == null ? void 0 : _l.toolUse)) {
1004
+ const blockIndex = value.contentBlockStart.contentBlockIndex;
1005
+ contentBlocks[blockIndex] = { type: "text" };
785
1006
  controller.enqueue({
786
- type: "text",
787
- text: value.contentBlockDelta.delta.text
1007
+ type: "text-start",
1008
+ id: String(blockIndex)
788
1009
  });
789
1010
  }
790
- if (((_l = value.contentBlockDelta) == null ? void 0 : _l.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
1011
+ if (((_m = value.contentBlockDelta) == null ? void 0 : _m.delta) && "text" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.text) {
1012
+ const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
1013
+ if (contentBlocks[blockIndex] == null) {
1014
+ contentBlocks[blockIndex] = { type: "text" };
1015
+ if (!usesJsonResponseTool) {
1016
+ controller.enqueue({
1017
+ type: "text-start",
1018
+ id: String(blockIndex)
1019
+ });
1020
+ }
1021
+ }
1022
+ if (!usesJsonResponseTool) {
1023
+ controller.enqueue({
1024
+ type: "text-delta",
1025
+ id: String(blockIndex),
1026
+ delta: value.contentBlockDelta.delta.text
1027
+ });
1028
+ }
1029
+ }
1030
+ if (((_n = value.contentBlockStop) == null ? void 0 : _n.contentBlockIndex) != null) {
1031
+ const blockIndex = value.contentBlockStop.contentBlockIndex;
1032
+ const contentBlock = contentBlocks[blockIndex];
1033
+ if (contentBlock != null) {
1034
+ if (contentBlock.type === "reasoning") {
1035
+ controller.enqueue({
1036
+ type: "reasoning-end",
1037
+ id: String(blockIndex)
1038
+ });
1039
+ } else if (contentBlock.type === "text") {
1040
+ if (!usesJsonResponseTool) {
1041
+ controller.enqueue({
1042
+ type: "text-end",
1043
+ id: String(blockIndex)
1044
+ });
1045
+ }
1046
+ } else if (contentBlock.type === "tool-call") {
1047
+ if (usesJsonResponseTool) {
1048
+ controller.enqueue({
1049
+ type: "text-start",
1050
+ id: String(blockIndex)
1051
+ });
1052
+ controller.enqueue({
1053
+ type: "text-delta",
1054
+ id: String(blockIndex),
1055
+ delta: contentBlock.jsonText
1056
+ });
1057
+ controller.enqueue({
1058
+ type: "text-end",
1059
+ id: String(blockIndex)
1060
+ });
1061
+ } else {
1062
+ controller.enqueue({
1063
+ type: "tool-input-end",
1064
+ id: contentBlock.toolCallId
1065
+ });
1066
+ controller.enqueue({
1067
+ type: "tool-call",
1068
+ toolCallId: contentBlock.toolCallId,
1069
+ toolName: contentBlock.toolName,
1070
+ input: contentBlock.jsonText
1071
+ });
1072
+ }
1073
+ }
1074
+ delete contentBlocks[blockIndex];
1075
+ }
1076
+ }
1077
+ if (((_o = value.contentBlockDelta) == null ? void 0 : _o.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
1078
+ const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
791
1079
  const reasoningContent = value.contentBlockDelta.delta.reasoningContent;
792
1080
  if ("text" in reasoningContent && reasoningContent.text) {
1081
+ if (contentBlocks[blockIndex] == null) {
1082
+ contentBlocks[blockIndex] = { type: "reasoning" };
1083
+ controller.enqueue({
1084
+ type: "reasoning-start",
1085
+ id: String(blockIndex)
1086
+ });
1087
+ }
793
1088
  controller.enqueue({
794
- type: "reasoning",
795
- reasoningType: "text",
796
- text: reasoningContent.text
1089
+ type: "reasoning-delta",
1090
+ id: String(blockIndex),
1091
+ delta: reasoningContent.text
797
1092
  });
798
1093
  } else if ("signature" in reasoningContent && reasoningContent.signature) {
799
1094
  controller.enqueue({
800
- type: "reasoning",
801
- reasoningType: "signature",
802
- signature: reasoningContent.signature
1095
+ type: "reasoning-delta",
1096
+ id: String(blockIndex),
1097
+ delta: "",
1098
+ providerMetadata: {
1099
+ bedrock: {
1100
+ signature: reasoningContent.signature
1101
+ }
1102
+ }
803
1103
  });
804
1104
  } else if ("data" in reasoningContent && reasoningContent.data) {
805
1105
  controller.enqueue({
806
- type: "reasoning",
807
- reasoningType: "redacted",
808
- data: reasoningContent.data
1106
+ type: "reasoning-delta",
1107
+ id: String(blockIndex),
1108
+ delta: "",
1109
+ providerMetadata: {
1110
+ bedrock: {
1111
+ redactedData: reasoningContent.data
1112
+ }
1113
+ }
809
1114
  });
810
1115
  }
811
1116
  }
812
1117
  const contentBlockStart = value.contentBlockStart;
813
- if (((_m = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _m.toolUse) != null) {
1118
+ if (((_p = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _p.toolUse) != null) {
814
1119
  const toolUse = contentBlockStart.start.toolUse;
815
- toolCallContentBlocks[contentBlockStart.contentBlockIndex] = {
1120
+ const blockIndex = contentBlockStart.contentBlockIndex;
1121
+ contentBlocks[blockIndex] = {
1122
+ type: "tool-call",
816
1123
  toolCallId: toolUse.toolUseId,
817
1124
  toolName: toolUse.name,
818
1125
  jsonText: ""
819
1126
  };
1127
+ if (!usesJsonResponseTool) {
1128
+ controller.enqueue({
1129
+ type: "tool-input-start",
1130
+ id: toolUse.toolUseId,
1131
+ toolName: toolUse.name
1132
+ });
1133
+ }
820
1134
  }
821
1135
  const contentBlockDelta = value.contentBlockDelta;
822
1136
  if ((contentBlockDelta == null ? void 0 : contentBlockDelta.delta) && "toolUse" in contentBlockDelta.delta && contentBlockDelta.delta.toolUse) {
823
- const contentBlock = toolCallContentBlocks[contentBlockDelta.contentBlockIndex];
824
- const delta = (_n = contentBlockDelta.delta.toolUse.input) != null ? _n : "";
825
- controller.enqueue({
826
- type: "tool-call-delta",
827
- toolCallType: "function",
828
- toolCallId: contentBlock.toolCallId,
829
- toolName: contentBlock.toolName,
830
- argsTextDelta: delta
831
- });
832
- contentBlock.jsonText += delta;
833
- }
834
- const contentBlockStop = value.contentBlockStop;
835
- if (contentBlockStop != null) {
836
- const index = contentBlockStop.contentBlockIndex;
837
- const contentBlock = toolCallContentBlocks[index];
838
- if (contentBlock != null) {
839
- controller.enqueue({
840
- type: "tool-call",
841
- toolCallType: "function",
842
- toolCallId: contentBlock.toolCallId,
843
- toolName: contentBlock.toolName,
844
- args: contentBlock.jsonText
845
- });
846
- delete toolCallContentBlocks[index];
1137
+ const blockIndex = contentBlockDelta.contentBlockIndex;
1138
+ const contentBlock = contentBlocks[blockIndex];
1139
+ if ((contentBlock == null ? void 0 : contentBlock.type) === "tool-call") {
1140
+ const delta = (_q = contentBlockDelta.delta.toolUse.input) != null ? _q : "";
1141
+ if (!usesJsonResponseTool) {
1142
+ controller.enqueue({
1143
+ type: "tool-input-delta",
1144
+ id: contentBlock.toolCallId,
1145
+ delta
1146
+ });
1147
+ }
1148
+ contentBlock.jsonText += delta;
847
1149
  }
848
1150
  }
849
1151
  },
@@ -943,9 +1245,9 @@ var BedrockStreamSchema = z3.object({
943
1245
  contentBlockStop: z3.object({
944
1246
  contentBlockIndex: z3.number()
945
1247
  }).nullish(),
946
- internalServerException: z3.record(z3.unknown()).nullish(),
1248
+ internalServerException: z3.record(z3.string(), z3.unknown()).nullish(),
947
1249
  messageStop: z3.object({
948
- additionalModelResponseFields: z3.record(z3.unknown()).nullish(),
1250
+ additionalModelResponseFields: z3.record(z3.string(), z3.unknown()).nullish(),
949
1251
  stopReason: BedrockStopReasonSchema
950
1252
  }).nullish(),
951
1253
  metadata: z3.object({
@@ -957,23 +1259,30 @@ var BedrockStreamSchema = z3.object({
957
1259
  outputTokens: z3.number()
958
1260
  }).nullish()
959
1261
  }).nullish(),
960
- modelStreamErrorException: z3.record(z3.unknown()).nullish(),
961
- throttlingException: z3.record(z3.unknown()).nullish(),
962
- validationException: z3.record(z3.unknown()).nullish()
1262
+ modelStreamErrorException: z3.record(z3.string(), z3.unknown()).nullish(),
1263
+ throttlingException: z3.record(z3.string(), z3.unknown()).nullish(),
1264
+ validationException: z3.record(z3.string(), z3.unknown()).nullish()
1265
+ });
1266
+ var bedrockReasoningMetadataSchema = z3.object({
1267
+ signature: z3.string().optional(),
1268
+ redactedData: z3.string().optional()
963
1269
  });
964
1270
 
965
1271
  // src/bedrock-embedding-model.ts
1272
+ import {
1273
+ TooManyEmbeddingValuesForCallError
1274
+ } from "@ai-sdk/provider";
966
1275
  import {
967
1276
  combineHeaders as combineHeaders2,
968
1277
  createJsonErrorResponseHandler as createJsonErrorResponseHandler2,
969
1278
  createJsonResponseHandler as createJsonResponseHandler2,
970
- parseProviderOptions as parseProviderOptions2,
1279
+ parseProviderOptions as parseProviderOptions3,
971
1280
  postJsonToApi as postJsonToApi2,
972
1281
  resolve as resolve2
973
1282
  } from "@ai-sdk/provider-utils";
974
1283
 
975
1284
  // src/bedrock-embedding-options.ts
976
- import { z as z4 } from "zod";
1285
+ import { z as z4 } from "zod/v4";
977
1286
  var bedrockEmbeddingProviderOptions = z4.object({
978
1287
  /**
979
1288
  The number of dimensions the resulting output embeddings should have (defaults to 1024).
@@ -988,14 +1297,14 @@ var bedrockEmbeddingProviderOptions = z4.object({
988
1297
  });
989
1298
 
990
1299
  // src/bedrock-embedding-model.ts
991
- import { z as z5 } from "zod";
1300
+ import { z as z5 } from "zod/v4";
992
1301
  var BedrockEmbeddingModel = class {
993
1302
  constructor(modelId, config) {
994
1303
  this.modelId = modelId;
995
1304
  this.config = config;
996
1305
  this.specificationVersion = "v2";
997
1306
  this.provider = "amazon-bedrock";
998
- this.maxEmbeddingsPerCall = void 0;
1307
+ this.maxEmbeddingsPerCall = 1;
999
1308
  this.supportsParallelCalls = true;
1000
1309
  }
1001
1310
  getUrl(modelId) {
@@ -1009,48 +1318,45 @@ var BedrockEmbeddingModel = class {
1009
1318
  providerOptions
1010
1319
  }) {
1011
1320
  var _a;
1012
- const bedrockOptions = (_a = parseProviderOptions2({
1321
+ if (values.length > this.maxEmbeddingsPerCall) {
1322
+ throw new TooManyEmbeddingValuesForCallError({
1323
+ provider: this.provider,
1324
+ modelId: this.modelId,
1325
+ maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
1326
+ values
1327
+ });
1328
+ }
1329
+ const bedrockOptions = (_a = await parseProviderOptions3({
1013
1330
  provider: "bedrock",
1014
1331
  providerOptions,
1015
1332
  schema: bedrockEmbeddingProviderOptions
1016
1333
  })) != null ? _a : {};
1017
- const embedSingleText = async (inputText) => {
1018
- const args = {
1019
- inputText,
1020
- dimensions: bedrockOptions.dimensions,
1021
- normalize: bedrockOptions.normalize
1022
- };
1023
- const url = this.getUrl(this.modelId);
1024
- const { value: response } = await postJsonToApi2({
1025
- url,
1026
- headers: await resolve2(
1027
- combineHeaders2(await resolve2(this.config.headers), headers)
1028
- ),
1029
- body: args,
1030
- failedResponseHandler: createJsonErrorResponseHandler2({
1031
- errorSchema: BedrockErrorSchema,
1032
- errorToMessage: (error) => `${error.type}: ${error.message}`
1033
- }),
1034
- successfulResponseHandler: createJsonResponseHandler2(
1035
- BedrockEmbeddingResponseSchema
1036
- ),
1037
- fetch: this.config.fetch,
1038
- abortSignal
1039
- });
1040
- return {
1041
- embedding: response.embedding,
1042
- inputTextTokenCount: response.inputTextTokenCount
1043
- };
1334
+ const args = {
1335
+ inputText: values[0],
1336
+ dimensions: bedrockOptions.dimensions,
1337
+ normalize: bedrockOptions.normalize
1338
+ };
1339
+ const url = this.getUrl(this.modelId);
1340
+ const { value: response } = await postJsonToApi2({
1341
+ url,
1342
+ headers: await resolve2(
1343
+ combineHeaders2(await resolve2(this.config.headers), headers)
1344
+ ),
1345
+ body: args,
1346
+ failedResponseHandler: createJsonErrorResponseHandler2({
1347
+ errorSchema: BedrockErrorSchema,
1348
+ errorToMessage: (error) => `${error.type}: ${error.message}`
1349
+ }),
1350
+ successfulResponseHandler: createJsonResponseHandler2(
1351
+ BedrockEmbeddingResponseSchema
1352
+ ),
1353
+ fetch: this.config.fetch,
1354
+ abortSignal
1355
+ });
1356
+ return {
1357
+ embeddings: [response.embedding],
1358
+ usage: { tokens: response.inputTextTokenCount }
1044
1359
  };
1045
- const responses = await Promise.all(values.map(embedSingleText));
1046
- return responses.reduce(
1047
- (accumulated, response) => {
1048
- accumulated.embeddings.push(response.embedding);
1049
- accumulated.usage.tokens += response.inputTextTokenCount;
1050
- return accumulated;
1051
- },
1052
- { embeddings: [], usage: { tokens: 0 } }
1053
- );
1054
1360
  }
1055
1361
  };
1056
1362
  var BedrockEmbeddingResponseSchema = z5.object({
@@ -1073,18 +1379,17 @@ var modelMaxImagesPerCall = {
1073
1379
  };
1074
1380
 
1075
1381
  // src/bedrock-image-model.ts
1076
- import { z as z6 } from "zod";
1382
+ import { z as z6 } from "zod/v4";
1077
1383
  var BedrockImageModel = class {
1078
- constructor(modelId, settings, config) {
1384
+ constructor(modelId, config) {
1079
1385
  this.modelId = modelId;
1080
- this.settings = settings;
1081
1386
  this.config = config;
1082
- this.specificationVersion = "v1";
1387
+ this.specificationVersion = "v2";
1083
1388
  this.provider = "amazon-bedrock";
1084
1389
  }
1085
1390
  get maxImagesPerCall() {
1086
- var _a, _b;
1087
- return (_b = (_a = this.settings.maxImagesPerCall) != null ? _a : modelMaxImagesPerCall[this.modelId]) != null ? _b : 1;
1391
+ var _a;
1392
+ return (_a = modelMaxImagesPerCall[this.modelId]) != null ? _a : 1;
1088
1393
  }
1089
1394
  getUrl(modelId) {
1090
1395
  const encodedModelId = encodeURIComponent(modelId);
@@ -1100,7 +1405,7 @@ var BedrockImageModel = class {
1100
1405
  headers,
1101
1406
  abortSignal
1102
1407
  }) {
1103
- var _a, _b, _c, _d, _e, _f;
1408
+ var _a, _b, _c, _d, _e, _f, _g;
1104
1409
  const warnings = [];
1105
1410
  const [width, height] = size ? size.split("x").map(Number) : [];
1106
1411
  const args = {
@@ -1109,6 +1414,9 @@ var BedrockImageModel = class {
1109
1414
  text: prompt,
1110
1415
  ...((_a = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _a.negativeText) ? {
1111
1416
  negativeText: providerOptions.bedrock.negativeText
1417
+ } : {},
1418
+ ...((_b = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _b.style) ? {
1419
+ style: providerOptions.bedrock.style
1112
1420
  } : {}
1113
1421
  },
1114
1422
  imageGenerationConfig: {
@@ -1116,8 +1424,8 @@ var BedrockImageModel = class {
1116
1424
  ...height ? { height } : {},
1117
1425
  ...seed ? { seed } : {},
1118
1426
  ...n ? { numberOfImages: n } : {},
1119
- ...((_b = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _b.quality) ? { quality: providerOptions.bedrock.quality } : {},
1120
- ...((_c = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _c.cfgScale) ? { cfgScale: providerOptions.bedrock.cfgScale } : {}
1427
+ ...((_c = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _c.quality) ? { quality: providerOptions.bedrock.quality } : {},
1428
+ ...((_d = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _d.cfgScale) ? { cfgScale: providerOptions.bedrock.cfgScale } : {}
1121
1429
  }
1122
1430
  };
1123
1431
  if (aspectRatio != void 0) {
@@ -1127,7 +1435,7 @@ var BedrockImageModel = class {
1127
1435
  details: "This model does not support aspect ratio. Use `size` instead."
1128
1436
  });
1129
1437
  }
1130
- const currentDate = (_f = (_e = (_d = this.config._internal) == null ? void 0 : _d.currentDate) == null ? void 0 : _e.call(_d)) != null ? _f : /* @__PURE__ */ new Date();
1438
+ const currentDate = (_g = (_f = (_e = this.config._internal) == null ? void 0 : _e.currentDate) == null ? void 0 : _f.call(_e)) != null ? _g : /* @__PURE__ */ new Date();
1131
1439
  const { value: response, responseHeaders } = await postJsonToApi3({
1132
1440
  url: this.getUrl(this.modelId),
1133
1441
  headers: await resolve3(
@@ -1178,11 +1486,7 @@ function extractHeaders(headers) {
1178
1486
  return originalHeaders;
1179
1487
  }
1180
1488
  function convertHeadersToRecord(headers) {
1181
- const record = {};
1182
- headers.forEach((value, key) => {
1183
- record[key.toLowerCase()] = value;
1184
- });
1185
- return record;
1489
+ return Object.fromEntries([...headers]);
1186
1490
  }
1187
1491
 
1188
1492
  // src/bedrock-sigv4-fetch.ts
@@ -1234,10 +1538,28 @@ function prepareBodyString(body) {
1234
1538
  return JSON.stringify(body);
1235
1539
  }
1236
1540
  }
1541
+ function createApiKeyFetchFunction(apiKey, fetch = globalThis.fetch) {
1542
+ return async (input, init) => {
1543
+ const originalHeaders = extractHeaders(init == null ? void 0 : init.headers);
1544
+ return fetch(input, {
1545
+ ...init,
1546
+ headers: removeUndefinedEntries(
1547
+ combineHeaders4(originalHeaders, {
1548
+ Authorization: `Bearer ${apiKey}`
1549
+ })
1550
+ )
1551
+ });
1552
+ };
1553
+ }
1237
1554
 
1238
1555
  // src/bedrock-provider.ts
1239
1556
  function createAmazonBedrock(options = {}) {
1240
- const sigv4Fetch = createSigV4FetchFunction(async () => {
1557
+ const rawApiKey = loadOptionalSetting({
1558
+ settingValue: options.apiKey,
1559
+ environmentVariableName: "AWS_BEARER_TOKEN_BEDROCK"
1560
+ });
1561
+ const apiKey = rawApiKey && rawApiKey.trim().length > 0 ? rawApiKey.trim() : void 0;
1562
+ const fetchFunction = apiKey ? createApiKeyFetchFunction(apiKey, options.fetch) : createSigV4FetchFunction(async () => {
1241
1563
  const region = loadSetting({
1242
1564
  settingValue: options.region,
1243
1565
  settingName: "region",
@@ -1245,30 +1567,58 @@ function createAmazonBedrock(options = {}) {
1245
1567
  description: "AWS region"
1246
1568
  });
1247
1569
  if (options.credentialProvider) {
1570
+ try {
1571
+ return {
1572
+ ...await options.credentialProvider(),
1573
+ region
1574
+ };
1575
+ } catch (error) {
1576
+ const errorMessage = error instanceof Error ? error.message : String(error);
1577
+ throw new Error(
1578
+ `AWS credential provider failed: ${errorMessage}. Please ensure your credential provider returns valid AWS credentials with accessKeyId and secretAccessKey properties.`
1579
+ );
1580
+ }
1581
+ }
1582
+ try {
1248
1583
  return {
1249
- ...await options.credentialProvider(),
1250
- region
1584
+ region,
1585
+ accessKeyId: loadSetting({
1586
+ settingValue: options.accessKeyId,
1587
+ settingName: "accessKeyId",
1588
+ environmentVariableName: "AWS_ACCESS_KEY_ID",
1589
+ description: "AWS access key ID"
1590
+ }),
1591
+ secretAccessKey: loadSetting({
1592
+ settingValue: options.secretAccessKey,
1593
+ settingName: "secretAccessKey",
1594
+ environmentVariableName: "AWS_SECRET_ACCESS_KEY",
1595
+ description: "AWS secret access key"
1596
+ }),
1597
+ sessionToken: loadOptionalSetting({
1598
+ settingValue: options.sessionToken,
1599
+ environmentVariableName: "AWS_SESSION_TOKEN"
1600
+ })
1251
1601
  };
1602
+ } catch (error) {
1603
+ const errorMessage = error instanceof Error ? error.message : String(error);
1604
+ if (errorMessage.includes("AWS_ACCESS_KEY_ID") || errorMessage.includes("accessKeyId")) {
1605
+ throw new Error(
1606
+ `AWS SigV4 authentication requires AWS credentials. Please provide either:
1607
+ 1. Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables
1608
+ 2. Provide accessKeyId and secretAccessKey in options
1609
+ 3. Use a credentialProvider function
1610
+ 4. Use API key authentication with AWS_BEARER_TOKEN_BEDROCK or apiKey option
1611
+ Original error: ${errorMessage}`
1612
+ );
1613
+ }
1614
+ if (errorMessage.includes("AWS_SECRET_ACCESS_KEY") || errorMessage.includes("secretAccessKey")) {
1615
+ throw new Error(
1616
+ `AWS SigV4 authentication requires both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. Please ensure both credentials are provided.
1617
+ Original error: ${errorMessage}`
1618
+ );
1619
+ }
1620
+ throw error;
1252
1621
  }
1253
- return {
1254
- region,
1255
- accessKeyId: loadSetting({
1256
- settingValue: options.accessKeyId,
1257
- settingName: "accessKeyId",
1258
- environmentVariableName: "AWS_ACCESS_KEY_ID",
1259
- description: "AWS access key ID"
1260
- }),
1261
- secretAccessKey: loadSetting({
1262
- settingValue: options.secretAccessKey,
1263
- settingName: "secretAccessKey",
1264
- environmentVariableName: "AWS_SECRET_ACCESS_KEY",
1265
- description: "AWS secret access key"
1266
- }),
1267
- sessionToken: loadOptionalSetting({
1268
- settingValue: options.sessionToken,
1269
- environmentVariableName: "AWS_SESSION_TOKEN"
1270
- })
1271
- };
1272
1622
  }, options.fetch);
1273
1623
  const getBaseUrl = () => {
1274
1624
  var _a, _b;
@@ -1286,7 +1636,7 @@ function createAmazonBedrock(options = {}) {
1286
1636
  return new BedrockChatLanguageModel(modelId, {
1287
1637
  baseUrl: getBaseUrl,
1288
1638
  headers: (_a = options.headers) != null ? _a : {},
1289
- fetch: sigv4Fetch,
1639
+ fetch: fetchFunction,
1290
1640
  generateId
1291
1641
  });
1292
1642
  };
@@ -1303,15 +1653,15 @@ function createAmazonBedrock(options = {}) {
1303
1653
  return new BedrockEmbeddingModel(modelId, {
1304
1654
  baseUrl: getBaseUrl,
1305
1655
  headers: (_a = options.headers) != null ? _a : {},
1306
- fetch: sigv4Fetch
1656
+ fetch: fetchFunction
1307
1657
  });
1308
1658
  };
1309
- const createImageModel = (modelId, settings = {}) => {
1659
+ const createImageModel = (modelId) => {
1310
1660
  var _a;
1311
- return new BedrockImageModel(modelId, settings, {
1661
+ return new BedrockImageModel(modelId, {
1312
1662
  baseUrl: getBaseUrl,
1313
1663
  headers: (_a = options.headers) != null ? _a : {},
1314
- fetch: sigv4Fetch
1664
+ fetch: fetchFunction
1315
1665
  });
1316
1666
  };
1317
1667
  provider.languageModel = createChatModel;
@@ -1320,6 +1670,7 @@ function createAmazonBedrock(options = {}) {
1320
1670
  provider.textEmbeddingModel = createEmbeddingModel;
1321
1671
  provider.image = createImageModel;
1322
1672
  provider.imageModel = createImageModel;
1673
+ provider.tools = anthropicTools2;
1323
1674
  return provider;
1324
1675
  }
1325
1676
  var bedrock = createAmazonBedrock();