@narumitw/pi-herdr 0.3.1 → 0.3.3
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/README.md +17 -9
- package/dist/index.ts +243 -277
- package/dist/index.ts.map +3 -3
- package/package.json +5 -5
- package/src/herdr-agent-state.ts +38 -21
- package/src/herdr-client.ts +44 -53
- package/src/herdr-observer.ts +170 -150
- package/src/herdr-protocol.ts +21 -32
- package/src/herdr-widget.ts +1 -55
package/src/herdr-observer.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { createEditorStatusPublisher, type EditorStatusPublisher } from "@narumitw/pi-tui-kit/editor-status-publisher";
|
|
2
3
|
import { type HerdrRequest, type HerdrSubscription, openHerdrSubscription, requestHerdr } from "./herdr-client.js";
|
|
3
4
|
import {
|
|
4
5
|
type HerdrPane,
|
|
@@ -11,7 +12,13 @@ import {
|
|
|
11
12
|
parsePaneListResult,
|
|
12
13
|
parseWorkspaceGetResult,
|
|
13
14
|
} from "./herdr-protocol.js";
|
|
14
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
createHerdrWidget,
|
|
17
|
+
HERDR_WIDGET_KEY,
|
|
18
|
+
HerdrWidgetModel,
|
|
19
|
+
type HerdrWidgetSnapshot,
|
|
20
|
+
herdrWidgetFingerprint,
|
|
21
|
+
} from "./herdr-widget.js";
|
|
15
22
|
|
|
16
23
|
export interface HerdrObserverEnvironment {
|
|
17
24
|
paneId: string;
|
|
@@ -37,16 +44,17 @@ export interface HerdrWidgetObserver {
|
|
|
37
44
|
shutdown(ctx: ExtensionContext): Promise<void>;
|
|
38
45
|
}
|
|
39
46
|
|
|
40
|
-
const MAX_PENDING_EVENTS = 1024;
|
|
41
|
-
|
|
42
47
|
class HerdrTopologyChangedError extends Error {}
|
|
48
|
+
class HerdrCleanupError extends AggregateError {}
|
|
43
49
|
|
|
44
|
-
function
|
|
45
|
-
return
|
|
50
|
+
function renderHerdrWidget(snapshot: HerdrWidgetSnapshot, theme: Theme, width: number): readonly string[] {
|
|
51
|
+
return createHerdrWidget(snapshot, theme).render(width);
|
|
46
52
|
}
|
|
47
53
|
|
|
48
54
|
function isRelevantEvent(model: HerdrWidgetModel, event: HerdrPaneEvent): boolean {
|
|
49
|
-
if (event.type === "created"
|
|
55
|
+
if (event.type === "created" || event.type === "updated") {
|
|
56
|
+
return model.isCurrentWorkspace(event.pane.workspaceId);
|
|
57
|
+
}
|
|
50
58
|
if (event.type === "moved") {
|
|
51
59
|
return (
|
|
52
60
|
model.isCurrentPane(event.previousPaneId) ||
|
|
@@ -54,7 +62,7 @@ function isRelevantEvent(model: HerdrWidgetModel, event: HerdrPaneEvent): boolea
|
|
|
54
62
|
model.isCurrentWorkspace(event.pane.workspaceId)
|
|
55
63
|
);
|
|
56
64
|
}
|
|
57
|
-
return model.isCurrentWorkspace(event.workspaceId);
|
|
65
|
+
return "workspaceId" in event && model.isCurrentWorkspace(event.workspaceId);
|
|
58
66
|
}
|
|
59
67
|
|
|
60
68
|
function subscriptionFingerprint(currentPane: HerdrPane, panes: readonly HerdrPane[]): string {
|
|
@@ -89,20 +97,21 @@ export function createHerdrWidgetObserver(options: HerdrObserverOptions): HerdrW
|
|
|
89
97
|
const subscribe = options.subscribe ?? openHerdrSubscription;
|
|
90
98
|
const retryDelayMs = options.retryDelayMs ?? 200;
|
|
91
99
|
let generation = 0;
|
|
92
|
-
let
|
|
100
|
+
let publisher: EditorStatusPublisher<HerdrWidgetSnapshot> | undefined;
|
|
93
101
|
let activeSession: ExtensionContext["sessionManager"] | undefined;
|
|
94
102
|
let controller: AbortController | undefined;
|
|
95
103
|
const ownedTasks = new Set<Promise<void>>();
|
|
104
|
+
const taskErrors = new Set<unknown>();
|
|
96
105
|
let publishedFingerprint: string | undefined;
|
|
97
106
|
let hasPublished = false;
|
|
98
107
|
|
|
99
108
|
const owns = (ctx: ExtensionContext, expectedGeneration = generation) =>
|
|
100
|
-
|
|
109
|
+
expectedGeneration === generation && ctx.sessionManager === activeSession;
|
|
101
110
|
|
|
102
111
|
const requestId = (kind: string) => `herdr:pi:widget:${kind}:${now()}:${random().toString(36).slice(2)}`;
|
|
103
112
|
|
|
104
|
-
const clearWidget = (
|
|
105
|
-
|
|
113
|
+
const clearWidget = () => {
|
|
114
|
+
publisher?.publish(undefined);
|
|
106
115
|
publishedFingerprint = undefined;
|
|
107
116
|
hasPublished = false;
|
|
108
117
|
};
|
|
@@ -112,13 +121,7 @@ export function createHerdrWidgetObserver(options: HerdrObserverOptions): HerdrW
|
|
|
112
121
|
const snapshot = model.snapshot();
|
|
113
122
|
const fingerprint = herdrWidgetFingerprint(snapshot);
|
|
114
123
|
if (hasPublished && fingerprint === publishedFingerprint) return;
|
|
115
|
-
|
|
116
|
-
ctx.ui.setWidget(HERDR_WIDGET_KEY, undefined);
|
|
117
|
-
} else {
|
|
118
|
-
ctx.ui.setWidget(HERDR_WIDGET_KEY, (_tui: unknown, theme: Theme) => createHerdrWidget(snapshot, theme), {
|
|
119
|
-
placement: "aboveEditor",
|
|
120
|
-
});
|
|
121
|
-
}
|
|
124
|
+
publisher?.publish(snapshot);
|
|
122
125
|
publishedFingerprint = fingerprint;
|
|
123
126
|
hasPublished = true;
|
|
124
127
|
};
|
|
@@ -165,7 +168,13 @@ export function createHerdrWidgetObserver(options: HerdrObserverOptions): HerdrW
|
|
|
165
168
|
),
|
|
166
169
|
]);
|
|
167
170
|
if (!owns(ctx, expectedGeneration) || signal.aborted) return undefined;
|
|
168
|
-
const
|
|
171
|
+
const panes = parsePaneListResult(listResult);
|
|
172
|
+
const paneIds = new Set(panes.map((pane) => pane.paneId));
|
|
173
|
+
const namesByPane = new Map(
|
|
174
|
+
parseAgentListResult(agentResult)
|
|
175
|
+
.filter((pane) => pane.workspaceId === currentPane.workspaceId && paneIds.has(pane.paneId))
|
|
176
|
+
.map((pane) => [pane.paneId, pane.agentName]),
|
|
177
|
+
);
|
|
169
178
|
const withAgentName = (pane: HerdrPane): HerdrPane => ({
|
|
170
179
|
...pane,
|
|
171
180
|
agentName: namesByPane.get(pane.paneId) ?? pane.agentName,
|
|
@@ -176,7 +185,7 @@ export function createHerdrWidgetObserver(options: HerdrObserverOptions): HerdrW
|
|
|
176
185
|
}
|
|
177
186
|
return {
|
|
178
187
|
currentPane: withAgentName(currentPane),
|
|
179
|
-
panes:
|
|
188
|
+
panes: panes.map(withAgentName),
|
|
180
189
|
workspace,
|
|
181
190
|
};
|
|
182
191
|
};
|
|
@@ -193,131 +202,106 @@ export function createHerdrWidgetObserver(options: HerdrObserverOptions): HerdrW
|
|
|
193
202
|
const signal = attemptController.signal;
|
|
194
203
|
let attemptActive = true;
|
|
195
204
|
let subscription: HerdrSubscription | undefined;
|
|
196
|
-
let
|
|
205
|
+
let failure: Error | undefined;
|
|
206
|
+
let attemptError: { error: unknown } | undefined;
|
|
207
|
+
let cleanupFailure: { error: unknown } | undefined;
|
|
208
|
+
let wake: (() => void) | undefined;
|
|
209
|
+
const wakeRefresh = () => {
|
|
210
|
+
const resolve = wake;
|
|
211
|
+
wake = undefined;
|
|
212
|
+
resolve?.();
|
|
213
|
+
};
|
|
214
|
+
signal.addEventListener("abort", wakeRefresh);
|
|
215
|
+
const failAttempt = (error: Error) => {
|
|
216
|
+
if (!attemptActive || signal.aborted) return;
|
|
217
|
+
failure = error;
|
|
218
|
+
try {
|
|
219
|
+
if (owns(ctx, expectedGeneration)) clearWidget();
|
|
220
|
+
} catch (cleanupError) {
|
|
221
|
+
failure = new AggregateError([error, cleanupError], "Herdr subscription and widget cleanup failed");
|
|
222
|
+
} finally {
|
|
223
|
+
// Never leave the refresh wait alive because UI cleanup failed.
|
|
224
|
+
attemptController.abort(failure);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
197
227
|
try {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
let initialized = false;
|
|
206
|
-
let topologyPending = false;
|
|
207
|
-
let refreshingTopology = false;
|
|
208
|
-
let refreshAgain = false;
|
|
209
|
-
let rejectReconfigure!: (error: Error) => void;
|
|
210
|
-
let reconfigureSettled = false;
|
|
211
|
-
const reconfigure = new Promise<never>((_resolve, reject) => {
|
|
212
|
-
rejectReconfigure = reject;
|
|
213
|
-
});
|
|
214
|
-
void reconfigure.catch(() => undefined);
|
|
215
|
-
const failAttempt = (error: Error) => {
|
|
216
|
-
if (reconfigureSettled) return;
|
|
217
|
-
reconfigureSettled = true;
|
|
218
|
-
rejectReconfigure(error);
|
|
219
|
-
};
|
|
220
|
-
const scheduleTopologyRefresh = () => {
|
|
221
|
-
if (!initialized || !attemptActive || signal.aborted) return;
|
|
222
|
-
if (refreshTask) {
|
|
223
|
-
refreshAgain = true;
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
|
-
refreshTask = (async () => {
|
|
227
|
-
do {
|
|
228
|
-
refreshAgain = false;
|
|
229
|
-
refreshingTopology = true;
|
|
230
|
-
const refreshed = await loadPaneSet(ctx, expectedGeneration, signal);
|
|
231
|
-
if (!refreshed || !attemptActive || signal.aborted) return;
|
|
232
|
-
model.reset(refreshed.currentPane, refreshed.panes, refreshed.workspace);
|
|
233
|
-
for (const event of pendingStatusEvents.splice(0)) {
|
|
234
|
-
if (isRelevantEvent(model, event)) model.apply(event);
|
|
235
|
-
}
|
|
236
|
-
refreshingTopology = false;
|
|
237
|
-
publish(ctx, model, expectedGeneration);
|
|
238
|
-
if (subscriptionFingerprint(refreshed.currentPane, refreshed.panes) !== subscribedFingerprint) {
|
|
239
|
-
failAttempt(new HerdrTopologyChangedError());
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
} while (refreshAgain && attemptActive && !signal.aborted);
|
|
243
|
-
})()
|
|
244
|
-
.catch((error) => {
|
|
245
|
-
failAttempt(error instanceof Error ? error : new Error(String(error)));
|
|
246
|
-
})
|
|
247
|
-
.finally(() => {
|
|
248
|
-
refreshingTopology = false;
|
|
249
|
-
refreshTask = undefined;
|
|
250
|
-
});
|
|
251
|
-
};
|
|
228
|
+
await (async () => {
|
|
229
|
+
const discovered = await loadPaneSet(ctx, expectedGeneration, signal);
|
|
230
|
+
if (!discovered) return;
|
|
231
|
+
const subscribedFingerprint = subscriptionFingerprint(discovered.currentPane, discovered.panes);
|
|
232
|
+
const model = new HerdrWidgetModel();
|
|
233
|
+
model.reset(discovered.currentPane, discovered.panes, discovered.workspace);
|
|
234
|
+
let dirty = true;
|
|
252
235
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
236
|
+
subscription = await subscribe(
|
|
237
|
+
options.environment.socketEndpoint,
|
|
238
|
+
{
|
|
239
|
+
id: requestId("subscribe"),
|
|
240
|
+
method: "events.subscribe",
|
|
241
|
+
params: { subscriptions: herdrWidgetSubscriptions(discovered.panes) },
|
|
242
|
+
},
|
|
243
|
+
signal,
|
|
244
|
+
(frame) => {
|
|
245
|
+
if (!attemptActive || !owns(ctx, expectedGeneration) || signal.aborted) return;
|
|
246
|
+
const event = parseHerdrPaneEvent(frame);
|
|
264
247
|
if (!event || !isRelevantEvent(model, event)) return;
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
248
|
+
dirty = true;
|
|
249
|
+
wakeRefresh();
|
|
250
|
+
},
|
|
251
|
+
).catch(async (error: unknown) => {
|
|
252
|
+
if (!owns(ctx, expectedGeneration) || signal.aborted) throw error;
|
|
253
|
+
// A pane can disappear between discovery and subscription acceptance.
|
|
254
|
+
// Verify a changed target set before exempting this from outage retries.
|
|
255
|
+
const latest = await loadPaneSet(ctx, expectedGeneration, signal);
|
|
256
|
+
if (latest && subscriptionFingerprint(latest.currentPane, latest.panes) !== subscribedFingerprint) {
|
|
257
|
+
throw new HerdrTopologyChangedError();
|
|
274
258
|
}
|
|
259
|
+
throw error;
|
|
260
|
+
});
|
|
261
|
+
void subscription.closed.then(
|
|
262
|
+
() => failAttempt(new Error("Herdr subscription ended")),
|
|
263
|
+
(error) => failAttempt(error instanceof Error ? error : new Error(String(error))),
|
|
264
|
+
);
|
|
275
265
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
if (
|
|
280
|
-
|
|
281
|
-
|
|
266
|
+
while (owns(ctx, expectedGeneration) && !signal.aborted) {
|
|
267
|
+
dirty = false;
|
|
268
|
+
const refreshed = await loadPaneSet(ctx, expectedGeneration, signal);
|
|
269
|
+
if (!refreshed || signal.aborted) break;
|
|
270
|
+
// Events and reads have no common sequence boundary. Never replay event
|
|
271
|
+
// payloads over a read, or publish a read invalidated while in flight.
|
|
272
|
+
if (dirty) continue;
|
|
273
|
+
if (subscriptionFingerprint(refreshed.currentPane, refreshed.panes) !== subscribedFingerprint) {
|
|
274
|
+
throw new HerdrTopologyChangedError();
|
|
282
275
|
}
|
|
283
|
-
|
|
284
|
-
if (pendingStatusEvents.length >= MAX_PENDING_EVENTS) {
|
|
285
|
-
throw new Error("Too many Herdr status events arrived during refresh");
|
|
286
|
-
}
|
|
287
|
-
pendingStatusEvents.push(event);
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
model.apply(event);
|
|
276
|
+
model.reset(refreshed.currentPane, refreshed.panes, refreshed.workspace);
|
|
291
277
|
publish(ctx, model, expectedGeneration);
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
}
|
|
302
|
-
if (subscriptionFingerprint(reconciled.currentPane, reconciled.panes) !== subscribedFingerprint) {
|
|
303
|
-
throw new HerdrTopologyChangedError();
|
|
304
|
-
}
|
|
305
|
-
initialized = true;
|
|
306
|
-
publish(ctx, model, expectedGeneration);
|
|
307
|
-
if (topologyPending) scheduleTopologyRefresh();
|
|
308
|
-
await Promise.race([
|
|
309
|
-
subscription.closed.then(() => {
|
|
310
|
-
if (!signal.aborted) throw new Error("Herdr subscription ended");
|
|
311
|
-
}),
|
|
312
|
-
reconfigure,
|
|
313
|
-
]);
|
|
278
|
+
await new Promise<void>((resolve) => {
|
|
279
|
+
wake = resolve;
|
|
280
|
+
if (dirty || signal.aborted) wakeRefresh();
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
if (failure) throw failure;
|
|
284
|
+
})();
|
|
285
|
+
} catch (error) {
|
|
286
|
+
attemptError = { error };
|
|
314
287
|
} finally {
|
|
315
288
|
attemptActive = false;
|
|
316
289
|
attemptController.abort(new DOMException("Herdr widget attempt ended", "AbortError"));
|
|
290
|
+
signal.removeEventListener("abort", wakeRefresh);
|
|
317
291
|
sessionSignal.removeEventListener("abort", abortAttempt);
|
|
318
|
-
|
|
319
|
-
|
|
292
|
+
try {
|
|
293
|
+
subscription?.close();
|
|
294
|
+
} catch (cleanupError) {
|
|
295
|
+
cleanupFailure = { error: cleanupError };
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (cleanupFailure) {
|
|
299
|
+
throw new HerdrCleanupError(
|
|
300
|
+
attemptError ? [attemptError.error, cleanupFailure.error] : [cleanupFailure.error],
|
|
301
|
+
"Herdr subscription cleanup failed",
|
|
302
|
+
);
|
|
320
303
|
}
|
|
304
|
+
if (attemptError) throw attemptError.error;
|
|
321
305
|
};
|
|
322
306
|
|
|
323
307
|
const run = async (ctx: ExtensionContext, expectedGeneration: number, signal: AbortSignal): Promise<void> => {
|
|
@@ -327,8 +311,13 @@ export function createHerdrWidgetObserver(options: HerdrObserverOptions): HerdrW
|
|
|
327
311
|
await runAttempt(ctx, expectedGeneration, signal);
|
|
328
312
|
return;
|
|
329
313
|
} catch (error) {
|
|
314
|
+
if (error instanceof HerdrCleanupError) throw error;
|
|
330
315
|
if (!owns(ctx, expectedGeneration) || signal.aborted) return;
|
|
331
|
-
|
|
316
|
+
try {
|
|
317
|
+
clearWidget();
|
|
318
|
+
} catch (cleanupError) {
|
|
319
|
+
throw new AggregateError([error, cleanupError], "Herdr observation and widget cleanup failed");
|
|
320
|
+
}
|
|
332
321
|
if (error instanceof HerdrTopologyChangedError) continue;
|
|
333
322
|
failures += 1;
|
|
334
323
|
if (failures >= 2) return;
|
|
@@ -344,34 +333,65 @@ export function createHerdrWidgetObserver(options: HerdrObserverOptions): HerdrW
|
|
|
344
333
|
|
|
345
334
|
return {
|
|
346
335
|
start(ctx) {
|
|
347
|
-
generation += 1;
|
|
348
336
|
controller?.abort(new DOMException("Herdr widget session replaced", "AbortError"));
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
337
|
+
try {
|
|
338
|
+
publisher?.dispose();
|
|
339
|
+
} finally {
|
|
340
|
+
// Invalidate old reads even if clearing the old widget throws.
|
|
341
|
+
publisher = undefined;
|
|
342
|
+
generation += 1;
|
|
343
|
+
activeSession = undefined;
|
|
344
|
+
controller = undefined;
|
|
345
|
+
publishedFingerprint = undefined;
|
|
346
|
+
hasPublished = false;
|
|
347
|
+
}
|
|
353
348
|
if (ctx.mode !== "tui") return;
|
|
354
349
|
|
|
355
|
-
activeContext = ctx;
|
|
356
350
|
activeSession = ctx.sessionManager;
|
|
357
351
|
controller = new AbortController();
|
|
358
352
|
const expectedGeneration = generation;
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
353
|
+
publisher = createEditorStatusPublisher({
|
|
354
|
+
key: HERDR_WIDGET_KEY,
|
|
355
|
+
context: ctx,
|
|
356
|
+
isCurrent: () => expectedGeneration === generation,
|
|
357
|
+
render: renderHerdrWidget,
|
|
362
358
|
});
|
|
359
|
+
const task = run(ctx, expectedGeneration, controller.signal);
|
|
363
360
|
ownedTasks.add(task);
|
|
361
|
+
// Observe every background rejection immediately; shutdown reports failures
|
|
362
|
+
// after joining source cleanup instead of leaking an unhandled rejection.
|
|
363
|
+
void task.then(
|
|
364
|
+
() => ownedTasks.delete(task),
|
|
365
|
+
(error) => {
|
|
366
|
+
taskErrors.add(error);
|
|
367
|
+
ownedTasks.delete(task);
|
|
368
|
+
},
|
|
369
|
+
);
|
|
364
370
|
},
|
|
365
371
|
async shutdown(ctx) {
|
|
366
|
-
if (!owns(ctx)) return;
|
|
367
|
-
const shutdownGeneration = ++generation;
|
|
372
|
+
if (activeSession !== undefined && !owns(ctx)) return;
|
|
368
373
|
controller?.abort(new DOMException("Herdr widget session shut down", "AbortError"));
|
|
369
|
-
|
|
374
|
+
const errors: unknown[] = [];
|
|
375
|
+
try {
|
|
376
|
+
publisher?.dispose();
|
|
377
|
+
} catch (error) {
|
|
378
|
+
errors.push(error);
|
|
379
|
+
} finally {
|
|
380
|
+
publisher = undefined;
|
|
381
|
+
generation += 1;
|
|
382
|
+
publishedFingerprint = undefined;
|
|
383
|
+
hasPublished = false;
|
|
384
|
+
}
|
|
385
|
+
const shutdownGeneration = generation;
|
|
370
386
|
await Promise.allSettled([...ownedTasks]);
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
387
|
+
errors.push(...taskErrors);
|
|
388
|
+
taskErrors.clear();
|
|
389
|
+
if (generation === shutdownGeneration) {
|
|
390
|
+
activeSession = undefined;
|
|
391
|
+
controller = undefined;
|
|
392
|
+
}
|
|
393
|
+
if (errors.length === 1) throw errors[0];
|
|
394
|
+
if (errors.length > 1) throw new AggregateError(errors, "Herdr widget cleanup failed");
|
|
375
395
|
},
|
|
376
396
|
};
|
|
377
397
|
}
|
package/src/herdr-protocol.ts
CHANGED
|
@@ -25,35 +25,24 @@ export interface HerdrWorkspace {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
export type HerdrPaneEvent =
|
|
28
|
-
| { type: "created"; pane: HerdrPane }
|
|
29
|
-
| { type: "closed" | "exited"; paneId: string; workspaceId: string }
|
|
28
|
+
| { type: "created" | "updated"; pane: HerdrPane }
|
|
30
29
|
| {
|
|
31
|
-
type: "
|
|
32
|
-
pane: HerdrPane;
|
|
33
|
-
previousPaneId: string;
|
|
34
|
-
previousWorkspaceId: string;
|
|
35
|
-
}
|
|
36
|
-
| {
|
|
37
|
-
type: "agent_detected";
|
|
30
|
+
type: "closed" | "exited" | "agent_detected" | "agent_status_changed";
|
|
38
31
|
paneId: string;
|
|
39
32
|
workspaceId: string;
|
|
40
|
-
agent?: string;
|
|
41
|
-
finalStatus?: HerdrAgentStatus;
|
|
42
|
-
released: boolean;
|
|
43
33
|
}
|
|
34
|
+
| { type: "workspace_renamed"; workspaceId: string }
|
|
44
35
|
| {
|
|
45
|
-
type: "
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
agentStatus: HerdrAgentStatus;
|
|
50
|
-
displayAgent?: string;
|
|
51
|
-
stateLabels?: Record<string, string>;
|
|
52
|
-
title?: string;
|
|
36
|
+
type: "moved";
|
|
37
|
+
pane: HerdrPane;
|
|
38
|
+
previousPaneId: string;
|
|
39
|
+
previousWorkspaceId: string;
|
|
53
40
|
};
|
|
54
41
|
|
|
55
42
|
const HERDR_WIDGET_TOPOLOGY_SUBSCRIPTIONS = [
|
|
56
43
|
{ type: "pane.created" },
|
|
44
|
+
{ type: "pane.updated" },
|
|
45
|
+
{ type: "workspace.renamed" },
|
|
57
46
|
{ type: "pane.closed" },
|
|
58
47
|
{ type: "pane.moved" },
|
|
59
48
|
{ type: "pane.exited" },
|
|
@@ -178,7 +167,13 @@ export function parseHerdrPaneEvent(value: unknown): HerdrPaneEvent | undefined
|
|
|
178
167
|
}
|
|
179
168
|
const data = value.data;
|
|
180
169
|
|
|
181
|
-
if (value.event === "
|
|
170
|
+
if (value.event === "workspace_renamed") {
|
|
171
|
+
const workspaceId = requiredString(data.workspace_id);
|
|
172
|
+
if (!workspaceId || typeof data.label !== "string") return undefined;
|
|
173
|
+
return { type: "workspace_renamed", workspaceId };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (value.event === "pane.agent_status_changed" || value.event === "pane_agent_status_changed") {
|
|
182
177
|
const paneId = requiredString(data.pane_id);
|
|
183
178
|
const workspaceId = requiredString(data.workspace_id);
|
|
184
179
|
const status = agentStatus(data.agent_status);
|
|
@@ -187,17 +182,12 @@ export function parseHerdrPaneEvent(value: unknown): HerdrPaneEvent | undefined
|
|
|
187
182
|
type: "agent_status_changed",
|
|
188
183
|
paneId,
|
|
189
184
|
workspaceId,
|
|
190
|
-
agent: optionalString(data.agent),
|
|
191
|
-
agentStatus: status,
|
|
192
|
-
displayAgent: optionalString(data.display_agent),
|
|
193
|
-
stateLabels: stringRecord(data.state_labels),
|
|
194
|
-
title: optionalString(data.title),
|
|
195
185
|
};
|
|
196
186
|
}
|
|
197
187
|
|
|
198
|
-
if (value.event === "pane_created") {
|
|
188
|
+
if (value.event === "pane_created" || value.event === "pane_updated") {
|
|
199
189
|
const pane = parseHerdrPane(data.pane);
|
|
200
|
-
return pane ? { type: "created", pane } : undefined;
|
|
190
|
+
return pane ? { type: value.event === "pane_created" ? "created" : "updated", pane } : undefined;
|
|
201
191
|
}
|
|
202
192
|
if (value.event === "pane_closed" || value.event === "pane_exited") {
|
|
203
193
|
const paneId = requiredString(data.pane_id);
|
|
@@ -219,16 +209,15 @@ export function parseHerdrPaneEvent(value: unknown): HerdrPaneEvent | undefined
|
|
|
219
209
|
if (value.event === "pane_agent_detected") {
|
|
220
210
|
const paneId = requiredString(data.pane_id);
|
|
221
211
|
const workspaceId = requiredString(data.workspace_id);
|
|
222
|
-
if (!paneId || !workspaceId || typeof data.released !== "boolean")
|
|
212
|
+
if (!paneId || !workspaceId || (data.released !== undefined && typeof data.released !== "boolean")) {
|
|
213
|
+
return undefined;
|
|
214
|
+
}
|
|
223
215
|
const finalStatus = data.final_status === null ? undefined : agentStatus(data.final_status);
|
|
224
216
|
if (data.final_status !== undefined && data.final_status !== null && !finalStatus) return undefined;
|
|
225
217
|
return {
|
|
226
218
|
type: "agent_detected",
|
|
227
219
|
paneId,
|
|
228
220
|
workspaceId,
|
|
229
|
-
agent: optionalString(data.agent),
|
|
230
|
-
finalStatus,
|
|
231
|
-
released: data.released,
|
|
232
221
|
};
|
|
233
222
|
}
|
|
234
223
|
return undefined;
|
package/src/herdr-widget.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import { EditorStatusWidget } from "@narumitw/pi-tui-kit/editor-status-widget";
|
|
3
3
|
import { sanitizeTerminalText } from "@narumitw/pi-tui-kit/terminal-text";
|
|
4
|
-
import type { HerdrAgentStatus, HerdrPane,
|
|
4
|
+
import type { HerdrAgentStatus, HerdrPane, HerdrWorkspace } from "./herdr-protocol.js";
|
|
5
5
|
|
|
6
6
|
export const HERDR_WIDGET_KEY = "herdr:agents";
|
|
7
7
|
export const MAX_HERDR_WIDGET_AGENTS = 5;
|
|
@@ -136,60 +136,6 @@ export class HerdrWidgetModel {
|
|
|
136
136
|
return this.currentWorkspaceId === workspaceId;
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
-
apply(event: HerdrPaneEvent): void {
|
|
140
|
-
if (event.type === "created") {
|
|
141
|
-
this.panes.set(event.pane.paneId, observedPane(event.pane));
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
if (event.type === "closed" || event.type === "exited") {
|
|
145
|
-
this.panes.delete(event.paneId);
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
if (event.type === "moved") {
|
|
149
|
-
this.panes.delete(event.previousPaneId);
|
|
150
|
-
this.panes.set(event.pane.paneId, observedPane(event.pane));
|
|
151
|
-
if (this.currentPaneId === event.previousPaneId) {
|
|
152
|
-
this.currentPaneId = event.pane.paneId;
|
|
153
|
-
this.currentWorkspaceId = event.pane.workspaceId;
|
|
154
|
-
this.workspace = { workspaceId: event.pane.workspaceId };
|
|
155
|
-
}
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const existing = this.panes.get(event.paneId);
|
|
160
|
-
if (event.type === "agent_detected") {
|
|
161
|
-
if (event.released || !event.agent) {
|
|
162
|
-
if (existing) this.panes.set(event.paneId, { ...existing, agent: undefined });
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
this.panes.set(event.paneId, {
|
|
166
|
-
...(existing ?? {
|
|
167
|
-
paneId: event.paneId,
|
|
168
|
-
workspaceId: event.workspaceId,
|
|
169
|
-
agentStatus: event.finalStatus ?? "unknown",
|
|
170
|
-
stateLabels: {},
|
|
171
|
-
}),
|
|
172
|
-
agent: event.agent,
|
|
173
|
-
agentStatus: event.finalStatus ?? existing?.agentStatus ?? "unknown",
|
|
174
|
-
});
|
|
175
|
-
return;
|
|
176
|
-
}
|
|
177
|
-
if (event.type !== "agent_status_changed") return;
|
|
178
|
-
|
|
179
|
-
this.panes.set(event.paneId, {
|
|
180
|
-
...(existing ?? {
|
|
181
|
-
paneId: event.paneId,
|
|
182
|
-
workspaceId: event.workspaceId,
|
|
183
|
-
stateLabels: {},
|
|
184
|
-
}),
|
|
185
|
-
agent: event.agent ?? existing?.agent,
|
|
186
|
-
agentStatus: event.agentStatus,
|
|
187
|
-
displayAgent: event.displayAgent ?? existing?.displayAgent,
|
|
188
|
-
stateLabels: event.stateLabels ?? existing?.stateLabels ?? {},
|
|
189
|
-
title: event.title ?? existing?.title,
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
|
|
193
139
|
snapshot(): HerdrWidgetSnapshot | undefined {
|
|
194
140
|
if (!this.currentPaneId || !this.currentWorkspaceId || !this.workspace) return undefined;
|
|
195
141
|
const rows = [...this.panes.values()]
|