Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

4.4 KB
Raw Blame
'use client';

/**
 * Vendored from the assistant-ui `sources` registry item
 * (https://r.assistant-ui.com/styles/base-nova/sources.json). Exposes the
 * `Source`/`SourceIcon`/`SourceTitle`/`DocumentSourceIcon` primitives; the
 * upstream `Sources` message-part component was dropped as unused — callers
 * compose the primitives directly (see `ChatSources.tsx`).
 *
 * Changes from upstream:
 * - `cn` import path (`@/components/assistant-ui/lib/utils`).
 */
import { cn } from '@/components/assistant-ui/lib/utils';
import { cva, type VariantProps } from 'class-variance-authority';
import { FileTextIcon } from 'lucide-react';
import { type ComponentProps, useState } from 'react';

const sourceVariants = cva(
  'inline-flex items-center justify-center gap-1 rounded-md text-xs font-medium transition-colors [&_svg]:size-3 [&_svg]:shrink-0',
  {
    variants: {
      variant: {
        outline:
          'border-input text-muted-foreground [a&]:hover:bg-accent [a&]:hover:text-accent-foreground border bg-transparent',
        secondary: 'bg-secondary text-secondary-foreground [a&]:hover:bg-secondary/80',
        muted: 'bg-muted text-muted-foreground [a&]:hover:bg-muted/80 [a&]:hover:text-foreground',
        ghost:
          'text-muted-foreground [a&]:hover:bg-accent [a&]:hover:text-accent-foreground bg-transparent',
        info: 'bg-blue-100 text-blue-700 dark:bg-blue-900/50 dark:text-blue-300 [a&]:hover:bg-blue-100/80',
        warning:
          'bg-amber-100 text-amber-700 dark:bg-amber-900/50 dark:text-amber-300 [a&]:hover:bg-amber-100/80',
        success:
          'bg-emerald-100 text-emerald-700 dark:bg-emerald-900/50 dark:text-emerald-300 [a&]:hover:bg-emerald-100/80',
        destructive:
          'bg-red-100 text-red-700 dark:bg-red-900/50 dark:text-red-300 [a&]:hover:bg-red-100/80',
      },
      size: { sm: 'px-1.5 py-0.5', default: 'px-2 py-1', lg: 'px-2.5 py-1.5 text-sm' },
    },
    defaultVariants: { variant: 'outline', size: 'default' },
  }
);

const extractDomain = (url: string): string => {
  try {
    return new URL(url).hostname.replace(/^www\./, '');
  } catch {
    return url;
  }
};

const defaultFaviconUrl = (domain: string) => `https://icons.duckduckgo.com/ip3/${domain}.ico`;

function SourceIcon({
  url,
  className,
  faviconUrl = defaultFaviconUrl,
  ...props
}: ComponentProps<'span'> & { url: string; faviconUrl?: (domain: string) => string }) {
  const domain = extractDomain(url);
  const src = faviconUrl(domain);
  const [errorSrc, setErrorSrc] = useState<string | undefined>(undefined);
  const hasError = errorSrc === src;

  if (hasError) {
    return (
      <span
        data-slot="source-icon-fallback"
        className={cn(
          'bg-muted flex size-3 shrink-0 items-center justify-center rounded-sm text-[10px] font-medium',
          className
        )}
        {...props}>
        {domain.charAt(0).toUpperCase() || '?'}
      </span>
    );
  }

  return (
    <img
      data-slot="source-icon"
      src={src}
      alt=""
      className={cn('size-3 shrink-0 rounded-sm', className)}
      onError={() => setErrorSrc(src)}
      {...(props as ComponentProps<'img'>)}
      // A server-rendered image that fails before hydration never fires onError.
      ref={el => {
        if (el?.complete && el.naturalWidth === 0) setErrorSrc(src);
      }}
    />
  );
}

function SourceTitle({ className, ...props }: ComponentProps<'span'>) {
  return (
    <span data-slot="source-title" className={cn('max-w-37.5 truncate', className)} {...props} />
  );
}

function DocumentSourceIcon({ className, ...props }: ComponentProps<'span'>) {
  return (
    <span
      data-slot="source-document-icon"
      className={cn(
        'text-muted-foreground flex size-3 shrink-0 items-center justify-center',
        className
      )}
      {...props}>
      <FileTextIcon className="size-3" />
    </span>
  );
}

export type SourceProps = ComponentProps<'a'> & VariantProps<typeof sourceVariants>;

function Source({
  className,
  variant,
  size,
  target = '_blank',
  rel = 'noopener noreferrer',
  ...props
}: SourceProps) {
  return (
    <a
      data-slot="source"
      className={cn(
        sourceVariants({ variant, size }),
        'focus-visible:border-ring focus-visible:ring-ring/50 cursor-pointer outline-none focus-visible:ring-1',
        className
      )}
      target={target}
      rel={rel}
      {...props}
    />
  );
}

export { Source, SourceIcon, SourceTitle, DocumentSourceIcon };