fix: persist greeting fast path safely
This commit is contained in:
parent
cb8da8b512
commit
317633085f
@ -4834,6 +4834,23 @@ export async function POST(request: Request) {
|
||||
}
|
||||
|
||||
const greetingMessage = "你好,我在。你可以直接告诉我想做什么:生图、修图、生成视频、分析图片都可以。";
|
||||
const fastPathTitle = requestTextContent.trim().slice(0, 80) || "新对话";
|
||||
await saveChat({
|
||||
id,
|
||||
userId: session.user.id,
|
||||
title: fastPathTitle,
|
||||
visibility: selectedVisibilityType,
|
||||
});
|
||||
|
||||
const greetingChat = await findChatWithRetry(id);
|
||||
if (!greetingChat) {
|
||||
throw new ChatSDKError("bad_request:database", "Failed to save chat");
|
||||
}
|
||||
|
||||
if (greetingChat.userId !== session.user.id) {
|
||||
return new ChatSDKError("forbidden:chat").toResponse();
|
||||
}
|
||||
|
||||
const greetingStream = createUIMessageStream({
|
||||
execute: async ({ writer }) => {
|
||||
const messageId = generateUUID();
|
||||
@ -4841,6 +4858,19 @@ export async function POST(request: Request) {
|
||||
|
||||
await saveMessages({
|
||||
messages: [
|
||||
{
|
||||
id: message.id,
|
||||
chatId: id,
|
||||
role: "user",
|
||||
parts: message.parts as any,
|
||||
attachments: [],
|
||||
createdAt: new Date(),
|
||||
videoStorageStatus: null,
|
||||
temporaryVideoUrl: null,
|
||||
permanentVideoUrl: null,
|
||||
archivedAt: null,
|
||||
videoMetadata: null,
|
||||
},
|
||||
{
|
||||
id: messageId,
|
||||
chatId: id,
|
||||
|
||||
@ -326,7 +326,9 @@ export async function getChatById({ id }: { id: string }) {
|
||||
|
||||
export async function saveMessages({ messages }: { messages: DBMessage[] }) {
|
||||
try {
|
||||
return await db.insert(message).values(messages);
|
||||
return await db.insert(message).values(messages).onConflictDoNothing({
|
||||
target: message.id,
|
||||
});
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError("bad_request:database", "Failed to save messages");
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user