An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat: Add support for including and excluding domains in search results
Yoshiki Miura44cf9920887a
diff --git a/components/search-section.tsx b/components/search-section.tsxindex 74695f69..63434b13 100644--- a/components/search-section.tsx+++ b/components/search-section.tsx@@ -10,17 +10,21 @@ import { StreamableValue, useStreamableValue } from 'ai/rsc'export type SearchSectionProps = {result?: StreamableValue<string>+ includeDomains?: string[]}-export function SearchSection({ result }: SearchSectionProps) {+export function SearchSection({ result, includeDomains }: SearchSectionProps) {const [data, error, pending] = useStreamableValue(result)const searchResults: TypeSearchResults = data ? JSON.parse(data) : undefined+ const includeDomainsString = includeDomains+ ? ` [${includeDomains.join(', ')}]`+ : ''return (<div>{!pending && data ? (<><Section size="sm" className="pt-2 pb-0">- <ToolBadge tool="search">{`${searchResults.query}`}</ToolBadge>+ <ToolBadge tool="search">{`${searchResults.query}${includeDomainsString}`}</ToolBadge></Section>{searchResults.images && searchResults.images.length > 0 && (<Section title="Images">diff --git a/lib/agents/researcher.tsx b/lib/agents/researcher.tsxindex 06754ce4..86b12213 100644--- a/lib/agents/researcher.tsx+++ b/lib/agents/researcher.tsx@@ -36,6 +36,7 @@ export async function researcher(Aim to directly address the user's question, augmenting your response with insights gleaned from the search results.Whenever quoting or referencing information from a specific URL, always cite the source URL explicitly.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 or a site name 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}`,messages: processedMessages,tools: getTools({diff --git a/lib/agents/tools/search.tsx b/lib/agents/tools/search.tsxindex 1998a82e..e288f117 100644--- a/lib/agents/tools/search.tsx+++ b/lib/agents/tools/search.tsx@@ -1,7 +1,6 @@import { createStreamableValue } from 'ai/rsc'import Exa from 'exa-js'import { searchSchema } from '@/lib/schema/search'-import { Card } from '@/components/ui/card'import { SearchSection } from '@/components/search-section'import { ToolProps } from '.'@@ -11,16 +10,25 @@ export const searchTool = ({ uiStream, fullResponse }: ToolProps) => ({execute: async ({query,max_results,- search_depth+ search_depth,+ include_domains,+ exclude_domains}: {query: stringmax_results: numbersearch_depth: 'basic' | 'advanced'+ include_domains: string[]+ exclude_domains: string[]}) => {let hasError = false// Append the search sectionconst streamResults = createStreamableValue<string>()- uiStream.append(<SearchSection result={streamResults.value} />)+ uiStream.append(+ <SearchSection+ result={streamResults.value}+ includeDomains={include_domains}+ />+ )// Tavily API requires a minimum of 5 characters in the queryconst filledQuery =@@ -30,7 +38,13 @@ export const searchTool = ({ uiStream, fullResponse }: ToolProps) => ({try {searchResult =searchAPI === 'tavily'- ? await tavilySearch(filledQuery, max_results, search_depth)+ ? await tavilySearch(+ filledQuery,+ max_results,+ search_depth,+ include_domains,+ exclude_domains+ ): await exaSearch(query)} catch (error) {console.error('Search API error:', error)@@ -53,7 +67,9 @@ export const searchTool = ({ uiStream, fullResponse }: ToolProps) => ({async function tavilySearch(query: string,maxResults: number = 10,- searchDepth: 'basic' | 'advanced' = 'basic'+ searchDepth: 'basic' | 'advanced' = 'basic',+ includeDomains: string[] = [],+ excludeDomains: string[] = []): Promise<any> {const apiKey = process.env.TAVILY_API_KEYconst response = await fetch('https://api.tavily.com/search', {@@ -67,7 +83,9 @@ async function tavilySearch(max_results: maxResults < 5 ? 5 : maxResults,search_depth: searchDepth,include_images: true,- include_answers: true+ include_answers: true,+ include_domains: includeDomains,+ exclude_domains: excludeDomains})})@@ -79,11 +97,18 @@ async function tavilySearch(return data}-async function exaSearch(query: string, maxResults: number = 10): Promise<any> {+async function exaSearch(+ query: string,+ maxResults: number = 10,+ includeDomains: string[] = [],+ excludeDomains: string[] = []+): Promise<any> {const apiKey = process.env.EXA_API_KEYconst exa = new Exa(apiKey)return exa.searchAndContents(query, {highlights: true,- numResults: maxResults+ numResults: maxResults,+ includeDomains,+ excludeDomains})}diff --git a/lib/schema/search.tsx b/lib/schema/search.tsxindex 6001e288..f0cb6e7d 100644--- a/lib/schema/search.tsx+++ b/lib/schema/search.tsx@@ -6,7 +6,19 @@ export const searchSchema = z.object({max_results: z.number().describe('The maximum number of results to return'),search_depth: z.enum(['basic', 'advanced'])- .describe('The depth of the search')+ .describe('The depth of the search'),+ include_domains: z+ .array(z.string())+ .optional()+ .describe(+ 'A list of domains to specifically include in the search results. Default is None, which includes all domains.'+ ),+ exclude_domains: z+ .array(z.string())+ .optional()+ .describe(+ "A list of domains to specifically exclude from the search results. Default is None, which doesn't exclude any domains."+ )})export type PartialInquiry = DeepPartial<typeof searchSchema>