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.8 KB
Raw Blame
import type { EntityData, Notification, WithNotification } from '@entity';
import {
  getSortedKeyProperties,
  soupPropertyToProperty,
} from '@entity/extractors-property/property-helpers';
import type { SoupProperty } from '@service-storage/generated/schemas/soupProperty';
import { match } from 'ts-pattern';

/**
 * Soup attaches notifications per `toNotificationEntity`, which maps a
 * `channel_thread` to its whole channel — so its notifications come back
 * channel-wide. Scope them to this thread's own message: the sends/mentions for
 * the message, and replies whose `threadId` is the message.
 */
export function scopeThreadNotifications(
  entity: WithNotification<EntityData>
): WithNotification<EntityData> {
  if (entity.type !== 'channel_thread') return entity;

  const notifications = entity.notifications;
  if (!notifications) return entity;

  const messageId = entity.messageId;
  return {
    ...entity,
    notifications: () =>
      notifications().filter((notification) =>
        match(notification.notification_metadata)
          .with(
            { tag: 'channel_message_send' },
            (m) => m.content.messageId === messageId
          )
          .with(
            { tag: 'channel_mention' },
            (m) => m.content.messageId === messageId
          )
          .with(
            { tag: 'channel_message_reaction' },
            (m) =>
              m.content.messageId === messageId ||
              m.content.threadId === messageId
          )
          .with(
            { tag: 'channel_message_reply' },
            (m) => m.content.threadId === messageId
          )
          .otherwise(() => false)
      ),
  };
}

/** Which metadata field carries the preview text, per notification kind. */
const NOTIFICATION_CONTENT_FIELD: Partial<
  Record<Notification['notification_metadata']['tag'], string>
> = {
  channel_mention: 'messageContent',
  channel_message_send: 'messageContent',
  channel_message_reaction: 'messageContent',
  channel_message_reply: 'messageContent',
  mentioned_in_document_comment: 'text',
  replied_to_document_comment_thread: 'text',
  commented_on_document: 'text',
  new_email: 'snippet',
  ai_response: 'summary',
  github_pr_comment: 'commentSnippet',
  github_pr_mention: 'textSnippet',
  github_pr_review: 'reviewSnippet',
  agent_session_settled: 'excerpt',
  agent_session_waiting_for_input: 'question',
};

function notificationContent(notification: Notification): string | undefined {
  const metadata = notification.notification_metadata;
  if (metadata.tag === 'entity_access_requested')
    return `Requested ${metadata.content.accessLevel} access`;
  if (metadata.tag === 'entity_access_granted')
    return `Granted you ${metadata.content.accessLevel} access`;
  const field =
    NOTIFICATION_CONTENT_FIELD[notification.notification_metadata.tag];
  if (!field) return undefined;
  const content = notification.notification_metadata.content as
    | Record<string, unknown>
    | undefined;
  const value = content?.[field];
  return typeof value === 'string' && value ? value : undefined;
}

export function getNotificationTag(notification?: Notification) {
  return notification?.notification_metadata.tag;
}

const channelMessageContent = (entity: EntityData): string | undefined => {
  if (entity.type === 'channel') return entity.latestRootMessage?.content;
  if (entity.type === 'channel_message' || entity.type === 'channel_thread') {
    return entity.content;
  }
  return undefined;
};

export function itemContent(
  entity: EntityData,
  notification?: Notification
): string | undefined {
  const meta = notification?.notification_metadata;

  if (meta?.tag === 'document_mention') {
    return meta.content.messageContent;
  }

  // The action line says who asked to join; the channel's latest message
  // would read as the request's content.
  if (
    meta?.tag === 'call_started' ||
    meta?.tag === 'channel_access_requested'
  ) {
    return;
  }

  const channel = channelMessageContent(entity);
  if (channel) return channel;
  return notification ? notificationContent(notification) : undefined;
}

export function getGithubLocationLabel(
  entity: EntityData,
  notification?: Notification
) {
  if (
    entity.type === 'foreign' &&
    entity.foreignSource === 'github_pull_request'
  ) {
    const { owner, repo, number } = entity.metadata;
    return `${owner}/${repo}#${number}`;
  }
  const content = notification?.notification_metadata.content as
    | { owner?: string; repo?: string; number?: number }
    | undefined;
  if (!content?.owner || !content.repo || content.number == null) {
    return undefined;
  }
  return `${content.owner}/${content.repo}#${content.number}`;
}

export function getGithubTitle(
  entity: EntityData,
  notification?: Notification
) {
  if (
    entity.type === 'foreign' &&
    entity.foreignSource === 'github_pull_request'
  ) {
    return entity.metadata.name;
  }
  const content = notification?.notification_metadata.content as
    | { title?: string }
    | undefined;

  return content?.title;
}

export function getEmailSubject(notification?: Notification) {
  const content = notification?.notification_metadata.content as
    | { subject?: string }
    | undefined;
  return content?.subject;
}

/** Key task properties (pills), derived from the document entity. */
export function getInboxTaskProperties(entity: EntityData) {
  if (entity.type !== 'document') return undefined;
  if (!('properties' in entity) || !entity.properties?.length) return undefined;

  const keyProperties = getSortedKeyProperties(
    entity.properties.map((property: SoupProperty) =>
      soupPropertyToProperty(property)
    )
  );
  return keyProperties.length ? keyProperties : undefined;
}

export function getFirstName(value: string) {
  const name = value.includes('@') ? value.split('@')[0] : value;
  return name.split(/[\s._-]+/).filter(Boolean)[0] ?? name;
}