An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
refactor: convert related questions from streaming to data parts
Yoshiki Miura1c3409b4ec3f
diff --git a/components/related-questions.tsx b/components/related-questions.tsxindex b784ec28..73b1cb52 100644--- a/components/related-questions.tsx+++ b/components/related-questions.tsx@@ -2,105 +2,51 @@import React from 'react'-import { UseChatHelpers } from '@ai-sdk/react'import { ArrowRight } from 'lucide-react'-import { Related } from '@/lib/schema/related'-import type { ToolPart, UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'-import { Button } from './ui/button'-import { Skeleton } from './ui/skeleton'import { CollapsibleMessage } from './collapsible-message'import { Section } from './section'-export interface RelatedQuestionsProps {- tool: ToolPart+interface RelatedQuestionsProps {+ questions: Array<{ question: string }>onQuerySelect: (query: string) => void- isOpen: boolean- onOpenChange: (open: boolean) => void- status?: UseChatHelpers<UIMessage<unknown, UIDataTypes, UITools>>['status']}export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({- tool,- onQuerySelect,- isOpen,- onOpenChange,- status+ questions,+ onQuerySelect}) => {- const isLoading =- status === 'submitted' ||- status === 'streaming' ||- tool.state === 'input-streaming' ||- tool.state === 'input-available'-- let data: Related | undefined = undefined-- if (tool.state === 'output-available' && tool.output) {- // Handle both array and object formats- if (Array.isArray(tool.output)) {- data = { questions: tool.output }- } else if (tool.output.questions) {- data = tool.output as Related- }- }-- if (!data && isLoading) {- return (- <CollapsibleMessage- role="assistant"- isCollapsible={false}- isOpen={isOpen}- onOpenChange={onOpenChange}- showIcon={false}- >- <Section title="Related" className="pt-0 pb-4">- <div className="flex flex-col gap-2">- {[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 flex-shrink-0 text-accent-foreground/50" />- <Skeleton className="h-6 w-full" />- </div>- ))}- </div>- </Section>- </CollapsibleMessage>- )- }-return (<CollapsibleMessagerole="assistant"isCollapsible={false}- isOpen={isOpen}- onOpenChange={onOpenChange}+ isOpen={true}+ onOpenChange={() => {}}showIcon={false}showBorder={false}><Section title="Related" className="pt-0 pb-4"><div className="flex flex-col">- {data && data.questions && Array.isArray(data.questions) ? (- 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 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"- type="submit"- name={'related_query'}- value={item.question}- onClick={() => onQuerySelect(item.question)}- >- {item.question}- </Button>- </div>- ))- ) : (- <div>No data available</div>- )}+ {questions.map((item, index) => (+ <div className="flex items-start w-full" key={index}>+ <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"+ type="submit"+ name={'related_query'}+ value={item.question}+ onClick={() => onQuerySelect(item.question)}+ >+ {item.question}+ </Button>+ </div>+ ))}</div></Section></CollapsibleMessage>)}+export default RelatedQuestionsdiff --git a/components/render-message.tsx b/components/render-message.tsxindex 1bb4d84b..37c34017 100644--- a/components/render-message.tsx+++ b/components/render-message.tsx@@ -6,6 +6,7 @@ import type { DynamicToolPart } from '@/lib/types/dynamic-tools'import { AnswerSection } from './answer-section'import { DynamicToolDisplay } from './dynamic-tool-display'import { ReasoningSection } from './reasoning-section'+import { RelatedQuestions } from './related-questions'import { ToolSection } from './tool-section'import { UserFileSection } from './user-file-section'import { UserTextSection } from './user-text-section'@@ -78,7 +79,6 @@ export function RenderMessage({case 'tool-search':case 'tool-fetch':case 'tool-askQuestion':- case 'tool-relatedQuestions':return (<ToolSectionkey={`${messageId}-tool-${index}`}@@ -100,16 +100,14 @@ export function RenderMessage({case 'text':// Show actions if:// 1. This is the last part and streaming is complete- // 2. Next part is relatedQuestions (or step-start then relatedQuestions)+ // 2. Next part is data-relatedQuestionsconst nextMessagePart = message.parts?.[index + 1]- const secondNextPart = message.parts?.[index + 2]const isStreamingComplete =status !== 'streaming' && status !== 'submitted'const isLastPart = !hasNextPartconst shouldShowActions =(isLastPart && isStreamingComplete) || // Last part and streaming done- (nextMessagePart?.type === 'step-start' &&- secondNextPart?.type === 'tool-relatedQuestions') // step-start then related questions+ nextMessagePart?.type === 'data-relatedQuestions' // Next part is related questionsreturn (<AnswerSectionkey={`${messageId}-text-${index}`}@@ -135,6 +133,14 @@ export function RenderMessage({onOpenChange={open => onOpenChange(messageId, open)}/>)+ case 'data-relatedQuestions':+ return (+ <RelatedQuestions+ key={`${messageId}-related-${index}`}+ questions={part.data.questions}+ onQuerySelect={onQuerySelect}+ />+ )// Add other part types as neededdefault:return nulldiff --git a/components/tool-section.tsx b/components/tool-section.tsxindex 1235054c..26fc57bc 100644--- a/components/tool-section.tsx+++ b/components/tool-section.tsx@@ -6,7 +6,6 @@ import type { ToolPart, UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'import FetchSection from './fetch-section'import { QuestionConfirmation } from './question-confirmation'-import { RelatedQuestions } from './related-questions'import { SearchSection } from './search-section'interface ToolSectionProps {@@ -83,16 +82,6 @@ export function ToolSection({status={status}/>)- case 'tool-relatedQuestions' as any:- return (- <RelatedQuestions- tool={tool}- isOpen={isOpen}- onOpenChange={onOpenChange}- status={status}- onQuerySelect={onQuerySelect}- />- )default:return null}diff --git a/lib/agents/generate-related-questions.ts b/lib/agents/generate-related-questions.tsindex 0a743606..07231529 100644--- a/lib/agents/generate-related-questions.ts+++ b/lib/agents/generate-related-questions.ts@@ -1,10 +1,21 @@-import { Experimental_Agent as Agent, tool } from 'ai'+import { generateObject } from 'ai'+import { z } from 'zod'-import { relatedSchema } from '../schema/related'import { getModel } from '../utils/registry'-export function generateRelatedQuestions(+const relatedQuestionsSchema = z.object({+ questions: z+ .array(+ z.object({+ question: z.string()+ })+ )+ .length(3)+})++export async function generateRelatedQuestions(model: string,+ messages: any[],abortSignal?: AbortSignal) {const systemPrompt = `You are a professional web researcher tasked with generating follow-up questions. Based on the conversation history and search results, create 3 DIFFERENT related questions that:@@ -32,26 +43,23 @@ Bad follow-ups (avoid these):- "Is Nvidia growing?" (less specific than original)- "Tell me about Nvidia" (too general)`- // Return an agent instance for related questions- return new Agent({+ const { object } = await generateObject({model: getModel(model),- system:- systemPrompt +- '\n\nGenerate 3 unique follow-up questions that explore different aspects of the topic.',- tools: {- relatedQuestions: tool({- name: 'relatedQuestions',- description:- 'Generate 3 unique follow-up questions that explore different aspects of the topic, avoiding repetition of the original query',- inputSchema: relatedSchema,- execute: async ({ questions }) => {- // Return the questions in the expected format- return { questions }- }- })- },- activeTools: ['relatedQuestions'],- toolChoice: 'required',+ schema: relatedQuestionsSchema,+ schemaName: 'RelatedQuestions',+ schemaDescription:+ 'Generate 3 unique follow-up questions that explore different aspects of the topic',+ system: systemPrompt,+ messages: [+ ...messages,+ {+ role: 'user',+ content:+ 'Based on the conversation history and search results, generate 3 unique follow-up questions that would help the user explore different aspects of the topic. Focus on questions that dig deeper into specific findings or explore related areas not yet covered.'+ }+ ],abortSignal})++ return object}diff --git a/lib/db/schema.ts b/lib/db/schema.tsindex fcef1677..361dff1b 100644--- a/lib/db/schema.ts+++ b/lib/db/schema.ts@@ -128,12 +128,6 @@ export const parts = pgTable(tool_fetch_output: json('tool_fetch_output').$type<any>(),tool_question_input: json('tool_question_input').$type<any>(),tool_question_output: json('tool_question_output').$type<any>(),- tool_relatedQuestions_input: json(- 'tool_related_questions_input'- ).$type<any>(),- tool_relatedQuestions_output: json(- 'tool_related_questions_output'- ).$type<any>(),// Dynamic tools (includes MCP and other runtime-defined tools)tool_dynamic_input: json('tool_dynamic_input').$type<any>(),diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.tsindex 2c029c42..473faa6b 100644--- a/lib/streaming/create-chat-stream-response.ts+++ b/lib/streaming/create-chat-stream-response.ts@@ -23,11 +23,7 @@ import {shouldTruncateMessages,truncateMessages} from '../utils/context-window'-import {- getTextFromParts,- hasToolCalls,- mergeUIMessages-} from '../utils/message-utils'+import { getTextFromParts, hasToolCalls } from '../utils/message-utils'import { retryDatabaseOperation } from '../utils/retry'import { BaseStreamConfig } from './types'@@ -71,7 +67,6 @@ export async function createChatStreamResponse(// Track variables for abort handlinglet researchMessage: UIMessage | null = null- let hasStartedProcessing = falselet wasAborted = false// Create the stream@@ -196,19 +191,47 @@ export async function createChatStreamResponse(})// Messages are tracked at the top level now- let relatedQuestionsMessage: UIMessage | null = null- hasStartedProcessing = true// Create a promise to track when research finishesconst researchPromise = new Promise<void>(resolve => {writer.merge(researchResult.toUIMessageStream({- sendFinish: false,- onFinish: ({ responseMessage, isAborted }) => {+ onFinish: async ({ responseMessage, isAborted }) => {if (isAborted) {wasAborted = true+ resolve()+ return}researchMessage = responseMessage++ // Generate related questions only if there are tool calls+ if (responseMessage && hasToolCalls(responseMessage)) {+ try {+ // Get research data+ const researchData = await researchResult.response++ const allMessages = [+ ...convertToModelMessages(messagesToModel),+ ...researchData.messages+ ]++ const relatedQuestions = await generateRelatedQuestions(+ modelId,+ allMessages,+ abortSignal+ )++ // Send as data part+ writer.write({+ type: 'data-relatedQuestions',+ id: generateId(),+ data: relatedQuestions+ })+ } catch (error) {+ console.error('Error generating related questions:', error)+ }+ }+resolve()}})@@ -233,11 +256,8 @@ export async function createChatStreamResponse(// At this point, TypeScript should know researchMessage is not nullconst validResearchMessage: UIMessage = researchMessage- // Check if the research message contains tool calls- const hasToolCallsInMessage = hasToolCalls(validResearchMessage)-- // If no tool calls (just answering), skip related questions- if (!hasToolCallsInMessage) {+ // Save message after research completes+ if (validResearchMessage) {// Save research message and generate title in parallelconst saveOperations: (() => Promise<any>)[] = [() => saveMessage(chatId, validResearchMessage)@@ -277,115 +297,6 @@ export async function createChatStreamResponse(// For now, we ensure the error is logged for monitoring}})-- // Send a finish message to complete the stream- writer.write({ type: 'finish' })- 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) {- writer.write({ type: 'finish' })- return- }-- // Prepare messages for related questions agent- const allMessages = [- ...convertToModelMessages(messagesToModel),- ...researchData.messages- ]-- // Get related questions agent- const relatedQuestionsAgent = generateRelatedQuestions(- modelId,- abortSignal- )- const relatedQuestionsResult = relatedQuestionsAgent.stream({- messages: allMessages- })-- // Create a promise to track when related questions finish- const relatedQuestionsPromise = new Promise<void>(resolve => {- writer.merge(- relatedQuestionsResult.toUIMessageStream({- sendStart: false,- onFinish: ({ responseMessage, isAborted }) => {- if (isAborted) {- wasAborted = true- }- relatedQuestionsMessage = responseMessage- resolve()- }- })- )- })-- // Wait for related questions to complete- await relatedQuestionsPromise-- // Check if aborted during related questions- if (wasAborted) {- writer.write({ type: 'finish' })- return- }-- // Save the complete message after both agents finish- const saveOperations: (() => Promise<any>)[] = []-- if (validResearchMessage && relatedQuestionsMessage) {- const mergedMessage = mergeUIMessages(- validResearchMessage,- relatedQuestionsMessage- )- saveOperations.push(() => saveMessage(chatId, mergedMessage))- } else if (validResearchMessage) {- // Save research message only if related questions failed- saveOperations.push(() => saveMessage(chatId, validResearchMessage))- }-- // Generate proper title after conversation starts- if (!initialChat && message) {- const userContent = getTextFromParts(message.parts)- saveOperations.push(() =>- generateChatTitle({- userMessageContent: userContent,- modelId,- abortSignal- }).then(async title => {- const { updateChatTitle } = await import('../db/actions')- return updateChatTitle(chatId, title)- })- )- }-- // Execute saves in background with exponential backoff retry- // Note: This is intentionally not awaited to avoid blocking the stream- Promise.all(saveOperations.map(op => op())).catch(async error => {- console.error('Error saving message or title:', error)-- // Retry critical save operations with backoff- try {- for (const operation of saveOperations) {- await retryDatabaseOperation(operation, 'save message/title')- }- } catch (retryError) {- console.error(- 'Failed to save after retries with backoff:',- retryError- )- // Consider implementing alerting mechanism here- // For now, we ensure the error is logged for monitoring- }- })- } catch (error) {- console.error('Error generating related questions:', error)- // Save research message even if related questions fail- await saveMessage(chatId, validResearchMessage)}} catch (error) {console.error('Stream execution error:', error)diff --git a/lib/utils/message-mapping.ts b/lib/utils/message-mapping.tsindex 567033aa..7f1baab5 100644--- a/lib/utils/message-mapping.ts+++ b/lib/utils/message-mapping.ts@@ -250,7 +250,6 @@ export function mapUIMessagePartsToDBParts(case 'tool-search':case 'tool-fetch':case 'tool-question':- case 'tool-relatedQuestions':// These are tool parts with state trackingif (!isExtendedToolPart(part)) {console.error('Invalid extended tool part:', part)@@ -368,9 +367,7 @@ export function mapDBPartToUIMessagePart(}// Special handling for tool parts that maintain their type- if (- ['search', 'fetch', 'question', 'relatedQuestions'].includes(toolName)- ) {+ if (['search', 'fetch', 'question'].includes(toolName)) {return {type: part.type as any,toolCallId: part.tool_toolCallId || '',@@ -435,8 +432,7 @@ function getToolNameFromType(toolName: string): string {search: 'search',fetch: 'fetch',askQuestion: 'question',- question: 'question',- relatedQuestions: 'relatedQuestions'+ question: 'question'}// For dynamic tools (MCP and others)