@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/dist/daemon-client.js
DELETED
|
@@ -1,218 +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 * as net from "node:net";
|
|
6
|
-
import * as os from "node:os";
|
|
7
|
-
import * as path from "node:path";
|
|
8
|
-
/**
|
|
9
|
-
* Get the gob daemon socket path.
|
|
10
|
-
* Uses $XDG_RUNTIME_DIR/gob/daemon.sock if set.
|
|
11
|
-
* Falls back to ~/Library/Application Support/gob/daemon.sock on macOS
|
|
12
|
-
* (matching adrg/xdg Go library behavior).
|
|
13
|
-
* Falls back to /run/user/<uid>/gob/daemon.sock on Linux.
|
|
14
|
-
*/
|
|
15
|
-
function getSocketPath() {
|
|
16
|
-
const xdgRuntime = process.env.XDG_RUNTIME_DIR;
|
|
17
|
-
if (xdgRuntime) {
|
|
18
|
-
return path.join(xdgRuntime, "gob", "daemon.sock");
|
|
19
|
-
}
|
|
20
|
-
// macOS fallback — adrg/xdg uses ~/Library/Application Support as RuntimeDir
|
|
21
|
-
if (process.platform === "darwin") {
|
|
22
|
-
return path.join(os.homedir(), "Library", "Application Support", "gob", "daemon.sock");
|
|
23
|
-
}
|
|
24
|
-
// Linux fallback if XDG_RUNTIME_DIR not set
|
|
25
|
-
return path.join("/run/user", String(process.getuid?.()), "gob", "daemon.sock");
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Send a single request over a new ephemeral connection and return the response.
|
|
29
|
-
* The daemon closes the connection after each response (except subscribe).
|
|
30
|
-
*/
|
|
31
|
-
async function sendEphemeralRequest(socketPath, req) {
|
|
32
|
-
return new Promise((resolve, reject) => {
|
|
33
|
-
const conn = net.createConnection(socketPath);
|
|
34
|
-
let buffer = "";
|
|
35
|
-
conn.on("connect", () => {
|
|
36
|
-
conn.write(`${JSON.stringify(req)}\n`);
|
|
37
|
-
});
|
|
38
|
-
conn.on("data", (data) => {
|
|
39
|
-
buffer += data.toString();
|
|
40
|
-
const newlineIdx = buffer.indexOf("\n");
|
|
41
|
-
if (newlineIdx !== -1) {
|
|
42
|
-
const line = buffer.slice(0, newlineIdx);
|
|
43
|
-
try {
|
|
44
|
-
const resp = JSON.parse(line);
|
|
45
|
-
conn.destroy();
|
|
46
|
-
resolve(resp);
|
|
47
|
-
}
|
|
48
|
-
catch (err) {
|
|
49
|
-
conn.destroy();
|
|
50
|
-
reject(new Error(`Failed to parse response: ${err}`));
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
conn.on("error", (err) => {
|
|
55
|
-
reject(err);
|
|
56
|
-
});
|
|
57
|
-
conn.on("close", () => {
|
|
58
|
-
if (buffer.trim()) {
|
|
59
|
-
try {
|
|
60
|
-
resolve(JSON.parse(buffer.trim()));
|
|
61
|
-
}
|
|
62
|
-
catch {
|
|
63
|
-
reject(new Error("Connection closed before complete response"));
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
// Timeout after 5 seconds
|
|
68
|
-
conn.setTimeout(5000, () => {
|
|
69
|
-
conn.destroy();
|
|
70
|
-
reject(new Error("Connection timed out"));
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
export class DaemonClient {
|
|
75
|
-
socketPath;
|
|
76
|
-
subscriptionConn = null;
|
|
77
|
-
_connected = false;
|
|
78
|
-
constructor() {
|
|
79
|
-
this.socketPath = getSocketPath();
|
|
80
|
-
}
|
|
81
|
-
get connected() {
|
|
82
|
-
return this._connected;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Probe the daemon socket and verify it's reachable.
|
|
86
|
-
* Does NOT auto-start the daemon.
|
|
87
|
-
* Returns true if daemon is running, false otherwise.
|
|
88
|
-
*/
|
|
89
|
-
async connect() {
|
|
90
|
-
try {
|
|
91
|
-
const resp = await sendEphemeralRequest(this.socketPath, {
|
|
92
|
-
type: "version",
|
|
93
|
-
payload: { client_version: "pi-extension" },
|
|
94
|
-
});
|
|
95
|
-
if (resp.success) {
|
|
96
|
-
this._connected = true;
|
|
97
|
-
return true;
|
|
98
|
-
}
|
|
99
|
-
return false;
|
|
100
|
-
}
|
|
101
|
-
catch {
|
|
102
|
-
return false;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Send a request to the daemon over an ephemeral connection.
|
|
107
|
-
*/
|
|
108
|
-
async sendRequest(type, payload) {
|
|
109
|
-
const req = { type: type };
|
|
110
|
-
if (payload) {
|
|
111
|
-
req.payload = payload;
|
|
112
|
-
}
|
|
113
|
-
return sendEphemeralRequest(this.socketPath, req);
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* List jobs, optionally filtered by workdir.
|
|
117
|
-
*/
|
|
118
|
-
async list(workdir) {
|
|
119
|
-
const payload = {};
|
|
120
|
-
if (workdir) {
|
|
121
|
-
payload.workdir = workdir;
|
|
122
|
-
}
|
|
123
|
-
const resp = await this.sendRequest("list", payload);
|
|
124
|
-
if (!resp.success) {
|
|
125
|
-
throw new Error(`list failed: ${resp.error}`);
|
|
126
|
-
}
|
|
127
|
-
const jobs = resp.data?.jobs;
|
|
128
|
-
if (!jobs || !Array.isArray(jobs)) {
|
|
129
|
-
return [];
|
|
130
|
-
}
|
|
131
|
-
return jobs;
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Subscribe to daemon events. Returns an unsubscribe function.
|
|
135
|
-
* The subscription uses a persistent connection that streams events.
|
|
136
|
-
*
|
|
137
|
-
* @param workdir - Filter events to jobs in this workdir
|
|
138
|
-
* @param onEvent - Callback for each event
|
|
139
|
-
* @param onError - Callback when the subscription disconnects
|
|
140
|
-
*/
|
|
141
|
-
subscribe(workdir, onEvent, onError) {
|
|
142
|
-
// Close existing subscription if any
|
|
143
|
-
this.closeSubscription();
|
|
144
|
-
const conn = net.createConnection(this.socketPath);
|
|
145
|
-
this.subscriptionConn = conn;
|
|
146
|
-
let buffer = "";
|
|
147
|
-
let gotInitialResponse = false;
|
|
148
|
-
conn.on("connect", () => {
|
|
149
|
-
const req = { type: "subscribe" };
|
|
150
|
-
if (workdir) {
|
|
151
|
-
req.payload = { workdir };
|
|
152
|
-
}
|
|
153
|
-
conn.write(`${JSON.stringify(req)}\n`);
|
|
154
|
-
});
|
|
155
|
-
conn.on("data", (data) => {
|
|
156
|
-
buffer += data.toString();
|
|
157
|
-
// Process all complete lines
|
|
158
|
-
for (let newlineIdx = buffer.indexOf("\n"); newlineIdx !== -1; newlineIdx = buffer.indexOf("\n")) {
|
|
159
|
-
const line = buffer.slice(0, newlineIdx);
|
|
160
|
-
buffer = buffer.slice(newlineIdx + 1);
|
|
161
|
-
if (!line.trim())
|
|
162
|
-
continue;
|
|
163
|
-
try {
|
|
164
|
-
const parsed = JSON.parse(line);
|
|
165
|
-
if (!gotInitialResponse) {
|
|
166
|
-
// First message is the subscribe response
|
|
167
|
-
gotInitialResponse = true;
|
|
168
|
-
if (!parsed.success) {
|
|
169
|
-
conn.destroy();
|
|
170
|
-
onError(new Error(`Subscribe failed: ${parsed.error}`));
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
continue;
|
|
174
|
-
}
|
|
175
|
-
// Subsequent messages are events
|
|
176
|
-
onEvent(parsed);
|
|
177
|
-
}
|
|
178
|
-
catch {
|
|
179
|
-
// Skip malformed lines
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
conn.on("error", (err) => {
|
|
184
|
-
this.subscriptionConn = null;
|
|
185
|
-
onError(err);
|
|
186
|
-
});
|
|
187
|
-
conn.on("close", () => {
|
|
188
|
-
const wasSubscription = this.subscriptionConn === conn;
|
|
189
|
-
if (wasSubscription) {
|
|
190
|
-
this.subscriptionConn = null;
|
|
191
|
-
onError(new Error("Subscription connection closed"));
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
// Return unsubscribe function
|
|
195
|
-
return () => {
|
|
196
|
-
if (this.subscriptionConn === conn) {
|
|
197
|
-
this.closeSubscription();
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Close the subscription connection.
|
|
203
|
-
*/
|
|
204
|
-
closeSubscription() {
|
|
205
|
-
if (this.subscriptionConn) {
|
|
206
|
-
this.subscriptionConn.destroy();
|
|
207
|
-
this.subscriptionConn = null;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Disconnect from the daemon, closing the subscription.
|
|
212
|
-
*/
|
|
213
|
-
disconnect() {
|
|
214
|
-
this.closeSubscription();
|
|
215
|
-
this._connected = false;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
//# sourceMappingURL=daemon-client.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"daemon-client.js","sourceRoot":"","sources":["../src/daemon-client.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAChC,OA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CAAC,CAAC;QAEH,8BAA8B;QAC9B,OAAO,GAAG,EAAE;YACX,IAAI,IAAI,CAAC,gBAAgB,KAAK,IAAI,EAAE,CAAC;gBACpC,IAAI,CAAC,iBAAiB,EAAE,CAAC;YAC1B,CAAC;QACF,CAAC,CAAC;IACH,CAAC;IAED;;OAEG;IACK,iBAAiB;QACxB,IAAI,IAAI,CAAC,gBAAgB,EAAE,CAAC;YAC3B,IAAI,CAAC,gBAAgB,CAAC,OAAO,EAAE,CAAC;YAChC,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC;QAC9B,CAAC;IACF,CAAC;IAED;;OAEG;IACH,UAAU;QACT,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACzB,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC;IACzB,CAAC;CACD","sourcesContent":["/**\n * Daemon client for connecting to gob daemon via Unix socket.\n * Uses the newline-delimited JSON protocol defined in gob's daemon package.\n */\n\nimport * as net from \"node:net\";\nimport * as os from \"node:os\";\nimport * as path from \"node:path\";\nimport type { Event, JobResponse, Request, Response } from \"./types.js\";\n\n/**\n * Get the gob daemon socket path.\n * Uses $XDG_RUNTIME_DIR/gob/daemon.sock if set.\n * Falls back to ~/Library/Application Support/gob/daemon.sock on macOS\n * (matching adrg/xdg Go library behavior).\n * Falls back to /run/user/<uid>/gob/daemon.sock on Linux.\n */\nfunction getSocketPath(): string {\n\tconst xdgRuntime = process.env.XDG_RUNTIME_DIR;\n\tif (xdgRuntime) {\n\t\treturn path.join(xdgRuntime, \"gob\", \"daemon.sock\");\n\t}\n\t// macOS fallback — adrg/xdg uses ~/Library/Application Support as RuntimeDir\n\tif (process.platform === \"darwin\") {\n\t\treturn path.join(os.homedir(), \"Library\", \"Application Support\", \"gob\", \"daemon.sock\");\n\t}\n\t// Linux fallback if XDG_RUNTIME_DIR not set\n\treturn path.join(\"/run/user\", String(process.getuid?.()), \"gob\", \"daemon.sock\");\n}\n\n/**\n * Send a single request over a new ephemeral connection and return the response.\n * The daemon closes the connection after each response (except subscribe).\n */\nasync function sendEphemeralRequest(socketPath: string, req: Request): Promise<Response> {\n\treturn new Promise((resolve, reject) => {\n\t\tconst conn = net.createConnection(socketPath);\n\t\tlet buffer = \"\";\n\n\t\tconn.on(\"connect\", () => {\n\t\t\tconn.write(`${JSON.stringify(req)}\\n`);\n\t\t});\n\n\t\tconn.on(\"data\", (data) => {\n\t\t\tbuffer += data.toString();\n\t\t\tconst newlineIdx = buffer.indexOf(\"\\n\");\n\t\t\tif (newlineIdx !== -1) {\n\t\t\t\tconst line = buffer.slice(0, newlineIdx);\n\t\t\t\ttry {\n\t\t\t\t\tconst resp = JSON.parse(line) as Response;\n\t\t\t\t\tconn.destroy();\n\t\t\t\t\tresolve(resp);\n\t\t\t\t} catch (err) {\n\t\t\t\t\tconn.destroy();\n\t\t\t\t\treject(new Error(`Failed to parse response: ${err}`));\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tconn.on(\"error\", (err) => {\n\t\t\treject(err);\n\t\t});\n\n\t\tconn.on(\"close\", () => {\n\t\t\tif (buffer.trim()) {\n\t\t\t\ttry {\n\t\t\t\t\tresolve(JSON.parse(buffer.trim()) as Response);\n\t\t\t\t} catch {\n\t\t\t\t\treject(new Error(\"Connection closed before complete response\"));\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\t// Timeout after 5 seconds\n\t\tconn.setTimeout(5000, () => {\n\t\t\tconn.destroy();\n\t\t\treject(new Error(\"Connection timed out\"));\n\t\t});\n\t});\n}\n\nexport class DaemonClient {\n\tprivate socketPath: string;\n\tprivate subscriptionConn: net.Socket | null = null;\n\tprivate _connected = false;\n\n\tconstructor() {\n\t\tthis.socketPath = getSocketPath();\n\t}\n\n\tget connected(): boolean {\n\t\treturn this._connected;\n\t}\n\n\t/**\n\t * Probe the daemon socket and verify it's reachable.\n\t * Does NOT auto-start the daemon.\n\t * Returns true if daemon is running, false otherwise.\n\t */\n\tasync connect(): Promise<boolean> {\n\t\ttry {\n\t\t\tconst resp = await sendEphemeralRequest(this.socketPath, {\n\t\t\t\ttype: \"version\",\n\t\t\t\tpayload: { client_version: \"pi-extension\" },\n\t\t\t});\n\t\t\tif (resp.success) {\n\t\t\t\tthis._connected = true;\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Send a request to the daemon over an ephemeral connection.\n\t */\n\tasync sendRequest(type: string, payload?: Record<string, unknown>): Promise<Response> {\n\t\tconst req: Request = { type: type as Request[\"type\"] };\n\t\tif (payload) {\n\t\t\treq.payload = payload;\n\t\t}\n\t\treturn sendEphemeralRequest(this.socketPath, req);\n\t}\n\n\t/**\n\t * List jobs, optionally filtered by workdir.\n\t */\n\tasync list(workdir?: string): Promise<JobResponse[]> {\n\t\tconst payload: Record<string, unknown> = {};\n\t\tif (workdir) {\n\t\t\tpayload.workdir = workdir;\n\t\t}\n\t\tconst resp = await this.sendRequest(\"list\", payload);\n\t\tif (!resp.success) {\n\t\t\tthrow new Error(`list failed: ${resp.error}`);\n\t\t}\n\t\tconst jobs = resp.data?.jobs;\n\t\tif (!jobs || !Array.isArray(jobs)) {\n\t\t\treturn [];\n\t\t}\n\t\treturn jobs as JobResponse[];\n\t}\n\n\t/**\n\t * Subscribe to daemon events. Returns an unsubscribe function.\n\t * The subscription uses a persistent connection that streams events.\n\t *\n\t * @param workdir - Filter events to jobs in this workdir\n\t * @param onEvent - Callback for each event\n\t * @param onError - Callback when the subscription disconnects\n\t */\n\tsubscribe(workdir: string | undefined, onEvent: (event: Event) => void, onError: (err: Error) => void): () => void {\n\t\t// Close existing subscription if any\n\t\tthis.closeSubscription();\n\n\t\tconst conn = net.createConnection(this.socketPath);\n\t\tthis.subscriptionConn = conn;\n\t\tlet buffer = \"\";\n\t\tlet gotInitialResponse = false;\n\n\t\tconn.on(\"connect\", () => {\n\t\t\tconst req: Request = { type: \"subscribe\" };\n\t\t\tif (workdir) {\n\t\t\t\treq.payload = { workdir };\n\t\t\t}\n\t\t\tconn.write(`${JSON.stringify(req)}\\n`);\n\t\t});\n\n\t\tconn.on(\"data\", (data) => {\n\t\t\tbuffer += data.toString();\n\t\t\t// Process all complete lines\n\t\t\tfor (let newlineIdx = buffer.indexOf(\"\\n\"); newlineIdx !== -1; newlineIdx = buffer.indexOf(\"\\n\")) {\n\t\t\t\tconst line = buffer.slice(0, newlineIdx);\n\t\t\t\tbuffer = buffer.slice(newlineIdx + 1);\n\n\t\t\t\tif (!line.trim()) continue;\n\n\t\t\t\ttry {\n\t\t\t\t\tconst parsed = JSON.parse(line);\n\n\t\t\t\t\tif (!gotInitialResponse) {\n\t\t\t\t\t\t// First message is the subscribe response\n\t\t\t\t\t\tgotInitialResponse = true;\n\t\t\t\t\t\tif (!parsed.success) {\n\t\t\t\t\t\t\tconn.destroy();\n\t\t\t\t\t\t\tonError(new Error(`Subscribe failed: ${parsed.error}`));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\n\t\t\t\t\t// Subsequent messages are events\n\t\t\t\t\tonEvent(parsed as Event);\n\t\t\t\t} catch {\n\t\t\t\t\t// Skip malformed lines\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tconn.on(\"error\", (err) => {\n\t\t\tthis.subscriptionConn = null;\n\t\t\tonError(err);\n\t\t});\n\n\t\tconn.on(\"close\", () => {\n\t\t\tconst wasSubscription = this.subscriptionConn === conn;\n\t\t\tif (wasSubscription) {\n\t\t\t\tthis.subscriptionConn = null;\n\t\t\t\tonError(new Error(\"Subscription connection closed\"));\n\t\t\t}\n\t\t});\n\n\t\t// Return unsubscribe function\n\t\treturn () => {\n\t\t\tif (this.subscriptionConn === conn) {\n\t\t\t\tthis.closeSubscription();\n\t\t\t}\n\t\t};\n\t}\n\n\t/**\n\t * Close the subscription connection.\n\t */\n\tprivate closeSubscription(): void {\n\t\tif (this.subscriptionConn) {\n\t\t\tthis.subscriptionConn.destroy();\n\t\t\tthis.subscriptionConn = null;\n\t\t}\n\t}\n\n\t/**\n\t * Disconnect from the daemon, closing the subscription.\n\t */\n\tdisconnect(): void {\n\t\tthis.closeSubscription();\n\t\tthis._connected = false;\n\t}\n}\n"]}
|
package/dist/index.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
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
|
-
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
10
|
-
export default function (pi: ExtensionAPI): void;
|
|
11
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAoB,MAAM,+BAA+B,CAAC;AAWpF,MAAM,CAAC,OAAO,WAAW,EAAE,EAAE,YAAY,QAkaxC"}
|
package/dist/index.js
DELETED
|
@@ -1,387 +0,0 @@
|
|
|
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
|
-
import { DynamicBorder } from "@mariozechner/pi-coding-agent";
|
|
10
|
-
import { Container, Key, matchesKey, SelectList, Text } from "@mariozechner/pi-tui";
|
|
11
|
-
import { DaemonClient } from "./daemon-client.js";
|
|
12
|
-
import { formatJobSegment } from "./segment.js";
|
|
13
|
-
const SEGMENT_ID = "gob";
|
|
14
|
-
const RECONNECT_DELAY_MS = 5000;
|
|
15
|
-
const TICK_INTERVAL_MS = 1000;
|
|
16
|
-
export default function (pi) {
|
|
17
|
-
// Register powerbar segment
|
|
18
|
-
pi.events.emit("powerbar:register-segment", { id: "gob", label: "Gob Jobs" });
|
|
19
|
-
const client = new DaemonClient();
|
|
20
|
-
// Map of running jobs by ID (only running jobs for the current workdir)
|
|
21
|
-
const runningJobs = new Map();
|
|
22
|
-
// Session CWD
|
|
23
|
-
let sessionCwd;
|
|
24
|
-
// Cleanup handles
|
|
25
|
-
let unsubscribe;
|
|
26
|
-
let reconnectTimer;
|
|
27
|
-
let tickInterval;
|
|
28
|
-
/**
|
|
29
|
-
* Emit a powerbar:update event with current running jobs.
|
|
30
|
-
*/
|
|
31
|
-
function updateSegment() {
|
|
32
|
-
const jobs = Array.from(runningJobs.values());
|
|
33
|
-
if (jobs.length === 0) {
|
|
34
|
-
// Clear the segment
|
|
35
|
-
pi.events.emit("powerbar:update", { id: SEGMENT_ID });
|
|
36
|
-
stopTick();
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
const segment = formatJobSegment(jobs);
|
|
40
|
-
pi.events.emit("powerbar:update", {
|
|
41
|
-
id: SEGMENT_ID,
|
|
42
|
-
text: segment.text,
|
|
43
|
-
icon: segment.icon,
|
|
44
|
-
color: segment.color,
|
|
45
|
-
});
|
|
46
|
-
startTick();
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Start the 1-second tick interval for updating elapsed times.
|
|
50
|
-
*/
|
|
51
|
-
function startTick() {
|
|
52
|
-
if (tickInterval)
|
|
53
|
-
return;
|
|
54
|
-
tickInterval = setInterval(() => {
|
|
55
|
-
if (runningJobs.size > 0) {
|
|
56
|
-
updateSegment();
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
stopTick();
|
|
60
|
-
}
|
|
61
|
-
}, TICK_INTERVAL_MS);
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Stop the tick interval.
|
|
65
|
-
*/
|
|
66
|
-
function stopTick() {
|
|
67
|
-
if (tickInterval) {
|
|
68
|
-
clearInterval(tickInterval);
|
|
69
|
-
tickInterval = undefined;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Handle a daemon event by updating the running jobs map.
|
|
74
|
-
*/
|
|
75
|
-
function handleEvent(event) {
|
|
76
|
-
const job = event.job;
|
|
77
|
-
switch (event.type) {
|
|
78
|
-
case "job_added":
|
|
79
|
-
case "job_started":
|
|
80
|
-
case "run_started":
|
|
81
|
-
case "job_updated":
|
|
82
|
-
case "ports_updated":
|
|
83
|
-
if (job.status === "running") {
|
|
84
|
-
runningJobs.set(job.id, job);
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
runningJobs.delete(job.id);
|
|
88
|
-
}
|
|
89
|
-
break;
|
|
90
|
-
case "job_stopped":
|
|
91
|
-
case "run_stopped":
|
|
92
|
-
runningJobs.delete(job.id);
|
|
93
|
-
break;
|
|
94
|
-
case "job_removed":
|
|
95
|
-
case "run_removed":
|
|
96
|
-
runningJobs.delete(job.id);
|
|
97
|
-
break;
|
|
98
|
-
}
|
|
99
|
-
updateSegment();
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Connect to daemon, fetch initial state, and subscribe to events.
|
|
103
|
-
*/
|
|
104
|
-
async function connectAndSubscribe() {
|
|
105
|
-
// Clear any pending reconnect
|
|
106
|
-
if (reconnectTimer) {
|
|
107
|
-
clearTimeout(reconnectTimer);
|
|
108
|
-
reconnectTimer = undefined;
|
|
109
|
-
}
|
|
110
|
-
const ok = await client.connect();
|
|
111
|
-
if (!ok) {
|
|
112
|
-
// Daemon not running, schedule reconnect
|
|
113
|
-
scheduleReconnect();
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
try {
|
|
117
|
-
// Fetch initial list of jobs
|
|
118
|
-
const jobs = await client.list(sessionCwd);
|
|
119
|
-
runningJobs.clear();
|
|
120
|
-
for (const job of jobs) {
|
|
121
|
-
if (job.status === "running") {
|
|
122
|
-
runningJobs.set(job.id, job);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
updateSegment();
|
|
126
|
-
}
|
|
127
|
-
catch {
|
|
128
|
-
// Failed to list, schedule reconnect
|
|
129
|
-
scheduleReconnect();
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
// Subscribe to events
|
|
133
|
-
unsubscribe = client.subscribe(sessionCwd, (event) => handleEvent(event), (_err) => {
|
|
134
|
-
// Subscription disconnected, try to reconnect
|
|
135
|
-
unsubscribe = undefined;
|
|
136
|
-
runningJobs.clear();
|
|
137
|
-
updateSegment();
|
|
138
|
-
scheduleReconnect();
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Schedule a reconnection attempt.
|
|
143
|
-
*/
|
|
144
|
-
function scheduleReconnect() {
|
|
145
|
-
if (reconnectTimer)
|
|
146
|
-
return;
|
|
147
|
-
reconnectTimer = setTimeout(() => {
|
|
148
|
-
reconnectTimer = undefined;
|
|
149
|
-
connectAndSubscribe();
|
|
150
|
-
}, RECONNECT_DELAY_MS);
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Cleanup all connections and timers.
|
|
154
|
-
*/
|
|
155
|
-
function cleanup() {
|
|
156
|
-
if (unsubscribe) {
|
|
157
|
-
unsubscribe();
|
|
158
|
-
unsubscribe = undefined;
|
|
159
|
-
}
|
|
160
|
-
if (reconnectTimer) {
|
|
161
|
-
clearTimeout(reconnectTimer);
|
|
162
|
-
reconnectTimer = undefined;
|
|
163
|
-
}
|
|
164
|
-
stopTick();
|
|
165
|
-
client.disconnect();
|
|
166
|
-
runningJobs.clear();
|
|
167
|
-
}
|
|
168
|
-
// ── Lifecycle ──────────────────────────────────────────
|
|
169
|
-
pi.on("session_start", async (_event, ctx) => {
|
|
170
|
-
sessionCwd = ctx.cwd;
|
|
171
|
-
await connectAndSubscribe();
|
|
172
|
-
});
|
|
173
|
-
pi.on("session_shutdown", async () => {
|
|
174
|
-
cleanup();
|
|
175
|
-
pi.events.emit("powerbar:update", { id: SEGMENT_ID });
|
|
176
|
-
});
|
|
177
|
-
// ── Job list UI ──
|
|
178
|
-
async function fetchJobs() {
|
|
179
|
-
// Try daemon protocol first
|
|
180
|
-
if (client.connected) {
|
|
181
|
-
try {
|
|
182
|
-
return await client.list(sessionCwd);
|
|
183
|
-
}
|
|
184
|
-
catch {
|
|
185
|
-
// Fall through to CLI
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
// CLI fallback
|
|
189
|
-
const result = await pi.exec("gob", ["list"]);
|
|
190
|
-
if (result.code !== 0) {
|
|
191
|
-
return [];
|
|
192
|
-
}
|
|
193
|
-
return parseGobList(result.stdout);
|
|
194
|
-
}
|
|
195
|
-
function parseGobList(output) {
|
|
196
|
-
const jobs = [];
|
|
197
|
-
const lines = output.split("\n").filter((l) => l.trim().length > 0);
|
|
198
|
-
let i = 0;
|
|
199
|
-
while (i < lines.length) {
|
|
200
|
-
const line = lines[i];
|
|
201
|
-
// Format: <job_id>: [<pid>] <status> (<exit_code>): <command>
|
|
202
|
-
const match = line.match(/^(\S+): \[(\S+)\] (\S+)(?: \((\d+)\))?: (.+)$/);
|
|
203
|
-
if (match) {
|
|
204
|
-
const description = i + 1 < lines.length && lines[i + 1].startsWith(" ") ? lines[i + 1].trim() : undefined;
|
|
205
|
-
if (description)
|
|
206
|
-
i++;
|
|
207
|
-
jobs.push({
|
|
208
|
-
id: match[1],
|
|
209
|
-
pid: Number.parseInt(match[2], 10) || 0,
|
|
210
|
-
status: match[3],
|
|
211
|
-
command: match[5].split(" "),
|
|
212
|
-
workdir: "",
|
|
213
|
-
description,
|
|
214
|
-
created_at: "",
|
|
215
|
-
started_at: "",
|
|
216
|
-
stdout_path: "",
|
|
217
|
-
stderr_path: "",
|
|
218
|
-
exit_code: match[4] ? Number.parseInt(match[4], 10) : undefined,
|
|
219
|
-
run_count: 0,
|
|
220
|
-
success_count: 0,
|
|
221
|
-
failure_count: 0,
|
|
222
|
-
success_rate: 0,
|
|
223
|
-
avg_duration_ms: 0,
|
|
224
|
-
failure_avg_duration_ms: 0,
|
|
225
|
-
min_duration_ms: 0,
|
|
226
|
-
max_duration_ms: 0,
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
i++;
|
|
230
|
-
}
|
|
231
|
-
return jobs;
|
|
232
|
-
}
|
|
233
|
-
async function executeJobAction(action, jobId) {
|
|
234
|
-
// Try daemon protocol first
|
|
235
|
-
if (client.connected) {
|
|
236
|
-
try {
|
|
237
|
-
const resp = await client.sendRequest(action, { job_id: jobId });
|
|
238
|
-
if (resp.success) {
|
|
239
|
-
const verb = action === "stop"
|
|
240
|
-
? "Stopped"
|
|
241
|
-
: action === "start"
|
|
242
|
-
? "Started"
|
|
243
|
-
: action === "restart"
|
|
244
|
-
? "Restarted"
|
|
245
|
-
: action === "remove"
|
|
246
|
-
? "Removed"
|
|
247
|
-
: action;
|
|
248
|
-
return { message: `${verb} ${jobId}`, success: true };
|
|
249
|
-
}
|
|
250
|
-
return { message: `Error: ${resp.error}`, success: false };
|
|
251
|
-
}
|
|
252
|
-
catch {
|
|
253
|
-
// Fall through to CLI
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
// CLI fallback
|
|
257
|
-
const result = await pi.exec("gob", [action, jobId]);
|
|
258
|
-
if (result.code === 0) {
|
|
259
|
-
const verb = action === "stop"
|
|
260
|
-
? "Stopped"
|
|
261
|
-
: action === "start"
|
|
262
|
-
? "Started"
|
|
263
|
-
: action === "restart"
|
|
264
|
-
? "Restarted"
|
|
265
|
-
: action === "remove"
|
|
266
|
-
? "Removed"
|
|
267
|
-
: action;
|
|
268
|
-
return { message: `${verb} ${jobId}`, success: true };
|
|
269
|
-
}
|
|
270
|
-
return { message: `Error: ${result.stderr}`, success: false };
|
|
271
|
-
}
|
|
272
|
-
const showJobList = async (ctx) => {
|
|
273
|
-
if (!ctx.hasUI) {
|
|
274
|
-
ctx.ui.notify("No UI available", "error");
|
|
275
|
-
return;
|
|
276
|
-
}
|
|
277
|
-
const jobs = await fetchJobs();
|
|
278
|
-
if (jobs.length === 0) {
|
|
279
|
-
ctx.ui.notify("No gob jobs found", "info");
|
|
280
|
-
return;
|
|
281
|
-
}
|
|
282
|
-
await ctx.ui.custom((tui, theme, _kb, done) => {
|
|
283
|
-
const container = new Container();
|
|
284
|
-
// Top border
|
|
285
|
-
container.addChild(new DynamicBorder((s) => theme.fg("accent", s)));
|
|
286
|
-
// Title
|
|
287
|
-
container.addChild(new Text(theme.fg("accent", theme.bold(" Gob Jobs")), 0, 0));
|
|
288
|
-
// Build select items
|
|
289
|
-
const items = jobs.map((job) => {
|
|
290
|
-
const statusColor = job.status === "running" ? "success" : "muted";
|
|
291
|
-
const statusIcon = job.status === "running" ? "●" : "○";
|
|
292
|
-
const exitInfo = job.status === "stopped" && job.exit_code != null ? theme.fg("dim", ` (${job.exit_code})`) : "";
|
|
293
|
-
const status = theme.fg(statusColor, statusIcon);
|
|
294
|
-
const cmdStr = job.command.join(" ");
|
|
295
|
-
const label = `${status} ${theme.fg("dim", job.id)} ${cmdStr}${exitInfo}`;
|
|
296
|
-
const description = job.description ? theme.fg("muted", job.description) : undefined;
|
|
297
|
-
return {
|
|
298
|
-
value: job.id,
|
|
299
|
-
label,
|
|
300
|
-
description,
|
|
301
|
-
};
|
|
302
|
-
});
|
|
303
|
-
const visibleRows = Math.min(jobs.length, 15);
|
|
304
|
-
let currentIndex = 0;
|
|
305
|
-
const selectList = new SelectList(items, visibleRows, {
|
|
306
|
-
selectedPrefix: (t) => theme.fg("accent", t),
|
|
307
|
-
selectedText: (t) => t,
|
|
308
|
-
description: (t) => t,
|
|
309
|
-
scrollInfo: (t) => theme.fg("dim", t),
|
|
310
|
-
noMatch: (t) => theme.fg("warning", t),
|
|
311
|
-
});
|
|
312
|
-
selectList.onSelect = async (item) => {
|
|
313
|
-
done();
|
|
314
|
-
const job = jobs.find((j) => j.id === item.value);
|
|
315
|
-
if (job) {
|
|
316
|
-
await showJobActions(ctx, job);
|
|
317
|
-
}
|
|
318
|
-
};
|
|
319
|
-
selectList.onCancel = () => done();
|
|
320
|
-
selectList.onSelectionChange = (item) => {
|
|
321
|
-
currentIndex = items.indexOf(item);
|
|
322
|
-
};
|
|
323
|
-
container.addChild(selectList);
|
|
324
|
-
// Help text
|
|
325
|
-
container.addChild(new Text(theme.fg("dim", " ↑↓ navigate • ←→ page • enter actions • esc close"), 0, 0));
|
|
326
|
-
// Bottom border
|
|
327
|
-
container.addChild(new DynamicBorder((s) => theme.fg("accent", s)));
|
|
328
|
-
return {
|
|
329
|
-
render: (w) => container.render(w),
|
|
330
|
-
invalidate: () => container.invalidate(),
|
|
331
|
-
handleInput: (data) => {
|
|
332
|
-
if (matchesKey(data, Key.left)) {
|
|
333
|
-
currentIndex = Math.max(0, currentIndex - visibleRows);
|
|
334
|
-
selectList.setSelectedIndex(currentIndex);
|
|
335
|
-
}
|
|
336
|
-
else if (matchesKey(data, Key.right)) {
|
|
337
|
-
currentIndex = Math.min(items.length - 1, currentIndex + visibleRows);
|
|
338
|
-
selectList.setSelectedIndex(currentIndex);
|
|
339
|
-
}
|
|
340
|
-
else {
|
|
341
|
-
selectList.handleInput(data);
|
|
342
|
-
}
|
|
343
|
-
tui.requestRender();
|
|
344
|
-
},
|
|
345
|
-
};
|
|
346
|
-
});
|
|
347
|
-
};
|
|
348
|
-
const showJobActions = async (ctx, job) => {
|
|
349
|
-
const actions = [];
|
|
350
|
-
if (job.status === "running") {
|
|
351
|
-
actions.push("logs", "stop", "restart");
|
|
352
|
-
}
|
|
353
|
-
else {
|
|
354
|
-
actions.push("logs", "start", "restart", "remove");
|
|
355
|
-
}
|
|
356
|
-
const cmdStr = job.command.join(" ");
|
|
357
|
-
const action = await ctx.ui.select(`${job.id}: ${cmdStr}`, actions);
|
|
358
|
-
if (!action)
|
|
359
|
-
return;
|
|
360
|
-
switch (action) {
|
|
361
|
-
case "logs": {
|
|
362
|
-
const result = await pi.exec("gob", ["logs", "--tail", "50", job.id], { timeout: 5000 });
|
|
363
|
-
if (result.code === 0 && result.stdout.trim()) {
|
|
364
|
-
ctx.ui.notify(`Logs for ${job.id}:\n${result.stdout.trim()}`, "info");
|
|
365
|
-
}
|
|
366
|
-
else {
|
|
367
|
-
ctx.ui.notify(`No logs for ${job.id}`, "info");
|
|
368
|
-
}
|
|
369
|
-
break;
|
|
370
|
-
}
|
|
371
|
-
case "stop":
|
|
372
|
-
case "start":
|
|
373
|
-
case "restart":
|
|
374
|
-
case "remove": {
|
|
375
|
-
const result = await executeJobAction(action, job.id);
|
|
376
|
-
ctx.ui.notify(result.message, result.success ? "info" : "error");
|
|
377
|
-
break;
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
};
|
|
381
|
-
// Register /gob command
|
|
382
|
-
pi.registerCommand("gob", {
|
|
383
|
-
description: "View and manage gob background jobs",
|
|
384
|
-
handler: async (_args, ctx) => showJobList(ctx),
|
|
385
|
-
});
|
|
386
|
-
}
|
|
387
|
-
//# sourceMappingURL=index.js.map
|