ai 6.0.176 → 6.0.177

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 CHANGED
@@ -1,5 +1,12 @@
1
1
  # ai
2
2
 
3
+ ## 6.0.177
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [5c73af8]
8
+ - @ai-sdk/gateway@3.0.112
9
+
3
10
  ## 6.0.176
4
11
 
5
12
  ### Patch Changes
package/dist/index.js CHANGED
@@ -1254,7 +1254,7 @@ function detectMediaType({
1254
1254
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
1255
1255
 
1256
1256
  // src/version.ts
1257
- var VERSION = true ? "6.0.176" : "0.0.0-test";
1257
+ var VERSION = true ? "6.0.177" : "0.0.0-test";
1258
1258
 
1259
1259
  // src/util/download/download.ts
1260
1260
  var download = async ({
package/dist/index.mjs CHANGED
@@ -1144,7 +1144,7 @@ import {
1144
1144
  } from "@ai-sdk/provider-utils";
1145
1145
 
1146
1146
  // src/version.ts
1147
- var VERSION = true ? "6.0.176" : "0.0.0-test";
1147
+ var VERSION = true ? "6.0.177" : "0.0.0-test";
1148
1148
 
1149
1149
  // src/util/download/download.ts
1150
1150
  var download = async ({
@@ -152,7 +152,7 @@ function detectMediaType({
152
152
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
153
153
 
154
154
  // src/version.ts
155
- var VERSION = true ? "6.0.176" : "0.0.0-test";
155
+ var VERSION = true ? "6.0.177" : "0.0.0-test";
156
156
 
157
157
  // src/util/download/download.ts
158
158
  var download = async ({
@@ -131,7 +131,7 @@ import {
131
131
  } from "@ai-sdk/provider-utils";
132
132
 
133
133
  // src/version.ts
134
- var VERSION = true ? "6.0.176" : "0.0.0-test";
134
+ var VERSION = true ? "6.0.177" : "0.0.0-test";
135
135
 
136
136
  // src/util/download/download.ts
137
137
  var download = async ({
@@ -8,10 +8,12 @@ description: Learn how to resume chatbot streams after client disconnects.
8
8
  `useChat` supports resuming ongoing streams after page reloads. Use this feature to build applications with long-running generations.
9
9
 
10
10
  <Note type="warning">
11
- Stream resumption is not compatible with abort functionality. Closing a tab or
12
- refreshing the page triggers an abort signal that will break the resumption
13
- mechanism. Do not use `resume: true` if you need abort functionality in your
14
- application. See
11
+ In a resumable stream setup, client-side aborts are treated as disconnects.
12
+ Closing a tab, refreshing the page, or calling `stop()` only closes the
13
+ current HTTP connection and should not cancel the underlying generation. To
14
+ let users stop generation, add a dedicated stop endpoint that persists the
15
+ partial response, cancels the active work, and clears the active stream. See
16
+ [Stop an Active Resumable Stream](#stop-an-active-resumable-stream) and
15
17
  [troubleshooting](/docs/troubleshooting/abort-breaks-resumable-streams) for
16
18
  more details.
17
19
  </Note>
@@ -250,9 +252,148 @@ This lets you:
250
252
  - Add query parameters or custom paths
251
253
  - Integrate with different backend architectures
252
254
 
255
+ ## Stop an Active Resumable Stream
256
+
257
+ `useChat` includes a `stop()` function that aborts the current client request. In a resumable stream setup, that abort is a disconnect signal, not a request to stop generation.
258
+
259
+ Stream resumption lets a client reconnect to an active stream after the original connection closes. To make that possible, the server keeps the stream running even when no client is actively consuming it. If the user refreshes the page, closes the tab, loses their connection, or navigates away, the client can reconnect later with `resumeStream()`.
260
+
261
+ Because of this, a client-side abort (e.g. closing the page or refreshing) only closes the current HTTP connection. It is not a request to cancel the underlying work. If your stop button only calls `stop()`, the model request, background job, workflow, or stream writer can continue running, and the client can reconnect to the same active stream.
262
+
263
+ To support an explicit stop button, create a dedicated stop endpoint. The endpoint should accept the current assistant message from the client, persist that partial response, cancel the work that is producing the stream, and clear the active stream record for the chat.
264
+
265
+ Stream resumption also needs your application to store a reference from the chat to the stream that can be resumed. This guide calls that reference `activeStreamId`. The resume endpoint uses it to find the stream to reconnect to. The stop endpoint uses the same value to find the work to cancel, and to avoid clearing a newer stream that may have started while the stop request was in flight.
266
+
267
+ ### Client-side: send the current assistant message
268
+
269
+ The chat setup is the same as the resumable stream setup above. To add stop behavior, send the latest partial assistant message to your stop endpoint before stopping the local chat stream.
270
+
271
+ When the client knows the active stream ID, include it in the request. This lets the server ignore stale stop requests that arrive after a newer stream has already started.
272
+
273
+ ```tsx filename="app/chat/[chatId]/chat.tsx"
274
+ 'use client';
275
+
276
+ import { useChat } from '@ai-sdk/react';
277
+ import { type UIMessage } from 'ai';
278
+
279
+ export function Chat({
280
+ chatData,
281
+ resume = false,
282
+ }: {
283
+ chatData: {
284
+ id: string;
285
+ messages: UIMessage[];
286
+ activeStreamId?: string | null;
287
+ };
288
+ resume?: boolean;
289
+ }) {
290
+ const chat = useChat({
291
+ id: chatData.id,
292
+ messages: chatData.messages,
293
+ resume,
294
+ });
295
+
296
+ const stop = () => {
297
+ const lastMessage = chat.messages[chat.messages.length - 1];
298
+ const assistantMessage =
299
+ lastMessage?.role === 'assistant' ? lastMessage : undefined;
300
+
301
+ void fetch(`/api/chat/${chatData.id}/stop`, {
302
+ method: 'POST',
303
+ headers: { 'Content-Type': 'application/json' },
304
+ body: JSON.stringify(
305
+ assistantMessage || chatData.activeStreamId
306
+ ? {
307
+ assistantMessage,
308
+ activeStreamId: chatData.activeStreamId,
309
+ }
310
+ : {},
311
+ ),
312
+ });
313
+
314
+ void chat.stop();
315
+ };
316
+
317
+ return <button onClick={stop}>Stop</button>;
318
+ }
319
+ ```
320
+
321
+ The stop request tells your server to cancel the active work. `chat.stop()` stops the local client from reading more chunks.
322
+
323
+ ### Server-side: stop the active work and clear the stream
324
+
325
+ The stop endpoint should:
326
+
327
+ 1. Load the chat and read its `activeStreamId`
328
+ 2. Persist the assistant snapshot if one was sent
329
+ 3. Cancel the work that is producing the stream
330
+ 4. Clear `activeStreamId` only if it still points to the same stream
331
+
332
+ ```ts filename="app/api/chat/[id]/stop/route.ts"
333
+ import { readChat, saveChat } from '@util/chat-store';
334
+ import { type UIMessage } from 'ai';
335
+
336
+ type StopRequest = {
337
+ activeStreamId?: string | null;
338
+ assistantMessage?: UIMessage;
339
+ };
340
+
341
+ export async function POST(
342
+ req: Request,
343
+ { params }: { params: Promise<{ id: string }> },
344
+ ) {
345
+ const { id } = await params;
346
+ const chat = await readChat(id);
347
+
348
+ if (chat.activeStreamId == null) {
349
+ return Response.json({ success: true });
350
+ }
351
+
352
+ const activeStreamId = chat.activeStreamId;
353
+ const body = (await req.json().catch(() => ({}))) as StopRequest;
354
+
355
+ if (
356
+ body.activeStreamId != null &&
357
+ body.activeStreamId !== activeStreamId
358
+ ) {
359
+ return Response.json({ success: true });
360
+ }
361
+
362
+ if (body.assistantMessage) {
363
+ await saveAssistantSnapshot({
364
+ chatId: id,
365
+ message: body.assistantMessage,
366
+ });
367
+ }
368
+
369
+ await markStreamAsStopped(activeStreamId);
370
+ await cancelActiveWork(activeStreamId);
371
+
372
+ const latestChat = await readChat(id);
373
+ if (latestChat.activeStreamId === activeStreamId) {
374
+ await saveChat({ id, activeStreamId: null });
375
+ }
376
+
377
+ return Response.json({ success: true });
378
+ }
379
+ ```
380
+
381
+ `markStreamAsStopped` and `cancelActiveWork` depend on your backend. In a Redis-backed resumable stream setup, you might close the stored stream and abort the model request that is writing to it. In a workflow setup, you might cancel the workflow run that owns the stream. In a job queue setup, you might cancel the job or write a cancellation flag that the job checks.
382
+
383
+ The `activeStreamId` can identify replay state, producer state, or both. If those are separate in your system, store enough information with the chat to cancel the producer that writes to the stream.
384
+
385
+ Persist the assistant snapshot as an insert or merge. Avoid overwriting a newer server-written message with an older client snapshot.
386
+
387
+ ### Keep navigation separate from stop
388
+
389
+ Do not call the stop endpoint from route cleanup code. Route cleanup is a disconnect, not an explicit stop. The active stream should remain resumable when the user refreshes the page or navigates away.
390
+
391
+ Only call the stop endpoint for an explicit user action, such as pressing a stop button.
392
+
393
+ After a user stops a stream, avoid automatic reconnect attempts for that chat until the user sends another message or explicitly retries. Otherwise the client can reconnect before cancellation has finished.
394
+
253
395
  ## Important considerations
254
396
 
255
- - **Incompatibility with abort**: Stream resumption is not compatible with abort functionality. Closing a tab or refreshing the page triggers an abort signal that will break the resumption mechanism. Do not use `resume: true` if you need abort functionality in your application
256
397
  - **Stream expiration**: Streams in Redis expire after a set time (configurable in the `resumable-stream` package)
257
398
  - **Multiple clients**: Multiple clients can connect to the same stream simultaneously
258
399
  - **Error handling**: When no active stream exists, the GET handler returns a 204 (No Content) status code
@@ -1,46 +1,60 @@
1
1
  ---
2
- title: Abort breaks resumable streams
3
- description: Troubleshooting stream resumption failures when using abort functionality
2
+ title: Abort and resumable streams
3
+ description: Troubleshooting abort and stop behavior with resumable streams
4
4
  ---
5
5
 
6
- # Abort breaks resumable streams
6
+ # Abort and resumable streams
7
7
 
8
8
  ## Issue
9
9
 
10
- When using `useChat` with `resume: true` for stream resumption, the abort functionality breaks. Closing a tab, refreshing the page, or calling the `stop()` function will trigger an abort signal that interferes with the resumption mechanism, preventing streams from being properly resumed.
10
+ When using `useChat` with `resume: true` for stream resumption, client-side aborts are treated as disconnects. Closing a tab, refreshing the page, navigating away, or calling `stop()` closes the current HTTP connection, but it should not cancel the underlying generation.
11
+
12
+ If your application passes the request abort signal through to the model call, disconnects can cancel the work that stream resumption expects to keep running. If your stop button only calls `stop()`, the server-side generation can continue and the client may reconnect to the same active stream.
11
13
 
12
14
  ```tsx
13
- // This configuration will cause conflicts
14
15
  const { messages, stop } = useChat({
15
16
  id: chatId,
16
17
  resume: true, // Stream resumption enabled
17
18
  });
18
19
 
19
- // Closing the tab will trigger abort and stop resumption
20
+ // stop() only aborts the current client request.
21
+ // It is not a server-side cancellation request.
20
22
  ```
21
23
 
22
24
  ## Background
23
25
 
24
- When a page is closed or refreshed, the browser automatically sends an abort signal, which breaks the resumption flow.
26
+ Stream resumption lets the client reconnect to an active stream after the original connection closes. To support that, the server needs to keep the stream producer running even when no client is currently connected.
27
+
28
+ This means route cleanup, page unloads, and network disconnects should be handled as resumable disconnects. Explicit user cancellation needs a separate server-side signal that cancels the active producer and clears the stored active stream reference.
25
29
 
26
- ## Current limitations
30
+ ## Solution
27
31
 
28
- We're aware of this incompatibility and are exploring solutions. **In the meantime, please choose either stream resumption or abort functionality based on your application's requirements**, but not both.
32
+ Use `resume: true` for reconnecting after disconnects, and add a dedicated stop endpoint for explicit user cancellation.
29
33
 
30
- ### Option 1: Use stream resumption without abort
34
+ The stop endpoint should:
31
35
 
32
- If you need to support long-running generations that persist across page reloads:
36
+ 1. Load the chat and read its active stream ID
37
+ 2. Persist the latest partial assistant message if the client sends one
38
+ 3. Cancel the work that is producing the stream
39
+ 4. Clear the active stream reference only if it still points to the same stream
40
+
41
+ On the client, call your stop endpoint before stopping the local chat stream:
33
42
 
34
43
  ```tsx
35
- const { messages, sendMessage } = useChat({
44
+ const chat = useChat({
36
45
  id: chatId,
37
46
  resume: true,
38
47
  });
48
+
49
+ async function stopStream() {
50
+ await fetch(`/api/chat/${chatId}/stop`, { method: 'POST' });
51
+ chat.stop();
52
+ }
39
53
  ```
40
54
 
41
- ### Option 2: Use abort without stream resumption
55
+ Keep navigation separate from stop behavior. Do not call the stop endpoint from route cleanup code, page unload handlers, or component unmount cleanup. Those events are disconnects that should remain resumable.
42
56
 
43
- If you need to allow users to stop streams manually:
57
+ If you do not need stream resumption and only want client-side cancellation, disable `resume` and use `stop()` directly:
44
58
 
45
59
  ```tsx
46
60
  const { messages, sendMessage, stop } = useChat({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "6.0.176",
3
+ "version": "6.0.177",
4
4
  "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.",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -45,7 +45,7 @@
45
45
  },
46
46
  "dependencies": {
47
47
  "@opentelemetry/api": "1.9.0",
48
- "@ai-sdk/gateway": "3.0.111",
48
+ "@ai-sdk/gateway": "3.0.112",
49
49
  "@ai-sdk/provider": "3.0.10",
50
50
  "@ai-sdk/provider-utils": "4.0.27"
51
51
  },