@matthewdunbar/amazon-bedrock-mantle 0.0.1
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/README.md +52 -0
- package/dist/index.d.mts +89 -0
- package/dist/index.d.ts +89 -0
- package/dist/index.js +142 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +119 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +69 -0
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# AI SDK - Amazon Bedrock Mantle Provider
|
|
2
|
+
|
|
3
|
+
The **Amazon Bedrock Mantle** provider for the [AI SDK](https://ai-sdk.dev) uses AWS Bedrock's OpenAI-compatible endpoint (bedrock-mantle) to provide language model capabilities.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
The Bedrock Mantle provider is available in the `@ai-sdk/amazon-bedrock-mantle` module. You can install it with:
|
|
8
|
+
|
|
9
|
+
````bash
|
|
10
|
+
npm install @ai-sdk/amazon-bedrock-mantle
|
|
11
|
+
```n
|
|
12
|
+
## Provider Configuration
|
|
13
|
+
|
|
14
|
+
You can create a Bedrock Mantle provider instance using `createAmazonBedrockMantle`:
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { createAmazonBedrockMantle } from '@ai-sdk/amazon-bedrock-mantle';
|
|
18
|
+
|
|
19
|
+
const bedrockMantle = createAmazonBedrockMantle({
|
|
20
|
+
region: 'us-east-1',
|
|
21
|
+
// Authentication via API key (bearer token)
|
|
22
|
+
apiKey: 'your-api-key',
|
|
23
|
+
// OR authentication via AWS credentials
|
|
24
|
+
// accessKeyId: 'your-access-key',
|
|
25
|
+
// secretAccessKey: 'your-secret-key',
|
|
26
|
+
});
|
|
27
|
+
````
|
|
28
|
+
|
|
29
|
+
### Authentication
|
|
30
|
+
|
|
31
|
+
The provider supports two authentication methods:
|
|
32
|
+
|
|
33
|
+
1. **Bearer Token** (API Key): Set `apiKey` or `AWS_BEARER_TOKEN_BEDROCK` environment variable
|
|
34
|
+
2. **AWS SigV4**: Set `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables
|
|
35
|
+
|
|
36
|
+
Bearer token authentication takes precedence if both are configured.
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { generateText } from 'ai';
|
|
42
|
+
import { bedrockMantle } from '@ai-sdk/amazon-bedrock-mantle';
|
|
43
|
+
|
|
44
|
+
const { text } = await generateText({
|
|
45
|
+
model: bedrockMantle('moonshotai.kimi-k2.5'),
|
|
46
|
+
prompt: 'Hello, how are you?',
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Documentation
|
|
51
|
+
|
|
52
|
+
For full documentation, visit [ai-sdk.dev/docs](https://ai-sdk.dev/docs).
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { ProviderV3, LanguageModelV3, EmbeddingModelV3, ImageModelV3 } from '@ai-sdk/provider';
|
|
2
|
+
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
3
|
+
|
|
4
|
+
interface AmazonBedrockMantleProviderSettings {
|
|
5
|
+
/**
|
|
6
|
+
* The AWS region to use for the Bedrock Mantle provider. Defaults to the value of the
|
|
7
|
+
* `AWS_REGION` environment variable.
|
|
8
|
+
*/
|
|
9
|
+
region?: string;
|
|
10
|
+
/**
|
|
11
|
+
* API key for authenticating requests using Bearer token authentication.
|
|
12
|
+
* When provided, this will be used instead of AWS SigV4 authentication.
|
|
13
|
+
* Defaults to the value of the `AWS_BEARER_TOKEN_BEDROCK` environment variable.
|
|
14
|
+
*
|
|
15
|
+
* Note: When `apiKey` is provided, it takes precedence over AWS SigV4 authentication.
|
|
16
|
+
* If neither `apiKey` nor `AWS_BEARER_TOKEN_BEDROCK` environment variable is set,
|
|
17
|
+
* the provider will fall back to AWS SigV4 authentication using AWS credentials.
|
|
18
|
+
*/
|
|
19
|
+
apiKey?: string;
|
|
20
|
+
/**
|
|
21
|
+
* The AWS access key ID to use for the Bedrock Mantle provider. Defaults to the value of the
|
|
22
|
+
* `AWS_ACCESS_KEY_ID` environment variable.
|
|
23
|
+
*/
|
|
24
|
+
accessKeyId?: string;
|
|
25
|
+
/**
|
|
26
|
+
* The AWS secret access key to use for the Bedrock Mantle provider. Defaults to the value of the
|
|
27
|
+
* `AWS_SECRET_ACCESS_KEY` environment variable.
|
|
28
|
+
*/
|
|
29
|
+
secretAccessKey?: string;
|
|
30
|
+
/**
|
|
31
|
+
* The AWS session token to use for the Bedrock Mantle provider. Defaults to the value of the
|
|
32
|
+
* `AWS_SESSION_TOKEN` environment variable.
|
|
33
|
+
*/
|
|
34
|
+
sessionToken?: string;
|
|
35
|
+
/**
|
|
36
|
+
* The AWS profile to use for loading credentials from the AWS CLI configuration.
|
|
37
|
+
* When specified, credentials will be loaded from ~/.aws/credentials for the given profile.
|
|
38
|
+
* This takes precedence over static credentials but is overridden by `apiKey`.
|
|
39
|
+
*/
|
|
40
|
+
profile?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Base URL for the Bedrock Mantle API calls. Defaults to the bedrock-mantle endpoint.
|
|
43
|
+
*/
|
|
44
|
+
baseURL?: string;
|
|
45
|
+
/**
|
|
46
|
+
* Custom headers to include in the requests.
|
|
47
|
+
*/
|
|
48
|
+
headers?: Record<string, string>;
|
|
49
|
+
/**
|
|
50
|
+
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
51
|
+
* or to provide a custom fetch implementation for e.g. testing.
|
|
52
|
+
*/
|
|
53
|
+
fetch?: FetchFunction;
|
|
54
|
+
/**
|
|
55
|
+
* The AWS credential provider to use for the Bedrock Mantle provider to get dynamic
|
|
56
|
+
* credentials similar to the AWS SDK. Setting a provider here will cause its
|
|
57
|
+
* credential values to be used instead of the `accessKeyId`, `secretAccessKey`,
|
|
58
|
+
* and `sessionToken` settings.
|
|
59
|
+
*/
|
|
60
|
+
credentialProvider?: () => PromiseLike<{
|
|
61
|
+
accessKeyId: string;
|
|
62
|
+
secretAccessKey: string;
|
|
63
|
+
sessionToken?: string;
|
|
64
|
+
}>;
|
|
65
|
+
}
|
|
66
|
+
type BedrockMantleChatModelId = string;
|
|
67
|
+
type BedrockMantleEmbeddingModelId = string;
|
|
68
|
+
type BedrockMantleImageModelId = string;
|
|
69
|
+
interface AmazonBedrockMantleProvider extends ProviderV3 {
|
|
70
|
+
(modelId: BedrockMantleChatModelId): LanguageModelV3;
|
|
71
|
+
languageModel(modelId: BedrockMantleChatModelId): LanguageModelV3;
|
|
72
|
+
chatModel(modelId: BedrockMantleChatModelId): LanguageModelV3;
|
|
73
|
+
embeddingModel(modelId: BedrockMantleEmbeddingModelId): EmbeddingModelV3;
|
|
74
|
+
imageModel(modelId: BedrockMantleImageModelId): ImageModelV3;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Create an Amazon Bedrock Mantle provider instance.
|
|
78
|
+
* Bedrock Mantle is AWS Bedrock's OpenAI-compatible endpoint that routes all requests
|
|
79
|
+
* through bedrock-mantle.<region>.api.aws/v1 using OpenAI API format.
|
|
80
|
+
*/
|
|
81
|
+
declare function createAmazonBedrockMantle(options?: AmazonBedrockMantleProviderSettings): AmazonBedrockMantleProvider;
|
|
82
|
+
/**
|
|
83
|
+
* Default Bedrock Mantle provider instance.
|
|
84
|
+
*/
|
|
85
|
+
declare const bedrockMantle: AmazonBedrockMantleProvider;
|
|
86
|
+
|
|
87
|
+
declare const VERSION: string;
|
|
88
|
+
|
|
89
|
+
export { type AmazonBedrockMantleProvider, type AmazonBedrockMantleProviderSettings, type BedrockMantleChatModelId, VERSION, bedrockMantle, createAmazonBedrockMantle };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { ProviderV3, LanguageModelV3, EmbeddingModelV3, ImageModelV3 } from '@ai-sdk/provider';
|
|
2
|
+
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
3
|
+
|
|
4
|
+
interface AmazonBedrockMantleProviderSettings {
|
|
5
|
+
/**
|
|
6
|
+
* The AWS region to use for the Bedrock Mantle provider. Defaults to the value of the
|
|
7
|
+
* `AWS_REGION` environment variable.
|
|
8
|
+
*/
|
|
9
|
+
region?: string;
|
|
10
|
+
/**
|
|
11
|
+
* API key for authenticating requests using Bearer token authentication.
|
|
12
|
+
* When provided, this will be used instead of AWS SigV4 authentication.
|
|
13
|
+
* Defaults to the value of the `AWS_BEARER_TOKEN_BEDROCK` environment variable.
|
|
14
|
+
*
|
|
15
|
+
* Note: When `apiKey` is provided, it takes precedence over AWS SigV4 authentication.
|
|
16
|
+
* If neither `apiKey` nor `AWS_BEARER_TOKEN_BEDROCK` environment variable is set,
|
|
17
|
+
* the provider will fall back to AWS SigV4 authentication using AWS credentials.
|
|
18
|
+
*/
|
|
19
|
+
apiKey?: string;
|
|
20
|
+
/**
|
|
21
|
+
* The AWS access key ID to use for the Bedrock Mantle provider. Defaults to the value of the
|
|
22
|
+
* `AWS_ACCESS_KEY_ID` environment variable.
|
|
23
|
+
*/
|
|
24
|
+
accessKeyId?: string;
|
|
25
|
+
/**
|
|
26
|
+
* The AWS secret access key to use for the Bedrock Mantle provider. Defaults to the value of the
|
|
27
|
+
* `AWS_SECRET_ACCESS_KEY` environment variable.
|
|
28
|
+
*/
|
|
29
|
+
secretAccessKey?: string;
|
|
30
|
+
/**
|
|
31
|
+
* The AWS session token to use for the Bedrock Mantle provider. Defaults to the value of the
|
|
32
|
+
* `AWS_SESSION_TOKEN` environment variable.
|
|
33
|
+
*/
|
|
34
|
+
sessionToken?: string;
|
|
35
|
+
/**
|
|
36
|
+
* The AWS profile to use for loading credentials from the AWS CLI configuration.
|
|
37
|
+
* When specified, credentials will be loaded from ~/.aws/credentials for the given profile.
|
|
38
|
+
* This takes precedence over static credentials but is overridden by `apiKey`.
|
|
39
|
+
*/
|
|
40
|
+
profile?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Base URL for the Bedrock Mantle API calls. Defaults to the bedrock-mantle endpoint.
|
|
43
|
+
*/
|
|
44
|
+
baseURL?: string;
|
|
45
|
+
/**
|
|
46
|
+
* Custom headers to include in the requests.
|
|
47
|
+
*/
|
|
48
|
+
headers?: Record<string, string>;
|
|
49
|
+
/**
|
|
50
|
+
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
51
|
+
* or to provide a custom fetch implementation for e.g. testing.
|
|
52
|
+
*/
|
|
53
|
+
fetch?: FetchFunction;
|
|
54
|
+
/**
|
|
55
|
+
* The AWS credential provider to use for the Bedrock Mantle provider to get dynamic
|
|
56
|
+
* credentials similar to the AWS SDK. Setting a provider here will cause its
|
|
57
|
+
* credential values to be used instead of the `accessKeyId`, `secretAccessKey`,
|
|
58
|
+
* and `sessionToken` settings.
|
|
59
|
+
*/
|
|
60
|
+
credentialProvider?: () => PromiseLike<{
|
|
61
|
+
accessKeyId: string;
|
|
62
|
+
secretAccessKey: string;
|
|
63
|
+
sessionToken?: string;
|
|
64
|
+
}>;
|
|
65
|
+
}
|
|
66
|
+
type BedrockMantleChatModelId = string;
|
|
67
|
+
type BedrockMantleEmbeddingModelId = string;
|
|
68
|
+
type BedrockMantleImageModelId = string;
|
|
69
|
+
interface AmazonBedrockMantleProvider extends ProviderV3 {
|
|
70
|
+
(modelId: BedrockMantleChatModelId): LanguageModelV3;
|
|
71
|
+
languageModel(modelId: BedrockMantleChatModelId): LanguageModelV3;
|
|
72
|
+
chatModel(modelId: BedrockMantleChatModelId): LanguageModelV3;
|
|
73
|
+
embeddingModel(modelId: BedrockMantleEmbeddingModelId): EmbeddingModelV3;
|
|
74
|
+
imageModel(modelId: BedrockMantleImageModelId): ImageModelV3;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Create an Amazon Bedrock Mantle provider instance.
|
|
78
|
+
* Bedrock Mantle is AWS Bedrock's OpenAI-compatible endpoint that routes all requests
|
|
79
|
+
* through bedrock-mantle.<region>.api.aws/v1 using OpenAI API format.
|
|
80
|
+
*/
|
|
81
|
+
declare function createAmazonBedrockMantle(options?: AmazonBedrockMantleProviderSettings): AmazonBedrockMantleProvider;
|
|
82
|
+
/**
|
|
83
|
+
* Default Bedrock Mantle provider instance.
|
|
84
|
+
*/
|
|
85
|
+
declare const bedrockMantle: AmazonBedrockMantleProvider;
|
|
86
|
+
|
|
87
|
+
declare const VERSION: string;
|
|
88
|
+
|
|
89
|
+
export { type AmazonBedrockMantleProvider, type AmazonBedrockMantleProviderSettings, type BedrockMantleChatModelId, VERSION, bedrockMantle, createAmazonBedrockMantle };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
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/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
VERSION: () => VERSION,
|
|
24
|
+
bedrockMantle: () => bedrockMantle,
|
|
25
|
+
createAmazonBedrockMantle: () => createAmazonBedrockMantle
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(src_exports);
|
|
28
|
+
|
|
29
|
+
// src/bedrock-mantle-provider.ts
|
|
30
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
31
|
+
var import_openai_compatible = require("@ai-sdk/openai-compatible");
|
|
32
|
+
var import_aws4fetch = require("aws4fetch");
|
|
33
|
+
function createMantleSigV4FetchFunction(getCredentials, fetchImpl = globalThis.fetch) {
|
|
34
|
+
return async (input, init) => {
|
|
35
|
+
var _a, _b;
|
|
36
|
+
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
|
|
37
|
+
const body = typeof (init == null ? void 0 : init.body) === "string" ? init.body : (init == null ? void 0 : init.body) ? JSON.stringify(init.body) : void 0;
|
|
38
|
+
const headers = (_a = init == null ? void 0 : init.headers) != null ? _a : {};
|
|
39
|
+
const headerEntries = headers instanceof Headers ? Array.from(headers.entries()) : Array.isArray(headers) ? headers : Object.entries(headers);
|
|
40
|
+
const credentials = await getCredentials();
|
|
41
|
+
const signer = new import_aws4fetch.AwsV4Signer({
|
|
42
|
+
url,
|
|
43
|
+
method: (_b = init == null ? void 0 : init.method) != null ? _b : "POST",
|
|
44
|
+
headers: headerEntries,
|
|
45
|
+
body,
|
|
46
|
+
region: credentials.region,
|
|
47
|
+
accessKeyId: credentials.accessKeyId,
|
|
48
|
+
secretAccessKey: credentials.secretAccessKey,
|
|
49
|
+
sessionToken: credentials.sessionToken,
|
|
50
|
+
service: "bedrock-mantle"
|
|
51
|
+
});
|
|
52
|
+
const signed = await signer.sign();
|
|
53
|
+
return fetchImpl(input, {
|
|
54
|
+
...init,
|
|
55
|
+
body,
|
|
56
|
+
headers: Object.fromEntries(signed.headers.entries())
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function createAmazonBedrockMantle(options = {}) {
|
|
61
|
+
var _a, _b, _c;
|
|
62
|
+
const rawApiKey = (0, import_provider_utils.loadOptionalSetting)({
|
|
63
|
+
settingValue: options.apiKey,
|
|
64
|
+
environmentVariableName: "AWS_BEARER_TOKEN_BEDROCK"
|
|
65
|
+
});
|
|
66
|
+
const apiKey = rawApiKey && rawApiKey.trim().length > 0 ? rawApiKey.trim() : void 0;
|
|
67
|
+
const region = (0, import_provider_utils.loadSetting)({
|
|
68
|
+
settingValue: options.region,
|
|
69
|
+
settingName: "region",
|
|
70
|
+
environmentVariableName: "AWS_REGION",
|
|
71
|
+
description: "AWS region"
|
|
72
|
+
});
|
|
73
|
+
let fetchFunction;
|
|
74
|
+
if (apiKey) {
|
|
75
|
+
fetchFunction = (_a = options.fetch) != null ? _a : globalThis.fetch;
|
|
76
|
+
} else if (options.credentialProvider) {
|
|
77
|
+
fetchFunction = createMantleSigV4FetchFunction(async () => {
|
|
78
|
+
return {
|
|
79
|
+
...await options.credentialProvider(),
|
|
80
|
+
region
|
|
81
|
+
};
|
|
82
|
+
}, options.fetch);
|
|
83
|
+
} else {
|
|
84
|
+
fetchFunction = createMantleSigV4FetchFunction(async () => {
|
|
85
|
+
return {
|
|
86
|
+
region,
|
|
87
|
+
accessKeyId: (0, import_provider_utils.loadSetting)({
|
|
88
|
+
settingValue: options.accessKeyId,
|
|
89
|
+
settingName: "accessKeyId",
|
|
90
|
+
environmentVariableName: "AWS_ACCESS_KEY_ID",
|
|
91
|
+
description: "AWS access key ID"
|
|
92
|
+
}),
|
|
93
|
+
secretAccessKey: (0, import_provider_utils.loadSetting)({
|
|
94
|
+
settingValue: options.secretAccessKey,
|
|
95
|
+
settingName: "secretAccessKey",
|
|
96
|
+
environmentVariableName: "AWS_SECRET_ACCESS_KEY",
|
|
97
|
+
description: "AWS secret access key"
|
|
98
|
+
}),
|
|
99
|
+
sessionToken: (0, import_provider_utils.loadOptionalSetting)({
|
|
100
|
+
settingValue: options.sessionToken,
|
|
101
|
+
environmentVariableName: "AWS_SESSION_TOKEN"
|
|
102
|
+
})
|
|
103
|
+
};
|
|
104
|
+
}, options.fetch);
|
|
105
|
+
}
|
|
106
|
+
const baseURL = (_c = (0, import_provider_utils.withoutTrailingSlash)(
|
|
107
|
+
(_b = options.baseURL) != null ? _b : `https://bedrock-mantle.${region}.api.aws/v1`
|
|
108
|
+
)) != null ? _c : `https://bedrock-mantle.us-east-1.api.aws/v1`;
|
|
109
|
+
const mantleProvider = (0, import_openai_compatible.createOpenAICompatible)({
|
|
110
|
+
baseURL,
|
|
111
|
+
name: "amazon-bedrock-mantle",
|
|
112
|
+
apiKey: apiKey != null ? apiKey : "dummy-key-for-sigv4",
|
|
113
|
+
fetch: fetchFunction,
|
|
114
|
+
headers: options.headers,
|
|
115
|
+
includeUsage: true
|
|
116
|
+
});
|
|
117
|
+
const provider = function(modelId) {
|
|
118
|
+
if (new.target) {
|
|
119
|
+
throw new Error(
|
|
120
|
+
"The Amazon Bedrock Mantle model function cannot be called with the new keyword."
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
return mantleProvider.languageModel(modelId);
|
|
124
|
+
};
|
|
125
|
+
provider.specificationVersion = "v3";
|
|
126
|
+
provider.languageModel = mantleProvider.languageModel;
|
|
127
|
+
provider.chatModel = mantleProvider.languageModel;
|
|
128
|
+
provider.embeddingModel = mantleProvider.embeddingModel;
|
|
129
|
+
provider.imageModel = mantleProvider.imageModel;
|
|
130
|
+
return provider;
|
|
131
|
+
}
|
|
132
|
+
var bedrockMantle = createAmazonBedrockMantle();
|
|
133
|
+
|
|
134
|
+
// src/version.ts
|
|
135
|
+
var VERSION = true ? "0.0.1" : "0.0.0-test";
|
|
136
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
137
|
+
0 && (module.exports = {
|
|
138
|
+
VERSION,
|
|
139
|
+
bedrockMantle,
|
|
140
|
+
createAmazonBedrockMantle
|
|
141
|
+
});
|
|
142
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/bedrock-mantle-provider.ts","../src/version.ts"],"sourcesContent":["export {\n bedrockMantle,\n createAmazonBedrockMantle,\n} from './bedrock-mantle-provider';\nexport type {\n AmazonBedrockMantleProvider,\n AmazonBedrockMantleProviderSettings,\n BedrockMantleChatModelId,\n} from './bedrock-mantle-provider';\nexport { VERSION } from './version';\n","import {\n EmbeddingModelV3,\n ImageModelV3,\n LanguageModelV3,\n ProviderV3,\n} from '@ai-sdk/provider';\nimport {\n FetchFunction,\n loadOptionalSetting,\n loadSetting,\n withoutTrailingSlash,\n} from '@ai-sdk/provider-utils';\nimport {\n createOpenAICompatible,\n OpenAICompatibleProvider,\n} from '@ai-sdk/openai-compatible';\nimport { AwsV4Signer } from 'aws4fetch';\n\nexport interface AmazonBedrockMantleProviderSettings {\n /**\n * The AWS region to use for the Bedrock Mantle provider. Defaults to the value of the\n * `AWS_REGION` environment variable.\n */\n region?: string;\n\n /**\n * API key for authenticating requests using Bearer token authentication.\n * When provided, this will be used instead of AWS SigV4 authentication.\n * Defaults to the value of the `AWS_BEARER_TOKEN_BEDROCK` environment variable.\n *\n * Note: When `apiKey` is provided, it takes precedence over AWS SigV4 authentication.\n * If neither `apiKey` nor `AWS_BEARER_TOKEN_BEDROCK` environment variable is set,\n * the provider will fall back to AWS SigV4 authentication using AWS credentials.\n */\n apiKey?: string;\n\n /**\n * The AWS access key ID to use for the Bedrock Mantle provider. Defaults to the value of the\n * `AWS_ACCESS_KEY_ID` environment variable.\n */\n accessKeyId?: string;\n\n /**\n * The AWS secret access key to use for the Bedrock Mantle provider. Defaults to the value of the\n * `AWS_SECRET_ACCESS_KEY` environment variable.\n */\n secretAccessKey?: string;\n\n /**\n * The AWS session token to use for the Bedrock Mantle provider. Defaults to the value of the\n * `AWS_SESSION_TOKEN` environment variable.\n */\n sessionToken?: string;\n\n /**\n * The AWS profile to use for loading credentials from the AWS CLI configuration.\n * When specified, credentials will be loaded from ~/.aws/credentials for the given profile.\n * This takes precedence over static credentials but is overridden by `apiKey`.\n */\n profile?: string;\n\n /**\n * Base URL for the Bedrock Mantle API calls. Defaults to the bedrock-mantle endpoint.\n */\n baseURL?: string;\n\n /**\n * Custom headers to include in the requests.\n */\n headers?: Record<string, string>;\n\n /**\n * Custom fetch implementation. You can use it as a middleware to intercept requests,\n * or to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n\n /**\n * The AWS credential provider to use for the Bedrock Mantle provider to get dynamic\n * credentials similar to the AWS SDK. Setting a provider here will cause its\n * credential values to be used instead of the `accessKeyId`, `secretAccessKey`,\n * and `sessionToken` settings.\n */\n credentialProvider?: () => PromiseLike<{\n accessKeyId: string;\n secretAccessKey: string;\n sessionToken?: string;\n }>;\n}\n\nexport type BedrockMantleChatModelId = string;\nexport type BedrockMantleEmbeddingModelId = string;\nexport type BedrockMantleImageModelId = string;\n\nexport interface AmazonBedrockMantleProvider extends ProviderV3 {\n (modelId: BedrockMantleChatModelId): LanguageModelV3;\n\n languageModel(modelId: BedrockMantleChatModelId): LanguageModelV3;\n\n chatModel(modelId: BedrockMantleChatModelId): LanguageModelV3;\n\n embeddingModel(modelId: BedrockMantleEmbeddingModelId): EmbeddingModelV3;\n\n imageModel(modelId: BedrockMantleImageModelId): ImageModelV3;\n}\n\ninterface BedrockCredentials {\n region: string;\n accessKeyId: string;\n secretAccessKey: string;\n sessionToken?: string;\n}\n\n/**\n * Creates a fetch function that applies AWS Signature Version 4 signing for bedrock-mantle service.\n *\n * @param getCredentials - Function that returns the AWS credentials to use when signing.\n * @param fetchImpl - Optional original fetch implementation to wrap. Defaults to global fetch.\n * @returns A FetchFunction that signs requests before passing them to the underlying fetch.\n */\nfunction createMantleSigV4FetchFunction(\n getCredentials: () => BedrockCredentials | PromiseLike<BedrockCredentials>,\n fetchImpl: FetchFunction = globalThis.fetch,\n): FetchFunction {\n return async (\n input: RequestInfo | URL,\n init?: RequestInit,\n ): Promise<Response> => {\n const url =\n typeof input === 'string'\n ? input\n : input instanceof URL\n ? input.href\n : input.url;\n\n const body =\n typeof init?.body === 'string'\n ? init.body\n : init?.body\n ? JSON.stringify(init.body)\n : undefined;\n\n const headers = init?.headers ?? {};\n const headerEntries =\n headers instanceof Headers\n ? Array.from(headers.entries())\n : Array.isArray(headers)\n ? headers\n : Object.entries(headers);\n\n const credentials = await getCredentials();\n const signer = new AwsV4Signer({\n url,\n method: init?.method ?? 'POST',\n headers: headerEntries,\n body,\n region: credentials.region,\n accessKeyId: credentials.accessKeyId,\n secretAccessKey: credentials.secretAccessKey,\n sessionToken: credentials.sessionToken,\n service: 'bedrock-mantle',\n });\n\n const signed = await signer.sign();\n return fetchImpl(input, {\n ...init,\n body,\n headers: Object.fromEntries(signed.headers.entries()),\n });\n };\n}\n\n/**\n * Create an Amazon Bedrock Mantle provider instance.\n * Bedrock Mantle is AWS Bedrock's OpenAI-compatible endpoint that routes all requests\n * through bedrock-mantle.<region>.api.aws/v1 using OpenAI API format.\n */\nexport function createAmazonBedrockMantle(\n options: AmazonBedrockMantleProviderSettings = {},\n): AmazonBedrockMantleProvider {\n // Check for API key authentication first\n const rawApiKey = loadOptionalSetting({\n settingValue: options.apiKey,\n environmentVariableName: 'AWS_BEARER_TOKEN_BEDROCK',\n });\n\n // Only use API key if it's a non-empty, non-whitespace string\n const apiKey =\n rawApiKey && rawApiKey.trim().length > 0 ? rawApiKey.trim() : undefined;\n\n const region = loadSetting({\n settingValue: options.region,\n settingName: 'region',\n environmentVariableName: 'AWS_REGION',\n description: 'AWS region',\n });\n\n // Use API key authentication if available, otherwise fall back to SigV4\n let fetchFunction: FetchFunction;\n if (apiKey) {\n fetchFunction = options.fetch ?? globalThis.fetch;\n } else if (options.credentialProvider) {\n fetchFunction = createMantleSigV4FetchFunction(async () => {\n return {\n ...(await options.credentialProvider!()),\n region,\n };\n }, options.fetch);\n } else {\n fetchFunction = createMantleSigV4FetchFunction(async () => {\n return {\n region,\n accessKeyId: loadSetting({\n settingValue: options.accessKeyId,\n settingName: 'accessKeyId',\n environmentVariableName: 'AWS_ACCESS_KEY_ID',\n description: 'AWS access key ID',\n }),\n secretAccessKey: loadSetting({\n settingValue: options.secretAccessKey,\n settingName: 'secretAccessKey',\n environmentVariableName: 'AWS_SECRET_ACCESS_KEY',\n description: 'AWS secret access key',\n }),\n sessionToken: loadOptionalSetting({\n settingValue: options.sessionToken,\n environmentVariableName: 'AWS_SESSION_TOKEN',\n }),\n };\n }, options.fetch);\n }\n\n const baseURL =\n withoutTrailingSlash(\n options.baseURL ?? `https://bedrock-mantle.${region}.api.aws/v1`,\n ) ?? `https://bedrock-mantle.us-east-1.api.aws/v1`;\n\n // Create the OpenAI-compatible provider\n const mantleProvider: OpenAICompatibleProvider<\n string,\n string,\n string,\n string\n > = createOpenAICompatible({\n baseURL,\n name: 'amazon-bedrock-mantle',\n apiKey: apiKey ?? 'dummy-key-for-sigv4',\n fetch: fetchFunction,\n headers: options.headers,\n includeUsage: true,\n });\n\n const provider = function (modelId: BedrockMantleChatModelId) {\n if (new.target) {\n throw new Error(\n 'The Amazon Bedrock Mantle model function cannot be called with the new keyword.',\n );\n }\n return mantleProvider.languageModel(modelId);\n };\n\n provider.specificationVersion = 'v3' as const;\n provider.languageModel = mantleProvider.languageModel;\n provider.chatModel = mantleProvider.languageModel;\n provider.embeddingModel = mantleProvider.embeddingModel;\n provider.imageModel = mantleProvider.imageModel;\n\n return provider as AmazonBedrockMantleProvider;\n}\n\n/**\n * Default Bedrock Mantle provider instance.\n */\nexport const bedrockMantle = createAmazonBedrockMantle();\n","// Version string of this package injected at build time.\ndeclare const __PACKAGE_VERSION__: string | undefined;\nexport const VERSION: string =\n typeof __PACKAGE_VERSION__ !== 'undefined'\n ? __PACKAGE_VERSION__\n : '0.0.0-test';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMA,4BAKO;AACP,+BAGO;AACP,uBAA4B;AAwG5B,SAAS,+BACP,gBACA,YAA2B,WAAW,OACvB;AACf,SAAO,OACL,OACA,SACsB;AA/H1B;AAgII,UAAM,MACJ,OAAO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antle-provider.ts
|
|
2
|
+
import {
|
|
3
|
+
loadOptionalSetting,
|
|
4
|
+
loadSetting,
|
|
5
|
+
withoutTrailingSlash
|
|
6
|
+
} from "@ai-sdk/provider-utils";
|
|
7
|
+
import {
|
|
8
|
+
createOpenAICompatible
|
|
9
|
+
} from "@ai-sdk/openai-compatible";
|
|
10
|
+
import { AwsV4Signer } from "aws4fetch";
|
|
11
|
+
function createMantleSigV4FetchFunction(getCredentials, fetchImpl = globalThis.fetch) {
|
|
12
|
+
return async (input, init) => {
|
|
13
|
+
var _a, _b;
|
|
14
|
+
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
|
|
15
|
+
const body = typeof (init == null ? void 0 : init.body) === "string" ? init.body : (init == null ? void 0 : init.body) ? JSON.stringify(init.body) : void 0;
|
|
16
|
+
const headers = (_a = init == null ? void 0 : init.headers) != null ? _a : {};
|
|
17
|
+
const headerEntries = headers instanceof Headers ? Array.from(headers.entries()) : Array.isArray(headers) ? headers : Object.entries(headers);
|
|
18
|
+
const credentials = await getCredentials();
|
|
19
|
+
const signer = new AwsV4Signer({
|
|
20
|
+
url,
|
|
21
|
+
method: (_b = init == null ? void 0 : init.method) != null ? _b : "POST",
|
|
22
|
+
headers: headerEntries,
|
|
23
|
+
body,
|
|
24
|
+
region: credentials.region,
|
|
25
|
+
accessKeyId: credentials.accessKeyId,
|
|
26
|
+
secretAccessKey: credentials.secretAccessKey,
|
|
27
|
+
sessionToken: credentials.sessionToken,
|
|
28
|
+
service: "bedrock-mantle"
|
|
29
|
+
});
|
|
30
|
+
const signed = await signer.sign();
|
|
31
|
+
return fetchImpl(input, {
|
|
32
|
+
...init,
|
|
33
|
+
body,
|
|
34
|
+
headers: Object.fromEntries(signed.headers.entries())
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function createAmazonBedrockMantle(options = {}) {
|
|
39
|
+
var _a, _b, _c;
|
|
40
|
+
const rawApiKey = loadOptionalSetting({
|
|
41
|
+
settingValue: options.apiKey,
|
|
42
|
+
environmentVariableName: "AWS_BEARER_TOKEN_BEDROCK"
|
|
43
|
+
});
|
|
44
|
+
const apiKey = rawApiKey && rawApiKey.trim().length > 0 ? rawApiKey.trim() : void 0;
|
|
45
|
+
const region = loadSetting({
|
|
46
|
+
settingValue: options.region,
|
|
47
|
+
settingName: "region",
|
|
48
|
+
environmentVariableName: "AWS_REGION",
|
|
49
|
+
description: "AWS region"
|
|
50
|
+
});
|
|
51
|
+
let fetchFunction;
|
|
52
|
+
if (apiKey) {
|
|
53
|
+
fetchFunction = (_a = options.fetch) != null ? _a : globalThis.fetch;
|
|
54
|
+
} else if (options.credentialProvider) {
|
|
55
|
+
fetchFunction = createMantleSigV4FetchFunction(async () => {
|
|
56
|
+
return {
|
|
57
|
+
...await options.credentialProvider(),
|
|
58
|
+
region
|
|
59
|
+
};
|
|
60
|
+
}, options.fetch);
|
|
61
|
+
} else {
|
|
62
|
+
fetchFunction = createMantleSigV4FetchFunction(async () => {
|
|
63
|
+
return {
|
|
64
|
+
region,
|
|
65
|
+
accessKeyId: loadSetting({
|
|
66
|
+
settingValue: options.accessKeyId,
|
|
67
|
+
settingName: "accessKeyId",
|
|
68
|
+
environmentVariableName: "AWS_ACCESS_KEY_ID",
|
|
69
|
+
description: "AWS access key ID"
|
|
70
|
+
}),
|
|
71
|
+
secretAccessKey: loadSetting({
|
|
72
|
+
settingValue: options.secretAccessKey,
|
|
73
|
+
settingName: "secretAccessKey",
|
|
74
|
+
environmentVariableName: "AWS_SECRET_ACCESS_KEY",
|
|
75
|
+
description: "AWS secret access key"
|
|
76
|
+
}),
|
|
77
|
+
sessionToken: loadOptionalSetting({
|
|
78
|
+
settingValue: options.sessionToken,
|
|
79
|
+
environmentVariableName: "AWS_SESSION_TOKEN"
|
|
80
|
+
})
|
|
81
|
+
};
|
|
82
|
+
}, options.fetch);
|
|
83
|
+
}
|
|
84
|
+
const baseURL = (_c = withoutTrailingSlash(
|
|
85
|
+
(_b = options.baseURL) != null ? _b : `https://bedrock-mantle.${region}.api.aws/v1`
|
|
86
|
+
)) != null ? _c : `https://bedrock-mantle.us-east-1.api.aws/v1`;
|
|
87
|
+
const mantleProvider = createOpenAICompatible({
|
|
88
|
+
baseURL,
|
|
89
|
+
name: "amazon-bedrock-mantle",
|
|
90
|
+
apiKey: apiKey != null ? apiKey : "dummy-key-for-sigv4",
|
|
91
|
+
fetch: fetchFunction,
|
|
92
|
+
headers: options.headers,
|
|
93
|
+
includeUsage: true
|
|
94
|
+
});
|
|
95
|
+
const provider = function(modelId) {
|
|
96
|
+
if (new.target) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
"The Amazon Bedrock Mantle model function cannot be called with the new keyword."
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
return mantleProvider.languageModel(modelId);
|
|
102
|
+
};
|
|
103
|
+
provider.specificationVersion = "v3";
|
|
104
|
+
provider.languageModel = mantleProvider.languageModel;
|
|
105
|
+
provider.chatModel = mantleProvider.languageModel;
|
|
106
|
+
provider.embeddingModel = mantleProvider.embeddingModel;
|
|
107
|
+
provider.imageModel = mantleProvider.imageModel;
|
|
108
|
+
return provider;
|
|
109
|
+
}
|
|
110
|
+
var bedrockMantle = createAmazonBedrockMantle();
|
|
111
|
+
|
|
112
|
+
// src/version.ts
|
|
113
|
+
var VERSION = true ? "0.0.1" : "0.0.0-test";
|
|
114
|
+
export {
|
|
115
|
+
VERSION,
|
|
116
|
+
bedrockMantle,
|
|
117
|
+
createAmazonBedrockMantle
|
|
118
|
+
};
|
|
119
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/bedrock-mantle-provider.ts","../src/version.ts"],"sourcesContent":["import {\n EmbeddingModelV3,\n ImageModelV3,\n LanguageModelV3,\n ProviderV3,\n} from '@ai-sdk/provider';\nimport {\n FetchFunction,\n loadOptionalSetting,\n loadSetting,\n withoutTrailingSlash,\n} from '@ai-sdk/provider-utils';\nimport {\n createOpenAICompatible,\n OpenAICompatibleProvider,\n} from '@ai-sdk/openai-compatible';\nimport { AwsV4Signer } from 'aws4fetch';\n\nexport interface AmazonBedrockMantleProviderSettings {\n /**\n * The AWS region to use for the Bedrock Mantle provider. Defaults to the value of the\n * `AWS_REGION` environment variable.\n */\n region?: string;\n\n /**\n * API key for authenticating requests using Bearer token authentication.\n * When provided, this will be used instead of AWS SigV4 authentication.\n * Defaults to the value of the `AWS_BEARER_TOKEN_BEDROCK` environment variable.\n *\n * Note: When `apiKey` is provided, it takes precedence over AWS SigV4 authentication.\n * If neither `apiKey` nor `AWS_BEARER_TOKEN_BEDROCK` environment variable is set,\n * the provider will fall back to AWS SigV4 authentication using AWS credentials.\n */\n apiKey?: string;\n\n /**\n * The AWS access key ID to use for the Bedrock Mantle provider. Defaults to the value of the\n * `AWS_ACCESS_KEY_ID` environment variable.\n */\n accessKeyId?: string;\n\n /**\n * The AWS secret access key to use for the Bedrock Mantle provider. Defaults to the value of the\n * `AWS_SECRET_ACCESS_KEY` environment variable.\n */\n secretAccessKey?: string;\n\n /**\n * The AWS session token to use for the Bedrock Mantle provider. Defaults to the value of the\n * `AWS_SESSION_TOKEN` environment variable.\n */\n sessionToken?: string;\n\n /**\n * The AWS profile to use for loading credentials from the AWS CLI configuration.\n * When specified, credentials will be loaded from ~/.aws/credentials for the given profile.\n * This takes precedence over static credentials but is overridden by `apiKey`.\n */\n profile?: string;\n\n /**\n * Base URL for the Bedrock Mantle API calls. Defaults to the bedrock-mantle endpoint.\n */\n baseURL?: string;\n\n /**\n * Custom headers to include in the requests.\n */\n headers?: Record<string, string>;\n\n /**\n * Custom fetch implementation. You can use it as a middleware to intercept requests,\n * or to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n\n /**\n * The AWS credential provider to use for the Bedrock Mantle provider to get dynamic\n * credentials similar to the AWS SDK. Setting a provider here will cause its\n * credential values to be used instead of the `accessKeyId`, `secretAccessKey`,\n * and `sessionToken` settings.\n */\n credentialProvider?: () => PromiseLike<{\n accessKeyId: string;\n secretAccessKey: string;\n sessionToken?: string;\n }>;\n}\n\nexport type BedrockMantleChatModelId = string;\nexport type BedrockMantleEmbeddingModelId = string;\nexport type BedrockMantleImageModelId = string;\n\nexport interface AmazonBedrockMantleProvider extends ProviderV3 {\n (modelId: BedrockMantleChatModelId): LanguageModelV3;\n\n languageModel(modelId: BedrockMantleChatModelId): LanguageModelV3;\n\n chatModel(modelId: BedrockMantleChatModelId): LanguageModelV3;\n\n embeddingModel(modelId: BedrockMantleEmbeddingModelId): EmbeddingModelV3;\n\n imageModel(modelId: BedrockMantleImageModelId): ImageModelV3;\n}\n\ninterface BedrockCredentials {\n region: string;\n accessKeyId: string;\n secretAccessKey: string;\n sessionToken?: string;\n}\n\n/**\n * Creates a fetch function that applies AWS Signature Version 4 signing for bedrock-mantle service.\n *\n * @param getCredentials - Function that returns the AWS credentials to use when signing.\n * @param fetchImpl - Optional original fetch implementation to wrap. Defaults to global fetch.\n * @returns A FetchFunction that signs requests before passing them to the underlying fetch.\n */\nfunction createMantleSigV4FetchFunction(\n getCredentials: () => BedrockCredentials | PromiseLike<BedrockCredentials>,\n fetchImpl: FetchFunction = globalThis.fetch,\n): FetchFunction {\n return async (\n input: RequestInfo | URL,\n init?: RequestInit,\n ): Promise<Response> => {\n const url =\n typeof input === 'string'\n ? input\n : input instanceof URL\n ? input.href\n : input.url;\n\n const body =\n typeof init?.body === 'string'\n ? init.body\n : init?.body\n ? JSON.stringify(init.body)\n : undefined;\n\n const headers = init?.headers ?? {};\n const headerEntries =\n headers instanceof Headers\n ? Array.from(headers.entries())\n : Array.isArray(headers)\n ? headers\n : Object.entries(headers);\n\n const credentials = await getCredentials();\n const signer = new AwsV4Signer({\n url,\n method: init?.method ?? 'POST',\n headers: headerEntries,\n body,\n region: credentials.region,\n accessKeyId: credentials.accessKeyId,\n secretAccessKey: credentials.secretAccessKey,\n sessionToken: credentials.sessionToken,\n service: 'bedrock-mantle',\n });\n\n const signed = await signer.sign();\n return fetchImpl(input, {\n ...init,\n body,\n headers: Object.fromEntries(signed.headers.entries()),\n });\n };\n}\n\n/**\n * Create an Amazon Bedrock Mantle provider instance.\n * Bedrock Mantle is AWS Bedrock's OpenAI-compatible endpoint that routes all requests\n * through bedrock-mantle.<region>.api.aws/v1 using OpenAI API format.\n */\nexport function createAmazonBedrockMantle(\n options: AmazonBedrockMantleProviderSettings = {},\n): AmazonBedrockMantleProvider {\n // Check for API key authentication first\n const rawApiKey = loadOptionalSetting({\n settingValue: options.apiKey,\n environmentVariableName: 'AWS_BEARER_TOKEN_BEDROCK',\n });\n\n // Only use API key if it's a non-empty, non-whitespace string\n const apiKey =\n rawApiKey && rawApiKey.trim().length > 0 ? rawApiKey.trim() : undefined;\n\n const region = loadSetting({\n settingValue: options.region,\n settingName: 'region',\n environmentVariableName: 'AWS_REGION',\n description: 'AWS region',\n });\n\n // Use API key authentication if available, otherwise fall back to SigV4\n let fetchFunction: FetchFunction;\n if (apiKey) {\n fetchFunction = options.fetch ?? globalThis.fetch;\n } else if (options.credentialProvider) {\n fetchFunction = createMantleSigV4FetchFunction(async () => {\n return {\n ...(await options.credentialProvider!()),\n region,\n };\n }, options.fetch);\n } else {\n fetchFunction = createMantleSigV4FetchFunction(async () => {\n return {\n region,\n accessKeyId: loadSetting({\n settingValue: options.accessKeyId,\n settingName: 'accessKeyId',\n environmentVariableName: 'AWS_ACCESS_KEY_ID',\n description: 'AWS access key ID',\n }),\n secretAccessKey: loadSetting({\n settingValue: options.secretAccessKey,\n settingName: 'secretAccessKey',\n environmentVariableName: 'AWS_SECRET_ACCESS_KEY',\n description: 'AWS secret access key',\n }),\n sessionToken: loadOptionalSetting({\n settingValue: options.sessionToken,\n environmentVariableName: 'AWS_SESSION_TOKEN',\n }),\n };\n }, options.fetch);\n }\n\n const baseURL =\n withoutTrailingSlash(\n options.baseURL ?? `https://bedrock-mantle.${region}.api.aws/v1`,\n ) ?? `https://bedrock-mantle.us-east-1.api.aws/v1`;\n\n // Create the OpenAI-compatible provider\n const mantleProvider: OpenAICompatibleProvider<\n string,\n string,\n string,\n string\n > = createOpenAICompatible({\n baseURL,\n name: 'amazon-bedrock-mantle',\n apiKey: apiKey ?? 'dummy-key-for-sigv4',\n fetch: fetchFunction,\n headers: options.headers,\n includeUsage: true,\n });\n\n const provider = function (modelId: BedrockMantleChatModelId) {\n if (new.target) {\n throw new Error(\n 'The Amazon Bedrock Mantle model function cannot be called with the new keyword.',\n );\n }\n return mantleProvider.languageModel(modelId);\n };\n\n provider.specificationVersion = 'v3' as const;\n provider.languageModel = mantleProvider.languageModel;\n provider.chatModel = mantleProvider.languageModel;\n provider.embeddingModel = mantleProvider.embeddingModel;\n provider.imageModel = mantleProvider.imageModel;\n\n return provider as AmazonBedrockMantleProvider;\n}\n\n/**\n * Default Bedrock Mantle provider instance.\n */\nexport const bedrockMantle = createAmazonBedrockMantle();\n","// Version string of this package injected at build time.\ndeclare const __PACKAGE_VERSION__: string | undefined;\nexport const VERSION: string =\n typeof __PACKAGE_VERSION__ !== 'undefined'\n ? __PACKAGE_VERSION__\n : '0.0.0-test';\n"],"mappings":";AAMA;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,OAEK;AACP,SAAS,mBAAmB;AAwG5B,SAAS,+BACP,gBACA,YAA2B,WAAW,OACvB;AACf,SAAO,OACL,OACA,SACsB;AA/H1B;AAgII,UAAM,MACJ,OAAO,UAAU,WACb,QACA,iBAAiB,MACf,MAAM,OACN,MAAM;AAEd,UAAM,OACJ,QAAO,6BAAM,UAAS,WAClB,KAAK,QACL,6BAAM,QACJ,KAAK,UAAU,KAAK,IAAI,IACxB;AAER,UAAM,WAAU,kCAAM,YAAN,YAAiB,CAAC;AAClC,UAAM,gBACJ,mBAAmB,UACf,MAAM,KAAK,QAAQ,QAAQ,CAAC,IAC5B,MAAM,QAAQ,OAAO,IACnB,UACA,OAAO,QAAQ,OAAO;AAE9B,UAA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AA,UAC3B,cAAc,QAAQ;AAAA,UACtB,aAAa;AAAA,UACb,yBAAyB;AAAA,UACzB,aAAa;AAAA,QACf,CAAC;AAAA,QACD,cAAc,oBAAoB;AAAA,UAChC,cAAc,QAAQ;AAAA,UACtB,yBAAyB;AAAA,QAC3B,CAAC;AAAA,MACH;AAAA,IACF,GAAG,QAAQ,KAAK;AAAA,EAClB;AAEA,QAAM,WACJ;AAAA,KACE,aAAQ,YAAR,YAAmB,0BAA0B,MAAM;AAAA,EACrD,MAFA,YAEK;AAGP,QAAM,iBAKF,uBAAuB;AAAA,IACzB;AAAA,IACA,MAAM;AAAA,IACN,QAAQ,0BAAU;AAAA,IAClB,OAAO;AAAA,IACP,SAAS,QAAQ;AAAA,IACjB,cAAc;AAAA,EAChB,CAAC;AAED,QAAM,WAAW,SAAU,SAAmC;AAC5D,QAAI,YAAY;AACd,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AACA,WAAO,eAAe,cAAc,OAAO;AAAA,EAC7C;AAEA,WAAS,uBAAuB;AAChC,WAAS,gBAAgB,eAAe;AACxC,WAAS,YAAY,eAAe;AACpC,WAAS,iBAAiB,eAAe;AACzC,WAAS,aAAa,eAAe;AAErC,SAAO;AACT;AAKO,IAAM,gBAAgB,0BAA0B;;;AC/QhD,IAAM,UACX,OACI,UACA;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@matthewdunbar/amazon-bedrock-mantle",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.mjs",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist/**/*",
|
|
11
|
+
"CHANGELOG.md",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "pnpm clean && tsup --tsconfig tsconfig.build.json",
|
|
16
|
+
"build:watch": "pnpm clean && tsup --watch",
|
|
17
|
+
"clean": "del-cli dist *.tsbuildinfo",
|
|
18
|
+
"lint": "eslint \"./**/*.ts*\"",
|
|
19
|
+
"type-check": "tsc --build",
|
|
20
|
+
"prettier-check": "prettier --check \"./**/*.ts*\"",
|
|
21
|
+
"test": "pnpm test:node && pnpm test:edge",
|
|
22
|
+
"test:update": "pnpm test:node -u",
|
|
23
|
+
"test:watch": "vitest --config vitest.node.config.js",
|
|
24
|
+
"test:edge": "vitest --config vitest.edge.config.js --run",
|
|
25
|
+
"test:node": "vitest --config vitest.node.config.js --run"
|
|
26
|
+
},
|
|
27
|
+
"exports": {
|
|
28
|
+
"./package.json": "./package.json",
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.mjs",
|
|
32
|
+
"require": "./dist/index.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@ai-sdk/openai-compatible": "workspace:*",
|
|
37
|
+
"@ai-sdk/provider": "workspace:*",
|
|
38
|
+
"@ai-sdk/provider-utils": "workspace:*",
|
|
39
|
+
"aws4fetch": "^1.0.20"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@ai-sdk/test-server": "workspace:*",
|
|
43
|
+
"@types/node": "20.17.24",
|
|
44
|
+
"@vercel/ai-tsconfig": "workspace:*",
|
|
45
|
+
"tsup": "^8.3.0",
|
|
46
|
+
"typescript": "5.8.3",
|
|
47
|
+
"zod": "3.25.76"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"zod": "^3.25.76 || ^4.1.8"
|
|
51
|
+
},
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=18"
|
|
54
|
+
},
|
|
55
|
+
"publishConfig": {
|
|
56
|
+
"access": "public"
|
|
57
|
+
},
|
|
58
|
+
"homepage": "https://ai-sdk.dev/docs",
|
|
59
|
+
"repository": {
|
|
60
|
+
"type": "git",
|
|
61
|
+
"url": "git+https://github.com/vercel/ai.git"
|
|
62
|
+
},
|
|
63
|
+
"bugs": {
|
|
64
|
+
"url": "https://github.com/vercel/ai/issues"
|
|
65
|
+
},
|
|
66
|
+
"keywords": [
|
|
67
|
+
"ai"
|
|
68
|
+
]
|
|
69
|
+
}
|