Skip to content

Commit e7dc6c7

Browse files
authored
chore (ai): remove onResponse callback (#6281)
## Background `onResponse` callback is unrelated to streaming and should not be used. ## Summary Remove `onResponse` callback from `useChat` and `useCompletion`
1 parent a34eb39 commit e7dc6c7

File tree

11 files changed

+5
-38
lines changed

11 files changed

+5
-38
lines changed

‎.changeset/heavy-ducks-join.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 onResponse callback

‎packages/ai/src/ui/call-chat-api.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export async function callChatApi<MESSAGE_METADATA>({
2424
credentials,
2525
headers,
2626
abortController,
27-
onResponse,
2827
onUpdate,
2928
onFinish,
3029
onToolCall,
@@ -40,7 +39,6 @@ export async function callChatApi<MESSAGE_METADATA>({
4039
credentials: RequestCredentials | undefined;
4140
headers: HeadersInit | undefined;
4241
abortController: (() => AbortController | null) | undefined;
43-
onResponse: ((response: Response) => void | Promise<void>) | undefined;
4442
onUpdate: (options: { message: UIMessage<MESSAGE_METADATA> }) => void;
4543
onFinish: UseChatOptions<MESSAGE_METADATA>['onFinish'];
4644
onToolCall: UseChatOptions<MESSAGE_METADATA>['onToolCall'];
@@ -72,10 +70,6 @@ export async function callChatApi<MESSAGE_METADATA>({
7270
credentials,
7371
});
7472

75-
if (onResponse != null) {
76-
await onResponse(response);
77-
}
78-
7973
if (!response.ok) {
8074
throw new Error(
8175
(await response.text()) ?? 'Failed to fetch the chat response.',

‎packages/ai/src/ui/call-completion-api.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export async function callCompletionApi({
2020
setLoading,
2121
setError,
2222
setAbortController,
23-
onResponse,
2423
onFinish,
2524
onError,
2625
fetch = getOriginalFetch(),
@@ -35,7 +34,6 @@ export async function callCompletionApi({
3534
setLoading: (loading: boolean) => void;
3635
setError: (error: Error | undefined) => void;
3736
setAbortController: (abortController: AbortController | null) => void;
38-
onResponse: ((response: Response) => void | Promise<void>) | undefined;
3937
onFinish: ((prompt: string, completion: string) => void) | undefined;
4038
onError: ((error: Error) => void) | undefined;
4139
fetch: ReturnType<typeof getOriginalFetch> | undefined;
@@ -66,14 +64,6 @@ export async function callCompletionApi({
6664
throw err;
6765
});
6866

69-
if (onResponse) {
70-
try {
71-
await onResponse(response);
72-
} catch (err) {
73-
throw err;
74-
}
75-
}
76-
7767
if (!response.ok) {
7868
throw new Error(
7969
(await response.text()) ?? 'Failed to fetch the chat response.',

‎packages/ai/src/ui/use-chat.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ export type UseChatOptions<MESSAGE_METADATA = unknown> = {
6161
toolCall: ToolCall<string, unknown>;
6262
}) => void | Promise<unknown> | unknown;
6363

64-
/**
65-
* Callback function to be called when the API response is received.
66-
*/
67-
onResponse?: (response: Response) => void | Promise<void>;
68-
6964
/**
7065
* Optional callback function that is called when the assistant message is finished streaming.
7166
*

‎packages/ai/src/ui/use-completion.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ export type UseCompletionOptions = {
3535
*/
3636
initialCompletion?: string;
3737

38-
/**
39-
* Callback function to be called when the API response is received.
40-
*/
41-
onResponse?: (response: Response) => void | Promise<void>;
42-
4338
/**
4439
* Callback function to be called when the completion is finished streaming.
4540
*/

‎packages/react/src/use-chat.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ export function useChat<MESSAGE_METADATA>({
127127
experimental_prepareRequestBody,
128128
maxSteps = 1,
129129
streamProtocol = 'data',
130-
onResponse,
131130
onFinish,
132131
onError,
133132
credentials,
@@ -256,7 +255,6 @@ Default is undefined, which disables throttling.
256255
...chatRequest.headers,
257256
},
258257
abortController: () => abortControllerRef.current,
259-
onResponse,
260258
onUpdate({ message }) {
261259
mutateStatus('streaming');
262260

@@ -320,7 +318,6 @@ Default is undefined, which disables throttling.
320318
mutateStatus,
321319
api,
322320
extraMetadataRef,
323-
onResponse,
324321
onFinish,
325322
onError,
326323
setError,

‎packages/react/src/use-completion.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ export function useCompletion({
7171
body,
7272
streamProtocol = 'data',
7373
fetch,
74-
onResponse,
7574
onFinish,
7675
onError,
7776
experimental_throttle: throttleWaitMs,
@@ -138,7 +137,6 @@ export function useCompletion({
138137
setLoading: mutateLoading,
139138
setError,
140139
setAbortController,
141-
onResponse,
142140
onFinish,
143141
onError,
144142
}),
@@ -148,7 +146,6 @@ export function useCompletion({
148146
api,
149147
extraMetadataRef,
150148
setAbortController,
151-
onResponse,
152149
onFinish,
153150
onError,
154151
setError,

‎packages/svelte/src/chat.svelte.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ export class Chat<MESSAGE_METADATA = unknown> {
236236
...chatRequest.headers,
237237
},
238238
abortController: () => abortController,
239-
onResponse: this.#options.onResponse,
240239
onUpdate: ({ message }) => {
241240
this.#store.status = 'streaming';
242241

‎packages/svelte/src/completion.svelte.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ export class Completion {
109109
setAbortController: abortController => {
110110
this.#abortController = abortController ?? undefined;
111111
},
112-
onResponse: this.#options.onResponse,
113112
onFinish: this.#options.onFinish,
114113
onError: this.#options.onError,
115114
});

‎packages/vue/src/use-chat.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ export function useChat<MESSAGE_METADATA = unknown>(
106106
initialMessages = [],
107107
initialInput = '',
108108
streamProtocol = 'data',
109-
onResponse,
110109
onFinish,
111110
onError,
112111
credentials,
@@ -207,7 +206,6 @@ export function useChat<MESSAGE_METADATA = unknown>(
207206
},
208207
abortController: () => abortController,
209208
credentials,
210-
onResponse,
211209
onUpdate({ message }) {
212210
status.value = 'streaming';
213211

0 commit comments

Comments
 (0)