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.

4.0 KB
Raw Blame
import type {
  AgentSessionEntity,
  ChatEntity,
  EntityData,
  WithNotification,
} from '@entity';
import { isSessionArchived } from '@service-agent-harness/session-archive';
import { type AgentKind, kindForHarness, modeForKind } from './agent-kind';
import type { AgentsMode } from './mode';

export type AgentConversationEntity = WithNotification<
  AgentSessionEntity | ChatEntity
>;

/** The persona a conversation runs, when the entity says. Chats have none. */
export function conversationBotId(
  conversation: AgentConversationEntity
): string | undefined {
  if (conversation.type !== 'agent_session') return undefined;
  return conversation.bot?.id ?? conversation.botId;
}
export type AgentConversationTarget = Pick<
  AgentConversationEntity,
  'id' | 'type'
>;

function isAgentConversation(
  entity: EntityData
): entity is AgentConversationEntity {
  return entity.type === 'agent_session' || entity.type === 'chat';
}

/** When a conversation last had user-visible work, as epoch millis; 0 when unknown. */
export function conversationTimestamp(
  entity: Pick<AgentConversationEntity, 'updatedAt' | 'createdAt'>
): number {
  const value = entity.updatedAt ?? entity.createdAt;
  if (!value) return 0;

  const parsed = new Date(value).getTime();
  return Number.isNaN(parsed) ? 0 : parsed;
}

/** Select the current user's agent conversations, newest first. */
export function selectRecentAgentConversations(
  entities: EntityData[],
  ownerId: string | undefined,
  search: string
): AgentConversationEntity[] {
  if (!ownerId) return [];

  const query = search.trim().toLocaleLowerCase();

  return entities
    .filter(isAgentConversation)
    .filter((entity) => entity.ownerId === ownerId)
    .filter(
      (entity) =>
        !query ||
        (
          entity.name ||
          (entity.type === 'agent_session'
            ? 'Untitled conversation'
            : 'Untitled chat')
        )
          .toLocaleLowerCase()
          .includes(query)
    )
    .toSorted(
      (left, right) =>
        conversationTimestamp(right) - conversationTimestamp(left)
    );
}

/** Names the bot behind a session. Plain chats are always Macro's. */
export type ConversationKindResolver = (botId: string | undefined) => AgentKind;

/** Resolve navigation from the conversation itself, independent of composer mode. */
export function conversationMode(
  conversation: AgentConversationEntity,
  kindOf: ConversationKindResolver
): AgentsMode {
  return conversation.type === 'chat'
    ? 'chat'
    : modeForKind(
        conversation.harness
          ? kindForHarness(conversation.harness)
          : kindOf(conversationBotId(conversation))
      );
}

export type PartitionedConversations = {
  conversations: AgentConversationEntity[];
  archived: AgentConversationEntity[];
};

const isArchived = (conversation: AgentConversationEntity) =>
  conversation.type === 'agent_session' && isSessionArchived(conversation);

/** Splits archived agent sessions out, keeping the query's newest-first order. */
export function partitionArchived(
  conversations: readonly AgentConversationEntity[]
): PartitionedConversations {
  return {
    conversations: conversations.filter(
      (conversation) => !isArchived(conversation)
    ),
    archived: conversations.filter(isArchived),
  };
}

export type BotUsage = {
  /** Sessions in the loaded list that ran as this bot. */
  sessions: number;
  /** When the newest of them last moved, as epoch millis. */
  lastUsedAt: number;
};

/** Per-bot usage from the loaded sessions, for the coder cards' footers. */
export function botUsage(
  conversations: readonly AgentConversationEntity[]
): Map<string, BotUsage> {
  const usage = new Map<string, BotUsage>();
  for (const conversation of conversations) {
    const botId = conversationBotId(conversation);
    if (!botId) continue;
    const current = usage.get(botId) ?? { sessions: 0, lastUsedAt: 0 };
    usage.set(botId, {
      sessions: current.sessions + 1,
      lastUsedAt: Math.max(
        current.lastUsedAt,
        conversationTimestamp(conversation)
      ),
    });
  }
  return usage;
}