@ai-sdk/amazon-bedrock 3.0.0-canary.8 → 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/CHANGELOG.md +511 -0
- package/README.md +77 -2
- package/dist/index.d.mts +46 -53
- package/dist/index.d.ts +46 -53
- package/dist/index.js +773 -401
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +694 -316
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -7
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")]).
|
|
48
|
-
budgetTokens: z.number().
|
|
49
|
-
}).
|
|
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
|
-
|
|
143
|
-
|
|
166
|
+
const toolWarnings = [];
|
|
167
|
+
const betas = /* @__PURE__ */ new Set();
|
|
168
|
+
if (tools == null || tools.length === 0) {
|
|
144
169
|
return {
|
|
145
|
-
toolConfig: {
|
|
146
|
-
|
|
170
|
+
toolConfig: {},
|
|
171
|
+
additionalTools: void 0,
|
|
172
|
+
betas,
|
|
173
|
+
toolWarnings
|
|
147
174
|
};
|
|
148
175
|
}
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
-
|
|
185
|
+
return true;
|
|
186
|
+
});
|
|
187
|
+
if (supportedTools.length === 0) {
|
|
167
188
|
return {
|
|
168
|
-
toolConfig: {
|
|
189
|
+
toolConfig: {},
|
|
190
|
+
additionalTools: void 0,
|
|
191
|
+
betas,
|
|
169
192
|
toolWarnings
|
|
170
193
|
};
|
|
171
194
|
}
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
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
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
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 });
|
|
202
248
|
}
|
|
203
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
|
+
}
|
|
284
|
+
}
|
|
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,
|
|
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:
|
|
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: (
|
|
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 (
|
|
288
|
-
|
|
289
|
-
const
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
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
|
-
|
|
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
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
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
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
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.
|
|
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
|
|
421
|
-
|
|
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,10 +627,11 @@ var BedrockChatLanguageModel = class {
|
|
|
495
627
|
this.config = config;
|
|
496
628
|
this.specificationVersion = "v2";
|
|
497
629
|
this.provider = "amazon-bedrock";
|
|
498
|
-
this.
|
|
499
|
-
|
|
630
|
+
this.supportedUrls = {
|
|
631
|
+
// no supported urls for bedrock
|
|
632
|
+
};
|
|
500
633
|
}
|
|
501
|
-
getArgs({
|
|
634
|
+
async getArgs({
|
|
502
635
|
prompt,
|
|
503
636
|
maxOutputTokens,
|
|
504
637
|
temperature,
|
|
@@ -513,8 +646,8 @@ var BedrockChatLanguageModel = class {
|
|
|
513
646
|
toolChoice,
|
|
514
647
|
providerOptions
|
|
515
648
|
}) {
|
|
516
|
-
var _a, _b, _c, _d, _e;
|
|
517
|
-
const bedrockOptions = (_a =
|
|
649
|
+
var _a, _b, _c, _d, _e, _f;
|
|
650
|
+
const bedrockOptions = (_a = await parseProviderOptions2({
|
|
518
651
|
provider: "bedrock",
|
|
519
652
|
providerOptions,
|
|
520
653
|
schema: bedrockProviderOptions
|
|
@@ -544,14 +677,39 @@ var BedrockChatLanguageModel = class {
|
|
|
544
677
|
setting: "topK"
|
|
545
678
|
});
|
|
546
679
|
}
|
|
547
|
-
if (responseFormat != null && responseFormat.type !== "text") {
|
|
680
|
+
if (responseFormat != null && responseFormat.type !== "text" && responseFormat.type !== "json") {
|
|
548
681
|
warnings.push({
|
|
549
682
|
type: "unsupported-setting",
|
|
550
683
|
setting: "responseFormat",
|
|
551
|
-
details: "
|
|
684
|
+
details: "Only text and json response formats are supported."
|
|
552
685
|
});
|
|
553
686
|
}
|
|
554
|
-
|
|
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
|
+
}
|
|
555
713
|
const isThinking = ((_b = bedrockOptions.reasoningConfig) == null ? void 0 : _b.type) === "enabled";
|
|
556
714
|
const thinkingBudget = (_c = bedrockOptions.reasoningConfig) == null ? void 0 : _c.budgetTokens;
|
|
557
715
|
const inferenceConfig = {
|
|
@@ -568,7 +726,7 @@ var BedrockChatLanguageModel = class {
|
|
|
568
726
|
}
|
|
569
727
|
bedrockOptions.additionalModelRequestFields = {
|
|
570
728
|
...bedrockOptions.additionalModelRequestFields,
|
|
571
|
-
|
|
729
|
+
thinking: {
|
|
572
730
|
type: (_d = bedrockOptions.reasoningConfig) == null ? void 0 : _d.type,
|
|
573
731
|
budget_tokens: thinkingBudget
|
|
574
732
|
}
|
|
@@ -590,7 +748,34 @@ var BedrockChatLanguageModel = class {
|
|
|
590
748
|
details: "topP is not supported when thinking is enabled"
|
|
591
749
|
});
|
|
592
750
|
}
|
|
593
|
-
const
|
|
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) || {};
|
|
594
779
|
return {
|
|
595
780
|
command: {
|
|
596
781
|
system,
|
|
@@ -599,22 +784,36 @@ var BedrockChatLanguageModel = class {
|
|
|
599
784
|
...Object.keys(inferenceConfig).length > 0 && {
|
|
600
785
|
inferenceConfig
|
|
601
786
|
},
|
|
602
|
-
...
|
|
603
|
-
...
|
|
787
|
+
...filteredBedrockOptions,
|
|
788
|
+
...toolConfig.tools !== void 0 && toolConfig.tools.length > 0 ? { toolConfig } : {}
|
|
604
789
|
},
|
|
605
|
-
warnings
|
|
790
|
+
warnings,
|
|
791
|
+
usesJsonResponseTool: jsonResponseTool != null,
|
|
792
|
+
betas
|
|
606
793
|
};
|
|
607
794
|
}
|
|
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
|
+
);
|
|
804
|
+
}
|
|
608
805
|
async doGenerate(options) {
|
|
609
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
610
|
-
const {
|
|
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);
|
|
611
813
|
const url = `${this.getUrl(this.modelId)}/converse`;
|
|
612
814
|
const { value: response, responseHeaders } = await postJsonToApi({
|
|
613
815
|
url,
|
|
614
|
-
headers:
|
|
615
|
-
await resolve(this.config.headers),
|
|
616
|
-
options.headers
|
|
617
|
-
),
|
|
816
|
+
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
618
817
|
body: args,
|
|
619
818
|
failedResponseHandler: createJsonErrorResponseHandler({
|
|
620
819
|
errorSchema: BedrockErrorSchema,
|
|
@@ -632,49 +831,60 @@ var BedrockChatLanguageModel = class {
|
|
|
632
831
|
const content = [];
|
|
633
832
|
for (const part of response.output.message.content) {
|
|
634
833
|
if (part.text) {
|
|
635
|
-
|
|
834
|
+
if (!usesJsonResponseTool) {
|
|
835
|
+
content.push({ type: "text", text: part.text });
|
|
836
|
+
}
|
|
636
837
|
}
|
|
637
838
|
if (part.reasoningContent) {
|
|
638
839
|
if ("reasoningText" in part.reasoningContent) {
|
|
639
|
-
|
|
840
|
+
const reasoning = {
|
|
640
841
|
type: "reasoning",
|
|
641
|
-
reasoningType: "text",
|
|
642
842
|
text: part.reasoningContent.reasoningText.text
|
|
643
|
-
}
|
|
843
|
+
};
|
|
644
844
|
if (part.reasoningContent.reasoningText.signature) {
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
}
|
|
845
|
+
reasoning.providerMetadata = {
|
|
846
|
+
bedrock: {
|
|
847
|
+
signature: part.reasoningContent.reasoningText.signature
|
|
848
|
+
}
|
|
849
|
+
};
|
|
650
850
|
}
|
|
851
|
+
content.push(reasoning);
|
|
651
852
|
} else if ("redactedReasoning" in part.reasoningContent) {
|
|
652
853
|
content.push({
|
|
653
854
|
type: "reasoning",
|
|
654
|
-
|
|
655
|
-
|
|
855
|
+
text: "",
|
|
856
|
+
providerMetadata: {
|
|
857
|
+
bedrock: {
|
|
858
|
+
redactedData: (_a = part.reasoningContent.redactedReasoning.data) != null ? _a : ""
|
|
859
|
+
}
|
|
860
|
+
}
|
|
656
861
|
});
|
|
657
862
|
}
|
|
658
863
|
}
|
|
659
864
|
if (part.toolUse) {
|
|
660
|
-
content.push(
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
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
|
+
);
|
|
667
877
|
}
|
|
668
878
|
}
|
|
669
|
-
const providerMetadata = response.trace || response.usage ? {
|
|
879
|
+
const providerMetadata = response.trace || response.usage || usesJsonResponseTool ? {
|
|
670
880
|
bedrock: {
|
|
671
881
|
...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
|
|
672
|
-
...response.usage && {
|
|
882
|
+
...((_h = response.usage) == null ? void 0 : _h.cacheWriteInputTokens) != null && {
|
|
673
883
|
usage: {
|
|
674
|
-
|
|
675
|
-
cacheWriteInputTokens: (_k = (_j = response.usage) == null ? void 0 : _j.cacheWriteInputTokens) != null ? _k : Number.NaN
|
|
884
|
+
cacheWriteInputTokens: response.usage.cacheWriteInputTokens
|
|
676
885
|
}
|
|
677
|
-
}
|
|
886
|
+
},
|
|
887
|
+
...usesJsonResponseTool && { isJsonResponseFromTool: true }
|
|
678
888
|
}
|
|
679
889
|
} : void 0;
|
|
680
890
|
return {
|
|
@@ -683,8 +893,10 @@ var BedrockChatLanguageModel = class {
|
|
|
683
893
|
response.stopReason
|
|
684
894
|
),
|
|
685
895
|
usage: {
|
|
686
|
-
inputTokens: (
|
|
687
|
-
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
|
|
688
900
|
},
|
|
689
901
|
response: {
|
|
690
902
|
// TODO add id, timestamp, etc
|
|
@@ -695,14 +907,16 @@ var BedrockChatLanguageModel = class {
|
|
|
695
907
|
};
|
|
696
908
|
}
|
|
697
909
|
async doStream(options) {
|
|
698
|
-
const {
|
|
910
|
+
const {
|
|
911
|
+
command: args,
|
|
912
|
+
warnings,
|
|
913
|
+
usesJsonResponseTool,
|
|
914
|
+
betas
|
|
915
|
+
} = await this.getArgs(options);
|
|
699
916
|
const url = `${this.getUrl(this.modelId)}/converse-stream`;
|
|
700
917
|
const { value: response, responseHeaders } = await postJsonToApi({
|
|
701
918
|
url,
|
|
702
|
-
headers:
|
|
703
|
-
await resolve(this.config.headers),
|
|
704
|
-
options.headers
|
|
705
|
-
),
|
|
919
|
+
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
706
920
|
body: args,
|
|
707
921
|
failedResponseHandler: createJsonErrorResponseHandler({
|
|
708
922
|
errorSchema: BedrockErrorSchema,
|
|
@@ -715,10 +929,11 @@ var BedrockChatLanguageModel = class {
|
|
|
715
929
|
let finishReason = "unknown";
|
|
716
930
|
const usage = {
|
|
717
931
|
inputTokens: void 0,
|
|
718
|
-
outputTokens: void 0
|
|
932
|
+
outputTokens: void 0,
|
|
933
|
+
totalTokens: void 0
|
|
719
934
|
};
|
|
720
935
|
let providerMetadata = void 0;
|
|
721
|
-
const
|
|
936
|
+
const contentBlocks = {};
|
|
722
937
|
return {
|
|
723
938
|
stream: response.pipeThrough(
|
|
724
939
|
new TransformStream({
|
|
@@ -726,11 +941,14 @@ var BedrockChatLanguageModel = class {
|
|
|
726
941
|
controller.enqueue({ type: "stream-start", warnings });
|
|
727
942
|
},
|
|
728
943
|
transform(chunk, controller) {
|
|
729
|
-
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;
|
|
730
945
|
function enqueueError(bedrockError) {
|
|
731
946
|
finishReason = "error";
|
|
732
947
|
controller.enqueue({ type: "error", error: bedrockError });
|
|
733
948
|
}
|
|
949
|
+
if (options.includeRawChunks) {
|
|
950
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
951
|
+
}
|
|
734
952
|
if (!chunk.success) {
|
|
735
953
|
enqueueError(chunk.error);
|
|
736
954
|
return;
|
|
@@ -760,87 +978,174 @@ var BedrockChatLanguageModel = class {
|
|
|
760
978
|
if (value.metadata) {
|
|
761
979
|
usage.inputTokens = (_b = (_a = value.metadata.usage) == null ? void 0 : _a.inputTokens) != null ? _b : usage.inputTokens;
|
|
762
980
|
usage.outputTokens = (_d = (_c = value.metadata.usage) == null ? void 0 : _c.outputTokens) != null ? _d : usage.outputTokens;
|
|
763
|
-
|
|
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 ? {
|
|
764
984
|
usage: {
|
|
765
|
-
|
|
766
|
-
cacheWriteInputTokens: (_j = (_i = value.metadata.usage) == null ? void 0 : _i.cacheWriteInputTokens) != null ? _j : Number.NaN
|
|
985
|
+
cacheWriteInputTokens: value.metadata.usage.cacheWriteInputTokens
|
|
767
986
|
}
|
|
768
987
|
} : void 0;
|
|
769
988
|
const trace = value.metadata.trace ? {
|
|
770
989
|
trace: value.metadata.trace
|
|
771
990
|
} : void 0;
|
|
772
|
-
if (cacheUsage || trace) {
|
|
991
|
+
if (cacheUsage || trace || usesJsonResponseTool) {
|
|
773
992
|
providerMetadata = {
|
|
774
993
|
bedrock: {
|
|
775
994
|
...cacheUsage,
|
|
776
|
-
...trace
|
|
995
|
+
...trace,
|
|
996
|
+
...usesJsonResponseTool && {
|
|
997
|
+
isJsonResponseFromTool: true
|
|
998
|
+
}
|
|
777
999
|
}
|
|
778
1000
|
};
|
|
779
1001
|
}
|
|
780
1002
|
}
|
|
781
|
-
if (((
|
|
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" };
|
|
782
1006
|
controller.enqueue({
|
|
783
|
-
type: "text",
|
|
784
|
-
|
|
1007
|
+
type: "text-start",
|
|
1008
|
+
id: String(blockIndex)
|
|
785
1009
|
});
|
|
786
1010
|
}
|
|
787
|
-
if (((
|
|
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;
|
|
788
1079
|
const reasoningContent = value.contentBlockDelta.delta.reasoningContent;
|
|
789
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
|
+
}
|
|
790
1088
|
controller.enqueue({
|
|
791
|
-
type: "reasoning",
|
|
792
|
-
|
|
793
|
-
|
|
1089
|
+
type: "reasoning-delta",
|
|
1090
|
+
id: String(blockIndex),
|
|
1091
|
+
delta: reasoningContent.text
|
|
794
1092
|
});
|
|
795
1093
|
} else if ("signature" in reasoningContent && reasoningContent.signature) {
|
|
796
1094
|
controller.enqueue({
|
|
797
|
-
type: "reasoning",
|
|
798
|
-
|
|
799
|
-
|
|
1095
|
+
type: "reasoning-delta",
|
|
1096
|
+
id: String(blockIndex),
|
|
1097
|
+
delta: "",
|
|
1098
|
+
providerMetadata: {
|
|
1099
|
+
bedrock: {
|
|
1100
|
+
signature: reasoningContent.signature
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
800
1103
|
});
|
|
801
1104
|
} else if ("data" in reasoningContent && reasoningContent.data) {
|
|
802
1105
|
controller.enqueue({
|
|
803
|
-
type: "reasoning",
|
|
804
|
-
|
|
805
|
-
|
|
1106
|
+
type: "reasoning-delta",
|
|
1107
|
+
id: String(blockIndex),
|
|
1108
|
+
delta: "",
|
|
1109
|
+
providerMetadata: {
|
|
1110
|
+
bedrock: {
|
|
1111
|
+
redactedData: reasoningContent.data
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
806
1114
|
});
|
|
807
1115
|
}
|
|
808
1116
|
}
|
|
809
1117
|
const contentBlockStart = value.contentBlockStart;
|
|
810
|
-
if (((
|
|
1118
|
+
if (((_p = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _p.toolUse) != null) {
|
|
811
1119
|
const toolUse = contentBlockStart.start.toolUse;
|
|
812
|
-
|
|
1120
|
+
const blockIndex = contentBlockStart.contentBlockIndex;
|
|
1121
|
+
contentBlocks[blockIndex] = {
|
|
1122
|
+
type: "tool-call",
|
|
813
1123
|
toolCallId: toolUse.toolUseId,
|
|
814
1124
|
toolName: toolUse.name,
|
|
815
1125
|
jsonText: ""
|
|
816
1126
|
};
|
|
1127
|
+
if (!usesJsonResponseTool) {
|
|
1128
|
+
controller.enqueue({
|
|
1129
|
+
type: "tool-input-start",
|
|
1130
|
+
id: toolUse.toolUseId,
|
|
1131
|
+
toolName: toolUse.name
|
|
1132
|
+
});
|
|
1133
|
+
}
|
|
817
1134
|
}
|
|
818
1135
|
const contentBlockDelta = value.contentBlockDelta;
|
|
819
1136
|
if ((contentBlockDelta == null ? void 0 : contentBlockDelta.delta) && "toolUse" in contentBlockDelta.delta && contentBlockDelta.delta.toolUse) {
|
|
820
|
-
const
|
|
821
|
-
const
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
if (contentBlockStop != null) {
|
|
833
|
-
const index = contentBlockStop.contentBlockIndex;
|
|
834
|
-
const contentBlock = toolCallContentBlocks[index];
|
|
835
|
-
if (contentBlock != null) {
|
|
836
|
-
controller.enqueue({
|
|
837
|
-
type: "tool-call",
|
|
838
|
-
toolCallType: "function",
|
|
839
|
-
toolCallId: contentBlock.toolCallId,
|
|
840
|
-
toolName: contentBlock.toolName,
|
|
841
|
-
args: contentBlock.jsonText
|
|
842
|
-
});
|
|
843
|
-
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;
|
|
844
1149
|
}
|
|
845
1150
|
}
|
|
846
1151
|
},
|
|
@@ -940,9 +1245,9 @@ var BedrockStreamSchema = z3.object({
|
|
|
940
1245
|
contentBlockStop: z3.object({
|
|
941
1246
|
contentBlockIndex: z3.number()
|
|
942
1247
|
}).nullish(),
|
|
943
|
-
internalServerException: z3.record(z3.unknown()).nullish(),
|
|
1248
|
+
internalServerException: z3.record(z3.string(), z3.unknown()).nullish(),
|
|
944
1249
|
messageStop: z3.object({
|
|
945
|
-
additionalModelResponseFields: z3.record(z3.unknown()).nullish(),
|
|
1250
|
+
additionalModelResponseFields: z3.record(z3.string(), z3.unknown()).nullish(),
|
|
946
1251
|
stopReason: BedrockStopReasonSchema
|
|
947
1252
|
}).nullish(),
|
|
948
1253
|
metadata: z3.object({
|
|
@@ -954,28 +1259,52 @@ var BedrockStreamSchema = z3.object({
|
|
|
954
1259
|
outputTokens: z3.number()
|
|
955
1260
|
}).nullish()
|
|
956
1261
|
}).nullish(),
|
|
957
|
-
modelStreamErrorException: z3.record(z3.unknown()).nullish(),
|
|
958
|
-
throttlingException: z3.record(z3.unknown()).nullish(),
|
|
959
|
-
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()
|
|
960
1269
|
});
|
|
961
1270
|
|
|
962
1271
|
// src/bedrock-embedding-model.ts
|
|
1272
|
+
import {
|
|
1273
|
+
TooManyEmbeddingValuesForCallError
|
|
1274
|
+
} from "@ai-sdk/provider";
|
|
963
1275
|
import {
|
|
964
1276
|
combineHeaders as combineHeaders2,
|
|
965
1277
|
createJsonErrorResponseHandler as createJsonErrorResponseHandler2,
|
|
966
1278
|
createJsonResponseHandler as createJsonResponseHandler2,
|
|
1279
|
+
parseProviderOptions as parseProviderOptions3,
|
|
967
1280
|
postJsonToApi as postJsonToApi2,
|
|
968
1281
|
resolve as resolve2
|
|
969
1282
|
} from "@ai-sdk/provider-utils";
|
|
970
|
-
|
|
1283
|
+
|
|
1284
|
+
// src/bedrock-embedding-options.ts
|
|
1285
|
+
import { z as z4 } from "zod/v4";
|
|
1286
|
+
var bedrockEmbeddingProviderOptions = z4.object({
|
|
1287
|
+
/**
|
|
1288
|
+
The number of dimensions the resulting output embeddings should have (defaults to 1024).
|
|
1289
|
+
Only supported in amazon.titan-embed-text-v2:0.
|
|
1290
|
+
*/
|
|
1291
|
+
dimensions: z4.union([z4.literal(1024), z4.literal(512), z4.literal(256)]).optional(),
|
|
1292
|
+
/**
|
|
1293
|
+
Flag indicating whether or not to normalize the output embeddings. Defaults to true
|
|
1294
|
+
Only supported in amazon.titan-embed-text-v2:0.
|
|
1295
|
+
*/
|
|
1296
|
+
normalize: z4.boolean().optional()
|
|
1297
|
+
});
|
|
1298
|
+
|
|
1299
|
+
// src/bedrock-embedding-model.ts
|
|
1300
|
+
import { z as z5 } from "zod/v4";
|
|
971
1301
|
var BedrockEmbeddingModel = class {
|
|
972
|
-
constructor(modelId,
|
|
1302
|
+
constructor(modelId, config) {
|
|
973
1303
|
this.modelId = modelId;
|
|
974
|
-
this.settings = settings;
|
|
975
1304
|
this.config = config;
|
|
976
1305
|
this.specificationVersion = "v2";
|
|
977
1306
|
this.provider = "amazon-bedrock";
|
|
978
|
-
this.maxEmbeddingsPerCall =
|
|
1307
|
+
this.maxEmbeddingsPerCall = 1;
|
|
979
1308
|
this.supportsParallelCalls = true;
|
|
980
1309
|
}
|
|
981
1310
|
getUrl(modelId) {
|
|
@@ -985,50 +1314,54 @@ var BedrockEmbeddingModel = class {
|
|
|
985
1314
|
async doEmbed({
|
|
986
1315
|
values,
|
|
987
1316
|
headers,
|
|
988
|
-
abortSignal
|
|
1317
|
+
abortSignal,
|
|
1318
|
+
providerOptions
|
|
989
1319
|
}) {
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
const { value: response } = await postJsonToApi2({
|
|
998
|
-
url,
|
|
999
|
-
headers: await resolve2(
|
|
1000
|
-
combineHeaders2(await resolve2(this.config.headers), headers)
|
|
1001
|
-
),
|
|
1002
|
-
body: args,
|
|
1003
|
-
failedResponseHandler: createJsonErrorResponseHandler2({
|
|
1004
|
-
errorSchema: BedrockErrorSchema,
|
|
1005
|
-
errorToMessage: (error) => `${error.type}: ${error.message}`
|
|
1006
|
-
}),
|
|
1007
|
-
successfulResponseHandler: createJsonResponseHandler2(
|
|
1008
|
-
BedrockEmbeddingResponseSchema
|
|
1009
|
-
),
|
|
1010
|
-
fetch: this.config.fetch,
|
|
1011
|
-
abortSignal
|
|
1320
|
+
var _a;
|
|
1321
|
+
if (values.length > this.maxEmbeddingsPerCall) {
|
|
1322
|
+
throw new TooManyEmbeddingValuesForCallError({
|
|
1323
|
+
provider: this.provider,
|
|
1324
|
+
modelId: this.modelId,
|
|
1325
|
+
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
|
1326
|
+
values
|
|
1012
1327
|
});
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1328
|
+
}
|
|
1329
|
+
const bedrockOptions = (_a = await parseProviderOptions3({
|
|
1330
|
+
provider: "bedrock",
|
|
1331
|
+
providerOptions,
|
|
1332
|
+
schema: bedrockEmbeddingProviderOptions
|
|
1333
|
+
})) != null ? _a : {};
|
|
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 }
|
|
1017
1359
|
};
|
|
1018
|
-
const responses = await Promise.all(values.map(embedSingleText));
|
|
1019
|
-
return responses.reduce(
|
|
1020
|
-
(accumulated, response) => {
|
|
1021
|
-
accumulated.embeddings.push(response.embedding);
|
|
1022
|
-
accumulated.usage.tokens += response.inputTextTokenCount;
|
|
1023
|
-
return accumulated;
|
|
1024
|
-
},
|
|
1025
|
-
{ embeddings: [], usage: { tokens: 0 } }
|
|
1026
|
-
);
|
|
1027
1360
|
}
|
|
1028
1361
|
};
|
|
1029
|
-
var BedrockEmbeddingResponseSchema =
|
|
1030
|
-
embedding:
|
|
1031
|
-
inputTextTokenCount:
|
|
1362
|
+
var BedrockEmbeddingResponseSchema = z5.object({
|
|
1363
|
+
embedding: z5.array(z5.number()),
|
|
1364
|
+
inputTextTokenCount: z5.number()
|
|
1032
1365
|
});
|
|
1033
1366
|
|
|
1034
1367
|
// src/bedrock-image-model.ts
|
|
@@ -1046,18 +1379,17 @@ var modelMaxImagesPerCall = {
|
|
|
1046
1379
|
};
|
|
1047
1380
|
|
|
1048
1381
|
// src/bedrock-image-model.ts
|
|
1049
|
-
import { z as
|
|
1382
|
+
import { z as z6 } from "zod/v4";
|
|
1050
1383
|
var BedrockImageModel = class {
|
|
1051
|
-
constructor(modelId,
|
|
1384
|
+
constructor(modelId, config) {
|
|
1052
1385
|
this.modelId = modelId;
|
|
1053
|
-
this.settings = settings;
|
|
1054
1386
|
this.config = config;
|
|
1055
|
-
this.specificationVersion = "
|
|
1387
|
+
this.specificationVersion = "v2";
|
|
1056
1388
|
this.provider = "amazon-bedrock";
|
|
1057
1389
|
}
|
|
1058
1390
|
get maxImagesPerCall() {
|
|
1059
|
-
var _a
|
|
1060
|
-
return (
|
|
1391
|
+
var _a;
|
|
1392
|
+
return (_a = modelMaxImagesPerCall[this.modelId]) != null ? _a : 1;
|
|
1061
1393
|
}
|
|
1062
1394
|
getUrl(modelId) {
|
|
1063
1395
|
const encodedModelId = encodeURIComponent(modelId);
|
|
@@ -1073,7 +1405,7 @@ var BedrockImageModel = class {
|
|
|
1073
1405
|
headers,
|
|
1074
1406
|
abortSignal
|
|
1075
1407
|
}) {
|
|
1076
|
-
var _a, _b, _c, _d, _e, _f;
|
|
1408
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
1077
1409
|
const warnings = [];
|
|
1078
1410
|
const [width, height] = size ? size.split("x").map(Number) : [];
|
|
1079
1411
|
const args = {
|
|
@@ -1082,6 +1414,9 @@ var BedrockImageModel = class {
|
|
|
1082
1414
|
text: prompt,
|
|
1083
1415
|
...((_a = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _a.negativeText) ? {
|
|
1084
1416
|
negativeText: providerOptions.bedrock.negativeText
|
|
1417
|
+
} : {},
|
|
1418
|
+
...((_b = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _b.style) ? {
|
|
1419
|
+
style: providerOptions.bedrock.style
|
|
1085
1420
|
} : {}
|
|
1086
1421
|
},
|
|
1087
1422
|
imageGenerationConfig: {
|
|
@@ -1089,8 +1424,8 @@ var BedrockImageModel = class {
|
|
|
1089
1424
|
...height ? { height } : {},
|
|
1090
1425
|
...seed ? { seed } : {},
|
|
1091
1426
|
...n ? { numberOfImages: n } : {},
|
|
1092
|
-
...((
|
|
1093
|
-
...((
|
|
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 } : {}
|
|
1094
1429
|
}
|
|
1095
1430
|
};
|
|
1096
1431
|
if (aspectRatio != void 0) {
|
|
@@ -1100,7 +1435,7 @@ var BedrockImageModel = class {
|
|
|
1100
1435
|
details: "This model does not support aspect ratio. Use `size` instead."
|
|
1101
1436
|
});
|
|
1102
1437
|
}
|
|
1103
|
-
const currentDate = (
|
|
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();
|
|
1104
1439
|
const { value: response, responseHeaders } = await postJsonToApi3({
|
|
1105
1440
|
url: this.getUrl(this.modelId),
|
|
1106
1441
|
headers: await resolve3(
|
|
@@ -1128,8 +1463,8 @@ var BedrockImageModel = class {
|
|
|
1128
1463
|
};
|
|
1129
1464
|
}
|
|
1130
1465
|
};
|
|
1131
|
-
var bedrockImageResponseSchema =
|
|
1132
|
-
images:
|
|
1466
|
+
var bedrockImageResponseSchema = z6.object({
|
|
1467
|
+
images: z6.array(z6.string())
|
|
1133
1468
|
});
|
|
1134
1469
|
|
|
1135
1470
|
// src/headers-utils.ts
|
|
@@ -1151,11 +1486,7 @@ function extractHeaders(headers) {
|
|
|
1151
1486
|
return originalHeaders;
|
|
1152
1487
|
}
|
|
1153
1488
|
function convertHeadersToRecord(headers) {
|
|
1154
|
-
|
|
1155
|
-
headers.forEach((value, key) => {
|
|
1156
|
-
record[key.toLowerCase()] = value;
|
|
1157
|
-
});
|
|
1158
|
-
return record;
|
|
1489
|
+
return Object.fromEntries([...headers]);
|
|
1159
1490
|
}
|
|
1160
1491
|
|
|
1161
1492
|
// src/bedrock-sigv4-fetch.ts
|
|
@@ -1207,10 +1538,28 @@ function prepareBodyString(body) {
|
|
|
1207
1538
|
return JSON.stringify(body);
|
|
1208
1539
|
}
|
|
1209
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
|
+
}
|
|
1210
1554
|
|
|
1211
1555
|
// src/bedrock-provider.ts
|
|
1212
1556
|
function createAmazonBedrock(options = {}) {
|
|
1213
|
-
const
|
|
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 () => {
|
|
1214
1563
|
const region = loadSetting({
|
|
1215
1564
|
settingValue: options.region,
|
|
1216
1565
|
settingName: "region",
|
|
@@ -1218,30 +1567,58 @@ function createAmazonBedrock(options = {}) {
|
|
|
1218
1567
|
description: "AWS region"
|
|
1219
1568
|
});
|
|
1220
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 {
|
|
1221
1583
|
return {
|
|
1222
|
-
|
|
1223
|
-
|
|
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
|
+
})
|
|
1224
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;
|
|
1225
1621
|
}
|
|
1226
|
-
return {
|
|
1227
|
-
region,
|
|
1228
|
-
accessKeyId: loadSetting({
|
|
1229
|
-
settingValue: options.accessKeyId,
|
|
1230
|
-
settingName: "accessKeyId",
|
|
1231
|
-
environmentVariableName: "AWS_ACCESS_KEY_ID",
|
|
1232
|
-
description: "AWS access key ID"
|
|
1233
|
-
}),
|
|
1234
|
-
secretAccessKey: loadSetting({
|
|
1235
|
-
settingValue: options.secretAccessKey,
|
|
1236
|
-
settingName: "secretAccessKey",
|
|
1237
|
-
environmentVariableName: "AWS_SECRET_ACCESS_KEY",
|
|
1238
|
-
description: "AWS secret access key"
|
|
1239
|
-
}),
|
|
1240
|
-
sessionToken: loadOptionalSetting({
|
|
1241
|
-
settingValue: options.sessionToken,
|
|
1242
|
-
environmentVariableName: "AWS_SESSION_TOKEN"
|
|
1243
|
-
})
|
|
1244
|
-
};
|
|
1245
1622
|
}, options.fetch);
|
|
1246
1623
|
const getBaseUrl = () => {
|
|
1247
1624
|
var _a, _b;
|
|
@@ -1259,7 +1636,7 @@ function createAmazonBedrock(options = {}) {
|
|
|
1259
1636
|
return new BedrockChatLanguageModel(modelId, {
|
|
1260
1637
|
baseUrl: getBaseUrl,
|
|
1261
1638
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1262
|
-
fetch:
|
|
1639
|
+
fetch: fetchFunction,
|
|
1263
1640
|
generateId
|
|
1264
1641
|
});
|
|
1265
1642
|
};
|
|
@@ -1271,20 +1648,20 @@ function createAmazonBedrock(options = {}) {
|
|
|
1271
1648
|
}
|
|
1272
1649
|
return createChatModel(modelId);
|
|
1273
1650
|
};
|
|
1274
|
-
const createEmbeddingModel = (modelId
|
|
1651
|
+
const createEmbeddingModel = (modelId) => {
|
|
1275
1652
|
var _a;
|
|
1276
|
-
return new BedrockEmbeddingModel(modelId,
|
|
1653
|
+
return new BedrockEmbeddingModel(modelId, {
|
|
1277
1654
|
baseUrl: getBaseUrl,
|
|
1278
1655
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1279
|
-
fetch:
|
|
1656
|
+
fetch: fetchFunction
|
|
1280
1657
|
});
|
|
1281
1658
|
};
|
|
1282
|
-
const createImageModel = (modelId
|
|
1659
|
+
const createImageModel = (modelId) => {
|
|
1283
1660
|
var _a;
|
|
1284
|
-
return new BedrockImageModel(modelId,
|
|
1661
|
+
return new BedrockImageModel(modelId, {
|
|
1285
1662
|
baseUrl: getBaseUrl,
|
|
1286
1663
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1287
|
-
fetch:
|
|
1664
|
+
fetch: fetchFunction
|
|
1288
1665
|
});
|
|
1289
1666
|
};
|
|
1290
1667
|
provider.languageModel = createChatModel;
|
|
@@ -1293,6 +1670,7 @@ function createAmazonBedrock(options = {}) {
|
|
|
1293
1670
|
provider.textEmbeddingModel = createEmbeddingModel;
|
|
1294
1671
|
provider.image = createImageModel;
|
|
1295
1672
|
provider.imageModel = createImageModel;
|
|
1673
|
+
provider.tools = anthropicTools2;
|
|
1296
1674
|
return provider;
|
|
1297
1675
|
}
|
|
1298
1676
|
var bedrock = createAmazonBedrock();
|