| 39 | } |
| 40 | |
| 41 | export function installWorkflowNotifications( |
| 42 | service: WorkflowService, |
| 43 | notify: WorkflowNotifier = defaultNotifier, |
| 44 | ): () => void { |
| 45 | const prevStatus = new Map<string, RunProgress['status'] | undefined>() |
| 46 | |
| 47 | const unsubscribe = service.subscribe(() => { |
| 48 | const runs = service.listRuns() |
| 49 | for (const run of runs) { |
| 50 | const prev = prevStatus.get(run.runId) |
| 51 | // First time seeing this run: just record the current status without notifying |
| 52 | // (avoids treating existing historical runs as new notifications on install) |
| 53 | if (prev === undefined) { |
| 54 | prevStatus.set(run.runId, run.status) |
| 55 | continue |
| 56 | } |
| 57 | // Status changed + entered terminal state → emit notification |
| 58 | if (prev !== run.status && TERMINAL_STATUSES.has(run.status)) { |
| 59 | notify(buildMessage(run)) |
| 60 | } |
| 61 | prevStatus.set(run.runId, run.status) |
| 62 | } |
| 63 | }) |
| 64 | |
| 65 | return () => { |
| 66 | unsubscribe() |
| 67 | prevStatus.clear() |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | function buildMessage(run: RunProgress): string { |
| 72 | const statusText = |