feat: edit and resubmit messages (#592)
This commit is contained in:
parent
2e479ccce7
commit
64d1aad2bb
12 changed files with 302 additions and 25 deletions
|
|
@ -4,6 +4,10 @@ import { type CoreUserMessage, generateText } from 'ai';
|
||||||
import { cookies } from 'next/headers';
|
import { cookies } from 'next/headers';
|
||||||
|
|
||||||
import { customModel } from '@/lib/ai';
|
import { customModel } from '@/lib/ai';
|
||||||
|
import {
|
||||||
|
deleteMessagesByChatIdAfterTimestamp,
|
||||||
|
getMessageById,
|
||||||
|
} from '@/lib/db/queries';
|
||||||
|
|
||||||
export async function saveModelId(model: string) {
|
export async function saveModelId(model: string) {
|
||||||
const cookieStore = await cookies();
|
const cookieStore = await cookies();
|
||||||
|
|
@ -27,3 +31,12 @@ export async function generateTitleFromUserMessage({
|
||||||
|
|
||||||
return title;
|
return title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function deleteTrailingMessages({ id }: { id: string }) {
|
||||||
|
const [message] = await getMessageById({ id });
|
||||||
|
|
||||||
|
await deleteMessagesByChatIdAfterTimestamp({
|
||||||
|
chatId: message.chatId,
|
||||||
|
timestamp: message.createdAt,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -81,14 +81,21 @@ export async function POST(request: Request) {
|
||||||
await saveChat({ id, userId: session.user.id, title });
|
await saveChat({ id, userId: session.user.id, title });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const userMessageId = generateUUID();
|
||||||
|
|
||||||
await saveMessages({
|
await saveMessages({
|
||||||
messages: [
|
messages: [
|
||||||
{ ...userMessage, id: generateUUID(), createdAt: new Date(), chatId: id },
|
{ ...userMessage, id: userMessageId, createdAt: new Date(), chatId: id },
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
const streamingData = new StreamData();
|
const streamingData = new StreamData();
|
||||||
|
|
||||||
|
streamingData.append({
|
||||||
|
type: 'user-message-id',
|
||||||
|
content: userMessageId,
|
||||||
|
});
|
||||||
|
|
||||||
const result = streamText({
|
const result = streamText({
|
||||||
model: customModel(model.apiIdentifier),
|
model: customModel(model.apiIdentifier),
|
||||||
system: systemPrompt,
|
system: systemPrompt,
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import { UIBlock } from './block';
|
||||||
import { PreviewMessage } from './message';
|
import { PreviewMessage } from './message';
|
||||||
import { useScrollToBottom } from './use-scroll-to-bottom';
|
import { useScrollToBottom } from './use-scroll-to-bottom';
|
||||||
import { Vote } from '@/lib/db/schema';
|
import { Vote } from '@/lib/db/schema';
|
||||||
import { Message } from 'ai';
|
import { ChatRequestOptions, Message } from 'ai';
|
||||||
|
|
||||||
interface BlockMessagesProps {
|
interface BlockMessagesProps {
|
||||||
chatId: string;
|
chatId: string;
|
||||||
|
|
@ -12,6 +12,12 @@ interface BlockMessagesProps {
|
||||||
isLoading: boolean;
|
isLoading: boolean;
|
||||||
votes: Array<Vote> | undefined;
|
votes: Array<Vote> | undefined;
|
||||||
messages: Array<Message>;
|
messages: Array<Message>;
|
||||||
|
setMessages: (
|
||||||
|
messages: Message[] | ((messages: Message[]) => Message[]),
|
||||||
|
) => void;
|
||||||
|
reload: (
|
||||||
|
chatRequestOptions?: ChatRequestOptions,
|
||||||
|
) => Promise<string | null | undefined>;
|
||||||
}
|
}
|
||||||
|
|
||||||
function PureBlockMessages({
|
function PureBlockMessages({
|
||||||
|
|
@ -21,6 +27,8 @@ function PureBlockMessages({
|
||||||
isLoading,
|
isLoading,
|
||||||
votes,
|
votes,
|
||||||
messages,
|
messages,
|
||||||
|
setMessages,
|
||||||
|
reload,
|
||||||
}: BlockMessagesProps) {
|
}: BlockMessagesProps) {
|
||||||
const [messagesContainerRef, messagesEndRef] =
|
const [messagesContainerRef, messagesEndRef] =
|
||||||
useScrollToBottom<HTMLDivElement>();
|
useScrollToBottom<HTMLDivElement>();
|
||||||
|
|
@ -43,6 +51,8 @@ function PureBlockMessages({
|
||||||
? votes.find((vote) => vote.messageId === message.id)
|
? votes.find((vote) => vote.messageId === message.id)
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
|
setMessages={setMessages}
|
||||||
|
reload={reload}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,7 @@ function PureBlock({
|
||||||
setBlock,
|
setBlock,
|
||||||
messages,
|
messages,
|
||||||
setMessages,
|
setMessages,
|
||||||
|
reload,
|
||||||
votes,
|
votes,
|
||||||
}: {
|
}: {
|
||||||
chatId: string;
|
chatId: string;
|
||||||
|
|
@ -82,6 +83,9 @@ function PureBlock({
|
||||||
},
|
},
|
||||||
chatRequestOptions?: ChatRequestOptions,
|
chatRequestOptions?: ChatRequestOptions,
|
||||||
) => void;
|
) => void;
|
||||||
|
reload: (
|
||||||
|
chatRequestOptions?: ChatRequestOptions,
|
||||||
|
) => Promise<string | null | undefined>;
|
||||||
}) {
|
}) {
|
||||||
const {
|
const {
|
||||||
data: documents,
|
data: documents,
|
||||||
|
|
@ -286,6 +290,8 @@ function PureBlock({
|
||||||
setBlock={setBlock}
|
setBlock={setBlock}
|
||||||
votes={votes}
|
votes={votes}
|
||||||
messages={messages}
|
messages={messages}
|
||||||
|
setMessages={setMessages}
|
||||||
|
reload={reload}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<form className="flex flex-row gap-2 relative items-end w-full px-4 pb-4">
|
<form className="flex flex-row gap-2 relative items-end w-full px-4 pb-4">
|
||||||
|
|
|
||||||
|
|
@ -36,8 +36,10 @@ export function Chat({
|
||||||
append,
|
append,
|
||||||
isLoading,
|
isLoading,
|
||||||
stop,
|
stop,
|
||||||
|
reload,
|
||||||
data: streamingData,
|
data: streamingData,
|
||||||
} = useChat({
|
} = useChat({
|
||||||
|
id,
|
||||||
body: { id, modelId: selectedModelId },
|
body: { id, modelId: selectedModelId },
|
||||||
initialMessages,
|
initialMessages,
|
||||||
onFinish: () => {
|
onFinish: () => {
|
||||||
|
|
@ -81,6 +83,8 @@ export function Chat({
|
||||||
isLoading={isLoading}
|
isLoading={isLoading}
|
||||||
votes={votes}
|
votes={votes}
|
||||||
messages={messages}
|
messages={messages}
|
||||||
|
setMessages={setMessages}
|
||||||
|
reload={reload}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<form className="flex mx-auto px-4 bg-background pb-4 md:pb-6 gap-2 w-full md:max-w-3xl">
|
<form className="flex mx-auto px-4 bg-background pb-4 md:pb-6 gap-2 w-full md:max-w-3xl">
|
||||||
|
|
@ -116,6 +120,7 @@ export function Chat({
|
||||||
setBlock={setBlock}
|
setBlock={setBlock}
|
||||||
messages={messages}
|
messages={messages}
|
||||||
setMessages={setMessages}
|
setMessages={setMessages}
|
||||||
|
reload={reload}
|
||||||
votes={votes}
|
votes={votes}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
|
||||||
113
components/message-editor.tsx
Normal file
113
components/message-editor.tsx
Normal file
|
|
@ -0,0 +1,113 @@
|
||||||
|
'use client';
|
||||||
|
|
||||||
|
import { ChatRequestOptions, Message } from 'ai';
|
||||||
|
import { Button } from './ui/button';
|
||||||
|
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;
|
||||||
|
setMode: Dispatch<SetStateAction<'view' | 'edit'>>;
|
||||||
|
setMessages: (
|
||||||
|
messages: Message[] | ((messages: Message[]) => Message[]),
|
||||||
|
) => void;
|
||||||
|
reload: (
|
||||||
|
chatRequestOptions?: ChatRequestOptions,
|
||||||
|
) => Promise<string | null | undefined>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function MessageEditor({
|
||||||
|
message,
|
||||||
|
setMode,
|
||||||
|
setMessages,
|
||||||
|
reload,
|
||||||
|
}: MessageEditorProps) {
|
||||||
|
const { userMessageIdFromServer } = useUserMessageId();
|
||||||
|
const [isSubmitting, setIsSubmitting] = useState<boolean>(false);
|
||||||
|
|
||||||
|
const [draftContent, setDraftContent] = useState<string>(message.content);
|
||||||
|
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (textareaRef.current) {
|
||||||
|
adjustHeight();
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const adjustHeight = () => {
|
||||||
|
if (textareaRef.current) {
|
||||||
|
textareaRef.current.style.height = 'auto';
|
||||||
|
textareaRef.current.style.height = `${textareaRef.current.scrollHeight + 2}px`;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleInput = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||||||
|
setDraftContent(event.target.value);
|
||||||
|
adjustHeight();
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-2 w-full">
|
||||||
|
<Textarea
|
||||||
|
ref={textareaRef}
|
||||||
|
className="bg-transparent outline-none overflow-hidden resize-none !text-base rounded-xl w-full"
|
||||||
|
value={draftContent}
|
||||||
|
onChange={handleInput}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<div className="flex flex-row gap-2 justify-end">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
className="h-fit py-2 px-3"
|
||||||
|
onClick={() => {
|
||||||
|
setMode('view');
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="default"
|
||||||
|
className="h-fit py-2 px-3"
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
|
||||||
|
setMessages((messages) => {
|
||||||
|
const index = messages.findIndex((m) => m.id === message.id);
|
||||||
|
|
||||||
|
if (index !== -1) {
|
||||||
|
const updatedMessage = {
|
||||||
|
...message,
|
||||||
|
content: draftContent,
|
||||||
|
};
|
||||||
|
|
||||||
|
return [...messages.slice(0, index), updatedMessage];
|
||||||
|
}
|
||||||
|
|
||||||
|
return messages;
|
||||||
|
});
|
||||||
|
|
||||||
|
setMode('view');
|
||||||
|
reload();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{isSubmitting ? 'Sending...' : 'Send'}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -1,20 +1,24 @@
|
||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import type { Message } from 'ai';
|
import type { ChatRequestOptions, Message } from 'ai';
|
||||||
import cx from 'classnames';
|
import cx from 'classnames';
|
||||||
import { motion } from 'framer-motion';
|
import { motion } from 'framer-motion';
|
||||||
import { memo, type Dispatch, type SetStateAction } from 'react';
|
import { memo, useState, type Dispatch, type SetStateAction } from 'react';
|
||||||
|
|
||||||
import type { Vote } from '@/lib/db/schema';
|
import type { Vote } from '@/lib/db/schema';
|
||||||
|
|
||||||
import type { UIBlock } from './block';
|
import type { UIBlock } from './block';
|
||||||
import { DocumentToolCall, DocumentToolResult } from './document';
|
import { DocumentToolCall, DocumentToolResult } from './document';
|
||||||
import { SparklesIcon } from './icons';
|
import { PencilEditIcon, SparklesIcon } from './icons';
|
||||||
import { Markdown } from './markdown';
|
import { Markdown } from './markdown';
|
||||||
import { MessageActions } from './message-actions';
|
import { MessageActions } from './message-actions';
|
||||||
import { PreviewAttachment } from './preview-attachment';
|
import { PreviewAttachment } from './preview-attachment';
|
||||||
import { Weather } from './weather';
|
import { Weather } from './weather';
|
||||||
import equal from 'fast-deep-equal';
|
import equal from 'fast-deep-equal';
|
||||||
|
import { cn } from '@/lib/utils';
|
||||||
|
import { Button } from './ui/button';
|
||||||
|
import { Tooltip, TooltipContent, TooltipTrigger } from './ui/tooltip';
|
||||||
|
import { MessageEditor } from './message-editor';
|
||||||
|
|
||||||
const PurePreviewMessage = ({
|
const PurePreviewMessage = ({
|
||||||
chatId,
|
chatId,
|
||||||
|
|
@ -23,6 +27,8 @@ const PurePreviewMessage = ({
|
||||||
setBlock,
|
setBlock,
|
||||||
vote,
|
vote,
|
||||||
isLoading,
|
isLoading,
|
||||||
|
setMessages,
|
||||||
|
reload,
|
||||||
}: {
|
}: {
|
||||||
chatId: string;
|
chatId: string;
|
||||||
message: Message;
|
message: Message;
|
||||||
|
|
@ -30,7 +36,15 @@ const PurePreviewMessage = ({
|
||||||
setBlock: Dispatch<SetStateAction<UIBlock>>;
|
setBlock: Dispatch<SetStateAction<UIBlock>>;
|
||||||
vote: Vote | undefined;
|
vote: Vote | undefined;
|
||||||
isLoading: boolean;
|
isLoading: boolean;
|
||||||
|
setMessages: (
|
||||||
|
messages: Message[] | ((messages: Message[]) => Message[]),
|
||||||
|
) => void;
|
||||||
|
reload: (
|
||||||
|
chatRequestOptions?: ChatRequestOptions,
|
||||||
|
) => Promise<string | null | undefined>;
|
||||||
}) => {
|
}) => {
|
||||||
|
const [mode, setMode] = useState<'view' | 'edit'>('view');
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<motion.div
|
<motion.div
|
||||||
className="w-full mx-auto max-w-3xl px-4 group/message"
|
className="w-full mx-auto max-w-3xl px-4 group/message"
|
||||||
|
|
@ -39,8 +53,12 @@ const PurePreviewMessage = ({
|
||||||
data-role={message.role}
|
data-role={message.role}
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
className={cx(
|
className={cn(
|
||||||
'group-data-[role=user]/message:bg-primary group-data-[role=user]/message:text-primary-foreground flex gap-4 group-data-[role=user]/message:px-3 w-full group-data-[role=user]/message:w-fit group-data-[role=user]/message:ml-auto group-data-[role=user]/message:max-w-2xl group-data-[role=user]/message:py-2 rounded-xl',
|
'flex gap-4 w-full group-data-[role=user]/message:ml-auto group-data-[role=user]/message:max-w-2xl',
|
||||||
|
{
|
||||||
|
'w-full': mode === 'edit',
|
||||||
|
'group-data-[role=user]/message:w-fit': mode !== 'edit',
|
||||||
|
},
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
{message.role === 'assistant' && (
|
{message.role === 'assistant' && (
|
||||||
|
|
@ -50,9 +68,58 @@ const PurePreviewMessage = ({
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<div className="flex flex-col gap-2 w-full">
|
<div className="flex flex-col gap-2 w-full">
|
||||||
{message.content && (
|
{message.experimental_attachments && (
|
||||||
<div className="flex flex-col gap-4">
|
<div className="flex flex-row justify-end gap-2">
|
||||||
<Markdown>{message.content as string}</Markdown>
|
{message.experimental_attachments.map((attachment) => (
|
||||||
|
<PreviewAttachment
|
||||||
|
key={attachment.url}
|
||||||
|
attachment={attachment}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{message.content && mode === 'view' && (
|
||||||
|
<div className="flex flex-row gap-2 items-start">
|
||||||
|
{message.role === 'user' && (
|
||||||
|
<Tooltip>
|
||||||
|
<TooltipTrigger asChild>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
className="px-2 h-fit rounded-full text-muted-foreground opacity-0 group-hover/message:opacity-100"
|
||||||
|
onClick={() => {
|
||||||
|
setMode('edit');
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<PencilEditIcon />
|
||||||
|
</Button>
|
||||||
|
</TooltipTrigger>
|
||||||
|
<TooltipContent>Edit message</TooltipContent>
|
||||||
|
</Tooltip>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div
|
||||||
|
className={cn('flex flex-col gap-4', {
|
||||||
|
'bg-primary text-primary-foreground px-3 py-2 rounded-xl':
|
||||||
|
message.role === 'user',
|
||||||
|
})}
|
||||||
|
>
|
||||||
|
<Markdown>{message.content as string}</Markdown>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{message.content && mode === 'edit' && (
|
||||||
|
<div className="flex flex-row gap-2 items-start">
|
||||||
|
<div className="size-8" />
|
||||||
|
|
||||||
|
<MessageEditor
|
||||||
|
key={message.id}
|
||||||
|
message={message}
|
||||||
|
setMode={setMode}
|
||||||
|
setMessages={setMessages}
|
||||||
|
reload={reload}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
|
@ -129,17 +196,6 @@ const PurePreviewMessage = ({
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{message.experimental_attachments && (
|
|
||||||
<div className="flex flex-row gap-2">
|
|
||||||
{message.experimental_attachments.map((attachment) => (
|
|
||||||
<PreviewAttachment
|
|
||||||
key={attachment.url}
|
|
||||||
attachment={attachment}
|
|
||||||
/>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<MessageActions
|
<MessageActions
|
||||||
key={`action-${message.id}`}
|
key={`action-${message.id}`}
|
||||||
chatId={chatId}
|
chatId={chatId}
|
||||||
|
|
@ -158,6 +214,7 @@ export const PreviewMessage = memo(
|
||||||
(prevProps, nextProps) => {
|
(prevProps, nextProps) => {
|
||||||
if (prevProps.isLoading !== nextProps.isLoading) return false;
|
if (prevProps.isLoading !== nextProps.isLoading) return false;
|
||||||
if (prevProps.isLoading && nextProps.isLoading) return false;
|
if (prevProps.isLoading && nextProps.isLoading) return false;
|
||||||
|
if (prevProps.message.content && nextProps.message.content) return false;
|
||||||
if (!equal(prevProps.vote, nextProps.vote)) return false;
|
if (!equal(prevProps.vote, nextProps.vote)) return false;
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { Message } from 'ai';
|
import { ChatRequestOptions, Message } from 'ai';
|
||||||
import { PreviewMessage, ThinkingMessage } from './message';
|
import { PreviewMessage, ThinkingMessage } from './message';
|
||||||
import { useScrollToBottom } from './use-scroll-to-bottom';
|
import { useScrollToBottom } from './use-scroll-to-bottom';
|
||||||
import { Overview } from './overview';
|
import { Overview } from './overview';
|
||||||
|
|
@ -13,6 +13,12 @@ interface MessagesProps {
|
||||||
isLoading: boolean;
|
isLoading: boolean;
|
||||||
votes: Array<Vote> | undefined;
|
votes: Array<Vote> | undefined;
|
||||||
messages: Array<Message>;
|
messages: Array<Message>;
|
||||||
|
setMessages: (
|
||||||
|
messages: Message[] | ((messages: Message[]) => Message[]),
|
||||||
|
) => void;
|
||||||
|
reload: (
|
||||||
|
chatRequestOptions?: ChatRequestOptions,
|
||||||
|
) => Promise<string | null | undefined>;
|
||||||
}
|
}
|
||||||
|
|
||||||
function PureMessages({
|
function PureMessages({
|
||||||
|
|
@ -22,6 +28,8 @@ function PureMessages({
|
||||||
isLoading,
|
isLoading,
|
||||||
votes,
|
votes,
|
||||||
messages,
|
messages,
|
||||||
|
setMessages,
|
||||||
|
reload,
|
||||||
}: MessagesProps) {
|
}: MessagesProps) {
|
||||||
const [messagesContainerRef, messagesEndRef] =
|
const [messagesContainerRef, messagesEndRef] =
|
||||||
useScrollToBottom<HTMLDivElement>();
|
useScrollToBottom<HTMLDivElement>();
|
||||||
|
|
@ -46,6 +54,8 @@ function PureMessages({
|
||||||
? votes.find((vote) => vote.messageId === message.id)
|
? votes.find((vote) => vote.messageId === message.id)
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
|
setMessages={setMessages}
|
||||||
|
reload={reload}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -228,7 +228,7 @@ function PureMultimodalInput({
|
||||||
value={input}
|
value={input}
|
||||||
onChange={handleInput}
|
onChange={handleInput}
|
||||||
className={cx(
|
className={cx(
|
||||||
'min-h-[24px] max-h-[calc(75dvh)] overflow-hidden resize-none rounded-xl text-base bg-muted',
|
'min-h-[24px] max-h-[calc(75dvh)] overflow-hidden resize-none rounded-xl !text-base bg-muted',
|
||||||
className,
|
className,
|
||||||
)}
|
)}
|
||||||
rows={3}
|
rows={3}
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,18 @@ import { useSWRConfig } from 'swr';
|
||||||
import type { Suggestion } from '@/lib/db/schema';
|
import type { Suggestion } from '@/lib/db/schema';
|
||||||
|
|
||||||
import type { UIBlock } from './block';
|
import type { UIBlock } from './block';
|
||||||
|
import { useUserMessageId } from '@/hooks/use-user-message-id';
|
||||||
|
|
||||||
type StreamingDelta = {
|
type StreamingDelta = {
|
||||||
type: 'text-delta' | 'title' | 'id' | 'suggestion' | 'clear' | 'finish';
|
type:
|
||||||
|
| 'text-delta'
|
||||||
|
| 'title'
|
||||||
|
| 'id'
|
||||||
|
| 'suggestion'
|
||||||
|
| 'clear'
|
||||||
|
| 'finish'
|
||||||
|
| 'user-message-id';
|
||||||
|
|
||||||
content: string | Suggestion;
|
content: string | Suggestion;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -23,6 +32,8 @@ export function useBlockStream({
|
||||||
Array<Suggestion>
|
Array<Suggestion>
|
||||||
>([]);
|
>([]);
|
||||||
|
|
||||||
|
const { setUserMessageIdFromServer } = useUserMessageId();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (optimisticSuggestions && optimisticSuggestions.length > 0) {
|
if (optimisticSuggestions && optimisticSuggestions.length > 0) {
|
||||||
const [optimisticSuggestion] = optimisticSuggestions;
|
const [optimisticSuggestion] = optimisticSuggestions;
|
||||||
|
|
@ -37,6 +48,11 @@ export function useBlockStream({
|
||||||
|
|
||||||
const delta = mostRecentDelta as StreamingDelta;
|
const delta = mostRecentDelta as StreamingDelta;
|
||||||
|
|
||||||
|
if (delta.type === 'user-message-id') {
|
||||||
|
setUserMessageIdFromServer(delta.content as string);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
setBlock((draftBlock) => {
|
setBlock((draftBlock) => {
|
||||||
switch (delta.type) {
|
switch (delta.type) {
|
||||||
case 'id':
|
case 'id':
|
||||||
|
|
|
||||||
10
hooks/use-user-message-id.ts
Normal file
10
hooks/use-user-message-id.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
'use client';
|
||||||
|
|
||||||
|
import useSWR from 'swr';
|
||||||
|
|
||||||
|
export function useUserMessageId() {
|
||||||
|
const { data: userMessageIdFromServer, mutate: setUserMessageIdFromServer } =
|
||||||
|
useSWR('userMessageIdFromServer', null);
|
||||||
|
|
||||||
|
return { userMessageIdFromServer, setUserMessageIdFromServer };
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import 'server-only';
|
import 'server-only';
|
||||||
|
|
||||||
import { genSaltSync, hashSync } from 'bcrypt-ts';
|
import { genSaltSync, hashSync } from 'bcrypt-ts';
|
||||||
import { and, asc, desc, eq, gt } from 'drizzle-orm';
|
import { and, asc, desc, eq, gt, gte } from 'drizzle-orm';
|
||||||
import { drizzle } from 'drizzle-orm/postgres-js';
|
import { drizzle } from 'drizzle-orm/postgres-js';
|
||||||
import postgres from 'postgres';
|
import postgres from 'postgres';
|
||||||
|
|
||||||
|
|
@ -279,3 +279,33 @@ export async function getSuggestionsByDocumentId({
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getMessageById({ id }: { id: string }) {
|
||||||
|
try {
|
||||||
|
return await db.select().from(message).where(eq(message.id, id));
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to get message by id from database');
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteMessagesByChatIdAfterTimestamp({
|
||||||
|
chatId,
|
||||||
|
timestamp,
|
||||||
|
}: {
|
||||||
|
chatId: string;
|
||||||
|
timestamp: Date;
|
||||||
|
}) {
|
||||||
|
try {
|
||||||
|
return await db
|
||||||
|
.delete(message)
|
||||||
|
.where(
|
||||||
|
and(eq(message.chatId, chatId), gte(message.createdAt, timestamp)),
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(
|
||||||
|
'Failed to delete messages by id after timestamp from database',
|
||||||
|
);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue