An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Stream related questions incrementally
Yoshiki Miura6a5bf9dd75c1
diff --git a/components/related-questions.tsx b/components/related-questions.tsxindex 48d328e3..fc1cebfb 100644--- a/components/related-questions.tsx+++ b/components/related-questions.tsx@@ -20,6 +20,23 @@ export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({data,onQuerySelect}) => {+ const renderQuestionButtons = (questions: Array<{ question: string }>) =>+ questions.map((item, index) => (+ <div className="flex items-start w-full" key={index}>+ <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"+ type="submit"+ name={'related_query'}+ value={item.question}+ onClick={() => onQuerySelect(item.question)}+ >+ {item.question}+ </Button>+ </div>+ ))+return (<CollapsibleMessagerole="assistant"@@ -31,6 +48,24 @@ export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({><Section title="Related" className="pt-0 pb-4"><div className="flex flex-col gap-2">+ {data.status === 'streaming' && data.questions && (+ // Show received questions immediately while the rest stream+ <>+ {renderQuestionButtons(data.questions)}+ {Array.from({+ length: Math.max(0, 3 - data.questions.length)+ }).map((_, index) => (+ <div+ className="flex items-start w-full"+ key={`placeholder-${index}`}+ >+ <ArrowRight className="h-4 w-4 mr-2 mt-0.5 shrink-0 text-accent-foreground/50" />+ <Skeleton className="h-6 w-full" />+ </div>+ ))}+ </>+ )}+{data.status === 'loading' && (<>{[1, 2, 3].map((_, index) => (@@ -49,23 +84,7 @@ export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({)}{data.status === 'success' && 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-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"- type="submit"- name={'related_query'}- value={item.question}- onClick={() => onQuerySelect(item.question)}- >- {item.question}- </Button>- </div>- ))}- </>+ <>{renderQuestionButtons(data.questions)}</>)}</div></Section>diff --git a/lib/agents/generate-related-questions.ts b/lib/agents/generate-related-questions.tsindex 11784d35..f895eb69 100644--- a/lib/agents/generate-related-questions.ts+++ b/lib/agents/generate-related-questions.ts@@ -1,23 +1,13 @@-import { generateObject, type ModelMessage } from 'ai'-import { z } from 'zod'+import { type ModelMessage, streamObject } from 'ai'import { getRelatedQuestionsModel } from '../config/model-types'+import { relatedQuestionSchema } from '../schema/related'import { getModel } from '../utils/registry'import { isTracingEnabled } from '../utils/telemetry'import { RELATED_QUESTIONS_PROMPT } from './prompts/related-questions-prompt'-const relatedQuestionsSchema = z.object({- questions: z- .array(- z.object({- question: z.string()- })- )- .length(3)-})--export async function generateRelatedQuestions(+export function createRelatedQuestionsStream(messages: ModelMessage[],abortSignal?: AbortSignal,parentTraceId?: string@@ -26,12 +16,13 @@ export async function generateRelatedQuestions(const relatedModel = getRelatedQuestionsModel()const modelId = `${relatedModel.providerId}:${relatedModel.id}`- const { object } = await generateObject({+ return streamObject({model: getModel(modelId),- schema: relatedQuestionsSchema,- schemaName: 'RelatedQuestions',+ output: 'array',+ schema: relatedQuestionSchema,+ schemaName: 'RelatedQuestion',schemaDescription:- 'Generate 3 concise follow-up questions (max 10-12 words each)',+ 'Generate a concise follow-up question (max 10-12 words)',system: RELATED_QUESTIONS_PROMPT,messages: [...messages,@@ -56,6 +47,4 @@ export async function generateRelatedQuestions(}}})-- return object}diff --git a/lib/schema/related.tsx b/lib/schema/related.tsxindex 6405b729..6a782fe0 100644--- a/lib/schema/related.tsx+++ b/lib/schema/related.tsx@@ -1,11 +1,10 @@import { z } from 'zod'-export const relatedSchema = z.object({- questions: z.array(- z.object({- question: z.string()- })- )+export const relatedQuestionSchema = z.object({+ question: z.string()})+export const relatedSchema = z.array(relatedQuestionSchema).length(3)++export type RelatedQuestion = z.infer<typeof relatedQuestionSchema>export type Related = z.infer<typeof relatedSchema>diff --git a/lib/streaming/helpers/stream-related-questions.ts b/lib/streaming/helpers/stream-related-questions.tsindex 031b9267..a18c720e 100644--- a/lib/streaming/helpers/stream-related-questions.ts+++ b/lib/streaming/helpers/stream-related-questions.ts@@ -1,7 +1,8 @@import { ModelMessage, UIMessageStreamWriter } from 'ai'-import { generateRelatedQuestions } from '@/lib/agents/generate-related-questions'+import { createRelatedQuestionsStream } from '@/lib/agents/generate-related-questions'import { generateId } from '@/lib/db/schema'+import { relatedSchema } from '@/lib/schema/related'/*** Generates and streams related questions if there are tool calls in the response@@ -11,7 +12,10 @@ export async function streamRelatedQuestions(messages: ModelMessage[],abortSignal?: AbortSignal,parentTraceId?: string-): Promise<{ questionPartId?: string; questions?: any[] }> {+): Promise<{+ questionPartId?: string+ questions?: Array<{ question: string }>+}> {// Check if the last message has tool callsconst lastMessage = messages[messages.length - 1]if (!lastMessage || lastMessage.role !== 'assistant') {@@ -28,26 +32,62 @@ export async function streamRelatedQuestions(data: { status: 'loading' }})- // Generate related questions- const relatedQuestions = await generateRelatedQuestions(+ const relatedQuestionsResult = createRelatedQuestionsStream(messages,abortSignal,parentTraceId)- // Write success state+ const collectedQuestions: Array<{ question: string }> = []++ for await (const question of relatedQuestionsResult.elementStream) {+ if (!question || typeof question.question !== 'string') {+ continue+ }++ collectedQuestions.push(question)++ writer.write({+ type: 'data-relatedQuestions',+ id: questionPartId,+ data: {+ status: 'streaming',+ questions: [...collectedQuestions]+ }+ })+ }++ let finalQuestions = collectedQuestions++ try {+ const completedQuestions = await relatedQuestionsResult.object+ const parsedQuestions = relatedSchema.safeParse(completedQuestions)++ if (parsedQuestions.success) {+ finalQuestions = parsedQuestions.data+ } else if (Array.isArray(completedQuestions)) {+ finalQuestions = completedQuestions+ console.warn(+ 'Related questions validation failed:',+ parsedQuestions.error+ )+ }+ } catch (error) {+ console.warn('Error retrieving final related questions object:', error)+ }+writer.write({type: 'data-relatedQuestions',id: questionPartId,data: {status: 'success',- questions: relatedQuestions.questions+ questions: finalQuestions}})return {questionPartId,- questions: relatedQuestions.questions+ questions: finalQuestions}} catch (error) {console.error('Error generating related questions:', error)diff --git a/lib/types/ai.ts b/lib/types/ai.tsindex 27413efa..889a1e67 100644--- a/lib/types/ai.ts+++ b/lib/types/ai.ts@@ -26,7 +26,7 @@ export type UIMessage<> = AIMessageexport interface RelatedQuestionsData {- status: 'loading' | 'success' | 'error'+ status: 'loading' | 'streaming' | 'success' | 'error'questions?: Array<{ question: string }>}