@ai-sdk/amazon-bedrock 4.0.20 → 4.0.22
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 +13 -0
- package/anthropic/index.d.ts +1 -0
- package/dist/anthropic/index.d.mts +91 -0
- package/dist/anthropic/index.d.ts +91 -0
- package/dist/anthropic/index.js +404 -0
- package/dist/anthropic/index.js.map +1 -0
- package/dist/anthropic/index.mjs +392 -0
- package/dist/anthropic/index.mjs.map +1 -0
- package/dist/index.js +68 -54
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +68 -54
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# @ai-sdk/amazon-bedrock
|
|
2
2
|
|
|
3
|
+
## 4.0.22
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 3318e0d: feat(provider/amazon-bedrock): add anthropic sub-provider using native invokemodel api
|
|
8
|
+
|
|
9
|
+
## 4.0.21
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Updated dependencies [4729bed]
|
|
14
|
+
- @ai-sdk/anthropic@3.0.17
|
|
15
|
+
|
|
3
16
|
## 4.0.20
|
|
4
17
|
|
|
5
18
|
### Patch Changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/anthropic';
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { ProviderV3, LanguageModelV3 } 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-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-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 ProviderV3 {
|
|
15
|
+
/**
|
|
16
|
+
Creates a model for text generation.
|
|
17
|
+
*/
|
|
18
|
+
(modelId: BedrockAnthropicModelId): LanguageModelV3;
|
|
19
|
+
/**
|
|
20
|
+
Creates a model for text generation.
|
|
21
|
+
*/
|
|
22
|
+
languageModel(modelId: BedrockAnthropicModelId): LanguageModelV3;
|
|
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 };
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { ProviderV3, LanguageModelV3 } 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-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-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 ProviderV3 {
|
|
15
|
+
/**
|
|
16
|
+
Creates a model for text generation.
|
|
17
|
+
*/
|
|
18
|
+
(modelId: BedrockAnthropicModelId): LanguageModelV3;
|
|
19
|
+
/**
|
|
20
|
+
Creates a model for text generation.
|
|
21
|
+
*/
|
|
22
|
+
languageModel(modelId: BedrockAnthropicModelId): LanguageModelV3;
|
|
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 };
|
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/anthropic/index.ts
|
|
21
|
+
var anthropic_exports = {};
|
|
22
|
+
__export(anthropic_exports, {
|
|
23
|
+
bedrockAnthropic: () => bedrockAnthropic,
|
|
24
|
+
createBedrockAnthropic: () => createBedrockAnthropic
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(anthropic_exports);
|
|
27
|
+
|
|
28
|
+
// src/anthropic/bedrock-anthropic-provider.ts
|
|
29
|
+
var import_provider = require("@ai-sdk/provider");
|
|
30
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
31
|
+
var import_internal = require("@ai-sdk/anthropic/internal");
|
|
32
|
+
|
|
33
|
+
// src/bedrock-sigv4-fetch.ts
|
|
34
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
35
|
+
var import_aws4fetch = require("aws4fetch");
|
|
36
|
+
|
|
37
|
+
// src/version.ts
|
|
38
|
+
var VERSION = true ? "4.0.22" : "0.0.0-test";
|
|
39
|
+
|
|
40
|
+
// src/bedrock-sigv4-fetch.ts
|
|
41
|
+
function createSigV4FetchFunction(getCredentials, fetch = globalThis.fetch) {
|
|
42
|
+
return async (input, init) => {
|
|
43
|
+
var _a, _b;
|
|
44
|
+
const request = input instanceof Request ? input : void 0;
|
|
45
|
+
const originalHeaders = (0, import_provider_utils.combineHeaders)(
|
|
46
|
+
(0, import_provider_utils.normalizeHeaders)(request == null ? void 0 : request.headers),
|
|
47
|
+
(0, import_provider_utils.normalizeHeaders)(init == null ? void 0 : init.headers)
|
|
48
|
+
);
|
|
49
|
+
const headersWithUserAgent = (0, import_provider_utils.withUserAgentSuffix)(
|
|
50
|
+
originalHeaders,
|
|
51
|
+
`ai-sdk/amazon-bedrock/${VERSION}`,
|
|
52
|
+
(0, import_provider_utils.getRuntimeEnvironmentUserAgent)()
|
|
53
|
+
);
|
|
54
|
+
let effectiveBody = (_a = init == null ? void 0 : init.body) != null ? _a : void 0;
|
|
55
|
+
if (effectiveBody === void 0 && request && request.body !== null) {
|
|
56
|
+
try {
|
|
57
|
+
effectiveBody = await request.clone().text();
|
|
58
|
+
} catch (e) {
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const effectiveMethod = (_b = init == null ? void 0 : init.method) != null ? _b : request == null ? void 0 : request.method;
|
|
62
|
+
if ((effectiveMethod == null ? void 0 : effectiveMethod.toUpperCase()) !== "POST" || !effectiveBody) {
|
|
63
|
+
return fetch(input, {
|
|
64
|
+
...init,
|
|
65
|
+
headers: headersWithUserAgent
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
|
|
69
|
+
const body = prepareBodyString(effectiveBody);
|
|
70
|
+
const credentials = await getCredentials();
|
|
71
|
+
const signer = new import_aws4fetch.AwsV4Signer({
|
|
72
|
+
url,
|
|
73
|
+
method: "POST",
|
|
74
|
+
headers: Object.entries(headersWithUserAgent),
|
|
75
|
+
body,
|
|
76
|
+
region: credentials.region,
|
|
77
|
+
accessKeyId: credentials.accessKeyId,
|
|
78
|
+
secretAccessKey: credentials.secretAccessKey,
|
|
79
|
+
sessionToken: credentials.sessionToken,
|
|
80
|
+
service: "bedrock"
|
|
81
|
+
});
|
|
82
|
+
const signingResult = await signer.sign();
|
|
83
|
+
const signedHeaders = (0, import_provider_utils.normalizeHeaders)(signingResult.headers);
|
|
84
|
+
const combinedHeaders = (0, import_provider_utils.combineHeaders)(headersWithUserAgent, signedHeaders);
|
|
85
|
+
return fetch(input, {
|
|
86
|
+
...init,
|
|
87
|
+
body,
|
|
88
|
+
headers: combinedHeaders
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function prepareBodyString(body) {
|
|
93
|
+
if (typeof body === "string") {
|
|
94
|
+
return body;
|
|
95
|
+
} else if (body instanceof Uint8Array) {
|
|
96
|
+
return new TextDecoder().decode(body);
|
|
97
|
+
} else if (body instanceof ArrayBuffer) {
|
|
98
|
+
return new TextDecoder().decode(new Uint8Array(body));
|
|
99
|
+
} else {
|
|
100
|
+
return JSON.stringify(body);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function createApiKeyFetchFunction(apiKey, fetch = globalThis.fetch) {
|
|
104
|
+
return async (input, init) => {
|
|
105
|
+
const originalHeaders = (0, import_provider_utils.normalizeHeaders)(init == null ? void 0 : init.headers);
|
|
106
|
+
const headersWithUserAgent = (0, import_provider_utils.withUserAgentSuffix)(
|
|
107
|
+
originalHeaders,
|
|
108
|
+
`ai-sdk/amazon-bedrock/${VERSION}`,
|
|
109
|
+
(0, import_provider_utils.getRuntimeEnvironmentUserAgent)()
|
|
110
|
+
);
|
|
111
|
+
const finalHeaders = (0, import_provider_utils.combineHeaders)(headersWithUserAgent, {
|
|
112
|
+
Authorization: `Bearer ${apiKey}`
|
|
113
|
+
});
|
|
114
|
+
return fetch(input, {
|
|
115
|
+
...init,
|
|
116
|
+
headers: finalHeaders
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// src/anthropic/bedrock-anthropic-fetch.ts
|
|
122
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
123
|
+
|
|
124
|
+
// src/bedrock-event-stream-decoder.ts
|
|
125
|
+
var import_eventstream_codec = require("@smithy/eventstream-codec");
|
|
126
|
+
var import_util_utf8 = require("@smithy/util-utf8");
|
|
127
|
+
function createBedrockEventStreamDecoder(body, processEvent) {
|
|
128
|
+
const codec = new import_eventstream_codec.EventStreamCodec(import_util_utf8.toUtf8, import_util_utf8.fromUtf8);
|
|
129
|
+
let buffer = new Uint8Array(0);
|
|
130
|
+
const textDecoder = new TextDecoder();
|
|
131
|
+
return body.pipeThrough(
|
|
132
|
+
new TransformStream({
|
|
133
|
+
async transform(chunk, controller) {
|
|
134
|
+
var _a, _b;
|
|
135
|
+
const newBuffer = new Uint8Array(buffer.length + chunk.length);
|
|
136
|
+
newBuffer.set(buffer);
|
|
137
|
+
newBuffer.set(chunk, buffer.length);
|
|
138
|
+
buffer = newBuffer;
|
|
139
|
+
while (buffer.length >= 4) {
|
|
140
|
+
const totalLength = new DataView(
|
|
141
|
+
buffer.buffer,
|
|
142
|
+
buffer.byteOffset,
|
|
143
|
+
buffer.byteLength
|
|
144
|
+
).getUint32(0, false);
|
|
145
|
+
if (buffer.length < totalLength) {
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
try {
|
|
149
|
+
const subView = buffer.subarray(0, totalLength);
|
|
150
|
+
const decoded = codec.decode(subView);
|
|
151
|
+
buffer = buffer.slice(totalLength);
|
|
152
|
+
const messageType = (_a = decoded.headers[":message-type"]) == null ? void 0 : _a.value;
|
|
153
|
+
const eventType = (_b = decoded.headers[":event-type"]) == null ? void 0 : _b.value;
|
|
154
|
+
const data = textDecoder.decode(decoded.body);
|
|
155
|
+
await processEvent({ messageType, eventType, data }, controller);
|
|
156
|
+
} catch (e) {
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
})
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// src/anthropic/bedrock-anthropic-fetch.ts
|
|
166
|
+
function createBedrockAnthropicFetch(baseFetch) {
|
|
167
|
+
return async (url, options) => {
|
|
168
|
+
const response = await baseFetch(url, options);
|
|
169
|
+
const contentType = response.headers.get("content-type");
|
|
170
|
+
if ((contentType == null ? void 0 : contentType.includes("application/vnd.amazon.eventstream")) && response.body != null) {
|
|
171
|
+
const transformedBody = transformBedrockEventStreamToSSE(response.body);
|
|
172
|
+
return new Response(transformedBody, {
|
|
173
|
+
status: response.status,
|
|
174
|
+
statusText: response.statusText,
|
|
175
|
+
headers: new Headers({
|
|
176
|
+
...Object.fromEntries(response.headers.entries()),
|
|
177
|
+
"content-type": "text/event-stream"
|
|
178
|
+
})
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
return response;
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
function transformBedrockEventStreamToSSE(body) {
|
|
185
|
+
const textEncoder = new TextEncoder();
|
|
186
|
+
return createBedrockEventStreamDecoder(body, async (event, controller) => {
|
|
187
|
+
if (event.messageType === "event") {
|
|
188
|
+
if (event.eventType === "chunk") {
|
|
189
|
+
const parsed = await (0, import_provider_utils2.safeParseJSON)({ text: event.data });
|
|
190
|
+
if (!parsed.success) {
|
|
191
|
+
controller.enqueue(textEncoder.encode(`data: ${event.data}
|
|
192
|
+
|
|
193
|
+
`));
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
const bytes = parsed.value.bytes;
|
|
197
|
+
if (bytes) {
|
|
198
|
+
const anthropicEvent = atob(bytes);
|
|
199
|
+
controller.enqueue(textEncoder.encode(`data: ${anthropicEvent}
|
|
200
|
+
|
|
201
|
+
`));
|
|
202
|
+
} else {
|
|
203
|
+
controller.enqueue(textEncoder.encode(`data: ${event.data}
|
|
204
|
+
|
|
205
|
+
`));
|
|
206
|
+
}
|
|
207
|
+
} else if (event.eventType === "messageStop") {
|
|
208
|
+
controller.enqueue(textEncoder.encode("data: [DONE]\n\n"));
|
|
209
|
+
}
|
|
210
|
+
} else if (event.messageType === "exception") {
|
|
211
|
+
controller.enqueue(
|
|
212
|
+
textEncoder.encode(
|
|
213
|
+
`data: ${JSON.stringify({ type: "error", error: event.data })}
|
|
214
|
+
|
|
215
|
+
`
|
|
216
|
+
)
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// src/anthropic/bedrock-anthropic-provider.ts
|
|
223
|
+
var BEDROCK_TOOL_VERSION_MAP = {
|
|
224
|
+
bash_20241022: "bash_20250124",
|
|
225
|
+
text_editor_20241022: "text_editor_20250728",
|
|
226
|
+
computer_20241022: "computer_20250124"
|
|
227
|
+
};
|
|
228
|
+
var BEDROCK_TOOL_NAME_MAP = {
|
|
229
|
+
text_editor_20250728: "str_replace_based_edit_tool"
|
|
230
|
+
};
|
|
231
|
+
var BEDROCK_TOOL_BETA_MAP = {
|
|
232
|
+
bash_20250124: "computer-use-2025-01-24",
|
|
233
|
+
bash_20241022: "computer-use-2024-10-22",
|
|
234
|
+
text_editor_20250124: "computer-use-2025-01-24",
|
|
235
|
+
text_editor_20241022: "computer-use-2024-10-22",
|
|
236
|
+
text_editor_20250429: "computer-use-2025-01-24",
|
|
237
|
+
text_editor_20250728: "computer-use-2025-01-24",
|
|
238
|
+
computer_20250124: "computer-use-2025-01-24",
|
|
239
|
+
computer_20241022: "computer-use-2024-10-22"
|
|
240
|
+
};
|
|
241
|
+
function createBedrockAnthropic(options = {}) {
|
|
242
|
+
const rawApiKey = (0, import_provider_utils3.loadOptionalSetting)({
|
|
243
|
+
settingValue: options.apiKey,
|
|
244
|
+
environmentVariableName: "AWS_BEARER_TOKEN_BEDROCK"
|
|
245
|
+
});
|
|
246
|
+
const apiKey = rawApiKey && rawApiKey.trim().length > 0 ? rawApiKey.trim() : void 0;
|
|
247
|
+
const baseFetchFunction = apiKey ? createApiKeyFetchFunction(apiKey, options.fetch) : createSigV4FetchFunction(async () => {
|
|
248
|
+
const region = (0, import_provider_utils3.loadSetting)({
|
|
249
|
+
settingValue: options.region,
|
|
250
|
+
settingName: "region",
|
|
251
|
+
environmentVariableName: "AWS_REGION",
|
|
252
|
+
description: "AWS region"
|
|
253
|
+
});
|
|
254
|
+
if (options.credentialProvider) {
|
|
255
|
+
try {
|
|
256
|
+
return {
|
|
257
|
+
...await options.credentialProvider(),
|
|
258
|
+
region
|
|
259
|
+
};
|
|
260
|
+
} catch (error) {
|
|
261
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
262
|
+
throw new Error(
|
|
263
|
+
`AWS credential provider failed: ${errorMessage}. Please ensure your credential provider returns valid AWS credentials with accessKeyId and secretAccessKey properties.`
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
try {
|
|
268
|
+
return {
|
|
269
|
+
region,
|
|
270
|
+
accessKeyId: (0, import_provider_utils3.loadSetting)({
|
|
271
|
+
settingValue: options.accessKeyId,
|
|
272
|
+
settingName: "accessKeyId",
|
|
273
|
+
environmentVariableName: "AWS_ACCESS_KEY_ID",
|
|
274
|
+
description: "AWS access key ID"
|
|
275
|
+
}),
|
|
276
|
+
secretAccessKey: (0, import_provider_utils3.loadSetting)({
|
|
277
|
+
settingValue: options.secretAccessKey,
|
|
278
|
+
settingName: "secretAccessKey",
|
|
279
|
+
environmentVariableName: "AWS_SECRET_ACCESS_KEY",
|
|
280
|
+
description: "AWS secret access key"
|
|
281
|
+
}),
|
|
282
|
+
sessionToken: (0, import_provider_utils3.loadOptionalSetting)({
|
|
283
|
+
settingValue: options.sessionToken,
|
|
284
|
+
environmentVariableName: "AWS_SESSION_TOKEN"
|
|
285
|
+
})
|
|
286
|
+
};
|
|
287
|
+
} catch (error) {
|
|
288
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
289
|
+
if (errorMessage.includes("AWS_ACCESS_KEY_ID") || errorMessage.includes("accessKeyId")) {
|
|
290
|
+
throw new Error(
|
|
291
|
+
`AWS SigV4 authentication requires AWS credentials. Please provide either:
|
|
292
|
+
1. Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables
|
|
293
|
+
2. Provide accessKeyId and secretAccessKey in options
|
|
294
|
+
3. Use a credentialProvider function
|
|
295
|
+
4. Use API key authentication with AWS_BEARER_TOKEN_BEDROCK or apiKey option
|
|
296
|
+
Original error: ${errorMessage}`
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
if (errorMessage.includes("AWS_SECRET_ACCESS_KEY") || errorMessage.includes("secretAccessKey")) {
|
|
300
|
+
throw new Error(
|
|
301
|
+
`AWS SigV4 authentication requires both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. Please ensure both credentials are provided.
|
|
302
|
+
Original error: ${errorMessage}`
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
throw error;
|
|
306
|
+
}
|
|
307
|
+
}, options.fetch);
|
|
308
|
+
const fetchFunction = createBedrockAnthropicFetch(baseFetchFunction);
|
|
309
|
+
const getBaseURL = () => {
|
|
310
|
+
var _a, _b;
|
|
311
|
+
return (_b = (0, import_provider_utils3.withoutTrailingSlash)(
|
|
312
|
+
(_a = options.baseURL) != null ? _a : `https://bedrock-runtime.${(0, import_provider_utils3.loadSetting)({
|
|
313
|
+
settingValue: options.region,
|
|
314
|
+
settingName: "region",
|
|
315
|
+
environmentVariableName: "AWS_REGION",
|
|
316
|
+
description: "AWS region"
|
|
317
|
+
})}.amazonaws.com`
|
|
318
|
+
)) != null ? _b : "https://bedrock-runtime.us-east-1.amazonaws.com";
|
|
319
|
+
};
|
|
320
|
+
const getHeaders = async () => {
|
|
321
|
+
var _a;
|
|
322
|
+
const baseHeaders = (_a = await (0, import_provider_utils3.resolve)(options.headers)) != null ? _a : {};
|
|
323
|
+
return (0, import_provider_utils3.withUserAgentSuffix)(baseHeaders, `ai-sdk/amazon-bedrock/${VERSION}`);
|
|
324
|
+
};
|
|
325
|
+
const createChatModel = (modelId) => new import_internal.AnthropicMessagesLanguageModel(modelId, {
|
|
326
|
+
provider: "bedrock.anthropic.messages",
|
|
327
|
+
baseURL: getBaseURL(),
|
|
328
|
+
headers: getHeaders,
|
|
329
|
+
fetch: fetchFunction,
|
|
330
|
+
buildRequestUrl: (baseURL, isStreaming) => `${baseURL}/model/${encodeURIComponent(modelId)}/${isStreaming ? "invoke-with-response-stream" : "invoke"}`,
|
|
331
|
+
transformRequestBody: (args) => {
|
|
332
|
+
const { model, stream, tool_choice, tools, ...rest } = args;
|
|
333
|
+
const transformedToolChoice = tool_choice != null ? {
|
|
334
|
+
type: tool_choice.type,
|
|
335
|
+
...tool_choice.name != null ? { name: tool_choice.name } : {}
|
|
336
|
+
} : void 0;
|
|
337
|
+
const requiredBetas = /* @__PURE__ */ new Set();
|
|
338
|
+
const transformedTools = tools == null ? void 0 : tools.map((tool) => {
|
|
339
|
+
const toolType = tool.type;
|
|
340
|
+
if (toolType && toolType in BEDROCK_TOOL_VERSION_MAP) {
|
|
341
|
+
const newType = BEDROCK_TOOL_VERSION_MAP[toolType];
|
|
342
|
+
if (newType in BEDROCK_TOOL_BETA_MAP) {
|
|
343
|
+
requiredBetas.add(BEDROCK_TOOL_BETA_MAP[newType]);
|
|
344
|
+
}
|
|
345
|
+
const newName = newType in BEDROCK_TOOL_NAME_MAP ? BEDROCK_TOOL_NAME_MAP[newType] : tool.name;
|
|
346
|
+
return {
|
|
347
|
+
...tool,
|
|
348
|
+
type: newType,
|
|
349
|
+
name: newName
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
if (toolType && toolType in BEDROCK_TOOL_BETA_MAP) {
|
|
353
|
+
requiredBetas.add(BEDROCK_TOOL_BETA_MAP[toolType]);
|
|
354
|
+
}
|
|
355
|
+
if (toolType && toolType in BEDROCK_TOOL_NAME_MAP) {
|
|
356
|
+
return {
|
|
357
|
+
...tool,
|
|
358
|
+
name: BEDROCK_TOOL_NAME_MAP[toolType]
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
return tool;
|
|
362
|
+
});
|
|
363
|
+
return {
|
|
364
|
+
...rest,
|
|
365
|
+
...transformedTools != null ? { tools: transformedTools } : {},
|
|
366
|
+
...transformedToolChoice != null ? { tool_choice: transformedToolChoice } : {},
|
|
367
|
+
...requiredBetas.size > 0 ? { anthropic_beta: Array.from(requiredBetas) } : {},
|
|
368
|
+
anthropic_version: "bedrock-2023-05-31"
|
|
369
|
+
};
|
|
370
|
+
},
|
|
371
|
+
// Bedrock Anthropic doesn't support URL sources, force download and base64 conversion
|
|
372
|
+
supportedUrls: () => ({}),
|
|
373
|
+
// force the use of JSON tool fallback for structured outputs since beta header isn't supported
|
|
374
|
+
supportsNativeStructuredOutput: false
|
|
375
|
+
});
|
|
376
|
+
const provider = function(modelId) {
|
|
377
|
+
if (new.target) {
|
|
378
|
+
throw new Error(
|
|
379
|
+
"The Bedrock Anthropic model function cannot be called with the new keyword."
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
return createChatModel(modelId);
|
|
383
|
+
};
|
|
384
|
+
provider.specificationVersion = "v3";
|
|
385
|
+
provider.languageModel = createChatModel;
|
|
386
|
+
provider.chat = createChatModel;
|
|
387
|
+
provider.messages = createChatModel;
|
|
388
|
+
provider.embeddingModel = (modelId) => {
|
|
389
|
+
throw new import_provider.NoSuchModelError({ modelId, modelType: "embeddingModel" });
|
|
390
|
+
};
|
|
391
|
+
provider.textEmbeddingModel = provider.embeddingModel;
|
|
392
|
+
provider.imageModel = (modelId) => {
|
|
393
|
+
throw new import_provider.NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
394
|
+
};
|
|
395
|
+
provider.tools = import_internal.anthropicTools;
|
|
396
|
+
return provider;
|
|
397
|
+
}
|
|
398
|
+
var bedrockAnthropic = createBedrockAnthropic();
|
|
399
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
400
|
+
0 && (module.exports = {
|
|
401
|
+
bedrockAnthropic,
|
|
402
|
+
createBedrockAnthropic
|
|
403
|
+
});
|
|
404
|
+
//# sourceMappingURL=index.js.map
|