An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat: implement citation system with hover popovers
Yoshiki Miura9aa392bfa60d
diff --git a/components/answer-section.tsx b/components/answer-section.tsxindex 0bd4f2f2..f639df81 100644--- a/components/answer-section.tsx+++ b/components/answer-section.tsx@@ -3,6 +3,7 @@import { UseChatHelpers } from '@ai-sdk/react'import { ChatRequestOptions } from 'ai'+import type { SearchResultItem } from '@/lib/types'import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'import { CollapsibleMessage } from './collapsible-message'@@ -22,6 +23,7 @@ export type AnswerSectionProps = {messageId: string,options?: ChatRequestOptions) => Promise<void | string | null | undefined>+ citationMap?: Record<number, SearchResultItem>}export function AnswerSection({@@ -32,7 +34,8 @@ export function AnswerSection({showActions = true, // Default to true for backward compatibilitymessageId,status,- reload+ reload,+ citationMap}: AnswerSectionProps) {const enableShare = process.env.NEXT_PUBLIC_SUPABASE_URL !== undefined@@ -45,7 +48,10 @@ export function AnswerSection({const message = content ? (<div className="flex flex-col gap-1">- <BotMessage message={content} />+ <BotMessage+ message={content}+ citationMap={citationMap}+ />{showActions && (<MessageActionsmessage={content} // Keep original message content for copydiff --git a/components/citation-context.tsx b/components/citation-context.tsxnew file mode 100644index 00000000..06784042--- /dev/null+++ b/components/citation-context.tsx@@ -0,0 +1,30 @@+'use client'++import { createContext, ReactNode,useContext } from 'react'++import type { SearchResultItem } from '@/lib/types'++interface CitationContextValue {+ citationMap?: Record<number, SearchResultItem>+}++const CitationContext = createContext<CitationContextValue | undefined>(undefined)++export function CitationProvider({+ children,+ citationMap+}: {+ children: ReactNode+ citationMap?: Record<number, SearchResultItem>+}) {+ return (+ <CitationContext.Provider value={{ citationMap }}>+ {children}+ </CitationContext.Provider>+ )+}++export function useCitation() {+ const context = useContext(CitationContext)+ return context || { citationMap: undefined }+}\ No newline at end of filediff --git a/components/citation-link.tsx b/components/citation-link.tsxnew file mode 100644index 00000000..b76bf51f--- /dev/null+++ b/components/citation-link.tsx@@ -0,0 +1,133 @@+'use client'++import { memo, useState } from 'react'+import Link from 'next/link'++import type { SearchResultItem } from '@/lib/types'+import { cn } from '@/lib/utils'++import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'+import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'++interface CitationLinkProps {+ href: string+ children: React.ReactNode+ className?: string+ citationData?: SearchResultItem+}++// Helper function to safely extract hostname from URL+const getHostname = (url: string): string => {+ try {+ return new URL(url).hostname+ } catch {+ return 'unknown'+ }+}++export const CitationLink = memo(function CitationLink({+ href,+ children,+ className,+ citationData+}: CitationLinkProps) {+ const [open, setOpen] = useState(false)+ const childrenText = children?.toString() || ''+ const isNumber = /^\d+$/.test(childrenText)++ const linkClasses = cn(+ isNumber+ ? 'text-[10px] bg-muted text-muted-foreground rounded-full w-4 h-4 px-0.5 inline-flex items-center justify-center hover:bg-muted/50 duration-200 no-underline -translate-y-0.5'+ : 'hover:underline inline-flex items-center gap-1.5',+ className+ )++ // If no citation data, render as simple link+ if (!citationData) {+ return (+ <a+ href={href}+ target="_blank"+ rel="noopener noreferrer"+ className={linkClasses}+ >+ {children}+ </a>+ )+ }++ // For numbered citations with data, show popover on hover+ if (isNumber && citationData) {+ return (+ <Popover open={open} onOpenChange={setOpen}>+ <PopoverTrigger asChild>+ <a+ href={href}+ target="_blank"+ rel="noopener noreferrer"+ className={linkClasses}+ onMouseEnter={() => setOpen(true)}+ onMouseLeave={() => setOpen(false)}+ >+ {children}+ </a>+ </PopoverTrigger>+ <PopoverContent+ className="w-72 p-3 z-50"+ side="top"+ align="center"+ sideOffset={8}+ onPointerDownOutside={(e) => e.preventDefault()}+ >+ {citationData ? (+ <div className="space-y-2">+ <div className="flex items-start space-x-2">+ <Avatar className="h-4 w-4 mt-0.5 flex-shrink-0">+ <AvatarImage+ src={`https://www.google.com/s2/favicons?domain=${+ getHostname(citationData.url)+ }`}+ alt={getHostname(citationData.url)}+ />+ <AvatarFallback className="text-xs">+ {getHostname(citationData.url)[0]?.toUpperCase() || '?'}+ </AvatarFallback>+ </Avatar>+ <div className="flex-1 min-w-0 space-y-1">+ <Link+ href={citationData.url}+ target="_blank"+ rel="noopener noreferrer"+ className="text-sm font-medium hover:underline block"+ >+ <span className="line-clamp-2">+ {citationData.title}+ </span>+ </Link>+ <p className="text-xs text-muted-foreground line-clamp-2">+ {citationData.content}+ </p>+ <p className="text-xs text-muted-foreground/80 truncate">+ {getHostname(citationData.url)}+ </p>+ </div>+ </div>+ </div>+ ) : null}+ </PopoverContent>+ </Popover>+ )+ }++ // For non-numbered citations, render as regular link+ return (+ <a+ href={href}+ target="_blank"+ rel="noopener noreferrer"+ className={linkClasses}+ >+ {children}+ </a>+ )+})\ No newline at end of filediff --git a/components/custom-link.tsx b/components/custom-link.tsxindex 16d971c3..213ef7b1 100644--- a/components/custom-link.tsx+++ b/components/custom-link.tsx@@ -2,6 +2,9 @@ import { AnchorHTMLAttributes, DetailedHTMLProps, ReactNode } from 'react'import { cn } from '@/lib/utils'+import { useCitation } from './citation-context'+import { CitationLink } from './citation-link'+type CustomLinkProps = Omit<DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>,'ref'@@ -15,24 +18,22 @@ export function Citing({className,...props}: CustomLinkProps) {+ const { citationMap } = useCitation()const childrenText = children?.toString() || ''const isNumber = /^\d+$/.test(childrenText)- const linkClasses = cn(- isNumber- ? 'text-[10px] bg-muted text-muted-froreground rounded-full w-4 h-4 px-0.5 inline-flex items-center justify-center hover:bg-muted/50 duration-200 no-underline -translate-y-0.5'- : 'hover:underline inline-flex items-center gap-1.5',- className- )++ // Get citation data if this is a numbered citation+ const citationNumber = isNumber ? parseInt(childrenText) : null+ const citationData = citationNumber && citationMap ? citationMap[citationNumber] : undefinedreturn (- <a- href={href}- target="_blank"- rel="noopener noreferrer"- className={linkClasses}+ <CitationLink+ href={href || '#'}+ className={className}+ citationData={citationData}{...props}>{children}- </a>+ </CitationLink>)}diff --git a/components/message.tsx b/components/message.tsxindex 08165b03..929075e2 100644--- a/components/message.tsx+++ b/components/message.tsx@@ -5,91 +5,106 @@ import rehypeKatex from 'rehype-katex'import remarkGfm from 'remark-gfm'import remarkMath from 'remark-math'+import type { SearchResultItem } from '@/lib/types'import { cn } from '@/lib/utils'+import { processCitations } from '@/lib/utils/citation'import { CodeBlock } from './ui/codeblock'import { MemoizedReactMarkdown } from './ui/markdown'+import { CitationProvider } from './citation-context'import { Citing } from './custom-link'import 'katex/dist/katex.min.css'export function BotMessage({message,- className+ className,+ citationMap}: {message: stringclassName?: string+ citationMap?: Record<number, SearchResultItem>}) {+ // Process citations to replace [number](#) with [number](actual-url)+ const processedMessage = processCitations(message || '', citationMap)+// Check if the content contains LaTeX patternsconst containsLaTeX = /\\\[([\s\S]*?)\\\]|\\\(([\s\S]*?)\\\)/.test(- message || ''+ processedMessage)// Modify the content to render LaTeX equations if LaTeX patterns are found- const processedData = preprocessLaTeX(message || '')+ const processedData = preprocessLaTeX(processedMessage)if (containsLaTeX) {return (+ <CitationProvider citationMap={citationMap}>+ <MemoizedReactMarkdown+ rehypePlugins={[+ [rehypeExternalLinks, { target: '_blank' }],+ [rehypeKatex]+ ]}+ remarkPlugins={[remarkGfm, remarkMath]}+ className={cn(+ 'prose-sm prose-neutral prose-a:text-accent-foreground/50',+ className+ )}+ components={{+ a: Citing+ }}+ >+ {processedData}+ </MemoizedReactMarkdown>+ </CitationProvider>+ )+ }++ return (+ <CitationProvider citationMap={citationMap}><MemoizedReactMarkdown- rehypePlugins={[- [rehypeExternalLinks, { target: '_blank' }],- [rehypeKatex]- ]}- remarkPlugins={[remarkGfm, remarkMath]}+ rehypePlugins={[[rehypeExternalLinks, { target: '_blank' }]]}+ remarkPlugins={[remarkGfm]}className={cn('prose-sm prose-neutral prose-a:text-accent-foreground/50',className)}- >- {processedData}- </MemoizedReactMarkdown>- )- }+ components={{+ code({ node, inline, className, children, ...props }) {+ if (children.length) {+ if (children[0] == '▍') {+ return (+ <span className="mt-1 cursor-default animate-pulse">▍</span>+ )+ }- return (- <MemoizedReactMarkdown- rehypePlugins={[[rehypeExternalLinks, { target: '_blank' }]]}- remarkPlugins={[remarkGfm]}- className={cn(- 'prose-sm prose-neutral prose-a:text-accent-foreground/50',- className- )}- components={{- code({ node, inline, className, children, ...props }) {- if (children.length) {- if (children[0] == '▍') {- return (- <span className="mt-1 cursor-default animate-pulse">▍</span>- )+ children[0] = (children[0] as string).replace('`▍`', '▍')}- children[0] = (children[0] as string).replace('`▍`', '▍')- }+ const match = /language-(\w+)/.exec(className || '')- const match = /language-(\w+)/.exec(className || '')+ if (inline) {+ return (+ <code className={className} {...props}>+ {children}+ </code>+ )+ }- if (inline) {return (- <code className={className} {...props}>- {children}- </code>+ <CodeBlock+ key={Math.random()}+ language={(match && match[1]) || ''}+ value={String(children).replace(/\n$/, '')}+ {...props}+ />)- }-- return (- <CodeBlock- key={Math.random()}- language={(match && match[1]) || ''}- value={String(children).replace(/\n$/, '')}- {...props}- />- )- },- a: Citing- }}- >- {message}- </MemoizedReactMarkdown>+ },+ a: Citing+ }}+ >+ {processedMessage}+ </MemoizedReactMarkdown>+ </CitationProvider>)}diff --git a/components/render-message.tsx b/components/render-message.tsxindex 751af4bf..e5813034 100644--- a/components/render-message.tsx+++ b/components/render-message.tsx@@ -2,6 +2,7 @@ import { UseChatHelpers } from '@ai-sdk/react'import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'import type { DynamicToolPart } from '@/lib/types/dynamic-tools'+import { extractCitationMap } from '@/lib/utils/citation'import { AnswerSection } from './answer-section'import { DataSection } from './data-section'@@ -36,6 +37,8 @@ export function RenderMessage({onUpdateMessage,reload}: RenderMessageProps) {+ // Extract citation map from the message's tool outputs+ const citationMap = extractCitationMap(message)if (message.role === 'user') {return (<>@@ -119,6 +122,7 @@ export function RenderMessage({messageId={messageId}reload={reload}status={status}+ citationMap={citationMap}/>)case 'reasoning':diff --git a/lib/agents/researcher-with-dynamic-tools.ts b/lib/agents/researcher-with-dynamic-tools.tsindex 0fa34ff1..2e088e0f 100644--- a/lib/agents/researcher-with-dynamic-tools.ts+++ b/lib/agents/researcher-with-dynamic-tools.ts@@ -23,8 +23,8 @@ When asked a question, you should:5. Use the fetch tool to get detailed content from specific URLs6. Use any available dynamic tools when they are relevant to the user's request7. 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.-9. If results are not relevant or helpful, rely on your general knowledge+8. **CRITICAL: You MUST cite sources inline using the [number](#) format** (e.g., "According to recent data [1](#), AI adoption has increased..."). Use [1](#), [2](#), [3](#), etc., matching the order of search results. Always use # as the URL. Include citations immediately after statements that use information from that source. Every piece of information from search results MUST have a citation.+9. If results are not relevant or helpful, rely on your general knowledge (but do not add citations for general knowledge)10. Provide comprehensive and detailed responses based on search results, ensuring thorough coverage of the user's question11. 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.**@@ -57,7 +57,9 @@ When using dynamic tools:- Handle any errors gracefully and inform the user if a tool failsCitation Format:-[number](url)+[number](#) - Always use this EXACT format with # as the URL, e.g., [1](#), [2](#), [3](#)+IMPORTANT: Citations must appear INLINE within your response text, not separately.+Example: "Nvidia's stock has risen 200% [1](#) due to AI demand [2](#)."`import type { DynamicToolConfig } from '@/lib/types/dynamic-tools'diff --git a/lib/agents/researcher.ts b/lib/agents/researcher.tsindex 8b69954e..29eab3f3 100644--- a/lib/agents/researcher.ts+++ b/lib/agents/researcher.ts@@ -17,8 +17,8 @@ When asked a question, you should: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 URLs6. 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+7. **CRITICAL: You MUST cite sources inline using the [number](#) format** (e.g., "According to recent data [1](#), AI adoption has increased..."). Use [1](#), [2](#), [3](#), etc., matching the order of search results. Always use # as the URL. Include citations immediately after statements that use information from that source. Every piece of information from search results MUST have a citation.+8. If results are not relevant or helpful, rely on your general knowledge (but do not add citations for general knowledge)9. Provide comprehensive and detailed responses based on search results, ensuring thorough coverage of the user's question10. 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.**@@ -46,7 +46,9 @@ When using the ask_question tool:- Match the language to the user's language (except option values which must be in English)Citation Format:-[number](url)+[number](#) - Always use this EXACT format with # as the URL, e.g., [1](#), [2](#), [3](#)+IMPORTANT: Citations must appear INLINE within your response text, not separately.+Example: "Nvidia's stock has risen 200% [1](#) due to AI demand [2](#)."`export function researcher({diff --git a/lib/tools/search.ts b/lib/tools/search.tsindex bd480c5d..288f7862 100644--- a/lib/tools/search.ts+++ b/lib/tools/search.ts@@ -1,7 +1,7 @@import { tool } from 'ai'import { getSearchSchemaForModel } from '@/lib/schema/search'-import { SearchResults } from '@/lib/types'+import { SearchResultItem,SearchResults } from '@/lib/types'import { getBaseUrlString } from '@/lib/utils/url'import {@@ -123,6 +123,15 @@ export function createSearchTool(fullModel: string) {}}+ // Add citation mapping to search results+ if (searchResult.results && searchResult.results.length > 0) {+ const citationMap: Record<number, SearchResultItem> = {}+ searchResult.results.forEach((result, index) => {+ citationMap[index + 1] = result // Citation numbers start at 1+ })+ searchResult.citationMap = citationMap+ }+console.log('completed search')return searchResult}diff --git a/lib/types/index.ts b/lib/types/index.tsindex 6cf60833..ac1c507f 100644--- a/lib/types/index.ts+++ b/lib/types/index.ts@@ -4,6 +4,7 @@ export type SearchResults = {videos?: SerperSearchResultItem[]number_of_results?: numberquery: string+ citationMap?: Record<number, SearchResultItem> // Maps citation number to search result}// If enabled the include_images_description is true, the images will be an array of { url: string, description: string }diff --git a/lib/utils/citation.ts b/lib/utils/citation.tsnew file mode 100644index 00000000..8e34b313--- /dev/null+++ b/lib/utils/citation.ts@@ -0,0 +1,72 @@+import type { SearchResultItem, SearchResults } from '@/lib/types'+import type { UIMessage } from '@/lib/types/ai'++/**+ * Validate if a string is a valid URL+ */+function isValidUrl(url: string): boolean {+ try {+ new URL(url)+ return true+ } catch {+ return false+ }+}++/**+ * Extract citation map from a message's tool parts+ * Looks for search tool outputs and combines their citation maps+ */+export function extractCitationMap(message: UIMessage): Record<number, SearchResultItem> | undefined {+ if (!message.parts) return undefined++ const citationMap: Record<number, SearchResultItem> = {}++ message.parts.forEach((part: any) => {+ // Check for search tool output+ if (part.type === 'tool-search' && part.state === 'output-available' && part.output) {+ const searchResults = part.output as SearchResults+ if (searchResults.citationMap) {+ // Merge citation maps from multiple search tools+ Object.assign(citationMap, searchResults.citationMap)+ }+ }+ })++ return Object.keys(citationMap).length > 0 ? citationMap : undefined+}++/**+ * Process citations in content, replacing [number](#) with proper URLs+ * while keeping the display as just [number]+ */+export function processCitations(+ content: string,+ citationMap?: Record<number, SearchResultItem>+): string {+ if (!citationMap || !content) {+ return content || ''+ }++ // Replace [number](#) with [number](actual-url)+ // Also handle cases with spaces: [ number ]+ return content.replace(+ /\[\s*(\d+)\s*\]\(#\)/g,+ (match, num) => {+ const citationNum = parseInt(num, 10)++ // Validate citation number bounds+ if (isNaN(citationNum) || citationNum < 1 || citationNum > 100) {+ return match+ }++ const citation = citationMap[citationNum]+ if (citation && isValidUrl(citation.url)) {+ // Encode URI to prevent injection attacks+ return `[${num}](${encodeURI(citation.url)})`+ }++ return match // Keep original if no valid citation found+ }+ )+}\ No newline at end of file