ai 7.0.95 → 7.0.96

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.
@@ -91,7 +91,7 @@ import {
91
91
  } from "@ai-sdk/provider-utils";
92
92
 
93
93
  // src/version.ts
94
- var VERSION = true ? "7.0.95" : "0.0.0-test";
94
+ var VERSION = true ? "7.0.96" : "0.0.0-test";
95
95
 
96
96
  // src/util/download/download.ts
97
97
  var download = async ({
@@ -18,7 +18,7 @@ summarization, and content generation that do not need an immediate response.
18
18
  The batch API is designed to support multiple request types. Currently, only
19
19
  `type: 'text'` is supported.
20
20
 
21
- The AI SDK provides three functions for the batch lifecycle:
21
+ The AI SDK provides five functions for the batch lifecycle:
22
22
 
23
23
  - [`experimental_startBatch`](/docs/reference/ai-sdk-core/start-batch)
24
24
  submits a batch and returns its initial status and a serializable reference.
@@ -26,18 +26,30 @@ The AI SDK provides three functions for the batch lifecycle:
26
26
  retrieves the latest status and request counts.
27
27
  - [`experimental_getBatchResults`](/docs/reference/ai-sdk-core/get-batch-results)
28
28
  asynchronously iterates over the terminal result for each request.
29
+ - [`experimental_cancelBatch`](/docs/reference/ai-sdk-core/cancel-batch)
30
+ requests cancellation of a batch.
31
+ - [`experimental_listBatches`](/docs/reference/ai-sdk-core/list-batches)
32
+ lists batches and their latest statuses.
29
33
 
30
- All three functions are exported from `ai`. The examples below use aliases so
34
+ All five functions are exported from `ai`. The examples below use aliases so
31
35
  the shorter names can be used in application code:
32
36
 
33
37
  ```ts
34
38
  import {
39
+ experimental_cancelBatch as cancelBatch,
35
40
  experimental_getBatchResults as getBatchResults,
36
41
  experimental_getBatchStatus as getBatchStatus,
42
+ experimental_listBatches as listBatches,
37
43
  experimental_startBatch as startBatch,
38
44
  } from 'ai';
39
45
  ```
40
46
 
47
+ Cancellation and listing are optional provider capabilities. Calling either
48
+ function with a provider that does not implement it throws an
49
+ `UnsupportedFunctionalityError`. In the cancellation and listing examples
50
+ below, `provider` represents a batch provider that implements the respective
51
+ capability.
52
+
41
53
  ## Supported providers
42
54
 
43
55
  Batch processing requires a provider that implements the batch interface.
@@ -218,6 +230,55 @@ support and payloads are provider-specific. The provider pages linked above
218
230
  describe their webhook behavior; providers that do not support webhooks return
219
231
  an unsupported warning and continue without one.
220
232
 
233
+ ## Cancelling a batch
234
+
235
+ Use `cancelBatch` to ask the provider to stop processing a batch:
236
+
237
+ ```ts
238
+ const result = await cancelBatch({
239
+ provider,
240
+ batch,
241
+ });
242
+
243
+ console.log(result.providerMetadata);
244
+ ```
245
+
246
+ A successful call means that the provider accepted the cancellation request.
247
+ It does not guarantee that cancellation has finished or that every pending
248
+ request will be cancelled. Use `getBatchStatus` to retrieve the latest status
249
+ after requesting cancellation.
250
+
251
+ The result can include provider metadata with additional information from the
252
+ cancellation response.
253
+
254
+ ## Listing batches
255
+
256
+ Use `listBatches` to retrieve batches from the provider. Results are paginated.
257
+ Pass the returned `nextCursor` as `cursor` to retrieve the next page:
258
+
259
+ ```ts
260
+ let cursor: string | undefined;
261
+
262
+ do {
263
+ const page = await listBatches({
264
+ provider,
265
+ limit: 20,
266
+ cursor,
267
+ });
268
+
269
+ for (const batch of page.batches) {
270
+ console.log(batch.id, batch.status);
271
+ }
272
+
273
+ cursor = page.nextCursor;
274
+ } while (cursor != null);
275
+ ```
276
+
277
+ Each item is a serializable batch reference with its latest normalized status,
278
+ so it can be passed directly to `getBatchStatus`, `getBatchResults`, or
279
+ `cancelBatch`. Cursors are opaque and provider-specific; applications should
280
+ store or pass them unchanged rather than inspect their contents.
281
+
221
282
  ## Retrieving results
222
283
 
223
284
  After the batch is complete, `getBatchResults` returns an async iterable. Each
@@ -257,14 +318,14 @@ sensitive data.
257
318
 
258
319
  ## Request controls
259
320
 
260
- `getBatchStatus` and `getBatchResults` accept `providerOptions`, `headers`,
261
- `timeout`, `abortSignal`, and `maxRetries` for the current status or result
262
- retrieval operation:
321
+ All batch lifecycle functions accept `providerOptions`, `headers`, `timeout`,
322
+ and `abortSignal` for the current operation. `getBatchStatus`,
323
+ `getBatchResults`, and `listBatches` also accept `maxRetries`:
263
324
 
264
- - `maxRetries` controls retries for status and result retrieval. It does not
265
- retry batch creation, which could create a duplicate batch. It defaults to 2;
266
- set it to 0 to disable retries.
267
- - `abortSignal` cancels the current status or result request.
325
+ - `maxRetries` controls retries for status, result retrieval, and listing. It
326
+ does not retry batch creation or cancellation. It defaults to 2; set it to 0
327
+ to disable retries.
328
+ - `abortSignal` cancels the current API request.
268
329
  - `timeout` limits the current HTTP operation.
269
330
 
270
331
  These controls affect communication with the provider. They do not change the
@@ -0,0 +1,96 @@
1
+ ---
2
+ title: experimental_cancelBatch
3
+ description: API Reference for experimental_cancelBatch.
4
+ ---
5
+
6
+ # `experimental_cancelBatch()`
7
+
8
+ <Note type="warning">
9
+ Batch support is experimental and the API may change in patch releases.
10
+ </Note>
11
+
12
+ Requests cancellation of an asynchronous batch. A successful call means that
13
+ the provider accepted the cancellation request, not that cancellation has
14
+ finished. For a complete guide to the batch lifecycle, see
15
+ [Batch](/docs/ai-sdk-core/batch).
16
+
17
+ ```ts
18
+ import { experimental_cancelBatch as cancelBatch } from 'ai';
19
+
20
+ const result = await cancelBatch({
21
+ provider,
22
+ batch,
23
+ });
24
+
25
+ console.log(result.providerMetadata);
26
+ ```
27
+
28
+ ## Import
29
+
30
+ <Snippet
31
+ text={`import { experimental_cancelBatch } from "ai"`}
32
+ prompt={false}
33
+ />
34
+
35
+ ## API Signature
36
+
37
+ ### Parameters
38
+
39
+ <PropertiesTable
40
+ content={[
41
+ {
42
+ name: 'provider',
43
+ type: 'Experimental_BatchProvider',
44
+ isOptional: true,
45
+ description:
46
+ 'The provider used to access the batch. Defaults to the global provider, or the AI Gateway when no global provider is configured.',
47
+ },
48
+ {
49
+ name: 'batch',
50
+ type: 'Experimental_BatchReference',
51
+ description:
52
+ 'The serializable reference returned by experimental_startBatch.',
53
+ },
54
+ {
55
+ name: 'providerOptions',
56
+ type: 'ProviderOptions',
57
+ isOptional: true,
58
+ description:
59
+ 'Additional provider-specific options for the cancellation request.',
60
+ },
61
+ {
62
+ name: 'abortSignal',
63
+ type: 'AbortSignal',
64
+ isOptional: true,
65
+ description: 'An optional abort signal to cancel the API request.',
66
+ },
67
+ {
68
+ name: 'timeout',
69
+ type: 'number | { totalMs?: number }',
70
+ isOptional: true,
71
+ description: 'Maximum time allowed for the cancellation request.',
72
+ },
73
+ {
74
+ name: 'headers',
75
+ type: 'Record<string, string | undefined>',
76
+ isOptional: true,
77
+ description: 'Additional HTTP headers for the request.',
78
+ },
79
+ ]}
80
+ />
81
+
82
+ ### Returns
83
+
84
+ <PropertiesTable
85
+ content={[
86
+ {
87
+ name: 'providerMetadata',
88
+ type: 'ProviderMetadata',
89
+ isOptional: true,
90
+ description: 'Provider-specific metadata from the cancellation response.',
91
+ },
92
+ ]}
93
+ />
94
+
95
+ Calling this function with a provider that does not support batch cancellation
96
+ throws an `UnsupportedFunctionalityError`.
@@ -0,0 +1,121 @@
1
+ ---
2
+ title: experimental_listBatches
3
+ description: API Reference for experimental_listBatches.
4
+ ---
5
+
6
+ # `experimental_listBatches()`
7
+
8
+ <Note type="warning">
9
+ Batch support is experimental and the API may change in patch releases.
10
+ </Note>
11
+
12
+ Lists a page of asynchronous batches and their latest normalized statuses. For
13
+ a complete guide to the batch lifecycle, see [Batch](/docs/ai-sdk-core/batch).
14
+
15
+ ```ts
16
+ import { experimental_listBatches as listBatches } from 'ai';
17
+
18
+ const page = await listBatches({
19
+ provider,
20
+ limit: 20,
21
+ cursor,
22
+ });
23
+
24
+ console.log(page.batches, page.nextCursor);
25
+ ```
26
+
27
+ ## Import
28
+
29
+ <Snippet
30
+ text={`import { experimental_listBatches } from "ai"`}
31
+ prompt={false}
32
+ />
33
+
34
+ ## API Signature
35
+
36
+ ### Parameters
37
+
38
+ <PropertiesTable
39
+ content={[
40
+ {
41
+ name: 'provider',
42
+ type: 'Experimental_BatchProvider',
43
+ isOptional: true,
44
+ description:
45
+ 'The provider whose batches should be listed. Defaults to the global provider, or the AI Gateway when no global provider is configured.',
46
+ },
47
+ {
48
+ name: 'limit',
49
+ type: 'number',
50
+ isOptional: true,
51
+ description: 'Maximum number of batches to return in this page.',
52
+ },
53
+ {
54
+ name: 'cursor',
55
+ type: 'string',
56
+ isOptional: true,
57
+ description:
58
+ 'Opaque cursor returned as nextCursor by the previous list operation.',
59
+ },
60
+ {
61
+ name: 'providerOptions',
62
+ type: 'ProviderOptions',
63
+ isOptional: true,
64
+ description: 'Additional provider-specific options for listing batches.',
65
+ },
66
+ {
67
+ name: 'maxRetries',
68
+ type: 'number',
69
+ isOptional: true,
70
+ description:
71
+ 'Maximum number of retries for listing batches. Set to 0 to disable retries. Default: 2.',
72
+ },
73
+ {
74
+ name: 'abortSignal',
75
+ type: 'AbortSignal',
76
+ isOptional: true,
77
+ description: 'An optional abort signal to cancel the request.',
78
+ },
79
+ {
80
+ name: 'timeout',
81
+ type: 'number | { totalMs?: number }',
82
+ isOptional: true,
83
+ description: 'Maximum time allowed for the list request.',
84
+ },
85
+ {
86
+ name: 'headers',
87
+ type: 'Record<string, string | undefined>',
88
+ isOptional: true,
89
+ description: 'Additional HTTP headers for the request.',
90
+ },
91
+ ]}
92
+ />
93
+
94
+ ### Returns
95
+
96
+ <PropertiesTable
97
+ content={[
98
+ {
99
+ name: 'batches',
100
+ type: 'Array<Experimental_Batch>',
101
+ description:
102
+ 'The batches in this page. Each item contains a serializable batch reference and its latest normalized status.',
103
+ },
104
+ {
105
+ name: 'nextCursor',
106
+ type: 'string',
107
+ isOptional: true,
108
+ description:
109
+ 'Opaque cursor to pass as cursor to retrieve the next page. Omitted when there are no more batches.',
110
+ },
111
+ {
112
+ name: 'providerMetadata',
113
+ type: 'ProviderMetadata',
114
+ isOptional: true,
115
+ description: 'Provider-specific metadata for the list operation.',
116
+ },
117
+ ]}
118
+ />
119
+
120
+ Calling this function with a provider that does not support listing batches
121
+ throws an `UnsupportedFunctionalityError`.
@@ -68,6 +68,16 @@ AI SDK Core contains the following main functions:
68
68
  description: 'Retrieve terminal results from an asynchronous batch.',
69
69
  href: '/docs/reference/ai-sdk-core/get-batch-results',
70
70
  },
71
+ {
72
+ title: 'experimental_cancelBatch()',
73
+ description: 'Request cancellation of an asynchronous batch.',
74
+ href: '/docs/reference/ai-sdk-core/cancel-batch',
75
+ },
76
+ {
77
+ title: 'experimental_listBatches()',
78
+ description: 'List asynchronous batches and their latest statuses.',
79
+ href: '/docs/reference/ai-sdk-core/list-batches',
80
+ },
71
81
  {
72
82
  title: 'transcribe()',
73
83
  description: 'Generate a transcript from an audio file.',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "7.0.95",
3
+ "version": "7.0.96",
4
4
  "type": "module",
5
5
  "description": "AI SDK by Vercel - build apps like ChatGPT, Claude, Gemini, and more with a single interface for any model using the Vercel AI Gateway or go direct to OpenAI, Anthropic, Google, or any other model provider.",
6
6
  "license": "Apache-2.0",
@@ -42,20 +42,20 @@
42
42
  }
43
43
  },
44
44
  "dependencies": {
45
- "@ai-sdk/gateway": "4.0.76",
46
- "@ai-sdk/provider": "4.0.11",
47
- "@ai-sdk/provider-utils": "5.0.37"
45
+ "@ai-sdk/gateway": "4.0.77",
46
+ "@ai-sdk/provider": "4.0.12",
47
+ "@ai-sdk/provider-utils": "5.0.38"
48
48
  },
49
49
  "devDependencies": {
50
- "@ai-sdk/amazon-bedrock": "5.0.79",
51
- "@ai-sdk/deepseek": "3.0.40",
52
- "@ai-sdk/google": "4.0.65",
53
- "@ai-sdk/groq": "4.0.38",
54
- "@ai-sdk/huggingface": "2.0.45",
55
- "@ai-sdk/moonshotai": "3.0.46",
56
- "@ai-sdk/openai": "4.0.63",
50
+ "@ai-sdk/amazon-bedrock": "5.0.80",
51
+ "@ai-sdk/deepseek": "3.0.41",
52
+ "@ai-sdk/google": "4.0.66",
53
+ "@ai-sdk/groq": "4.0.39",
54
+ "@ai-sdk/huggingface": "2.0.46",
55
+ "@ai-sdk/moonshotai": "3.0.47",
56
+ "@ai-sdk/openai": "4.0.64",
57
57
  "@ai-sdk/test-server": "2.0.1",
58
- "@ai-sdk/xai": "4.0.55",
58
+ "@ai-sdk/xai": "4.0.56",
59
59
  "@edge-runtime/vm": "^5.0.0",
60
60
  "@smithy/eventstream-codec": "^4.3.3",
61
61
  "@smithy/util-utf8": "^4.3.3",
@@ -118,6 +118,42 @@ export type StartBatchResult = Batch & {
118
118
  readonly warnings: BatchV4StartResult['warnings'];
119
119
  };
120
120
 
121
+ /**
122
+ * Options for requesting cancellation of a batch.
123
+ */
124
+ export type CancelBatchOptions = {
125
+ provider?: BatchProvider;
126
+ batch: BatchReference;
127
+ providerOptions?: ProviderOptions;
128
+ } & BatchCallOptions;
129
+
130
+ /**
131
+ * Result of requesting cancellation of a batch.
132
+ */
133
+ export type CancelBatchResult = {
134
+ readonly providerMetadata?: ProviderMetadata;
135
+ };
136
+
137
+ /**
138
+ * Options for listing batches.
139
+ */
140
+ export type ListBatchesOptions = {
141
+ provider?: BatchProvider;
142
+ providerOptions?: ProviderOptions;
143
+ limit?: number;
144
+ cursor?: string;
145
+ maxRetries?: number;
146
+ } & BatchCallOptions;
147
+
148
+ /**
149
+ * One page of listed batches.
150
+ */
151
+ export type ListBatchesResult = {
152
+ readonly batches: Array<Batch>;
153
+ readonly nextCursor?: string;
154
+ readonly providerMetadata?: ProviderMetadata;
155
+ };
156
+
121
157
  /**
122
158
  * Options for retrieving batch status.
123
159
  */
@@ -31,14 +31,114 @@ import type {
31
31
  BatchProvider,
32
32
  BatchReference,
33
33
  BatchStatus,
34
+ CancelBatchOptions,
35
+ CancelBatchResult,
34
36
  GetBatchResultsOptions,
35
37
  GetBatchStatusOptions,
38
+ ListBatchesOptions,
39
+ ListBatchesResult,
36
40
  StartBatchOptions,
37
41
  StartBatchResult,
38
42
  TextBatchGenerationResult,
39
43
  TextBatchItemResult,
40
44
  } from './batch-types';
41
45
 
46
+ /**
47
+ * Requests cancellation of a batch.
48
+ */
49
+ export async function cancelBatch({
50
+ provider,
51
+ batch,
52
+ providerOptions,
53
+ abortSignal,
54
+ headers,
55
+ timeout,
56
+ }: CancelBatchOptions): Promise<CancelBatchResult> {
57
+ const batchApi = resolveBatchApi(provider);
58
+ validateBatchReference({ batchApi, batch });
59
+
60
+ if (batchApi.doCancelBatch == null) {
61
+ throw new UnsupportedFunctionalityError({
62
+ functionality: 'batch cancellation',
63
+ message: 'The provider does not support batch cancellation.',
64
+ });
65
+ }
66
+
67
+ const operationAbortSignal = mergeAbortSignals(
68
+ abortSignal,
69
+ getTotalTimeoutMs(timeout),
70
+ );
71
+
72
+ try {
73
+ return await batchApi.doCancelBatch({
74
+ batchId: batch.id,
75
+ providerOptions,
76
+ abortSignal: operationAbortSignal,
77
+ headers: withUserAgentSuffix(headers ?? {}, `ai/${VERSION}`),
78
+ });
79
+ } catch (error) {
80
+ throw wrapGatewayError(error);
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Lists a page of batches.
86
+ */
87
+ export async function listBatches({
88
+ provider,
89
+ providerOptions,
90
+ limit,
91
+ cursor,
92
+ maxRetries,
93
+ abortSignal,
94
+ headers,
95
+ timeout,
96
+ }: ListBatchesOptions = {}): Promise<ListBatchesResult> {
97
+ const batchApi = resolveBatchApi(provider);
98
+
99
+ const doListBatches = batchApi.doListBatches?.bind(batchApi);
100
+ if (doListBatches == null) {
101
+ throw new UnsupportedFunctionalityError({
102
+ functionality: 'batch listing',
103
+ message: 'The provider does not support listing batches.',
104
+ });
105
+ }
106
+
107
+ const operationAbortSignal = mergeAbortSignals(
108
+ abortSignal,
109
+ getTotalTimeoutMs(timeout),
110
+ );
111
+ const { retry } = prepareRetries({
112
+ maxRetries,
113
+ abortSignal: operationAbortSignal,
114
+ });
115
+
116
+ try {
117
+ const { batches, nextCursor, providerMetadata } = await retry(() =>
118
+ doListBatches({
119
+ providerOptions,
120
+ abortSignal: operationAbortSignal,
121
+ headers: withUserAgentSuffix(headers ?? {}, `ai/${VERSION}`),
122
+ ...(limit != null && { limit }),
123
+ ...(cursor != null && { cursor }),
124
+ }),
125
+ );
126
+
127
+ return {
128
+ batches: batches.map(({ batchId, ...status }) => ({
129
+ version: 2,
130
+ id: batchId,
131
+ provider: batchApi.provider,
132
+ ...status,
133
+ })),
134
+ ...(nextCursor != null && { nextCursor }),
135
+ ...(providerMetadata != null && { providerMetadata }),
136
+ };
137
+ } catch (error) {
138
+ throw wrapGatewayError(error);
139
+ }
140
+ }
141
+
42
142
  /**
43
143
  * Starts a batch.
44
144
  */
@@ -1,15 +1,21 @@
1
1
  export {
2
+ cancelBatch as experimental_cancelBatch,
2
3
  startBatch as experimental_startBatch,
3
4
  getBatchResults as experimental_getBatchResults,
4
5
  getBatchStatus as experimental_getBatchStatus,
6
+ listBatches as experimental_listBatches,
5
7
  } from './batch';
6
8
  export type {
7
9
  BatchError as Experimental_BatchError,
8
10
  BatchProvider as Experimental_BatchProvider,
9
11
  BatchReference as Experimental_BatchReference,
10
12
  BatchStatus as Experimental_BatchStatus,
13
+ CancelBatchOptions as Experimental_CancelBatchOptions,
14
+ CancelBatchResult as Experimental_CancelBatchResult,
11
15
  GetBatchResultsOptions as Experimental_GetBatchResultsOptions,
12
16
  GetBatchStatusOptions as Experimental_GetBatchStatusOptions,
17
+ ListBatchesOptions as Experimental_ListBatchesOptions,
18
+ ListBatchesResult as Experimental_ListBatchesResult,
13
19
  StartBatchOptions as Experimental_StartBatchOptions,
14
20
  StartBatchResult as Experimental_StartBatchResult,
15
21
  Batch as Experimental_Batch,
@@ -1,3 +1,8 @@
1
+ // atob needs to be invoked as a function call, not as a method call.
2
+ // Otherwise Cloudflare will throw a
3
+ // "TypeError: Illegal invocation: function called with incorrect this reference"
4
+ const { atob } = globalThis;
5
+
1
6
  /**
2
7
  * Converts a data URL of type text/* to a text string.
3
8
  */
@@ -10,7 +15,7 @@ export function getTextFromDataUrl(dataUrl: string): string {
10
15
  }
11
16
 
12
17
  try {
13
- return globalThis.atob(base64Content);
18
+ return atob(base64Content);
14
19
  } catch {
15
20
  throw new Error(`Error decoding data URL`);
16
21
  }