{"version":3,"file":"window.mjs","names":[],"sources":["../../../../../../../ai/src/orchestrator/window.ts"],"sourcesContent":["import type { Message } from \"../contracts/conversation-message.type\";\nimport type { OrchestratorEngineContext } from \"./engine-context.type\";\n\n/**\n * Framework-default history windows (orchestrator.md §4 Phase 4 — \"5\n * for the router, 15 for intents\"). Applied when neither a per-entity\n * override nor a tier default is configured.\n */\nexport const DEFAULT_ROUTER_WINDOW = 5;\nexport const DEFAULT_AGENTS_WINDOW = 15;\n\n/** A history window: keep the last N messages, or a custom slicer. */\nexport type HistoryWindowValue =\n  | number\n  | ((messages: Message[]) => Message[]);\n\n/**\n * The windowed history bound into each consumer for a turn (§4 Phase\n * 4). Applied per-dispatchable, independently: the router can see a\n * different slice than the dispatched agents.\n */\nexport type WindowedHistory = {\n  /** Slice bound into the router's context. */\n  router: Message[];\n  /** Slice forwarded to every dispatched intent/agent. */\n  agents: Message[];\n};\n\n/**\n * Apply a single window to a history array. A number keeps the last N\n * messages (most recent, chronological order preserved); a callback\n * takes full control of the slice (the escape hatch for token-counting\n * or semantic windowing — §4 Phase 4). `N <= 0` keeps nothing.\n */\nexport function applyWindow(\n  messages: Message[],\n  window: HistoryWindowValue,\n): Message[] {\n  if (typeof window === \"function\") {\n    return window(messages);\n  }\n\n  if (window <= 0) {\n    return [];\n  }\n\n  if (messages.length <= window) {\n    return messages.slice();\n  }\n\n  return messages.slice(messages.length - window);\n}\n\n/**\n * Phase 4 — window history (orchestrator.md §3 / §4 Phase 4). Applies\n * the `historyWindow` cascade to the dev-supplied `history` before it\n * is bound into the router and the dispatched agents. Two tiers,\n * evaluated per-role:\n *\n * 1. Tier default — `historyWindow.router` / `historyWindow.agents`.\n * 2. Framework default — `5` for the router, `15` for agents.\n *\n * (The first cascade layer — per-entity overrides on individual\n * intents / the router entry — is the supervisor's concern: it lives\n * on the intent entries the orchestrator spreads into the supervisor,\n * and the supervisor applies it to the agent-windowed slice this phase\n * produces. The orchestrator owns only the two role-level tiers.)\n *\n * Emits `orchestrator.history.windowed` with the agent-slice message\n * count. Pure aside from the event — returns the per-role slices for\n * the dispatch phase to thread through.\n */\nexport function windowHistory(\n  ctx: OrchestratorEngineContext,\n  sessionId: string,\n  history: Message[],\n): WindowedHistory {\n  const config = ctx.config.historyWindow;\n\n  const router = applyWindow(history, config?.router ?? DEFAULT_ROUTER_WINDOW);\n  const agents = applyWindow(history, config?.agents ?? DEFAULT_AGENTS_WINDOW);\n\n  ctx.emitter.emit(\"orchestrator.history.windowed\", {\n    sessionId,\n    messageCount: agents.length,\n  });\n\n  return { router, agents };\n}\n"],"mappings":";;;;;;AAQA,MAAa,wBAAwB;AACrC,MAAa,wBAAwB;;;;;;;AAyBrC,SAAgB,YACd,UACA,QACW;CACX,IAAI,OAAO,WAAW,YACpB,OAAO,OAAO,QAAQ;CAGxB,IAAI,UAAU,GACZ,OAAO,CAAC;CAGV,IAAI,SAAS,UAAU,QACrB,OAAO,SAAS,MAAM;CAGxB,OAAO,SAAS,MAAM,SAAS,SAAS,MAAM;AAChD;;;;;;;;;;;;;;;;;;;;AAqBA,SAAgB,cACd,KACA,WACA,SACiB;CACjB,MAAM,SAAS,IAAI,OAAO;CAE1B,MAAM,SAAS,YAAY,SAAS,QAAQ,WAA+B;CAC3E,MAAM,SAAS,YAAY,SAAS,QAAQ,YAA+B;CAE3E,IAAI,QAAQ,KAAK,iCAAiC;EAChD;EACA,cAAc,OAAO;CACvB,CAAC;CAED,OAAO;EAAE;EAAQ;CAAO;AAC1B"}