@juanibiapina/pi-gob 0.5.1 → 0.7.0
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 +8 -18
- package/src/daemon-client.ts +240 -0
- package/src/index.ts +439 -0
- package/src/segment.ts +82 -0
- package/src/types.ts +119 -0
- package/dist/daemon-client.d.ts +0 -44
- package/dist/daemon-client.d.ts.map +0 -1
- package/dist/daemon-client.js +0 -218
- package/dist/daemon-client.js.map +0 -1
- package/dist/index.d.ts +0 -11
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -387
- package/dist/index.js.map +0 -1
- package/dist/segment.d.ts +0 -24
- package/dist/segment.d.ts.map +0 -1
- package/dist/segment.js +0 -67
- package/dist/segment.js.map +0 -1
- package/dist/types.d.ts +0 -74
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -6
- package/dist/types.js.map +0 -1
package/src/index.ts
ADDED
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gob Extension
|
|
3
|
+
*
|
|
4
|
+
* Manages gob background jobs from within pi.
|
|
5
|
+
* Connects to the gob daemon via Unix socket for real-time job monitoring.
|
|
6
|
+
* Emits powerbar:update events with segment id "gob" showing running jobs.
|
|
7
|
+
* Use /gob to view and interact with running and stopped jobs.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
11
|
+
import { DynamicBorder } from "@earendil-works/pi-coding-agent";
|
|
12
|
+
import { Container, Key, matchesKey, type SelectItem, SelectList, Text } from "@earendil-works/pi-tui";
|
|
13
|
+
import { DaemonClient } from "./daemon-client.js";
|
|
14
|
+
import { formatJobSegment } from "./segment.js";
|
|
15
|
+
import type { Event, JobResponse } from "./types.js";
|
|
16
|
+
|
|
17
|
+
const SEGMENT_ID = "gob";
|
|
18
|
+
const RECONNECT_DELAY_MS = 5000;
|
|
19
|
+
const TICK_INTERVAL_MS = 1000;
|
|
20
|
+
|
|
21
|
+
export default function (pi: ExtensionAPI) {
|
|
22
|
+
// Register powerbar segment
|
|
23
|
+
pi.events.emit("powerbar:register-segment", { id: "gob", label: "Gob Jobs" });
|
|
24
|
+
|
|
25
|
+
const client = new DaemonClient();
|
|
26
|
+
|
|
27
|
+
// Map of running jobs by ID (only running jobs for the current workdir)
|
|
28
|
+
const runningJobs = new Map<string, JobResponse>();
|
|
29
|
+
|
|
30
|
+
// Session CWD
|
|
31
|
+
let sessionCwd: string | undefined;
|
|
32
|
+
|
|
33
|
+
// Cleanup handles
|
|
34
|
+
let unsubscribe: (() => void) | undefined;
|
|
35
|
+
let reconnectTimer: ReturnType<typeof setTimeout> | undefined;
|
|
36
|
+
let tickInterval: ReturnType<typeof setInterval> | undefined;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Emit a powerbar:update event with current running jobs.
|
|
40
|
+
*/
|
|
41
|
+
function updateSegment(): void {
|
|
42
|
+
const jobs = Array.from(runningJobs.values());
|
|
43
|
+
|
|
44
|
+
if (jobs.length === 0) {
|
|
45
|
+
// Clear the segment
|
|
46
|
+
pi.events.emit("powerbar:update", { id: SEGMENT_ID });
|
|
47
|
+
stopTick();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const segment = formatJobSegment(jobs);
|
|
52
|
+
|
|
53
|
+
pi.events.emit("powerbar:update", {
|
|
54
|
+
id: SEGMENT_ID,
|
|
55
|
+
text: segment.text,
|
|
56
|
+
icon: segment.icon,
|
|
57
|
+
color: segment.color,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
startTick();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Start the 1-second tick interval for updating elapsed times.
|
|
65
|
+
*/
|
|
66
|
+
function startTick(): void {
|
|
67
|
+
if (tickInterval) return;
|
|
68
|
+
tickInterval = setInterval(() => {
|
|
69
|
+
if (runningJobs.size > 0) {
|
|
70
|
+
updateSegment();
|
|
71
|
+
} else {
|
|
72
|
+
stopTick();
|
|
73
|
+
}
|
|
74
|
+
}, TICK_INTERVAL_MS);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Stop the tick interval.
|
|
79
|
+
*/
|
|
80
|
+
function stopTick(): void {
|
|
81
|
+
if (tickInterval) {
|
|
82
|
+
clearInterval(tickInterval);
|
|
83
|
+
tickInterval = undefined;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Handle a daemon event by updating the running jobs map.
|
|
89
|
+
*/
|
|
90
|
+
function handleEvent(event: Event): void {
|
|
91
|
+
const job = event.job;
|
|
92
|
+
|
|
93
|
+
switch (event.type) {
|
|
94
|
+
case "job_added":
|
|
95
|
+
case "job_started":
|
|
96
|
+
case "run_started":
|
|
97
|
+
case "job_updated":
|
|
98
|
+
case "ports_updated":
|
|
99
|
+
if (job.status === "running") {
|
|
100
|
+
runningJobs.set(job.id, job);
|
|
101
|
+
} else {
|
|
102
|
+
runningJobs.delete(job.id);
|
|
103
|
+
}
|
|
104
|
+
break;
|
|
105
|
+
|
|
106
|
+
case "job_stopped":
|
|
107
|
+
case "run_stopped":
|
|
108
|
+
runningJobs.delete(job.id);
|
|
109
|
+
break;
|
|
110
|
+
|
|
111
|
+
case "job_removed":
|
|
112
|
+
case "run_removed":
|
|
113
|
+
runningJobs.delete(job.id);
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
updateSegment();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Connect to daemon, fetch initial state, and subscribe to events.
|
|
122
|
+
*/
|
|
123
|
+
async function connectAndSubscribe(): Promise<void> {
|
|
124
|
+
// Clear any pending reconnect
|
|
125
|
+
if (reconnectTimer) {
|
|
126
|
+
clearTimeout(reconnectTimer);
|
|
127
|
+
reconnectTimer = undefined;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const ok = await client.connect();
|
|
131
|
+
if (!ok) {
|
|
132
|
+
// Daemon not running, schedule reconnect
|
|
133
|
+
scheduleReconnect();
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
// Fetch initial list of jobs
|
|
139
|
+
const jobs = await client.list(sessionCwd);
|
|
140
|
+
runningJobs.clear();
|
|
141
|
+
for (const job of jobs) {
|
|
142
|
+
if (job.status === "running") {
|
|
143
|
+
runningJobs.set(job.id, job);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
updateSegment();
|
|
147
|
+
} catch {
|
|
148
|
+
// Failed to list, schedule reconnect
|
|
149
|
+
scheduleReconnect();
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Subscribe to events
|
|
154
|
+
unsubscribe = client.subscribe(
|
|
155
|
+
sessionCwd,
|
|
156
|
+
(event) => handleEvent(event),
|
|
157
|
+
(_err) => {
|
|
158
|
+
// Subscription disconnected, try to reconnect
|
|
159
|
+
unsubscribe = undefined;
|
|
160
|
+
runningJobs.clear();
|
|
161
|
+
updateSegment();
|
|
162
|
+
scheduleReconnect();
|
|
163
|
+
},
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Schedule a reconnection attempt.
|
|
169
|
+
*/
|
|
170
|
+
function scheduleReconnect(): void {
|
|
171
|
+
if (reconnectTimer) return;
|
|
172
|
+
reconnectTimer = setTimeout(() => {
|
|
173
|
+
reconnectTimer = undefined;
|
|
174
|
+
connectAndSubscribe();
|
|
175
|
+
}, RECONNECT_DELAY_MS);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Cleanup all connections and timers.
|
|
180
|
+
*/
|
|
181
|
+
function cleanup(): void {
|
|
182
|
+
if (unsubscribe) {
|
|
183
|
+
unsubscribe();
|
|
184
|
+
unsubscribe = undefined;
|
|
185
|
+
}
|
|
186
|
+
if (reconnectTimer) {
|
|
187
|
+
clearTimeout(reconnectTimer);
|
|
188
|
+
reconnectTimer = undefined;
|
|
189
|
+
}
|
|
190
|
+
stopTick();
|
|
191
|
+
client.disconnect();
|
|
192
|
+
runningJobs.clear();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// ── Lifecycle ──────────────────────────────────────────
|
|
196
|
+
|
|
197
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
198
|
+
sessionCwd = ctx.cwd;
|
|
199
|
+
await connectAndSubscribe();
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
pi.on("session_shutdown", async () => {
|
|
203
|
+
cleanup();
|
|
204
|
+
pi.events.emit("powerbar:update", { id: SEGMENT_ID });
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
// ── Job list UI ──
|
|
208
|
+
|
|
209
|
+
async function fetchJobs(): Promise<JobResponse[]> {
|
|
210
|
+
// Try daemon protocol first
|
|
211
|
+
if (client.connected) {
|
|
212
|
+
try {
|
|
213
|
+
return await client.list(sessionCwd);
|
|
214
|
+
} catch {
|
|
215
|
+
// Fall through to CLI
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// CLI fallback
|
|
220
|
+
const result = await pi.exec("gob", ["list"]);
|
|
221
|
+
if (result.code !== 0) {
|
|
222
|
+
return [];
|
|
223
|
+
}
|
|
224
|
+
return parseGobList(result.stdout);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function parseGobList(output: string): JobResponse[] {
|
|
228
|
+
const jobs: JobResponse[] = [];
|
|
229
|
+
const lines = output.split("\n").filter((l) => l.trim().length > 0);
|
|
230
|
+
|
|
231
|
+
let i = 0;
|
|
232
|
+
while (i < lines.length) {
|
|
233
|
+
const line = lines[i];
|
|
234
|
+
// Format: <job_id>: [<pid>] <status> (<exit_code>): <command>
|
|
235
|
+
const match = line.match(/^(\S+): \[(\S+)\] (\S+)(?: \((\d+)\))?: (.+)$/);
|
|
236
|
+
if (match) {
|
|
237
|
+
const description =
|
|
238
|
+
i + 1 < lines.length && lines[i + 1].startsWith(" ") ? lines[i + 1].trim() : undefined;
|
|
239
|
+
|
|
240
|
+
if (description) i++;
|
|
241
|
+
|
|
242
|
+
jobs.push({
|
|
243
|
+
id: match[1],
|
|
244
|
+
pid: Number.parseInt(match[2], 10) || 0,
|
|
245
|
+
status: match[3],
|
|
246
|
+
command: match[5].split(" "),
|
|
247
|
+
workdir: "",
|
|
248
|
+
description,
|
|
249
|
+
created_at: "",
|
|
250
|
+
started_at: "",
|
|
251
|
+
stdout_path: "",
|
|
252
|
+
stderr_path: "",
|
|
253
|
+
exit_code: match[4] ? Number.parseInt(match[4], 10) : undefined,
|
|
254
|
+
run_count: 0,
|
|
255
|
+
success_count: 0,
|
|
256
|
+
failure_count: 0,
|
|
257
|
+
success_rate: 0,
|
|
258
|
+
avg_duration_ms: 0,
|
|
259
|
+
failure_avg_duration_ms: 0,
|
|
260
|
+
min_duration_ms: 0,
|
|
261
|
+
max_duration_ms: 0,
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
i++;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return jobs;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async function executeJobAction(action: string, jobId: string): Promise<{ message: string; success: boolean }> {
|
|
271
|
+
// Try daemon protocol first
|
|
272
|
+
if (client.connected) {
|
|
273
|
+
try {
|
|
274
|
+
const resp = await client.sendRequest(action, { job_id: jobId });
|
|
275
|
+
if (resp.success) {
|
|
276
|
+
const verb =
|
|
277
|
+
action === "stop"
|
|
278
|
+
? "Stopped"
|
|
279
|
+
: action === "start"
|
|
280
|
+
? "Started"
|
|
281
|
+
: action === "restart"
|
|
282
|
+
? "Restarted"
|
|
283
|
+
: action === "remove"
|
|
284
|
+
? "Removed"
|
|
285
|
+
: action;
|
|
286
|
+
return { message: `${verb} ${jobId}`, success: true };
|
|
287
|
+
}
|
|
288
|
+
return { message: `Error: ${resp.error}`, success: false };
|
|
289
|
+
} catch {
|
|
290
|
+
// Fall through to CLI
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// CLI fallback
|
|
295
|
+
const result = await pi.exec("gob", [action, jobId]);
|
|
296
|
+
if (result.code === 0) {
|
|
297
|
+
const verb =
|
|
298
|
+
action === "stop"
|
|
299
|
+
? "Stopped"
|
|
300
|
+
: action === "start"
|
|
301
|
+
? "Started"
|
|
302
|
+
: action === "restart"
|
|
303
|
+
? "Restarted"
|
|
304
|
+
: action === "remove"
|
|
305
|
+
? "Removed"
|
|
306
|
+
: action;
|
|
307
|
+
return { message: `${verb} ${jobId}`, success: true };
|
|
308
|
+
}
|
|
309
|
+
return { message: `Error: ${result.stderr}`, success: false };
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const showJobList = async (ctx: ExtensionContext) => {
|
|
313
|
+
if (!ctx.hasUI) {
|
|
314
|
+
ctx.ui.notify("No UI available", "error");
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const jobs = await fetchJobs();
|
|
319
|
+
|
|
320
|
+
if (jobs.length === 0) {
|
|
321
|
+
ctx.ui.notify("No gob jobs found", "info");
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
await ctx.ui.custom<void>((tui, theme, _kb, done) => {
|
|
326
|
+
const container = new Container();
|
|
327
|
+
|
|
328
|
+
// Top border
|
|
329
|
+
container.addChild(new DynamicBorder((s: string) => theme.fg("accent", s)));
|
|
330
|
+
|
|
331
|
+
// Title
|
|
332
|
+
container.addChild(new Text(theme.fg("accent", theme.bold(" Gob Jobs")), 0, 0));
|
|
333
|
+
|
|
334
|
+
// Build select items
|
|
335
|
+
const items: SelectItem[] = jobs.map((job) => {
|
|
336
|
+
const statusColor = job.status === "running" ? "success" : "muted";
|
|
337
|
+
const statusIcon = job.status === "running" ? "●" : "○";
|
|
338
|
+
const exitInfo =
|
|
339
|
+
job.status === "stopped" && job.exit_code != null ? theme.fg("dim", ` (${job.exit_code})`) : "";
|
|
340
|
+
const status = theme.fg(statusColor, statusIcon);
|
|
341
|
+
const cmdStr = job.command.join(" ");
|
|
342
|
+
const label = `${status} ${theme.fg("dim", job.id)} ${cmdStr}${exitInfo}`;
|
|
343
|
+
const description = job.description ? theme.fg("muted", job.description) : undefined;
|
|
344
|
+
return {
|
|
345
|
+
value: job.id,
|
|
346
|
+
label,
|
|
347
|
+
description,
|
|
348
|
+
};
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
const visibleRows = Math.min(jobs.length, 15);
|
|
352
|
+
let currentIndex = 0;
|
|
353
|
+
|
|
354
|
+
const selectList = new SelectList(items, visibleRows, {
|
|
355
|
+
selectedPrefix: (t) => theme.fg("accent", t),
|
|
356
|
+
selectedText: (t) => t,
|
|
357
|
+
description: (t) => t,
|
|
358
|
+
scrollInfo: (t) => theme.fg("dim", t),
|
|
359
|
+
noMatch: (t) => theme.fg("warning", t),
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
selectList.onSelect = async (item) => {
|
|
363
|
+
done();
|
|
364
|
+
const job = jobs.find((j) => j.id === item.value);
|
|
365
|
+
if (job) {
|
|
366
|
+
await showJobActions(ctx, job);
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
selectList.onCancel = () => done();
|
|
370
|
+
selectList.onSelectionChange = (item) => {
|
|
371
|
+
currentIndex = items.indexOf(item);
|
|
372
|
+
};
|
|
373
|
+
container.addChild(selectList);
|
|
374
|
+
|
|
375
|
+
// Help text
|
|
376
|
+
container.addChild(new Text(theme.fg("dim", " ↑↓ navigate • ←→ page • enter actions • esc close"), 0, 0));
|
|
377
|
+
|
|
378
|
+
// Bottom border
|
|
379
|
+
container.addChild(new DynamicBorder((s: string) => theme.fg("accent", s)));
|
|
380
|
+
|
|
381
|
+
return {
|
|
382
|
+
render: (w) => container.render(w),
|
|
383
|
+
invalidate: () => container.invalidate(),
|
|
384
|
+
handleInput: (data) => {
|
|
385
|
+
if (matchesKey(data, Key.left)) {
|
|
386
|
+
currentIndex = Math.max(0, currentIndex - visibleRows);
|
|
387
|
+
selectList.setSelectedIndex(currentIndex);
|
|
388
|
+
} else if (matchesKey(data, Key.right)) {
|
|
389
|
+
currentIndex = Math.min(items.length - 1, currentIndex + visibleRows);
|
|
390
|
+
selectList.setSelectedIndex(currentIndex);
|
|
391
|
+
} else {
|
|
392
|
+
selectList.handleInput(data);
|
|
393
|
+
}
|
|
394
|
+
tui.requestRender();
|
|
395
|
+
},
|
|
396
|
+
};
|
|
397
|
+
});
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
const showJobActions = async (ctx: ExtensionContext, job: JobResponse) => {
|
|
401
|
+
const actions: string[] = [];
|
|
402
|
+
|
|
403
|
+
if (job.status === "running") {
|
|
404
|
+
actions.push("logs", "stop", "restart");
|
|
405
|
+
} else {
|
|
406
|
+
actions.push("logs", "start", "restart", "remove");
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const cmdStr = job.command.join(" ");
|
|
410
|
+
const action = await ctx.ui.select(`${job.id}: ${cmdStr}`, actions);
|
|
411
|
+
if (!action) return;
|
|
412
|
+
|
|
413
|
+
switch (action) {
|
|
414
|
+
case "logs": {
|
|
415
|
+
const result = await pi.exec("gob", ["logs", "--tail", "50", job.id], { timeout: 5000 });
|
|
416
|
+
if (result.code === 0 && result.stdout.trim()) {
|
|
417
|
+
ctx.ui.notify(`Logs for ${job.id}:\n${result.stdout.trim()}`, "info");
|
|
418
|
+
} else {
|
|
419
|
+
ctx.ui.notify(`No logs for ${job.id}`, "info");
|
|
420
|
+
}
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
case "stop":
|
|
424
|
+
case "start":
|
|
425
|
+
case "restart":
|
|
426
|
+
case "remove": {
|
|
427
|
+
const result = await executeJobAction(action, job.id);
|
|
428
|
+
ctx.ui.notify(result.message, result.success ? "info" : "error");
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
// Register /gob command
|
|
435
|
+
pi.registerCommand("gob", {
|
|
436
|
+
description: "View and manage gob background jobs",
|
|
437
|
+
handler: async (_args, ctx) => showJobList(ctx),
|
|
438
|
+
});
|
|
439
|
+
}
|
package/src/segment.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Powerbar segment formatting for gob running jobs.
|
|
3
|
+
*
|
|
4
|
+
* Each job with historical run data renders its own progress bar.
|
|
5
|
+
* Jobs without progress data are grouped: 1 shows its command, N shows "N jobs".
|
|
6
|
+
* Parts are joined with " · ".
|
|
7
|
+
*
|
|
8
|
+
* Examples:
|
|
9
|
+
* 1 job, no history: "⚙ npm run dev"
|
|
10
|
+
* 1 job, with history: "⚙ npm run dev ████░░░░ 57%"
|
|
11
|
+
* mixed: "⚙ npm run dev ████░░░░ 57% · 2 jobs"
|
|
12
|
+
* all with history: "⚙ npm run dev ████░░░░ 57% · make build ████████ 100%"
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { JobResponse } from "./types.js";
|
|
16
|
+
|
|
17
|
+
const BAR_WIDTH = 8;
|
|
18
|
+
|
|
19
|
+
/** Display label for a job: description if set, otherwise the raw command. */
|
|
20
|
+
function jobLabel(job: JobResponse): string {
|
|
21
|
+
return job.description || job.command.join(" ");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface SegmentData {
|
|
25
|
+
text: string;
|
|
26
|
+
icon: string;
|
|
27
|
+
color: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Compute progress percentage for a running job with known average duration.
|
|
32
|
+
* Returns undefined if no historical data. Clamps to 0–100.
|
|
33
|
+
*/
|
|
34
|
+
function jobProgress(job: JobResponse): number | undefined {
|
|
35
|
+
if (job.avg_duration_ms <= 0 || !job.started_at) return undefined;
|
|
36
|
+
|
|
37
|
+
const start = new Date(job.started_at).getTime();
|
|
38
|
+
const elapsedMs = Date.now() - start;
|
|
39
|
+
const pct = Math.round((elapsedMs / job.avg_duration_ms) * 100);
|
|
40
|
+
|
|
41
|
+
return Math.max(0, Math.min(100, pct));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Render a progress bar using block characters.
|
|
46
|
+
* "████░░░░ 57%" or "████████ 100%"
|
|
47
|
+
*/
|
|
48
|
+
function renderBar(pct: number): string {
|
|
49
|
+
const filled = Math.round((pct / 100) * BAR_WIDTH);
|
|
50
|
+
const empty = BAR_WIDTH - filled;
|
|
51
|
+
return `${"█".repeat(filled)}${"░".repeat(empty)} ${pct}%`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Format running jobs into a powerbar segment payload.
|
|
56
|
+
*/
|
|
57
|
+
export function formatJobSegment(jobs: JobResponse[]): SegmentData {
|
|
58
|
+
if (jobs.length === 0) {
|
|
59
|
+
return { text: "", icon: "⚙", color: "muted" };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const withBar: string[] = [];
|
|
63
|
+
const withoutBar: JobResponse[] = [];
|
|
64
|
+
for (const job of jobs) {
|
|
65
|
+
const pct = jobProgress(job);
|
|
66
|
+
if (pct !== undefined) {
|
|
67
|
+
withBar.push(`${jobLabel(job)} ${renderBar(pct)}`);
|
|
68
|
+
} else {
|
|
69
|
+
withoutBar.push(job);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const parts: string[] = [...withBar];
|
|
74
|
+
|
|
75
|
+
if (withoutBar.length === 1) {
|
|
76
|
+
parts.push(jobLabel(withoutBar[0]));
|
|
77
|
+
} else if (withoutBar.length > 1) {
|
|
78
|
+
parts.push(`${withoutBar.length} jobs`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return { text: parts.join(" · "), icon: "⚙", color: "muted" };
|
|
82
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeScript types mirroring the gob daemon protocol.
|
|
3
|
+
* See gob/internal/daemon/protocol.go for the canonical definitions.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Request types
|
|
7
|
+
export type RequestType =
|
|
8
|
+
| "ping"
|
|
9
|
+
| "shutdown"
|
|
10
|
+
| "list"
|
|
11
|
+
| "add"
|
|
12
|
+
| "create"
|
|
13
|
+
| "stop"
|
|
14
|
+
| "start"
|
|
15
|
+
| "restart"
|
|
16
|
+
| "remove"
|
|
17
|
+
| "stop_all"
|
|
18
|
+
| "signal"
|
|
19
|
+
| "get_job"
|
|
20
|
+
| "runs"
|
|
21
|
+
| "stats"
|
|
22
|
+
| "subscribe"
|
|
23
|
+
| "version"
|
|
24
|
+
| "ports"
|
|
25
|
+
| "remove_run";
|
|
26
|
+
|
|
27
|
+
// Event types
|
|
28
|
+
export type EventType =
|
|
29
|
+
| "job_added"
|
|
30
|
+
| "job_started"
|
|
31
|
+
| "job_stopped"
|
|
32
|
+
| "job_removed"
|
|
33
|
+
| "job_updated"
|
|
34
|
+
| "run_started"
|
|
35
|
+
| "run_stopped"
|
|
36
|
+
| "run_removed"
|
|
37
|
+
| "ports_updated";
|
|
38
|
+
|
|
39
|
+
// Protocol messages
|
|
40
|
+
export interface Request {
|
|
41
|
+
type: RequestType;
|
|
42
|
+
payload?: Record<string, unknown>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface Response {
|
|
46
|
+
success: boolean;
|
|
47
|
+
error?: string;
|
|
48
|
+
data?: Record<string, unknown>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Port information
|
|
52
|
+
export interface PortInfo {
|
|
53
|
+
port: number;
|
|
54
|
+
protocol: string;
|
|
55
|
+
pid: number;
|
|
56
|
+
address: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Job response from daemon
|
|
60
|
+
export interface JobResponse {
|
|
61
|
+
id: string;
|
|
62
|
+
pid: number;
|
|
63
|
+
status: string;
|
|
64
|
+
command: string[];
|
|
65
|
+
workdir: string;
|
|
66
|
+
description?: string;
|
|
67
|
+
blocked?: boolean;
|
|
68
|
+
created_at: string;
|
|
69
|
+
started_at: string;
|
|
70
|
+
stopped_at?: string;
|
|
71
|
+
stdout_path: string;
|
|
72
|
+
stderr_path: string;
|
|
73
|
+
exit_code?: number | null;
|
|
74
|
+
ports?: PortInfo[];
|
|
75
|
+
|
|
76
|
+
// Statistics
|
|
77
|
+
run_count: number;
|
|
78
|
+
success_count: number;
|
|
79
|
+
failure_count: number;
|
|
80
|
+
success_rate: number;
|
|
81
|
+
avg_duration_ms: number;
|
|
82
|
+
failure_avg_duration_ms: number;
|
|
83
|
+
min_duration_ms: number;
|
|
84
|
+
max_duration_ms: number;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Run response from daemon
|
|
88
|
+
export interface RunResponse {
|
|
89
|
+
id: string;
|
|
90
|
+
job_id: string;
|
|
91
|
+
pid: number;
|
|
92
|
+
status: string;
|
|
93
|
+
exit_code?: number | null;
|
|
94
|
+
stdout_path: string;
|
|
95
|
+
stderr_path: string;
|
|
96
|
+
started_at: string;
|
|
97
|
+
stopped_at?: string;
|
|
98
|
+
duration_ms: number;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Event from subscription stream
|
|
102
|
+
export interface Event {
|
|
103
|
+
type: EventType;
|
|
104
|
+
job_id: string;
|
|
105
|
+
job: JobResponse;
|
|
106
|
+
run?: RunResponse;
|
|
107
|
+
ports?: PortInfo[];
|
|
108
|
+
job_count: number;
|
|
109
|
+
running_job_count: number;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Job ports response
|
|
113
|
+
export interface JobPorts {
|
|
114
|
+
job_id: string;
|
|
115
|
+
pid: number;
|
|
116
|
+
ports: PortInfo[];
|
|
117
|
+
status?: string;
|
|
118
|
+
message?: string;
|
|
119
|
+
}
|
package/dist/daemon-client.d.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Daemon client for connecting to gob daemon via Unix socket.
|
|
3
|
-
* Uses the newline-delimited JSON protocol defined in gob's daemon package.
|
|
4
|
-
*/
|
|
5
|
-
import type { Event, JobResponse, Response } from "./types.js";
|
|
6
|
-
export declare class DaemonClient {
|
|
7
|
-
private socketPath;
|
|
8
|
-
private subscriptionConn;
|
|
9
|
-
private _connected;
|
|
10
|
-
constructor();
|
|
11
|
-
get connected(): boolean;
|
|
12
|
-
/**
|
|
13
|
-
* Probe the daemon socket and verify it's reachable.
|
|
14
|
-
* Does NOT auto-start the daemon.
|
|
15
|
-
* Returns true if daemon is running, false otherwise.
|
|
16
|
-
*/
|
|
17
|
-
connect(): Promise<boolean>;
|
|
18
|
-
/**
|
|
19
|
-
* Send a request to the daemon over an ephemeral connection.
|
|
20
|
-
*/
|
|
21
|
-
sendRequest(type: string, payload?: Record<string, unknown>): Promise<Response>;
|
|
22
|
-
/**
|
|
23
|
-
* List jobs, optionally filtered by workdir.
|
|
24
|
-
*/
|
|
25
|
-
list(workdir?: string): Promise<JobResponse[]>;
|
|
26
|
-
/**
|
|
27
|
-
* Subscribe to daemon events. Returns an unsubscribe function.
|
|
28
|
-
* The subscription uses a persistent connection that streams events.
|
|
29
|
-
*
|
|
30
|
-
* @param workdir - Filter events to jobs in this workdir
|
|
31
|
-
* @param onEvent - Callback for each event
|
|
32
|
-
* @param onError - Callback when the subscription disconnects
|
|
33
|
-
*/
|
|
34
|
-
subscribe(workdir: string | undefined, onEvent: (event: Event) => void, onError: (err: Error) => void): () => void;
|
|
35
|
-
/**
|
|
36
|
-
* Close the subscription connection.
|
|
37
|
-
*/
|
|
38
|
-
private closeSubscription;
|
|
39
|
-
/**
|
|
40
|
-
* Disconnect from the daemon, closing the subscription.
|
|
41
|
-
*/
|
|
42
|
-
disconnect(): void;
|
|
43
|
-
}
|
|
44
|
-
//# sourceMappingURL=daemon-client.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"daemon-client.d.ts","sourceRoot":"","sources":["../src/daemon-client.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,KAAK,EAAE,KAAK,EAAE,WAAW,EAAW,QAAQ,EAAE,MAAM,YAAY,CAAC;AAyExE,qBAAa,YAAY;IACxB,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,gBAAgB,CAA2B;IACnD,OAAO,CAAC,UAAU,CAAS;;IAM3B,IAAI,SAAS,IAAI,OAAO,CAEvB;IAED;;;;OAIG;IACG,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC;IAgBjC;;OAEG;IACG,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC;IAQrF;;OAEG;IACG,IAAI,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAgBpD;;;;;;;OAOG;IACH,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,EAAE,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,EAAE,OAAO,EAAE,CAAC,GAAG,EAAE,KAAK,KAAK,IAAI,GAAG,MAAM,IAAI;IAqElH;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAOzB;;OAEG;IACH,UAAU,IAAI,IAAI;CAIlB"}
|