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 { MessageData } from '@core/messages/types';
export type GroupableMessage = Pick<
MessageData,
'id' | 'sender_id' | 'sender' | 'created_at' | 'attachments' | 'deleted_at'
> & {
thread?: { reply_count: number };
};
export const MESSAGE_GROUPING_WINDOW_MS = 5 * 60 * 1000;
function isDeleted(message: Pick<GroupableMessage, 'deleted_at'>): boolean {
return message.deleted_at != null;
}
function hasThreadReplies(message: GroupableMessage): boolean {
return (message.thread?.reply_count ?? 0) > 0;
}
export function shouldGroupWithPreviousMessage(
current: GroupableMessage,
previous: GroupableMessage | undefined
): boolean {
if (!previous) return false;
if (current.sender_id !== previous.sender_id) return false;
// Agent messages share the Macro bot's sender_id but carry the triggering
// user in `sender.triggered_by`; messages prompted by different users must
// not merge under a single "from" pill.
// Optimistic sends omit attribution; server messages can return null.
// Both represent the same absence and must group before acknowledgement.
if (
(current.sender?.triggered_by ?? null) !==
(previous.sender?.triggered_by ?? null)
) {
return false;
}
if (isDeleted(current) || isDeleted(previous)) return false;
if (hasThreadReplies(previous)) return false;
const currentCreatedAt = new Date(current.created_at).getTime();
const previousCreatedAt = new Date(previous.created_at).getTime();
const timeGap = currentCreatedAt - previousCreatedAt;
return timeGap >= 0 && timeGap <= MESSAGE_GROUPING_WINDOW_MS;
}