Skip to content

An AI-powered search engine with a generative UI

feat: implement two-type search system with video integration

Yoshiki Miura657b7c8f6165
diff --git a/components/artifact/search-artifact-content.tsx b/components/artifact/search-artifact-content.tsx
index bfdd46fa..39ca056d 100644
--- a/components/artifact/search-artifact-content.tsx
+++ b/components/artifact/search-artifact-content.tsx
@@ -6,19 +6,34 @@ import type { ToolPart } from '@/lib/types/ai'
import { SearchResults } from '@/components/search-results'
import { SearchResultsImageSection } from '@/components/search-results-image'
import { Section, ToolArgsSection } from '@/components/section'
+import { VideoSearchResults } from '@/components/video-search-results'
export function SearchArtifactContent({ tool }: { tool: ToolPart<'search'> }) {
const searchResults: TypeSearchResults | undefined =
tool.state === 'output-available' ? tool.output : undefined
const query = tool.input?.query
- if (!searchResults?.results) {
+ const hasResults =
+ searchResults &&
+ ((searchResults.results && searchResults.results.length > 0) ||
+ (searchResults.videos && searchResults.videos.length > 0) ||
+ (searchResults.images && searchResults.images.length > 0))
+
+ if (!hasResults) {
return <div className="p-4">No search results</div>
}
return (
<div className="space-y-2">
- <ToolArgsSection tool="search">{`${query}`}</ToolArgsSection>
+ <ToolArgsSection
+ tool="search"
+ number={
+ (searchResults.results?.length || 0) +
+ (searchResults.videos?.length || 0) +
+ (searchResults.images?.length || 0)
+ }
+ >{`${query}`}</ToolArgsSection>
+
{searchResults.images && searchResults.images.length > 0 && (
<SearchResultsImageSection
images={searchResults.images}
@@ -27,9 +42,28 @@ export function SearchArtifactContent({ tool }: { tool: ToolPart<'search'> }) {
/>
)}
- <Section title="Sources">
- <SearchResults results={searchResults.results} displayMode="list" />
- </Section>
+ {searchResults.videos && searchResults.videos.length > 0 && (
+ <Section title="Videos">
+ <VideoSearchResults
+ results={{
+ ...searchResults,
+ videos: searchResults.videos,
+ searchParameters: {
+ q: query || '',
+ type: 'video',
+ engine: 'google'
+ }
+ }}
+ displayMode="artifact"
+ />
+ </Section>
+ )}
+
+ {searchResults.results && searchResults.results.length > 0 && (
+ <Section title="Sources">
+ <SearchResults results={searchResults.results} displayMode="list" />
+ </Section>
+ )}
</div>
)
}
diff --git a/components/search-section.tsx b/components/search-section.tsx
index 8b7e06da..defa5dcf 100644
--- a/components/search-section.tsx
+++ b/components/search-section.tsx
@@ -12,6 +12,7 @@ import { SearchSkeleton } from './default-skeleton'
import { SearchResults } from './search-results'
import { SearchResultsImageSection } from './search-results-image'
import { Section, ToolArgsSection } from './section'
+import { VideoSearchResults } from './video-search-results'
interface SearchSectionProps {
tool: ToolPart<'search'>
@@ -48,7 +49,11 @@ export function SearchSection({
>
<ToolArgsSection
tool="search"
- number={searchResults?.results?.length}
+ number={
+ (searchResults?.results?.length || 0) +
+ (searchResults?.videos?.length || 0) +
+ (searchResults?.images?.length || 0)
+ }
>{`${query}${includeDomainsString}`}</ToolArgsSection>
</button>
)
@@ -72,9 +77,26 @@ export function SearchSection({
/>
</Section>
)}
+ {searchResults &&
+ searchResults.videos &&
+ searchResults.videos.length > 0 && (
+ <Section title="Videos">
+ <VideoSearchResults
+ results={{
+ ...searchResults,
+ videos: searchResults.videos,
+ searchParameters: {
+ q: query || '',
+ type: 'video',
+ engine: 'google'
+ }
+ }}
+ />
+ </Section>
+ )}
{isLoading && isToolLoading ? (
<SearchSkeleton />
- ) : searchResults?.results ? (
+ ) : searchResults?.results && searchResults.results.length > 0 ? (
<Section title="Sources">
<SearchResults results={searchResults.results} />
</Section>
diff --git a/components/tool-section.tsx b/components/tool-section.tsx
index 370d3940..1235054c 100644
--- a/components/tool-section.tsx
+++ b/components/tool-section.tsx
@@ -8,7 +8,6 @@ import FetchSection from './fetch-section'
import { QuestionConfirmation } from './question-confirmation'
import { RelatedQuestions } from './related-questions'
import { SearchSection } from './search-section'
-import { VideoSearchSection } from './video-search-section'
interface ToolSectionProps {
tool: ToolPart
@@ -75,15 +74,6 @@ export function ToolSection({
status={status}
/>
)
- case 'tool-videoSearch':
- return (
- <VideoSearchSection
- tool={tool as ToolPart<'videoSearch'>}
- isOpen={isOpen}
- onOpenChange={onOpenChange}
- status={status}
- />
- )
case 'tool-fetch':
return (
<FetchSection
diff --git a/components/video-search-results.tsx b/components/video-search-results.tsx
index f754b499..3a535714 100644
--- a/components/video-search-results.tsx
+++ b/components/video-search-results.tsx
@@ -7,9 +7,13 @@ import { VideoResultGrid } from './video-result-grid'
export interface VideoSearchResultsProps {
results: SerperSearchResults
+ displayMode?: 'chat' | 'artifact'
}
-export function VideoSearchResults({ results }: VideoSearchResultsProps) {
+export function VideoSearchResults({
+ results,
+ displayMode = 'chat'
+}: VideoSearchResultsProps) {
const videos = results.videos.filter((video: SerperSearchResultItem) => {
try {
return new URL(video.link).pathname === '/watch'
@@ -25,5 +29,7 @@ export function VideoSearchResults({ results }: VideoSearchResultsProps) {
return <div className="text-muted-foreground">No videos found</div>
}
- return <VideoResultGrid videos={videos} query={query} displayMode="chat" />
+ return (
+ <VideoResultGrid videos={videos} query={query} displayMode={displayMode} />
+ )
}
diff --git a/components/video-search-section.tsx b/components/video-search-section.tsx
deleted file mode 100644
index 4e7fc8b4..00000000
--- a/components/video-search-section.tsx
+++ /dev/null
@@ -1,68 +0,0 @@
-'use client'
-
-import { UseChatHelpers } from '@ai-sdk/react'
-
-import type { SerperSearchResults } from '@/lib/types'
-import type { ToolPart, UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'
-
-import { useArtifact } from '@/components/artifact/artifact-context'
-
-import { CollapsibleMessage } from './collapsible-message'
-import { DefaultSkeleton } from './default-skeleton'
-import { Section, ToolArgsSection } from './section'
-import { VideoSearchResults } from './video-search-results'
-
-interface VideoSearchSectionProps {
- tool: ToolPart<'videoSearch'>
- isOpen: boolean
- onOpenChange: (open: boolean) => void
- status?: UseChatHelpers<UIMessage<unknown, UIDataTypes, UITools>>['status']
-}
-
-export function VideoSearchSection({
- tool,
- isOpen,
- onOpenChange,
- status
-}: VideoSearchSectionProps) {
- const isLoading = status === 'submitted' || status === 'streaming'
-
- const isToolLoading =
- tool.state === 'input-streaming' || tool.state === 'input-available'
- const videoResults: SerperSearchResults =
- tool.state === 'output-available' ? tool.output : undefined
- const query = tool.input?.query
-
- const { open } = useArtifact()
- const header = (
- <button
- type="button"
- onClick={() => open(tool)}
- className="flex items-center justify-between w-full text-left rounded-md p-1 -ml-1"
- title="Open details"
- >
- <ToolArgsSection tool="videoSearch" number={videoResults?.videos?.length}>
- {query}
- </ToolArgsSection>
- </button>
- )
-
- return (
- <CollapsibleMessage
- role="assistant"
- isCollapsible={true}
- header={header}
- isOpen={isOpen}
- onOpenChange={onOpenChange}
- showIcon={false}
- >
- {!isLoading && videoResults ? (
- <Section title="Videos">
- <VideoSearchResults results={videoResults} />
- </Section>
- ) : (
- <DefaultSkeleton />
- )}
- </CollapsibleMessage>
- )
-}
diff --git a/lib/agents/researcher-with-dynamic-tools.ts b/lib/agents/researcher-with-dynamic-tools.ts
index d62e6c67..0fa34ff1 100644
--- a/lib/agents/researcher-with-dynamic-tools.ts
+++ b/lib/agents/researcher-with-dynamic-tools.ts
@@ -8,20 +8,19 @@ import {
import { fetchTool } from '../tools/fetch'
import { createQuestionTool } from '../tools/question'
import { createSearchTool } from '../tools/search'
-import { createVideoSearchTool } from '../tools/video-search'
import { getModel } from '../utils/registry'
const SYSTEM_PROMPT = `
Instructions:
-You are a helpful AI assistant with access to real-time web search, content retrieval, video search capabilities, the ability to ask clarifying questions, and dynamic tools that may be added at runtime.
+You are a helpful AI assistant with access to real-time web search, content retrieval, the ability to ask clarifying questions, and dynamic tools that may be added at runtime.
When asked a question, you should:
1. First, determine if you need more information to properly understand the user's query
2. **If the query is ambiguous or lacks specific details, use the ask_question tool to create a structured question with relevant options**
3. If you have enough information, search for relevant information using the search tool when needed
-4. Use the fetch tool to get detailed content from specific URLs
-5. Use the video search tool when looking for video content
+4. For video content, use the search tool with content_types: ['video'] or ['web', 'video']
+5. Use the fetch tool to get detailed content from specific URLs
6. Use any available dynamic tools when they are relevant to the user's request
7. Analyze all search results to provide accurate, up-to-date information
8. Always cite sources using the [number](url) format, matching the order of search results. If multiple sources are relevant, include all of them, and comma separate them. Only use information that has a URL available for citation.
@@ -30,6 +29,22 @@ When asked a question, you should:
11. Use markdown to structure your responses. Use headings to break up the content into sections.
12. **Use the fetch tool only with user-provided URLs.**
+Search tool usage:
+- **IMPORTANT: For video searches (YouTube, tutorials, etc.), ALWAYS use type="general" with content_types: ['video'] or ['web', 'video']**
+ - This enables specialized video display components with thumbnails and better formatting
+ - Even if the query doesn't explicitly mention "video", use this for YouTube-related content
+- Use type="general" with appropriate content_types when:
+ - Searching for videos (MANDATORY: content_types must include 'video')
+ - Searching for images (with content_types: ['image'] or ['web', 'image'])
+ - Looking for weather, news, or current events that need simple results + fetch
+ - Need latest/real-time information with follow-up fetches
+- Use type="optimized" for:
+ - Detailed research and fact-checking
+ - When you need content snippets without fetching
+ - Complex queries requiring in-depth analysis
+ - General knowledge questions
+- When mentioning YouTube or video content in your response, explain that you're using specialized video search to provide the best visual presentation
+
When using the ask_question tool:
- Create clear, concise questions
- Provide relevant predefined options
@@ -61,14 +76,12 @@ export function researcherWithDynamicTools({
// Create model-specific tools
const searchTool = createSearchTool(model)
- const videoSearchTool = createVideoSearchTool(model)
const askQuestionTool = createQuestionTool(model)
// Create base tools object
const tools: Record<string, any> = {
search: searchTool,
fetch: fetchTool,
- videoSearch: videoSearchTool,
askQuestion: askQuestionTool
}
@@ -100,7 +113,7 @@ export function researcherWithDynamicTools({
const activeToolNames = searchMode
? allToolNames.filter(
name =>
- ['search', 'fetch', 'videoSearch'].includes(name) ||
+ ['search', 'fetch'].includes(name) ||
name.startsWith('mcp__') ||
name.startsWith('dynamic__')
)
diff --git a/lib/agents/researcher.ts b/lib/agents/researcher.ts
index c7cbd110..6dd22f92 100644
--- a/lib/agents/researcher.ts
+++ b/lib/agents/researcher.ts
@@ -3,20 +3,19 @@ import { Experimental_Agent as Agent, stepCountIs } from 'ai'
import { fetchTool } from '../tools/fetch'
import { createQuestionTool } from '../tools/question'
import { createSearchTool } from '../tools/search'
-import { createVideoSearchTool } from '../tools/video-search'
import { getModel } from '../utils/registry'
const SYSTEM_PROMPT = `
Instructions:
-You are a helpful AI assistant with access to real-time web search, content retrieval, video search capabilities, and the ability to ask clarifying questions.
+You are a helpful AI assistant with access to real-time web search, content retrieval, and the ability to ask clarifying questions.
When asked a question, you should:
1. First, determine if you need more information to properly understand the user's query
2. **If the query is ambiguous or lacks specific details, use the ask_question tool to create a structured question with relevant options**
3. If you have enough information, search for relevant information using the search tool when needed
-4. Use the fetch tool to get detailed content from specific URLs
-5. Use the video search tool when looking for video content
+4. For video content, use the search tool with content_types: ['video'] or ['web', 'video']
+5. Use the fetch tool to get detailed content from specific URLs
6. Analyze all search results to provide accurate, up-to-date information
7. Always cite sources using the [number](url) format, matching the order of search results. If multiple sources are relevant, include all of them, and comma separate them. Only use information that has a URL available for citation.
8. If results are not relevant or helpful, rely on your general knowledge
@@ -24,6 +23,22 @@ When asked a question, you should:
10. Use markdown to structure your responses. Use headings to break up the content into sections.
11. **Use the fetch tool only with user-provided URLs.**
+Search tool usage:
+- **IMPORTANT: For video searches (YouTube, tutorials, etc.), ALWAYS use type="general" with content_types: ['video'] or ['web', 'video']**
+ - This enables specialized video display components with thumbnails and better formatting
+ - Even if the query doesn't explicitly mention "video", use this for YouTube-related content
+- Use type="general" with appropriate content_types when:
+ - Searching for videos (MANDATORY: content_types must include 'video')
+ - Searching for images (with content_types: ['image'] or ['web', 'image'])
+ - Looking for weather, news, or current events that need simple results + fetch
+ - Need latest/real-time information with follow-up fetches
+- Use type="optimized" for:
+ - Detailed research and fact-checking
+ - When you need content snippets without fetching
+ - Complex queries requiring in-depth analysis
+ - General knowledge questions
+- When mentioning YouTube or video content in your response, explain that you're using specialized video search to provide the best visual presentation
+
When using the ask_question tool:
- Create clear, concise questions
- Provide relevant predefined options
@@ -46,7 +61,6 @@ export function researcher({
// Create model-specific tools
const searchTool = createSearchTool(model)
- const videoSearchTool = createVideoSearchTool(model)
const askQuestionTool = createQuestionTool(model)
// Return an agent instance
@@ -56,10 +70,9 @@ export function researcher({
tools: {
search: searchTool,
fetch: fetchTool,
- videoSearch: videoSearchTool,
askQuestion: askQuestionTool
},
- activeTools: searchMode ? ['search', 'fetch', 'videoSearch'] : undefined,
+ activeTools: searchMode ? ['search', 'fetch'] : undefined,
stopWhen: searchMode ? stepCountIs(10) : stepCountIs(1)
})
} catch (error) {
diff --git a/lib/db/migrations/0005_remove_video_search.sql b/lib/db/migrations/0005_remove_video_search.sql
new file mode 100644
index 00000000..ba080e76
--- /dev/null
+++ b/lib/db/migrations/0005_remove_video_search.sql
@@ -0,0 +1,3 @@
+-- Remove video search columns from parts table
+ALTER TABLE "parts" DROP COLUMN IF EXISTS "tool_video_search_input";
+ALTER TABLE "parts" DROP COLUMN IF EXISTS "tool_video_search_output";
\ No newline at end of file
diff --git a/lib/db/schema.ts b/lib/db/schema.ts
index 132855bc..fcef1677 100644
--- a/lib/db/schema.ts
+++ b/lib/db/schema.ts
@@ -128,8 +128,6 @@ export const parts = pgTable(
tool_fetch_output: json('tool_fetch_output').$type<any>(),
tool_question_input: json('tool_question_input').$type<any>(),
tool_question_output: json('tool_question_output').$type<any>(),
- tool_videoSearch_input: json('tool_video_search_input').$type<any>(),
- tool_videoSearch_output: json('tool_video_search_output').$type<any>(),
tool_relatedQuestions_input: json(
'tool_related_questions_input'
).$type<any>(),
diff --git a/lib/schema/search.tsx b/lib/schema/search.tsx
index 1742cdbc..40832b00 100644
--- a/lib/schema/search.tsx
+++ b/lib/schema/search.tsx
@@ -3,6 +3,20 @@ import { z } from 'zod'
export const searchSchema = z.object({
query: z.string().describe('The query to search for'),
+ type: z
+ .enum(['general', 'optimized'])
+ .optional()
+ .default('optimized')
+ .describe(
+ 'Search type: general for Brave search (basic results, may need fetch for details), optimized for AI-focused providers with content snippets (Tavily/Exa/SearXNG)'
+ ),
+ content_types: z
+ .array(z.enum(['web', 'video', 'image', 'news']))
+ .optional()
+ .default(['web'])
+ .describe(
+ 'Types of content to include in search results. Only applicable when type is "general". Ignored for "optimized" searches.'
+ ),
max_results: z
.number()
.optional()
@@ -30,6 +44,16 @@ export const searchSchema = z.object({
// Strict schema with all fields required
export const strictSearchSchema = z.object({
query: z.string().describe('The query to search for'),
+ type: z
+ .enum(['general', 'optimized'])
+ .describe(
+ 'Search type: general for Brave search (basic results, may need fetch for details), optimized for AI-focused providers with content snippets (Tavily/Exa/SearXNG)'
+ ),
+ content_types: z
+ .array(z.enum(['web', 'video', 'image', 'news']))
+ .describe(
+ 'Types of content to include in search results. Only applicable when type is "general". Ignored for "optimized" searches.'
+ ),
max_results: z.number().describe('The maximum number of results to return.'),
search_depth: z
.enum(['basic', 'advanced'])
diff --git a/lib/tools/search.ts b/lib/tools/search.ts
index 61603d75..bd480c5d 100644
--- a/lib/tools/search.ts
+++ b/lib/tools/search.ts
@@ -15,10 +15,13 @@ import {
*/
export function createSearchTool(fullModel: string) {
return tool({
- description: 'Search the web for information',
+ description:
+ 'Search the web for information. For YouTube/video content, use type="general" with content_types:["video"] for optimal visual presentation with thumbnails.',
inputSchema: getSearchSchemaForModel(fullModel),
execute: async ({
query,
+ type = 'optimized',
+ content_types = ['web'],
max_results = 20,
search_depth = 'basic', // Default for standard schema
include_domains = [],
@@ -35,8 +38,16 @@ export function createSearchTool(fullModel: string) {
// Use the original query as is - any provider-specific handling will be done in the provider
const filledQuery = query
let searchResult: SearchResults
- const searchAPI =
- (process.env.SEARCH_API as SearchProviderType) || DEFAULT_PROVIDER
+
+ // Determine which provider to use based on type
+ let searchAPI: SearchProviderType
+ if (type === 'general') {
+ searchAPI = 'brave'
+ } else {
+ // For 'optimized', use the configured provider
+ searchAPI =
+ (process.env.SEARCH_API as SearchProviderType) || DEFAULT_PROVIDER
+ }
const effectiveSearchDepthForAPI =
searchAPI === 'searxng' &&
@@ -45,7 +56,7 @@ export function createSearchTool(fullModel: string) {
: effectiveSearchDepth || 'basic'
console.log(
- `Using search API: ${searchAPI}, Search Depth: ${effectiveSearchDepthForAPI}`
+ `Using search API: ${searchAPI}, Type: ${type}, Search Depth: ${effectiveSearchDepthForAPI}`
)
try {
@@ -76,13 +87,31 @@ export function createSearchTool(fullModel: string) {
} else {
// Use the provider factory to get the appropriate search provider
const searchProvider = createSearchProvider(searchAPI)
- searchResult = await searchProvider.search(
- filledQuery,
- effectiveMaxResults,
- effectiveSearchDepthForAPI,
- include_domains,
- exclude_domains
- )
+
+ // Pass content_types only for Brave provider
+ if (searchAPI === 'brave') {
+ searchResult = await searchProvider.search(
+ filledQuery,
+ effectiveMaxResults,
+ effectiveSearchDepthForAPI,
+ include_domains,
+ exclude_domains,
+ {
+ type: type as 'general' | 'optimized',
+ content_types: content_types as Array<
+ 'web' | 'video' | 'image' | 'news'
+ >
+ }
+ )
+ } else {
+ searchResult = await searchProvider.search(
+ filledQuery,
+ effectiveMaxResults,
+ effectiveSearchDepthForAPI,
+ include_domains,
+ exclude_domains
+ )
+ }
}
} catch (error) {
console.error('Search API error:', error)
@@ -114,6 +143,8 @@ export async function search(
searchTool.execute?.(
{
query,
+ type: 'general',
+ content_types: ['web'],
max_results: maxResults,
search_depth: searchDepth,
include_domains: includeDomains as any,
diff --git a/lib/tools/search/providers/base.ts b/lib/tools/search/providers/base.ts
index c709e7d1..4a76d479 100644
--- a/lib/tools/search/providers/base.ts
+++ b/lib/tools/search/providers/base.ts
@@ -6,7 +6,11 @@ export interface SearchProvider {
maxResults: number,
searchDepth: 'basic' | 'advanced',
includeDomains: string[],
- excludeDomains: string[]
+ excludeDomains: string[],
+ options?: {
+ type?: 'general' | 'optimized'
+ content_types?: Array<'web' | 'video' | 'image' | 'news'>
+ }
): Promise<SearchResults>
}
@@ -16,7 +20,11 @@ export abstract class BaseSearchProvider implements SearchProvider {
maxResults: number,
searchDepth: 'basic' | 'advanced',
includeDomains: string[],
- excludeDomains: string[]
+ excludeDomains: string[],
+ options?: {
+ type?: 'general' | 'optimized'
+ content_types?: Array<'web' | 'video' | 'image' | 'news'>
+ }
): Promise<SearchResults>
protected validateApiKey(
diff --git a/lib/tools/search/providers/brave.ts b/lib/tools/search/providers/brave.ts
new file mode 100644
index 00000000..63d89a09
--- /dev/null
+++ b/lib/tools/search/providers/brave.ts
@@ -0,0 +1,220 @@
+import {
+ SearchImageItem,
+ SearchResults,
+ SerperSearchResultItem
+} from '@/lib/types'
+
+import { SearchProvider } from './base'
+
+interface BraveWebResult {
+ title?: string
+ description?: string
+ url: string
+}
+
+interface BraveVideoResult {
+ title?: string
+ description?: string
+ url?: string
+ thumbnail?: {
+ src?: string
+ }
+ video?: {
+ duration?: string
+ }
+ duration?: string
+ date?: string
+ publisher?: string
+}
+
+interface BraveImageResult {
+ title?: string
+ source?: string
+ url?: string
+ thumbnail?: {
+ src?: string
+ }
+ properties?: {
+ thumbnail?: string
+ width?: number
+ height?: number
+ }
+ width?: number
+ height?: number
+}
+
+export class BraveSearchProvider implements SearchProvider {
+ private apiKey: string | undefined
+
+ constructor() {
+ this.apiKey = process.env.BRAVE_SEARCH_API_KEY
+ }
+
+ async search(
+ query: string,
+ maxResults: number = 10,
+ searchDepth?: 'basic' | 'advanced',
+ includeDomains?: string[],
+ excludeDomains?: string[],
+ options?: {
+ type?: 'general' | 'optimized'
+ content_types?: Array<'web' | 'video' | 'image' | 'news'>
+ }
+ ): Promise<SearchResults> {
+ if (!this.apiKey) {
+ throw new Error('Brave Search API key not configured')
+ }
+
+ const contentTypes = options?.content_types || ['web']
+ const results: SearchResults = {
+ results: [],
+ images: [],
+ videos: [],
+ query,
+ number_of_results: 0
+ }
+
+ // Execute searches in parallel for each content type
+ const promises: Promise<void>[] = []
+
+ if (contentTypes.includes('web')) {
+ promises.push(this.searchWeb(query, maxResults, results))
+ }
+
+ if (contentTypes.includes('video')) {
+ promises.push(this.searchVideos(query, maxResults, results))
+ }
+
+ if (contentTypes.includes('image')) {
+ promises.push(this.searchImages(query, maxResults, results))
+ }
+
+ await Promise.all(promises)
+
+ // Update total count
+ results.number_of_results = results.results.length
+
+ return results
+ }
+
+ private async searchWeb(
+ query: string,
+ maxResults: number,
+ results: SearchResults
+ ): Promise<void> {
+ try {
+ const response = await fetch(
+ `https://api.search.brave.com/res/v1/web/search?q=${encodeURIComponent(
+ query
+ )}&count=${maxResults}`,
+ {
+ headers: {
+ Accept: 'application/json',
+ 'Accept-Encoding': 'gzip',
+ 'X-Subscription-Token': this.apiKey!
+ }
+ }
+ )
+
+ if (!response.ok) {
+ throw new Error(`Brave web search failed: ${response.statusText}`)
+ }
+
+ const data = await response.json()
+ results.results = (data.web?.results || [])
+ .slice(0, maxResults)
+ .map((result: BraveWebResult) => ({
+ title: result.title || 'No title',
+ description: result.description || 'No description available',
+ url: result.url
+ }))
+ } catch (error) {
+ console.error('Brave web search error:', error)
+ }
+ }
+
+ private async searchVideos(
+ query: string,
+ maxResults: number,
+ results: SearchResults
+ ): Promise<void> {
+ try {
+ const response = await fetch(
+ `https://api.search.brave.com/res/v1/videos/search?q=${encodeURIComponent(
+ query
+ )}&count=${maxResults}`,
+ {
+ headers: {
+ Accept: 'application/json',
+ 'Accept-Encoding': 'gzip',
+ 'X-Subscription-Token': this.apiKey!
+ }
+ }
+ )
+
+ if (!response.ok) {
+ throw new Error(`Brave video search failed: ${response.statusText}`)
+ }
+
+ const data = await response.json()
+
+ // Convert to SerperSearchResultItem format for compatibility
+ results.videos = (data.results || []).slice(0, maxResults).map(
+ (result: BraveVideoResult) =>
+ ({
+ title: result.title || 'No title',
+ link: result.url || '',
+ snippet: result.description || 'No description available',
+ imageUrl: result.thumbnail?.src,
+ duration: result.video?.duration || result.duration,
+ source: result.publisher || '',
+ channel: result.publisher || '',
+ date: result.date || '',
+ position: 0
+ }) as SerperSearchResultItem
+ )
+ } catch (error) {
+ console.error('Brave video search error:', error)
+ results.videos = []
+ }
+ }
+
+ private async searchImages(
+ query: string,
+ maxResults: number,
+ results: SearchResults
+ ): Promise<void> {
+ try {
+ const response = await fetch(
+ `https://api.search.brave.com/res/v1/images/search?q=${encodeURIComponent(
+ query
+ )}&count=${maxResults}`,
+ {
+ headers: {
+ Accept: 'application/json',
+ 'Accept-Encoding': 'gzip',
+ 'X-Subscription-Token': this.apiKey!
+ }
+ }
+ )
+
+ if (!response.ok) {
+ throw new Error(`Brave image search failed: ${response.statusText}`)
+ }
+
+ const data = await response.json()
+ results.images = (data.results || []).slice(0, maxResults).map(
+ (result: BraveImageResult) =>
+ ({
+ title: result.title || 'No title',
+ link: result.url || result.source || '',
+ thumbnailUrl:
+ result.thumbnail?.src || result.properties?.thumbnail || ''
+ }) as SearchImageItem
+ )
+ } catch (error) {
+ console.error('Brave image search error:', error)
+ results.images = []
+ }
+ }
+}
diff --git a/lib/tools/search/providers/index.ts b/lib/tools/search/providers/index.ts
index ebaba485..2ccf1676 100644
--- a/lib/tools/search/providers/index.ts
+++ b/lib/tools/search/providers/index.ts
@@ -1,9 +1,10 @@
import { SearchProvider } from './base'
+import { BraveSearchProvider } from './brave'
import { ExaSearchProvider } from './exa'
import { SearXNGSearchProvider } from './searxng'
import { TavilySearchProvider } from './tavily'
-export type SearchProviderType = 'tavily' | 'exa' | 'searxng'
+export type SearchProviderType = 'tavily' | 'exa' | 'searxng' | 'brave'
export const DEFAULT_PROVIDER: SearchProviderType = 'tavily'
export function createSearchProvider(
@@ -19,12 +20,15 @@ export function createSearchProvider(
return new ExaSearchProvider()
case 'searxng':
return new SearXNGSearchProvider()
+ case 'brave':
+ return new BraveSearchProvider()
default:
// Default to TavilySearchProvider if an unknown provider is specified
return new TavilySearchProvider()
}
}
+export { BraveSearchProvider } from './brave'
export type { ExaSearchProvider } from './exa'
export { SearXNGSearchProvider } from './searxng'
export { TavilySearchProvider } from './tavily'
diff --git a/lib/tools/video-search.ts b/lib/tools/video-search.ts
deleted file mode 100644
index 2b462fb5..00000000
--- a/lib/tools/video-search.ts
+++ /dev/null
@@ -1,37 +0,0 @@
-import { tool } from 'ai'
-
-import { getSearchSchemaForModel } from '@/lib/schema/search'
-
-/**
- * Creates a video search tool with the appropriate schema for the model.
- */
-export function createVideoSearchTool(fullModel: string) {
- return tool({
- description: 'Search for videos from YouTube',
- inputSchema: getSearchSchemaForModel(fullModel),
- execute: async ({ query }) => {
- try {
- const response = await fetch('https://google.serper.dev/videos', {
- method: 'POST',
- headers: {
- 'X-API-KEY': process.env.SERPER_API_KEY || '',
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({ q: query })
- })
-
- if (!response.ok) {
- throw new Error('Network response was not ok')
- }
-
- return await response.json()
- } catch (error) {
- console.error('Video Search API error:', error)
- return null
- }
- }
- })
-}
-
-// Default export for backward compatibility, using a default model
-export const videoSearchTool = createVideoSearchTool('openai:gpt-4o-mini')
diff --git a/lib/types/ai.ts b/lib/types/ai.ts
index 07652910..29fa2df6 100644
--- a/lib/types/ai.ts
+++ b/lib/types/ai.ts
@@ -3,7 +3,6 @@ import type { InferUITool, UIMessage as AIMessage } from 'ai'
import { fetchTool } from '@/lib/tools/fetch'
import { askQuestionTool } from '@/lib/tools/question'
import { searchTool } from '@/lib/tools/search'
-import { videoSearchTool } from '@/lib/tools/video-search'
export type UIMessage<
TMetadata = unknown,
@@ -18,7 +17,6 @@ export type UIDataTypes = {
export type UITools = {
search: InferUITool<typeof searchTool>
fetch: InferUITool<typeof fetchTool>
- videoSearch: InferUITool<typeof videoSearchTool>
askQuestion: InferUITool<typeof askQuestionTool>
// Dynamic tools will be added at runtime
[key: string]: any
diff --git a/lib/types/index.ts b/lib/types/index.ts
index df5143c4..6cf60833 100644
--- a/lib/types/index.ts
+++ b/lib/types/index.ts
@@ -1,6 +1,7 @@
export type SearchResults = {
images: SearchResultImage[]
results: SearchResultItem[]
+ videos?: SerperSearchResultItem[]
number_of_results?: number
query: string
}
@@ -55,6 +56,12 @@ export type SerperSearchResultItem = {
position: number
}
+export type SearchImageItem = {
+ title: string
+ link: string
+ thumbnailUrl: string
+}
+
export interface SearXNGResult {
title: string
url: string
diff --git a/next.config.mjs b/next.config.mjs
index eb6fcf00..a89da5cd 100644
--- a/next.config.mjs
+++ b/next.config.mjs
@@ -13,6 +13,12 @@ const nextConfig = {
hostname: 'lh3.googleusercontent.com',
port: '',
pathname: '/a/**' // Google user content often follows this pattern
+ },
+ {
+ protocol: 'https',
+ hostname: 'imgs.search.brave.com',
+ port: '',
+ pathname: '/**' // Brave search cached images
}
]
}