Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

8.1 KB
Raw Blame
import { useCallback, useEffect, useRef, useState } from 'react';
import { readFileAsBase64 } from '../utils/fileUpload';
import { formatBytes } from '../utils/formatters';

const MAX_ATTACHMENTS = 8;
// Matches server-side ATTACHMENT_BASE64_MAX_CHARS (13,333,333 chars). Base64 expands raw bytes
// by 4/3, so 13,333,333 chars ≈ 9,999,999 raw bytes after block-rounding. Use that as the limit
// so a file that passes client validation is guaranteed to pass server validation too.
const MAX_ATTACHMENT_FILE_SIZE = 9_999_999; // effective per-file server limit in raw bytes
// Matches server-side ATTACHMENTS_TOTAL_BASE64_MAX_CHARS (50,000,000 chars ≈ 37.5MB raw)
const MAX_ATTACHMENTS_TOTAL_BASE64_CHARS = 50_000_000;

const ALLOWED_ATTACHMENT_MIME_PREFIXES = ['image/'];
const ALLOWED_ATTACHMENT_MIME_TYPES = new Set([
  'text/plain', 'text/markdown', 'text/x-markdown',
  'application/json', 'text/csv', 'application/csv', 'text/x-csv',
  'application/pdf'
]);
const ALLOWED_ATTACHMENT_EXTENSIONS = new Set(['.txt', '.md', '.json', '.csv', '.pdf']);

/**
 * `accept` for the composer's file picker, derived from the same allowlists
 * `isAllowedAttachmentType` enforces — so the picker can never offer a file the
 * hook then rejects (or hide one it would have taken). Extensions AND MIME
 * types, because iOS/iPadOS pickers only understand the latter and grey out
 * everything given an extension-only filter.
 */
export const OPENCLAW_ATTACHMENT_ACCEPT = [
  ...ALLOWED_ATTACHMENT_MIME_PREFIXES.map((p) => `${p}*`),
  ...ALLOWED_ATTACHMENT_MIME_TYPES,
  ...ALLOWED_ATTACHMENT_EXTENSIONS
].join(',');

function getAttachmentKind(file) {
  return file.type.startsWith('image/') ? 'image' : 'file';
}

function isAllowedAttachmentType(file) {
  if (ALLOWED_ATTACHMENT_MIME_PREFIXES.some(p => file.type?.startsWith(p))) return true;
  const mimeBase = file.type ? file.type.split(';')[0].trim().toLowerCase() : '';
  if (mimeBase && ALLOWED_ATTACHMENT_MIME_TYPES.has(mimeBase)) return true;
  const ext = file.name ? `.${file.name.split('.').pop().toLowerCase()}` : '';
  return ALLOWED_ATTACHMENT_EXTENSIONS.has(ext);
}

async function filesToAttachments(files) {
  const createdPreviewUrls = [];
  try {
    return await Promise.all(files.map(async (file) => {
      const kind = getAttachmentKind(file);
      const mediaType = file.type || 'application/octet-stream';
      const base64 = await readFileAsBase64(file);
      const previewUrl = kind === 'image' ? URL.createObjectURL(file) : '';
      if (previewUrl) createdPreviewUrls.push(previewUrl);
      return {
        id: `attachment-${file.name}-${file.lastModified}-${Math.random().toString(36).slice(2, 8)}`,
        name: file.name,
        filename: file.name,
        mediaType,
        kind,
        size: file.size,
        data: base64,
        previewUrl
      };
    }));
  } catch (err) {
    createdPreviewUrls.forEach(url => URL.revokeObjectURL(url));
    throw err;
  }
}

export function useOpenClawAttachments({ sending, onError = () => {} } = {}) {
  const [attachments, setAttachments] = useState([]);
  const [isDragActive, setIsDragActive] = useState(false);
  const dragCounterRef = useRef(0);
  const attachmentsRef = useRef(attachments);

  useEffect(() => {
    attachmentsRef.current = attachments;
  }, [attachments]);

  // Revoke any remaining object URLs when the hook unmounts — owned here since
  // previewUrls are created by this hook.
  useEffect(() => {
    return () => {
      attachmentsRef.current.forEach(a => { if (a.previewUrl) URL.revokeObjectURL(a.previewUrl); });
    };
  }, []);

  const appendFiles = useCallback(async (files) => {
    if (!files || files.length === 0) return;
    if (sending) return;

    const remainingSlots = MAX_ATTACHMENTS - attachmentsRef.current.length;

    if (remainingSlots <= 0) {
      onError(`You can attach up to ${MAX_ATTACHMENTS} files per message.`);
      return;
    }

    const limitedFiles = files.slice(0, remainingSlots);
    const tooLargeFile = limitedFiles.find(
      (file) => typeof file.size === 'number' && file.size > MAX_ATTACHMENT_FILE_SIZE
    );

    if (tooLargeFile) {
      onError(
        `"${tooLargeFile.name}" is too large. Maximum attachment size is ${formatBytes(MAX_ATTACHMENT_FILE_SIZE, 0)}.`
      );
      return;
    }

    const disallowedFile = limitedFiles.find(file => !isAllowedAttachmentType(file));
    if (disallowedFile) {
      onError(`"${disallowedFile.name}" is not a supported file type. Allowed: images, .txt, .md, .json, .csv, .pdf`);
      return;
    }

    try {
      const next = await filesToAttachments(limitedFiles);
      const newBase64Chars = next.reduce((sum, a) => sum + (typeof a.data === 'string' ? a.data.length : 0), 0);

      // Read live state via the ref after the async work completes, then call
      // setAttachments once with the deterministic result. Concurrent appendFiles
      // calls are not possible in this single-user UI (paste/picker/drop are all
      // sequential user gestures), so the ref is authoritative here.
      const liveAttachments = attachmentsRef.current;
      const existingBase64Chars = liveAttachments.reduce((sum, a) => sum + (typeof a.data === 'string' ? a.data.length : 0), 0);
      if (existingBase64Chars + newBase64Chars > MAX_ATTACHMENTS_TOTAL_BASE64_CHARS) {
        next.forEach(a => { if (a.previewUrl) URL.revokeObjectURL(a.previewUrl); });
        // The 50 MB ceiling counts base64 CHARS (decimal), so it stays a literal;
        // the decoded estimate is real bytes and goes through the canonical helper.
        const decodedBytes = (existingBase64Chars + newBase64Chars) * 0.75;
        onError(`Combined attachments exceed the 50 MB encoded total limit (~${formatBytes(decodedBytes, 0)} decoded).`);
        return;
      }

      const liveRemaining = MAX_ATTACHMENTS - liveAttachments.length;
      if (liveRemaining <= 0) {
        next.forEach(a => { if (a.previewUrl) URL.revokeObjectURL(a.previewUrl); });
        onError(`You can attach up to ${MAX_ATTACHMENTS} files per message.`);
        return;
      }

      const accepted = next.slice(0, liveRemaining);
      next.slice(liveRemaining).forEach(a => { if (a.previewUrl) URL.revokeObjectURL(a.previewUrl); });
      setAttachments(prev => [...prev, ...accepted]);
      onError('');
    } catch (err) {
      onError(err.message || 'Failed to prepare attachment');
    }
  }, [sending, onError]);

  const removeAttachment = (attachmentId) => {
    setAttachments(current => {
      const toRemove = current.find(a => a.id === attachmentId);
      if (toRemove?.previewUrl) URL.revokeObjectURL(toRemove.previewUrl);
      return current.filter(item => item.id !== attachmentId);
    });
  };

  const handleAttachmentSelect = async (event) => {
    const files = Array.from(event.target.files || []);
    await appendFiles(files);
  };

  const handlePaste = async (event) => {
    const items = Array.from(event.clipboardData?.items || []);
    const files = items
      .map(item => item.getAsFile())
      .filter(Boolean);

    if (files.length === 0) return;
    event.preventDefault();
    await appendFiles(files);
  };

  const handleDragEnter = (event) => {
    event.preventDefault();
    event.stopPropagation();
    if (event.dataTransfer?.types?.includes('Files')) {
      dragCounterRef.current += 1;
      setIsDragActive(true);
    }
  };

  const handleDragOver = (event) => {
    event.preventDefault();
    event.stopPropagation();
    if (event.dataTransfer) event.dataTransfer.dropEffect = 'copy';
  };

  const handleDragLeave = (event) => {
    event.preventDefault();
    event.stopPropagation();
    dragCounterRef.current -= 1;
    if (dragCounterRef.current <= 0) {
      dragCounterRef.current = 0;
      setIsDragActive(false);
    }
  };

  const handleDrop = async (event) => {
    event.preventDefault();
    event.stopPropagation();
    dragCounterRef.current = 0;
    setIsDragActive(false);
    const files = Array.from(event.dataTransfer?.files || []);
    await appendFiles(files);
  };

  return {
    attachments,
    setAttachments,
    isDragActive,
    appendFiles,
    removeAttachment,
    handleAttachmentSelect,
    handlePaste,
    handleDragEnter,
    handleDragOver,
    handleDragLeave,
    handleDrop
  };
}