An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat: start chat streaming optimistically
Yoshiki Miuraa54c7786a4a3
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.tsindex 05e0b5ce..2234d7c6 100644--- a/lib/streaming/create-chat-stream-response.ts+++ b/lib/streaming/create-chat-stream-response.ts@@ -232,7 +232,9 @@ export async function createChatStreamResponse(titlePromise,parentTraceId,searchMode,- context.modelId+ context.modelId,+ context.pendingInitialSave,+ context.pendingInitialUserMessage)}})diff --git a/lib/streaming/helpers/persist-stream-results.ts b/lib/streaming/helpers/persist-stream-results.tsindex 392b31c9..7462dc35 100644--- a/lib/streaming/helpers/persist-stream-results.ts+++ b/lib/streaming/helpers/persist-stream-results.ts@@ -1,6 +1,6 @@import { UIMessage } from 'ai'-import { upsertMessage } from '@/lib/actions/chat'+import { createChatWithFirstMessage, upsertMessage } from '@/lib/actions/chat'import { updateChatTitle } from '@/lib/db/actions'import { SearchMode } from '@/lib/types/search'import { perfTime } from '@/lib/utils/perf-logging'@@ -15,7 +15,9 @@ export async function persistStreamResults(titlePromise?: Promise<string>,parentTraceId?: string,searchMode?: SearchMode,- modelId?: string+ modelId?: string,+ initialSavePromise?: Promise<Awaited<ReturnType<typeof createChatWithFirstMessage>>>,+ initialUserMessage?: UIMessage) {// Attach metadata to the response messageresponseMessage.metadata = {@@ -28,6 +30,34 @@ export async function persistStreamResults(// Wait for title generation if it was startedconst chatTitle = titlePromise ? await titlePromise : undefined+ // Ensure the initial chat/message persistence finished before saving the response+ if (initialSavePromise) {+ const initialSaveStart = performance.now()+ try {+ await initialSavePromise+ perfTime('initial chat persistence awaited', initialSaveStart)+ } catch (error) {+ console.error('Initial chat persistence failed:', error)+ if (initialUserMessage) {+ const fallbackStart = performance.now()+ try {+ await createChatWithFirstMessage(+ chatId,+ initialUserMessage,+ userId,+ DEFAULT_CHAT_TITLE+ )+ perfTime('initial chat persistence fallback completed', fallbackStart)+ } catch (fallbackError) {+ console.error('Fallback chat creation failed:', fallbackError)+ return+ }+ } else {+ return+ }+ }+ }+// Save message with retry logicconst saveStart = performance.now()try {diff --git a/lib/streaming/helpers/prepare-messages.ts b/lib/streaming/helpers/prepare-messages.tsindex 39e3d77f..6814f92a 100644--- a/lib/streaming/helpers/prepare-messages.ts+++ b/lib/streaming/helpers/prepare-messages.ts@@ -85,16 +85,28 @@ export async function prepareMessages(// Optimize for new chats: create chat and save message togetherif (isNewChat) {- // Use createChatWithFirstMessage for atomic operation+ // Persist the chat and first message optimistically in the backgroundconst createStart = performance.now()- await createChatWithFirstMessage(+ const persistencePromise = createChatWithFirstMessage(chatId,messageWithId,userId,DEFAULT_CHAT_TITLE)- perfTime('createChatWithFirstMessage completed', createStart)- perfTime('prepareMessages - Total', startTime)+ .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]}diff --git a/lib/streaming/helpers/types.ts b/lib/streaming/helpers/types.tsindex 2a55dbac..789afdaf 100644--- a/lib/streaming/helpers/types.ts+++ b/lib/streaming/helpers/types.ts@@ -1,4 +1,4 @@-import type { Chat } from '@/lib/db/schema'+import type { Chat, Message } from '@/lib/db/schema'import type { UIMessage } from '@/lib/types/ai'export interface StreamContext {@@ -11,4 +11,6 @@ export interface StreamContext {abortSignal?: AbortSignalparentTraceId?: stringisNewChat?: boolean+ pendingInitialSave?: Promise<{ chat: Chat; message: Message }>+ pendingInitialUserMessage?: UIMessage}