An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
import { UIMessage } from 'ai'
import {
createChat,
createChatWithFirstMessage,
deleteMessagesFromIndex,
loadChat,
upsertMessage
} from '@/lib/actions/chat'
import { generateId } from '@/lib/db/schema'
import { DeterministicPreparationError } from '@/lib/errors/deterministic-preparation-error'
import {
getUserFileObjectKeyPrefix,
signFilePartUrls
} from '@/lib/storage/r2-client'
import {
countFileParts,
MAX_ATTACHMENTS_PER_MESSAGE
} from '@/lib/utils/attachment-limits'
import { perfLog, perfTime } from '@/lib/utils/perf-logging'
import type { StreamContext } from './types'
const DEFAULT_CHAT_TITLE = 'Untitled'
export async function prepareMessages(
context: StreamContext,
message: UIMessage | null
): Promise<UIMessage[]> {
const { chatId, userId, trigger, messageId, initialChat, isNewChat } = context
const startTime = performance.now()
perfLog(`prepareMessages - Start: trigger=${trigger}, isNewChat=${isNewChat}`)
if (trigger === 'regenerate-message' && messageId) {
// Handle regeneration - use initialChat if available to avoid DB call
let currentChat = initialChat
if (!currentChat) {
currentChat = await loadChat(chatId, userId)
}
if (!currentChat || !currentChat.messages.length) {
throw new DeterministicPreparationError('No messages found')
}
let messageIndex = currentChat.messages.findIndex(
(m: any) => m.id === messageId
)
// Fallback: If message not found by ID, try to find by position
if (messageIndex === -1) {
const lastAssistantIndex = currentChat.messages.findLastIndex(
(m: any) => m.role === 'assistant'
)
const lastUserIndex = currentChat.messages.findLastIndex(
(m: any) => m.role === 'user'
)
if (lastAssistantIndex >= 0 || lastUserIndex >= 0) {
messageIndex = Math.max(lastAssistantIndex, lastUserIndex)
} else {
throw new DeterministicPreparationError(
`Message ${messageId} not found and no fallback available`
)
}
}
const targetMessage = currentChat.messages[messageIndex]
const targetMessageId = targetMessage.id
if (targetMessage.role === 'assistant') {
await deleteMessagesFromIndex(chatId, targetMessageId, userId)
// Use in-memory messages instead of re-fetching
return currentChat.messages.slice(0, messageIndex)
} else {
// User message edit
if (message && message.id === messageId) {
// Messages saved before the limit was enforced may already carry more
// files, so only reject an edit that adds files beyond it.
const fileCount = countFileParts(message)
if (
fileCount > MAX_ATTACHMENTS_PER_MESSAGE &&
fileCount > countFileParts(targetMessage)
) {
throw new DeterministicPreparationError(
'Edited message exceeds the attachment limit'
)
}
await upsertMessage(chatId, message, userId)
}
const messagesToDelete = currentChat.messages.slice(messageIndex + 1)
if (messagesToDelete.length > 0) {
await deleteMessagesFromIndex(chatId, messagesToDelete[0].id, userId)
}
// Use in-memory messages instead of re-fetching
const messagesBeforeTarget = currentChat.messages.slice(0, messageIndex)
const editedMessage =
message && message.id === messageId ? message : targetMessage
return [...messagesBeforeTarget, editedMessage]
}
} else {
// Handle normal message submission
if (!message) {
throw new Error('No message provided')
}
const messageWithId = {
...message,
id: message.id || generateId(),
parts: await signFilePartUrls(message.parts, {
allowedKeyPrefix: getUserFileObjectKeyPrefix(userId)
})
}
// Optimize for new chats: create chat and save message together
if (isNewChat) {
// Persist the chat and first message optimistically in the background
const createStart = performance.now()
const persistencePromise = createChatWithFirstMessage(
chatId,
messageWithId,
userId,
DEFAULT_CHAT_TITLE
)
.then(result => {
perfTime('createChatWithFirstMessage completed', createStart)
perfTime('prepareMessages - Total', startTime)
return result
})
.catch(error => {
console.error('Error creating chat with first message:', error)
throw error
})
context.pendingInitialSave = persistencePromise
context.pendingInitialUserMessage = messageWithId
perfTime('prepareMessages - Return (optimistic)', startTime)
return [messageWithId]
}
// For existing chats
if (!initialChat) {
const createStart = performance.now()
await createChat(chatId, DEFAULT_CHAT_TITLE, userId)
perfTime('createChat completed', createStart)
}
const upsertStart = performance.now()
await upsertMessage(chatId, messageWithId, userId)
perfTime('upsertMessage completed', upsertStart)
// If we have initialChat, append the new message instead of fetching all messages
if (initialChat && initialChat.messages) {
perfTime('prepareMessages - Total (using cached chat)', startTime)
return [...initialChat.messages, messageWithId]
}
// Fallback to fetching if no initialChat
const loadStart = performance.now()
const updatedChat = await loadChat(chatId, userId)
perfTime('loadChat (fallback) completed', loadStart)
perfTime('prepareMessages - Total', startTime)
return updatedChat?.messages || [messageWithId]
}
}