An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Fix async message save issue in Vercel environment
Yoshiki Miura8d0f16b1dac0
diff --git a/lib/streaming/helpers/handle-stream-finish.ts b/lib/streaming/helpers/handle-stream-finish.tsindex fe593159..8624cf93 100644--- a/lib/streaming/helpers/handle-stream-finish.ts+++ b/lib/streaming/helpers/handle-stream-finish.ts@@ -79,26 +79,30 @@ export async function handleStreamFinish(// Save message with retry logicconst saveStart = performance.now()- upsertMessage(chatId, responseMessage, userId)- .then(() => {- perfTime('upsertMessage (AI response) completed', saveStart)- })- .catch(async error => {- console.error('Error saving message:', error)- try {- await retryDatabaseOperation(- () => upsertMessage(chatId, responseMessage, userId),- 'save message'- )- } catch (retryError) {- console.error('Failed to save after retries:', retryError)- }- })+ try {+ await upsertMessage(chatId, responseMessage, userId)+ perfTime('upsertMessage (AI response) completed', saveStart)+ } catch (error) {+ console.error('Error saving message:', error)+ try {+ await retryDatabaseOperation(+ () => upsertMessage(chatId, responseMessage, userId),+ 'save message'+ )+ perfTime('upsertMessage (AI response) completed after retry', saveStart)+ } catch (retryError) {+ console.error('Failed to save after retries:', retryError)+ // Don't throw here to avoid breaking the stream+ }+ }// Update title after message is savedif (chatTitle && chatTitle !== DEFAULT_CHAT_TITLE) {- updateChatTitle(chatId, chatTitle, userId).catch(error =>+ try {+ await updateChatTitle(chatId, chatTitle, userId)+ } catch (error) {console.error('Error updating title:', error)- )+ // Don't throw here as title update is not critical+ }}}