{"version":3,"file":"timelineGroups.mjs","names":[],"sources":["../../../src/web/lib/timelineGroups.ts"],"sourcesContent":["import type { StatusTone } from '@agimon-ai/po-web-components';\n\nimport type { TimelineEntry, ToolEntry } from './sessionModel';\n\n/**\n * Runs of the same tool, gathered so the transcript draws one frame instead of\n * five.\n *\n * An agent rarely calls a tool once: it reads four files, or runs five\n * commands, and each call arrives as its own entry. Rendered one card apiece\n * that reads as repetition rather than as progress, so adjacent calls of the\n * same tool are handed to the timeline as a single unit. Grouping is decided\n * here, on the entry list alone, so it can be tested without a browser and the\n * component stays a renderer.\n */\n\n/** Two calls are the fewest that can read as a repetition, so a lone call keeps its card. */\nconst MIN_GROUP = 2;\n\nexport interface SingleUnit {\n  readonly kind: 'single';\n  readonly entry: TimelineEntry;\n  /** The entry's index in the source list, for the timeline's live-tail window. */\n  readonly index: number;\n}\n\nexport interface GroupUnit {\n  readonly kind: 'group';\n  readonly name: string;\n  readonly entries: readonly ToolEntry[];\n  /** The index of the last entry in the run, so a group is skipped only once it is fully above the tail. */\n  readonly index: number;\n}\n\nexport type TimelineUnit = SingleUnit | GroupUnit;\n\nfunction isTool(entry: TimelineEntry | undefined): entry is ToolEntry {\n  return entry?.kind === 'tool';\n}\n\n/**\n * The entries as units to render.\n *\n * `groupable` decides which tool names may be gathered: a tool whose renderer\n * presents itself as a message, not a card, has no frame to share and stays on\n * its own. Only adjacent entries group, so anything between two calls, an\n * assistant reply or a notice, ends the run and keeps the transcript in order.\n */\nexport function timelineUnits(\n  entries: readonly TimelineEntry[],\n  groupable: (name: string) => boolean,\n): readonly TimelineUnit[] {\n  const units: TimelineUnit[] = [];\n  let index = 0;\n  while (index < entries.length) {\n    const entry = entries[index];\n    if (entry === undefined) break;\n    if (!isTool(entry) || !groupable(entry.name)) {\n      units.push({ kind: 'single', entry, index });\n      index += 1;\n      continue;\n    }\n    const run: ToolEntry[] = [entry];\n    let next = index + 1;\n    while (next < entries.length) {\n      const candidate = entries[next];\n      if (!isTool(candidate) || candidate.name !== entry.name) break;\n      run.push(candidate);\n      next += 1;\n    }\n    if (run.length < MIN_GROUP) units.push({ kind: 'single', entry, index });\n    else units.push({ kind: 'group', name: entry.name, entries: run, index: next - 1 });\n    index = next;\n  }\n  return units;\n}\n\n/** The group's status follows its final call; earlier failures stay on their own rows. */\nexport function groupTone(entries: readonly ToolEntry[]): StatusTone {\n  const last = entries.at(-1);\n  return last?.running ? 'running' : last?.isError ? 'error' : 'ok';\n}\n\n/** `5 calls`, the line beside the tool's name in the group header. */\nexport function groupSummary(entries: readonly ToolEntry[]): string {\n  return `${String(entries.length)} call${entries.length === 1 ? '' : 's'}`;\n}\n"],"mappings":";;;;;;;;;;;;;AAiBA,MAAM,YAAY;AAmBlB,SAAS,OAAO,OAAsD;CACpE,OAAO,OAAO,SAAS;AACzB;;;;;;;;;AAUA,SAAgB,cACd,SACA,WACyB;CACzB,MAAM,QAAwB,CAAC;CAC/B,IAAI,QAAQ;CACZ,OAAO,QAAQ,QAAQ,QAAQ;EAC7B,MAAM,QAAQ,QAAQ;EACtB,IAAI,UAAU,KAAA,GAAW;EACzB,IAAI,CAAC,OAAO,KAAK,KAAK,CAAC,UAAU,MAAM,IAAI,GAAG;GAC5C,MAAM,KAAK;IAAE,MAAM;IAAU;IAAO;GAAM,CAAC;GAC3C,SAAS;GACT;EACF;EACA,MAAM,MAAmB,CAAC,KAAK;EAC/B,IAAI,OAAO,QAAQ;EACnB,OAAO,OAAO,QAAQ,QAAQ;GAC5B,MAAM,YAAY,QAAQ;GAC1B,IAAI,CAAC,OAAO,SAAS,KAAK,UAAU,SAAS,MAAM,MAAM;GACzD,IAAI,KAAK,SAAS;GAClB,QAAQ;EACV;EACA,IAAI,IAAI,SAAS,WAAW,MAAM,KAAK;GAAE,MAAM;GAAU;GAAO;EAAM,CAAC;OAClE,MAAM,KAAK;GAAE,MAAM;GAAS,MAAM,MAAM;GAAM,SAAS;GAAK,OAAO,OAAO;EAAE,CAAC;EAClF,QAAQ;CACV;CACA,OAAO;AACT;;AAGA,SAAgB,UAAU,SAA2C;CACnE,MAAM,OAAO,QAAQ,GAAG,EAAE;CAC1B,OAAO,MAAM,UAAU,YAAY,MAAM,UAAU,UAAU;AAC/D;;AAGA,SAAgB,aAAa,SAAuC;CAClE,OAAO,GAAG,OAAO,QAAQ,MAAM,EAAE,OAAO,QAAQ,WAAW,IAAI,KAAK;AACtE"}