Skip to content

An AI-powered search engine with a generative UI

refactor: improve streaming architecture with helper extraction and type safety

Yoshiki Miura12aea9329ce7
diff --git a/components/data-section.tsx b/components/data-section.tsx
index bd6d9939..ecebe4ad 100644
--- a/components/data-section.tsx
+++ b/components/data-section.tsx
@@ -2,14 +2,12 @@
import React from 'react'
+import type { DataPart } from '@/lib/types/ai'
+
import { RelatedQuestions } from './related-questions'
interface DataSectionProps {
- part: {
- type: string
- data: any
- id?: string
- }
+ part: DataPart
onQuerySelect?: (query: string) => void
}
diff --git a/components/related-questions.tsx b/components/related-questions.tsx
index 5d03f2b4..9ae300a5 100644
--- a/components/related-questions.tsx
+++ b/components/related-questions.tsx
@@ -55,7 +55,7 @@ export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({
<ArrowRight className="h-4 w-4 mr-2 mt-1.5 flex-shrink-0 text-accent-foreground/50" />
<Button
variant="link"
- className="flex-1 justify-start px-0 py-1 h-fit font-semibold text-accent-foreground/50 whitespace-normal text-left"
+ className="flex-1 justify-start px-0 py-0 h-fit font-semibold text-accent-foreground/50 whitespace-normal text-left"
type="submit"
name={'related_query'}
value={item.question}
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.ts
index 7d29b1ad..7ded70e2 100644
--- a/lib/streaming/create-chat-stream-response.ts
+++ b/lib/streaming/create-chat-stream-response.ts
@@ -9,24 +9,18 @@ import {
import { researcher } from '@/lib/agents/researcher'
-import {
- createChat,
- deleteMessagesFromIndex,
- getChat as getChatAction,
- saveMessage
-} from '../actions/chat'
-import { generateRelatedQuestions } from '../agents/generate-related-questions'
+import { getChat as getChatAction } from '../actions/chat'
import { generateChatTitle } from '../agents/title-generator'
-import { updateChatTitle } from '../db/actions'
-import { generateId } from '../db/schema'
import {
getMaxAllowedTokens,
shouldTruncateMessages,
truncateMessages
} from '../utils/context-window'
-import { getTextFromParts, hasToolCalls } from '../utils/message-utils'
-import { retryDatabaseOperation } from '../utils/retry'
+import { getTextFromParts } from '../utils/message-utils'
+import { handleStreamFinish } from './helpers/handle-stream-finish'
+import { prepareMessages } from './helpers/prepare-messages'
+import type { StreamContext } from './helpers/types'
import { BaseStreamConfig } from './types'
// Constants
@@ -66,103 +60,27 @@ export async function createChatStreamResponse(
})
}
+ // Create stream context
+ const context: StreamContext = {
+ chatId,
+ userId,
+ modelId: `${model.providerId}:${model.id}`,
+ messageId,
+ trigger,
+ initialChat,
+ abortSignal
+ }
+
// Create the stream
const stream = createUIMessageStream<UIMessage>({
execute: async ({ writer }: { writer: UIMessageStreamWriter }) => {
try {
- let messagesToModel: UIMessage[]
-
- if (trigger === 'regenerate-assistant-message' && messageId) {
- // Handle regeneration
- // Use cached chat data or fetch if not available
- const currentChat =
- initialChat || (await getChatAction(chatId, userId))
- if (!currentChat || !currentChat.messages.length) {
- throw new Error('No messages found')
- }
-
- let messageIndex = currentChat.messages.findIndex(
- m => m.id === messageId
- )
-
- // Fallback: If message not found by ID, try to find by position
- // This handles cases where AI SDK generates different IDs
- if (messageIndex === -1) {
- // For assistant message regeneration: find the last assistant message
- const lastAssistantIndex = currentChat.messages.findLastIndex(
- m => m.role === 'assistant'
- )
-
- // For user message edit: find the last user message
- const lastUserIndex = currentChat.messages.findLastIndex(
- m => m.role === 'user'
- )
-
- // Use the most recent message (either user or assistant)
- if (lastAssistantIndex >= 0 || lastUserIndex >= 0) {
- messageIndex = Math.max(lastAssistantIndex, lastUserIndex)
- } else {
- throw new Error(
- `Message ${messageId} not found and no fallback available`
- )
- }
- }
-
- // 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 (!initialChat) {
- 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]
- }
+ // Prepare messages for the model
+ const messagesToModel = await prepareMessages(context, message)
// Get the researcher agent
const researchAgent = researcher({
- model: modelId,
+ model: context.modelId,
searchMode,
abortSignal
})
@@ -188,7 +106,7 @@ export async function createChatStreamResponse(
const userContent = getTextFromParts(message.parts)
titlePromise = generateChatTitle({
userMessageContent: userContent,
- modelId,
+ modelId: context.modelId,
abortSignal
}).catch(error => {
console.error('Error generating title:', error)
@@ -201,77 +119,13 @@ export async function createChatStreamResponse(
researchAgent.stream({ messages: modelMessages }).toUIMessageStream({
onFinish: async ({ responseMessage, isAborted }) => {
if (isAborted || !responseMessage) return
-
- // Generate related questions if there are tool calls
- if (hasToolCalls(responseMessage)) {
- const questionPartId = generateId()
-
- try {
- // Send initial loading state
- writer.write({
- type: 'data-relatedQuestions',
- id: questionPartId,
- data: { status: 'loading' }
- })
-
- const relatedQuestions = await generateRelatedQuestions(
- modelId,
- [...messagesToModel, responseMessage],
- abortSignal
- )
-
- // Add to message parts for saving (with actual data)
- responseMessage.parts.push({
- type: 'data-relatedQuestions',
- id: questionPartId,
- data: {
- status: 'success',
- questions: relatedQuestions.questions
- }
- })
-
- // Update stream with actual data (same ID)
- writer.write({
- type: 'data-relatedQuestions',
- id: questionPartId,
- data: {
- status: 'success',
- questions: relatedQuestions.questions
- }
- })
- } catch (error) {
- console.error('Error generating related questions:', error)
- // Send error state with same ID
- writer.write({
- type: 'data-relatedQuestions',
- id: questionPartId,
- data: { status: 'error' }
- })
- }
- }
-
- // Wait for title generation if it was started
- const chatTitle = titlePromise ? await titlePromise : undefined
-
- // Save message with retry logic
- saveMessage(chatId, responseMessage).catch(async error => {
- console.error('Error saving message:', error)
- try {
- await retryDatabaseOperation(
- () => saveMessage(chatId, responseMessage),
- 'save message'
- )
- } catch (retryError) {
- console.error('Failed to save after retries:', retryError)
- }
- })
-
- // Update title after message is saved
- if (chatTitle && chatTitle !== DEFAULT_CHAT_TITLE) {
- updateChatTitle(chatId, chatTitle).catch(error =>
- console.error('Error updating title:', error)
- )
- }
+ await handleStreamFinish(
+ writer,
+ responseMessage,
+ messagesToModel,
+ context,
+ titlePromise
+ )
}
})
)
diff --git a/lib/streaming/helpers/handle-stream-finish.ts b/lib/streaming/helpers/handle-stream-finish.ts
new file mode 100644
index 00000000..ce592702
--- /dev/null
+++ b/lib/streaming/helpers/handle-stream-finish.ts
@@ -0,0 +1,89 @@
+import { UIMessage, UIMessageStreamWriter } from 'ai'
+
+import { saveMessage } from '@/lib/actions/chat'
+import { generateRelatedQuestions } from '@/lib/agents/generate-related-questions'
+import { updateChatTitle } from '@/lib/db/actions'
+import { generateId } from '@/lib/db/schema'
+import { hasToolCalls } from '@/lib/utils/message-utils'
+import { retryDatabaseOperation } from '@/lib/utils/retry'
+
+import type { StreamContext } from './types'
+
+const DEFAULT_CHAT_TITLE = 'Untitled'
+
+export async function handleStreamFinish(
+ writer: UIMessageStreamWriter,
+ responseMessage: UIMessage,
+ messagesToModel: UIMessage[],
+ context: StreamContext,
+ titlePromise?: Promise<string>
+) {
+ const { chatId, modelId, abortSignal } = context
+
+ // Generate related questions if there are tool calls
+ if (hasToolCalls(responseMessage as UIMessage | null)) {
+ const questionPartId = generateId()
+
+ try {
+ writer.write({
+ type: 'data-relatedQuestions',
+ id: questionPartId,
+ data: { status: 'loading' }
+ })
+
+ const relatedQuestions = await generateRelatedQuestions(
+ modelId,
+ [...messagesToModel, responseMessage],
+ abortSignal
+ )
+
+ responseMessage.parts.push({
+ type: 'data-relatedQuestions',
+ id: questionPartId,
+ data: {
+ status: 'success',
+ questions: relatedQuestions.questions
+ }
+ })
+
+ writer.write({
+ type: 'data-relatedQuestions',
+ id: questionPartId,
+ data: {
+ status: 'success',
+ questions: relatedQuestions.questions
+ }
+ })
+ } catch (error) {
+ console.error('Error generating related questions:', error)
+ writer.write({
+ type: 'data-relatedQuestions',
+ id: questionPartId,
+ data: { status: 'error' }
+ })
+ }
+ }
+
+ // Wait for title generation if it was started
+ const chatTitle = titlePromise ? await titlePromise : undefined
+
+ // Save message with retry logic
+ saveMessage(chatId, responseMessage).catch(async error => {
+ console.error('Error saving message:', error)
+ try {
+ await retryDatabaseOperation(
+ () => saveMessage(chatId, responseMessage),
+ 'save message'
+ )
+ } catch (retryError) {
+ console.error('Failed to save after retries:', retryError)
+ }
+ })
+
+ // Update title after message is saved
+ if (chatTitle && chatTitle !== DEFAULT_CHAT_TITLE) {
+ updateChatTitle(chatId, chatTitle).catch(error =>
+ console.error('Error updating title:', error)
+ )
+ }
+}
diff --git a/lib/streaming/helpers/prepare-messages.ts b/lib/streaming/helpers/prepare-messages.ts
new file mode 100644
index 00000000..e8d880a8
--- /dev/null
+++ b/lib/streaming/helpers/prepare-messages.ts
@@ -0,0 +1,87 @@
+import { UIMessage } from 'ai'
+
+import {
+ createChat,
+ deleteMessagesFromIndex,
+ getChat as getChatAction,
+ saveMessage
+} from '@/lib/actions/chat'
+import { generateId } from '@/lib/db/schema'
+
+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 } = context
+
+ if (trigger === 'regenerate-assistant-message' && messageId) {
+ // Handle regeneration
+ const currentChat = initialChat || (await getChatAction(chatId, userId))
+ if (!currentChat || !currentChat.messages.length) {
+ throw new Error('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 Error(
+ `Message ${messageId} not found and no fallback available`
+ )
+ }
+ }
+
+ const targetMessage = currentChat.messages[messageIndex]
+ if (targetMessage.role === 'assistant') {
+ await deleteMessagesFromIndex(chatId, messageId)
+ return currentChat.messages.slice(0, messageIndex)
+ } else {
+ // User message edit
+ if (message && message.id === messageId) {
+ await saveMessage(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)
+ return (
+ updatedChat?.messages || currentChat.messages.slice(0, messageIndex + 1)
+ )
+ }
+ } else {
+ // Handle normal message submission
+ if (!message) {
+ throw new Error('No message provided')
+ }
+
+ const messageWithId = {
+ ...message,
+ id: message.id || generateId()
+ }
+
+ if (!initialChat) {
+ await createChat(chatId, DEFAULT_CHAT_TITLE)
+ }
+
+ await saveMessage(chatId, messageWithId)
+ const updatedChat = await getChatAction(chatId, userId)
+ return updatedChat?.messages || [messageWithId]
+ }
+}
diff --git a/lib/streaming/helpers/types.ts b/lib/streaming/helpers/types.ts
new file mode 100644
index 00000000..c3edc810
--- /dev/null
+++ b/lib/streaming/helpers/types.ts
@@ -0,0 +1,9 @@
+export interface StreamContext {
+ chatId: string
+ userId: string
+ modelId: string
+ messageId?: string
+ trigger?: string
+ initialChat: any
+ abortSignal?: AbortSignal
+}
diff --git a/lib/types/ai.ts b/lib/types/ai.ts
index 4ca0db14..640184ba 100644
--- a/lib/types/ai.ts
+++ b/lib/types/ai.ts
@@ -20,6 +20,15 @@ export type UIDataTypes = {
relatedQuestions?: RelatedQuestionsData
}
+// Data part types for DataSection
+export type DataRelatedQuestionsPart = {
+ type: 'data-relatedQuestions'
+ id?: string
+ data: RelatedQuestionsData
+}
+
+export type DataPart = DataRelatedQuestionsPart
+
export type UITools = {
search: InferUITool<typeof searchTool>
fetch: InferUITool<typeof fetchTool>