diff --git a/lib/actions/chat-db.ts b/lib/actions/chat-db.ts index c4eea9ec..3fcdc2f9 100644 --- a/lib/actions/chat-db.ts +++ b/lib/actions/chat-db.ts @@ -112,13 +112,30 @@ export async function saveChat(chatData: NewChat, messagesData: Omit 0) { - const messagesToInsert = messagesData.map(msg => ({ - ...msg, - chatId: chatId!, // Ensure chatId is set for all messages - userId: msg.userId || chatData.userId!, // Ensure userId is set - })); + const seenIds = new Set(); + const messagesToInsert: typeof messages.$inferInsert[] = []; + + for (const msg of messagesData) { + let id = msg.id; + + // If we've already seen this ID in this batch, generate a unique one + while (seenIds.has(id)) { + id = `${id}-${Math.random().toString(36).substring(2, 8)}`; + } + seenIds.add(id); + + messagesToInsert.push({ + ...msg, + id, + chatId: chatId!, + userId: msg.userId || chatData.userId!, + }); + } + await tx.insert(messages).values(messagesToInsert).onConflictDoUpdate({ target: messages.id, set: { content: sql`EXCLUDED.content`, role: sql`EXCLUDED.role` } }); } return chatId;