An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Add AnswerSection component for displaying bot answers
Yoshiki Miurafcb0d0a0401b
diff --git a/app/actions.tsx b/app/actions.tsxindex 0741e67b..d6a719b5 100644--- a/app/actions.tsx+++ b/app/actions.tsx@@ -16,13 +16,13 @@ import { saveChat } from '@/lib/actions/chat'import { Chat } from '@/lib/types'import { AIMessage } from '@/lib/types'import { UserMessage } from '@/components/user-message'-import { BotMessage } from '@/components/message'import { SearchSection } from '@/components/search-section'import SearchRelated from '@/components/search-related'import { CopilotDisplay } from '@/components/copilot-display'import RetrieveSection from '@/components/retrieve-section'import { VideoSearchSection } from '@/components/video-search-section'import { transformToolMessages } from '@/lib/utils'+import { AnswerSection } from '@/components/answer-section'async function submit(formData?: FormData, skip?: boolean) {'use server'@@ -365,11 +365,7 @@ export const getUIStateFromAIState = (aiState: Chat) => {case 'answer':return {id,- component: (- <Section title="Answer">- <BotMessage content={answer.value} />- </Section>- )+ component: <AnswerSection result={answer.value} />}case 'related':const relatedQueries = createStreamableValue()diff --git a/components/answer-section.tsx b/components/answer-section.tsxnew file mode 100644index 00000000..e1d9af5f--- /dev/null+++ b/components/answer-section.tsx@@ -0,0 +1,28 @@+'use client'++import { Skeleton } from '@/components/ui/skeleton'+import { Section } from './section'+import { StreamableValue, useStreamableValue } from 'ai/rsc'+import { BotMessage } from './message'++export type AnswerSectionProps = {+ result?: StreamableValue<string>+}++export function AnswerSection({ result }: AnswerSectionProps) {+ const [data, error, pending] = useStreamableValue(result)+ return (+ <div>+ {data && data.length > 0 ? (+ <Section title="Answer">+ <BotMessage content={data} />+ </Section>+ ) : (+ <div className="flex flex-col gap-2 py-2">+ <Skeleton className="h-6 w-48" />+ <Skeleton className="w-full h-6" />+ </div>+ )}+ </div>+ )+}diff --git a/components/message.tsx b/components/message.tsxindex 70d77115..8c1a5294 100644--- a/components/message.tsx+++ b/components/message.tsx@@ -1,6 +1,5 @@'use client'-import { StreamableValue, useStreamableValue } from 'ai/rsc'import { MemoizedReactMarkdown } from './ui/markdown'import rehypeExternalLinks from 'rehype-external-links'import remarkGfm from 'remark-gfm'@@ -8,17 +7,14 @@ import remarkMath from 'remark-math'import rehypeKatex from 'rehype-katex'import 'katex/dist/katex.min.css'-export function BotMessage({ content }: { content: StreamableValue<string> }) {- const [data, error, pending] = useStreamableValue(content)-- // Currently, sometimes error occurs after finishing the stream.- if (error) return <div>Error</div>-+export function BotMessage({ content }: { content: string }) {// Check if the content contains LaTeX patterns- const containsLaTeX = /\\\[([\s\S]*?)\\\]|\\\(([\s\S]*?)\\\)/.test(data || '')+ const containsLaTeX = /\\\[([\s\S]*?)\\\]|\\\(([\s\S]*?)\\\)/.test(+ content || ''+ )// Modify the content to render LaTeX equations if LaTeX patterns are found- const processedData = preprocessLaTeX(data || '')+ const processedData = preprocessLaTeX(content || '')if (containsLaTeX) {return (@@ -41,7 +37,7 @@ export function BotMessage({ content }: { content: StreamableValue<string> }) {remarkPlugins={[remarkGfm]}className="prose-sm prose-neutral prose-a:text-accent-foreground/50">- {data || ''}+ {content}</MemoizedReactMarkdown>)}diff --git a/lib/agents/researcher.tsx b/lib/agents/researcher.tsxindex 5cd2ec7c..f339d34a 100644--- a/lib/agents/researcher.tsx+++ b/lib/agents/researcher.tsx@@ -1,9 +1,8 @@import { createStreamableUI, createStreamableValue } from 'ai/rsc'import { CoreMessage, ToolCallPart, ToolResultPart, streamText } from 'ai'-import { Section } from '@/components/section'-import { BotMessage } from '@/components/message'import { getTools } from './tools'import { getModel } from '../utils'+import { AnswerSection } from '@/components/answer-section'export async function researcher(uiStream: ReturnType<typeof createStreamableUI>,@@ -13,12 +12,7 @@ export async function researcher() {let fullResponse = ''let hasError = false- const answerSection = (- <Section title="Answer">- <BotMessage content={streamableText.value} />- </Section>- )-+ const answerSection = <AnswerSection result={streamableText.value} />const currentDate = new Date().toLocaleString()const result = await streamText({model: getModel(),diff --git a/lib/agents/writer.tsx b/lib/agents/writer.tsxindex 04db752f..66f449ff 100644--- a/lib/agents/writer.tsx+++ b/lib/agents/writer.tsx@@ -1,8 +1,7 @@import { createStreamableUI, createStreamableValue } from 'ai/rsc'import { CoreMessage, streamText } from 'ai'-import { Section } from '@/components/section'-import { BotMessage } from '@/components/message'import { OpenAI } from '@ai-sdk/openai'+import { AnswerSection } from '@/components/answer-section'export async function writer(uiStream: ReturnType<typeof createStreamableUI>,@@ -11,11 +10,7 @@ export async function writer() {let fullResponse = ''let hasError = false- const answerSection = (- <Section title="Answer">- <BotMessage content={streamableText.value} />- </Section>- )+ const answerSection = <AnswerSection result={streamableText.value} />uiStream.append(answerSection)const openai = new OpenAI({