An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Refactor code and fix error handling
Yoshiki Miura048ce2ad8720
diff --git a/app/action.tsx b/app/action.tsxindex 7e54db5e..b5686868 100644--- a/app/action.tsx+++ b/app/action.tsx@@ -44,7 +44,7 @@ async function submit(formData?: FormData, skip?: boolean) {if (action.object.next === 'inquire') {// Generate inquiry- const inquiry = await inquire(uiStream, messages, userInput)+ const inquiry = await inquire(uiStream, messages)uiStream.done()isGenerating.done()diff --git a/components/copilot.tsx b/components/copilot.tsxindex c3c40dfa..83b8eccd 100644--- a/components/copilot.tsx+++ b/components/copilot.tsx@@ -13,14 +13,10 @@ import { IconLogo } from './ui/icons'import { cn } from '@/lib/utils'export type CopilotProps = {- initialQuery?: stringinquiry?: PartialInquiry}-export const Copilot: React.FC<CopilotProps> = ({- initialQuery,- inquiry-}: CopilotProps) => {+export const Copilot: React.FC<CopilotProps> = ({ inquiry }: CopilotProps) => {const [completed, setCompleted] = useState(false)const [query, setQuery] = useState('')const [skipped, setSkipped] = useState(false)@@ -57,7 +53,7 @@ export const Copilot: React.FC<CopilotProps> = ({const selectedOptions = Object.entries(checkedOptions).filter(([, checked]) => checked).map(([option]) => option)- return [initialQuery, ...selectedOptions, query].filter(Boolean).join(', ')+ return [...selectedOptions, query].filter(Boolean).join(', ')}useEffect(() => {diff --git a/components/message.tsx b/components/message.tsxindex 810cddbb..74c78fc7 100644--- a/components/message.tsx+++ b/components/message.tsx@@ -10,8 +10,8 @@ export function BotMessage({}) {const [data, error, pending] = useStreamableValue(content)- // Currently, sometimes error occurs after finishing the stream. So, comment out this line.- // if (error) return <div>Error</div>+ // Currently, sometimes error occurs after finishing the stream.+ if (error) return <div>Error</div>return (<MemoizedReactMarkdown className="prose-sm prose-neutral prose-a:text-accent-foreground/50">diff --git a/components/search-results-image.tsx b/components/search-results-image.tsxindex 3bbc406a..e3fd2e2d 100644--- a/components/search-results-image.tsx+++ b/components/search-results-image.tsx@@ -19,6 +19,7 @@ import {CarouselPrevious} from '@/components/ui/carousel'import { useEffect, useState } from 'react'+import { PlusCircle } from 'lucide-react'interface SearchResultsImageSectionProps {images: string[]@@ -80,6 +81,11 @@ export const SearchResultsImageSection: React.FC<)}</CardContent></Card>+ {index === 3 && images.length > 4 && (+ <div className="absolute inset-0 bg-black/30 rounded-md flex items-center justify-center text-white/80 text-sm">+ <PlusCircle size={24} />+ </div>+ )}</div></DialogTrigger><DialogContent className="sm:max-w-3xl max-h-[80vh] overflow-auto">diff --git a/lib/agents/inquire.tsx b/lib/agents/inquire.tsxindex 687ac8fa..f7e84532 100644--- a/lib/agents/inquire.tsx+++ b/lib/agents/inquire.tsx@@ -6,16 +6,14 @@ import { PartialInquiry, inquirySchema } from '@/lib/schema/inquiry'export async function inquire(uiStream: ReturnType<typeof createStreamableUI>,- messages: ExperimentalMessage[],- query?: string+ messages: ExperimentalMessage[]) {const objectStream = createStreamableValue<PartialInquiry>()- uiStream.update(<Copilot initialQuery={query} inquiry={objectStream.value} />)+ uiStream.update(<Copilot inquiry={objectStream.value} />)let finalInquiry: PartialInquiry = {}await experimental_streamObject({model: openai.chat('gpt-4-turbo-preview'),- maxTokens: 2500,system: `You are a professional web researcher tasked with deepening your understanding of the user's input through further inquiries.Only ask additional questions if absolutely necessary after receiving an initial response from the user.Structure your inquiry as follows:diff --git a/lib/agents/query-suggestor.tsx b/lib/agents/query-suggestor.tsxindex 60e792ce..8f9a4bee 100644--- a/lib/agents/query-suggestor.tsx+++ b/lib/agents/query-suggestor.tsx@@ -18,7 +18,6 @@ export async function querySuggestor(await experimental_streamObject({model: openai.chat('gpt-4-turbo-preview'),- maxTokens: 2500,system: `You are tasked as a professional web researcher to generate queries that delve deeper into the subject based on the initial query and its search results. Your goal is to formulate three related questions.For example, given the query: "Starship's third test flight key milestones",Your output should look like:diff --git a/lib/agents/researcher.tsx b/lib/agents/researcher.tsxindex 08b979a7..da206dc3 100644--- a/lib/agents/researcher.tsx+++ b/lib/agents/researcher.tsx@@ -103,6 +103,9 @@ export async function researcher(case 'tool-result':toolResponses.push(delta)break+ case 'error':+ fullResponse += `\nError occurred while executing the tool`+ break}}messages.push({