@ai-sdk/amazon-bedrock 5.0.0-beta.4 → 5.0.0-beta.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +312 -4
- package/README.md +2 -0
- package/dist/anthropic/index.d.ts +1 -1
- package/dist/anthropic/index.js +88 -69
- package/dist/anthropic/index.js.map +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +469 -300
- package/dist/index.js.map +1 -1
- package/docs/08-amazon-bedrock.mdx +49 -6
- package/package.json +10 -12
- package/src/anthropic/bedrock-anthropic-fetch.ts +26 -0
- package/src/anthropic/bedrock-anthropic-options.ts +2 -0
- package/src/anthropic/bedrock-anthropic-provider.ts +11 -1
- package/src/bedrock-api-types.ts +3 -0
- package/src/bedrock-chat-language-model.ts +120 -6
- package/src/bedrock-chat-options.ts +12 -0
- package/src/bedrock-embedding-model.ts +22 -2
- package/src/bedrock-image-model.ts +22 -2
- package/src/convert-to-bedrock-chat-messages.ts +46 -34
- package/dist/anthropic/index.d.mts +0 -91
- package/dist/anthropic/index.mjs +0 -397
- package/dist/anthropic/index.mjs.map +0 -1
- package/dist/index.d.mts +0 -194
- package/dist/index.mjs +0 -2329
- package/dist/index.mjs.map +0 -1
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
} from '@ai-sdk/provider';
|
|
8
8
|
import {
|
|
9
9
|
convertToBase64,
|
|
10
|
+
isProviderReference,
|
|
10
11
|
parseProviderOptions,
|
|
11
12
|
stripFileExtension,
|
|
12
13
|
} from '@ai-sdk/provider-utils';
|
|
@@ -112,6 +113,12 @@ export async function convertToBedrockChatMessages(
|
|
|
112
113
|
}
|
|
113
114
|
|
|
114
115
|
case 'file': {
|
|
116
|
+
if (isProviderReference(part.data)) {
|
|
117
|
+
throw new UnsupportedFunctionalityError({
|
|
118
|
+
functionality: 'file parts with provider references',
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
115
122
|
if (part.data instanceof URL) {
|
|
116
123
|
// The AI SDK automatically downloads files for user file parts with URLs
|
|
117
124
|
throw new UnsupportedFunctionalityError({
|
|
@@ -174,7 +181,7 @@ export async function convertToBedrockChatMessages(
|
|
|
174
181
|
switch (contentPart.type) {
|
|
175
182
|
case 'text':
|
|
176
183
|
return { text: contentPart.text };
|
|
177
|
-
case '
|
|
184
|
+
case 'file-data':
|
|
178
185
|
if (!contentPart.mediaType.startsWith('image/')) {
|
|
179
186
|
throw new UnsupportedFunctionalityError({
|
|
180
187
|
functionality: `media type: ${contentPart.mediaType}`,
|
|
@@ -253,6 +260,9 @@ export async function convertToBedrockChatMessages(
|
|
|
253
260
|
const message = block.messages[j];
|
|
254
261
|
const isLastMessage = j === block.messages.length - 1;
|
|
255
262
|
const { content } = message;
|
|
263
|
+
const hasReasoningBlocks = content.some(
|
|
264
|
+
part => part.type === 'reasoning',
|
|
265
|
+
);
|
|
256
266
|
|
|
257
267
|
for (let k = 0; k < content.length; k++) {
|
|
258
268
|
const part = content[k];
|
|
@@ -260,8 +270,8 @@ export async function convertToBedrockChatMessages(
|
|
|
260
270
|
|
|
261
271
|
switch (part.type) {
|
|
262
272
|
case 'text': {
|
|
263
|
-
// Skip empty text blocks
|
|
264
|
-
if (!part.text.trim()) {
|
|
273
|
+
// Skip empty text blocks unless reasoning blocks are present
|
|
274
|
+
if (!part.text.trim() && !hasReasoningBlocks) {
|
|
265
275
|
break;
|
|
266
276
|
}
|
|
267
277
|
|
|
@@ -287,33 +297,41 @@ export async function convertToBedrockChatMessages(
|
|
|
287
297
|
schema: bedrockReasoningMetadataSchema,
|
|
288
298
|
});
|
|
289
299
|
|
|
290
|
-
if (reasoningMetadata != null) {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
text: trimIfLast(
|
|
299
|
-
isLastBlock,
|
|
300
|
-
isLastMessage,
|
|
301
|
-
isLastContentPart,
|
|
302
|
-
part.text,
|
|
303
|
-
),
|
|
304
|
-
signature: reasoningMetadata.signature,
|
|
305
|
-
},
|
|
300
|
+
if (reasoningMetadata?.signature != null) {
|
|
301
|
+
// do not trim reasoning text when a signature is present:
|
|
302
|
+
// the signature validates the exact original bytes
|
|
303
|
+
bedrockContent.push({
|
|
304
|
+
reasoningContent: {
|
|
305
|
+
reasoningText: {
|
|
306
|
+
text: part.text,
|
|
307
|
+
signature: reasoningMetadata.signature,
|
|
306
308
|
},
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
309
|
+
},
|
|
310
|
+
});
|
|
311
|
+
} else if (reasoningMetadata?.redactedData != null) {
|
|
312
|
+
bedrockContent.push({
|
|
313
|
+
reasoningContent: {
|
|
314
|
+
redactedReasoning: {
|
|
315
|
+
data: reasoningMetadata.redactedData,
|
|
314
316
|
},
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
+
},
|
|
318
|
+
});
|
|
319
|
+
} else {
|
|
320
|
+
// trim the last text part if it's the last message in the block
|
|
321
|
+
// because Bedrock does not allow trailing whitespace
|
|
322
|
+
// in pre-filled assistant responses
|
|
323
|
+
bedrockContent.push({
|
|
324
|
+
reasoningContent: {
|
|
325
|
+
reasoningText: {
|
|
326
|
+
text: trimIfLast(
|
|
327
|
+
isLastBlock,
|
|
328
|
+
isLastMessage,
|
|
329
|
+
isLastContentPart,
|
|
330
|
+
part.text,
|
|
331
|
+
),
|
|
332
|
+
},
|
|
333
|
+
},
|
|
334
|
+
});
|
|
317
335
|
}
|
|
318
336
|
|
|
319
337
|
break;
|
|
@@ -352,12 +370,6 @@ export async function convertToBedrockChatMessages(
|
|
|
352
370
|
return { system, messages };
|
|
353
371
|
}
|
|
354
372
|
|
|
355
|
-
function isBedrockImageFormat(format: string): format is BedrockImageFormat {
|
|
356
|
-
return Object.values(BEDROCK_IMAGE_MIME_TYPES).includes(
|
|
357
|
-
format as BedrockImageFormat,
|
|
358
|
-
);
|
|
359
|
-
}
|
|
360
|
-
|
|
361
373
|
function getBedrockImageFormat(mimeType?: string): BedrockImageFormat {
|
|
362
374
|
if (!mimeType) {
|
|
363
375
|
throw new UnsupportedFunctionalityError({
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { ProviderV4, LanguageModelV4 } from '@ai-sdk/provider';
|
|
2
|
-
import { Resolvable, FetchFunction } from '@ai-sdk/provider-utils';
|
|
3
|
-
import { anthropicTools } from '@ai-sdk/anthropic/internal';
|
|
4
|
-
|
|
5
|
-
interface BedrockCredentials {
|
|
6
|
-
region: string;
|
|
7
|
-
accessKeyId: string;
|
|
8
|
-
secretAccessKey: string;
|
|
9
|
-
sessionToken?: string;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
type BedrockAnthropicModelId = 'anthropic.claude-opus-4-6-v1' | 'anthropic.claude-sonnet-4-6-v1' | 'anthropic.claude-opus-4-5-20251101-v1:0' | 'anthropic.claude-sonnet-4-5-20250929-v1:0' | 'anthropic.claude-opus-4-20250514-v1:0' | 'anthropic.claude-sonnet-4-20250514-v1:0' | 'anthropic.claude-opus-4-1-20250805-v1:0' | 'anthropic.claude-haiku-4-5-20251001-v1:0' | 'anthropic.claude-3-7-sonnet-20250219-v1:0' | 'anthropic.claude-3-5-sonnet-20241022-v2:0' | 'anthropic.claude-3-5-sonnet-20240620-v1:0' | 'anthropic.claude-3-5-haiku-20241022-v1:0' | 'anthropic.claude-3-opus-20240229-v1:0' | 'anthropic.claude-3-sonnet-20240229-v1:0' | 'anthropic.claude-3-haiku-20240307-v1:0' | 'us.anthropic.claude-opus-4-6-v1' | 'us.anthropic.claude-sonnet-4-6-v1' | 'us.anthropic.claude-opus-4-5-20251101-v1:0' | 'us.anthropic.claude-sonnet-4-5-20250929-v1:0' | 'us.anthropic.claude-opus-4-20250514-v1:0' | 'us.anthropic.claude-sonnet-4-20250514-v1:0' | 'us.anthropic.claude-opus-4-1-20250805-v1:0' | 'us.anthropic.claude-haiku-4-5-20251001-v1:0' | 'us.anthropic.claude-3-7-sonnet-20250219-v1:0' | 'us.anthropic.claude-3-5-sonnet-20241022-v2:0' | 'us.anthropic.claude-3-5-sonnet-20240620-v1:0' | 'us.anthropic.claude-3-5-haiku-20241022-v1:0' | 'us.anthropic.claude-3-opus-20240229-v1:0' | 'us.anthropic.claude-3-sonnet-20240229-v1:0' | 'us.anthropic.claude-3-haiku-20240307-v1:0' | (string & {});
|
|
13
|
-
|
|
14
|
-
interface BedrockAnthropicProvider extends ProviderV4 {
|
|
15
|
-
/**
|
|
16
|
-
* Creates a model for text generation.
|
|
17
|
-
*/
|
|
18
|
-
(modelId: BedrockAnthropicModelId): LanguageModelV4;
|
|
19
|
-
/**
|
|
20
|
-
* Creates a model for text generation.
|
|
21
|
-
*/
|
|
22
|
-
languageModel(modelId: BedrockAnthropicModelId): LanguageModelV4;
|
|
23
|
-
/**
|
|
24
|
-
* Anthropic-specific computer use tool.
|
|
25
|
-
*/
|
|
26
|
-
tools: typeof anthropicTools;
|
|
27
|
-
/**
|
|
28
|
-
* @deprecated Use `embeddingModel` instead.
|
|
29
|
-
*/
|
|
30
|
-
textEmbeddingModel(modelId: string): never;
|
|
31
|
-
}
|
|
32
|
-
interface BedrockAnthropicProviderSettings {
|
|
33
|
-
/**
|
|
34
|
-
* The AWS region to use for the Bedrock provider. Defaults to the value of the
|
|
35
|
-
* `AWS_REGION` environment variable.
|
|
36
|
-
*/
|
|
37
|
-
region?: string;
|
|
38
|
-
/**
|
|
39
|
-
* API key for authenticating requests using Bearer token authentication.
|
|
40
|
-
* When provided, this will be used instead of AWS SigV4 authentication.
|
|
41
|
-
* Defaults to the value of the `AWS_BEARER_TOKEN_BEDROCK` environment variable.
|
|
42
|
-
*/
|
|
43
|
-
apiKey?: string;
|
|
44
|
-
/**
|
|
45
|
-
* The AWS access key ID to use for the Bedrock provider. Defaults to the value of the
|
|
46
|
-
* `AWS_ACCESS_KEY_ID` environment variable.
|
|
47
|
-
*/
|
|
48
|
-
accessKeyId?: string;
|
|
49
|
-
/**
|
|
50
|
-
* The AWS secret access key to use for the Bedrock provider. Defaults to the value of the
|
|
51
|
-
* `AWS_SECRET_ACCESS_KEY` environment variable.
|
|
52
|
-
*/
|
|
53
|
-
secretAccessKey?: string;
|
|
54
|
-
/**
|
|
55
|
-
* The AWS session token to use for the Bedrock provider. Defaults to the value of the
|
|
56
|
-
* `AWS_SESSION_TOKEN` environment variable.
|
|
57
|
-
*/
|
|
58
|
-
sessionToken?: string;
|
|
59
|
-
/**
|
|
60
|
-
* Base URL for the Bedrock API calls.
|
|
61
|
-
*/
|
|
62
|
-
baseURL?: string;
|
|
63
|
-
/**
|
|
64
|
-
* Custom headers to include in the requests.
|
|
65
|
-
*/
|
|
66
|
-
headers?: Resolvable<Record<string, string | undefined>>;
|
|
67
|
-
/**
|
|
68
|
-
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
69
|
-
* or to provide a custom fetch implementation for e.g. testing.
|
|
70
|
-
*/
|
|
71
|
-
fetch?: FetchFunction;
|
|
72
|
-
/**
|
|
73
|
-
* The AWS credential provider to use for the Bedrock provider to get dynamic
|
|
74
|
-
* credentials similar to the AWS SDK. Setting a provider here will cause its
|
|
75
|
-
* credential values to be used instead of the `accessKeyId`, `secretAccessKey`,
|
|
76
|
-
* and `sessionToken` settings.
|
|
77
|
-
*/
|
|
78
|
-
credentialProvider?: () => PromiseLike<Omit<BedrockCredentials, 'region'>>;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Create an Amazon Bedrock Anthropic provider instance.
|
|
82
|
-
* This provider uses the native Anthropic API through Bedrock's InvokeModel endpoint,
|
|
83
|
-
* bypassing the Converse API for better feature compatibility.
|
|
84
|
-
*/
|
|
85
|
-
declare function createBedrockAnthropic(options?: BedrockAnthropicProviderSettings): BedrockAnthropicProvider;
|
|
86
|
-
/**
|
|
87
|
-
* Default Bedrock Anthropic provider instance.
|
|
88
|
-
*/
|
|
89
|
-
declare const bedrockAnthropic: BedrockAnthropicProvider;
|
|
90
|
-
|
|
91
|
-
export { type BedrockAnthropicModelId, type BedrockAnthropicProvider, type BedrockAnthropicProviderSettings, bedrockAnthropic, createBedrockAnthropic };
|
package/dist/anthropic/index.mjs
DELETED
|
@@ -1,397 +0,0 @@
|
|
|
1
|
-
// src/anthropic/bedrock-anthropic-provider.ts
|
|
2
|
-
import {
|
|
3
|
-
NoSuchModelError
|
|
4
|
-
} from "@ai-sdk/provider";
|
|
5
|
-
import {
|
|
6
|
-
loadOptionalSetting,
|
|
7
|
-
loadSetting,
|
|
8
|
-
resolve,
|
|
9
|
-
withoutTrailingSlash,
|
|
10
|
-
withUserAgentSuffix as withUserAgentSuffix2
|
|
11
|
-
} from "@ai-sdk/provider-utils";
|
|
12
|
-
import {
|
|
13
|
-
anthropicTools,
|
|
14
|
-
AnthropicMessagesLanguageModel
|
|
15
|
-
} from "@ai-sdk/anthropic/internal";
|
|
16
|
-
|
|
17
|
-
// src/bedrock-sigv4-fetch.ts
|
|
18
|
-
import {
|
|
19
|
-
combineHeaders,
|
|
20
|
-
normalizeHeaders,
|
|
21
|
-
withUserAgentSuffix,
|
|
22
|
-
getRuntimeEnvironmentUserAgent
|
|
23
|
-
} from "@ai-sdk/provider-utils";
|
|
24
|
-
import { AwsV4Signer } from "aws4fetch";
|
|
25
|
-
|
|
26
|
-
// src/version.ts
|
|
27
|
-
var VERSION = true ? "5.0.0-beta.4" : "0.0.0-test";
|
|
28
|
-
|
|
29
|
-
// src/bedrock-sigv4-fetch.ts
|
|
30
|
-
function createSigV4FetchFunction(getCredentials, fetch = globalThis.fetch) {
|
|
31
|
-
return async (input, init) => {
|
|
32
|
-
var _a, _b;
|
|
33
|
-
const request = input instanceof Request ? input : void 0;
|
|
34
|
-
const originalHeaders = combineHeaders(
|
|
35
|
-
normalizeHeaders(request == null ? void 0 : request.headers),
|
|
36
|
-
normalizeHeaders(init == null ? void 0 : init.headers)
|
|
37
|
-
);
|
|
38
|
-
const headersWithUserAgent = withUserAgentSuffix(
|
|
39
|
-
originalHeaders,
|
|
40
|
-
`ai-sdk/amazon-bedrock/${VERSION}`,
|
|
41
|
-
getRuntimeEnvironmentUserAgent()
|
|
42
|
-
);
|
|
43
|
-
let effectiveBody = (_a = init == null ? void 0 : init.body) != null ? _a : void 0;
|
|
44
|
-
if (effectiveBody === void 0 && request && request.body !== null) {
|
|
45
|
-
try {
|
|
46
|
-
effectiveBody = await request.clone().text();
|
|
47
|
-
} catch (e) {
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
const effectiveMethod = (_b = init == null ? void 0 : init.method) != null ? _b : request == null ? void 0 : request.method;
|
|
51
|
-
if ((effectiveMethod == null ? void 0 : effectiveMethod.toUpperCase()) !== "POST" || !effectiveBody) {
|
|
52
|
-
return fetch(input, {
|
|
53
|
-
...init,
|
|
54
|
-
headers: headersWithUserAgent
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
|
|
58
|
-
const body = prepareBodyString(effectiveBody);
|
|
59
|
-
const credentials = await getCredentials();
|
|
60
|
-
const signer = new AwsV4Signer({
|
|
61
|
-
url,
|
|
62
|
-
method: "POST",
|
|
63
|
-
headers: Object.entries(headersWithUserAgent),
|
|
64
|
-
body,
|
|
65
|
-
region: credentials.region,
|
|
66
|
-
accessKeyId: credentials.accessKeyId,
|
|
67
|
-
secretAccessKey: credentials.secretAccessKey,
|
|
68
|
-
sessionToken: credentials.sessionToken,
|
|
69
|
-
service: "bedrock"
|
|
70
|
-
});
|
|
71
|
-
const signingResult = await signer.sign();
|
|
72
|
-
const signedHeaders = normalizeHeaders(signingResult.headers);
|
|
73
|
-
const combinedHeaders = combineHeaders(headersWithUserAgent, signedHeaders);
|
|
74
|
-
return fetch(input, {
|
|
75
|
-
...init,
|
|
76
|
-
body,
|
|
77
|
-
headers: combinedHeaders
|
|
78
|
-
});
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
function prepareBodyString(body) {
|
|
82
|
-
if (typeof body === "string") {
|
|
83
|
-
return body;
|
|
84
|
-
} else if (body instanceof Uint8Array) {
|
|
85
|
-
return new TextDecoder().decode(body);
|
|
86
|
-
} else if (body instanceof ArrayBuffer) {
|
|
87
|
-
return new TextDecoder().decode(new Uint8Array(body));
|
|
88
|
-
} else {
|
|
89
|
-
return JSON.stringify(body);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
function createApiKeyFetchFunction(apiKey, fetch = globalThis.fetch) {
|
|
93
|
-
return async (input, init) => {
|
|
94
|
-
const originalHeaders = normalizeHeaders(init == null ? void 0 : init.headers);
|
|
95
|
-
const headersWithUserAgent = withUserAgentSuffix(
|
|
96
|
-
originalHeaders,
|
|
97
|
-
`ai-sdk/amazon-bedrock/${VERSION}`,
|
|
98
|
-
getRuntimeEnvironmentUserAgent()
|
|
99
|
-
);
|
|
100
|
-
const finalHeaders = combineHeaders(headersWithUserAgent, {
|
|
101
|
-
Authorization: `Bearer ${apiKey}`
|
|
102
|
-
});
|
|
103
|
-
return fetch(input, {
|
|
104
|
-
...init,
|
|
105
|
-
headers: finalHeaders
|
|
106
|
-
});
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// src/anthropic/bedrock-anthropic-fetch.ts
|
|
111
|
-
import {
|
|
112
|
-
convertBase64ToUint8Array,
|
|
113
|
-
safeParseJSON
|
|
114
|
-
} from "@ai-sdk/provider-utils";
|
|
115
|
-
|
|
116
|
-
// src/bedrock-event-stream-decoder.ts
|
|
117
|
-
import { EventStreamCodec } from "@smithy/eventstream-codec";
|
|
118
|
-
import { toUtf8, fromUtf8 } from "@smithy/util-utf8";
|
|
119
|
-
function createBedrockEventStreamDecoder(body, processEvent) {
|
|
120
|
-
const codec = new EventStreamCodec(toUtf8, fromUtf8);
|
|
121
|
-
let buffer = new Uint8Array(0);
|
|
122
|
-
const textDecoder = new TextDecoder();
|
|
123
|
-
return body.pipeThrough(
|
|
124
|
-
new TransformStream({
|
|
125
|
-
async transform(chunk, controller) {
|
|
126
|
-
var _a, _b;
|
|
127
|
-
const newBuffer = new Uint8Array(buffer.length + chunk.length);
|
|
128
|
-
newBuffer.set(buffer);
|
|
129
|
-
newBuffer.set(chunk, buffer.length);
|
|
130
|
-
buffer = newBuffer;
|
|
131
|
-
while (buffer.length >= 4) {
|
|
132
|
-
const totalLength = new DataView(
|
|
133
|
-
buffer.buffer,
|
|
134
|
-
buffer.byteOffset,
|
|
135
|
-
buffer.byteLength
|
|
136
|
-
).getUint32(0, false);
|
|
137
|
-
if (buffer.length < totalLength) {
|
|
138
|
-
break;
|
|
139
|
-
}
|
|
140
|
-
try {
|
|
141
|
-
const subView = buffer.subarray(0, totalLength);
|
|
142
|
-
const decoded = codec.decode(subView);
|
|
143
|
-
buffer = buffer.slice(totalLength);
|
|
144
|
-
const messageType = (_a = decoded.headers[":message-type"]) == null ? void 0 : _a.value;
|
|
145
|
-
const eventType = (_b = decoded.headers[":event-type"]) == null ? void 0 : _b.value;
|
|
146
|
-
const data = textDecoder.decode(decoded.body);
|
|
147
|
-
await processEvent({ messageType, eventType, data }, controller);
|
|
148
|
-
} catch (e) {
|
|
149
|
-
break;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
})
|
|
154
|
-
);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// src/anthropic/bedrock-anthropic-fetch.ts
|
|
158
|
-
function createBedrockAnthropicFetch(baseFetch) {
|
|
159
|
-
return async (url, options) => {
|
|
160
|
-
const response = await baseFetch(url, options);
|
|
161
|
-
const contentType = response.headers.get("content-type");
|
|
162
|
-
if ((contentType == null ? void 0 : contentType.includes("application/vnd.amazon.eventstream")) && response.body != null) {
|
|
163
|
-
const transformedBody = transformBedrockEventStreamToSSE(response.body);
|
|
164
|
-
return new Response(transformedBody, {
|
|
165
|
-
status: response.status,
|
|
166
|
-
statusText: response.statusText,
|
|
167
|
-
headers: new Headers({
|
|
168
|
-
...Object.fromEntries(response.headers.entries()),
|
|
169
|
-
"content-type": "text/event-stream"
|
|
170
|
-
})
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
return response;
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
function transformBedrockEventStreamToSSE(body) {
|
|
177
|
-
const textEncoder = new TextEncoder();
|
|
178
|
-
return createBedrockEventStreamDecoder(body, async (event, controller) => {
|
|
179
|
-
if (event.messageType === "event") {
|
|
180
|
-
if (event.eventType === "chunk") {
|
|
181
|
-
const parsed = await safeParseJSON({ text: event.data });
|
|
182
|
-
if (!parsed.success) {
|
|
183
|
-
controller.enqueue(textEncoder.encode(`data: ${event.data}
|
|
184
|
-
|
|
185
|
-
`));
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
const bytes = parsed.value.bytes;
|
|
189
|
-
if (bytes) {
|
|
190
|
-
const anthropicEvent = new TextDecoder().decode(
|
|
191
|
-
convertBase64ToUint8Array(bytes)
|
|
192
|
-
);
|
|
193
|
-
controller.enqueue(textEncoder.encode(`data: ${anthropicEvent}
|
|
194
|
-
|
|
195
|
-
`));
|
|
196
|
-
} else {
|
|
197
|
-
controller.enqueue(textEncoder.encode(`data: ${event.data}
|
|
198
|
-
|
|
199
|
-
`));
|
|
200
|
-
}
|
|
201
|
-
} else if (event.eventType === "messageStop") {
|
|
202
|
-
controller.enqueue(textEncoder.encode("data: [DONE]\n\n"));
|
|
203
|
-
}
|
|
204
|
-
} else if (event.messageType === "exception") {
|
|
205
|
-
controller.enqueue(
|
|
206
|
-
textEncoder.encode(
|
|
207
|
-
`data: ${JSON.stringify({ type: "error", error: event.data })}
|
|
208
|
-
|
|
209
|
-
`
|
|
210
|
-
)
|
|
211
|
-
);
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// src/anthropic/bedrock-anthropic-provider.ts
|
|
217
|
-
var BEDROCK_TOOL_VERSION_MAP = {
|
|
218
|
-
bash_20241022: "bash_20250124",
|
|
219
|
-
text_editor_20241022: "text_editor_20250728",
|
|
220
|
-
computer_20241022: "computer_20250124"
|
|
221
|
-
};
|
|
222
|
-
var BEDROCK_TOOL_NAME_MAP = {
|
|
223
|
-
text_editor_20250728: "str_replace_based_edit_tool"
|
|
224
|
-
};
|
|
225
|
-
var BEDROCK_TOOL_BETA_MAP = {
|
|
226
|
-
bash_20250124: "computer-use-2025-01-24",
|
|
227
|
-
bash_20241022: "computer-use-2024-10-22",
|
|
228
|
-
text_editor_20250124: "computer-use-2025-01-24",
|
|
229
|
-
text_editor_20241022: "computer-use-2024-10-22",
|
|
230
|
-
text_editor_20250429: "computer-use-2025-01-24",
|
|
231
|
-
text_editor_20250728: "computer-use-2025-01-24",
|
|
232
|
-
computer_20250124: "computer-use-2025-01-24",
|
|
233
|
-
computer_20241022: "computer-use-2024-10-22"
|
|
234
|
-
};
|
|
235
|
-
function createBedrockAnthropic(options = {}) {
|
|
236
|
-
const rawApiKey = loadOptionalSetting({
|
|
237
|
-
settingValue: options.apiKey,
|
|
238
|
-
environmentVariableName: "AWS_BEARER_TOKEN_BEDROCK"
|
|
239
|
-
});
|
|
240
|
-
const apiKey = rawApiKey && rawApiKey.trim().length > 0 ? rawApiKey.trim() : void 0;
|
|
241
|
-
const baseFetchFunction = apiKey ? createApiKeyFetchFunction(apiKey, options.fetch) : createSigV4FetchFunction(async () => {
|
|
242
|
-
const region = loadSetting({
|
|
243
|
-
settingValue: options.region,
|
|
244
|
-
settingName: "region",
|
|
245
|
-
environmentVariableName: "AWS_REGION",
|
|
246
|
-
description: "AWS region"
|
|
247
|
-
});
|
|
248
|
-
if (options.credentialProvider) {
|
|
249
|
-
try {
|
|
250
|
-
return {
|
|
251
|
-
...await options.credentialProvider(),
|
|
252
|
-
region
|
|
253
|
-
};
|
|
254
|
-
} catch (error) {
|
|
255
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
256
|
-
throw new Error(
|
|
257
|
-
`AWS credential provider failed: ${errorMessage}. Please ensure your credential provider returns valid AWS credentials with accessKeyId and secretAccessKey properties.`
|
|
258
|
-
);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
try {
|
|
262
|
-
return {
|
|
263
|
-
region,
|
|
264
|
-
accessKeyId: loadSetting({
|
|
265
|
-
settingValue: options.accessKeyId,
|
|
266
|
-
settingName: "accessKeyId",
|
|
267
|
-
environmentVariableName: "AWS_ACCESS_KEY_ID",
|
|
268
|
-
description: "AWS access key ID"
|
|
269
|
-
}),
|
|
270
|
-
secretAccessKey: loadSetting({
|
|
271
|
-
settingValue: options.secretAccessKey,
|
|
272
|
-
settingName: "secretAccessKey",
|
|
273
|
-
environmentVariableName: "AWS_SECRET_ACCESS_KEY",
|
|
274
|
-
description: "AWS secret access key"
|
|
275
|
-
}),
|
|
276
|
-
sessionToken: loadOptionalSetting({
|
|
277
|
-
settingValue: options.sessionToken,
|
|
278
|
-
environmentVariableName: "AWS_SESSION_TOKEN"
|
|
279
|
-
})
|
|
280
|
-
};
|
|
281
|
-
} catch (error) {
|
|
282
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
283
|
-
if (errorMessage.includes("AWS_ACCESS_KEY_ID") || errorMessage.includes("accessKeyId")) {
|
|
284
|
-
throw new Error(
|
|
285
|
-
`AWS SigV4 authentication requires AWS credentials. Please provide either:
|
|
286
|
-
1. Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables
|
|
287
|
-
2. Provide accessKeyId and secretAccessKey in options
|
|
288
|
-
3. Use a credentialProvider function
|
|
289
|
-
4. Use API key authentication with AWS_BEARER_TOKEN_BEDROCK or apiKey option
|
|
290
|
-
Original error: ${errorMessage}`
|
|
291
|
-
);
|
|
292
|
-
}
|
|
293
|
-
if (errorMessage.includes("AWS_SECRET_ACCESS_KEY") || errorMessage.includes("secretAccessKey")) {
|
|
294
|
-
throw new Error(
|
|
295
|
-
`AWS SigV4 authentication requires both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. Please ensure both credentials are provided.
|
|
296
|
-
Original error: ${errorMessage}`
|
|
297
|
-
);
|
|
298
|
-
}
|
|
299
|
-
throw error;
|
|
300
|
-
}
|
|
301
|
-
}, options.fetch);
|
|
302
|
-
const fetchFunction = createBedrockAnthropicFetch(baseFetchFunction);
|
|
303
|
-
const getBaseURL = () => {
|
|
304
|
-
var _a, _b;
|
|
305
|
-
return (_b = withoutTrailingSlash(
|
|
306
|
-
(_a = options.baseURL) != null ? _a : `https://bedrock-runtime.${loadSetting({
|
|
307
|
-
settingValue: options.region,
|
|
308
|
-
settingName: "region",
|
|
309
|
-
environmentVariableName: "AWS_REGION",
|
|
310
|
-
description: "AWS region"
|
|
311
|
-
})}.amazonaws.com`
|
|
312
|
-
)) != null ? _b : "https://bedrock-runtime.us-east-1.amazonaws.com";
|
|
313
|
-
};
|
|
314
|
-
const getHeaders = async () => {
|
|
315
|
-
var _a;
|
|
316
|
-
const baseHeaders = (_a = await resolve(options.headers)) != null ? _a : {};
|
|
317
|
-
return withUserAgentSuffix2(baseHeaders, `ai-sdk/amazon-bedrock/${VERSION}`);
|
|
318
|
-
};
|
|
319
|
-
const createChatModel = (modelId) => new AnthropicMessagesLanguageModel(modelId, {
|
|
320
|
-
provider: "bedrock.anthropic.messages",
|
|
321
|
-
baseURL: getBaseURL(),
|
|
322
|
-
headers: getHeaders,
|
|
323
|
-
fetch: fetchFunction,
|
|
324
|
-
buildRequestUrl: (baseURL, isStreaming) => `${baseURL}/model/${encodeURIComponent(modelId)}/${isStreaming ? "invoke-with-response-stream" : "invoke"}`,
|
|
325
|
-
transformRequestBody: (args, betas) => {
|
|
326
|
-
const { model, stream, tool_choice, tools, ...rest } = args;
|
|
327
|
-
const transformedToolChoice = tool_choice != null ? {
|
|
328
|
-
type: tool_choice.type,
|
|
329
|
-
...tool_choice.name != null ? { name: tool_choice.name } : {}
|
|
330
|
-
} : void 0;
|
|
331
|
-
const requiredBetas = new Set(betas);
|
|
332
|
-
const transformedTools = tools == null ? void 0 : tools.map((tool) => {
|
|
333
|
-
const toolType = tool.type;
|
|
334
|
-
if (toolType && toolType in BEDROCK_TOOL_VERSION_MAP) {
|
|
335
|
-
const newType = BEDROCK_TOOL_VERSION_MAP[toolType];
|
|
336
|
-
if (newType in BEDROCK_TOOL_BETA_MAP) {
|
|
337
|
-
requiredBetas.add(BEDROCK_TOOL_BETA_MAP[newType]);
|
|
338
|
-
}
|
|
339
|
-
const newName = newType in BEDROCK_TOOL_NAME_MAP ? BEDROCK_TOOL_NAME_MAP[newType] : tool.name;
|
|
340
|
-
return {
|
|
341
|
-
...tool,
|
|
342
|
-
type: newType,
|
|
343
|
-
name: newName
|
|
344
|
-
};
|
|
345
|
-
}
|
|
346
|
-
if (toolType && toolType in BEDROCK_TOOL_BETA_MAP) {
|
|
347
|
-
requiredBetas.add(BEDROCK_TOOL_BETA_MAP[toolType]);
|
|
348
|
-
}
|
|
349
|
-
if (toolType && toolType in BEDROCK_TOOL_NAME_MAP) {
|
|
350
|
-
return {
|
|
351
|
-
...tool,
|
|
352
|
-
name: BEDROCK_TOOL_NAME_MAP[toolType]
|
|
353
|
-
};
|
|
354
|
-
}
|
|
355
|
-
return tool;
|
|
356
|
-
});
|
|
357
|
-
return {
|
|
358
|
-
...rest,
|
|
359
|
-
...transformedTools != null ? { tools: transformedTools } : {},
|
|
360
|
-
...transformedToolChoice != null ? { tool_choice: transformedToolChoice } : {},
|
|
361
|
-
...requiredBetas.size > 0 ? { anthropic_beta: Array.from(requiredBetas) } : {},
|
|
362
|
-
anthropic_version: "bedrock-2023-05-31"
|
|
363
|
-
};
|
|
364
|
-
},
|
|
365
|
-
// Bedrock Anthropic doesn't support URL sources, force download and base64 conversion
|
|
366
|
-
supportedUrls: () => ({}),
|
|
367
|
-
// native structured output via output_config.format is supported on Bedrock
|
|
368
|
-
supportsNativeStructuredOutput: true
|
|
369
|
-
});
|
|
370
|
-
const provider = function(modelId) {
|
|
371
|
-
if (new.target) {
|
|
372
|
-
throw new Error(
|
|
373
|
-
"The Bedrock Anthropic model function cannot be called with the new keyword."
|
|
374
|
-
);
|
|
375
|
-
}
|
|
376
|
-
return createChatModel(modelId);
|
|
377
|
-
};
|
|
378
|
-
provider.specificationVersion = "v4";
|
|
379
|
-
provider.languageModel = createChatModel;
|
|
380
|
-
provider.chat = createChatModel;
|
|
381
|
-
provider.messages = createChatModel;
|
|
382
|
-
provider.embeddingModel = (modelId) => {
|
|
383
|
-
throw new NoSuchModelError({ modelId, modelType: "embeddingModel" });
|
|
384
|
-
};
|
|
385
|
-
provider.textEmbeddingModel = provider.embeddingModel;
|
|
386
|
-
provider.imageModel = (modelId) => {
|
|
387
|
-
throw new NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
388
|
-
};
|
|
389
|
-
provider.tools = anthropicTools;
|
|
390
|
-
return provider;
|
|
391
|
-
}
|
|
392
|
-
var bedrockAnthropic = createBedrockAnthropic();
|
|
393
|
-
export {
|
|
394
|
-
bedrockAnthropic,
|
|
395
|
-
createBedrockAnthropic
|
|
396
|
-
};
|
|
397
|
-
//# sourceMappingURL=index.mjs.map
|