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.4 KB
Raw Blame
import { createFreshSearch, NAME_MATCH_RANKING } from '@core/util/freshSort';
import { match } from 'ts-pattern';
import type { CommandMenuItem } from './useCommandItems';

/** The label a row shows; its search text can add emails, aliases, or command keywords. */
function displayName(item: CommandMenuItem): string {
  return match(item)
    .with({ kind: 'entity' }, { kind: 'user' }, (row) => row.data.name)
    .with({ kind: 'command' }, ({ data }) =>
      typeof data.description === 'function'
        ? data.description()
        : data.description
    )
    .with(
      { kind: 'search' },
      { kind: 'ask-ai' },
      { kind: 'new-project' },
      (row) => row.searchText
    )
    .exhaustive();
}

function compareRecency(left: CommandMenuItem, right: CommandMenuItem) {
  return (
    right.sortTimestamp - left.sortTimestamp || left.id.localeCompare(right.id)
  );
}

function isPeopleItem(item: CommandMenuItem) {
  return (
    item.bucket === 'dm' ||
    item.bucket === 'person' ||
    item.bucket === 'crm_contact'
  );
}

/** Keep the existing order within each group and the positions of other results. */
function prioritizeInternalPeople<T extends CommandMenuItem>(items: T[]): T[] {
  const people = items
    .filter(isPeopleItem)
    .sort(
      (left, right) =>
        Number(left.bucket === 'crm_contact') -
        Number(right.bucket === 'crm_contact')
    );
  let nextPerson = 0;
  return items.map((item) =>
    isPeopleItem(item) ? people[nextPerson++] : item
  );
}

/** Rank cached and local candidates together using the command menu's rules. */
export function rankCommandSearchItems<T extends CommandMenuItem>(
  items: T[],
  query: string,
  options: {
    preserveAdditionalEntityMatches: boolean;
    /** Rows a server search already matched, kept even when the menu's
     * fuzzy matcher would not match them. */
    preservedIds?: ReadonlySet<string>;
  }
): T[] {
  const search = createFreshSearch<T>({
    config: {
      ...NAME_MATCH_RANKING,
      useViewedAt: true,
      channelBoost: 3,
      dmBoost: 1.8,
      fuzzyWeight: 0.7,
      timeWeight: 0.7,
      minFuzzyThreshold: 0.1,
      commaSeparatedChannelMatch: true,
    },
    getName: (item) => item.searchText,
    getDisplayName: displayName,
    isChannelItem: (item) => item.bucket === 'channel' || item.bucket === 'dm',
    isDmItem: (item) => item.bucket === 'dm',
    isPersonItem: (item) => item.bucket === 'dm' || item.bucket === 'person',
    getTimestamp: (item) => item.timestamps,
  });

  const ranked = search(items, query)
    .sort(
      (left, right) =>
        right.nameMatchRank - left.nameMatchRank ||
        right.combinedScore - left.combinedScore ||
        compareRecency(left.item, right.item)
    )
    .map(({ item }) => item);
  const preserved = (item: CommandMenuItem) =>
    options.preservedIds?.has(item.id) ||
    (options.preserveAdditionalEntityMatches && item.kind === 'entity');
  if (!options.preserveAdditionalEntityMatches && !options.preservedIds?.size)
    return prioritizeInternalPeople(ranked);

  // Cache search accepts broader subsequences than the menu's fuzzy matcher.
  // Keep these additional matches reachable after the regular ranked results.
  const matchedIds = new Set(ranked.map((item) => item.id));
  const additional = items
    .filter((item) => preserved(item) && !matchedIds.has(item.id))
    .sort(compareRecency);
  return prioritizeInternalPeople([...ranked, ...additional]);
}