@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/package.json
CHANGED
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@juanibiapina/pi-gob",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Pi extension for managing gob background jobs",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "dist/index.js",
|
|
7
|
-
"types": "./dist/index.d.ts",
|
|
8
6
|
"scripts": {
|
|
9
|
-
"
|
|
10
|
-
"build": "tsc -p tsconfig.build.json",
|
|
11
|
-
"dev": "tsc -p tsconfig.build.json --watch",
|
|
12
|
-
"check": "biome check --write --error-on-warnings . && tsc --noEmit",
|
|
13
|
-
"prepublishOnly": "npm run clean && npm run build && npm run check"
|
|
7
|
+
"check": "biome check --write --error-on-warnings . && tsc --noEmit"
|
|
14
8
|
},
|
|
15
9
|
"files": [
|
|
16
|
-
"
|
|
10
|
+
"src/**/*",
|
|
17
11
|
"README.md"
|
|
18
12
|
],
|
|
19
13
|
"keywords": [
|
|
@@ -25,7 +19,7 @@
|
|
|
25
19
|
],
|
|
26
20
|
"pi": {
|
|
27
21
|
"extensions": [
|
|
28
|
-
"./
|
|
22
|
+
"./src/index.ts"
|
|
29
23
|
]
|
|
30
24
|
},
|
|
31
25
|
"author": "Juan Ibiapina",
|
|
@@ -37,14 +31,10 @@
|
|
|
37
31
|
"engines": {
|
|
38
32
|
"node": ">=20.0.0"
|
|
39
33
|
},
|
|
40
|
-
"peerDependencies": {
|
|
41
|
-
"@mariozechner/pi-coding-agent": "*",
|
|
42
|
-
"@mariozechner/pi-tui": "*"
|
|
43
|
-
},
|
|
44
34
|
"devDependencies": {
|
|
45
|
-
"@biomejs/biome": "2.4.
|
|
46
|
-
"@
|
|
47
|
-
"@types/node": "^25.
|
|
48
|
-
"typescript": "^6.0.
|
|
35
|
+
"@biomejs/biome": "2.4.15",
|
|
36
|
+
"@earendil-works/pi-coding-agent": "^0.75.3",
|
|
37
|
+
"@types/node": "^25.9.1",
|
|
38
|
+
"typescript": "^6.0.3"
|
|
49
39
|
}
|
|
50
40
|
}
|
|
@@ -0,0 +1,240 @@
|
|
|
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
|
+
|
|
6
|
+
import * as net from "node:net";
|
|
7
|
+
import * as os from "node:os";
|
|
8
|
+
import * as path from "node:path";
|
|
9
|
+
import type { Event, JobResponse, Request, Response } from "./types.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Get the gob daemon socket path.
|
|
13
|
+
* Uses $XDG_RUNTIME_DIR/gob/daemon.sock if set.
|
|
14
|
+
* Falls back to ~/Library/Application Support/gob/daemon.sock on macOS
|
|
15
|
+
* (matching adrg/xdg Go library behavior).
|
|
16
|
+
* Falls back to /run/user/<uid>/gob/daemon.sock on Linux.
|
|
17
|
+
*/
|
|
18
|
+
function getSocketPath(): string {
|
|
19
|
+
const xdgRuntime = process.env.XDG_RUNTIME_DIR;
|
|
20
|
+
if (xdgRuntime) {
|
|
21
|
+
return path.join(xdgRuntime, "gob", "daemon.sock");
|
|
22
|
+
}
|
|
23
|
+
// macOS fallback — adrg/xdg uses ~/Library/Application Support as RuntimeDir
|
|
24
|
+
if (process.platform === "darwin") {
|
|
25
|
+
return path.join(os.homedir(), "Library", "Application Support", "gob", "daemon.sock");
|
|
26
|
+
}
|
|
27
|
+
// Linux fallback if XDG_RUNTIME_DIR not set
|
|
28
|
+
return path.join("/run/user", String(process.getuid?.()), "gob", "daemon.sock");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Send a single request over a new ephemeral connection and return the response.
|
|
33
|
+
* The daemon closes the connection after each response (except subscribe).
|
|
34
|
+
*/
|
|
35
|
+
async function sendEphemeralRequest(socketPath: string, req: Request): Promise<Response> {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
const conn = net.createConnection(socketPath);
|
|
38
|
+
let buffer = "";
|
|
39
|
+
|
|
40
|
+
conn.on("connect", () => {
|
|
41
|
+
conn.write(`${JSON.stringify(req)}\n`);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
conn.on("data", (data) => {
|
|
45
|
+
buffer += data.toString();
|
|
46
|
+
const newlineIdx = buffer.indexOf("\n");
|
|
47
|
+
if (newlineIdx !== -1) {
|
|
48
|
+
const line = buffer.slice(0, newlineIdx);
|
|
49
|
+
try {
|
|
50
|
+
const resp = JSON.parse(line) as Response;
|
|
51
|
+
conn.destroy();
|
|
52
|
+
resolve(resp);
|
|
53
|
+
} catch (err) {
|
|
54
|
+
conn.destroy();
|
|
55
|
+
reject(new Error(`Failed to parse response: ${err}`));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
conn.on("error", (err) => {
|
|
61
|
+
reject(err);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
conn.on("close", () => {
|
|
65
|
+
if (buffer.trim()) {
|
|
66
|
+
try {
|
|
67
|
+
resolve(JSON.parse(buffer.trim()) as Response);
|
|
68
|
+
} catch {
|
|
69
|
+
reject(new Error("Connection closed before complete response"));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Timeout after 5 seconds
|
|
75
|
+
conn.setTimeout(5000, () => {
|
|
76
|
+
conn.destroy();
|
|
77
|
+
reject(new Error("Connection timed out"));
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export class DaemonClient {
|
|
83
|
+
private socketPath: string;
|
|
84
|
+
private subscriptionConn: net.Socket | null = null;
|
|
85
|
+
private _connected = false;
|
|
86
|
+
|
|
87
|
+
constructor() {
|
|
88
|
+
this.socketPath = getSocketPath();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
get connected(): boolean {
|
|
92
|
+
return this._connected;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Probe the daemon socket and verify it's reachable.
|
|
97
|
+
* Does NOT auto-start the daemon.
|
|
98
|
+
* Returns true if daemon is running, false otherwise.
|
|
99
|
+
*/
|
|
100
|
+
async connect(): Promise<boolean> {
|
|
101
|
+
try {
|
|
102
|
+
const resp = await sendEphemeralRequest(this.socketPath, {
|
|
103
|
+
type: "version",
|
|
104
|
+
payload: { client_version: "pi-extension" },
|
|
105
|
+
});
|
|
106
|
+
if (resp.success) {
|
|
107
|
+
this._connected = true;
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
return false;
|
|
111
|
+
} catch {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Send a request to the daemon over an ephemeral connection.
|
|
118
|
+
*/
|
|
119
|
+
async sendRequest(type: string, payload?: Record<string, unknown>): Promise<Response> {
|
|
120
|
+
const req: Request = { type: type as Request["type"] };
|
|
121
|
+
if (payload) {
|
|
122
|
+
req.payload = payload;
|
|
123
|
+
}
|
|
124
|
+
return sendEphemeralRequest(this.socketPath, req);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* List jobs, optionally filtered by workdir.
|
|
129
|
+
*/
|
|
130
|
+
async list(workdir?: string): Promise<JobResponse[]> {
|
|
131
|
+
const payload: Record<string, unknown> = {};
|
|
132
|
+
if (workdir) {
|
|
133
|
+
payload.workdir = workdir;
|
|
134
|
+
}
|
|
135
|
+
const resp = await this.sendRequest("list", payload);
|
|
136
|
+
if (!resp.success) {
|
|
137
|
+
throw new Error(`list failed: ${resp.error}`);
|
|
138
|
+
}
|
|
139
|
+
const jobs = resp.data?.jobs;
|
|
140
|
+
if (!jobs || !Array.isArray(jobs)) {
|
|
141
|
+
return [];
|
|
142
|
+
}
|
|
143
|
+
return jobs as JobResponse[];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Subscribe to daemon events. Returns an unsubscribe function.
|
|
148
|
+
* The subscription uses a persistent connection that streams events.
|
|
149
|
+
*
|
|
150
|
+
* @param workdir - Filter events to jobs in this workdir
|
|
151
|
+
* @param onEvent - Callback for each event
|
|
152
|
+
* @param onError - Callback when the subscription disconnects
|
|
153
|
+
*/
|
|
154
|
+
subscribe(workdir: string | undefined, onEvent: (event: Event) => void, onError: (err: Error) => void): () => void {
|
|
155
|
+
// Close existing subscription if any
|
|
156
|
+
this.closeSubscription();
|
|
157
|
+
|
|
158
|
+
const conn = net.createConnection(this.socketPath);
|
|
159
|
+
this.subscriptionConn = conn;
|
|
160
|
+
let buffer = "";
|
|
161
|
+
let gotInitialResponse = false;
|
|
162
|
+
|
|
163
|
+
conn.on("connect", () => {
|
|
164
|
+
const req: Request = { type: "subscribe" };
|
|
165
|
+
if (workdir) {
|
|
166
|
+
req.payload = { workdir };
|
|
167
|
+
}
|
|
168
|
+
conn.write(`${JSON.stringify(req)}\n`);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
conn.on("data", (data) => {
|
|
172
|
+
buffer += data.toString();
|
|
173
|
+
// Process all complete lines
|
|
174
|
+
for (let newlineIdx = buffer.indexOf("\n"); newlineIdx !== -1; newlineIdx = buffer.indexOf("\n")) {
|
|
175
|
+
const line = buffer.slice(0, newlineIdx);
|
|
176
|
+
buffer = buffer.slice(newlineIdx + 1);
|
|
177
|
+
|
|
178
|
+
if (!line.trim()) continue;
|
|
179
|
+
|
|
180
|
+
try {
|
|
181
|
+
const parsed = JSON.parse(line);
|
|
182
|
+
|
|
183
|
+
if (!gotInitialResponse) {
|
|
184
|
+
// First message is the subscribe response
|
|
185
|
+
gotInitialResponse = true;
|
|
186
|
+
if (!parsed.success) {
|
|
187
|
+
conn.destroy();
|
|
188
|
+
onError(new Error(`Subscribe failed: ${parsed.error}`));
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Subsequent messages are events
|
|
195
|
+
onEvent(parsed as Event);
|
|
196
|
+
} catch {
|
|
197
|
+
// Skip malformed lines
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
conn.on("error", (err) => {
|
|
203
|
+
this.subscriptionConn = null;
|
|
204
|
+
onError(err);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
conn.on("close", () => {
|
|
208
|
+
const wasSubscription = this.subscriptionConn === conn;
|
|
209
|
+
if (wasSubscription) {
|
|
210
|
+
this.subscriptionConn = null;
|
|
211
|
+
onError(new Error("Subscription connection closed"));
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// Return unsubscribe function
|
|
216
|
+
return () => {
|
|
217
|
+
if (this.subscriptionConn === conn) {
|
|
218
|
+
this.closeSubscription();
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Close the subscription connection.
|
|
225
|
+
*/
|
|
226
|
+
private closeSubscription(): void {
|
|
227
|
+
if (this.subscriptionConn) {
|
|
228
|
+
this.subscriptionConn.destroy();
|
|
229
|
+
this.subscriptionConn = null;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Disconnect from the daemon, closing the subscription.
|
|
235
|
+
*/
|
|
236
|
+
disconnect(): void {
|
|
237
|
+
this.closeSubscription();
|
|
238
|
+
this._connected = false;
|
|
239
|
+
}
|
|
240
|
+
}
|