An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Optimize chat storage: only update metadata for new chats, add message-utils for conversion
Yoshiki Miurafcc4c54aee96
diff --git a/lib/actions/chat-db.ts b/lib/actions/chat-db.tsindex c11b7e2b..82c312ff 100644--- a/lib/actions/chat-db.ts+++ b/lib/actions/chat-db.ts@@ -37,7 +37,6 @@ interface ClientChatInput {// Interface for new message input from the clientinterface ClientNewMessageInput {- id: string // Accept message.id from AI SDKrole: DBMessage['role']parts: DBMessage['parts']}@@ -45,42 +44,51 @@ interface ClientNewMessageInput {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+ userId: string, // Authenticated user ID is passed as a separate argument+ updateMetadata: boolean = true // New parameter to control whether to update metadata for existing chats) {try {- // 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]+ // 1. Check if chat exists+ const existingChat = await chatDb.getChat(clientChatInput.id, userId)++ // Variables to hold the saved/updated chat details+ let savedOrUpdatedChatDetails++ if (existingChat && !updateMetadata) {+ // If chat exists and we don't want to update metadata, use the existing chat details+ savedOrUpdatedChatDetails = existingChat+ } else {+ // Otherwise, prepare chat metadata and save/update it+ 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+ }- if (!savedOrUpdatedChatDetails) {- throw new Error(- `Failed to save chat metadata for chat ID: ${clientChatInput.id}. The operation returned no details.`+ // Call lib/db/chat.ts saveChat to save/update chat metadata in the DB+ const savedOrUpdatedChatArray = await chatDb.saveChat(+ chatDataForDb as DBChat,+ userId)+ savedOrUpdatedChatDetails =+ savedOrUpdatedChatArray && savedOrUpdatedChatArray[0]++ if (!savedOrUpdatedChatDetails) {+ throw new Error(+ `Failed to save chat metadata for chat ID: ${clientChatInput.id}. The operation returned no details.`+ )+ }}// 2. Add new messagesif (clientNewMessages && clientNewMessages.length > 0) {for (const message of clientNewMessages) {await chatDb.addMessage({- id: message.id, // Use ID provided by the client (from AI SDK)chatId: savedOrUpdatedChatDetails.id, // Use the ID of the saved/updated chatrole: message.role,parts: message.partsdiff --git a/lib/streaming/create-tool-calling-stream.ts b/lib/streaming/create-tool-calling-stream.tsindex 712835f0..39cf361f 100644--- a/lib/streaming/create-tool-calling-stream.ts+++ b/lib/streaming/create-tool-calling-stream.ts@@ -1,4 +1,8 @@import { researcher } from '@/lib/agents/researcher'+import {+ convertMessagesForDB,+ extractTitleFromMessage+} from '@/lib/utils/message-utils'import {convertToCoreMessages,CoreMessage,@@ -6,9 +10,10 @@ import {DataStreamWriter,streamText} from 'ai'+import { saveChat } from '../actions/chat-db'+import { getCurrentUserId } from '../auth/get-current-user'+import { getChat } from '../db/chat'import { getMaxAllowedTokens, truncateMessages } from '../utils/context-window'-import { isReasoningModel } from '../utils/registry'-import { handleStreamFinish } from './handle-stream-finish'import { BaseStreamConfig } from './types'// Function to check if a message contains ask_question tool invocation@@ -44,31 +49,57 @@ export function createToolCallingStreamResponse(config: BaseStreamConfig) {})const result = streamText({- ...researcherConfig,- onFinish: async result => {- // Check if the last message contains an ask_question tool invocation- const shouldSkipRelatedQuestions =- isReasoningModel(modelId) ||- (result.response.messages.length > 0 &&- containsAskQuestionTool(- result.response.messages[- result.response.messages.length - 1- ] as CoreMessage- ))+ ...researcherConfig+ // onFinish: async result => {+ // // Check if the last message contains an ask_question tool invocation+ // const shouldSkipRelatedQuestions =+ // isReasoningModel(modelId) ||+ // (result.response.messages.length > 0 &&+ // containsAskQuestionTool(+ // result.response.messages[+ // result.response.messages.length - 1+ // ] as CoreMessage+ // ))- await handleStreamFinish({- responseMessages: result.response.messages,- originalMessages: messages,- model: modelId,- chatId,- dataStream,- userId,- skipRelatedQuestions: shouldSkipRelatedQuestions- })- }+ // await handleStreamFinish({+ // responseMessages: result.response.messages,+ // originalMessages: messages,+ // model: modelId,+ // chatId,+ // dataStream,+ // userId,+ // skipRelatedQuestions: shouldSkipRelatedQuestions+ // })+ // }})result.mergeIntoDataStream(dataStream)++ const userId = await getCurrentUserId()+ const responseMessages = (await result.response).messages++ // Convert messages to database format+ const saveMessages = convertMessagesForDB(responseMessages)++ // Check if chat exists to determine title and update strategy+ const existingChat = await getChat(chatId, userId)++ // Get appropriate title - use existing title if available, otherwise extract from message+ const title = existingChat+ ? existingChat.title+ : extractTitleFromMessage(coreMessages[0]) || 'New Chat'++ // Save the chat to the database+ // Only update metadata if this is a new chat (existingChat is null)+ await saveChat(+ {+ id: chatId,+ title+ },+ saveMessages,+ userId,+ !existingChat // Only update metadata for new chats+ )} catch (error) {console.error('Stream execution error:', error)throw errordiff --git a/lib/utils/message-utils.ts b/lib/utils/message-utils.tsnew file mode 100644index 00000000..7cb11a0a--- /dev/null+++ b/lib/utils/message-utils.ts@@ -0,0 +1,93 @@+import { type Message as DBMessage } from '@/lib/db/schema'+import { CoreMessage as SDKCoreMessage } from 'ai'++// Type definition for the CoreMessage from AI SDK+interface CoreMessage {+ role: string+ content: string | Array<{ type: string; text: string }> | null+ id?: string+ // Add other properties that might be in CoreMessage here+}++// Interface matching the expected DB message input format+interface DatabaseMessageInput {+ role: DBMessage['role']+ parts: any // Using 'any' here as we don't know the exact structure expected by the database+}++/**+ * Converts a single message from AI SDK to a database-compatible message format+ * @param message - Message from AI SDK+ * @returns Database-compatible message object+ */+export function convertMessageForDB(+ message: SDKCoreMessage+): DatabaseMessageInput {+ // Handle case where content might be a string, array, or null+ let parts: any++ if (message.content === null || message.content === undefined) {+ parts = []+ } else if (typeof message.content === 'string') {+ parts = [{ text: message.content }]+ } else if (Array.isArray(message.content)) {+ // For array content (common in assistant messages with tool calls)+ // Extract text parts and join them+ const textParts = message.content+ .filter(part => part.type === 'text')+ .map(part => ({ text: part.text }))++ if (textParts.length > 0) {+ parts = textParts+ } else {+ // If no text parts, use the first part's content or stringify the whole content+ parts = [{ text: JSON.stringify(message.content) }]+ }+ } else {+ // Fall back to JSON string for other content types+ parts = [{ text: JSON.stringify(message.content) }]+ }++ return {+ role: message.role,+ parts: parts+ }+}++/**+ * Converts an array of messages from AI SDK to database-compatible message format+ * @param messages - Array of messages from AI SDK+ * @returns Array of database-compatible message objects+ */+export function convertMessagesForDB(+ messages: SDKCoreMessage[]+): DatabaseMessageInput[] {+ return messages.map(convertMessageForDB)+}++/**+ * Extract the first text content from a message for use as a title+ * @param message - Message from AI SDK+ * @param maxLength - Maximum title length to extract+ * @returns Extracted title string, truncated to maxLength+ */+export function extractTitleFromMessage(+ message: SDKCoreMessage,+ maxLength = 100+): string {+ if (!message.content) return 'New Chat'++ if (typeof message.content === 'string') {+ return message.content.substring(0, maxLength)+ }++ // For array content, try to find text parts+ if (Array.isArray(message.content)) {+ const textPart = message.content.find(part => part.type === 'text')+ if (textPart && 'text' in textPart) {+ return textPart.text.substring(0, maxLength)+ }+ }++ return 'New Chat'+}