An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
style: apply prettier formatting
Yoshiki Miuraa98412157bb9
diff --git a/components/answer-section.tsx b/components/answer-section.tsxindex f639df81..458fbb30 100644--- a/components/answer-section.tsx+++ b/components/answer-section.tsx@@ -48,10 +48,7 @@ export function AnswerSection({const message = content ? (<div className="flex flex-col gap-1">- <BotMessage- message={content}- citationMap={citationMap}- />+ <BotMessage message={content} citationMap={citationMap} />{showActions && (<MessageActionsmessage={content} // Keep original message content for copydiff --git a/components/citation-context.tsx b/components/citation-context.tsxindex 06784042..e264c58d 100644--- a/components/citation-context.tsx+++ b/components/citation-context.tsx@@ -1,6 +1,6 @@'use client'-import { createContext, ReactNode,useContext } from 'react'+import { createContext, ReactNode, useContext } from 'react'import type { SearchResultItem } from '@/lib/types'@@ -8,12 +8,14 @@ interface CitationContextValue {citationMap?: Record<number, SearchResultItem>}-const CitationContext = createContext<CitationContextValue | undefined>(undefined)+const CitationContext = createContext<CitationContextValue | undefined>(+ undefined+)-export function CitationProvider({- children,+export function CitationProvider({+ children,citationMap-}: {+}: {children: ReactNodecitationMap?: Record<number, SearchResultItem>}) {@@ -27,4 +29,4 @@ export function CitationProvider({export function useCitation() {const context = useContext(CitationContext)return context || { citationMap: undefined }-}\ No newline at end of file+}diff --git a/components/citation-link.tsx b/components/citation-link.tsxindex b76bf51f..6b679d2a 100644--- a/components/citation-link.tsx+++ b/components/citation-link.tsx@@ -7,7 +7,11 @@ 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'+import {+ Popover,+ PopoverContent,+ PopoverTrigger+} from '@/components/ui/popover'interface CitationLinkProps {href: string@@ -34,7 +38,7 @@ export const CitationLink = memo(function CitationLink({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'@@ -72,21 +76,21 @@ export const CitationLink = memo(function CitationLink({{children}</a></PopoverTrigger>- <PopoverContent- className="w-72 p-3 z-50"- side="top"+ <PopoverContent+ className="w-72 p-3 z-50"+ side="top"align="center"sideOffset={8}- onPointerDownOutside={(e) => e.preventDefault()}+ 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)- }`}+ src={`https://www.google.com/s2/favicons?domain=${getHostname(+ citationData.url+ )}`}alt={getHostname(citationData.url)}/><AvatarFallback className="text-xs">@@ -100,9 +104,7 @@ export const CitationLink = memo(function CitationLink({rel="noopener noreferrer"className="text-sm font-medium hover:underline block">- <span className="line-clamp-2">- {citationData.title}- </span>+ <span className="line-clamp-2">{citationData.title}</span></Link><p className="text-xs text-muted-foreground line-clamp-2">{citationData.content}@@ -130,4 +132,4 @@ export const CitationLink = memo(function CitationLink({{children}</a>)-})\ No newline at end of file+})diff --git a/components/custom-link.tsx b/components/custom-link.tsxindex c2de3bfc..cc65dfdd 100644--- a/components/custom-link.tsx+++ b/components/custom-link.tsx@@ -19,10 +19,11 @@ export function Citing({const { citationMap } = useCitation()const childrenText = children?.toString() || ''const isNumber = /^\d+$/.test(childrenText)-+// Get citation data if this is a numbered citationconst citationNumber = isNumber ? parseInt(childrenText) : null- const citationData = citationNumber && citationMap ? citationMap[citationNumber] : undefined+ const citationData =+ citationNumber && citationMap ? citationMap[citationNumber] : undefinedreturn (<CitationLinkdiff --git a/components/message.tsx b/components/message.tsxindex 282259d9..b4acad35 100644--- a/components/message.tsx+++ b/components/message.tsx@@ -1,6 +1,5 @@'use client'-import rehypeExternalLinks from 'rehype-external-links'import rehypeKatex from 'rehype-katex'import remarkGfm from 'remark-gfm'@@ -28,7 +27,7 @@ export function BotMessage({}) {// 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(processedMessage@@ -42,12 +41,10 @@ export function BotMessage({code(props: any) {const { children, className, ...rest } = propsconst inline = !('data-language' in props)-+if (children && typeof children === 'string') {if (children === '▍') {- return (- <span className="mt-1 cursor-default animate-pulse">▍</span>- )+ return <span className="mt-1 cursor-default animate-pulse">▍</span>}const processedChildren = children.replace('`▍`', '▍')@@ -83,10 +80,12 @@ export function BotMessage({if (containsLaTeX) {return (<CitationProvider citationMap={citationMap}>- <div className={cn(- 'prose-sm prose-neutral prose-a:text-accent-foreground/50',- className- )}>+ <div+ className={cn(+ 'prose-sm prose-neutral prose-a:text-accent-foreground/50',+ className+ )}+ ><MemoizedReactMarkdownrehypePlugins={[[rehypeExternalLinks, { target: '_blank' }],@@ -107,10 +106,12 @@ export function BotMessage({return (<CitationProvider citationMap={citationMap}>- <div className={cn(- 'prose-sm prose-neutral prose-a:text-accent-foreground/50',- className- )}>+ <div+ className={cn(+ 'prose-sm prose-neutral prose-a:text-accent-foreground/50',+ className+ )}+ ><MemoizedReactMarkdownrehypePlugins={[[rehypeExternalLinks, { target: '_blank' }]]}remarkPlugins={[remarkGfm]}diff --git a/components/ui/markdown.tsx b/components/ui/markdown.tsxindex 546df049..c7aa1651 100644--- a/components/ui/markdown.tsx+++ b/components/ui/markdown.tsx@@ -3,6 +3,5 @@ import ReactMarkdown, { Options } from 'react-markdown'export const MemoizedReactMarkdown: FC<Options> = memo(ReactMarkdown,- (prevProps, nextProps) =>- prevProps.children === nextProps.children+ (prevProps, nextProps) => prevProps.children === nextProps.children)diff --git a/lib/tools/search.ts b/lib/tools/search.tsindex 288f7862..e8d331f0 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 { SearchResultItem,SearchResults } from '@/lib/types'+import { SearchResultItem, SearchResults } from '@/lib/types'import { getBaseUrlString } from '@/lib/utils/url'import {diff --git a/lib/utils/citation.ts b/lib/utils/citation.tsindex 8e34b313..71f42631 100644--- a/lib/utils/citation.ts+++ b/lib/utils/citation.ts@@ -17,14 +17,20 @@ function isValidUrl(url: string): boolean {* 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 {+export function extractCitationMap(+ message: UIMessage+): Record<number, SearchResultItem> | undefined {if (!message.parts) return undefinedconst 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) {+ if (+ part.type === 'tool-search' &&+ part.state === 'output-available' &&+ part.output+ ) {const searchResults = part.output as SearchResultsif (searchResults.citationMap) {// Merge citation maps from multiple search tools@@ -41,32 +47,29 @@ export function extractCitationMap(message: UIMessage): Record<number, SearchRes* while keeping the display as just [number]*/export function processCitations(- content: string,+ 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+ 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}- )-}\ No newline at end of file++ 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+ })+}