An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat: Add error handling for search tool in UI
Yoshiki Miurad37b3f7c1079
diff --git a/components/search-section.tsx b/components/search-section.tsxindex a7152b05..321ebaf5 100644--- a/components/search-section.tsx+++ b/components/search-section.tsx@@ -36,6 +36,8 @@ export function SearchSection({tool.state === 'input-streaming' || tool.state === 'input-available'const searchResults: TypeSearchResults | undefined =tool.state === 'output-available' ? tool.output : undefined+ const isError = tool.state === 'output-error'+ const errorMessage = tool.errorText || 'Search failed'const query = tool.input?.query || ''const includeDomains = tool.input?.include_domainsconst includeDomainsString = includeDomains@@ -92,7 +94,17 @@ export function SearchSection({/></Section>)}- {isLoading && isToolLoading ? (+ {isError ? (+ <Section>+ <div className="p-3 bg-card border border-destructive rounded-lg">+ <div className="flex items-center gap-2 w-full">+ <span className="text-sm text-destructive block flex-1 min-w-0">+ {errorMessage}+ </span>+ </div>+ </div>+ </Section>+ ) : isLoading && isToolLoading ? (<SearchSkeleton />) : searchResults?.results && searchResults.results.length > 0 ? (<Section title="Sources">diff --git a/lib/tools/fetch.ts b/lib/tools/fetch.tsindex 78f91c7b..696c0192 100644--- a/lib/tools/fetch.ts+++ b/lib/tools/fetch.ts@@ -6,9 +6,7 @@ import { SearchResults as SearchResultsType } from '@/lib/types'const CONTENT_CHARACTER_LIMIT = 10000const TITLE_CHARACTER_LIMIT = 100-async function fetchRegularData(- url: string-): Promise<SearchResultsType> {+async function fetchRegularData(url: string): Promise<SearchResultsType> {try {const controller = new AbortController()const timeoutId = setTimeout(() => controller.abort(), 10000) // 10 second timeout@@ -91,9 +89,7 @@ async function fetchRegularData(}}-async function fetchJinaReaderData(- url: string-): Promise<SearchResultsType> {+async function fetchJinaReaderData(url: string): Promise<SearchResultsType> {try {const response = await fetch(`https://r.jina.ai/${url}`, {method: 'GET',@@ -128,9 +124,7 @@ async function fetchJinaReaderData(}}-async function fetchTavilyExtractData(- url: string-): Promise<SearchResultsType> {+async function fetchTavilyExtractData(url: string): Promise<SearchResultsType> {try {const apiKey = process.env.TAVILY_API_KEYconst response = await fetch('https://api.tavily.com/extract', {