@ai-sdk/amazon-bedrock 5.0.0-beta.44 → 5.0.0-beta.46

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.
Files changed (38) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/dist/anthropic/index.d.ts +10 -10
  3. package/dist/anthropic/index.js +58 -46
  4. package/dist/anthropic/index.js.map +1 -1
  5. package/dist/index.d.ts +25 -23
  6. package/dist/index.js +496 -436
  7. package/dist/index.js.map +1 -1
  8. package/docs/08-amazon-bedrock.mdx +80 -82
  9. package/package.json +5 -5
  10. package/src/amazon-bedrock-api-types.ts +224 -0
  11. package/src/{bedrock-chat-options.ts → amazon-bedrock-chat-language-model-options.ts} +7 -7
  12. package/src/{bedrock-chat-language-model.ts → amazon-bedrock-chat-language-model.ts} +217 -196
  13. package/src/{bedrock-embedding-options.ts → amazon-bedrock-embedding-model-options.ts} +1 -1
  14. package/src/{bedrock-embedding-model.ts → amazon-bedrock-embedding-model.ts} +34 -27
  15. package/src/{bedrock-error.ts → amazon-bedrock-error.ts} +1 -1
  16. package/src/{bedrock-event-stream-decoder.ts → amazon-bedrock-event-stream-decoder.ts} +1 -1
  17. package/src/{bedrock-event-stream-response-handler.ts → amazon-bedrock-event-stream-response-handler.ts} +6 -6
  18. package/src/{bedrock-image-model.ts → amazon-bedrock-image-model.ts} +43 -39
  19. package/src/amazon-bedrock-image-settings.ts +9 -0
  20. package/src/{bedrock-prepare-tools.ts → amazon-bedrock-prepare-tools.ts} +22 -18
  21. package/src/{bedrock-provider.ts → amazon-bedrock-provider.ts} +53 -46
  22. package/src/amazon-bedrock-reasoning-metadata.ts +10 -0
  23. package/src/{bedrock-sigv4-fetch.ts → amazon-bedrock-sigv4-fetch.ts} +5 -3
  24. package/src/anthropic/amazon-bedrock-anthropic-fetch.ts +104 -0
  25. package/src/anthropic/{bedrock-anthropic-options.ts → amazon-bedrock-anthropic-options.ts} +1 -1
  26. package/src/anthropic/{bedrock-anthropic-provider.ts → amazon-bedrock-anthropic-provider.ts} +22 -20
  27. package/src/anthropic/index.ts +19 -7
  28. package/src/{convert-bedrock-usage.ts → convert-amazon-bedrock-usage.ts} +4 -4
  29. package/src/{convert-to-bedrock-chat-messages.ts → convert-to-amazon-bedrock-chat-messages.ts} +146 -103
  30. package/src/index.ts +15 -8
  31. package/src/inject-fetch-headers.ts +1 -1
  32. package/src/{map-bedrock-finish-reason.ts → map-amazon-bedrock-finish-reason.ts} +4 -4
  33. package/src/reranking/{bedrock-reranking-api.ts → amazon-bedrock-reranking-api.ts} +3 -3
  34. package/src/reranking/{bedrock-reranking-options.ts → amazon-bedrock-reranking-model-options.ts} +1 -1
  35. package/src/reranking/{bedrock-reranking-model.ts → amazon-bedrock-reranking-model.ts} +32 -25
  36. package/src/anthropic/bedrock-anthropic-fetch.ts +0 -94
  37. package/src/bedrock-api-types.ts +0 -219
  38. package/src/bedrock-image-settings.ts +0 -6
@@ -1,94 +0,0 @@
1
- import {
2
- convertBase64ToUint8Array,
3
- FetchFunction,
4
- safeParseJSON,
5
- } from '@ai-sdk/provider-utils';
6
- import { z } from 'zod/v4';
7
- import { createBedrockEventStreamDecoder } from '../bedrock-event-stream-decoder';
8
-
9
- const bedrockErrorSchema = z.looseObject({
10
- message: z.string().optional(),
11
- });
12
-
13
- export function createBedrockAnthropicFetch(
14
- baseFetch: FetchFunction,
15
- ): FetchFunction {
16
- return async (url, options) => {
17
- const response = await baseFetch(url, options);
18
-
19
- // Transform Bedrock error responses into Anthropic error format
20
- // so that anthropicFailedResponseHandler can extract the message.
21
- if (!response.ok) {
22
- const text = await response.text();
23
- const parsed = await safeParseJSON({ text, schema: bedrockErrorSchema });
24
-
25
- const message =
26
- parsed.success && parsed.value.message ? parsed.value.message : text;
27
-
28
- const anthropicError = JSON.stringify({
29
- type: 'error',
30
- error: { type: 'error', message },
31
- });
32
-
33
- return new Response(anthropicError, {
34
- status: response.status,
35
- statusText: response.statusText,
36
- headers: response.headers,
37
- });
38
- }
39
-
40
- const contentType = response.headers.get('content-type');
41
- if (
42
- contentType?.includes('application/vnd.amazon.eventstream') &&
43
- response.body != null
44
- ) {
45
- const transformedBody = transformBedrockEventStreamToSSE(response.body);
46
-
47
- return new Response(transformedBody, {
48
- status: response.status,
49
- statusText: response.statusText,
50
- headers: new Headers({
51
- ...Object.fromEntries(response.headers.entries()),
52
- 'content-type': 'text/event-stream',
53
- }),
54
- });
55
- }
56
-
57
- return response;
58
- };
59
- }
60
-
61
- function transformBedrockEventStreamToSSE(
62
- body: ReadableStream<Uint8Array>,
63
- ): ReadableStream<Uint8Array> {
64
- const textEncoder = new TextEncoder();
65
-
66
- return createBedrockEventStreamDecoder(body, async (event, controller) => {
67
- if (event.messageType === 'event') {
68
- if (event.eventType === 'chunk') {
69
- const parsed = await safeParseJSON({ text: event.data });
70
- if (!parsed.success) {
71
- controller.enqueue(textEncoder.encode(`data: ${event.data}\n\n`));
72
- return;
73
- }
74
- const bytes = (parsed.value as { bytes?: string }).bytes;
75
- if (bytes) {
76
- const anthropicEvent = new TextDecoder().decode(
77
- convertBase64ToUint8Array(bytes),
78
- );
79
- controller.enqueue(textEncoder.encode(`data: ${anthropicEvent}\n\n`));
80
- } else {
81
- controller.enqueue(textEncoder.encode(`data: ${event.data}\n\n`));
82
- }
83
- } else if (event.eventType === 'messageStop') {
84
- controller.enqueue(textEncoder.encode('data: [DONE]\n\n'));
85
- }
86
- } else if (event.messageType === 'exception') {
87
- controller.enqueue(
88
- textEncoder.encode(
89
- `data: ${JSON.stringify({ type: 'error', error: event.data })}\n\n`,
90
- ),
91
- );
92
- }
93
- });
94
- }
@@ -1,219 +0,0 @@
1
- import { JSONObject } from '@ai-sdk/provider';
2
-
3
- export interface BedrockConverseInput {
4
- system?: BedrockSystemMessages;
5
- messages: BedrockMessages;
6
- toolConfig?: BedrockToolConfiguration;
7
- inferenceConfig?: {
8
- maxOutputTokens?: number;
9
- temperature?: number;
10
- topP?: number;
11
- topK?: number;
12
- stopSequences?: string[];
13
- };
14
- additionalModelRequestFields?: Record<string, unknown>;
15
- additionalModelResponseFieldPaths?: string[];
16
- serviceTier?: {
17
- type: string;
18
- };
19
- guardrailConfig?:
20
- | BedrockGuardrailConfiguration
21
- | BedrockGuardrailStreamConfiguration
22
- | undefined;
23
- }
24
-
25
- export type BedrockSystemMessages = Array<BedrockSystemContentBlock>;
26
-
27
- export type BedrockMessages = Array<
28
- BedrockAssistantMessage | BedrockUserMessage
29
- >;
30
-
31
- export interface BedrockAssistantMessage {
32
- role: 'assistant';
33
- content: Array<BedrockContentBlock>;
34
- }
35
-
36
- export interface BedrockUserMessage {
37
- role: 'user';
38
- content: Array<BedrockContentBlock>;
39
- }
40
-
41
- /**
42
- * Cache TTL options for Bedrock prompt caching.
43
- * @see https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html
44
- *
45
- * - '5m': 5-minute TTL (default, supported by all models)
46
- * - '1h': 1-hour TTL (supported by Claude Opus 4.5, Claude Haiku 4.5, Claude Sonnet 4.5)
47
- */
48
- export type BedrockCacheTTL = '5m' | '1h';
49
-
50
- export type BedrockCachePoint = {
51
- cachePoint: { type: 'default'; ttl?: BedrockCacheTTL };
52
- };
53
-
54
- /**
55
- * Creates a cache point with an optional TTL.
56
- * @param ttl - Cache TTL ('5m' or '1h'). If not provided, uses the default 5-minute TTL.
57
- */
58
- export function createBedrockCachePoint(
59
- ttl?: BedrockCacheTTL,
60
- ): BedrockCachePoint {
61
- return {
62
- cachePoint: { type: 'default', ttl },
63
- };
64
- }
65
-
66
- export type BedrockSystemContentBlock = { text: string } | BedrockCachePoint;
67
-
68
- export interface BedrockGuardrailConfiguration {
69
- guardrails?: Array<{
70
- name: string;
71
- description?: string;
72
- parameters?: Record<string, unknown>;
73
- }>;
74
- }
75
-
76
- export type BedrockGuardrailStreamConfiguration = BedrockGuardrailConfiguration;
77
-
78
- export interface BedrockToolInputSchema {
79
- json: Record<string, unknown>;
80
- }
81
-
82
- export interface BedrockTool {
83
- toolSpec: {
84
- name: string;
85
- description?: string;
86
- strict?: boolean;
87
- inputSchema: { json: JSONObject };
88
- };
89
- }
90
-
91
- export interface BedrockToolConfiguration {
92
- tools?: Array<BedrockTool | BedrockCachePoint>;
93
- toolChoice?:
94
- | { tool: { name: string } }
95
- | { auto: {} }
96
- | { any: {} }
97
- | undefined;
98
- }
99
-
100
- export const BEDROCK_STOP_REASONS = [
101
- 'stop',
102
- 'stop_sequence',
103
- 'end_turn',
104
- 'length',
105
- 'max_tokens',
106
- 'content-filter',
107
- 'content_filtered',
108
- 'guardrail_intervened',
109
- 'tool-calls',
110
- 'tool_use',
111
- ] as const;
112
-
113
- export type BedrockStopReason = (typeof BEDROCK_STOP_REASONS)[number];
114
-
115
- /**
116
- * @see https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ImageBlock.html
117
- */
118
- export const BEDROCK_IMAGE_MIME_TYPES = {
119
- 'image/jpeg': 'jpeg',
120
- 'image/png': 'png',
121
- 'image/gif': 'gif',
122
- 'image/webp': 'webp',
123
- } as const;
124
- type BedrockImageFormats = typeof BEDROCK_IMAGE_MIME_TYPES;
125
- export type BedrockImageFormat = BedrockImageFormats[keyof BedrockImageFormats];
126
- export type BedrockImageMimeType = keyof BedrockImageFormats;
127
-
128
- /**
129
- * @see https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_DocumentBlock.html
130
- */
131
- export const BEDROCK_DOCUMENT_MIME_TYPES = {
132
- 'application/pdf': 'pdf',
133
- 'text/csv': 'csv',
134
- 'application/msword': 'doc',
135
- 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
136
- 'docx',
137
- 'application/vnd.ms-excel': 'xls',
138
- 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'xlsx',
139
- 'text/html': 'html',
140
- 'text/plain': 'txt',
141
- 'text/markdown': 'md',
142
- } as const;
143
- type BedrockDocumentFormats = typeof BEDROCK_DOCUMENT_MIME_TYPES;
144
- export type BedrockDocumentFormat =
145
- BedrockDocumentFormats[keyof BedrockDocumentFormats];
146
- export type BedrockDocumentMimeType = keyof BedrockDocumentFormats;
147
-
148
- export interface BedrockDocumentBlock {
149
- document: {
150
- format: BedrockDocumentFormat;
151
- name: string;
152
- source: {
153
- bytes: string;
154
- };
155
- citations?: {
156
- enabled: boolean;
157
- };
158
- };
159
- }
160
-
161
- export interface BedrockGuardrailConverseContentBlock {
162
- guardContent: unknown;
163
- }
164
-
165
- export interface BedrockImageBlock {
166
- image: {
167
- format: BedrockImageFormat;
168
- source: {
169
- bytes: string;
170
- };
171
- };
172
- }
173
-
174
- export interface BedrockToolResultBlock {
175
- toolResult: {
176
- toolUseId: string;
177
- content: Array<BedrockTextBlock | BedrockImageBlock>;
178
- };
179
- }
180
-
181
- export interface BedrockToolUseBlock {
182
- toolUse: {
183
- toolUseId: string;
184
- name: string;
185
- input: Record<string, unknown>;
186
- };
187
- }
188
-
189
- export interface BedrockTextBlock {
190
- text: string;
191
- }
192
-
193
- export interface BedrockReasoningContentBlock {
194
- reasoningContent: {
195
- reasoningText: {
196
- text: string;
197
- signature?: string;
198
- };
199
- };
200
- }
201
-
202
- export interface BedrockRedactedReasoningContentBlock {
203
- reasoningContent: {
204
- redactedReasoning: {
205
- data: string;
206
- };
207
- };
208
- }
209
-
210
- export type BedrockContentBlock =
211
- | BedrockDocumentBlock
212
- | BedrockGuardrailConverseContentBlock
213
- | BedrockImageBlock
214
- | BedrockTextBlock
215
- | BedrockToolResultBlock
216
- | BedrockToolUseBlock
217
- | BedrockReasoningContentBlock
218
- | BedrockRedactedReasoningContentBlock
219
- | BedrockCachePoint;
@@ -1,6 +0,0 @@
1
- export type BedrockImageModelId = 'amazon.nova-canvas-v1:0' | (string & {});
2
-
3
- // https://docs.aws.amazon.com/nova/latest/userguide/image-gen-req-resp-structure.html
4
- export const modelMaxImagesPerCall: Record<BedrockImageModelId, number> = {
5
- 'amazon.nova-canvas-v1:0': 5,
6
- };