@ai-sdk/amazon-bedrock 5.0.100 → 5.0.102
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 +20 -0
- package/dist/anthropic/index.d.ts +79 -76
- package/dist/anthropic/index.d.ts.map +1 -0
- package/dist/anthropic/index.js +429 -521
- package/dist/anthropic/index.js.map +1 -1
- package/dist/index.d.ts +218 -211
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2547 -3233
- package/dist/index.js.map +1 -1
- package/dist/mantle/index.cjs +181 -235
- package/dist/mantle/index.cjs.map +1 -1
- package/dist/mantle/index.d.cts +85 -82
- package/dist/mantle/index.d.cts.map +1 -0
- package/dist/mantle/index.d.ts +85 -82
- package/dist/mantle/index.d.ts.map +1 -0
- package/dist/mantle/index.js +178 -221
- package/dist/mantle/index.js.map +1 -1
- package/docs/08-amazon-bedrock.mdx +2 -2
- package/package.json +10 -10
package/dist/anthropic/index.js
CHANGED
|
@@ -1,555 +1,463 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
} from "@ai-sdk/provider";
|
|
5
|
-
import {
|
|
6
|
-
loadOptionalSetting as loadOptionalSetting2,
|
|
7
|
-
loadSetting,
|
|
8
|
-
resolve,
|
|
9
|
-
withUserAgentSuffix as withUserAgentSuffix2
|
|
10
|
-
} from "@ai-sdk/provider-utils";
|
|
11
|
-
import {
|
|
12
|
-
anthropicTools,
|
|
13
|
-
AnthropicLanguageModel
|
|
14
|
-
} from "@ai-sdk/anthropic/internal";
|
|
15
|
-
|
|
16
|
-
// src/amazon-bedrock-sigv4-fetch.ts
|
|
17
|
-
import {
|
|
18
|
-
combineHeaders,
|
|
19
|
-
normalizeHeaders,
|
|
20
|
-
withUserAgentSuffix,
|
|
21
|
-
getRuntimeEnvironmentUserAgent
|
|
22
|
-
} from "@ai-sdk/provider-utils";
|
|
1
|
+
import { NoSuchModelError } from "@ai-sdk/provider";
|
|
2
|
+
import { combineHeaders, convertBase64ToUint8Array, getRuntimeEnvironmentUserAgent, loadOptionalSetting, loadSetting, normalizeHeaders, resolve, safeParseJSON, withUserAgentSuffix, withoutTrailingSlash } from "@ai-sdk/provider-utils";
|
|
3
|
+
import { AnthropicLanguageModel, anthropicTools } from "@ai-sdk/anthropic/internal";
|
|
23
4
|
import { AwsV4Signer } from "aws4fetch";
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
5
|
+
import { z } from "zod/v4";
|
|
6
|
+
import { EventStreamCodec } from "@smithy/eventstream-codec";
|
|
7
|
+
import { fromUtf8, toUtf8 } from "@smithy/util-utf8";
|
|
8
|
+
//#region src/version.ts
|
|
9
|
+
const VERSION = "5.0.102";
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/amazon-bedrock-sigv4-fetch.ts
|
|
12
|
+
/**
|
|
13
|
+
* Creates a fetch function that applies AWS Signature Version 4 signing.
|
|
14
|
+
*
|
|
15
|
+
* @param getCredentials - Function that returns the AWS credentials to use when signing.
|
|
16
|
+
* @param fetch - Optional original fetch implementation to wrap. Defaults to global fetch.
|
|
17
|
+
* @param service - The AWS service name to use for SigV4 signing scope. Defaults to 'bedrock'.
|
|
18
|
+
* @returns A FetchFunction that signs requests before passing them to the underlying fetch.
|
|
19
|
+
*/
|
|
29
20
|
function createSigV4FetchFunction(getCredentials, fetch, service = "bedrock") {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
accessKeyId: credentials.accessKeyId,
|
|
66
|
-
secretAccessKey: credentials.secretAccessKey,
|
|
67
|
-
sessionToken: credentials.sessionToken,
|
|
68
|
-
service
|
|
69
|
-
});
|
|
70
|
-
const signingResult = await signer.sign();
|
|
71
|
-
const signedHeaders = normalizeHeaders(signingResult.headers);
|
|
72
|
-
const combinedHeaders = combineHeaders(headersWithUserAgent, signedHeaders);
|
|
73
|
-
return effectiveFetch(input, {
|
|
74
|
-
...init,
|
|
75
|
-
body,
|
|
76
|
-
headers: combinedHeaders
|
|
77
|
-
});
|
|
78
|
-
};
|
|
21
|
+
return async (input, init) => {
|
|
22
|
+
const effectiveFetch = fetch ?? globalThis.fetch;
|
|
23
|
+
const request = input instanceof Request ? input : void 0;
|
|
24
|
+
const originalHeaders = combineHeaders(normalizeHeaders(request?.headers), normalizeHeaders(init?.headers));
|
|
25
|
+
const headersWithUserAgent = withUserAgentSuffix(originalHeaders, `ai-sdk-amazon-bedrock/${VERSION}`, getRuntimeEnvironmentUserAgent());
|
|
26
|
+
let effectiveBody = init?.body ?? void 0;
|
|
27
|
+
if (effectiveBody === void 0 && request && request.body !== null) try {
|
|
28
|
+
effectiveBody = await request.clone().text();
|
|
29
|
+
} catch {}
|
|
30
|
+
if ((init?.method ?? request?.method)?.toUpperCase() !== "POST" || !effectiveBody) return effectiveFetch(input, {
|
|
31
|
+
...init,
|
|
32
|
+
headers: headersWithUserAgent
|
|
33
|
+
});
|
|
34
|
+
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
|
|
35
|
+
const body = prepareBodyString(effectiveBody);
|
|
36
|
+
const credentials = await getCredentials();
|
|
37
|
+
const signingResult = await new AwsV4Signer({
|
|
38
|
+
url,
|
|
39
|
+
method: "POST",
|
|
40
|
+
headers: Object.entries(headersWithUserAgent),
|
|
41
|
+
body,
|
|
42
|
+
region: credentials.region,
|
|
43
|
+
accessKeyId: credentials.accessKeyId,
|
|
44
|
+
secretAccessKey: credentials.secretAccessKey,
|
|
45
|
+
sessionToken: credentials.sessionToken,
|
|
46
|
+
service
|
|
47
|
+
}).sign();
|
|
48
|
+
const signedHeaders = normalizeHeaders(signingResult.headers);
|
|
49
|
+
const combinedHeaders = combineHeaders(headersWithUserAgent, signedHeaders);
|
|
50
|
+
return effectiveFetch(input, {
|
|
51
|
+
...init,
|
|
52
|
+
body,
|
|
53
|
+
headers: combinedHeaders
|
|
54
|
+
});
|
|
55
|
+
};
|
|
79
56
|
}
|
|
80
57
|
function prepareBodyString(body) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
} else if (body instanceof ArrayBuffer) {
|
|
86
|
-
return new TextDecoder().decode(new Uint8Array(body));
|
|
87
|
-
} else {
|
|
88
|
-
return JSON.stringify(body);
|
|
89
|
-
}
|
|
58
|
+
if (typeof body === "string") return body;
|
|
59
|
+
else if (body instanceof Uint8Array) return new TextDecoder().decode(body);
|
|
60
|
+
else if (body instanceof ArrayBuffer) return new TextDecoder().decode(new Uint8Array(body));
|
|
61
|
+
else return JSON.stringify(body);
|
|
90
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Creates a fetch function that applies Bearer token authentication.
|
|
65
|
+
*
|
|
66
|
+
* @param apiKey - The API key to use for Bearer token authentication.
|
|
67
|
+
* @param fetch - Optional original fetch implementation to wrap. Defaults to global fetch.
|
|
68
|
+
* @returns A FetchFunction that adds Authorization header with Bearer token to requests.
|
|
69
|
+
*/
|
|
91
70
|
function createApiKeyFetchFunction(apiKey, fetch) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
});
|
|
103
|
-
return effectiveFetch(input, {
|
|
104
|
-
...init,
|
|
105
|
-
headers: finalHeaders
|
|
106
|
-
});
|
|
107
|
-
};
|
|
71
|
+
return async (input, init) => {
|
|
72
|
+
const effectiveFetch = fetch ?? globalThis.fetch;
|
|
73
|
+
const originalHeaders = normalizeHeaders(init?.headers);
|
|
74
|
+
const headersWithUserAgent = withUserAgentSuffix(originalHeaders, `ai-sdk-amazon-bedrock/${VERSION}`, getRuntimeEnvironmentUserAgent());
|
|
75
|
+
const finalHeaders = combineHeaders(headersWithUserAgent, { Authorization: `Bearer ${apiKey}` });
|
|
76
|
+
return effectiveFetch(input, {
|
|
77
|
+
...init,
|
|
78
|
+
headers: finalHeaders
|
|
79
|
+
});
|
|
80
|
+
};
|
|
108
81
|
}
|
|
109
|
-
|
|
110
|
-
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/amazon-bedrock-anthropic-model-support.ts
|
|
111
84
|
function supportsStrictTools(modelId) {
|
|
112
|
-
|
|
85
|
+
return !matchesModel(modelId, MODELS_WITHOUT_STRICT_TOOL_SUPPORT);
|
|
113
86
|
}
|
|
114
87
|
function supportsNativeStructuredOutput(modelId) {
|
|
115
|
-
|
|
116
|
-
modelId,
|
|
117
|
-
MODELS_WITHOUT_RELIABLE_NATIVE_STRUCTURED_OUTPUT
|
|
118
|
-
);
|
|
88
|
+
return !matchesModel(modelId, MODELS_WITHOUT_RELIABLE_NATIVE_STRUCTURED_OUTPUT);
|
|
119
89
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
90
|
+
const MODELS_WITHOUT_STRICT_TOOL_SUPPORT = [
|
|
91
|
+
"claude-opus-4-7",
|
|
92
|
+
"claude-opus-4-8",
|
|
93
|
+
"claude-opus-5",
|
|
94
|
+
"claude-fable-5",
|
|
95
|
+
"claude-sonnet-5"
|
|
126
96
|
];
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
97
|
+
const MODELS_WITHOUT_RELIABLE_NATIVE_STRUCTURED_OUTPUT = [
|
|
98
|
+
...MODELS_WITHOUT_STRICT_TOOL_SUPPORT,
|
|
99
|
+
"claude-sonnet-4-6",
|
|
100
|
+
"claude-haiku-4-5"
|
|
131
101
|
];
|
|
132
102
|
function matchesModel(modelId, models) {
|
|
133
|
-
|
|
103
|
+
return models.some((model) => modelId.includes(model));
|
|
134
104
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
105
|
+
//#endregion
|
|
106
|
+
//#region src/resolve-amazon-bedrock-base-url.ts
|
|
107
|
+
const AWS_PARTITION_DNS_SUFFIXES = [
|
|
108
|
+
{
|
|
109
|
+
regionPrefix: "cn-",
|
|
110
|
+
dnsSuffix: "amazonaws.com.cn"
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
regionPrefix: "us-iso-",
|
|
114
|
+
dnsSuffix: "c2s.ic.gov"
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
regionPrefix: "us-isob-",
|
|
118
|
+
dnsSuffix: "sc2s.sgov.gov"
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
regionPrefix: "eu-isoe-",
|
|
122
|
+
dnsSuffix: "cloud.adc-e.uk"
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
regionPrefix: "us-isof-",
|
|
126
|
+
dnsSuffix: "csp.hci.ic.gov"
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
regionPrefix: "eusc-",
|
|
130
|
+
dnsSuffix: "amazonaws.eu"
|
|
131
|
+
}
|
|
148
132
|
];
|
|
149
|
-
function resolveAmazonBedrockBaseURL({
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
});
|
|
162
|
-
if (resolvedBaseURL != null) {
|
|
163
|
-
return withoutTrailingSlash(resolvedBaseURL) ?? resolvedBaseURL;
|
|
164
|
-
}
|
|
165
|
-
const region = getRegion();
|
|
166
|
-
const dnsSuffix = AWS_PARTITION_DNS_SUFFIXES.find(
|
|
167
|
-
({ regionPrefix }) => region.startsWith(regionPrefix)
|
|
168
|
-
)?.dnsSuffix ?? "amazonaws.com";
|
|
169
|
-
const generatedBaseURL = `https://${service}.${region}.${dnsSuffix}`;
|
|
170
|
-
return withoutTrailingSlash(generatedBaseURL) ?? generatedBaseURL;
|
|
133
|
+
function resolveAmazonBedrockBaseURL({ baseURL, getRegion, service, serviceEndpointUrlEnvironmentVariableName }) {
|
|
134
|
+
const resolvedBaseURL = baseURL ?? loadOptionalSetting({
|
|
135
|
+
settingValue: void 0,
|
|
136
|
+
environmentVariableName: serviceEndpointUrlEnvironmentVariableName
|
|
137
|
+
}) ?? loadOptionalSetting({
|
|
138
|
+
settingValue: void 0,
|
|
139
|
+
environmentVariableName: "AWS_ENDPOINT_URL"
|
|
140
|
+
});
|
|
141
|
+
if (resolvedBaseURL != null) return withoutTrailingSlash(resolvedBaseURL) ?? resolvedBaseURL;
|
|
142
|
+
const region = getRegion();
|
|
143
|
+
const generatedBaseURL = `https://${service}.${region}.${AWS_PARTITION_DNS_SUFFIXES.find(({ regionPrefix }) => region.startsWith(regionPrefix))?.dnsSuffix ?? "amazonaws.com"}`;
|
|
144
|
+
return withoutTrailingSlash(generatedBaseURL) ?? generatedBaseURL;
|
|
171
145
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
import {
|
|
175
|
-
convertBase64ToUint8Array,
|
|
176
|
-
safeParseJSON
|
|
177
|
-
} from "@ai-sdk/provider-utils";
|
|
178
|
-
import { z } from "zod/v4";
|
|
179
|
-
|
|
180
|
-
// src/amazon-bedrock-event-stream-decoder.ts
|
|
181
|
-
import { EventStreamCodec } from "@smithy/eventstream-codec";
|
|
182
|
-
import { toUtf8, fromUtf8 } from "@smithy/util-utf8";
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/amazon-bedrock-event-stream-decoder.ts
|
|
183
148
|
function createAmazonBedrockEventStreamDecoder(body, processEvent) {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
},
|
|
216
|
-
flush() {
|
|
217
|
-
if (buffer.length > 0) {
|
|
218
|
-
throw new Error(
|
|
219
|
-
`Incomplete Amazon Bedrock event-stream frame: ${buffer.length} buffered bytes remain at end of stream.`
|
|
220
|
-
);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
})
|
|
224
|
-
);
|
|
149
|
+
const codec = new EventStreamCodec(toUtf8, fromUtf8);
|
|
150
|
+
let buffer = /* @__PURE__ */ new Uint8Array(0);
|
|
151
|
+
const textDecoder = new TextDecoder();
|
|
152
|
+
return body.pipeThrough(new TransformStream({
|
|
153
|
+
async transform(chunk, controller) {
|
|
154
|
+
const newBuffer = new Uint8Array(buffer.length + chunk.length);
|
|
155
|
+
newBuffer.set(buffer);
|
|
156
|
+
newBuffer.set(chunk, buffer.length);
|
|
157
|
+
buffer = newBuffer;
|
|
158
|
+
while (buffer.length >= 4) {
|
|
159
|
+
const totalLength = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength).getUint32(0, false);
|
|
160
|
+
if (buffer.length < totalLength) break;
|
|
161
|
+
const subView = buffer.subarray(0, totalLength);
|
|
162
|
+
const decoded = codec.decode(subView);
|
|
163
|
+
buffer = buffer.slice(totalLength);
|
|
164
|
+
const messageType = decoded.headers[":message-type"]?.value;
|
|
165
|
+
const eventType = decoded.headers[":event-type"]?.value;
|
|
166
|
+
const exceptionType = decoded.headers[":exception-type"]?.value;
|
|
167
|
+
await processEvent({
|
|
168
|
+
messageType,
|
|
169
|
+
eventType,
|
|
170
|
+
exceptionType,
|
|
171
|
+
data: textDecoder.decode(decoded.body)
|
|
172
|
+
}, controller);
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
flush() {
|
|
176
|
+
if (buffer.length > 0) throw new Error(`Incomplete Amazon Bedrock event-stream frame: ${buffer.length} buffered bytes remain at end of stream.`);
|
|
177
|
+
}
|
|
178
|
+
}));
|
|
225
179
|
}
|
|
226
|
-
|
|
227
|
-
|
|
180
|
+
//#endregion
|
|
181
|
+
//#region src/amazon-bedrock-stream-error.ts
|
|
228
182
|
function getAmazonBedrockStreamErrorMetadata(type) {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
183
|
+
switch (type) {
|
|
184
|
+
case "internalServerException":
|
|
185
|
+
case "InternalServerException": return {
|
|
186
|
+
statusCode: 500,
|
|
187
|
+
isRetryable: true
|
|
188
|
+
};
|
|
189
|
+
case "modelStreamErrorException":
|
|
190
|
+
case "ModelStreamErrorException": return {
|
|
191
|
+
statusCode: 424,
|
|
192
|
+
isRetryable: true
|
|
193
|
+
};
|
|
194
|
+
case "serviceUnavailableException":
|
|
195
|
+
case "ServiceUnavailableException": return {
|
|
196
|
+
statusCode: 503,
|
|
197
|
+
isRetryable: true
|
|
198
|
+
};
|
|
199
|
+
case "throttlingException":
|
|
200
|
+
case "ThrottlingException": return {
|
|
201
|
+
statusCode: 429,
|
|
202
|
+
isRetryable: true
|
|
203
|
+
};
|
|
204
|
+
case "validationException":
|
|
205
|
+
case "ValidationException": return {
|
|
206
|
+
statusCode: 400,
|
|
207
|
+
isRetryable: false
|
|
208
|
+
};
|
|
209
|
+
default: return {};
|
|
210
|
+
}
|
|
248
211
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
message: z.string().optional()
|
|
253
|
-
});
|
|
212
|
+
//#endregion
|
|
213
|
+
//#region src/anthropic/amazon-bedrock-anthropic-fetch.ts
|
|
214
|
+
const amazonBedrockErrorSchema = z.looseObject({ message: z.string().optional() });
|
|
254
215
|
function createAmazonBedrockAnthropicFetch(baseFetch) {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
216
|
+
return async (url, options) => {
|
|
217
|
+
const response = await baseFetch(url, options);
|
|
218
|
+
if (!response.ok) {
|
|
219
|
+
const text = await response.text();
|
|
220
|
+
const parsed = await safeParseJSON({
|
|
221
|
+
text,
|
|
222
|
+
schema: amazonBedrockErrorSchema
|
|
223
|
+
});
|
|
224
|
+
const message = parsed.success && parsed.value.message ? parsed.value.message : text;
|
|
225
|
+
const anthropicError = JSON.stringify({
|
|
226
|
+
type: "error",
|
|
227
|
+
error: {
|
|
228
|
+
type: "error",
|
|
229
|
+
message
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
return new Response(anthropicError, {
|
|
233
|
+
status: response.status,
|
|
234
|
+
statusText: response.statusText,
|
|
235
|
+
headers: response.headers
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
if (response.headers.get("content-type")?.includes("application/vnd.amazon.eventstream") && response.body != null) {
|
|
239
|
+
const transformedBody = transformAmazonBedrockEventStreamToSSE(response.body);
|
|
240
|
+
return new Response(transformedBody, {
|
|
241
|
+
status: response.status,
|
|
242
|
+
statusText: response.statusText,
|
|
243
|
+
headers: new Headers({
|
|
244
|
+
...Object.fromEntries(response.headers.entries()),
|
|
245
|
+
"content-type": "text/event-stream"
|
|
246
|
+
})
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
return response;
|
|
250
|
+
};
|
|
290
251
|
}
|
|
291
252
|
function transformAmazonBedrockEventStreamToSSE(body) {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
`
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
const parsed = await safeParseJSON({ text: event.data });
|
|
325
|
-
const data = parsed.success ? parsed.value : event.data;
|
|
326
|
-
const message = typeof data === "object" && data != null && typeof data.message === "string" ? data.message : event.data;
|
|
327
|
-
const type = event.eventType ?? event.exceptionType ?? "error";
|
|
328
|
-
const metadata = getAmazonBedrockStreamErrorMetadata(type);
|
|
329
|
-
controller.enqueue(
|
|
330
|
-
textEncoder.encode(
|
|
331
|
-
`data: ${JSON.stringify({
|
|
332
|
-
type: "error",
|
|
333
|
-
error: {
|
|
334
|
-
type,
|
|
335
|
-
message,
|
|
336
|
-
...metadata,
|
|
337
|
-
data
|
|
338
|
-
}
|
|
339
|
-
})}
|
|
340
|
-
|
|
341
|
-
`
|
|
342
|
-
)
|
|
343
|
-
);
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
);
|
|
253
|
+
const textEncoder = new TextEncoder();
|
|
254
|
+
return createAmazonBedrockEventStreamDecoder(body, async (event, controller) => {
|
|
255
|
+
if (event.messageType === "event") {
|
|
256
|
+
if (event.eventType === "chunk") {
|
|
257
|
+
const parsed = await safeParseJSON({ text: event.data });
|
|
258
|
+
if (!parsed.success) {
|
|
259
|
+
controller.enqueue(textEncoder.encode(`data: ${event.data}\n\n`));
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
const bytes = parsed.value.bytes;
|
|
263
|
+
if (bytes) {
|
|
264
|
+
const anthropicEvent = new TextDecoder().decode(convertBase64ToUint8Array(bytes));
|
|
265
|
+
controller.enqueue(textEncoder.encode(`data: ${anthropicEvent}\n\n`));
|
|
266
|
+
} else controller.enqueue(textEncoder.encode(`data: ${event.data}\n\n`));
|
|
267
|
+
} else if (event.eventType === "messageStop") controller.enqueue(textEncoder.encode("data: [DONE]\n\n"));
|
|
268
|
+
} else if (event.messageType === "exception") {
|
|
269
|
+
const parsed = await safeParseJSON({ text: event.data });
|
|
270
|
+
const data = parsed.success ? parsed.value : event.data;
|
|
271
|
+
const message = typeof data === "object" && data != null && typeof data.message === "string" ? data.message : event.data;
|
|
272
|
+
const type = event.eventType ?? event.exceptionType ?? "error";
|
|
273
|
+
const metadata = getAmazonBedrockStreamErrorMetadata(type);
|
|
274
|
+
controller.enqueue(textEncoder.encode(`data: ${JSON.stringify({
|
|
275
|
+
type: "error",
|
|
276
|
+
error: {
|
|
277
|
+
type,
|
|
278
|
+
message,
|
|
279
|
+
...metadata,
|
|
280
|
+
data
|
|
281
|
+
}
|
|
282
|
+
})}\n\n`));
|
|
283
|
+
}
|
|
284
|
+
});
|
|
347
285
|
}
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
};
|
|
355
|
-
var BEDROCK_TOOL_NAME_MAP = {
|
|
356
|
-
text_editor_20250728: "str_replace_based_edit_tool"
|
|
286
|
+
//#endregion
|
|
287
|
+
//#region src/anthropic/amazon-bedrock-anthropic-provider.ts
|
|
288
|
+
const BEDROCK_TOOL_VERSION_MAP = {
|
|
289
|
+
bash_20241022: "bash_20250124",
|
|
290
|
+
text_editor_20241022: "text_editor_20250728",
|
|
291
|
+
computer_20241022: "computer_20250124"
|
|
357
292
|
};
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
tool_search_tool_bm25_20251119: "tool-search-tool-2025-10-19"
|
|
293
|
+
const BEDROCK_TOOL_NAME_MAP = { text_editor_20250728: "str_replace_based_edit_tool" };
|
|
294
|
+
const BEDROCK_TOOL_BETA_MAP = {
|
|
295
|
+
bash_20250124: "computer-use-2025-01-24",
|
|
296
|
+
bash_20241022: "computer-use-2024-10-22",
|
|
297
|
+
text_editor_20250124: "computer-use-2025-01-24",
|
|
298
|
+
text_editor_20241022: "computer-use-2024-10-22",
|
|
299
|
+
text_editor_20250429: "computer-use-2025-01-24",
|
|
300
|
+
text_editor_20250728: "computer-use-2025-01-24",
|
|
301
|
+
computer_20250124: "computer-use-2025-01-24",
|
|
302
|
+
computer_20241022: "computer-use-2024-10-22",
|
|
303
|
+
tool_search_tool_regex_20251119: "tool-search-tool-2025-10-19",
|
|
304
|
+
tool_search_tool_bm25_20251119: "tool-search-tool-2025-10-19"
|
|
371
305
|
};
|
|
306
|
+
/**
|
|
307
|
+
* Create an Amazon Bedrock Anthropic provider instance.
|
|
308
|
+
* This provider uses the native Anthropic API through Bedrock's InvokeModel endpoint,
|
|
309
|
+
* bypassing the Converse API for better feature compatibility.
|
|
310
|
+
*/
|
|
372
311
|
function createAmazonBedrockAnthropic(options = {}) {
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
};
|
|
418
|
-
} catch (error) {
|
|
419
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
420
|
-
if (errorMessage.includes("AWS_ACCESS_KEY_ID") || errorMessage.includes("accessKeyId")) {
|
|
421
|
-
throw new Error(
|
|
422
|
-
`AWS SigV4 authentication requires AWS credentials. Please provide either:
|
|
312
|
+
const rawApiKey = loadOptionalSetting({
|
|
313
|
+
settingValue: options.apiKey,
|
|
314
|
+
environmentVariableName: "AWS_BEARER_TOKEN_BEDROCK"
|
|
315
|
+
});
|
|
316
|
+
const apiKey = rawApiKey && rawApiKey.trim().length > 0 ? rawApiKey.trim() : void 0;
|
|
317
|
+
const fetchFunction = createAmazonBedrockAnthropicFetch(apiKey ? createApiKeyFetchFunction(apiKey, options.fetch) : createSigV4FetchFunction(async () => {
|
|
318
|
+
const region = loadSetting({
|
|
319
|
+
settingValue: options.region,
|
|
320
|
+
settingName: "region",
|
|
321
|
+
environmentVariableName: "AWS_REGION",
|
|
322
|
+
description: "AWS region"
|
|
323
|
+
});
|
|
324
|
+
if (options.credentialProvider) try {
|
|
325
|
+
return {
|
|
326
|
+
...await options.credentialProvider(),
|
|
327
|
+
region
|
|
328
|
+
};
|
|
329
|
+
} catch (error) {
|
|
330
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
331
|
+
throw new Error(`AWS credential provider failed: ${errorMessage}. Please ensure your credential provider returns valid AWS credentials with accessKeyId and secretAccessKey properties.`);
|
|
332
|
+
}
|
|
333
|
+
try {
|
|
334
|
+
return {
|
|
335
|
+
region,
|
|
336
|
+
accessKeyId: loadSetting({
|
|
337
|
+
settingValue: options.accessKeyId,
|
|
338
|
+
settingName: "accessKeyId",
|
|
339
|
+
environmentVariableName: "AWS_ACCESS_KEY_ID",
|
|
340
|
+
description: "AWS access key ID"
|
|
341
|
+
}),
|
|
342
|
+
secretAccessKey: loadSetting({
|
|
343
|
+
settingValue: options.secretAccessKey,
|
|
344
|
+
settingName: "secretAccessKey",
|
|
345
|
+
environmentVariableName: "AWS_SECRET_ACCESS_KEY",
|
|
346
|
+
description: "AWS secret access key"
|
|
347
|
+
}),
|
|
348
|
+
sessionToken: loadOptionalSetting({
|
|
349
|
+
settingValue: options.sessionToken,
|
|
350
|
+
environmentVariableName: "AWS_SESSION_TOKEN"
|
|
351
|
+
})
|
|
352
|
+
};
|
|
353
|
+
} catch (error) {
|
|
354
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
355
|
+
if (errorMessage.includes("AWS_ACCESS_KEY_ID") || errorMessage.includes("accessKeyId")) throw new Error(`AWS SigV4 authentication requires AWS credentials. Please provide either:
|
|
423
356
|
1. Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables
|
|
424
357
|
2. Provide accessKeyId and secretAccessKey in options
|
|
425
358
|
3. Use a credentialProvider function
|
|
426
359
|
4. Use API key authentication with AWS_BEARER_TOKEN_BEDROCK or apiKey option
|
|
427
|
-
Original error: ${errorMessage}`
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
supportedUrls: () => ({}),
|
|
523
|
-
supportsNativeStructuredOutput: supportsNativeStructuredOutput(modelId),
|
|
524
|
-
supportsStrictTools: supportsStrictTools(modelId)
|
|
525
|
-
});
|
|
526
|
-
const provider = function(modelId) {
|
|
527
|
-
if (new.target) {
|
|
528
|
-
throw new Error(
|
|
529
|
-
"The Bedrock Anthropic model function cannot be called with the new keyword."
|
|
530
|
-
);
|
|
531
|
-
}
|
|
532
|
-
return createChatModel(modelId);
|
|
533
|
-
};
|
|
534
|
-
provider.specificationVersion = "v4";
|
|
535
|
-
provider.languageModel = createChatModel;
|
|
536
|
-
provider.chat = createChatModel;
|
|
537
|
-
provider.messages = createChatModel;
|
|
538
|
-
provider.embeddingModel = (modelId) => {
|
|
539
|
-
throw new NoSuchModelError({ modelId, modelType: "embeddingModel" });
|
|
540
|
-
};
|
|
541
|
-
provider.textEmbeddingModel = provider.embeddingModel;
|
|
542
|
-
provider.imageModel = (modelId) => {
|
|
543
|
-
throw new NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
544
|
-
};
|
|
545
|
-
provider.tools = anthropicTools;
|
|
546
|
-
return provider;
|
|
360
|
+
Original error: ${errorMessage}`);
|
|
361
|
+
if (errorMessage.includes("AWS_SECRET_ACCESS_KEY") || errorMessage.includes("secretAccessKey")) throw new Error(`AWS SigV4 authentication requires both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. Please ensure both credentials are provided.
|
|
362
|
+
Original error: ${errorMessage}`);
|
|
363
|
+
throw error;
|
|
364
|
+
}
|
|
365
|
+
}, options.fetch));
|
|
366
|
+
const getBaseURL = () => resolveAmazonBedrockBaseURL({
|
|
367
|
+
baseURL: options.baseURL,
|
|
368
|
+
getRegion: () => loadSetting({
|
|
369
|
+
settingValue: options.region,
|
|
370
|
+
settingName: "region",
|
|
371
|
+
environmentVariableName: "AWS_REGION",
|
|
372
|
+
description: "AWS region"
|
|
373
|
+
}),
|
|
374
|
+
service: "bedrock-runtime",
|
|
375
|
+
serviceEndpointUrlEnvironmentVariableName: "AWS_ENDPOINT_URL_BEDROCK_RUNTIME"
|
|
376
|
+
});
|
|
377
|
+
const getHeaders = async () => {
|
|
378
|
+
const baseHeaders = await resolve(options.headers) ?? {};
|
|
379
|
+
return withUserAgentSuffix(baseHeaders, `ai-sdk-amazon-bedrock/${VERSION}`);
|
|
380
|
+
};
|
|
381
|
+
const createChatModel = (modelId) => new AnthropicLanguageModel(modelId, {
|
|
382
|
+
provider: "bedrock.anthropic.messages",
|
|
383
|
+
baseURL: getBaseURL(),
|
|
384
|
+
headers: getHeaders,
|
|
385
|
+
fetch: fetchFunction,
|
|
386
|
+
buildRequestUrl: (baseURL, isStreaming) => `${baseURL}/model/${encodeURIComponent(modelId)}/${isStreaming ? "invoke-with-response-stream" : "invoke"}`,
|
|
387
|
+
transformRequestBody: (args, betas) => {
|
|
388
|
+
const { model: _model, stream: _stream, tool_choice, thinking, tools, ...rest } = args;
|
|
389
|
+
const transformedToolChoice = tool_choice != null ? {
|
|
390
|
+
type: tool_choice.type,
|
|
391
|
+
...tool_choice.name != null ? { name: tool_choice.name } : {}
|
|
392
|
+
} : void 0;
|
|
393
|
+
const transformedThinking = thinking?.block_binding != null ? {
|
|
394
|
+
...thinking,
|
|
395
|
+
block_binding: { mismatch_behavior: thinking.block_binding.prefix_mismatch_behavior }
|
|
396
|
+
} : thinking;
|
|
397
|
+
const requiredBetas = new Set(betas);
|
|
398
|
+
const transformedTools = tools?.map((rawTool) => {
|
|
399
|
+
const { eager_input_streaming: eagerInputStreaming, ...tool } = rawTool;
|
|
400
|
+
if (eagerInputStreaming === true) requiredBetas.add("fine-grained-tool-streaming-2025-05-14");
|
|
401
|
+
const toolType = tool.type;
|
|
402
|
+
if (toolType && toolType in BEDROCK_TOOL_VERSION_MAP) {
|
|
403
|
+
const newType = BEDROCK_TOOL_VERSION_MAP[toolType];
|
|
404
|
+
if (newType in BEDROCK_TOOL_BETA_MAP) requiredBetas.add(BEDROCK_TOOL_BETA_MAP[newType]);
|
|
405
|
+
const newName = newType in BEDROCK_TOOL_NAME_MAP ? BEDROCK_TOOL_NAME_MAP[newType] : tool.name;
|
|
406
|
+
return {
|
|
407
|
+
...tool,
|
|
408
|
+
type: newType,
|
|
409
|
+
name: newName
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
if (toolType && toolType in BEDROCK_TOOL_BETA_MAP) requiredBetas.add(BEDROCK_TOOL_BETA_MAP[toolType]);
|
|
413
|
+
if (toolType && toolType in BEDROCK_TOOL_NAME_MAP) return {
|
|
414
|
+
...tool,
|
|
415
|
+
name: BEDROCK_TOOL_NAME_MAP[toolType]
|
|
416
|
+
};
|
|
417
|
+
return tool;
|
|
418
|
+
});
|
|
419
|
+
return {
|
|
420
|
+
...rest,
|
|
421
|
+
...thinking != null ? { thinking: transformedThinking } : {},
|
|
422
|
+
...transformedTools != null ? { tools: transformedTools } : {},
|
|
423
|
+
...transformedToolChoice != null ? { tool_choice: transformedToolChoice } : {},
|
|
424
|
+
...requiredBetas.size > 0 ? { anthropic_beta: Array.from(requiredBetas) } : {},
|
|
425
|
+
anthropic_version: "bedrock-2023-05-31"
|
|
426
|
+
};
|
|
427
|
+
},
|
|
428
|
+
supportedUrls: () => ({}),
|
|
429
|
+
supportsNativeStructuredOutput: supportsNativeStructuredOutput(modelId),
|
|
430
|
+
supportsStrictTools: supportsStrictTools(modelId)
|
|
431
|
+
});
|
|
432
|
+
const provider = function(modelId) {
|
|
433
|
+
if (new.target) throw new Error("The Bedrock Anthropic model function cannot be called with the new keyword.");
|
|
434
|
+
return createChatModel(modelId);
|
|
435
|
+
};
|
|
436
|
+
provider.specificationVersion = "v4";
|
|
437
|
+
provider.languageModel = createChatModel;
|
|
438
|
+
provider.chat = createChatModel;
|
|
439
|
+
provider.messages = createChatModel;
|
|
440
|
+
provider.embeddingModel = (modelId) => {
|
|
441
|
+
throw new NoSuchModelError({
|
|
442
|
+
modelId,
|
|
443
|
+
modelType: "embeddingModel"
|
|
444
|
+
});
|
|
445
|
+
};
|
|
446
|
+
provider.textEmbeddingModel = provider.embeddingModel;
|
|
447
|
+
provider.imageModel = (modelId) => {
|
|
448
|
+
throw new NoSuchModelError({
|
|
449
|
+
modelId,
|
|
450
|
+
modelType: "imageModel"
|
|
451
|
+
});
|
|
452
|
+
};
|
|
453
|
+
provider.tools = anthropicTools;
|
|
454
|
+
return provider;
|
|
547
455
|
}
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
456
|
+
/**
|
|
457
|
+
* Default Bedrock Anthropic provider instance.
|
|
458
|
+
*/
|
|
459
|
+
const amazonBedrockAnthropic = createAmazonBedrockAnthropic();
|
|
460
|
+
//#endregion
|
|
461
|
+
export { amazonBedrockAnthropic, amazonBedrockAnthropic as bedrockAnthropic, createAmazonBedrockAnthropic, createAmazonBedrockAnthropic as createBedrockAnthropic };
|
|
462
|
+
|
|
555
463
|
//# sourceMappingURL=index.js.map
|