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.

5.6 KB
Raw Blame
import type { BlockAlias, BlockName } from '@core/block';
import { match } from 'ts-pattern';
import {
  type CallEntity,
  type ChannelEntity,
  type ChatEntity,
  type DocumentEntity,
  type EmailEntity,
  type EntityData,
  type ProjectEntity,
  type RoutineEntity,
  routineStatus,
  type SkillEntity,
  type SnippetEntity,
  type TaskEntity,
} from '../types/entity';

export type BuildEntityDataArgs = {
  id: string;
  name: string;
  blockName: BlockName | BlockAlias;
  ownerId?: string;
  projectId?: string;
  fileType?: string;
  isCompleted?: boolean;
  channelType?: ChannelEntity['channelType'];
  teamId?: string;
  isParticipant?: boolean;
  cron?: string;
  enabled?: boolean;
  channelId?: string | null;
  botId?: string;
  sessionStatus?: string;
  isActive?: boolean;
  status?: CallEntity['status'];
  attended?: boolean;
  participantIds?: string[];
  isRead?: boolean;
  isDraft?: boolean;
  isImportant?: boolean;
  done?: boolean;
};

/**
 * Build a full `EntityData` from the simple shape that's typically
 * available at component level (id, name, blockName).
 *
 * The `blockName` discriminator is funneled into the right `EntityData`
 * variant: e.g. `'task'` becomes `{ type: 'document', fileType: 'md',
 * subType: { type: 'task' } }`, `'pdf'` becomes `{ type: 'document',
 * fileType: 'pdf' }`, etc.
 *
 * Returns `undefined` when required fields for the resolved variant are
 * missing (e.g. a `'channel'` without a `channelType`).
 */
export function buildEntityData(
  args: BuildEntityDataArgs
): EntityData | undefined {
  const { id, name, blockName, ownerId = '' } = args;
  if (!id || !name || !blockName) return undefined;

  const base = { id, name, ownerId };

  return (
    match<BlockName | BlockAlias, EntityData | undefined>(blockName)
      .with(
        'task',
        (): TaskEntity => ({
          ...base,
          type: 'document',
          fileType: 'md',
          subType: { type: 'task', is_completed: args.isCompleted ?? false },
          projectId: args.projectId,
        })
      )
      .with(
        'snippet',
        (): SnippetEntity => ({
          ...base,
          type: 'document',
          fileType: 'md',
          subType: { type: 'snippet' },
          projectId: args.projectId,
        })
      )
      .with(
        'skill',
        (): SkillEntity => ({
          ...base,
          type: 'document',
          fileType: 'md',
          subType: { type: 'skill' },
          projectId: args.projectId,
        })
      )
      .with(
        'md',
        (): DocumentEntity => ({
          ...base,
          type: 'document',
          fileType: 'md',
          projectId: args.projectId,
        })
      )
      .with(
        'pdf',
        'write',
        'code',
        'image',
        'canvas',
        'spreadsheet',
        'pptx',
        'psd',
        'fig',
        'ai',
        'video',
        'unknown',
        'csv',
        (name): DocumentEntity => ({
          ...base,
          type: 'document',
          fileType: args.fileType ?? name,
          projectId: args.projectId,
        })
      )
      .with(
        'chat',
        (): ChatEntity => ({
          ...base,
          type: 'chat',
          projectId: args.projectId,
        })
      )
      .with(
        'project',
        (): ProjectEntity => ({
          ...base,
          type: 'project',
          projectId: args.projectId,
        })
      )
      .with('channel', (): ChannelEntity | undefined => {
        if (!args.channelType) return undefined;
        return {
          ...base,
          type: 'channel',
          channelType: args.channelType,
          ...(args.teamId === undefined ? {} : { teamId: args.teamId }),
          ...(args.isParticipant === undefined
            ? {}
            : { isParticipant: args.isParticipant }),
        };
      })
      .with(
        'email',
        (): EmailEntity => ({
          ...base,
          type: 'email',
          isRead: args.isRead ?? true,
          isDraft: args.isDraft ?? false,
          isImportant: args.isImportant ?? false,
          done: args.done ?? false,
        })
      )
      .with('routine', (): RoutineEntity | undefined => {
        if (!args.cron) return undefined;
        return {
          ...base,
          type: 'routine',
          cron: args.cron,
          status: routineStatus({
            enabled: args.enabled ?? false,
            isRunning: false,
          }),
        };
      })
      .with('call', (): CallEntity | undefined => {
        const status: CallEntity['status'] =
          args.status ?? (args.attended ? 'ATTENDED' : 'UNATTENDED');

        return {
          ...base,
          type: 'call',
          channelId: args.channelId,
          isActive: args.isActive ?? false,
          status,
          attended: status === 'ATTENDED',
          participantIds: args.participantIds ?? [],
        };
      })
      // The singleton calendar block has no entity-shaped block id.
      .with('calendar', (): undefined => undefined)
      // Databases, forms and pipelines use REST; CRM records and initiatives come from Soup.
      .with(
        'database',
        'form',
        'pipeline',
        'company',
        'contact',
        'initiative',
        (): undefined => undefined
      )
      // PRs are virtual blocks backed by GitHub, not Macro entities.
      .with('pr', (): undefined => undefined)
      .with('agent', (): EntityData | undefined =>
        args.botId
          ? {
              ...base,
              type: 'agent_session',
              botId: args.botId,
              status: args.sessionStatus ?? 'no_messages',
            }
          : undefined
      )
      .exhaustive()
  );
}