refactor: use generateId() for id creation (#736)

This commit is contained in:
Jeremy 2025-01-28 18:51:04 +05:30 committed by GitHub
parent 085f4a8ac4
commit 2d47ffbd77
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 20 additions and 75 deletions

View file

@ -7,7 +7,7 @@ import useSWR, { useSWRConfig } from 'swr';
import { ChatHeader } from '@/components/chat-header';
import type { Vote } from '@/lib/db/schema';
import { fetcher } from '@/lib/utils';
import { fetcher, generateUUID } from '@/lib/utils';
import { Block } from './block';
import { MultimodalInput } from './multimodal-input';
@ -45,6 +45,8 @@ export function Chat({
body: { id, modelId: selectedModelId },
initialMessages,
experimental_throttle: 100,
sendExtraMessageFields: true,
generateId: generateUUID,
onFinish: () => {
mutate('/api/history');
},

View file

@ -5,7 +5,6 @@ import { useEffect, useRef } from 'react';
import { blockDefinitions, BlockKind } from './block';
import { Suggestion } from '@/lib/db/schema';
import { initialBlockData, useBlock } from '@/hooks/use-block';
import { useUserMessageId } from '@/hooks/use-user-message-id';
export type DataStreamDelta = {
type:
@ -17,14 +16,12 @@ export type DataStreamDelta = {
| 'suggestion'
| 'clear'
| 'finish'
| 'user-message-id'
| 'kind';
content: string | Suggestion;
};
export function DataStreamHandler({ id }: { id: string }) {
const { data: dataStream } = useChat({ id });
const { setUserMessageIdFromServer } = useUserMessageId();
const { block, setBlock, setMetadata } = useBlock();
const lastProcessedIndex = useRef(-1);
@ -35,11 +32,6 @@ export function DataStreamHandler({ id }: { id: string }) {
lastProcessedIndex.current = dataStream.length - 1;
(newDeltas as DataStreamDelta[]).forEach((delta: DataStreamDelta) => {
if (delta.type === 'user-message-id') {
setUserMessageIdFromServer(delta.content as string);
return;
}
const blockDefinition = blockDefinitions.find(
(blockDefinition) => blockDefinition.kind === block.kind,
);
@ -97,7 +89,7 @@ export function DataStreamHandler({ id }: { id: string }) {
}
});
});
}, [dataStream, setBlock, setUserMessageIdFromServer, setMetadata, block]);
}, [dataStream, setBlock, setMetadata, block]);
return null;
}

View file

@ -4,7 +4,6 @@ import { useSWRConfig } from 'swr';
import { useCopyToClipboard } from 'usehooks-ts';
import type { Vote } from '@/lib/db/schema';
import { getMessageIdFromAnnotations } from '@/lib/utils';
import { CopyIcon, ThumbDownIcon, ThumbUpIcon } from './icons';
import { Button } from './ui/button';
@ -62,13 +61,11 @@ export function PureMessageActions({
disabled={vote?.isUpvoted}
variant="outline"
onClick={async () => {
const messageId = getMessageIdFromAnnotations(message);
const upvote = fetch('/api/vote', {
method: 'PATCH',
body: JSON.stringify({
chatId,
messageId,
messageId: message.id,
type: 'up',
}),
});
@ -116,13 +113,11 @@ export function PureMessageActions({
variant="outline"
disabled={vote && !vote.isUpvoted}
onClick={async () => {
const messageId = getMessageIdFromAnnotations(message);
const downvote = fetch('/api/vote', {
method: 'PATCH',
body: JSON.stringify({
chatId,
messageId,
messageId: message.id,
type: 'down',
}),
});

View file

@ -6,7 +6,6 @@ import { Dispatch, SetStateAction, useEffect, useRef, useState } from 'react';
import { Textarea } from './ui/textarea';
import { deleteTrailingMessages } from '@/app/(chat)/actions';
import { toast } from 'sonner';
import { useUserMessageId } from '@/hooks/use-user-message-id';
export type MessageEditorProps = {
message: Message;
@ -25,7 +24,6 @@ export function MessageEditor({
setMessages,
reload,
}: MessageEditorProps) {
const { userMessageIdFromServer } = useUserMessageId();
const [isSubmitting, setIsSubmitting] = useState<boolean>(false);
const [draftContent, setDraftContent] = useState<string>(message.content);
@ -74,16 +72,9 @@ export function MessageEditor({
disabled={isSubmitting}
onClick={async () => {
setIsSubmitting(true);
const messageId = userMessageIdFromServer ?? message.id;
if (!messageId) {
toast.error('Something went wrong, please try again!');
setIsSubmitting(false);
return;
}
await deleteTrailingMessages({
id: messageId,
id: message.id,
});
setMessages((messages) => {