An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
fix: Add generated answer section component
Yoshiki Miurac830a97a6f53
diff --git a/components/answer-section-generated.tsx b/components/answer-section-generated.tsxnew file mode 100644index 00000000..c3b03b27--- /dev/null+++ b/components/answer-section-generated.tsx@@ -0,0 +1,18 @@+'use client'++import { Section } from './section'+import { BotMessage } from './message'++export type AnswerSectionProps = {+ result: string+}++export function AnswerSectionGenerated({ result }: AnswerSectionProps) {+ return (+ <div>+ <Section title="Answer">+ <BotMessage content={result} />+ </Section>+ </div>+ )+}diff --git a/lib/agents/researcher.tsx b/lib/agents/researcher.tsxindex 2d38cd3e..c1dbc074 100644--- a/lib/agents/researcher.tsx+++ b/lib/agents/researcher.tsx@@ -3,6 +3,7 @@ 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>,@@ -43,7 +44,14 @@ export async function researcher(tools: getTools({uiStream,fullResponse- })+ }),+ 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+ if (event.text.length > 0) {+ uiStream.update(<AnswerSectionGenerated result={event.text} />)+ }+ }}).catch(err => {hasError = truefullResponse = 'Error: ' + err.messagediff --git a/lib/agents/writer.tsx b/lib/agents/writer.tsxindex 5e2291d3..ca9c074f 100644--- a/lib/agents/writer.tsx+++ b/lib/agents/writer.tsx@@ -2,6 +2,7 @@ import { createStreamableUI, createStreamableValue } from 'ai/rsc'import { CoreMessage, streamText } from 'ai'import { createOpenAI } from '@ai-sdk/openai'import { AnswerSection } from '@/components/answer-section'+import { AnswerSectionGenerated } from '@/components/answer-section-generated'export async function writer(uiStream: ReturnType<typeof createStreamableUI>,@@ -28,7 +29,12 @@ export async function writer(Link format: [link text](url)Image format: `,- messages+ 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} />)+ }}).then(async result => {for await (const text of result.textStream) {