@ai-sdk/amazon-bedrock 3.0.0-canary.1 → 3.0.0-canary.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +101 -0
- package/dist/index.d.mts +37 -24
- package/dist/index.d.ts +37 -24
- package/dist/index.js +259 -271
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +262 -278
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -9
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,
|
|
18
15
|
postJsonToApi,
|
|
19
16
|
resolve
|
|
20
17
|
} from "@ai-sdk/provider-utils";
|
|
21
|
-
import { z as
|
|
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-
|
|
37
|
+
// src/bedrock-chat-options.ts
|
|
41
38
|
import { z } from "zod";
|
|
42
|
-
var
|
|
43
|
-
|
|
44
|
-
|
|
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
|
|
@@ -123,9 +135,11 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
|
|
|
123
135
|
import {
|
|
124
136
|
UnsupportedFunctionalityError
|
|
125
137
|
} from "@ai-sdk/provider";
|
|
126
|
-
function prepareTools(
|
|
127
|
-
|
|
128
|
-
|
|
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: `
|
|
200
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
188
201
|
});
|
|
189
202
|
}
|
|
190
203
|
}
|
|
@@ -194,17 +207,14 @@ function prepareTools(mode) {
|
|
|
194
207
|
import {
|
|
195
208
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
196
209
|
} from "@ai-sdk/provider";
|
|
197
|
-
import {
|
|
198
|
-
convertUint8ArrayToBase64,
|
|
199
|
-
createIdGenerator
|
|
200
|
-
} from "@ai-sdk/provider-utils";
|
|
210
|
+
import { convertToBase64, createIdGenerator } from "@ai-sdk/provider-utils";
|
|
201
211
|
var generateFileId = createIdGenerator({ prefix: "file", size: 16 });
|
|
202
212
|
function getCachePoint(providerMetadata) {
|
|
203
213
|
var _a;
|
|
204
214
|
return (_a = providerMetadata == null ? void 0 : providerMetadata.bedrock) == null ? void 0 : _a.cachePoint;
|
|
205
215
|
}
|
|
206
216
|
function convertToBedrockChatMessages(prompt) {
|
|
207
|
-
var _a, _b, _c, _d
|
|
217
|
+
var _a, _b, _c, _d;
|
|
208
218
|
const blocks = groupIntoBlocks(prompt);
|
|
209
219
|
let system = [];
|
|
210
220
|
const messages = [];
|
|
@@ -221,7 +231,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
221
231
|
}
|
|
222
232
|
for (const message of block.messages) {
|
|
223
233
|
system.push({ text: message.content });
|
|
224
|
-
if (getCachePoint(message.
|
|
234
|
+
if (getCachePoint(message.providerOptions)) {
|
|
225
235
|
system.push(BEDROCK_CACHE_POINT);
|
|
226
236
|
}
|
|
227
237
|
}
|
|
@@ -230,7 +240,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
230
240
|
case "user": {
|
|
231
241
|
const bedrockContent = [];
|
|
232
242
|
for (const message of block.messages) {
|
|
233
|
-
const { role, content,
|
|
243
|
+
const { role, content, providerOptions } = message;
|
|
234
244
|
switch (role) {
|
|
235
245
|
case "user": {
|
|
236
246
|
for (let j = 0; j < content.length; j++) {
|
|
@@ -242,43 +252,31 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
242
252
|
});
|
|
243
253
|
break;
|
|
244
254
|
}
|
|
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
255
|
case "file": {
|
|
266
256
|
if (part.data instanceof URL) {
|
|
267
257
|
throw new UnsupportedFunctionalityError2({
|
|
268
|
-
functionality: "File
|
|
258
|
+
functionality: "File URL data"
|
|
269
259
|
});
|
|
270
260
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
source: {
|
|
278
|
-
bytes: part.data
|
|
261
|
+
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
|
+
bedrockContent.push({
|
|
264
|
+
image: {
|
|
265
|
+
format: bedrockImageFormat,
|
|
266
|
+
source: { bytes: convertToBase64(part.data) }
|
|
279
267
|
}
|
|
280
|
-
}
|
|
281
|
-
}
|
|
268
|
+
});
|
|
269
|
+
} else {
|
|
270
|
+
bedrockContent.push({
|
|
271
|
+
document: {
|
|
272
|
+
format: (_d = (_c = part.mediaType) == null ? void 0 : _c.split(
|
|
273
|
+
"/"
|
|
274
|
+
)) == null ? void 0 : _d[1],
|
|
275
|
+
name: generateFileId(),
|
|
276
|
+
source: { bytes: convertToBase64(part.data) }
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
}
|
|
282
280
|
break;
|
|
283
281
|
}
|
|
284
282
|
}
|
|
@@ -295,12 +293,12 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
295
293
|
text: part2.text
|
|
296
294
|
};
|
|
297
295
|
case "image":
|
|
298
|
-
if (!part2.
|
|
296
|
+
if (!part2.mediaType) {
|
|
299
297
|
throw new Error(
|
|
300
298
|
"Image mime type is required in tool result part content"
|
|
301
299
|
);
|
|
302
300
|
}
|
|
303
|
-
const format = part2.
|
|
301
|
+
const format = part2.mediaType.split("/")[1];
|
|
304
302
|
if (!isBedrockImageFormat(format)) {
|
|
305
303
|
throw new Error(
|
|
306
304
|
`Unsupported image format: ${format}`
|
|
@@ -330,7 +328,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
330
328
|
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
331
329
|
}
|
|
332
330
|
}
|
|
333
|
-
if (getCachePoint(
|
|
331
|
+
if (getCachePoint(providerOptions)) {
|
|
334
332
|
bedrockContent.push(BEDROCK_CACHE_POINT);
|
|
335
333
|
}
|
|
336
334
|
}
|
|
@@ -404,7 +402,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
404
402
|
}
|
|
405
403
|
}
|
|
406
404
|
}
|
|
407
|
-
if (getCachePoint(message.
|
|
405
|
+
if (getCachePoint(message.providerOptions)) {
|
|
408
406
|
bedrockContent.push(BEDROCK_CACHE_POINT);
|
|
409
407
|
}
|
|
410
408
|
}
|
|
@@ -492,19 +490,15 @@ function mapBedrockFinishReason(finishReason) {
|
|
|
492
490
|
|
|
493
491
|
// src/bedrock-chat-language-model.ts
|
|
494
492
|
var BedrockChatLanguageModel = class {
|
|
495
|
-
constructor(modelId,
|
|
493
|
+
constructor(modelId, config) {
|
|
496
494
|
this.modelId = modelId;
|
|
497
|
-
this.settings = settings;
|
|
498
495
|
this.config = config;
|
|
499
496
|
this.specificationVersion = "v2";
|
|
500
497
|
this.provider = "amazon-bedrock";
|
|
501
|
-
this.defaultObjectGenerationMode = "tool";
|
|
502
|
-
this.supportsImageUrls = false;
|
|
503
498
|
}
|
|
504
499
|
getArgs({
|
|
505
|
-
mode,
|
|
506
500
|
prompt,
|
|
507
|
-
|
|
501
|
+
maxOutputTokens,
|
|
508
502
|
temperature,
|
|
509
503
|
topP,
|
|
510
504
|
topK,
|
|
@@ -513,10 +507,16 @@ var BedrockChatLanguageModel = class {
|
|
|
513
507
|
stopSequences,
|
|
514
508
|
responseFormat,
|
|
515
509
|
seed,
|
|
516
|
-
|
|
510
|
+
tools,
|
|
511
|
+
toolChoice,
|
|
512
|
+
providerOptions
|
|
517
513
|
}) {
|
|
518
|
-
var _a, _b, _c, _d, _e
|
|
519
|
-
const
|
|
514
|
+
var _a, _b, _c, _d, _e;
|
|
515
|
+
const bedrockOptions = (_a = parseProviderOptions({
|
|
516
|
+
provider: "bedrock",
|
|
517
|
+
providerOptions,
|
|
518
|
+
schema: bedrockProviderOptions
|
|
519
|
+
})) != null ? _a : {};
|
|
520
520
|
const warnings = [];
|
|
521
521
|
if (frequencyPenalty != null) {
|
|
522
522
|
warnings.push({
|
|
@@ -550,34 +550,24 @@ var BedrockChatLanguageModel = class {
|
|
|
550
550
|
});
|
|
551
551
|
}
|
|
552
552
|
const { system, messages } = convertToBedrockChatMessages(prompt);
|
|
553
|
-
const
|
|
554
|
-
|
|
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;
|
|
553
|
+
const isThinking = ((_b = bedrockOptions.reasoningConfig) == null ? void 0 : _b.type) === "enabled";
|
|
554
|
+
const thinkingBudget = (_c = bedrockOptions.reasoningConfig) == null ? void 0 : _c.budgetTokens;
|
|
565
555
|
const inferenceConfig = {
|
|
566
|
-
...
|
|
556
|
+
...maxOutputTokens != null && { maxOutputTokens },
|
|
567
557
|
...temperature != null && { temperature },
|
|
568
558
|
...topP != null && { topP },
|
|
569
559
|
...stopSequences != null && { stopSequences }
|
|
570
560
|
};
|
|
571
561
|
if (isThinking && thinkingBudget != null) {
|
|
572
|
-
if (inferenceConfig.
|
|
573
|
-
inferenceConfig.
|
|
562
|
+
if (inferenceConfig.maxOutputTokens != null) {
|
|
563
|
+
inferenceConfig.maxOutputTokens += thinkingBudget;
|
|
574
564
|
} else {
|
|
575
|
-
inferenceConfig.
|
|
565
|
+
inferenceConfig.maxOutputTokens = thinkingBudget + 4096;
|
|
576
566
|
}
|
|
577
|
-
|
|
578
|
-
...
|
|
579
|
-
|
|
580
|
-
type: (
|
|
567
|
+
bedrockOptions.additionalModelRequestFields = {
|
|
568
|
+
...bedrockOptions.additionalModelRequestFields,
|
|
569
|
+
reasoningConfig: {
|
|
570
|
+
type: (_d = bedrockOptions.reasoningConfig) == null ? void 0 : _d.type,
|
|
581
571
|
budget_tokens: thinkingBudget
|
|
582
572
|
}
|
|
583
573
|
};
|
|
@@ -598,61 +588,28 @@ var BedrockChatLanguageModel = class {
|
|
|
598
588
|
details: "topP is not supported when thinking is enabled"
|
|
599
589
|
});
|
|
600
590
|
}
|
|
601
|
-
const
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
591
|
+
const { toolConfig, toolWarnings } = prepareTools({ tools, toolChoice });
|
|
592
|
+
return {
|
|
593
|
+
command: {
|
|
594
|
+
system,
|
|
595
|
+
messages,
|
|
596
|
+
additionalModelRequestFields: bedrockOptions.additionalModelRequestFields,
|
|
597
|
+
...Object.keys(inferenceConfig).length > 0 && {
|
|
598
|
+
inferenceConfig
|
|
599
|
+
},
|
|
600
|
+
...providerOptions == null ? void 0 : providerOptions.bedrock,
|
|
601
|
+
...((_e = toolConfig.tools) == null ? void 0 : _e.length) ? { toolConfig } : {}
|
|
606
602
|
},
|
|
607
|
-
|
|
608
|
-
|
|
603
|
+
warnings: [...warnings, ...toolWarnings]
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
async getSupportedUrls() {
|
|
607
|
+
return {
|
|
608
|
+
// no supported urls for bedrock
|
|
609
609
|
};
|
|
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
610
|
}
|
|
654
611
|
async doGenerate(options) {
|
|
655
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m
|
|
612
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
656
613
|
const { command: args, warnings } = this.getArgs(options);
|
|
657
614
|
const url = `${this.getUrl(this.modelId)}/converse`;
|
|
658
615
|
const { value: response, responseHeaders } = await postJsonToApi({
|
|
@@ -675,62 +632,68 @@ var BedrockChatLanguageModel = class {
|
|
|
675
632
|
abortSignal: options.abortSignal,
|
|
676
633
|
fetch: this.config.fetch
|
|
677
634
|
});
|
|
678
|
-
const
|
|
635
|
+
const content = [];
|
|
636
|
+
for (const part of response.output.message.content) {
|
|
637
|
+
if (part.text) {
|
|
638
|
+
content.push({ type: "text", text: part.text });
|
|
639
|
+
}
|
|
640
|
+
if (part.reasoningContent) {
|
|
641
|
+
if ("reasoningText" in part.reasoningContent) {
|
|
642
|
+
content.push({
|
|
643
|
+
type: "reasoning",
|
|
644
|
+
reasoningType: "text",
|
|
645
|
+
text: part.reasoningContent.reasoningText.text
|
|
646
|
+
});
|
|
647
|
+
if (part.reasoningContent.reasoningText.signature) {
|
|
648
|
+
content.push({
|
|
649
|
+
type: "reasoning",
|
|
650
|
+
reasoningType: "signature",
|
|
651
|
+
signature: part.reasoningContent.reasoningText.signature
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
} else if ("redactedReasoning" in part.reasoningContent) {
|
|
655
|
+
content.push({
|
|
656
|
+
type: "reasoning",
|
|
657
|
+
reasoningType: "redacted",
|
|
658
|
+
data: (_a = part.reasoningContent.redactedReasoning.data) != null ? _a : ""
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
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
|
+
});
|
|
670
|
+
}
|
|
671
|
+
}
|
|
679
672
|
const providerMetadata = response.trace || response.usage ? {
|
|
680
673
|
bedrock: {
|
|
681
674
|
...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
|
|
682
675
|
...response.usage && {
|
|
683
676
|
usage: {
|
|
684
|
-
cacheReadInputTokens: (
|
|
685
|
-
cacheWriteInputTokens: (
|
|
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
|
|
686
679
|
}
|
|
687
680
|
}
|
|
688
681
|
}
|
|
689
682
|
} : 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
683
|
return {
|
|
710
|
-
|
|
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
|
-
}),
|
|
684
|
+
content,
|
|
723
685
|
finishReason: mapBedrockFinishReason(
|
|
724
686
|
response.stopReason
|
|
725
687
|
),
|
|
726
688
|
usage: {
|
|
727
|
-
|
|
728
|
-
|
|
689
|
+
inputTokens: (_l = response.usage) == null ? void 0 : _l.inputTokens,
|
|
690
|
+
outputTokens: (_m = response.usage) == null ? void 0 : _m.outputTokens
|
|
691
|
+
},
|
|
692
|
+
response: {
|
|
693
|
+
// TODO add id, timestamp, etc
|
|
694
|
+
headers: responseHeaders
|
|
729
695
|
},
|
|
730
|
-
rawCall: { rawPrompt, rawSettings },
|
|
731
|
-
rawResponse: { headers: responseHeaders },
|
|
732
696
|
warnings,
|
|
733
|
-
reasoning: reasoning.length > 0 ? reasoning : void 0,
|
|
734
697
|
...providerMetadata && { providerMetadata }
|
|
735
698
|
};
|
|
736
699
|
}
|
|
@@ -752,17 +715,19 @@ var BedrockChatLanguageModel = class {
|
|
|
752
715
|
abortSignal: options.abortSignal,
|
|
753
716
|
fetch: this.config.fetch
|
|
754
717
|
});
|
|
755
|
-
const { messages: rawPrompt, ...rawSettings } = args;
|
|
756
718
|
let finishReason = "unknown";
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
719
|
+
const usage = {
|
|
720
|
+
inputTokens: void 0,
|
|
721
|
+
outputTokens: void 0
|
|
760
722
|
};
|
|
761
723
|
let providerMetadata = void 0;
|
|
762
724
|
const toolCallContentBlocks = {};
|
|
763
725
|
return {
|
|
764
726
|
stream: response.pipeThrough(
|
|
765
727
|
new TransformStream({
|
|
728
|
+
start(controller) {
|
|
729
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
730
|
+
},
|
|
766
731
|
transform(chunk, controller) {
|
|
767
732
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
768
733
|
function enqueueError(bedrockError) {
|
|
@@ -796,10 +761,8 @@ var BedrockChatLanguageModel = class {
|
|
|
796
761
|
);
|
|
797
762
|
}
|
|
798
763
|
if (value.metadata) {
|
|
799
|
-
usage =
|
|
800
|
-
|
|
801
|
-
completionTokens: (_d = (_c = value.metadata.usage) == null ? void 0 : _c.outputTokens) != null ? _d : Number.NaN
|
|
802
|
-
};
|
|
764
|
+
usage.inputTokens = (_b = (_a = value.metadata.usage) == null ? void 0 : _a.inputTokens) != null ? _b : usage.inputTokens;
|
|
765
|
+
usage.outputTokens = (_d = (_c = value.metadata.usage) == null ? void 0 : _c.outputTokens) != null ? _d : usage.outputTokens;
|
|
803
766
|
const cacheUsage = ((_e = value.metadata.usage) == null ? void 0 : _e.cacheReadInputTokens) != null || ((_f = value.metadata.usage) == null ? void 0 : _f.cacheWriteInputTokens) != null ? {
|
|
804
767
|
usage: {
|
|
805
768
|
cacheReadInputTokens: (_h = (_g = value.metadata.usage) == null ? void 0 : _g.cacheReadInputTokens) != null ? _h : Number.NaN,
|
|
@@ -820,8 +783,8 @@ var BedrockChatLanguageModel = class {
|
|
|
820
783
|
}
|
|
821
784
|
if (((_k = value.contentBlockDelta) == null ? void 0 : _k.delta) && "text" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.text) {
|
|
822
785
|
controller.enqueue({
|
|
823
|
-
type: "text
|
|
824
|
-
|
|
786
|
+
type: "text",
|
|
787
|
+
text: value.contentBlockDelta.delta.text
|
|
825
788
|
});
|
|
826
789
|
}
|
|
827
790
|
if (((_l = value.contentBlockDelta) == null ? void 0 : _l.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
|
|
@@ -829,16 +792,19 @@ var BedrockChatLanguageModel = class {
|
|
|
829
792
|
if ("text" in reasoningContent && reasoningContent.text) {
|
|
830
793
|
controller.enqueue({
|
|
831
794
|
type: "reasoning",
|
|
832
|
-
|
|
795
|
+
reasoningType: "text",
|
|
796
|
+
text: reasoningContent.text
|
|
833
797
|
});
|
|
834
798
|
} else if ("signature" in reasoningContent && reasoningContent.signature) {
|
|
835
799
|
controller.enqueue({
|
|
836
|
-
type: "reasoning
|
|
800
|
+
type: "reasoning",
|
|
801
|
+
reasoningType: "signature",
|
|
837
802
|
signature: reasoningContent.signature
|
|
838
803
|
});
|
|
839
804
|
} else if ("data" in reasoningContent && reasoningContent.data) {
|
|
840
805
|
controller.enqueue({
|
|
841
|
-
type: "
|
|
806
|
+
type: "reasoning",
|
|
807
|
+
reasoningType: "redacted",
|
|
842
808
|
data: reasoningContent.data
|
|
843
809
|
});
|
|
844
810
|
}
|
|
@@ -891,9 +857,8 @@ var BedrockChatLanguageModel = class {
|
|
|
891
857
|
}
|
|
892
858
|
})
|
|
893
859
|
),
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
warnings
|
|
860
|
+
// TODO request?
|
|
861
|
+
response: { headers: responseHeaders }
|
|
897
862
|
};
|
|
898
863
|
}
|
|
899
864
|
getUrl(modelId) {
|
|
@@ -901,105 +866,100 @@ var BedrockChatLanguageModel = class {
|
|
|
901
866
|
return `${this.config.baseUrl()}/model/${encodedModelId}`;
|
|
902
867
|
}
|
|
903
868
|
};
|
|
904
|
-
var
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
budgetTokens: z2.number().nullish()
|
|
908
|
-
}).nullish();
|
|
909
|
-
var BedrockStopReasonSchema = z2.union([
|
|
910
|
-
z2.enum(BEDROCK_STOP_REASONS),
|
|
911
|
-
z2.string()
|
|
869
|
+
var BedrockStopReasonSchema = z3.union([
|
|
870
|
+
z3.enum(BEDROCK_STOP_REASONS),
|
|
871
|
+
z3.string()
|
|
912
872
|
]);
|
|
913
|
-
var BedrockToolUseSchema =
|
|
914
|
-
toolUseId:
|
|
915
|
-
name:
|
|
916
|
-
input:
|
|
873
|
+
var BedrockToolUseSchema = z3.object({
|
|
874
|
+
toolUseId: z3.string(),
|
|
875
|
+
name: z3.string(),
|
|
876
|
+
input: z3.unknown()
|
|
917
877
|
});
|
|
918
|
-
var BedrockReasoningTextSchema =
|
|
919
|
-
signature:
|
|
920
|
-
text:
|
|
878
|
+
var BedrockReasoningTextSchema = z3.object({
|
|
879
|
+
signature: z3.string().nullish(),
|
|
880
|
+
text: z3.string()
|
|
921
881
|
});
|
|
922
|
-
var BedrockRedactedReasoningSchema =
|
|
923
|
-
data:
|
|
882
|
+
var BedrockRedactedReasoningSchema = z3.object({
|
|
883
|
+
data: z3.string()
|
|
924
884
|
});
|
|
925
|
-
var BedrockResponseSchema =
|
|
926
|
-
metrics:
|
|
927
|
-
latencyMs:
|
|
885
|
+
var BedrockResponseSchema = z3.object({
|
|
886
|
+
metrics: z3.object({
|
|
887
|
+
latencyMs: z3.number()
|
|
928
888
|
}).nullish(),
|
|
929
|
-
output:
|
|
930
|
-
message:
|
|
931
|
-
content:
|
|
932
|
-
|
|
933
|
-
text:
|
|
889
|
+
output: z3.object({
|
|
890
|
+
message: z3.object({
|
|
891
|
+
content: z3.array(
|
|
892
|
+
z3.object({
|
|
893
|
+
text: z3.string().nullish(),
|
|
934
894
|
toolUse: BedrockToolUseSchema.nullish(),
|
|
935
|
-
reasoningContent:
|
|
936
|
-
|
|
895
|
+
reasoningContent: z3.union([
|
|
896
|
+
z3.object({
|
|
937
897
|
reasoningText: BedrockReasoningTextSchema
|
|
938
898
|
}),
|
|
939
|
-
|
|
899
|
+
z3.object({
|
|
940
900
|
redactedReasoning: BedrockRedactedReasoningSchema
|
|
941
901
|
})
|
|
942
902
|
]).nullish()
|
|
943
903
|
})
|
|
944
904
|
),
|
|
945
|
-
role:
|
|
905
|
+
role: z3.string()
|
|
946
906
|
})
|
|
947
907
|
}),
|
|
948
908
|
stopReason: BedrockStopReasonSchema,
|
|
949
|
-
trace:
|
|
950
|
-
usage:
|
|
951
|
-
inputTokens:
|
|
952
|
-
outputTokens:
|
|
953
|
-
totalTokens:
|
|
954
|
-
cacheReadInputTokens:
|
|
955
|
-
cacheWriteInputTokens:
|
|
909
|
+
trace: z3.unknown().nullish(),
|
|
910
|
+
usage: z3.object({
|
|
911
|
+
inputTokens: z3.number(),
|
|
912
|
+
outputTokens: z3.number(),
|
|
913
|
+
totalTokens: z3.number(),
|
|
914
|
+
cacheReadInputTokens: z3.number().nullish(),
|
|
915
|
+
cacheWriteInputTokens: z3.number().nullish()
|
|
956
916
|
})
|
|
957
917
|
});
|
|
958
|
-
var BedrockStreamSchema =
|
|
959
|
-
contentBlockDelta:
|
|
960
|
-
contentBlockIndex:
|
|
961
|
-
delta:
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
reasoningContent:
|
|
918
|
+
var BedrockStreamSchema = z3.object({
|
|
919
|
+
contentBlockDelta: z3.object({
|
|
920
|
+
contentBlockIndex: z3.number(),
|
|
921
|
+
delta: z3.union([
|
|
922
|
+
z3.object({ text: z3.string() }),
|
|
923
|
+
z3.object({ toolUse: z3.object({ input: z3.string() }) }),
|
|
924
|
+
z3.object({
|
|
925
|
+
reasoningContent: z3.object({ text: z3.string() })
|
|
966
926
|
}),
|
|
967
|
-
|
|
968
|
-
reasoningContent:
|
|
969
|
-
signature:
|
|
927
|
+
z3.object({
|
|
928
|
+
reasoningContent: z3.object({
|
|
929
|
+
signature: z3.string()
|
|
970
930
|
})
|
|
971
931
|
}),
|
|
972
|
-
|
|
973
|
-
reasoningContent:
|
|
932
|
+
z3.object({
|
|
933
|
+
reasoningContent: z3.object({ data: z3.string() })
|
|
974
934
|
})
|
|
975
935
|
]).nullish()
|
|
976
936
|
}).nullish(),
|
|
977
|
-
contentBlockStart:
|
|
978
|
-
contentBlockIndex:
|
|
979
|
-
start:
|
|
937
|
+
contentBlockStart: z3.object({
|
|
938
|
+
contentBlockIndex: z3.number(),
|
|
939
|
+
start: z3.object({
|
|
980
940
|
toolUse: BedrockToolUseSchema.nullish()
|
|
981
941
|
}).nullish()
|
|
982
942
|
}).nullish(),
|
|
983
|
-
contentBlockStop:
|
|
984
|
-
contentBlockIndex:
|
|
943
|
+
contentBlockStop: z3.object({
|
|
944
|
+
contentBlockIndex: z3.number()
|
|
985
945
|
}).nullish(),
|
|
986
|
-
internalServerException:
|
|
987
|
-
messageStop:
|
|
988
|
-
additionalModelResponseFields:
|
|
946
|
+
internalServerException: z3.record(z3.unknown()).nullish(),
|
|
947
|
+
messageStop: z3.object({
|
|
948
|
+
additionalModelResponseFields: z3.record(z3.unknown()).nullish(),
|
|
989
949
|
stopReason: BedrockStopReasonSchema
|
|
990
950
|
}).nullish(),
|
|
991
|
-
metadata:
|
|
992
|
-
trace:
|
|
993
|
-
usage:
|
|
994
|
-
cacheReadInputTokens:
|
|
995
|
-
cacheWriteInputTokens:
|
|
996
|
-
inputTokens:
|
|
997
|
-
outputTokens:
|
|
951
|
+
metadata: z3.object({
|
|
952
|
+
trace: z3.unknown().nullish(),
|
|
953
|
+
usage: z3.object({
|
|
954
|
+
cacheReadInputTokens: z3.number().nullish(),
|
|
955
|
+
cacheWriteInputTokens: z3.number().nullish(),
|
|
956
|
+
inputTokens: z3.number(),
|
|
957
|
+
outputTokens: z3.number()
|
|
998
958
|
}).nullish()
|
|
999
959
|
}).nullish(),
|
|
1000
|
-
modelStreamErrorException:
|
|
1001
|
-
throttlingException:
|
|
1002
|
-
validationException:
|
|
960
|
+
modelStreamErrorException: z3.record(z3.unknown()).nullish(),
|
|
961
|
+
throttlingException: z3.record(z3.unknown()).nullish(),
|
|
962
|
+
validationException: z3.record(z3.unknown()).nullish()
|
|
1003
963
|
});
|
|
1004
964
|
|
|
1005
965
|
// src/bedrock-embedding-model.ts
|
|
@@ -1007,16 +967,33 @@ import {
|
|
|
1007
967
|
combineHeaders as combineHeaders2,
|
|
1008
968
|
createJsonErrorResponseHandler as createJsonErrorResponseHandler2,
|
|
1009
969
|
createJsonResponseHandler as createJsonResponseHandler2,
|
|
970
|
+
parseProviderOptions as parseProviderOptions2,
|
|
1010
971
|
postJsonToApi as postJsonToApi2,
|
|
1011
972
|
resolve as resolve2
|
|
1012
973
|
} from "@ai-sdk/provider-utils";
|
|
1013
|
-
|
|
974
|
+
|
|
975
|
+
// src/bedrock-embedding-options.ts
|
|
976
|
+
import { z as z4 } from "zod";
|
|
977
|
+
var bedrockEmbeddingProviderOptions = z4.object({
|
|
978
|
+
/**
|
|
979
|
+
The number of dimensions the resulting output embeddings should have (defaults to 1024).
|
|
980
|
+
Only supported in amazon.titan-embed-text-v2:0.
|
|
981
|
+
*/
|
|
982
|
+
dimensions: z4.union([z4.literal(1024), z4.literal(512), z4.literal(256)]).optional(),
|
|
983
|
+
/**
|
|
984
|
+
Flag indicating whether or not to normalize the output embeddings. Defaults to true
|
|
985
|
+
Only supported in amazon.titan-embed-text-v2:0.
|
|
986
|
+
*/
|
|
987
|
+
normalize: z4.boolean().optional()
|
|
988
|
+
});
|
|
989
|
+
|
|
990
|
+
// src/bedrock-embedding-model.ts
|
|
991
|
+
import { z as z5 } from "zod";
|
|
1014
992
|
var BedrockEmbeddingModel = class {
|
|
1015
|
-
constructor(modelId,
|
|
993
|
+
constructor(modelId, config) {
|
|
1016
994
|
this.modelId = modelId;
|
|
1017
|
-
this.settings = settings;
|
|
1018
995
|
this.config = config;
|
|
1019
|
-
this.specificationVersion = "
|
|
996
|
+
this.specificationVersion = "v2";
|
|
1020
997
|
this.provider = "amazon-bedrock";
|
|
1021
998
|
this.maxEmbeddingsPerCall = void 0;
|
|
1022
999
|
this.supportsParallelCalls = true;
|
|
@@ -1028,13 +1005,20 @@ var BedrockEmbeddingModel = class {
|
|
|
1028
1005
|
async doEmbed({
|
|
1029
1006
|
values,
|
|
1030
1007
|
headers,
|
|
1031
|
-
abortSignal
|
|
1008
|
+
abortSignal,
|
|
1009
|
+
providerOptions
|
|
1032
1010
|
}) {
|
|
1011
|
+
var _a;
|
|
1012
|
+
const bedrockOptions = (_a = parseProviderOptions2({
|
|
1013
|
+
provider: "bedrock",
|
|
1014
|
+
providerOptions,
|
|
1015
|
+
schema: bedrockEmbeddingProviderOptions
|
|
1016
|
+
})) != null ? _a : {};
|
|
1033
1017
|
const embedSingleText = async (inputText) => {
|
|
1034
1018
|
const args = {
|
|
1035
1019
|
inputText,
|
|
1036
|
-
dimensions:
|
|
1037
|
-
normalize:
|
|
1020
|
+
dimensions: bedrockOptions.dimensions,
|
|
1021
|
+
normalize: bedrockOptions.normalize
|
|
1038
1022
|
};
|
|
1039
1023
|
const url = this.getUrl(this.modelId);
|
|
1040
1024
|
const { value: response } = await postJsonToApi2({
|
|
@@ -1069,9 +1053,9 @@ var BedrockEmbeddingModel = class {
|
|
|
1069
1053
|
);
|
|
1070
1054
|
}
|
|
1071
1055
|
};
|
|
1072
|
-
var BedrockEmbeddingResponseSchema =
|
|
1073
|
-
embedding:
|
|
1074
|
-
inputTextTokenCount:
|
|
1056
|
+
var BedrockEmbeddingResponseSchema = z5.object({
|
|
1057
|
+
embedding: z5.array(z5.number()),
|
|
1058
|
+
inputTextTokenCount: z5.number()
|
|
1075
1059
|
});
|
|
1076
1060
|
|
|
1077
1061
|
// src/bedrock-image-model.ts
|
|
@@ -1089,7 +1073,7 @@ var modelMaxImagesPerCall = {
|
|
|
1089
1073
|
};
|
|
1090
1074
|
|
|
1091
1075
|
// src/bedrock-image-model.ts
|
|
1092
|
-
import { z as
|
|
1076
|
+
import { z as z6 } from "zod";
|
|
1093
1077
|
var BedrockImageModel = class {
|
|
1094
1078
|
constructor(modelId, settings, config) {
|
|
1095
1079
|
this.modelId = modelId;
|
|
@@ -1171,8 +1155,8 @@ var BedrockImageModel = class {
|
|
|
1171
1155
|
};
|
|
1172
1156
|
}
|
|
1173
1157
|
};
|
|
1174
|
-
var bedrockImageResponseSchema =
|
|
1175
|
-
images:
|
|
1158
|
+
var bedrockImageResponseSchema = z6.object({
|
|
1159
|
+
images: z6.array(z6.string())
|
|
1176
1160
|
});
|
|
1177
1161
|
|
|
1178
1162
|
// src/headers-utils.ts
|
|
@@ -1297,26 +1281,26 @@ function createAmazonBedrock(options = {}) {
|
|
|
1297
1281
|
})}.amazonaws.com`
|
|
1298
1282
|
)) != null ? _b : `https://bedrock-runtime.us-east-1.amazonaws.com`;
|
|
1299
1283
|
};
|
|
1300
|
-
const createChatModel = (modelId
|
|
1284
|
+
const createChatModel = (modelId) => {
|
|
1301
1285
|
var _a;
|
|
1302
|
-
return new BedrockChatLanguageModel(modelId,
|
|
1286
|
+
return new BedrockChatLanguageModel(modelId, {
|
|
1303
1287
|
baseUrl: getBaseUrl,
|
|
1304
1288
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1305
1289
|
fetch: sigv4Fetch,
|
|
1306
1290
|
generateId
|
|
1307
1291
|
});
|
|
1308
1292
|
};
|
|
1309
|
-
const provider = function(modelId
|
|
1293
|
+
const provider = function(modelId) {
|
|
1310
1294
|
if (new.target) {
|
|
1311
1295
|
throw new Error(
|
|
1312
1296
|
"The Amazon Bedrock model function cannot be called with the new keyword."
|
|
1313
1297
|
);
|
|
1314
1298
|
}
|
|
1315
|
-
return createChatModel(modelId
|
|
1299
|
+
return createChatModel(modelId);
|
|
1316
1300
|
};
|
|
1317
|
-
const createEmbeddingModel = (modelId
|
|
1301
|
+
const createEmbeddingModel = (modelId) => {
|
|
1318
1302
|
var _a;
|
|
1319
|
-
return new BedrockEmbeddingModel(modelId,
|
|
1303
|
+
return new BedrockEmbeddingModel(modelId, {
|
|
1320
1304
|
baseUrl: getBaseUrl,
|
|
1321
1305
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1322
1306
|
fetch: sigv4Fetch
|