The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
'use client';
import { cn } from '@/components/assistant-ui/lib/utils';
import type { FileMessagePartComponent } from '@assistant-ui/react';
import { cva, type VariantProps } from 'class-variance-authority';
import {
BracesIcon,
DownloadIcon,
FileIcon,
FileTextIcon,
ImageIcon,
MusicIcon,
VideoIcon,
} from 'lucide-react';
import { memo } from 'react';
const fileVariants = cva(
'aui-file-root inline-flex items-center gap-3 rounded-lg transition-colors',
{
variants: {
variant: {
outline: 'border-border hover:bg-muted/50 border',
ghost: 'hover:bg-muted/50',
muted: 'bg-muted/50 hover:bg-muted/70',
},
size: {
sm: 'px-2.5 py-1.5 text-xs',
default: 'px-3 py-2 text-sm',
lg: 'px-4 py-3 text-base',
},
},
defaultVariants: { variant: 'outline', size: 'default' },
}
);
function renderMimeTypeIcon(mimeType: string | undefined) {
const type = mimeType?.toLowerCase();
if (type?.startsWith('image/')) return <ImageIcon className="size-5" />;
if (type === 'application/pdf' || type === 'application/json' || type?.startsWith('text/')) {
return type === 'application/json' ? (
<BracesIcon className="size-5" />
) : (
<FileTextIcon className="size-5" />
);
}
if (type?.startsWith('audio/')) return <MusicIcon className="size-5" />;
if (type?.startsWith('video/')) return <VideoIcon className="size-5" />;
return <FileIcon className="size-5" />;
}
export type FileDataKind = 'data-uri' | 'url' | 'base64' | 'id';
function getFileDataKind(data: string, sourceType?: 'url' | 'id'): FileDataKind {
if (sourceType === 'url' && /^data:/i.test(data)) return 'data-uri';
if (sourceType) return sourceType;
if (/^data:/i.test(data)) return 'data-uri';
if (/^https?:\/\//i.test(data)) return 'url';
return 'base64';
}
function getBase64Size(base64: string): number {
const commaIndex = base64.indexOf(',');
const base64Data = commaIndex >= 0 ? base64.slice(commaIndex + 1) : base64;
const padding = (base64Data.match(/=/g) || []).length;
return Math.floor((base64Data.length * 3) / 4) - padding;
}
function formatFileSize(bytes: number): string {
if (bytes < 1024) {
return `${bytes} B`;
}
if (bytes < 1024 * 1024) {
return `${(bytes / 1024).toFixed(1)} KB`;
}
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}
export type FileRootProps = React.ComponentProps<'div'> & VariantProps<typeof fileVariants>;
function FileRoot({ className, variant, size, children, ...props }: FileRootProps) {
return (
<div
data-slot="file-root"
data-variant={variant}
data-size={size}
className={cn(fileVariants({ variant, size, className }))}
{...props}>
{children}
</div>
);
}
type FileIconDisplayProps = React.ComponentProps<'span'> & { mimeType?: string };
function FileIconDisplay({ mimeType, className, children, ...props }: FileIconDisplayProps) {
return (
<span
data-slot="file-icon"
className={cn('text-muted-foreground shrink-0', className)}
{...props}>
{children ?? renderMimeTypeIcon(mimeType)}
</span>
);
}
function FileName({ className, children, ...props }: React.ComponentProps<'span'>) {
return (
<span
data-slot="file-name"
className={cn('min-w-0 flex-1 truncate font-medium', className)}
{...props}>
{children || 'Unnamed file'}
</span>
);
}
type FileSizeProps = React.ComponentProps<'span'> & { bytes: number };
function FileSize({ bytes, className, ...props }: FileSizeProps) {
return (
<span
data-slot="file-size"
className={cn('text-muted-foreground shrink-0', className)}
{...props}>
{formatFileSize(bytes)}
</span>
);
}
type FileDownloadProps = Omit<React.ComponentProps<'a'>, 'href'> & {
data: string;
mimeType: string;
filename?: string;
sourceType?: 'url' | 'id';
};
function FileDownload({
data,
mimeType,
filename,
sourceType,
className,
children,
...props
}: FileDownloadProps) {
if (typeof data !== 'string') return null;
const kind = getFileDataKind(data, sourceType);
if (kind === 'id') return null;
if (kind === 'url' && !/^(https?:\/\/|blob:)/i.test(data)) return null;
const href = kind === 'base64' ? `data:${mimeType};base64,${data}` : data;
return (
<a
data-slot="file-download"
href={href}
download={filename || 'download'}
{...(kind === 'url' && { target: '_blank', rel: 'noopener noreferrer' })}
className={cn(
'text-muted-foreground hover:bg-accent hover:text-accent-foreground shrink-0 rounded-md p-1 transition-colors',
className
)}
{...props}>
{children || <DownloadIcon className="size-4" />}
</a>
);
}
const FileImpl: FileMessagePartComponent = ({ filename, data, mimeType, sourceType }) => {
const kind = getFileDataKind(data, sourceType);
const showSize = typeof data === 'string' && (kind === 'base64' || kind === 'data-uri');
return (
<FileRoot>
<FileIconDisplay mimeType={mimeType} />
<div className="flex min-w-0 flex-1 flex-col gap-0.5">
<FileName>{filename}</FileName>
{showSize && <FileSize bytes={getBase64Size(data)} className="text-xs" />}
</div>
<FileDownload
data={data}
mimeType={mimeType}
{...(filename !== undefined && { filename })}
{...(sourceType !== undefined && { sourceType })}
/>
</FileRoot>
);
};
const File = memo(FileImpl) as unknown as FileMessagePartComponent & {
Root: typeof FileRoot;
Icon: typeof FileIconDisplay;
Name: typeof FileName;
Size: typeof FileSize;
Download: typeof FileDownload;
};
File.displayName = 'File';
File.Root = FileRoot;
File.Icon = FileIconDisplay;
File.Name = FileName;
File.Size = FileSize;
File.Download = FileDownload;
export { File };