An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
chore: Update UI Stream
Yoshiki Miura162ffd6ac8d3
diff --git a/app/actions.tsx b/app/actions.tsxindex 67a2e3c2..a1163249 100644--- a/app/actions.tsx+++ b/app/actions.tsx@@ -24,6 +24,7 @@ import { VideoSearchSection } from '@/components/video-search-section'import { transformToolMessages } from '@/lib/utils'import { AnswerSection } from '@/components/answer-section'import { ErrorCard } from '@/components/error-card'+import { use } from 'react'async function submit(formData?: FormData,@@ -101,6 +102,9 @@ async function submit(}async function processEvents() {+ // Show the spinner+ uiStream.append(<Spinner />)+let action = { object: { next: 'proceed' } }// If the user skips the task, we proceed to the searchif (!skip) action = (await taskManager(messages)) ?? action@@ -131,24 +135,26 @@ async function submit(// Generate the answerlet answer = ''+ let stopReason = ''let toolOutputs: ToolResultPart[] = []let errorOccurred = false+const streamText = createStreamableValue<string>()+ uiStream.update(+ <AnswerSection result={streamText.value} hasHeader={false} />+ )// If useSpecificAPI is enabled, only function calls will be made// If not using a tool, this model generates the answerwhile (useSpecificAPI- ? toolOutputs.length === 0 && answer.length === 0- : answer.length === 0 && !errorOccurred+ ? toolOutputs.length === 0 && answer.length === 0 && !errorOccurred+ : stopReason !== 'stop' && !errorOccurred) {// Search the web and generate the answer- const { fullResponse, hasError, toolResponses } = await researcher(- uiStream,- streamText,- messages,- useSpecificAPI- )+ const { fullResponse, hasError, toolResponses, finishReason } =+ await researcher(uiStream, streamText, messages)+ stopReason = finishReason || ''answer = fullResponsetoolOutputs = toolResponseserrorOccurred = hasError@@ -177,13 +183,13 @@ async function submit(// modify the messages to be used by the specific modelconst modifiedMessages = transformToolMessages(messages)const latestMessages = modifiedMessages.slice(maxMessages * -1)- const { response, hasError } = await writer(- uiStream,- streamText,- latestMessages- )+ const { response, hasError } = await writer(uiStream, latestMessages)answer = responseerrorOccurred = hasError+ messages.push({+ role: 'assistant',+ content: answer+ })}if (!errorOccurred) {diff --git a/components/answer-section.tsx b/components/answer-section.tsxindex 28370c97..3d7c7cec 100644--- a/components/answer-section.tsx+++ b/components/answer-section.tsx@@ -8,9 +8,13 @@ import { useEffect, useState } from 'react'export type AnswerSectionProps = {result?: StreamableValue<string>+ hasHeader?: boolean}-export function AnswerSection({ result }: AnswerSectionProps) {+export function AnswerSection({+ result,+ hasHeader = true+}: AnswerSectionProps) {const [data, error, pending] = useStreamableValue(result)const [content, setContent] = useState<string>('')@@ -22,7 +26,7 @@ export function AnswerSection({ result }: AnswerSectionProps) {return (<div>{content.length > 0 ? (- <Section title="Answer">+ <Section title={hasHeader ? 'Answer' : undefined}><BotMessage content={content} /></Section>) : (diff --git a/lib/agents/query-suggestor.tsx b/lib/agents/query-suggestor.tsxindex 6bb72753..dbd5e938 100644--- a/lib/agents/query-suggestor.tsx+++ b/lib/agents/query-suggestor.tsx@@ -16,6 +16,13 @@ export async function querySuggestor(</Section>)+ const lastMessages = messages.slice(-1).map(message => {+ return {+ ...message,+ role: 'user'+ }+ }) as CoreMessage[]+let finalRelatedQueries: PartialRelated = {}await streamObject({model: getModel(),@@ -33,7 +40,7 @@ export async function querySuggestor(Aim to create queries that progressively delve into more specific aspects, implications, or adjacent topics related to the initial query. The goal is to anticipate the user's potential information needs and guide them towards a more comprehensive understanding of the subject matter.Please match the language of the response to the user's language.`,- messages,+ messages: lastMessages,schema: relatedSchema}).then(async result => {diff --git a/lib/agents/researcher.tsx b/lib/agents/researcher.tsxindex d10a96b8..78d1b3a4 100644--- a/lib/agents/researcher.tsx+++ b/lib/agents/researcher.tsx@@ -3,16 +3,15 @@ import { CoreMessage, ToolCallPart, ToolResultPart, streamText } from 'ai'import { getTools } from './tools'import { getModel, transformToolMessages } from '../utils'import { AnswerSection } from '@/components/answer-section'-import { AnswerSectionGenerated } from '@/components/answer-section-generated'export async function researcher(uiStream: ReturnType<typeof createStreamableUI>,streamableText: ReturnType<typeof createStreamableValue<string>>,- messages: CoreMessage[],- useSpecificModel?: boolean+ messages: CoreMessage[]) {let fullResponse = ''let hasError = false+ let finishReason = ''// Transform the messages if using Ollama providerlet processedMessages = messages@@ -25,7 +24,9 @@ export async function researcher(const includeToolResponses = messages.some(message => message.role === 'tool')const useSubModel = useOllamaProvider && includeToolResponses- const answerSection = <AnswerSection result={streamableText.value} />+ const streambleAnswer = createStreamableValue<string>('')+ const answerSection = <AnswerSection result={streambleAnswer.value} />+const currentDate = new Date().toLocaleString()const result = await streamText({model: getModel(useSubModel),@@ -39,12 +40,18 @@ export async function researcher(The number must always match the order of the search results.The retrieve tool can only be used with URLs provided by the user. URLs from search results cannot be used.If it is a domain instead of a URL, specify it in the include_domains of the search tool.- Please match the language of the response to the user's language. Current date and time: ${currentDate}`,+ Please match the language of the response to the user's language. Current date and time: ${currentDate}+ `,messages: processedMessages,tools: getTools({uiStream,fullResponse- })+ }),+ onFinish: async event => {+ finishReason = event.finishReason+ fullResponse = event.text+ streambleAnswer.done()+ }}).catch(err => {hasError = truefullResponse = 'Error: ' + err.message@@ -56,6 +63,11 @@ export async function researcher(return { result, fullResponse, hasError, toolResponses: [] }}+ const hasToolResult = messages.some(message => message.role === 'tool')+ if (hasToolResult) {+ uiStream.append(answerSection)+ }+// Process the responseconst toolCalls: ToolCallPart[] = []const toolResponses: ToolResultPart[] = []@@ -64,17 +76,17 @@ export async function researcher(case 'text-delta':if (delta.textDelta) {fullResponse += delta.textDelta- streamableText.update(fullResponse)+ if (hasToolResult) {+ streambleAnswer.update(fullResponse)+ } else {+ streamableText.update(fullResponse)+ }}breakcase 'tool-call':toolCalls.push(delta)breakcase 'tool-result':- // Append the answer section if the specific model is not used- if (!useSpecificModel && toolResponses.length === 0 && delta.result) {- uiStream.append(answerSection)- }if (!delta.result) {hasError = true}@@ -97,5 +109,5 @@ export async function researcher(messages.push({ role: 'tool', content: toolResponses })}- return { result, fullResponse, hasError, toolResponses }+ return { result, fullResponse, hasError, toolResponses, finishReason }}diff --git a/lib/agents/tools/search.tsx b/lib/agents/tools/search.tsxindex e288f117..a6f4a65b 100644--- a/lib/agents/tools/search.tsx+++ b/lib/agents/tools/search.tsx@@ -23,7 +23,7 @@ export const searchTool = ({ uiStream, fullResponse }: ToolProps) => ({let hasError = false// Append the search sectionconst streamResults = createStreamableValue<string>()- uiStream.append(+ uiStream.update(<SearchSectionresult={streamResults.value}includeDomains={include_domains}diff --git a/lib/agents/writer.tsx b/lib/agents/writer.tsxindex ca9c074f..0b1496eb 100644--- a/lib/agents/writer.tsx+++ b/lib/agents/writer.tsx@@ -6,12 +6,12 @@ import { AnswerSectionGenerated } from '@/components/answer-section-generated'export async function writer(uiStream: ReturnType<typeof createStreamableUI>,- streamableText: ReturnType<typeof createStreamableValue<string>>,messages: CoreMessage[]) {let fullResponse = ''let hasError = false- const answerSection = <AnswerSection result={streamableText.value} />+ const streamableAnswer = createStreamableValue<string>('')+ const answerSection = <AnswerSection result={streamableAnswer.value} />uiStream.append(answerSection)const openai = createOpenAI({@@ -31,23 +31,22 @@ export async function writer(`,messages,onFinish: event => {- // If the response is generated, update the generated answer section- // There is a bug where a new instance of the answer section is displayed once when the next section is added- uiStream.update(<AnswerSectionGenerated result={event.text} />)+ fullResponse = event.text+ streamableAnswer.done(event.text)}}).then(async result => {for await (const text of result.textStream) {if (text) {fullResponse += text- streamableText.update(fullResponse)+ streamableAnswer.update(fullResponse)}}}).catch(err => {hasError = truefullResponse = 'Error: ' + err.message- streamableText.update(fullResponse)+ streamableAnswer.update(fullResponse)})return { response: fullResponse, hasError }