@agenticmail/api 0.9.8 → 0.9.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agenticmail/api",
3
- "version": "0.9.8",
3
+ "version": "0.9.9",
4
4
  "description": "REST API server for AgenticMail — email and SMS endpoints for AI agents",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -12,6 +12,7 @@
12
12
  // reflects what the agent is doing right now.
13
13
 
14
14
  import { onSystemEvent } from './system-stream.js';
15
+ import { state, API_URL } from './state.js';
15
16
 
16
17
  const BADGE_CONTAINER_ID = 'activity-badges';
17
18
 
@@ -92,6 +93,32 @@ function handleEvent(event) {
92
93
  }
93
94
  }
94
95
 
96
+ /**
97
+ * One-shot backfill: pull the dispatcher's currently-active workers
98
+ * so the badges appear IMMEDIATELY on page load if anything is in
99
+ * flight. Without this, the user only sees badges when the next
100
+ * worker_heartbeat / worker_started event fires — which can be up
101
+ * to 30 s away (heartbeat cadence), or never if the worker happens
102
+ * to finish first.
103
+ *
104
+ * Failures here are silent — the SSE stream is the source of truth
105
+ * for subsequent updates and will paint badges as events arrive.
106
+ */
107
+ async function backfillActiveWorkers() {
108
+ try {
109
+ const res = await fetch(`${API_URL}/api/agenticmail/dispatcher/activity`, {
110
+ headers: { Authorization: `Bearer ${state.masterKey}` },
111
+ });
112
+ if (!res.ok) return;
113
+ const data = await res.json();
114
+ const active = Array.isArray(data?.active) ? data.active : [];
115
+ for (const w of active) {
116
+ if (w?.workerId) workers.set(w.workerId, w);
117
+ }
118
+ render();
119
+ } catch { /* silent — SSE will repaint as events come in */ }
120
+ }
121
+
95
122
  /**
96
123
  * Subscribe to worker_* events on the shared /system/events stream.
97
124
  * Idempotent — safe to call after agent-list refresh.
@@ -103,6 +130,12 @@ export function subscribeToActivity() {
103
130
  unsubWorkerStarted = onSystemEvent('worker_started', handleEvent);
104
131
  unsubWorkerHeartbeat = onSystemEvent('worker_heartbeat', handleEvent);
105
132
  unsubWorkerFinished = onSystemEvent('worker_finished', handleEvent);
133
+ // Paint whatever's already running BEFORE the first SSE event
134
+ // arrives. Without this, an in-flight worker stays invisible
135
+ // until its next heartbeat (~30 s) or until it finishes (which
136
+ // then never paints since worker_finished just removes the
137
+ // badge that never showed up).
138
+ backfillActiveWorkers();
106
139
  }
107
140
 
108
141
  // Tiny HTML escapers (kept local to avoid an import cycle).