import type { SuperagentMediaAttachment } from '../../types'; export const MAX_ATTACHMENTS = 10; export const SUPERAGENT_SUPPORTED_ATTACHMENT_EXTENSIONS = [ '.jpg', '.jpeg', '.png', '.webp', '.pdf', '.txt', '.html', '.csv', '.xlsx', '.docx', '.json', '.md', ] as const; export const SUPERAGENT_ATTACHMENT_SIZE_LIMIT_MB: Record = { '.jpg': 40, '.jpeg': 40, '.png': 40, '.webp': 40, '.pdf': 10, '.txt': 5, '.html': 5, '.csv': 10, '.xlsx': 15, '.docx': 5, '.json': 10, '.md': 5, }; export function normalizeMediaAttachments(input: unknown) { const attachments = Array.isArray(input) ? input : input ? [input] : []; return attachments.filter(isAttachment).filter((attachment) => isSafeAttachmentUrl(attachment.url)); } export function buildAttachmentPrompt(attachments: SuperagentMediaAttachment[]) { const images = attachments.filter(isImageAttachment); const allImages = images.length === attachments.length; if (allImages && attachments.length === 1) return "What's in this image?"; if (allImages && attachments.length > 1) return `What's in these ${attachments.length} images?`; if (attachments.length === 1) return `Here's a file: ${getAttachmentName(attachments[0])}`; return `Here are ${attachments.length} files.`; } export function getAttachmentName(attachment: SuperagentMediaAttachment) { if (attachment.name) return attachment.name; const cleanUrl = attachment.url.split('?')[0]; const filename = cleanUrl.split('/').pop(); if (!filename) return 'file'; try { return decodeURIComponent(filename); } catch { return filename; } } export function isImageAttachment(attachment: SuperagentMediaAttachment) { if (attachment.kind === 'image') return true; if (attachment.mimeType?.startsWith('image/')) return true; return isImageUrl(attachment.url); } export function isImageUrl(url: string) { return /\.(jpg|jpeg|png|gif|webp|bmp)(?:\?|$)/i.test(url); } /** * Recognized file types — each maps to a distinct icon in `FileAttachment`. * Mirrors the web DS `FileAttachment` (`file-attachment.tsx`) `FileType` union * and the builder's `fileTypeFromUrl` classifier. */ export type FileType = 'pdf' | 'image' | 'slides' | 'spreadsheet' | 'docs' | 'video' | 'upload'; export function fileTypeFromUrl(url: string): FileType { if (isImageUrl(url)) return 'image'; const ext = url.split('?')[0].split('.').pop()?.toLowerCase() ?? ''; if (ext === 'pdf') return 'pdf'; if (['ppt', 'pptx', 'key'].includes(ext)) return 'slides'; if (['xls', 'xlsx', 'csv'].includes(ext)) return 'spreadsheet'; if (['doc', 'docx', 'txt', 'md'].includes(ext)) return 'docs'; if (['mp4', 'mov', 'webm'].includes(ext)) return 'video'; return 'upload'; } function isAttachment(value: unknown): value is SuperagentMediaAttachment { return Boolean(value && typeof value === 'object' && typeof (value as { url?: unknown }).url === 'string'); } function isSafeAttachmentUrl(url: string) { return /^https?:\/\//i.test(url.trim()) && !url.includes('\0'); }