chore: update to ai sdk v5 beta (#1074)
This commit is contained in:
parent
7d8e71383f
commit
4c281fe09d
54 changed files with 1372 additions and 1060 deletions
|
|
@ -1,6 +1,6 @@
|
|||
'use client';
|
||||
|
||||
import type { Attachment, UIMessage } from 'ai';
|
||||
import { DefaultChatTransport } from 'ai';
|
||||
import { useChat } from '@ai-sdk/react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import useSWR, { useSWRConfig } from 'swr';
|
||||
|
|
@ -20,6 +20,8 @@ import { useSearchParams } from 'next/navigation';
|
|||
import { useChatVisibility } from '@/hooks/use-chat-visibility';
|
||||
import { useAutoResume } from '@/hooks/use-auto-resume';
|
||||
import { ChatSDKError } from '@/lib/errors';
|
||||
import type { Attachment, ChatMessage } from '@/lib/types';
|
||||
import { useDataStream } from './data-stream-provider';
|
||||
|
||||
export function Chat({
|
||||
id,
|
||||
|
|
@ -31,45 +33,54 @@ export function Chat({
|
|||
autoResume,
|
||||
}: {
|
||||
id: string;
|
||||
initialMessages: Array<UIMessage>;
|
||||
initialMessages: ChatMessage[];
|
||||
initialChatModel: string;
|
||||
initialVisibilityType: VisibilityType;
|
||||
isReadonly: boolean;
|
||||
session: Session;
|
||||
autoResume: boolean;
|
||||
}) {
|
||||
const { mutate } = useSWRConfig();
|
||||
|
||||
const { visibilityType } = useChatVisibility({
|
||||
chatId: id,
|
||||
initialVisibilityType,
|
||||
});
|
||||
|
||||
const { mutate } = useSWRConfig();
|
||||
const { setDataStream } = useDataStream();
|
||||
|
||||
const [input, setInput] = useState<string>('');
|
||||
|
||||
const {
|
||||
messages,
|
||||
setMessages,
|
||||
handleSubmit,
|
||||
input,
|
||||
setInput,
|
||||
append,
|
||||
sendMessage,
|
||||
status,
|
||||
stop,
|
||||
reload,
|
||||
experimental_resume,
|
||||
data,
|
||||
} = useChat({
|
||||
regenerate,
|
||||
resumeStream,
|
||||
} = useChat<ChatMessage>({
|
||||
id,
|
||||
initialMessages,
|
||||
messages: initialMessages,
|
||||
experimental_throttle: 100,
|
||||
sendExtraMessageFields: true,
|
||||
generateId: generateUUID,
|
||||
fetch: fetchWithErrorHandlers,
|
||||
experimental_prepareRequestBody: (body) => ({
|
||||
id,
|
||||
message: body.messages.at(-1),
|
||||
selectedChatModel: initialChatModel,
|
||||
selectedVisibilityType: visibilityType,
|
||||
transport: new DefaultChatTransport({
|
||||
api: '/api/chat',
|
||||
fetch: fetchWithErrorHandlers,
|
||||
prepareSendMessagesRequest({ messages, id, body }) {
|
||||
return {
|
||||
body: {
|
||||
id,
|
||||
message: messages.at(-1),
|
||||
selectedChatModel: initialChatModel,
|
||||
selectedVisibilityType: visibilityType,
|
||||
...body,
|
||||
},
|
||||
};
|
||||
},
|
||||
}),
|
||||
onData: (dataPart) => {
|
||||
setDataStream((ds) => (ds ? [...ds, dataPart] : []));
|
||||
},
|
||||
onFinish: () => {
|
||||
mutate(unstable_serialize(getChatHistoryPaginationKey));
|
||||
},
|
||||
|
|
@ -90,15 +101,15 @@ export function Chat({
|
|||
|
||||
useEffect(() => {
|
||||
if (query && !hasAppendedQuery) {
|
||||
append({
|
||||
role: 'user',
|
||||
content: query,
|
||||
sendMessage({
|
||||
role: 'user' as const,
|
||||
parts: [{ type: 'text', text: query }],
|
||||
});
|
||||
|
||||
setHasAppendedQuery(true);
|
||||
window.history.replaceState({}, '', `/chat/${id}`);
|
||||
}
|
||||
}, [query, append, hasAppendedQuery, id]);
|
||||
}, [query, sendMessage, hasAppendedQuery, id]);
|
||||
|
||||
const { data: votes } = useSWR<Array<Vote>>(
|
||||
messages.length >= 2 ? `/api/vote?chatId=${id}` : null,
|
||||
|
|
@ -111,8 +122,7 @@ export function Chat({
|
|||
useAutoResume({
|
||||
autoResume,
|
||||
initialMessages,
|
||||
experimental_resume,
|
||||
data,
|
||||
resumeStream,
|
||||
setMessages,
|
||||
});
|
||||
|
||||
|
|
@ -133,7 +143,7 @@ export function Chat({
|
|||
votes={votes}
|
||||
messages={messages}
|
||||
setMessages={setMessages}
|
||||
reload={reload}
|
||||
regenerate={regenerate}
|
||||
isReadonly={isReadonly}
|
||||
isArtifactVisible={isArtifactVisible}
|
||||
/>
|
||||
|
|
@ -144,14 +154,13 @@ export function Chat({
|
|||
chatId={id}
|
||||
input={input}
|
||||
setInput={setInput}
|
||||
handleSubmit={handleSubmit}
|
||||
status={status}
|
||||
stop={stop}
|
||||
attachments={attachments}
|
||||
setAttachments={setAttachments}
|
||||
messages={messages}
|
||||
setMessages={setMessages}
|
||||
append={append}
|
||||
sendMessage={sendMessage}
|
||||
selectedVisibilityType={visibilityType}
|
||||
/>
|
||||
)}
|
||||
|
|
@ -162,15 +171,14 @@ export function Chat({
|
|||
chatId={id}
|
||||
input={input}
|
||||
setInput={setInput}
|
||||
handleSubmit={handleSubmit}
|
||||
status={status}
|
||||
stop={stop}
|
||||
attachments={attachments}
|
||||
setAttachments={setAttachments}
|
||||
append={append}
|
||||
sendMessage={sendMessage}
|
||||
messages={messages}
|
||||
setMessages={setMessages}
|
||||
reload={reload}
|
||||
regenerate={regenerate}
|
||||
votes={votes}
|
||||
isReadonly={isReadonly}
|
||||
selectedVisibilityType={visibilityType}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue