Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

3.1 KB
Raw Blame
import { fileTypeToBlockName } from '@core/constant/allBlocks';
import { filenameWithoutExtension } from '@service-storage/util/filename';
import {
  CHANNEL_IMAGE_FILE_EXTENSIONS,
  CHANNEL_VIDEO_FILE_EXTENSIONS,
} from '../accepted-file-types';
import type { InputAttachmentData, InputAttachmentKind } from '../types';

export type UploadFailedResult = {
  failed: true;
  /** Shown instead of the generic failure, when the uploader knows why. */
  reason?: string;
};

/** A file the host stored for its own send, such as a WhatsApp group upload. */
type UploadStagedSuccessResult = {
  failed: false;
  destination: 'staged';
  id: string;
};

export type UploadStaticSuccessResult = {
  failed: false;
  destination: 'static';
  id: string;
};

export type UploadDocumentSuccessResult = {
  failed: false;
  destination: 'dss';
  type: 'document';
  documentId: string;
  fileType?: string;
};

export type UploadResult =
  | UploadFailedResult
  | UploadStaticSuccessResult
  | UploadStagedSuccessResult
  | UploadDocumentSuccessResult
  | {
      failed: false;
      destination: 'dss';
      type: string;
    };

export type UploadSuccess = Exclude<UploadResult, UploadFailedResult>;

const IMAGE_EXTENSIONS: ReadonlySet<string> = new Set(
  CHANNEL_IMAGE_FILE_EXTENSIONS
);
const VIDEO_EXTENSIONS: ReadonlySet<string> = new Set(
  CHANNEL_VIDEO_FILE_EXTENSIONS
);

export function fileExtension(filename: string): string | undefined {
  const extension = filename.split('.').pop()?.toLowerCase();
  if (!extension || extension === filename.toLowerCase()) return;
  return extension;
}

export function iconTypeFromFilename(filename: string) {
  return fileTypeToBlockName(fileExtension(filename), true);
}

export function getAttachmentKindFromFile(file: {
  name: string;
  mimeType?: string;
  type?: string;
}): InputAttachmentKind {
  const mimeType = file.mimeType ?? file.type ?? '';
  if (mimeType.startsWith('image/')) return 'image';
  if (mimeType.startsWith('video/')) return 'video';

  const extension = fileExtension(file.name);
  if (!extension) return 'document';

  if (IMAGE_EXTENSIONS.has(extension)) return 'image';
  if (VIDEO_EXTENSIONS.has(extension)) return 'video';
  return 'document';
}

export function buildUploadedAttachment(
  file: { name: string },
  pendingKind: InputAttachmentKind,
  result: UploadSuccess
): InputAttachmentData | undefined {
  if (result.destination === 'static' || result.destination === 'staged') {
    // A static upload keeps the kind the file was classified as: channels
    // only send media here, but the agent composer sends every file, and a
    // PDF stored as a static file is still a document chip.
    return {
      id: result.id,
      name: file.name,
      kind: pendingKind,
      iconType:
        pendingKind === 'document'
          ? iconTypeFromFilename(file.name)
          : undefined,
    };
  }

  if (
    result.destination === 'dss' &&
    result.type === 'document' &&
    'documentId' in result
  ) {
    return {
      id: result.documentId,
      name: filenameWithoutExtension(file.name) ?? file.name,
      kind: 'document',
      iconType: fileTypeToBlockName(result.fileType, true),
    };
  }

  return undefined;
}