{"version":3,"file":"sessionSummary.cjs","names":["t","clockText"],"sources":["../../../src/web/lib/sessionSummary.ts"],"sourcesContent":["import { t } from '@agimon-ai/po-core/web';\nimport { clockText } from '@agimon-ai/po-web-components/text';\n\nimport type { SessionPhase } from '../../types/hub';\n\n/** The rail's view of one session's attach state; offline means the page lost the hub. */\nexport type AttachPhase = 'offline' | 'connecting' | 'attached' | 'refused' | 'detached' | 'closed';\n\nexport interface StatusLineInput {\n  attach: AttachPhase;\n  phase: SessionPhase;\n  /** ISO 8601 timestamp of the last phase change. */\n  phaseSince: string;\n  awaitingInput: boolean;\n  everPrompted: boolean;\n  backgroundWorkActive?: boolean | null;\n  /** Set once any run finished; a settled session is not \"fresh\" even unprompted. */\n  lastSettledAt?: string;\n  /** Recorded by the hub but not running; opening it is what starts it. */\n  dormant?: boolean;\n  /** A line an extension published about background work it runs for the session. */\n  activity?: { label: string; since?: string };\n  /** Tool calls waiting for the person's approval. */\n  pendingApprovals?: number;\n}\n\n/** The ageing part of a status line: \"<label> · <elapsed since>\". */\nexport interface StatusClock {\n  label: string;\n  since: number;\n}\n\n/**\n * The clock a status line shows, or undefined when its copy does not age.\n *\n * It follows sessionStatusLine's priority, so a refused, dormant, or asking\n * session never shows a running clock.\n */\nexport function sessionStatusClock(input: StatusLineInput): StatusClock | undefined {\n  if (input.attach === 'refused' || input.dormant === true || input.awaitingInput || (input.pendingApprovals ?? 0) > 0)\n    return undefined;\n  if (input.phase !== 'idle') {\n    const since = Date.parse(input.phaseSince);\n    return Number.isFinite(since) ? { label: t('webDashboard.status.running', 'running'), since } : undefined;\n  }\n  if (input.backgroundWorkActive === true || input.activity?.since === undefined) return undefined;\n  const since = Date.parse(input.activity.since);\n  return Number.isFinite(since) ? { label: input.activity.label, since } : undefined;\n}\n\n/**\n * The one mapping from session facts to the rail's status copy.\n *\n * Priority order: a refusal outranks everything because nothing else the card\n * says can be trusted while another client holds the session; a question to\n * the user outranks \"running\" because the run is blocked on them.\n */\nexport function sessionStatusLine(input: StatusLineInput, now: number): string {\n  if (input.attach === 'refused') return t('webDashboard.status.anotherCockpit', 'another cockpit holds this session');\n  if (input.awaitingInput && input.dormant !== true)\n    return t('webDashboard.status.waitingForInput', 'waiting for your input');\n  // Below a question, which blocks the run, and above the clock: the agent keeps working meanwhile.\n  const approvals = input.pendingApprovals ?? 0;\n  if (approvals > 0)\n    return t('webDashboard.status.waitingForApproval', 'waiting for approval · {count}', { count: approvals });\n  // Durable approvals remain actionable while phase fields are only dormant defaults.\n  if (input.dormant === true) return t('webDashboard.status.stoppedOpenToWake', 'stopped · open to wake');\n  const clock = sessionStatusClock(input);\n  if (clock !== undefined) return clockText(clock, now);\n  if (input.phase !== 'idle') return t('webDashboard.status.runningUnderMinute', 'running · <1m');\n  if (input.backgroundWorkActive === true) return t('webDashboard.status.running', 'running');\n  // Background work outranks the idle copy: an agent waiting on a workflow is not \"done\".\n  if (input.activity !== undefined) return input.activity.label;\n  if (input.backgroundWorkActive === null) return t('webDashboard.status.statusUnavailable', 'status unavailable');\n  // Legacy history can be unavailable: false is not proof that nothing was sent.\n  if (!input.everPrompted && input.lastSettledAt === undefined)\n    return t('webDashboard.status.readyWhenYouAre', 'ready when you are');\n  return t('webDashboard.status.doneWaiting', 'done · waiting for you');\n}\n\n/** Sessions counted as running by the rail header and the top bar chip. */\nexport function runningCount(phases: Iterable<SessionPhase>): number {\n  let count = 0;\n  for (const phase of phases) if (phase !== 'idle') count += 1;\n  return count;\n}\n\n/** Two letters standing in for a persona avatar: the first letters of two words, or one word's first two. */\nexport function personaInitials(name: string): string {\n  const words = name.trim().split(/\\s+/u).filter(Boolean);\n  const letters = words.length > 1 ? `${words[0]?.[0] ?? ''}${words[1]?.[0] ?? ''}` : (words[0]?.slice(0, 2) ?? '');\n  return letters.toUpperCase() || 'DP';\n}\n\n/** Shortens a home-rooted cwd the way a shell prompt would. */\nexport function abbreviateCwd(cwd: string): string {\n  const match = /^\\/(?:Users|home)\\/[^/]+/u.exec(cwd);\n  if (!match) return cwd;\n  return `~${cwd.slice(match[0].length)}`;\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n  return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\n/** Summaries are JSON wire values; object key insertion order is not meaningful. */\nexport function summaryValueEqual(left: unknown, right: unknown): boolean {\n  if (left === right) return true;\n  if (Array.isArray(left) || Array.isArray(right))\n    return (\n      Array.isArray(left) &&\n      Array.isArray(right) &&\n      left.length === right.length &&\n      left.every((value, index) => summaryValueEqual(value, right[index]))\n    );\n  if (!isRecord(left) || !isRecord(right)) return false;\n  const keys = Object.keys(left);\n  return (\n    keys.length === Object.keys(right).length &&\n    keys.every((key) => Object.hasOwn(right, key) && summaryValueEqual(left[key], right[key]))\n  );\n}\n"],"mappings":";;;;;;;;;AAsCA,SAAgB,mBAAmB,OAAiD;CAClF,IAAI,MAAM,WAAW,aAAa,MAAM,YAAY,QAAQ,MAAM,kBAAkB,MAAM,oBAAoB,KAAK,GACjH,OAAO,KAAA;CACT,IAAI,MAAM,UAAU,QAAQ;EAC1B,MAAM,QAAQ,KAAK,MAAM,MAAM,UAAU;EACzC,OAAO,OAAO,SAAS,KAAK,IAAI;GAAE,QAAA,GAAOA,uBAAAA,EAAAA,CAAE,+BAA+B,SAAS;GAAG;EAAM,IAAI,KAAA;CAClG;CACA,IAAI,MAAM,yBAAyB,QAAQ,MAAM,UAAU,UAAU,KAAA,GAAW,OAAO,KAAA;CACvF,MAAM,QAAQ,KAAK,MAAM,MAAM,SAAS,KAAK;CAC7C,OAAO,OAAO,SAAS,KAAK,IAAI;EAAE,OAAO,MAAM,SAAS;EAAO;CAAM,IAAI,KAAA;AAC3E;;;;;;;;AASA,SAAgB,kBAAkB,OAAwB,KAAqB;CAC7E,IAAI,MAAM,WAAW,WAAW,QAAA,GAAOA,uBAAAA,EAAAA,CAAE,sCAAsC,oCAAoC;CACnH,IAAI,MAAM,iBAAiB,MAAM,YAAY,MAC3C,QAAA,GAAOA,uBAAAA,EAAAA,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