An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Add video search functionality
Yoshiki Miuraf58ba8d1afb0
diff --git a/app/actions.tsx b/app/actions.tsxindex 6bec8354..0212918f 100644--- a/app/actions.tsx+++ b/app/actions.tsx@@ -21,6 +21,7 @@ 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'async function submit(formData?: FormData, skip?: boolean) {'use server'@@ -398,6 +399,14 @@ export const getUIStateFromAIState = (aiState: Chat) => {component: <RetrieveSection data={toolOutput} />,isCollapsed: isCollapsed.value}+ case 'videoSearch':+ return {+ id,+ component: (+ <VideoSearchSection result={searchResults.value} />+ ),+ isCollapsed: isCollapsed.value+ }}} catch (error) {return {diff --git a/components/section.tsx b/components/section.tsxindex 2752eab9..8d9fc36f 100644--- a/components/section.tsx+++ b/components/section.tsx@@ -3,6 +3,7 @@import { cn } from '@/lib/utils'import {BookCheck,+ Film,Image,MessageCircleMore,Newspaper,@@ -33,6 +34,9 @@ export const Section: React.FC<SectionProps> = ({// eslint-disable-next-line jsx-a11y/alt-texticon = <Image size={18} className="mr-2" />break+ case 'Videos':+ icon = <Film size={18} className="mr-2" />+ breakcase 'Sources':icon = <Newspaper size={18} className="mr-2" />breakdiff --git a/components/video-search-results.tsx b/components/video-search-results.tsxnew file mode 100644index 00000000..92e6e4e7--- /dev/null+++ b/components/video-search-results.tsx@@ -0,0 +1,167 @@+/* eslint-disable @next/next/no-img-element */+'use client'++import { useEffect, useRef, useState } from 'react'+import { AvatarImage, Avatar, AvatarFallback } from '@/components/ui/avatar'+import { CardContent, Card } from '@/components/ui/card'+import {+ Dialog,+ DialogContent,+ DialogDescription,+ DialogHeader,+ DialogTitle,+ DialogTrigger+} from '@/components/ui/dialog'+import {+ Carousel,+ type CarouselApi,+ CarouselContent,+ CarouselItem,+ CarouselNext,+ CarouselPrevious+} from '@/components/ui/carousel'+import { SerperSearchResultItem, SerperSearchResults } from '@/lib/types'+import { PlusCircle } from 'lucide-react'++export interface VideoSearchResultsProps {+ results: SerperSearchResults+}++export function VideoSearchResults({ results }: VideoSearchResultsProps) {+ // State to manage whether to display the results+ const [showAllResults, setShowAllResults] = useState(false)+ const [api, setApi] = useState<CarouselApi>()+ const [current, setCurrent] = useState(1)+ const [count, setCount] = useState(0)+ const [selectedIndex, setSelectedIndex] = useState(0)+ const videoRefs = useRef<(HTMLIFrameElement | null)[]>([])++ // Update the current and count state when the carousel api is available+ useEffect(() => {+ if (api) {+ setCount(api.scrollSnapList().length)+ setCurrent(api.selectedScrollSnap() + 1)++ api.on('select', () => {+ const newCurrent = api.selectedScrollSnap() + 1+ if (newCurrent !== current && videoRefs.current[current - 1]) {+ const prevVideo = videoRefs.current[current - 1]+ prevVideo?.contentWindow?.postMessage(+ '{"event":"command","func":"pauseVideo","args":""}',+ '*'+ )+ }+ setCurrent(newCurrent)+ })+ }+ }, [api, current])++ // Scroll to the selected index+ useEffect(() => {+ if (api) {+ api.scrollTo(selectedIndex, true)+ }+ }, [api, selectedIndex])++ if (!results.videos || results.videos.length === 0) {+ return <div className="text-muted-foreground">No videos found</div>+ }++ return (+ <div className="flex flex-wrap">+ {results.videos+ .slice(0, 4)+ .map((result: SerperSearchResultItem, index: any) => (+ <Dialog key={index}>+ <DialogTrigger asChild>+ <div+ className="w-1/2 md:w-1/4 p-1 cursor-pointer relative"+ onClick={() => setSelectedIndex(index)}+ >+ <Card className="flex-1 min-h-40 ">+ <CardContent className="p-2">+ <img+ src={result.imageUrl}+ alt={result.title}+ className="w-full aspect-video mb-2"+ />+ <p className="text-xs line-clamp-2">{result.title}</p>+ <div className="mt-2 flex items-center space-x-2">+ <Avatar className="h-4 w-4">+ <AvatarImage+ src={`https://www.google.com/s2/favicons?domain=${+ new URL(result.link).hostname+ }`}+ alt={result.channel}+ />+ <AvatarFallback>+ {new URL(result.link).hostname[0]}+ </AvatarFallback>+ </Avatar>+ <div className="text-xs opacity-60 truncate">+ {new URL(result.link).hostname}+ </div>+ </div>+ </CardContent>+ </Card>+ {index === 3 && results.videos.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">+ <DialogHeader>+ <DialogTitle>Search Videos</DialogTitle>+ <DialogDescription className="text-sm">+ {results.searchParameters.q}+ </DialogDescription>+ </DialogHeader>+ <div className="py-4">+ <Carousel+ setApi={setApi}+ className="w-full bg-muted max-h-[60vh]"+ >+ <CarouselContent>+ {results.videos.map((video, idx) => {+ const videoId = video.link.split('v=')[1]+ return (+ <CarouselItem key={idx}>+ <div className="p-1 flex items-center justify-center h-full">+ <iframe+ ref={el => {+ videoRefs.current[idx] = el+ }}+ src={`https://www.youtube.com/embed/${videoId}?enablejsapi=1`}+ className="w-full aspect-video"+ title={video.title}+ allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"+ allowFullScreen+ />+ </div>+ </CarouselItem>+ )+ })}+ </CarouselContent>+ <div className="absolute inset-8 flex items-center justify-between p-4 pointer-events-none">+ <CarouselPrevious className="w-10 h-10 rounded-full shadow focus:outline-none pointer-events-auto">+ <span className="sr-only">Previous</span>+ </CarouselPrevious>+ <CarouselNext className="w-10 h-10 rounded-full shadow focus:outline-none pointer-events-auto">+ <span className="sr-only">Next</span>+ </CarouselNext>+ </div>+ </Carousel>+ <div className="py-2">+ <div className="text-center text-sm text-muted-foreground">+ {current} of {count}+ </div>+ </div>+ </div>+ </DialogContent>+ </Dialog>+ ))}+ </div>+ )+}diff --git a/components/video-search-section.tsx b/components/video-search-section.tsxnew file mode 100644index 00000000..49c399e1--- /dev/null+++ b/components/video-search-section.tsx@@ -0,0 +1,35 @@+'use client'++import { SearchSkeleton } from './search-skeleton'+import { Section } from './section'+import type { SerperSearchResults } from '@/lib/types'+import { StreamableValue, useStreamableValue } from 'ai/rsc'+import { VideoSearchResults } from './video-search-results'+import { ToolBadge } from './tool-badge'++export type VideoSearchSectionProps = {+ result?: StreamableValue<string>+}++export function VideoSearchSection({ result }: VideoSearchSectionProps) {+ const [data, error, pending] = useStreamableValue(result)+ const searchResults: SerperSearchResults = data ? JSON.parse(data) : undefined+ return (+ <div>+ {!pending && data ? (+ <>+ <Section size="sm" className="pt-2 pb-0">+ <ToolBadge tool="search">{`${searchResults.searchParameters.q}`}</ToolBadge>+ </Section>+ <Section title="Videos">+ <VideoSearchResults results={searchResults} />+ </Section>+ </>+ ) : (+ <Section className="pt-2 pb-0">+ <SearchSkeleton />+ </Section>+ )}+ </div>+ )+}diff --git a/lib/agents/researcher.tsx b/lib/agents/researcher.tsxindex ba84d4c3..e9dd698c 100644--- a/lib/agents/researcher.tsx+++ b/lib/agents/researcher.tsx@@ -40,11 +40,15 @@ export async function researcher(messages,tools: getTools({uiStream,- fullResponse,- isFirstToolResponse+ fullResponse})})+ if (isFirstToolResponse) {+ isFirstToolResponse = false+ uiStream.update(null)+ }+// Process the responseconst toolCalls: ToolCallPart[] = []const toolResponses: ToolResultPart[] = []diff --git a/lib/agents/tools/index.tsx b/lib/agents/tools/index.tsxindex 0c0ae4ad..8ebd85c3 100644--- a/lib/agents/tools/index.tsx+++ b/lib/agents/tools/index.tsx@@ -1,28 +1,29 @@import { createStreamableUI } from 'ai/rsc'import { retrieveTool } from './retrieve'import { searchTool } from './search'+import { videoSearchTool } from './video-search'-export interface ToolsProps {+export interface ToolProps {uiStream: ReturnType<typeof createStreamableUI>fullResponse: string- isFirstToolResponse: boolean}-export const getTools = ({- uiStream,- fullResponse,- isFirstToolResponse-}: ToolsProps) => {+export const getTools = ({ uiStream, fullResponse }: ToolProps) => {const tools: any = {search: searchTool({uiStream,- fullResponse,- isFirstToolResponse+ fullResponse}),retrieve: retrieveTool({uiStream,- fullResponse,- isFirstToolResponse+ fullResponse+ })+ }++ if (process.env.SERPER_API_KEY) {+ tools.videoSearch = videoSearchTool({+ uiStream,+ fullResponse})}diff --git a/lib/agents/tools/retrieve.tsx b/lib/agents/tools/retrieve.tsxindex 4fa0ca1f..df209be3 100644--- a/lib/agents/tools/retrieve.tsx+++ b/lib/agents/tools/retrieve.tsx@@ -1,25 +1,15 @@import { retrieveSchema } from '@/lib/schema/retrieve'-import { ToolsProps } from '.'+import { ToolProps } from '.'import { Card } from '@/components/ui/card'import { SearchSkeleton } from '@/components/search-skeleton'import { SearchResults as SearchResultsType } from '@/lib/types'import RetrieveSection from '@/components/retrieve-section'-export const retrieveTool = ({- uiStream,- fullResponse,- isFirstToolResponse-}: ToolsProps) => ({+export const retrieveTool = ({ uiStream, fullResponse }: ToolProps) => ({description: 'Retrieve content from the web',parameters: retrieveSchema,execute: async ({ url }: { url: string }) => {let hasError = false-- // If this is the first tool response, remove spinner- if (isFirstToolResponse) {- isFirstToolResponse = false- uiStream.update(null)- }// Append the search sectionuiStream.append(<SearchSkeleton />)diff --git a/lib/agents/tools/search.tsx b/lib/agents/tools/search.tsxindex 9b66d8d8..3ed9d82d 100644--- a/lib/agents/tools/search.tsx+++ b/lib/agents/tools/search.tsx@@ -3,13 +3,9 @@ import Exa from 'exa-js'import { searchSchema } from '@/lib/schema/search'import { Card } from '@/components/ui/card'import { SearchSection } from '@/components/search-section'-import { ToolsProps } from '.'+import { ToolProps } from '.'-export const searchTool = ({- uiStream,- fullResponse,- isFirstToolResponse-}: ToolsProps) => ({+export const searchTool = ({ uiStream, fullResponse }: ToolProps) => ({description: 'Search the web for information',parameters: searchSchema,execute: async ({@@ -22,11 +18,6 @@ export const searchTool = ({search_depth: 'basic' | 'advanced'}) => {let hasError = false- // If this is the first tool response, remove spinner- if (isFirstToolResponse) {- isFirstToolResponse = false- uiStream.update(null)- }// Append the search sectionconst streamResults = createStreamableValue<string>()uiStream.append(<SearchSection result={streamResults.value} />)diff --git a/lib/agents/tools/video-search.tsx b/lib/agents/tools/video-search.tsxnew file mode 100644index 00000000..0e5d0e05--- /dev/null+++ b/lib/agents/tools/video-search.tsx@@ -0,0 +1,50 @@+import { createStreamableValue } from 'ai/rsc'+import { searchSchema } from '@/lib/schema/search'+import { Card } from '@/components/ui/card'+import { ToolProps } from '.'+import { VideoSearchSection } from '@/components/video-search-section'++// Start Generation Here+export const videoSearchTool = ({ uiStream, fullResponse }: ToolProps) => ({+ description: 'Search for videos from YouTube',+ parameters: searchSchema,+ execute: async ({ query }: { query: string }) => {+ let hasError = false+ // Append the search section+ const streamResults = createStreamableValue<string>()+ uiStream.append(<VideoSearchSection result={streamResults.value} />)++ let searchResult+ 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')+ }+ searchResult = await response.json()+ } catch (error) {+ console.error('Video Search API error:', error)+ hasError = true+ }++ if (hasError) {+ fullResponse += `\nAn error occurred while searching for videos with "${query}.`+ uiStream.update(+ <Card className="p-4 mt-2 text-sm">+ {`An error occurred while searching for videos with "${query}".`}+ </Card>+ )+ return searchResult+ }++ streamResults.done(JSON.stringify(searchResult))++ return searchResult+ }+})diff --git a/lib/types/index.ts b/lib/types/index.tsindex fcb89730..12593c1e 100644--- a/lib/types/index.ts+++ b/lib/types/index.ts@@ -4,12 +4,46 @@ export type SearchResults = {query: string}+export type ExaSearchResults = {+ results: ExaSearchResultItem[]+}++export type SerperSearchResults = {+ searchParameters: {+ q: string+ type: string+ engine: string+ }+ videos: SerperSearchResultItem[]+}+export type SearchResultItem = {title: stringurl: stringcontent: string}+export type ExaSearchResultItem = {+ score: number+ title: string+ id: string+ url: string+ publishedDate: Date+ author: string+}++export type SerperSearchResultItem = {+ title: string+ link: string+ snippet: string+ imageUrl: string+ duration: string+ source: string+ channel: string+ date: string+ position: number+}+export interface Chat extends Record<string, any> {id: stringtitle: string