feat: edit and resubmit messages (#592)

This commit is contained in:
Jeremy 2024-12-05 15:29:33 +03:00 committed by GitHub
parent 2e479ccce7
commit 64d1aad2bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 302 additions and 25 deletions

View file

@ -4,6 +4,10 @@ import { type CoreUserMessage, generateText } from 'ai';
import { cookies } from 'next/headers';
import { customModel } from '@/lib/ai';
import {
deleteMessagesByChatIdAfterTimestamp,
getMessageById,
} from '@/lib/db/queries';
export async function saveModelId(model: string) {
const cookieStore = await cookies();
@ -27,3 +31,12 @@ export async function generateTitleFromUserMessage({
return title;
}
export async function deleteTrailingMessages({ id }: { id: string }) {
const [message] = await getMessageById({ id });
await deleteMessagesByChatIdAfterTimestamp({
chatId: message.chatId,
timestamp: message.createdAt,
});
}

View file

@ -81,14 +81,21 @@ export async function POST(request: Request) {
await saveChat({ id, userId: session.user.id, title });
}
const userMessageId = generateUUID();
await saveMessages({
messages: [
{ ...userMessage, id: generateUUID(), createdAt: new Date(), chatId: id },
{ ...userMessage, id: userMessageId, createdAt: new Date(), chatId: id },
],
});
const streamingData = new StreamData();
streamingData.append({
type: 'user-message-id',
content: userMessageId,
});
const result = streamText({
model: customModel(model.apiIdentifier),
system: systemPrompt,