Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
import type { EntityData } from '@entity/types/entity';
import type { EntityType } from '@service-properties/generated/schemas/entityType';
import type { UnifiedSearchResponseItem } from '@service-search/generated/models';
import type { SoupApiItem } from '@service-storage/generated/schemas/soupApiItem';
import type {
EntityRefInput,
GraphqlEntityFilterAst,
SoupInput,
} from '@service-storage/graphql/generated/graphql';
import { match } from 'ts-pattern';
const NIL_ENTITY_ID = '00000000-0000-0000-0000-000000000000';
type OrExpr<T> = T | { or: { left: OrExpr<T>; right: OrExpr<T> } };
/** A Soup entity with a known identity, independent of list membership. */
export type SoupEntityRef = {
entityType: Exclude<EntityType, 'USER' | 'CONTACT'>;
entityId: string;
};
/** Match the normalized identity used by Soup records, including task documents. */
export function soupEntityRefKey(entity: SoupEntityRef): string | undefined {
const typename = match(entity.entityType)
.with('DOCUMENT', 'TASK', () => 'GraphqlSoupDocument')
.with('PROJECT', () => 'GraphqlSoupProject')
.with('CHAT', () => 'GraphqlSoupChat')
.with('THREAD', () => 'GraphqlSoupEmailThread')
.with('CHANNEL', () => 'GraphqlSoupChannel')
.with('CALL_RECORD', () => 'GraphqlSoupCall')
.with('COMPANY', () => 'GraphqlSoupCrmCompany')
.with('CALENDAR_EVENT', () => 'GraphqlSoupCalendarEvent')
.with('INITIATIVE', () => 'GraphqlSoupInitiative')
.with('DATABASE_ROW', () => 'GraphqlSoupDatabaseRow')
.exhaustive(() => undefined);
return typename ? `${typename}:${entity.entityId}` : undefined;
}
/** Normalized identity for display entities and unified mutation targets. */
export function soupEntityKey(entity: {
type:
| EntityData['type']
| EntityRefInput['type']
| SoupApiItem['tag']
| UnifiedSearchResponseItem['type'];
id: string | number;
}): string | undefined {
const typename = match(entity.type)
.with('document', 'DOCUMENT', () => 'GraphqlSoupDocument')
.with('chat', 'CHAT', () => 'GraphqlSoupChat')
.with('project', 'PROJECT', () => 'GraphqlSoupProject')
.with('channel', 'CHANNEL', () => 'GraphqlSoupChannel')
.with(
'email',
'emailThread',
'EMAIL_THREAD',
() => 'GraphqlSoupEmailThread'
)
.with('call', 'CALL', () => 'GraphqlSoupCall')
.with('agent_session', 'agentSession', () => 'GraphqlSoupAgentSession')
.with(
'crm_company',
'crmCompany',
'company',
'CRM_COMPANY',
() => 'GraphqlSoupCrmCompany'
)
.with('calendar_event', 'calendarEvent', () => 'GraphqlSoupCalendarEvent')
.with('initiative', 'INITIATIVE', () => 'GraphqlSoupInitiative')
.otherwise(() => undefined);
return typename ? `${typename}:${entity.id}` : undefined;
}
// Balanced trees stay below the GraphQL filter ingress depth limit even for
// a full preview batch. A left-folded OR tree does not.
function or<T>(items: T[]): OrExpr<T> | undefined {
if (items.length < 2) return items[0];
const middle = Math.floor(items.length / 2);
return {
or: { left: or(items.slice(0, middle))!, right: or(items.slice(middle))! },
};
}
/** Exact multi-entity lookup, with every non-target entity branch excluded. */
export function buildGraphqlEntitiesSoupInput(
entities: ReadonlyArray<{ entityType: EntityType; entityId: string }>
): SoupInput | undefined {
const ids = (...types: EntityType[]) =>
[
...new Set(
entities
.filter((e) => types.includes(e.entityType))
.map((e) => e.entityId)
),
].sort();
const input = buildGraphqlEntitySoupInput('DOCUMENT', NIL_ENTITY_ID);
if (!input || !('initial' in input) || !input.initial) return undefined;
const base = input.initial.filters!;
const initiativeFilter = or(
ids('INITIATIVE').map((id) => ({ literal: { id } }))
);
const databaseRowFilter = or(
ids('DATABASE_ROW').map((id) => ({ literal: { id } }))
);
const filters: GraphqlEntityFilterAst = {
...base,
...(databaseRowFilter ? { databaseRowFilter } : {}),
documentFilter:
or(ids('DOCUMENT', 'TASK').map((id) => ({ literal: { id } }))) ??
base.documentFilter,
...(initiativeFilter ? { initiativeFilter } : {}),
projectFilter:
or(
ids('PROJECT').map((projectIdSelf) => ({ literal: { projectIdSelf } }))
) ?? base.projectFilter,
chatFilter:
or(ids('CHAT').map((chatId) => ({ literal: { chatId } }))) ??
base.chatFilter,
emailFilter: {
tree:
or(ids('THREAD').map((threadId) => ({ literal: { threadId } }))) ??
base.emailFilter!.tree,
},
channelFilter:
or(ids('CHANNEL').map((channelId) => ({ literal: { channelId } }))) ??
base.channelFilter,
callFilter:
or(ids('CALL_RECORD').map((callId) => ({ literal: { callId } }))) ??
base.callFilter,
crmCompanyFilter:
or(ids('COMPANY').map((id) => ({ literal: { id } }))) ??
base.crmCompanyFilter,
calendarEventFilter:
or(ids('CALENDAR_EVENT').map((id) => ({ literal: { id } }))) ??
base.calendarEventFilter,
};
const count = new Set(
entities
// Users and CRM contacts are not Soup items.
.filter((e) => e.entityType !== 'USER' && e.entityType !== 'CONTACT')
.map(
(e) =>
`${e.entityType === 'TASK' ? 'DOCUMENT' : e.entityType}:${e.entityId}`
)
).size;
if (!count) return undefined;
return { initial: { ...input.initial, limit: count, filters } };
}
/** Builds an exact single-entity Soup query with every non-target branch excluded. */
export function buildGraphqlEntitySoupInput(
entityType: EntityType,
entityId: string
): SoupInput | undefined {
const targetFilter: Partial<GraphqlEntityFilterAst> | undefined = match(
entityType
)
.with('DOCUMENT', 'TASK', () => ({
documentFilter: { literal: { id: entityId } },
}))
.with('PROJECT', () => ({
projectFilter: { literal: { projectIdSelf: entityId } },
}))
.with('CHAT', () => ({
chatFilter: { literal: { chatId: entityId } },
}))
.with('THREAD', () => ({
emailFilter: { tree: { literal: { threadId: entityId } } },
}))
.with('CHANNEL', () => ({
channelFilter: { literal: { channelId: entityId } },
}))
.with('CALL_RECORD', () => ({
callFilter: { literal: { callId: entityId } },
}))
.with('COMPANY', () => ({
crmCompanyFilter: { literal: { id: entityId } },
}))
.with('CALENDAR_EVENT', () => ({
calendarEventFilter: { literal: { id: entityId } },
}))
.with('INITIATIVE', () => ({
initiativeFilter: { literal: { id: entityId } },
}))
.with('DATABASE_ROW', () => ({
databaseRowFilter: { literal: { id: entityId } },
}))
// Users and CRM contacts are not Soup items.
.with('USER', 'CONTACT', () => undefined)
.exhaustive(() => undefined);
if (!targetFilter) return undefined;
const filters: GraphqlEntityFilterAst = {
calendarEventFilter: { literal: { id: NIL_ENTITY_ID } },
documentFilter: { literal: { id: NIL_ENTITY_ID } },
projectFilter: { literal: { projectIdSelf: NIL_ENTITY_ID } },
chatFilter: { literal: { chatId: NIL_ENTITY_ID } },
emailFilter: {
tree: { literal: { threadId: NIL_ENTITY_ID } },
},
channelFilter: { literal: { channelId: NIL_ENTITY_ID } },
channelThreadFilter: { literal: { threadId: NIL_ENTITY_ID } },
callFilter: { literal: { callId: NIL_ENTITY_ID } },
crmCompanyFilter: { literal: { id: NIL_ENTITY_ID } },
foreignEntityFilter: { literal: { id: NIL_ENTITY_ID } },
...targetFilter,
};
return {
initial: {
limit: 1,
expand: true,
sortMethod: 'UPDATED_AT',
emailView: 'ALL',
filters,
},
};
}