Skip to content

An AI-powered search engine with a generative UI

refactor: improve streaming architecture for related questions

Yoshiki Miura4d6992321427
diff --git a/components/related-questions.tsx b/components/related-questions.tsx
index d91bf751..e4271055 100644
--- a/components/related-questions.tsx
+++ b/components/related-questions.tsx
@@ -6,10 +6,10 @@ import { ArrowRight } from 'lucide-react'
import type { RelatedQuestionsData } from '@/lib/types/ai'
-import { Button } from './ui/button'
-import { Skeleton } from './ui/skeleton'
import { CollapsibleMessage } from './collapsible-message'
import { Section } from './section'
+import { Button } from './ui/button'
+import { Skeleton } from './ui/skeleton'
interface RelatedQuestionsProps {
data: RelatedQuestionsData
@@ -35,7 +35,7 @@ export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({
<>
{[1, 2, 3].map((_, index) => (
<div className="flex items-start w-full" key={index}>
- <ArrowRight className="h-4 w-4 mr-2 mt-1.5 shrink-0 text-accent-foreground/50" />
+ <ArrowRight className="h-4 w-4 mr-2 mt-0.5 shrink-0 text-accent-foreground/50" />
<Skeleton className="h-6 w-full" />
</div>
))}
@@ -52,7 +52,7 @@ export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({
<>
{data.questions.map((item, index) => (
<div className="flex items-start w-full" key={index}>
- <ArrowRight className="h-4 w-4 mr-2 mt-1.5 shrink-0 text-accent-foreground/50" />
+ <ArrowRight className="h-4 w-4 mr-2 mt-0.5 shrink-0 text-accent-foreground/50" />
<Button
variant="link"
className="flex-1 justify-start px-0 py-0 h-fit font-semibold text-accent-foreground/50 whitespace-normal text-left"
diff --git a/lib/agents/generate-related-questions.ts b/lib/agents/generate-related-questions.ts
index b17d7fee..1d67d45e 100644
--- a/lib/agents/generate-related-questions.ts
+++ b/lib/agents/generate-related-questions.ts
@@ -1,4 +1,4 @@
-import { convertToModelMessages, generateObject, type UIMessage } from 'ai'
+import { generateObject, type ModelMessage } from 'ai'
import { z } from 'zod'
import { RELATED_QUESTIONS_MODEL_CONFIG } from '../config/model-types'
@@ -18,16 +18,13 @@ const relatedQuestionsSchema = z.object({
})
export async function generateRelatedQuestions(
- messages: UIMessage[],
+ messages: ModelMessage[],
abortSignal?: AbortSignal,
parentTraceId?: string
) {
// Use the related questions model configuration
const modelId = `${RELATED_QUESTIONS_MODEL_CONFIG.providerId}:${RELATED_QUESTIONS_MODEL_CONFIG.id}`
- // Convert UIMessages to ModelMessages
- const modelMessages = convertToModelMessages(messages)
-
const { object } = await generateObject({
model: getModel(modelId),
schema: relatedQuestionsSchema,
@@ -36,7 +33,7 @@ export async function generateRelatedQuestions(
'Generate 3 concise follow-up questions (max 10-12 words each)',
system: RELATED_QUESTIONS_PROMPT,
messages: [
- ...modelMessages,
+ ...messages,
{
role: 'user',
content:
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.ts
index 521d6306..055b1bbe 100644
--- a/lib/streaming/create-chat-stream-response.ts
+++ b/lib/streaming/create-chat-stream-response.ts
@@ -25,6 +25,7 @@ import { perfLog, perfTime } from '../utils/perf-logging'
import { filterReasoningParts } from './helpers/filter-reasoning-parts'
import { handleStreamFinish } from './helpers/handle-stream-finish'
import { prepareMessages } from './helpers/prepare-messages'
+import { streamRelatedQuestions } from './helpers/stream-related-questions'
import type { StreamContext } from './helpers/types'
import { BaseStreamConfig } from './types'
@@ -107,6 +108,9 @@ export async function createChatStreamResponse(
isNewChat
}
+ // Declare titlePromise in outer scope for onFinish access
+ let titlePromise: Promise<string> | undefined
+
// Create the stream
const stream = createUIMessageStream<UIMessage>({
execute: async ({ writer }: { writer: UIMessageStreamWriter }) => {
@@ -152,7 +156,6 @@ export async function createChatStreamResponse(
}
// Start title generation in parallel if it's a new chat
- let titlePromise: Promise<string> | undefined
if (!initialChat && message) {
const userContent = getTextFromParts(message.parts)
titlePromise = generateChatTitle({
@@ -166,21 +169,29 @@ export async function createChatStreamResponse(
})
}
+ const result = researchAgent.stream({ messages: modelMessages })
+ result.consumeStream()
// Stream with the research agent
- writer.merge(
- researchAgent.stream({ messages: modelMessages }).toUIMessageStream({
- onFinish: async ({ responseMessage, isAborted }) => {
- if (isAborted || !responseMessage) return
- await handleStreamFinish(
- writer,
- responseMessage,
- messagesToModel,
- context,
- titlePromise
- )
- }
- })
- )
+ writer.merge(result.toUIMessageStream())
+
+ const responseMessages = (await result.response).messages
+ // Generate related questions
+ if (responseMessages && responseMessages.length > 0) {
+ // Find the last user message
+ const lastUserMessage = [...modelMessages]
+ .reverse()
+ .find(msg => msg.role === 'user')
+ const messagesForQuestions = lastUserMessage
+ ? [lastUserMessage, ...responseMessages]
+ : responseMessages
+
+ await streamRelatedQuestions(
+ writer,
+ messagesForQuestions,
+ abortSignal,
+ parentTraceId
+ )
+ }
} catch (error) {
console.error('Stream execution error:', error)
throw error // This error will be handled by the onError callback
@@ -194,6 +205,18 @@ export async function createChatStreamResponse(
onError: (error: any) => {
// console.error('Stream error:', error)
return error instanceof Error ? error.message : String(error)
+ },
+ onFinish: async ({ responseMessage, isAborted }) => {
+ if (isAborted || !responseMessage) return
+
+ // Handle stream finish tasks like saving message and updating title
+ await handleStreamFinish(
+ responseMessage,
+ chatId,
+ userId,
+ titlePromise,
+ parentTraceId
+ )
}
})
diff --git a/lib/streaming/helpers/handle-stream-finish.ts b/lib/streaming/helpers/handle-stream-finish.ts
index a072d62c..31f7d75a 100644
--- a/lib/streaming/helpers/handle-stream-finish.ts
+++ b/lib/streaming/helpers/handle-stream-finish.ts
@@ -1,26 +1,19 @@
-import { UIMessage, UIMessageStreamWriter } from 'ai'
+import { UIMessage } from 'ai'
import { upsertMessage } 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 { perfTime } from '@/lib/utils/perf-logging'
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>
+ chatId: string,
+ userId: string,
+ titlePromise?: Promise<string>,
+ parentTraceId?: string
) {
- const { chatId, userId, modelId, abortSignal, parentTraceId } = context
-
// Attach metadata to the response message if we have a traceId
if (parentTraceId) {
responseMessage.metadata = {
@@ -29,50 +22,6 @@ export async function handleStreamFinish(
}
}
- // 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(
- [...messagesToModel, responseMessage],
- abortSignal,
- parentTraceId
- )
-
- 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
diff --git a/lib/streaming/helpers/stream-initial-response.ts b/lib/streaming/helpers/stream-initial-response.ts
new file mode 100644
index 00000000..0067ea21
--- /dev/null
+++ b/lib/streaming/helpers/stream-initial-response.ts
@@ -0,0 +1,44 @@
+import type { ModelMessage, UIMessage, UIMessageStreamWriter } from 'ai'
+
+import type { researcher } from '@/lib/agents/researcher'
+import { hasToolCalls } from '@/lib/utils/message-utils'
+
+/**
+ * Streams the initial response from the research agent
+ * Returns the response messages for use in follow-up operations
+ */
+export async function streamInitialResponse(
+ researchAgent: ReturnType<typeof researcher>,
+ modelMessages: ModelMessage[],
+ writer: UIMessageStreamWriter
+): Promise<{
+ responseMessage: UIMessage | null
+ isAborted: boolean
+ hasTools: boolean
+}> {
+ // Stream the initial response
+ const streamResult = researchAgent.stream({ messages: modelMessages })
+
+ // Track the response message
+ let responseMessage: UIMessage | null = null
+ let isAborted = false
+
+ // Merge the stream into the UIMessageStream
+ writer.merge(
+ streamResult.toUIMessageStream({
+ onFinish: async ({ responseMessage: msg, isAborted: aborted }) => {
+ responseMessage = msg
+ isAborted = aborted
+ }
+ })
+ )
+
+ // Wait for the stream to complete
+ await streamResult.consumeStream()
+
+ return {
+ responseMessage,
+ isAborted,
+ hasTools: responseMessage ? hasToolCalls(responseMessage) : false
+ }
+}
diff --git a/lib/streaming/helpers/stream-related-questions.ts b/lib/streaming/helpers/stream-related-questions.ts
new file mode 100644
index 00000000..031b9267
--- /dev/null
+++ b/lib/streaming/helpers/stream-related-questions.ts
@@ -0,0 +1,64 @@
+import { ModelMessage, UIMessageStreamWriter } from 'ai'
+
+import { generateRelatedQuestions } from '@/lib/agents/generate-related-questions'
+import { generateId } from '@/lib/db/schema'
+
+/**
+ * Generates and streams related questions if there are tool calls in the response
+ */
+export async function streamRelatedQuestions(
+ writer: UIMessageStreamWriter,
+ messages: ModelMessage[],
+ abortSignal?: AbortSignal,
+ parentTraceId?: string
+): Promise<{ questionPartId?: string; questions?: any[] }> {
+ // Check if the last message has tool calls
+ const lastMessage = messages[messages.length - 1]
+ if (!lastMessage || lastMessage.role !== 'assistant') {
+ return {}
+ }
+
+ const questionPartId = generateId()
+
+ try {
+ // Write loading state
+ writer.write({
+ type: 'data-relatedQuestions',
+ id: questionPartId,
+ data: { status: 'loading' }
+ })
+
+ // Generate related questions
+ const relatedQuestions = await generateRelatedQuestions(
+ messages,
+ abortSignal,
+ parentTraceId
+ )
+
+ // Write success state
+ writer.write({
+ type: 'data-relatedQuestions',
+ id: questionPartId,
+ data: {
+ status: 'success',
+ questions: relatedQuestions.questions
+ }
+ })
+
+ return {
+ questionPartId,
+ questions: relatedQuestions.questions
+ }
+ } catch (error) {
+ console.error('Error generating related questions:', error)
+
+ // Write error state
+ writer.write({
+ type: 'data-relatedQuestions',
+ id: questionPartId,
+ data: { status: 'error' }
+ })
+
+ return { questionPartId }
+ }
+}