Skip to content

An AI-powered search engine with a generative UI

refactor: rename functions for better clarity

Yoshiki Miura00e1a1c3309e
diff --git a/lib/actions/chat.ts b/lib/actions/chat.ts
index fea0d544..6abdf114 100644
--- a/lib/actions/chat.ts
+++ b/lib/actions/chat.ts
@@ -37,10 +37,21 @@ export async function getChatsPage(limit = 20, offset = 0) {
/**
* Get a chat with messages (no cache)
+ * @deprecated Use loadChat instead
*/
export async function getChat(
chatId: string,
requestingUserId?: string
+): Promise<(Chat & { messages: UIMessage[] }) | null> {
+ return loadChat(chatId, requestingUserId)
+}
+
+/**
+ * Load a chat with messages (Vercel pattern)
+ */
+export async function loadChat(
+ chatId: string,
+ requestingUserId?: string
): Promise<(Chat & { messages: UIMessage[] }) | null> {
// Use optimized function that loads both in parallel
return dbActions.loadChatWithMessages(chatId, requestingUserId)
@@ -114,11 +125,22 @@ export async function createChatAndSaveMessage(
}
/**
- * Save a message to an existing chat
+ * Upsert a message (insert or update)
+ * @deprecated Use upsertMessage instead
*/
export async function saveMessage(
chatId: string,
message: UIMessage
+): Promise<Message> {
+ return upsertMessage(chatId, message)
+}
+
+/**
+ * Upsert a message to a chat (Vercel pattern)
+ */
+export async function upsertMessage(
+ chatId: string,
+ message: UIMessage
): Promise<Message> {
const userId = await getCurrentUserId()
if (!userId) {
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.ts
index 86c8004c..38532884 100644
--- a/lib/streaming/create-chat-stream-response.ts
+++ b/lib/streaming/create-chat-stream-response.ts
@@ -12,7 +12,7 @@ import { Langfuse } from 'langfuse'
import { researcher } from '@/lib/agents/researcher'
import { isTracingEnabled } from '@/lib/utils/telemetry'
-import { getChat as getChatAction } from '../actions/chat'
+import { loadChat } from '../actions/chat'
import { generateChatTitle } from '../agents/title-generator'
import {
getMaxAllowedTokens,
@@ -46,7 +46,7 @@ export async function createChatStreamResponse(
}
// Fetch chat data for authorization check and cache it
- let initialChat = await getChatAction(chatId, userId)
+ let initialChat = await loadChat(chatId, userId)
// Authorization check: if chat exists, it must belong to the user
if (initialChat && initialChat.userId !== userId) {
diff --git a/lib/streaming/helpers/handle-stream-finish.ts b/lib/streaming/helpers/handle-stream-finish.ts
index 622e1e68..df97d1aa 100644
--- a/lib/streaming/helpers/handle-stream-finish.ts
+++ b/lib/streaming/helpers/handle-stream-finish.ts
@@ -1,6 +1,6 @@
import { UIMessage, UIMessageStreamWriter } from 'ai'
-import { saveMessage } from '@/lib/actions/chat'
+import { upsertMessage } from '@/lib/actions/chat'
import { generateRelatedQuestions } from '@/lib/agents/generate-related-questions'
import { updateChatTitle } from '@/lib/db/actions'
import { generateId } from '@/lib/db/schema'
@@ -77,11 +77,11 @@ export async function handleStreamFinish(
const chatTitle = titlePromise ? await titlePromise : undefined
// Save message with retry logic
- saveMessage(chatId, responseMessage).catch(async error => {
+ upsertMessage(chatId, responseMessage).catch(async error => {
console.error('Error saving message:', error)
try {
await retryDatabaseOperation(
- () => saveMessage(chatId, responseMessage),
+ () => upsertMessage(chatId, responseMessage),
'save message'
)
} catch (retryError) {
diff --git a/lib/streaming/helpers/prepare-messages.ts b/lib/streaming/helpers/prepare-messages.ts
index 5dc7ec9a..486cbbc1 100644
--- a/lib/streaming/helpers/prepare-messages.ts
+++ b/lib/streaming/helpers/prepare-messages.ts
@@ -3,8 +3,8 @@ import { UIMessage } from 'ai'
import {
createChat,
deleteMessagesFromIndex,
- getChat as getChatAction,
- saveMessage
+ loadChat,
+ upsertMessage
} from '@/lib/actions/chat'
import { generateId } from '@/lib/db/schema'
@@ -22,7 +22,7 @@ export async function prepareMessages(
// Handle regeneration - use initialChat if available to avoid DB call
let currentChat = initialChat
if (!currentChat) {
- currentChat = await getChatAction(chatId, userId)
+ currentChat = await loadChat(chatId, userId)
}
if (!currentChat || !currentChat.messages.length) {
throw new Error('No messages found')
@@ -57,13 +57,13 @@ export async function prepareMessages(
} else {
// User message edit
if (message && message.id === messageId) {
- await saveMessage(chatId, message)
+ await upsertMessage(chatId, message)
}
const messagesToDelete = currentChat.messages.slice(messageIndex + 1)
if (messagesToDelete.length > 0) {
await deleteMessagesFromIndex(chatId, messagesToDelete[0].id)
}
- const updatedChat = await getChatAction(chatId, userId)
+ const updatedChat = await loadChat(chatId, userId)
return (
updatedChat?.messages || currentChat.messages.slice(0, messageIndex + 1)
)
@@ -83,7 +83,7 @@ export async function prepareMessages(
await createChat(chatId, DEFAULT_CHAT_TITLE)
}
- await saveMessage(chatId, messageWithId)
+ await upsertMessage(chatId, messageWithId)
// If we have initialChat, append the new message instead of fetching all messages
if (initialChat && initialChat.messages) {
@@ -91,7 +91,7 @@ export async function prepareMessages(
}
// Fallback to fetching if no initialChat
- const updatedChat = await getChatAction(chatId, userId)
+ const updatedChat = await loadChat(chatId, userId)
return updatedChat?.messages || [messageWithId]
}
}
diff --git a/lib/streaming/message-preparation.ts b/lib/streaming/message-preparation.ts
index f4cafd89..8182a898 100644
--- a/lib/streaming/message-preparation.ts
+++ b/lib/streaming/message-preparation.ts
@@ -3,8 +3,8 @@ import type { UIMessage } from 'ai'
import {
createChat,
deleteMessagesFromIndex,
- getChat as getChatAction,
- saveMessage
+ loadChat,
+ upsertMessage
} from '@/lib/actions/chat'
import type { Chat } from '@/lib/db/schema'
import { generateId } from '@/lib/db/schema'
@@ -26,7 +26,7 @@ export async function prepareMessagesForRegeneration(
messageId: string,
message: UIMessage | null
): Promise<UIMessage[]> {
- const currentChat = await getChatAction(chatId, userId)
+ const currentChat = await loadChat(chatId, userId)
if (!currentChat || !currentChat.messages.length) {
throw new Error('No messages found')
}
@@ -46,7 +46,7 @@ export async function prepareMessagesForRegeneration(
} else {
// If it's a user message that was edited, save the updated message first
if (message && message.id === messageId) {
- await saveMessage(chatId, message)
+ await upsertMessage(chatId, message)
}
// Delete everything after this user message
const messagesToDelete = currentChat.messages.slice(messageIndex + 1)
@@ -54,7 +54,7 @@ export async function prepareMessagesForRegeneration(
await deleteMessagesFromIndex(chatId, messagesToDelete[0].id)
}
// Get updated messages including the edited one
- const updatedChat = await getChatAction(chatId, userId)
+ const updatedChat = await loadChat(chatId, userId)
if (updatedChat?.messages) {
return updatedChat.messages
} else {
@@ -93,9 +93,9 @@ export async function prepareMessagesForSubmission(
await createChat(chatId, DEFAULT_CHAT_TITLE)
}
- await saveMessage(chatId, messageWithId)
+ await upsertMessage(chatId, messageWithId)
// Get all messages including the one just saved
- const updatedChat = await getChatAction(chatId, userId)
+ const updatedChat = await loadChat(chatId, userId)
return updatedChat?.messages || [messageWithId]
}