An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
refactor: improve readability of create-chat-stream-response.ts
Yoshiki Miuraf35f46758ee7
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.tsindex 3e64e40b..0fc327f3 100644--- a/lib/streaming/create-chat-stream-response.ts+++ b/lib/streaming/create-chat-stream-response.ts@@ -24,6 +24,191 @@ import { BaseStreamConfig } from './types'// Constantsconst DEFAULT_CHAT_TITLE = 'New Chat'+// Helper function to check if a message contains tool calls+function hasToolCalls(message: UIMessage | null): boolean {+ if (!message || !message.parts) return false++ return message.parts.some(+ (part: any) =>+ part.type &&+ (part.type.startsWith('tool-') || part.type === 'tool-call')+ )+}++// Helper function to save chat title+async function saveChatTitle(+ chat: any,+ chatId: string,+ message: UIMessage | null,+ modelId: string+) {+ if (!chat && message) {+ const userContent = getTextFromParts(message.parts)+ const title = await generateChatTitle({+ userMessageContent: userContent,+ modelId+ })+ const { updateChatTitle } = await import('../db/actions')+ await updateChatTitle(chatId, title)+ }+}++// Helper function to prepare messages for regeneration+async function prepareMessagesForRegeneration(+ chatId: string,+ userId: string,+ messageId: string,+ message: UIMessage | null+): Promise<UIMessage[]> {+ const currentChat = await getChatAction(chatId, userId)+ if (!currentChat || !currentChat.messages.length) {+ throw new Error('No messages found')+ }++ const messageIndex = currentChat.messages.findIndex(m => m.id === messageId)+ if (messageIndex === -1) {+ throw new Error(`Message ${messageId} not found`)+ }++ const targetMessage = currentChat.messages[messageIndex]++ if (targetMessage.role === 'assistant') {+ // Delete from this assistant message onwards+ await deleteMessagesFromIndex(chatId, messageId)+ // Use messages up to (but not including) this assistant message+ return currentChat.messages.slice(0, messageIndex)+ } else {+ // If it's a user message that was edited, save the updated message first+ if (message && message.id === messageId) {+ await saveMessage(chatId, message)+ }+ // Delete everything after this user message+ const messagesToDelete = currentChat.messages.slice(messageIndex + 1)+ if (messagesToDelete.length > 0) {+ await deleteMessagesFromIndex(chatId, messagesToDelete[0].id)+ }+ // Get updated messages including the edited one+ const updatedChat = await getChatAction(chatId, userId)+ if (updatedChat?.messages) {+ return updatedChat.messages+ } else {+ // Fallback: use current messages up to and including the edited message+ return currentChat.messages.slice(0, messageIndex + 1)+ }+ }+}++// Helper function to prepare messages for normal submission+async function prepareMessagesForSubmission(+ chatId: string,+ userId: string,+ message: UIMessage,+ chat: any+): Promise<UIMessage[]> {+ if (!message) {+ throw new Error('No message provided')+ }++ // Save the message+ const messageWithId = {+ ...message,+ id: message.id || generateId()+ }++ // If chat doesn't exist, create it with a temporary title+ if (!chat) {+ await createChat(chatId, DEFAULT_CHAT_TITLE)+ }++ await saveMessage(chatId, messageWithId)++ // Get all messages including the one just saved+ const updatedChat = await getChatAction(chatId, userId)+ return updatedChat?.messages || [messageWithId]+}++// Helper function to handle direct response (no tool calls)+async function handleDirectResponse(+ writer: UIMessageStreamWriter,+ researchMessage: UIMessage,+ chatId: string,+ chat: any,+ message: UIMessage | null,+ modelId: string+) {+ // Save research message+ await saveMessage(chatId, researchMessage)++ // Generate proper title after conversation starts+ await saveChatTitle(chat, chatId, message, modelId)++ // Send a finish message to complete the stream+ writer.write({ type: 'finish' })+}++// Helper function to generate related questions+async function generateRelatedQuestionsStream(+ writer: UIMessageStreamWriter,+ researchResult: any,+ messagesToModel: UIMessage[],+ modelId: string,+ researchMessage: UIMessage,+ chatId: string,+ chat: any,+ message: UIMessage | null+) {+ try {+ // Get the research data+ const researchData = await researchResult.response++ // Check if request was aborted+ if (researchData.messages.length === 0) {+ return+ }++ // Prepare messages for related questions agent+ const allMessages = [+ ...convertToModelMessages(messagesToModel),+ ...researchData.messages+ ]++ // Get related questions agent+ const relatedQuestionsAgent = generateRelatedQuestions(modelId)+ const relatedQuestionsResult = relatedQuestionsAgent.stream({+ messages: allMessages+ })++ // Merge related questions stream+ writer.merge(+ relatedQuestionsResult.toUIMessageStream({+ sendStart: false,+ onFinish: async ({ responseMessage }) => {+ const relatedQuestionsMessage = responseMessage++ // Save the complete message after both agents finish+ if (researchMessage && relatedQuestionsMessage) {+ const mergedMessage = mergeUIMessages(+ researchMessage,+ relatedQuestionsMessage+ )+ await saveMessage(chatId, mergedMessage)+ } else if (researchMessage) {+ // Save research message only if related questions failed+ await saveMessage(chatId, researchMessage)+ }++ // Generate proper title after conversation starts+ await saveChatTitle(chat, chatId, message, modelId)+ }+ })+ )+ } catch (error) {+ console.error('Error generating related questions:', error)+ // Save research message even if related questions fail+ await saveMessage(chatId, researchMessage)+ }+}+export async function createChatStreamResponse(config: BaseStreamConfig): Promise<Response> {@@ -54,73 +239,23 @@ export async function createChatStreamResponse(const stream = createUIMessageStream({execute: async ({ writer }: { writer: UIMessageStreamWriter }) => {try {+ // Prepare messages based on trigger typelet messagesToModel: UIMessage[]-+if (trigger === 'regenerate-assistant-message' && messageId) {- // Handle regeneration- // Find the message to regenerate from- const currentChat = await getChatAction(chatId, userId)- if (!currentChat || !currentChat.messages.length) {- throw new Error('No messages found')- }-- const messageIndex = currentChat.messages.findIndex(- m => m.id === messageId+ messagesToModel = await prepareMessagesForRegeneration(+ chatId,+ userId,+ messageId,+ message)- if (messageIndex === -1) {- throw new Error(`Message ${messageId} not found`)- }-- // Check if it's an assistant message that needs regeneration- const targetMessage = currentChat.messages[messageIndex]- if (targetMessage.role === 'assistant') {- // Delete from this assistant message onwards- await deleteMessagesFromIndex(chatId, messageId)- // Use messages up to (but not including) this assistant message- messagesToModel = currentChat.messages.slice(0, messageIndex)- } else {- // If it's a user message that was edited, save the updated message first- if (message && message.id === messageId) {- await saveMessage(chatId, message)- }- // Delete everything after this user message- const messagesToDelete = currentChat.messages.slice(- messageIndex + 1- )- if (messagesToDelete.length > 0) {- await deleteMessagesFromIndex(chatId, messagesToDelete[0].id)- }- // Get updated messages including the edited one- const updatedChat = await getChatAction(chatId, userId)- if (updatedChat?.messages) {- messagesToModel = updatedChat.messages- } else {- // Fallback: use current messages up to and including the edited message- messagesToModel = currentChat.messages.slice(0, messageIndex + 1)- }- }} else {- // Handle normal message submission- if (!message) {- throw new Error('No message provided')- }-- // Save the message- const messageWithId = {- ...message,- id: message.id || generateId()- }-- // If chat doesn't exist, create it with a temporary title- if (!chat) {- await createChat(chatId, DEFAULT_CHAT_TITLE)- }-- await saveMessage(chatId, messageWithId)-- // Get all messages including the one just saved- const updatedChat = await getChatAction(chatId, userId)- messagesToModel = updatedChat?.messages || [messageWithId]+ messagesToModel = await prepareMessagesForSubmission(+ chatId,+ userId,+ message!,+ chat+ )}// Get the researcher agent@@ -134,9 +269,8 @@ export async function createChatStreamResponse(messages: convertToModelMessages(messagesToModel)})- // Store messages for merging+ // Store research message for later uselet researchMessage: UIMessage | null = null- let relatedQuestionsMessage: UIMessage | null = null// Merge research stream without finishing and capture the messagewriter.merge(@@ -145,101 +279,30 @@ export async function createChatStreamResponse(onFinish: async ({ responseMessage }) => {researchMessage = responseMessage- // Check if the research message contains tool calls- const hasToolCalls =- responseMessage &&- responseMessage.parts &&- responseMessage.parts.some(- (part: any) =>- part.type &&- (part.type.startsWith('tool-') || part.type === 'tool-call')- )-// If no tool calls (just answering), skip related questions- if (!hasToolCalls) {- // Save research message- await saveMessage(chatId, researchMessage)-- // Generate proper title after conversation starts- if (!chat && message) {- const userContent = getTextFromParts(message.parts)- const title = await generateChatTitle({- userMessageContent: userContent,- modelId- })- // Update chat title- const { updateChatTitle } = await import('../db/actions')- await updateChatTitle(chatId, title)- }-- // Send a finish message to complete the stream- writer.write({ type: 'finish' })+ if (!hasToolCalls(researchMessage)) {+ await handleDirectResponse(+ writer,+ researchMessage!,+ chatId,+ chat,+ message,+ modelId+ )return}// If we have tool calls, proceed with related questions generation- try {- // Get the research data- const researchData = await researchResult.response-- // Check if request was aborted- if (researchData.messages.length === 0) {- return- }-- // Prepare messages for related questions agent- const allMessages = [- ...convertToModelMessages(messagesToModel),- ...researchData.messages- ]-- // Get related questions agent- const relatedQuestionsAgent = generateRelatedQuestions(modelId)- const relatedQuestionsResult = relatedQuestionsAgent.stream({- messages: allMessages- })-- // Merge related questions stream- writer.merge(- relatedQuestionsResult.toUIMessageStream({- sendStart: false,- onFinish: async ({ responseMessage }) => {- relatedQuestionsMessage = responseMessage-- // Save the complete message after both agents finish- // Merge and save both messages- if (researchMessage && relatedQuestionsMessage) {- const mergedMessage = mergeUIMessages(- researchMessage,- relatedQuestionsMessage- )- await saveMessage(chatId, mergedMessage)- } else if (researchMessage) {- // Save research message only if related questions failed- await saveMessage(chatId, researchMessage)- }-- // Generate proper title after conversation starts- if (!chat && message) {- const userContent = getTextFromParts(message.parts)- const title = await generateChatTitle({- userMessageContent: userContent,- modelId- })- // Update chat title- const { updateChatTitle } = await import(- '../db/actions'- )- await updateChatTitle(chatId, title)- }- }- })- )- } catch (error) {- console.error('Error generating related questions:', error)- // Save research message even if related questions fail- await saveMessage(chatId, researchMessage)- }+ await generateRelatedQuestionsStream(+ writer,+ researchResult,+ messagesToModel,+ modelId,+ researchMessage!,+ chatId,+ chat,+ message+ )}}))@@ -255,4 +318,4 @@ export async function createChatStreamResponse(})return createUIMessageStreamResponse({ stream })-}+}\ No newline at end of file