@ai-sdk/amazon-bedrock 3.0.0-canary.1 → 3.0.0-canary.11

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
@@ -7,18 +7,15 @@ import {
7
7
  } from "@ai-sdk/provider-utils";
8
8
 
9
9
  // src/bedrock-chat-language-model.ts
10
- import {
11
- InvalidArgumentError,
12
- UnsupportedFunctionalityError as UnsupportedFunctionalityError3
13
- } from "@ai-sdk/provider";
14
10
  import {
15
11
  combineHeaders,
16
12
  createJsonErrorResponseHandler,
17
13
  createJsonResponseHandler,
14
+ parseProviderOptions as parseProviderOptions2,
18
15
  postJsonToApi,
19
16
  resolve
20
17
  } from "@ai-sdk/provider-utils";
21
- import { z as z2 } from "zod";
18
+ import { z as z3 } from "zod";
22
19
 
23
20
  // src/bedrock-api-types.ts
24
21
  var BEDROCK_CACHE_POINT = {
@@ -37,11 +34,26 @@ var BEDROCK_STOP_REASONS = [
37
34
  "tool_use"
38
35
  ];
39
36
 
40
- // src/bedrock-error.ts
37
+ // src/bedrock-chat-options.ts
41
38
  import { z } from "zod";
42
- var BedrockErrorSchema = z.object({
43
- message: z.string(),
44
- type: z.string().nullish()
39
+ var bedrockProviderOptions = z.object({
40
+ /**
41
+ * Additional inference parameters that the model supports,
42
+ * beyond the base set of inference parameters that Converse
43
+ * supports in the inferenceConfig field
44
+ */
45
+ additionalModelRequestFields: z.record(z.any()).optional(),
46
+ reasoningConfig: z.object({
47
+ type: z.union([z.literal("enabled"), z.literal("disabled")]).nullish(),
48
+ budgetTokens: z.number().nullish()
49
+ }).nullish()
50
+ });
51
+
52
+ // src/bedrock-error.ts
53
+ import { z as z2 } from "zod";
54
+ var BedrockErrorSchema = z2.object({
55
+ message: z2.string(),
56
+ type: z2.string().nullish()
45
57
  });
46
58
 
47
59
  // src/bedrock-event-stream-response-handler.ts
@@ -65,7 +77,7 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
65
77
  responseHeaders,
66
78
  value: response.body.pipeThrough(
67
79
  new TransformStream({
68
- transform(chunk, controller) {
80
+ async transform(chunk, controller) {
69
81
  var _a, _b;
70
82
  const newBuffer = new Uint8Array(buffer.length + chunk.length);
71
83
  newBuffer.set(buffer);
@@ -86,7 +98,7 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
86
98
  buffer = buffer.slice(totalLength);
87
99
  if (((_a = decoded.headers[":message-type"]) == null ? void 0 : _a.value) === "event") {
88
100
  const data = textDecoder.decode(decoded.body);
89
- const parsedDataResult = safeParseJSON({ text: data });
101
+ const parsedDataResult = await safeParseJSON({ text: data });
90
102
  if (!parsedDataResult.success) {
91
103
  controller.enqueue(parsedDataResult);
92
104
  break;
@@ -95,7 +107,7 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
95
107
  let wrappedData = {
96
108
  [(_b = decoded.headers[":event-type"]) == null ? void 0 : _b.value]: parsedDataResult.value
97
109
  };
98
- const validatedWrappedData = safeValidateTypes({
110
+ const validatedWrappedData = await safeValidateTypes({
99
111
  value: wrappedData,
100
112
  schema: chunkSchema
101
113
  });
@@ -123,9 +135,11 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
123
135
  import {
124
136
  UnsupportedFunctionalityError
125
137
  } from "@ai-sdk/provider";
126
- function prepareTools(mode) {
127
- var _a;
128
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
138
+ function prepareTools({
139
+ tools,
140
+ toolChoice
141
+ }) {
142
+ tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
129
143
  if (tools == null) {
130
144
  return {
131
145
  toolConfig: { tools: void 0, toolChoice: void 0 },
@@ -149,7 +163,6 @@ function prepareTools(mode) {
149
163
  });
150
164
  }
151
165
  }
152
- const toolChoice = mode.toolChoice;
153
166
  if (toolChoice == null) {
154
167
  return {
155
168
  toolConfig: { tools: bedrockTools, toolChoice: void 0 },
@@ -184,7 +197,7 @@ function prepareTools(mode) {
184
197
  default: {
185
198
  const _exhaustiveCheck = type;
186
199
  throw new UnsupportedFunctionalityError({
187
- functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
200
+ functionality: `tool choice type: ${_exhaustiveCheck}`
188
201
  });
189
202
  }
190
203
  }
@@ -195,16 +208,17 @@ import {
195
208
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
196
209
  } from "@ai-sdk/provider";
197
210
  import {
198
- convertUint8ArrayToBase64,
199
- createIdGenerator
211
+ convertToBase64,
212
+ createIdGenerator,
213
+ parseProviderOptions
200
214
  } from "@ai-sdk/provider-utils";
201
215
  var generateFileId = createIdGenerator({ prefix: "file", size: 16 });
202
216
  function getCachePoint(providerMetadata) {
203
217
  var _a;
204
218
  return (_a = providerMetadata == null ? void 0 : providerMetadata.bedrock) == null ? void 0 : _a.cachePoint;
205
219
  }
206
- function convertToBedrockChatMessages(prompt) {
207
- var _a, _b, _c, _d, _e;
220
+ async function convertToBedrockChatMessages(prompt) {
221
+ var _a, _b, _c, _d;
208
222
  const blocks = groupIntoBlocks(prompt);
209
223
  let system = [];
210
224
  const messages = [];
@@ -221,7 +235,7 @@ function convertToBedrockChatMessages(prompt) {
221
235
  }
222
236
  for (const message of block.messages) {
223
237
  system.push({ text: message.content });
224
- if (getCachePoint(message.providerMetadata)) {
238
+ if (getCachePoint(message.providerOptions)) {
225
239
  system.push(BEDROCK_CACHE_POINT);
226
240
  }
227
241
  }
@@ -230,7 +244,7 @@ function convertToBedrockChatMessages(prompt) {
230
244
  case "user": {
231
245
  const bedrockContent = [];
232
246
  for (const message of block.messages) {
233
- const { role, content, providerMetadata } = message;
247
+ const { role, content, providerOptions } = message;
234
248
  switch (role) {
235
249
  case "user": {
236
250
  for (let j = 0; j < content.length; j++) {
@@ -242,43 +256,31 @@ function convertToBedrockChatMessages(prompt) {
242
256
  });
243
257
  break;
244
258
  }
245
- case "image": {
246
- if (part.image instanceof URL) {
247
- throw new UnsupportedFunctionalityError2({
248
- functionality: "Image URLs in user messages"
249
- });
250
- }
251
- bedrockContent.push({
252
- image: {
253
- format: (_b = (_a = part.mimeType) == null ? void 0 : _a.split(
254
- "/"
255
- )) == null ? void 0 : _b[1],
256
- source: {
257
- bytes: convertUint8ArrayToBase64(
258
- (_c = part.image) != null ? _c : part.image
259
- )
260
- }
261
- }
262
- });
263
- break;
264
- }
265
259
  case "file": {
266
260
  if (part.data instanceof URL) {
267
261
  throw new UnsupportedFunctionalityError2({
268
- functionality: "File URLs in user messages"
262
+ functionality: "File URL data"
269
263
  });
270
264
  }
271
- bedrockContent.push({
272
- document: {
273
- format: (_e = (_d = part.mimeType) == null ? void 0 : _d.split(
274
- "/"
275
- )) == null ? void 0 : _e[1],
276
- name: generateFileId(),
277
- source: {
278
- bytes: part.data
265
+ if (part.mediaType.startsWith("image/")) {
266
+ const bedrockImageFormat = part.mediaType === "image/*" ? void 0 : (_b = (_a = part.mediaType) == null ? void 0 : _a.split("/")) == null ? void 0 : _b[1];
267
+ bedrockContent.push({
268
+ image: {
269
+ format: bedrockImageFormat,
270
+ source: { bytes: convertToBase64(part.data) }
279
271
  }
280
- }
281
- });
272
+ });
273
+ } else {
274
+ bedrockContent.push({
275
+ document: {
276
+ format: (_d = (_c = part.mediaType) == null ? void 0 : _c.split(
277
+ "/"
278
+ )) == null ? void 0 : _d[1],
279
+ name: generateFileId(),
280
+ source: { bytes: convertToBase64(part.data) }
281
+ }
282
+ });
283
+ }
282
284
  break;
283
285
  }
284
286
  }
@@ -295,12 +297,12 @@ function convertToBedrockChatMessages(prompt) {
295
297
  text: part2.text
296
298
  };
297
299
  case "image":
298
- if (!part2.mimeType) {
300
+ if (!part2.mediaType) {
299
301
  throw new Error(
300
302
  "Image mime type is required in tool result part content"
301
303
  );
302
304
  }
303
- const format = part2.mimeType.split("/")[1];
305
+ const format = part2.mediaType.split("/")[1];
304
306
  if (!isBedrockImageFormat(format)) {
305
307
  throw new Error(
306
308
  `Unsupported image format: ${format}`
@@ -330,7 +332,7 @@ function convertToBedrockChatMessages(prompt) {
330
332
  throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
331
333
  }
332
334
  }
333
- if (getCachePoint(providerMetadata)) {
335
+ if (getCachePoint(providerOptions)) {
334
336
  bedrockContent.push(BEDROCK_CACHE_POINT);
335
337
  }
336
338
  }
@@ -364,32 +366,39 @@ function convertToBedrockChatMessages(prompt) {
364
366
  break;
365
367
  }
366
368
  case "reasoning": {
367
- bedrockContent.push({
368
- reasoningContent: {
369
- reasoningText: {
370
- // trim the last text part if it's the last message in the block
371
- // because Bedrock does not allow trailing whitespace
372
- // in pre-filled assistant responses
373
- text: trimIfLast(
374
- isLastBlock,
375
- isLastMessage,
376
- isLastContentPart,
377
- part.text
378
- ),
379
- signature: part.signature
380
- }
381
- }
369
+ const reasoningMetadata = await parseProviderOptions({
370
+ provider: "bedrock",
371
+ providerOptions: part.providerOptions,
372
+ schema: bedrockReasoningMetadataSchema
382
373
  });
383
- break;
384
- }
385
- case "redacted-reasoning": {
386
- bedrockContent.push({
387
- reasoningContent: {
388
- redactedReasoning: {
389
- data: part.data
390
- }
374
+ if (reasoningMetadata != null) {
375
+ if (reasoningMetadata.signature != null) {
376
+ bedrockContent.push({
377
+ reasoningContent: {
378
+ reasoningText: {
379
+ // trim the last text part if it's the last message in the block
380
+ // because Bedrock does not allow trailing whitespace
381
+ // in pre-filled assistant responses
382
+ text: trimIfLast(
383
+ isLastBlock,
384
+ isLastMessage,
385
+ isLastContentPart,
386
+ part.text
387
+ ),
388
+ signature: reasoningMetadata.signature
389
+ }
390
+ }
391
+ });
392
+ } else if (reasoningMetadata.redactedData != null) {
393
+ bedrockContent.push({
394
+ reasoningContent: {
395
+ redactedReasoning: {
396
+ data: reasoningMetadata.redactedData
397
+ }
398
+ }
399
+ });
391
400
  }
392
- });
401
+ }
393
402
  break;
394
403
  }
395
404
  case "tool-call": {
@@ -404,7 +413,7 @@ function convertToBedrockChatMessages(prompt) {
404
413
  }
405
414
  }
406
415
  }
407
- if (getCachePoint(message.providerMetadata)) {
416
+ if (getCachePoint(message.providerOptions)) {
408
417
  bedrockContent.push(BEDROCK_CACHE_POINT);
409
418
  }
410
419
  }
@@ -492,19 +501,15 @@ function mapBedrockFinishReason(finishReason) {
492
501
 
493
502
  // src/bedrock-chat-language-model.ts
494
503
  var BedrockChatLanguageModel = class {
495
- constructor(modelId, settings, config) {
504
+ constructor(modelId, config) {
496
505
  this.modelId = modelId;
497
- this.settings = settings;
498
506
  this.config = config;
499
507
  this.specificationVersion = "v2";
500
508
  this.provider = "amazon-bedrock";
501
- this.defaultObjectGenerationMode = "tool";
502
- this.supportsImageUrls = false;
503
509
  }
504
- getArgs({
505
- mode,
510
+ async getArgs({
506
511
  prompt,
507
- maxTokens,
512
+ maxOutputTokens,
508
513
  temperature,
509
514
  topP,
510
515
  topK,
@@ -513,10 +518,16 @@ var BedrockChatLanguageModel = class {
513
518
  stopSequences,
514
519
  responseFormat,
515
520
  seed,
516
- providerMetadata
521
+ tools,
522
+ toolChoice,
523
+ providerOptions
517
524
  }) {
518
- var _a, _b, _c, _d, _e, _f, _g;
519
- const type = mode.type;
525
+ var _a, _b, _c, _d, _e;
526
+ const bedrockOptions = (_a = await parseProviderOptions2({
527
+ provider: "bedrock",
528
+ providerOptions,
529
+ schema: bedrockProviderOptions
530
+ })) != null ? _a : {};
520
531
  const warnings = [];
521
532
  if (frequencyPenalty != null) {
522
533
  warnings.push({
@@ -549,35 +560,25 @@ var BedrockChatLanguageModel = class {
549
560
  details: "JSON response format is not supported."
550
561
  });
551
562
  }
552
- const { system, messages } = convertToBedrockChatMessages(prompt);
553
- const reasoningConfigOptions = BedrockReasoningConfigOptionsSchema.safeParse(
554
- (_a = providerMetadata == null ? void 0 : providerMetadata.bedrock) == null ? void 0 : _a.reasoning_config
555
- );
556
- if (!reasoningConfigOptions.success) {
557
- throw new InvalidArgumentError({
558
- argument: "providerOptions.bedrock.reasoning_config",
559
- message: "invalid reasoning configuration options",
560
- cause: reasoningConfigOptions.error
561
- });
562
- }
563
- const isThinking = ((_b = reasoningConfigOptions.data) == null ? void 0 : _b.type) === "enabled";
564
- const thinkingBudget = (_e = (_c = reasoningConfigOptions.data) == null ? void 0 : _c.budgetTokens) != null ? _e : (_d = reasoningConfigOptions.data) == null ? void 0 : _d.budget_tokens;
563
+ const { system, messages } = await convertToBedrockChatMessages(prompt);
564
+ const isThinking = ((_b = bedrockOptions.reasoningConfig) == null ? void 0 : _b.type) === "enabled";
565
+ const thinkingBudget = (_c = bedrockOptions.reasoningConfig) == null ? void 0 : _c.budgetTokens;
565
566
  const inferenceConfig = {
566
- ...maxTokens != null && { maxTokens },
567
+ ...maxOutputTokens != null && { maxOutputTokens },
567
568
  ...temperature != null && { temperature },
568
569
  ...topP != null && { topP },
569
570
  ...stopSequences != null && { stopSequences }
570
571
  };
571
572
  if (isThinking && thinkingBudget != null) {
572
- if (inferenceConfig.maxTokens != null) {
573
- inferenceConfig.maxTokens += thinkingBudget;
573
+ if (inferenceConfig.maxOutputTokens != null) {
574
+ inferenceConfig.maxOutputTokens += thinkingBudget;
574
575
  } else {
575
- inferenceConfig.maxTokens = thinkingBudget + 4096;
576
+ inferenceConfig.maxOutputTokens = thinkingBudget + 4096;
576
577
  }
577
- this.settings.additionalModelRequestFields = {
578
- ...this.settings.additionalModelRequestFields,
579
- reasoning_config: {
580
- type: (_f = reasoningConfigOptions.data) == null ? void 0 : _f.type,
578
+ bedrockOptions.additionalModelRequestFields = {
579
+ ...bedrockOptions.additionalModelRequestFields,
580
+ reasoningConfig: {
581
+ type: (_d = bedrockOptions.reasoningConfig) == null ? void 0 : _d.type,
581
582
  budget_tokens: thinkingBudget
582
583
  }
583
584
  };
@@ -598,62 +599,29 @@ var BedrockChatLanguageModel = class {
598
599
  details: "topP is not supported when thinking is enabled"
599
600
  });
600
601
  }
601
- const baseArgs = {
602
- system,
603
- additionalModelRequestFields: this.settings.additionalModelRequestFields,
604
- ...Object.keys(inferenceConfig).length > 0 && {
605
- inferenceConfig
602
+ const { toolConfig, toolWarnings } = prepareTools({ tools, toolChoice });
603
+ return {
604
+ command: {
605
+ system,
606
+ messages,
607
+ additionalModelRequestFields: bedrockOptions.additionalModelRequestFields,
608
+ ...Object.keys(inferenceConfig).length > 0 && {
609
+ inferenceConfig
610
+ },
611
+ ...providerOptions == null ? void 0 : providerOptions.bedrock,
612
+ ...((_e = toolConfig.tools) == null ? void 0 : _e.length) ? { toolConfig } : {}
606
613
  },
607
- messages,
608
- ...providerMetadata == null ? void 0 : providerMetadata.bedrock
614
+ warnings: [...warnings, ...toolWarnings]
615
+ };
616
+ }
617
+ async getSupportedUrls() {
618
+ return {
619
+ // no supported urls for bedrock
609
620
  };
610
- switch (type) {
611
- case "regular": {
612
- const { toolConfig, toolWarnings } = prepareTools(mode);
613
- return {
614
- command: {
615
- ...baseArgs,
616
- ...((_g = toolConfig.tools) == null ? void 0 : _g.length) ? { toolConfig } : {}
617
- },
618
- warnings: [...warnings, ...toolWarnings]
619
- };
620
- }
621
- case "object-json": {
622
- throw new UnsupportedFunctionalityError3({
623
- functionality: "json-mode object generation"
624
- });
625
- }
626
- case "object-tool": {
627
- return {
628
- command: {
629
- ...baseArgs,
630
- toolConfig: {
631
- tools: [
632
- {
633
- toolSpec: {
634
- name: mode.tool.name,
635
- description: mode.tool.description,
636
- inputSchema: {
637
- json: mode.tool.parameters
638
- }
639
- }
640
- }
641
- ],
642
- toolChoice: { tool: { name: mode.tool.name } }
643
- }
644
- },
645
- warnings
646
- };
647
- }
648
- default: {
649
- const _exhaustiveCheck = type;
650
- throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
651
- }
652
- }
653
621
  }
654
622
  async doGenerate(options) {
655
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
656
- const { command: args, warnings } = this.getArgs(options);
623
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
624
+ const { command: args, warnings } = await this.getArgs(options);
657
625
  const url = `${this.getUrl(this.modelId)}/converse`;
658
626
  const { value: response, responseHeaders } = await postJsonToApi({
659
627
  url,
@@ -675,67 +643,77 @@ var BedrockChatLanguageModel = class {
675
643
  abortSignal: options.abortSignal,
676
644
  fetch: this.config.fetch
677
645
  });
678
- const { messages: rawPrompt, ...rawSettings } = args;
646
+ const content = [];
647
+ for (const part of response.output.message.content) {
648
+ if (part.text) {
649
+ content.push({ type: "text", text: part.text });
650
+ }
651
+ if (part.reasoningContent) {
652
+ if ("reasoningText" in part.reasoningContent) {
653
+ const reasoning = {
654
+ type: "reasoning",
655
+ text: part.reasoningContent.reasoningText.text
656
+ };
657
+ if (part.reasoningContent.reasoningText.signature) {
658
+ reasoning.providerMetadata = {
659
+ bedrock: {
660
+ signature: part.reasoningContent.reasoningText.signature
661
+ }
662
+ };
663
+ }
664
+ content.push(reasoning);
665
+ } else if ("redactedReasoning" in part.reasoningContent) {
666
+ content.push({
667
+ type: "reasoning",
668
+ text: "",
669
+ providerMetadata: {
670
+ bedrock: {
671
+ redactedData: (_a = part.reasoningContent.redactedReasoning.data) != null ? _a : ""
672
+ }
673
+ }
674
+ });
675
+ }
676
+ }
677
+ if (part.toolUse) {
678
+ content.push({
679
+ type: "tool-call",
680
+ toolCallType: "function",
681
+ toolCallId: (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId(),
682
+ toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
683
+ args: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : "")
684
+ });
685
+ }
686
+ }
679
687
  const providerMetadata = response.trace || response.usage ? {
680
688
  bedrock: {
681
689
  ...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
682
690
  ...response.usage && {
683
691
  usage: {
684
- cacheReadInputTokens: (_b = (_a = response.usage) == null ? void 0 : _a.cacheReadInputTokens) != null ? _b : Number.NaN,
685
- cacheWriteInputTokens: (_d = (_c = response.usage) == null ? void 0 : _c.cacheWriteInputTokens) != null ? _d : Number.NaN
692
+ cacheReadInputTokens: (_i = (_h = response.usage) == null ? void 0 : _h.cacheReadInputTokens) != null ? _i : Number.NaN,
693
+ cacheWriteInputTokens: (_k = (_j = response.usage) == null ? void 0 : _j.cacheWriteInputTokens) != null ? _k : Number.NaN
686
694
  }
687
695
  }
688
696
  }
689
697
  } : void 0;
690
- const reasoning = response.output.message.content.filter((content) => content.reasoningContent).map((content) => {
691
- var _a2;
692
- if (content.reasoningContent && "reasoningText" in content.reasoningContent) {
693
- return {
694
- type: "text",
695
- text: content.reasoningContent.reasoningText.text,
696
- ...content.reasoningContent.reasoningText.signature && {
697
- signature: content.reasoningContent.reasoningText.signature
698
- }
699
- };
700
- } else if (content.reasoningContent && "redactedReasoning" in content.reasoningContent) {
701
- return {
702
- type: "redacted",
703
- data: (_a2 = content.reasoningContent.redactedReasoning.data) != null ? _a2 : ""
704
- };
705
- } else {
706
- return void 0;
707
- }
708
- }).filter((item) => item !== void 0);
709
698
  return {
710
- text: (_h = (_g = (_f = (_e = response.output) == null ? void 0 : _e.message) == null ? void 0 : _f.content) == null ? void 0 : _g.map((part) => {
711
- var _a2;
712
- return (_a2 = part.text) != null ? _a2 : "";
713
- }).join("")) != null ? _h : void 0,
714
- toolCalls: (_l = (_k = (_j = (_i = response.output) == null ? void 0 : _i.message) == null ? void 0 : _j.content) == null ? void 0 : _k.filter((part) => !!part.toolUse)) == null ? void 0 : _l.map((part) => {
715
- var _a2, _b2, _c2, _d2, _e2, _f2;
716
- return {
717
- toolCallType: "function",
718
- toolCallId: (_b2 = (_a2 = part.toolUse) == null ? void 0 : _a2.toolUseId) != null ? _b2 : this.config.generateId(),
719
- toolName: (_d2 = (_c2 = part.toolUse) == null ? void 0 : _c2.name) != null ? _d2 : `tool-${this.config.generateId()}`,
720
- args: JSON.stringify((_f2 = (_e2 = part.toolUse) == null ? void 0 : _e2.input) != null ? _f2 : "")
721
- };
722
- }),
699
+ content,
723
700
  finishReason: mapBedrockFinishReason(
724
701
  response.stopReason
725
702
  ),
726
703
  usage: {
727
- promptTokens: (_n = (_m = response.usage) == null ? void 0 : _m.inputTokens) != null ? _n : Number.NaN,
728
- completionTokens: (_p = (_o = response.usage) == null ? void 0 : _o.outputTokens) != null ? _p : Number.NaN
704
+ inputTokens: (_l = response.usage) == null ? void 0 : _l.inputTokens,
705
+ outputTokens: (_m = response.usage) == null ? void 0 : _m.outputTokens
706
+ },
707
+ response: {
708
+ // TODO add id, timestamp, etc
709
+ headers: responseHeaders
729
710
  },
730
- rawCall: { rawPrompt, rawSettings },
731
- rawResponse: { headers: responseHeaders },
732
711
  warnings,
733
- reasoning: reasoning.length > 0 ? reasoning : void 0,
734
712
  ...providerMetadata && { providerMetadata }
735
713
  };
736
714
  }
737
715
  async doStream(options) {
738
- const { command: args, warnings } = this.getArgs(options);
716
+ const { command: args, warnings } = await this.getArgs(options);
739
717
  const url = `${this.getUrl(this.modelId)}/converse-stream`;
740
718
  const { value: response, responseHeaders } = await postJsonToApi({
741
719
  url,
@@ -752,17 +730,19 @@ var BedrockChatLanguageModel = class {
752
730
  abortSignal: options.abortSignal,
753
731
  fetch: this.config.fetch
754
732
  });
755
- const { messages: rawPrompt, ...rawSettings } = args;
756
733
  let finishReason = "unknown";
757
- let usage = {
758
- promptTokens: Number.NaN,
759
- completionTokens: Number.NaN
734
+ const usage = {
735
+ inputTokens: void 0,
736
+ outputTokens: void 0
760
737
  };
761
738
  let providerMetadata = void 0;
762
739
  const toolCallContentBlocks = {};
763
740
  return {
764
741
  stream: response.pipeThrough(
765
742
  new TransformStream({
743
+ start(controller) {
744
+ controller.enqueue({ type: "stream-start", warnings });
745
+ },
766
746
  transform(chunk, controller) {
767
747
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
768
748
  function enqueueError(bedrockError) {
@@ -796,10 +776,8 @@ var BedrockChatLanguageModel = class {
796
776
  );
797
777
  }
798
778
  if (value.metadata) {
799
- usage = {
800
- promptTokens: (_b = (_a = value.metadata.usage) == null ? void 0 : _a.inputTokens) != null ? _b : Number.NaN,
801
- completionTokens: (_d = (_c = value.metadata.usage) == null ? void 0 : _c.outputTokens) != null ? _d : Number.NaN
802
- };
779
+ usage.inputTokens = (_b = (_a = value.metadata.usage) == null ? void 0 : _a.inputTokens) != null ? _b : usage.inputTokens;
780
+ usage.outputTokens = (_d = (_c = value.metadata.usage) == null ? void 0 : _c.outputTokens) != null ? _d : usage.outputTokens;
803
781
  const cacheUsage = ((_e = value.metadata.usage) == null ? void 0 : _e.cacheReadInputTokens) != null || ((_f = value.metadata.usage) == null ? void 0 : _f.cacheWriteInputTokens) != null ? {
804
782
  usage: {
805
783
  cacheReadInputTokens: (_h = (_g = value.metadata.usage) == null ? void 0 : _g.cacheReadInputTokens) != null ? _h : Number.NaN,
@@ -820,8 +798,8 @@ var BedrockChatLanguageModel = class {
820
798
  }
821
799
  if (((_k = value.contentBlockDelta) == null ? void 0 : _k.delta) && "text" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.text) {
822
800
  controller.enqueue({
823
- type: "text-delta",
824
- textDelta: value.contentBlockDelta.delta.text
801
+ type: "text",
802
+ text: value.contentBlockDelta.delta.text
825
803
  });
826
804
  }
827
805
  if (((_l = value.contentBlockDelta) == null ? void 0 : _l.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
@@ -829,18 +807,30 @@ var BedrockChatLanguageModel = class {
829
807
  if ("text" in reasoningContent && reasoningContent.text) {
830
808
  controller.enqueue({
831
809
  type: "reasoning",
832
- textDelta: reasoningContent.text
810
+ text: reasoningContent.text
833
811
  });
834
812
  } else if ("signature" in reasoningContent && reasoningContent.signature) {
835
813
  controller.enqueue({
836
- type: "reasoning-signature",
837
- signature: reasoningContent.signature
814
+ type: "reasoning",
815
+ text: "",
816
+ providerMetadata: {
817
+ bedrock: {
818
+ signature: reasoningContent.signature
819
+ }
820
+ }
838
821
  });
822
+ controller.enqueue({ type: "reasoning-part-finish" });
839
823
  } else if ("data" in reasoningContent && reasoningContent.data) {
840
824
  controller.enqueue({
841
- type: "redacted-reasoning",
842
- data: reasoningContent.data
825
+ type: "reasoning",
826
+ text: "",
827
+ providerMetadata: {
828
+ bedrock: {
829
+ redactedData: reasoningContent.data
830
+ }
831
+ }
843
832
  });
833
+ controller.enqueue({ type: "reasoning-part-finish" });
844
834
  }
845
835
  }
846
836
  const contentBlockStart = value.contentBlockStart;
@@ -891,9 +881,8 @@ var BedrockChatLanguageModel = class {
891
881
  }
892
882
  })
893
883
  ),
894
- rawCall: { rawPrompt, rawSettings },
895
- rawResponse: { headers: responseHeaders },
896
- warnings
884
+ // TODO request?
885
+ response: { headers: responseHeaders }
897
886
  };
898
887
  }
899
888
  getUrl(modelId) {
@@ -901,105 +890,104 @@ var BedrockChatLanguageModel = class {
901
890
  return `${this.config.baseUrl()}/model/${encodedModelId}`;
902
891
  }
903
892
  };
904
- var BedrockReasoningConfigOptionsSchema = z2.object({
905
- type: z2.union([z2.literal("enabled"), z2.literal("disabled")]).nullish(),
906
- budget_tokens: z2.number().nullish(),
907
- budgetTokens: z2.number().nullish()
908
- }).nullish();
909
- var BedrockStopReasonSchema = z2.union([
910
- z2.enum(BEDROCK_STOP_REASONS),
911
- z2.string()
893
+ var BedrockStopReasonSchema = z3.union([
894
+ z3.enum(BEDROCK_STOP_REASONS),
895
+ z3.string()
912
896
  ]);
913
- var BedrockToolUseSchema = z2.object({
914
- toolUseId: z2.string(),
915
- name: z2.string(),
916
- input: z2.unknown()
897
+ var BedrockToolUseSchema = z3.object({
898
+ toolUseId: z3.string(),
899
+ name: z3.string(),
900
+ input: z3.unknown()
917
901
  });
918
- var BedrockReasoningTextSchema = z2.object({
919
- signature: z2.string().nullish(),
920
- text: z2.string()
902
+ var BedrockReasoningTextSchema = z3.object({
903
+ signature: z3.string().nullish(),
904
+ text: z3.string()
921
905
  });
922
- var BedrockRedactedReasoningSchema = z2.object({
923
- data: z2.string()
906
+ var BedrockRedactedReasoningSchema = z3.object({
907
+ data: z3.string()
924
908
  });
925
- var BedrockResponseSchema = z2.object({
926
- metrics: z2.object({
927
- latencyMs: z2.number()
909
+ var BedrockResponseSchema = z3.object({
910
+ metrics: z3.object({
911
+ latencyMs: z3.number()
928
912
  }).nullish(),
929
- output: z2.object({
930
- message: z2.object({
931
- content: z2.array(
932
- z2.object({
933
- text: z2.string().nullish(),
913
+ output: z3.object({
914
+ message: z3.object({
915
+ content: z3.array(
916
+ z3.object({
917
+ text: z3.string().nullish(),
934
918
  toolUse: BedrockToolUseSchema.nullish(),
935
- reasoningContent: z2.union([
936
- z2.object({
919
+ reasoningContent: z3.union([
920
+ z3.object({
937
921
  reasoningText: BedrockReasoningTextSchema
938
922
  }),
939
- z2.object({
923
+ z3.object({
940
924
  redactedReasoning: BedrockRedactedReasoningSchema
941
925
  })
942
926
  ]).nullish()
943
927
  })
944
928
  ),
945
- role: z2.string()
929
+ role: z3.string()
946
930
  })
947
931
  }),
948
932
  stopReason: BedrockStopReasonSchema,
949
- trace: z2.unknown().nullish(),
950
- usage: z2.object({
951
- inputTokens: z2.number(),
952
- outputTokens: z2.number(),
953
- totalTokens: z2.number(),
954
- cacheReadInputTokens: z2.number().nullish(),
955
- cacheWriteInputTokens: z2.number().nullish()
933
+ trace: z3.unknown().nullish(),
934
+ usage: z3.object({
935
+ inputTokens: z3.number(),
936
+ outputTokens: z3.number(),
937
+ totalTokens: z3.number(),
938
+ cacheReadInputTokens: z3.number().nullish(),
939
+ cacheWriteInputTokens: z3.number().nullish()
956
940
  })
957
941
  });
958
- var BedrockStreamSchema = z2.object({
959
- contentBlockDelta: z2.object({
960
- contentBlockIndex: z2.number(),
961
- delta: z2.union([
962
- z2.object({ text: z2.string() }),
963
- z2.object({ toolUse: z2.object({ input: z2.string() }) }),
964
- z2.object({
965
- reasoningContent: z2.object({ text: z2.string() })
942
+ var BedrockStreamSchema = z3.object({
943
+ contentBlockDelta: z3.object({
944
+ contentBlockIndex: z3.number(),
945
+ delta: z3.union([
946
+ z3.object({ text: z3.string() }),
947
+ z3.object({ toolUse: z3.object({ input: z3.string() }) }),
948
+ z3.object({
949
+ reasoningContent: z3.object({ text: z3.string() })
966
950
  }),
967
- z2.object({
968
- reasoningContent: z2.object({
969
- signature: z2.string()
951
+ z3.object({
952
+ reasoningContent: z3.object({
953
+ signature: z3.string()
970
954
  })
971
955
  }),
972
- z2.object({
973
- reasoningContent: z2.object({ data: z2.string() })
956
+ z3.object({
957
+ reasoningContent: z3.object({ data: z3.string() })
974
958
  })
975
959
  ]).nullish()
976
960
  }).nullish(),
977
- contentBlockStart: z2.object({
978
- contentBlockIndex: z2.number(),
979
- start: z2.object({
961
+ contentBlockStart: z3.object({
962
+ contentBlockIndex: z3.number(),
963
+ start: z3.object({
980
964
  toolUse: BedrockToolUseSchema.nullish()
981
965
  }).nullish()
982
966
  }).nullish(),
983
- contentBlockStop: z2.object({
984
- contentBlockIndex: z2.number()
967
+ contentBlockStop: z3.object({
968
+ contentBlockIndex: z3.number()
985
969
  }).nullish(),
986
- internalServerException: z2.record(z2.unknown()).nullish(),
987
- messageStop: z2.object({
988
- additionalModelResponseFields: z2.record(z2.unknown()).nullish(),
970
+ internalServerException: z3.record(z3.unknown()).nullish(),
971
+ messageStop: z3.object({
972
+ additionalModelResponseFields: z3.record(z3.unknown()).nullish(),
989
973
  stopReason: BedrockStopReasonSchema
990
974
  }).nullish(),
991
- metadata: z2.object({
992
- trace: z2.unknown().nullish(),
993
- usage: z2.object({
994
- cacheReadInputTokens: z2.number().nullish(),
995
- cacheWriteInputTokens: z2.number().nullish(),
996
- inputTokens: z2.number(),
997
- outputTokens: z2.number()
975
+ metadata: z3.object({
976
+ trace: z3.unknown().nullish(),
977
+ usage: z3.object({
978
+ cacheReadInputTokens: z3.number().nullish(),
979
+ cacheWriteInputTokens: z3.number().nullish(),
980
+ inputTokens: z3.number(),
981
+ outputTokens: z3.number()
998
982
  }).nullish()
999
983
  }).nullish(),
1000
- modelStreamErrorException: z2.record(z2.unknown()).nullish(),
1001
- throttlingException: z2.record(z2.unknown()).nullish(),
1002
- validationException: z2.record(z2.unknown()).nullish()
984
+ modelStreamErrorException: z3.record(z3.unknown()).nullish(),
985
+ throttlingException: z3.record(z3.unknown()).nullish(),
986
+ validationException: z3.record(z3.unknown()).nullish()
987
+ });
988
+ var bedrockReasoningMetadataSchema = z3.object({
989
+ signature: z3.string().optional(),
990
+ redactedData: z3.string().optional()
1003
991
  });
1004
992
 
1005
993
  // src/bedrock-embedding-model.ts
@@ -1007,16 +995,33 @@ import {
1007
995
  combineHeaders as combineHeaders2,
1008
996
  createJsonErrorResponseHandler as createJsonErrorResponseHandler2,
1009
997
  createJsonResponseHandler as createJsonResponseHandler2,
998
+ parseProviderOptions as parseProviderOptions3,
1010
999
  postJsonToApi as postJsonToApi2,
1011
1000
  resolve as resolve2
1012
1001
  } from "@ai-sdk/provider-utils";
1013
- import { z as z3 } from "zod";
1002
+
1003
+ // src/bedrock-embedding-options.ts
1004
+ import { z as z4 } from "zod";
1005
+ var bedrockEmbeddingProviderOptions = z4.object({
1006
+ /**
1007
+ The number of dimensions the resulting output embeddings should have (defaults to 1024).
1008
+ Only supported in amazon.titan-embed-text-v2:0.
1009
+ */
1010
+ dimensions: z4.union([z4.literal(1024), z4.literal(512), z4.literal(256)]).optional(),
1011
+ /**
1012
+ Flag indicating whether or not to normalize the output embeddings. Defaults to true
1013
+ Only supported in amazon.titan-embed-text-v2:0.
1014
+ */
1015
+ normalize: z4.boolean().optional()
1016
+ });
1017
+
1018
+ // src/bedrock-embedding-model.ts
1019
+ import { z as z5 } from "zod";
1014
1020
  var BedrockEmbeddingModel = class {
1015
- constructor(modelId, settings, config) {
1021
+ constructor(modelId, config) {
1016
1022
  this.modelId = modelId;
1017
- this.settings = settings;
1018
1023
  this.config = config;
1019
- this.specificationVersion = "v1";
1024
+ this.specificationVersion = "v2";
1020
1025
  this.provider = "amazon-bedrock";
1021
1026
  this.maxEmbeddingsPerCall = void 0;
1022
1027
  this.supportsParallelCalls = true;
@@ -1028,13 +1033,20 @@ var BedrockEmbeddingModel = class {
1028
1033
  async doEmbed({
1029
1034
  values,
1030
1035
  headers,
1031
- abortSignal
1036
+ abortSignal,
1037
+ providerOptions
1032
1038
  }) {
1039
+ var _a;
1040
+ const bedrockOptions = (_a = await parseProviderOptions3({
1041
+ provider: "bedrock",
1042
+ providerOptions,
1043
+ schema: bedrockEmbeddingProviderOptions
1044
+ })) != null ? _a : {};
1033
1045
  const embedSingleText = async (inputText) => {
1034
1046
  const args = {
1035
1047
  inputText,
1036
- dimensions: this.settings.dimensions,
1037
- normalize: this.settings.normalize
1048
+ dimensions: bedrockOptions.dimensions,
1049
+ normalize: bedrockOptions.normalize
1038
1050
  };
1039
1051
  const url = this.getUrl(this.modelId);
1040
1052
  const { value: response } = await postJsonToApi2({
@@ -1069,9 +1081,9 @@ var BedrockEmbeddingModel = class {
1069
1081
  );
1070
1082
  }
1071
1083
  };
1072
- var BedrockEmbeddingResponseSchema = z3.object({
1073
- embedding: z3.array(z3.number()),
1074
- inputTextTokenCount: z3.number()
1084
+ var BedrockEmbeddingResponseSchema = z5.object({
1085
+ embedding: z5.array(z5.number()),
1086
+ inputTextTokenCount: z5.number()
1075
1087
  });
1076
1088
 
1077
1089
  // src/bedrock-image-model.ts
@@ -1089,7 +1101,7 @@ var modelMaxImagesPerCall = {
1089
1101
  };
1090
1102
 
1091
1103
  // src/bedrock-image-model.ts
1092
- import { z as z4 } from "zod";
1104
+ import { z as z6 } from "zod";
1093
1105
  var BedrockImageModel = class {
1094
1106
  constructor(modelId, settings, config) {
1095
1107
  this.modelId = modelId;
@@ -1171,8 +1183,8 @@ var BedrockImageModel = class {
1171
1183
  };
1172
1184
  }
1173
1185
  };
1174
- var bedrockImageResponseSchema = z4.object({
1175
- images: z4.array(z4.string())
1186
+ var bedrockImageResponseSchema = z6.object({
1187
+ images: z6.array(z6.string())
1176
1188
  });
1177
1189
 
1178
1190
  // src/headers-utils.ts
@@ -1297,26 +1309,26 @@ function createAmazonBedrock(options = {}) {
1297
1309
  })}.amazonaws.com`
1298
1310
  )) != null ? _b : `https://bedrock-runtime.us-east-1.amazonaws.com`;
1299
1311
  };
1300
- const createChatModel = (modelId, settings = {}) => {
1312
+ const createChatModel = (modelId) => {
1301
1313
  var _a;
1302
- return new BedrockChatLanguageModel(modelId, settings, {
1314
+ return new BedrockChatLanguageModel(modelId, {
1303
1315
  baseUrl: getBaseUrl,
1304
1316
  headers: (_a = options.headers) != null ? _a : {},
1305
1317
  fetch: sigv4Fetch,
1306
1318
  generateId
1307
1319
  });
1308
1320
  };
1309
- const provider = function(modelId, settings) {
1321
+ const provider = function(modelId) {
1310
1322
  if (new.target) {
1311
1323
  throw new Error(
1312
1324
  "The Amazon Bedrock model function cannot be called with the new keyword."
1313
1325
  );
1314
1326
  }
1315
- return createChatModel(modelId, settings);
1327
+ return createChatModel(modelId);
1316
1328
  };
1317
- const createEmbeddingModel = (modelId, settings = {}) => {
1329
+ const createEmbeddingModel = (modelId) => {
1318
1330
  var _a;
1319
- return new BedrockEmbeddingModel(modelId, settings, {
1331
+ return new BedrockEmbeddingModel(modelId, {
1320
1332
  baseUrl: getBaseUrl,
1321
1333
  headers: (_a = options.headers) != null ? _a : {},
1322
1334
  fetch: sigv4Fetch