Skip to content

Commit c60f895

Browse files
authored
chore (ai): remove useChat keepLastMessageOnError (#6114)
## Background `keepLastMessageOnError` is deprecated and should be removed in AI SDK 5. ## Summary Remove `keepLastMessageOnError` and simplify adjacent code.
1 parent 928fadf commit c60f895

File tree

6 files changed

+14
-42
lines changed

6 files changed

+14
-42
lines changed

.changeset/funny-mayflies-yawn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': major
3+
---
4+
5+
chore (ai): remove useChat keepLastMessageOnError

packages/ai/core/types/ui-messages.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,6 @@ Additional data to be sent to the API endpoint.
223223

224224
export type UseChatOptions = {
225225
/**
226-
Keeps the last message when an error happens. Defaults to `true`.
227-
228-
@deprecated This option will be removed in the next major release.
229-
*/
230-
keepLastMessageOnError?: boolean;
231-
232-
/**
233226
* The API endpoint that accepts a `{ messages: Message[] }` object and returns
234227
* a stream of tokens of the AI chat response. Defaults to `/api/chat`.
235228
*/

packages/ai/core/util/call-chat-api.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { JSONValue } from '@ai-sdk/provider';
2+
import { IdGenerator, UIMessage, UseChatOptions } from '../types';
13
import { processChatResponse } from './process-chat-response';
24
import { processChatTextResponse } from './process-chat-text-response';
3-
import { IdGenerator, UIMessage, UseChatOptions } from '../types';
4-
import { JSONValue } from '@ai-sdk/provider';
5+
import { request } from 'http';
56

67
// use function to allow for mocking in tests:
78
const getOriginalFetch = () => fetch;
@@ -13,7 +14,6 @@ export async function callChatApi({
1314
credentials,
1415
headers,
1516
abortController,
16-
restoreMessagesOnFailure,
1717
onResponse,
1818
onUpdate,
1919
onFinish,
@@ -30,7 +30,6 @@ export async function callChatApi({
3030
credentials: RequestCredentials | undefined;
3131
headers: HeadersInit | undefined;
3232
abortController: (() => AbortController | null) | undefined;
33-
restoreMessagesOnFailure: () => void;
3433
onResponse: ((response: Response) => void | Promise<void>) | undefined;
3534
onUpdate: (options: {
3635
message: UIMessage;
@@ -45,9 +44,9 @@ export async function callChatApi({
4544
getCurrentDate: () => Date;
4645
requestType?: 'generate' | 'resume';
4746
}) {
48-
const request =
47+
const response =
4948
requestType === 'resume'
50-
? fetch(`${api}?chatId=${body.id}`, {
49+
? await fetch(`${api}?chatId=${body.id}`, {
5150
method: 'GET',
5251
headers: {
5352
'Content-Type': 'application/json',
@@ -56,7 +55,7 @@ export async function callChatApi({
5655
signal: abortController?.()?.signal,
5756
credentials,
5857
})
59-
: fetch(api, {
58+
: await fetch(api, {
6059
method: 'POST',
6160
body: JSON.stringify(body),
6261
headers: {
@@ -67,21 +66,11 @@ export async function callChatApi({
6766
credentials,
6867
});
6968

70-
const response = await request.catch(err => {
71-
restoreMessagesOnFailure();
72-
throw err;
73-
});
74-
75-
if (onResponse) {
76-
try {
77-
await onResponse(response);
78-
} catch (err) {
79-
throw err;
80-
}
69+
if (onResponse != null) {
70+
await onResponse(response);
8171
}
8272

8373
if (!response.ok) {
84-
restoreMessagesOnFailure();
8574
throw new Error(
8675
(await response.text()) ?? 'Failed to fetch the chat response.',
8776
);

packages/react/src/use-chat.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ export function useChat({
142142
body,
143143
generateId = generateIdFunc,
144144
fetch,
145-
keepLastMessageOnError = true,
146145
experimental_throttle: throttleWaitMs,
147146
...options
148147
}: UseChatOptions & {
@@ -303,11 +302,6 @@ Default is undefined, which disables throttling.
303302
...chatRequest.headers,
304303
},
305304
abortController: () => abortControllerRef.current,
306-
restoreMessagesOnFailure() {
307-
if (!keepLastMessageOnError) {
308-
throttledMutate(previousMessages, false);
309-
}
310-
},
311305
onResponse,
312306
onUpdate({ message, data, replaceLastMessage }) {
313307
mutateStatus('streaming');
@@ -391,7 +385,6 @@ Default is undefined, which disables throttling.
391385
abortControllerRef,
392386
generateId,
393387
fetch,
394-
keepLastMessageOnError,
395388
throttleWaitMs,
396389
chatId,
397390
getCurrentDate,

packages/svelte/src/chat.svelte.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
} from './chat-context.svelte.js';
2424

2525
export type ChatOptions = Readonly<
26-
Omit<UseChatOptions, 'keepLastMessageOnError'> & {
26+
UseChatOptions & {
2727
'~internal'?: {
2828
currentDate?: () => Date;
2929
};
@@ -266,7 +266,6 @@ export class Chat {
266266
...chatRequest.headers,
267267
},
268268
abortController: () => abortController,
269-
restoreMessagesOnFailure: () => {},
270269
onResponse: this.#options.onResponse,
271270
onUpdate: ({ message, data, replaceLastMessage }) => {
272271
this.#store.status = 'streaming';

packages/vue/src/use-chat.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ export function useChat(
125125
generateId = generateIdFunc,
126126
onToolCall,
127127
fetch,
128-
keepLastMessageOnError = true,
129128
maxSteps = 1,
130129
experimental_prepareRequestBody,
131130
...options
@@ -259,12 +258,6 @@ export function useChat(
259258
}
260259
},
261260
onFinish,
262-
restoreMessagesOnFailure() {
263-
// Restore the previous messages if the request fails.
264-
if (!keepLastMessageOnError) {
265-
mutate(previousMessages);
266-
}
267-
},
268261
generateId,
269262
onToolCall,
270263
fetch,

0 commit comments

Comments
 (0)