{"version":3,"file":"contextComposition.cjs","names":["parseSelection","resolveModes"],"sources":["../../../src/web/lib/contextComposition.ts"],"sourcesContent":["import type { ContextProjection, ModeProjection } from '../../types/hub';\nimport { resolveModes } from './composition';\nimport { parseSelection } from './statusLine';\n\n/**\n * What the session is composed of, as opposed to what it is doing.\n *\n * The activity dock answers \"what is running\"; this answers \"what is loaded and\n * what does carrying it cost\". A group is a mode, because a mode is the thing a\n * reader can actually switch off: grouping by source would name the mechanism\n * rather than the decision. Rows inside a group are ordered by source kind so\n * the session's own extensions read before anything a server or plugin added.\n */\nexport type ContextGroupKind = 'preset' | 'mode' | 'domain' | 'shared' | 'core';\n\nexport type ContextItemSource = 'extension' | 'mcp' | 'plugin' | 'core';\n\n/** Extensions first: they are the session's own, and the eye starts there. */\nconst SOURCE_ORDER: Record<ContextItemSource, number> = { extension: 0, mcp: 1, plugin: 2, core: 3 };\n\nconst KIND_ORDER: Record<ContextGroupKind, number> = { preset: 0, mode: 1, domain: 2, shared: 3, core: 4 };\n\nexport interface ContextItem {\n  name: string;\n  itemKind: 'tool' | 'skill';\n  source: ContextItemSource;\n  /** The package or server that registered it. */\n  owner: string;\n  /** Estimated tokens, or null while the runtime has not reported a figure. */\n  tokens: number | null;\n  active: boolean;\n}\n\nexport interface ContextGroup {\n  id: string;\n  label: string;\n  kind: ContextGroupKind;\n  /** A short qualifier shown beside the label, e.g. a pending switch. */\n  detail: string;\n  items: ContextItem[];\n  /** Subtotal across active items, or null when no item carries a figure. */\n  tokens: number | null;\n  /** What the group's gated items would add if switched on. */\n  inactiveTokens: number;\n}\n\nfunction bySource(left: ContextItem, right: ContextItem): number {\n  return SOURCE_ORDER[left.source] - SOURCE_ORDER[right.source] || left.name.localeCompare(right.name);\n}\n\n/** A group with nothing priced reports null rather than a confident zero. */\nfunction subtotal(items: readonly ContextItem[]): number | null {\n  const priced = items.filter((item) => item.tokens !== null);\n  return priced.length === 0 ? null : priced.reduce((total, item) => total + (item.tokens ?? 0), 0);\n}\n\nfunction group(id: string, label: string, kind: ContextGroupKind, detail: string, items: ContextItem[]): ContextGroup {\n  const sorted = [...items].sort(bySource);\n  return { id, label, kind, detail, items: sorted, tokens: subtotal(sorted), inactiveTokens: 0 };\n}\n\n/**\n * The composition the session is running under.\n *\n * Until the runtime publishes its inventory every group is empty, which is the\n * honest reading: the modes are known from the footer the session already\n * publishes, the tools they brought are not.\n */\nexport function contextGroups(\n  statuses: Record<string, string>,\n  widgets: readonly string[],\n  projection: ModeProjection | null = null,\n  items: readonly ContextItem[] = [],\n  attribution: Readonly<Record<string, string>> = {},\n): ContextGroup[] {\n  const selection = parseSelection(statuses['po-preset'] ?? '');\n  const byGroup = new Map<string, ContextItem[]>();\n  for (const item of items) {\n    const key = attribution[item.name] ?? 'core';\n    const bucket = byGroup.get(key);\n    if (bucket) bucket.push(item);\n    else byGroup.set(key, [item]);\n  }\n  const take = (key: string): ContextItem[] => byGroup.get(key) ?? [];\n\n  const groups: ContextGroup[] = [];\n\n  if (selection.preset) {\n    groups.push(\n      group(selection.preset, selection.preset, 'preset', selection.pending ? 'switching' : '', take(selection.preset)),\n    );\n  }\n\n  for (const mode of resolveModes(statuses, widgets, projection)) {\n    if (mode.availability !== 'on') continue;\n    groups.push(group(mode.id, mode.name, 'mode', mode.detail, take(mode.id)));\n  }\n\n  for (const domain of [...selection.domains].sort((left, right) => left.localeCompare(right))) {\n    groups.push(group(domain, domain, 'domain', '', take(domain)));\n  }\n\n  // Pi's own tools, and anything the runtime could not attribute. An\n  // unattributed tool is still costing context, so it is listed rather than\n  // dropped for failing to name a sponsor.\n  const orphans = take('core');\n  if (orphans.length > 0) groups.push(group('core', 'core', 'core', '', orphans));\n\n  return groups.sort((left, right) => KIND_ORDER[left.kind] - KIND_ORDER[right.kind]);\n}\n\n/**\n * The runtime's own grouping, used as published.\n *\n * Once the session reports a composition it already knows which mode admitted\n * each tool, which is a join the page cannot make for itself. Re-deriving it\n * here from status strings would only be able to disagree.\n */\nexport function projectedGroups(projection: ContextProjection): ContextGroup[] {\n  return projection.groups.map((group) => ({\n    id: group.id,\n    label: group.label,\n    kind: group.kind,\n    detail: '',\n    inactiveTokens: group.inactiveTokens,\n    items: group.items.map((item) => ({\n      name: item.name,\n      itemKind: item.itemKind,\n      source: item.source,\n      owner: item.owner,\n      tokens: item.tokens,\n      active: item.active,\n    })),\n    tokens: group.tokens,\n  }));\n}\n\n/** The whole composition's estimate, or null while nothing is priced. */\nexport function totalTokens(groups: readonly ContextGroup[]): number | null {\n  const priced = groups.filter((entry) => entry.tokens !== null);\n  return priced.length === 0 ? null : priced.reduce((total, entry) => total + (entry.tokens ?? 0), 0);\n}\n\n/**\n * One package, server, or plugin inside a mode, with what it costs.\n *\n * A mode can admit two dozen tools from a dozen packages, and a flat list of\n * those names says nothing about which dependency to reconsider. The package is\n * the unit that gets added or removed, so it is the unit worth totalling.\n */\nexport interface ContextOwner {\n  owner: string;\n  /** Every tool a single owner registers shares one source kind. */\n  source: ContextItemSource;\n  items: ContextItem[];\n  tokens: number;\n  inactiveTokens: number;\n}\n\n/** The scope is noise once the rows beneath already name the package. */\nexport function ownerLabel(owner: string): string {\n  return owner.startsWith('@') ? (owner.split('/').pop() ?? owner) : owner;\n}\n\nexport function ownersOf(group: ContextGroup): ContextOwner[] {\n  const byOwner = new Map<string, ContextOwner>();\n  for (const item of group.items) {\n    const existing = byOwner.get(item.owner);\n    if (existing) {\n      existing.items.push(item);\n      existing.tokens += item.active ? (item.tokens ?? 0) : 0;\n      existing.inactiveTokens += item.active ? 0 : (item.tokens ?? 0);\n      continue;\n    }\n    byOwner.set(item.owner, {\n      owner: item.owner,\n      source: item.source,\n      items: [item],\n      tokens: item.active ? (item.tokens ?? 0) : 0,\n      inactiveTokens: item.active ? 0 : (item.tokens ?? 0),\n    });\n  }\n  for (const entry of byOwner.values()) entry.items.sort((left, right) => left.name.localeCompare(right.name));\n  return [...byOwner.values()].sort(\n    (left, right) =>\n      SOURCE_ORDER[left.source] - SOURCE_ORDER[right.source] ||\n      ownerLabel(left.owner).localeCompare(ownerLabel(right.owner)),\n  );\n}\n\n/** What every gated item across the composition would add if switched on. */\nexport function inactiveTotal(groups: readonly ContextGroup[]): number {\n  return groups.reduce((total, entry) => total + entry.inactiveTokens, 0);\n}\n"],"mappings":";;;;AAkBA,MAAM,eAAkD;CAAE,WAAW;CAAG,KAAK;CAAG,QAAQ;CAAG,MAAM;AAAE;AAEnG,MAAM,aAA+C;CAAE,QAAQ;CAAG,MAAM;CAAG,QAAQ;CAAG,QAAQ;CAAG,MAAM;AAAE;AA0BzG,SAAS,SAAS,MAAmB,OAA4B;CAC/D,OAAO,aAAa,KAAK,UAAU,aAAa,MAAM,WAAW,KAAK,KAAK,cAAc,MAAM,IAAI;AACrG;;AAGA,SAAS,SAAS,OAA8C;CAC9D,MAAM,SAAS,MAAM,QAAQ,SAAS,KAAK,WAAW,IAAI;CAC1D,OAAO,OAAO,WAAW,IAAI,OAAO,OAAO,QAAQ,OAAO,SAAS,SAAS,KAAK,UAAU,IAAI,CAAC;AAClG;AAEA,SAAS,MAAM,IAAY,OAAe,MAAwB,QAAgB,OAAoC;CACpH,MAAM,SAAS,CAAC,GAAG,KAAK,CAAC,CAAC,KAAK,QAAQ;CACvC,OAAO;EAAE;EAAI;EAAO;EAAM;EAAQ,OAAO;EAAQ,QAAQ,SAAS,MAAM;EAAG,gBAAgB;CAAE;AAC/F;;;;;;;;AASA,SAAgB,cACd,UACA,SACA,aAAoC,MACpC,QAAgC,CAAC,GACjC,cAAgD,CAAC,GACjC;CAChB,MAAM,YAAYA,mBAAAA,eAAe,SAAS,gBAAgB,EAAE;CAC5D,MAAM,0BAAU,IAAI,IAA2B;CAC/C,KAAK,MAAM,QAAQ,OAAO;EACxB,MAAM,MAAM,YAAY,KAAK,SAAS;EACtC,MAAM,SAAS,QAAQ,IAAI,GAAG;EAC9B,IAAI,QAAQ,OAAO,KAAK,IAAI;OACvB,QAAQ,IAAI,KAAK,CAAC,IAAI,CAAC;CAC9B;CACA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