Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useChat chunk #3252

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/react/src/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const getStreamedResponse = async (
abortControllerRef: React.MutableRefObject<AbortController | null>,
generateId: IdGenerator,
streamProtocol: UseChatOptions['streamProtocol'],
onChunk: UseChatOptions['onChunk'],
onFinish: UseChatOptions['onFinish'],
onResponse: ((response: Response) => void | Promise<void>) | undefined,
onToolCall: UseChatOptions['onToolCall'] | undefined,
Expand Down Expand Up @@ -178,6 +179,7 @@ const getStreamedResponse = async (
mutateStreamData([...(existingData || []), ...(data || [])], false);
},
onToolCall,
onChunk,
onFinish,
generateId,
fetch,
Expand All @@ -203,6 +205,7 @@ export function useChat({
onResponse,
onFinish,
onError,
onChunk,
credentials,
headers,
body,
Expand Down Expand Up @@ -363,6 +366,7 @@ By default, it's set to 1, which means that only a single LLM call is made.
abortControllerRef,
generateId,
streamProtocol,
onChunk,
onFinish,
onResponse,
onToolCall,
Expand Down Expand Up @@ -420,6 +424,7 @@ By default, it's set to 1, which means that only a single LLM call is made.
api,
extraMetadataRef,
onResponse,
onChunk,
onFinish,
onError,
setError,
Expand Down
48 changes: 48 additions & 0 deletions packages/react/src/use-chat.ui.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @next/next/no-img-element */
import { withTestServer } from '@ai-sdk/provider-utils/test';
import {
StreamPartType,
formatStreamPart,
getTextFromDataUrl,
Message,
Expand Down Expand Up @@ -30,13 +31,18 @@ describe('stream data stream', () => {
};
}> = [];

let chunks: StreamPartType[] = [];

const TestComponent = () => {
const [id, setId] = React.useState<string>('first-id');
const { messages, append, error, data, isLoading } = useChat({
id,
onFinish: (message, options) => {
onFinishCalls.push({ message, options });
},
onChunk(event) {
chunks.push(event.chunk);
},
});

return (
Expand Down Expand Up @@ -70,12 +76,14 @@ describe('stream data stream', () => {
beforeEach(() => {
render(<TestComponent />);
onFinishCalls = [];
chunks = [];
});

afterEach(() => {
vi.restoreAllMocks();
cleanup();
onFinishCalls = [];
chunks = [];
});

it(
Expand Down Expand Up @@ -257,6 +265,46 @@ describe('stream data stream', () => {
),
);
});

it(
'options.onChunk',
withTestServer(
{
url: '/api/chat',
type: 'stream-values',
content: [
formatStreamPart('text', 'Hello'),
formatStreamPart('text', ','),
formatStreamPart('text', ' world'),
formatStreamPart('text', '.'),
],
},
async () => {
await userEvent.click(screen.getByTestId('do-append'));

await screen.findByTestId('message-1');

expect(chunks).toStrictEqual([
{
type: 'text',
value: 'Hello',
},
{
type: 'text',
value: ',',
},
{
type: 'text',
value: ' world',
},
{
type: 'text',
value: '.',
},
]);
},
),
);
});

describe('text stream', () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/ui-utils/src/call-chat-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export async function callChatApi({
restoreMessagesOnFailure,
onResponse,
onUpdate,
onChunk,
onFinish,
onToolCall,
generateId,
Expand All @@ -29,6 +30,7 @@ export async function callChatApi({
restoreMessagesOnFailure: () => void;
onResponse: ((response: Response) => void | Promise<void>) | undefined;
onUpdate: (newMessages: Message[], data: JSONValue[] | undefined) => void;
onChunk?: UseChatOptions['onChunk'];
onFinish: UseChatOptions['onFinish'];
onToolCall: UseChatOptions['onToolCall'];
generateId: IdGenerator;
Expand Down Expand Up @@ -116,6 +118,7 @@ export async function callChatApi({
abortControllerRef:
abortController != null ? { current: abortController() } : undefined,
update: onUpdate,
onChunk,
onToolCall,
onFinish({ message, finishReason, usage }) {
if (onFinish && message != null) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ui-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ export { readDataStream } from './read-data-stream';
export { asSchema, jsonSchema, zodSchema } from './schema';
export type { Schema } from './schema';
export { formatStreamPart, parseStreamPart } from './stream-parts';
export type { StreamPart, StreamString } from './stream-parts';
export type { StreamPart, StreamString, StreamPartType } from './stream-parts';
7 changes: 6 additions & 1 deletion packages/ui-utils/src/process-data-protocol-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export async function processDataProtocolResponse({
reader,
abortControllerRef,
update,
onChunk,
onToolCall,
onFinish,
generateId = generateIdFunction,
Expand All @@ -47,6 +48,7 @@ export async function processDataProtocolResponse({
};
update: (newMessages: Message[], data: JSONValue[] | undefined) => void;
onToolCall?: UseChatOptions['onToolCall'];
onChunk?: UseChatOptions['onChunk'];
onFinish?: (options: {
message: Message | undefined;
finishReason: LanguageModelV1FinishReason;
Expand Down Expand Up @@ -89,9 +91,12 @@ export async function processDataProtocolResponse({
let finishReason: LanguageModelV1FinishReason = 'unknown';

// we create a map of each prefix, and for each prefixed message we push to the map
for await (const { type, value } of readDataStream(reader, {
for await (const chunk of readDataStream(reader, {
isAborted: () => abortControllerRef?.current === null,
})) {
const { type, value } = chunk;
await onChunk?.({ chunk });

if (type === 'error') {
throw new Error(value);
}
Expand Down
6 changes: 6 additions & 0 deletions packages/ui-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FetchFunction } from '@ai-sdk/provider-utils';
import { CompletionTokenUsage } from './duplicated/token-usage';
import { ToolCall as CoreToolCall } from './duplicated/tool-call';
import { ToolResult as CoreToolResult } from './duplicated/tool-result';
import { StreamPartType } from './stream-parts';

export * from './use-assistant-types';

Expand Down Expand Up @@ -448,6 +449,11 @@ either synchronously or asynchronously.
*/
onError?: (error: Error) => void;

/**
* Callback that is called for each chunk of the stream.
*/
onChunk?: (event: { chunk: StreamPartType }) => Promise<void> | void;

/**
* A way to provide a function that is going to be used for ids for messages.
* If not provided nanoid is used by default.
Expand Down
Loading