@ai-sdk/amazon-bedrock 5.0.127 → 5.0.129

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.
@@ -1,5 +1,8 @@
1
+ import { LoadAPIKeyError } from '@ai-sdk/provider';
1
2
  import {
2
3
  combineHeaders,
4
+ resolve,
5
+ type Resolvable,
3
6
  normalizeHeaders,
4
7
  withUserAgentSuffix,
5
8
  getRuntimeEnvironmentUserAgent,
@@ -124,7 +127,7 @@ function isAscii(value: string): boolean {
124
127
  * @returns A FetchFunction that adds Authorization header with Bearer token to requests.
125
128
  */
126
129
  export function createApiKeyFetchFunction(
127
- apiKey: string,
130
+ apiKey: Resolvable<string>,
128
131
  fetch?: FetchFunction,
129
132
  ): FetchFunction {
130
133
  return async (
@@ -140,8 +143,18 @@ export function createApiKeyFetchFunction(
140
143
  getRuntimeEnvironmentUserAgent(),
141
144
  );
142
145
 
146
+ const resolvedApiKey = await resolve(apiKey);
147
+ if (
148
+ typeof apiKey !== 'string' &&
149
+ (typeof resolvedApiKey !== 'string' || !resolvedApiKey.trim())
150
+ ) {
151
+ throw new LoadAPIKeyError({
152
+ message: 'Amazon Bedrock API key must be a non-empty string.',
153
+ });
154
+ }
155
+
143
156
  const finalHeaders = combineHeaders(headersWithUserAgent, {
144
- Authorization: `Bearer ${apiKey}`,
157
+ Authorization: `Bearer ${typeof apiKey === 'string' ? resolvedApiKey : resolvedApiKey.trim()}`,
145
158
  });
146
159
 
147
160
  return effectiveFetch(input, {
@@ -88,10 +88,11 @@ export interface AmazonBedrockAnthropicProviderSettings {
88
88
 
89
89
  /**
90
90
  * API key for authenticating requests using Bearer token authentication.
91
+ * A callback is resolved before each request and must return a non-empty key.
91
92
  * When provided, this will be used instead of AWS SigV4 authentication.
92
93
  * Defaults to the value of the `AWS_BEARER_TOKEN_BEDROCK` environment variable.
93
94
  */
94
- apiKey?: string;
95
+ apiKey?: Resolvable<string>;
95
96
 
96
97
  /**
97
98
  * The AWS access key ID to use for the Bedrock provider. Defaults to the value of the
@@ -147,14 +148,17 @@ export function createAmazonBedrockAnthropic(
147
148
  options: AmazonBedrockAnthropicProviderSettings = {},
148
149
  ): AmazonBedrockAnthropicProvider {
149
150
  // Check for API key authentication first
150
- const rawApiKey = loadOptionalSetting({
151
- settingValue: options.apiKey,
152
- environmentVariableName: 'AWS_BEARER_TOKEN_BEDROCK',
153
- });
151
+ const rawApiKey =
152
+ typeof options.apiKey === 'string' || options.apiKey == null
153
+ ? loadOptionalSetting({
154
+ settingValue: options.apiKey,
155
+ environmentVariableName: 'AWS_BEARER_TOKEN_BEDROCK',
156
+ })
157
+ : options.apiKey;
154
158
 
155
159
  // Only use API key if it's a non-empty, non-whitespace string
156
160
  const apiKey =
157
- rawApiKey && rawApiKey.trim().length > 0 ? rawApiKey.trim() : undefined;
161
+ typeof rawApiKey === 'string' ? rawApiKey.trim() || undefined : rawApiKey;
158
162
 
159
163
  // Use API key authentication if available, otherwise fall back to SigV4
160
164
  const baseFetchFunction = apiKey
@@ -15,6 +15,7 @@ import {
15
15
  withoutTrailingSlash,
16
16
  withUserAgentSuffix,
17
17
  type FetchFunction,
18
+ type Resolvable,
18
19
  } from '@ai-sdk/provider-utils';
19
20
  import {
20
21
  createApiKeyFetchFunction,
@@ -66,10 +67,11 @@ export interface BedrockMantleProviderSettings {
66
67
 
67
68
  /**
68
69
  * API key for authenticating requests using Bearer token authentication.
70
+ * A callback is resolved before each request and must return a non-empty key.
69
71
  * When provided, this will be used instead of AWS SigV4 authentication.
70
72
  * Defaults to the value of the `AWS_BEARER_TOKEN_BEDROCK` environment variable.
71
73
  */
72
- apiKey?: string;
74
+ apiKey?: Resolvable<string>;
73
75
 
74
76
  /**
75
77
  * The AWS access key ID to use for SigV4 authentication. Defaults to the value of the
@@ -125,14 +127,17 @@ export function createBedrockMantle(
125
127
  options: BedrockMantleProviderSettings = {},
126
128
  ): BedrockMantleProvider {
127
129
  // Check for API key authentication first
128
- const rawApiKey = loadOptionalSetting({
129
- settingValue: options.apiKey,
130
- environmentVariableName: 'AWS_BEARER_TOKEN_BEDROCK',
131
- });
130
+ const rawApiKey =
131
+ typeof options.apiKey === 'string' || options.apiKey == null
132
+ ? loadOptionalSetting({
133
+ settingValue: options.apiKey,
134
+ environmentVariableName: 'AWS_BEARER_TOKEN_BEDROCK',
135
+ })
136
+ : options.apiKey;
132
137
 
133
138
  // Only use API key if it's a non-empty, non-whitespace string
134
139
  const apiKey =
135
- rawApiKey && rawApiKey.trim().length > 0 ? rawApiKey.trim() : undefined;
140
+ typeof rawApiKey === 'string' ? rawApiKey.trim() || undefined : rawApiKey;
136
141
 
137
142
  // Use API key authentication if available, otherwise fall back to SigV4
138
143
  const fetchFunction = apiKey