An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Fix deletion under RLS by preserving user context
Yoshiki Miura98cb55d8e790
diff --git a/lib/actions/chat.ts b/lib/actions/chat.tsindex 023f86a3..62d25ee0 100644--- a/lib/actions/chat.ts+++ b/lib/actions/chat.ts@@ -286,9 +286,10 @@ export async function shareChat(chatId: string) {*/export async function deleteMessagesFromIndex(chatId: string,- messageId: string+ messageId: string,+ userIdOverride?: string) {- const userId = await getCurrentUserId()+ const userId = userIdOverride ?? (await getCurrentUserId())if (!userId) {return { success: false, error: 'User not authenticated' }}@@ -299,7 +300,11 @@ export async function deleteMessagesFromIndex(return { success: false, error: 'Unauthorized' }}- const result = await dbActions.deleteMessagesFromIndex(chatId, messageId)+ const result = await dbActions.deleteMessagesFromIndex(+ chatId,+ messageId,+ userId+ )revalidateTag(`chat-${chatId}`)diff --git a/lib/streaming/helpers/prepare-messages.ts b/lib/streaming/helpers/prepare-messages.tsindex bae8e2ec..39e3d77f 100644--- a/lib/streaming/helpers/prepare-messages.ts+++ b/lib/streaming/helpers/prepare-messages.ts@@ -56,7 +56,7 @@ export async function prepareMessages(const targetMessage = currentChat.messages[messageIndex]if (targetMessage.role === 'assistant') {- await deleteMessagesFromIndex(chatId, messageId)+ await deleteMessagesFromIndex(chatId, messageId, userId)return currentChat.messages.slice(0, messageIndex)} else {// User message edit@@ -65,7 +65,7 @@ export async function prepareMessages(}const messagesToDelete = currentChat.messages.slice(messageIndex + 1)if (messagesToDelete.length > 0) {- await deleteMessagesFromIndex(chatId, messagesToDelete[0].id)+ await deleteMessagesFromIndex(chatId, messagesToDelete[0].id, userId)}const updatedChat = await loadChat(chatId, userId)return (diff --git a/lib/streaming/message-preparation.ts b/lib/streaming/message-preparation.tsindex 445d0b3b..48d733f6 100644--- a/lib/streaming/message-preparation.ts+++ b/lib/streaming/message-preparation.ts@@ -40,7 +40,7 @@ export async function prepareMessagesForRegeneration(if (targetMessage.role === 'assistant') {// Delete from this assistant message onwards- await deleteMessagesFromIndex(chatId, messageId)+ await deleteMessagesFromIndex(chatId, messageId, userId)// Use messages up to (but not including) this assistant messagereturn currentChat.messages.slice(0, messageIndex)} else {@@ -51,7 +51,7 @@ export async function prepareMessagesForRegeneration(// Delete everything after this user messageconst messagesToDelete = currentChat.messages.slice(messageIndex + 1)if (messagesToDelete.length > 0) {- await deleteMessagesFromIndex(chatId, messagesToDelete[0].id)+ await deleteMessagesFromIndex(chatId, messagesToDelete[0].id, userId)}// Get updated messages including the edited oneconst updatedChat = await loadChat(chatId, userId)