Skip to content

An AI-powered search engine with a generative UI

feat: Update chat saving logic to support message IDs and improved types

Yoshiki Miura89fd28a59cb7
diff --git a/lib/actions/chat-db.ts b/lib/actions/chat-db.ts
index ea93821f..c11b7e2b 100644
--- a/lib/actions/chat-db.ts
+++ b/lib/actions/chat-db.ts
@@ -1,6 +1,7 @@
'use server'
import * as chatDb from '@/lib/db/chat'
+import { type Chat as DBChat, type Message as DBMessage } from '@/lib/db/schema' // Import DB schema types
// Get all chats for a user
export async function getChats(userId: string) {
@@ -27,35 +28,74 @@ export async function deleteChat(chatId: string, userId: string) {
return chatDb.deleteChat(chatId, userId)
}
-// Save a chat (with message history)
-export async function saveChat(chat: any, userId: string) {
+// Interface for chat metadata input from the client
+interface ClientChatInput {
+ id: string // Chat ID is required
+ title: string // Title is also required
+ visibility?: DBChat['visibility'] // Visibility setting is optional
+}
+
+// Interface for new message input from the client
+interface ClientNewMessageInput {
+ id: string // Accept message.id from AI SDK
+ role: DBMessage['role']
+ parts: DBMessage['parts']
+}
+
+export async function saveChat(
+ clientChatInput: ClientChatInput,
+ clientNewMessages: ClientNewMessageInput[] | undefined, // New messages are optional (e.g., when only updating metadata)
+ userId: string // Authenticated user ID is passed as a separate argument
+) {
try {
- // First save the chat
- const savedChat = await chatDb.saveChat(chat, userId)
-
- // Then save all messages if provided
- if (chat.messages && Array.isArray(chat.messages)) {
- // Delete existing messages first to avoid duplicates
- const existingMessages = await chatDb.getChatMessages(chat.id)
- if (existingMessages.length > 0) {
- // We would need a deleteMessages function, but for now we can recreate all
- // This would be more efficiently handled with a batch delete/insert
- }
+ // 1. Prepare chat metadata (align with the type expected by lib/db/chat.ts saveChat)
+ // Conform to DBChat type while setting required fields.
+ const chatDataForDb: Pick<
+ DBChat,
+ 'id' | 'title' | 'userId' | 'visibility'
+ > &
+ Partial<DBChat> = {
+ id: clientChatInput.id,
+ title: clientChatInput.title,
+ userId: userId, // Ensure authenticated user ID is used
+ visibility: clientChatInput.visibility || 'private' // Set default value
+ }
+
+ // Call lib/db/chat.ts saveChat to save/update chat metadata in the DB
+ // chatDb.saveChat is expected to return an array with a single element: [savedChat] or [updatedChat]
+ const savedOrUpdatedChatArray = await chatDb.saveChat(
+ chatDataForDb as DBChat,
+ userId
+ )
+ const savedOrUpdatedChatDetails =
+ savedOrUpdatedChatArray && savedOrUpdatedChatArray[0]
+
+ if (!savedOrUpdatedChatDetails) {
+ throw new Error(
+ `Failed to save chat metadata for chat ID: ${clientChatInput.id}. The operation returned no details.`
+ )
+ }
- // Add all messages
- for (const message of chat.messages) {
+ // 2. Add new messages
+ if (clientNewMessages && clientNewMessages.length > 0) {
+ for (const message of clientNewMessages) {
await chatDb.addMessage({
- chatId: chat.id,
+ id: message.id, // Use ID provided by the client (from AI SDK)
+ chatId: savedOrUpdatedChatDetails.id, // Use the ID of the saved/updated chat
role: message.role,
- parts: message.parts || message.content
+ parts: message.parts
})
}
}
- return savedChat
+ // Return the saved/updated chat metadata
+ return savedOrUpdatedChatDetails
} catch (error) {
- console.error('Error in saveChat action:', error)
- throw error
+ console.error(
+ `Error in saveChat action for chat ID ${clientChatInput.id}:`,
+ error
+ )
+ throw error // Rethrow the error to the caller
}
}
diff --git a/lib/db/chat.ts b/lib/db/chat.ts
index 7effad02..155aebe1 100644
--- a/lib/db/chat.ts
+++ b/lib/db/chat.ts
@@ -128,21 +128,34 @@ export async function deleteChat(
// Add a message to a chat
export async function addMessage({
+ id, // Optional ID parameter
chatId,
role,
parts
}: {
+ id?: string // Make ID optional
chatId: string
role: string
parts: any
}): Promise<Message> {
+ const valuesToInsert: {
+ id?: string
+ chatId: string
+ role: string
+ parts: any
+ } = {
+ chatId,
+ role,
+ parts
+ }
+
+ if (id) {
+ valuesToInsert.id = id // Set ID if provided
+ }
+
const [message] = await db
.insert(messages)
- .values({
- chatId,
- role,
- parts
- })
+ .values(valuesToInsert) // Use the modified values
.returning()
return message