{"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/services/workflowsHubChannel/index.ts"],"sourcesContent":["import type { DoomHubChannelSource, DoomHubSessionScope, DoomHubChannel } from '@agimon-ai/doompi-core/hub-channel';\n\nimport { presentWorkflowRuns, runBelongsToSession, type ParsedWorkflowRun } from '../../services/workflowRuns';\nimport { readWorkflowRuns, type ReadWorkflowRunsOptions } from '../../services/workflowWatcher';\nimport { WORKFLOW_RUNS_TYPE, type WorkflowRunView, type WorkflowRunsPayload } from '../../types/webWorkflows';\n\nexport interface WorkflowsChannelOptions {\n  /** Injectable durable initial snapshot reader for tests. */\n  read?: (options: ReadWorkflowRunsOptions) => ParsedWorkflowRun[];\n}\n\nfunction isWorkflowRunsPayload(value: unknown): value is WorkflowRunsPayload {\n  return typeof value === 'object' && value !== null && Array.isArray((value as { runs?: unknown }).runs);\n}\n\n/**\n * The workflow runs data channel. Durable registry state seeds each session\n * once; subsequent updates arrive through the host-owned direct event bus.\n */\nexport function createWorkflowsChannel(options: WorkflowsChannelOptions = {}): DoomHubChannel {\n  const read = options.read ?? readWorkflowRuns;\n  return {\n    frameType: WORKFLOW_RUNS_TYPE,\n    start(host) {\n      const scopes = new Map<string, DoomHubSessionScope>();\n      const latest = new Map<string, WorkflowRunsPayload>();\n      const subscriptions = new Map<string, () => void>();\n      const runsFor = (scope: DoomHubSessionScope, parsed: readonly ParsedWorkflowRun[]): WorkflowRunView[] =>\n        presentWorkflowRuns(\n          parsed.filter((run) => runBelongsToSession(run, scope.sessionId)).map((run) => run.view),\n          Date.now(),\n        );\n      const publish = (scope: DoomHubSessionScope, payload: WorkflowRunsPayload): void => {\n        latest.set(scope.sessionId, payload);\n        host.publish(scope.sessionId, payload);\n      };\n      const source: DoomHubChannelSource = {\n        payloadFor(scope) {\n          return scopes.has(scope.sessionId) ? latest.get(scope.sessionId) : undefined;\n        },\n        sessionAdded(scope) {\n          scopes.set(scope.sessionId, scope);\n          subscriptions.get(scope.sessionId)?.();\n          const unsubscribe = host.directEvents.subscribe(\n            WORKFLOW_RUNS_TYPE,\n            scope.sessionId,\n            (value) => {\n              if (isWorkflowRunsPayload(value)) publish(scope, value);\n            },\n            { replayLatest: true },\n          );\n          subscriptions.set(scope.sessionId, unsubscribe);\n          const payload = { runs: runsFor(scope, read({ environment: scope.environment })) };\n          publish(scope, payload);\n        },\n        sessionRemoved(sessionId) {\n          subscriptions.get(sessionId)?.();\n          subscriptions.delete(sessionId);\n          scopes.delete(sessionId);\n          latest.delete(sessionId);\n        },\n        close() {\n          for (const unsubscribe of subscriptions.values()) unsubscribe();\n          subscriptions.clear();\n          scopes.clear();\n          latest.clear();\n        },\n      };\n      return source;\n    },\n  };\n}\n"],"mappings":";;;;AAWA,SAAS,sBAAsB,OAA8C;CAC3E,OAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,MAAM,QAAS,MAA6B,IAAI;AACxG;;;;;AAMA,SAAgB,uBAAuB,UAAmC,CAAC,GAAmB;CAC5F,MAAM,OAAO,QAAQ,QAAQ;CAC7B,OAAO;EACL,WAAW;EACX,MAAM,MAAM;GACV,MAAM,yBAAS,IAAI,IAAiC;GACpD,MAAM,yBAAS,IAAI,IAAiC;GACpD,MAAM,gCAAgB,IAAI,IAAwB;GAClD,MAAM,WAAW,OAA4B,WAC3C,oBACE,OAAO,QAAQ,QAAQ,oBAAoB,KAAK,MAAM,SAAS,CAAC,CAAC,CAAC,KAAK,QAAQ,IAAI,IAAI,GACvF,KAAK,IAAI,CACX;GACF,MAAM,WAAW,OAA4B,YAAuC;IAClF,OAAO,IAAI,MAAM,WAAW,OAAO;IACnC,KAAK,QAAQ,MAAM,WAAW,OAAO;GACvC;GAiCA,OAAO;IA/BL,WAAW,OAAO;KAChB,OAAO,OAAO,IAAI,MAAM,SAAS,IAAI,OAAO,IAAI,MAAM,SAAS,IAAI,KAAA;IACrE;IACA,aAAa,OAAO;KAClB,OAAO,IAAI,MAAM,WAAW,KAAK;KACjC,cAAc,IAAI,MAAM,SAAS,CAAC,G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