{"version":3,"file":"agent/progress/progress-bus.mjs","sources":["../../../../src/agent/progress/progress-bus.ts"],"sourcesContent":["import type { AgentProgressEvent, AgentProgressListener } from '@/types';\nimport { getDebug } from '@midscene/shared/logger';\n\nconst debugError = getDebug('agent-progress-bus', { console: true });\n\n/**\n * A function that publishes one progress notification: it names the producer\n * (`scope`) and the lifecycle `phase`, and hands over the structured `data`.\n * The bus owns the sequence number, so producers never stamp it themselves.\n */\nexport type AgentProgressPublisher = (\n  scope: string,\n  phase: string,\n  data: unknown,\n) => Promise<void>;\n\n/**\n * The generic agent progress bus.\n *\n * A single broadcast channel every producer publishes onto and every consumer\n * subscribes to. The bus is intentionally tiny and producer-agnostic: it stamps\n * a monotonic sequence, wraps the payload in an {@link AgentProgressEvent}\n * envelope, and fans it out to listeners with per-listener error isolation.\n * `aiAct` is the first producer; adding more requires nothing here.\n */\nexport class AgentProgressBus {\n  private listeners: AgentProgressListener[] = [];\n\n  private sequence = 0;\n\n  /**\n   * Register a listener. Returns a disposer that removes it.\n   */\n  subscribe(listener: AgentProgressListener): () => void {\n    this.listeners.push(listener);\n    return () => {\n      this.unsubscribe(listener);\n    };\n  }\n\n  unsubscribe(listener: AgentProgressListener): void {\n    const index = this.listeners.indexOf(listener);\n    if (index > -1) {\n      this.listeners.splice(index, 1);\n    }\n  }\n\n  clear(): void {\n    this.listeners = [];\n  }\n\n  get listenerCount(): number {\n    return this.listeners.length;\n  }\n\n  /**\n   * Stamp a monotonic sequence, build the envelope, and broadcast it to every\n   * listener in registration order. A listener that throws is logged and\n   * skipped so it cannot break the others or the producer.\n   */\n  publish: AgentProgressPublisher = async (scope, phase, data) => {\n    const event: AgentProgressEvent = {\n      scope,\n      phase,\n      sequence: ++this.sequence,\n      data,\n    };\n    for (const listener of this.listeners) {\n      try {\n        await listener(event);\n      } catch (error) {\n        debugError('error in progress listener', error);\n      }\n    }\n  };\n}\n"],"names":["debugError","getDebug","AgentProgressBus","listener","index","scope","phase","data","event","error"],"mappings":";;;;;;;;;;;AAGA,MAAMA,aAAaC,SAAS,sBAAsB;IAAE,SAAS;AAAK;AAsB3D,MAAMC;IAQX,UAAUC,QAA+B,EAAc;QACrD,IAAI,CAAC,SAAS,CAAC,IAAI,CAACA;QACpB,OAAO;YACL,IAAI,CAAC,WAAW,CAACA;QACnB;IACF;IAEA,YAAYA,QAA+B,EAAQ;QACjD,MAAMC,QAAQ,IAAI,CAAC,SAAS,CAAC,OAAO,CAACD;QACrC,IAAIC,QAAQ,IACV,IAAI,CAAC,SAAS,CAAC,MAAM,CAACA,OAAO;IAEjC;IAEA,QAAc;QACZ,IAAI,CAAC,SAAS,GAAG,EAAE;IACrB;IAEA,IAAI,gBAAwB;QAC1B,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM;IAC9B;;QA3BA,uBAAQ,aAAqC,EAAE;QAE/C,uBAAQ,YAAW;QAgCnB,kCAAkC,OAAOC,OAAOC,OAAOC;YACrD,MAAMC,QAA4B;gBAChCH;gBACAC;gBACA,UAAU,EAAE,IAAI,CAAC,QAAQ;gBACzBC;YACF;YACA,KAAK,MAAMJ,YAAY,IAAI,CAAC,SAAS,CACnC,IAAI;gBACF,MAAMA,SAASK;YACjB,EAAE,OAAOC,OAAO;gBACdT,WAAW,8BAA8BS;YAC3C;QAEJ;;AACF"}