import type { HistoryEntry, Session } from '../api'; export type AgentBubbleRole = 'subagent' | 'agent'; type MailUserBlock = { mail?: { senderId: string | null; external: boolean; answer: boolean; } | null; turnType?: HistoryEntry['turn_type']; }; /** * Classifies a `user` block for chat bubble placement and tinting: null for * the person or non-agent triggers, `'subagent'` for a descendant of this * session, or `'agent'` for a sibling, parent, or other agent. */ export function agentBubbleRole( block: MailUserBlock, sessionId: string, knownSessions: ReadonlyMap, ): AgentBubbleRole | null { const mail = block.mail; if (mail?.external) return null; if (!mail && block.turnType !== 'agent') return null; if (mail?.answer) return 'subagent'; const senderId = mail?.senderId; if (!senderId) return 'agent'; const sender = knownSessions.get(senderId); if (!sender) return 'agent'; const seen = new Set([sender.id]); let parentId = sender.parent_id; while (parentId) { if (parentId === sessionId) return 'subagent'; if (seen.has(parentId)) break; seen.add(parentId); parentId = knownSessions.get(parentId)?.parent_id ?? null; } return 'agent'; }