@thurstonsand/pi-agent-switchboard 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/index.js +261 -0
- package/package.json +21 -5
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Thurston Sandberg
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.js
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
// packages/shared/src/index.ts
|
|
2
|
+
import { execFileSync } from "node:child_process";
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
import { dirname as dirname2, join } from "node:path";
|
|
5
|
+
|
|
6
|
+
// packages/shared/src/db.ts
|
|
7
|
+
import { closeSync, existsSync, mkdirSync, openSync } from "node:fs";
|
|
8
|
+
import { createRequire } from "node:module";
|
|
9
|
+
import { dirname } from "node:path";
|
|
10
|
+
var require2 = createRequire(import.meta.url);
|
|
11
|
+
function connect(path) {
|
|
12
|
+
if (process.versions.bun) {
|
|
13
|
+
const { Database } = require2("bun:sqlite");
|
|
14
|
+
return new Database(path, { strict: false });
|
|
15
|
+
}
|
|
16
|
+
const { DatabaseSync } = require2("node:sqlite");
|
|
17
|
+
return new DatabaseSync(path);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class Db {
|
|
21
|
+
path;
|
|
22
|
+
#driver;
|
|
23
|
+
constructor(path, { create }) {
|
|
24
|
+
this.path = path;
|
|
25
|
+
if (create) {
|
|
26
|
+
mkdirSync(dirname(path), { recursive: true, mode: 448 });
|
|
27
|
+
closeSync(openSync(path, "a", 384));
|
|
28
|
+
} else if (!existsSync(path)) {
|
|
29
|
+
throw new Error(`no db at ${path}`);
|
|
30
|
+
}
|
|
31
|
+
this.#driver = connect(path);
|
|
32
|
+
this.#driver.exec("PRAGMA journal_mode = WAL; PRAGMA synchronous = FULL; PRAGMA busy_timeout = 5000; PRAGMA foreign_keys = ON;");
|
|
33
|
+
}
|
|
34
|
+
get(sql, ...params) {
|
|
35
|
+
return this.#driver.prepare(sql).get(...params) ?? undefined;
|
|
36
|
+
}
|
|
37
|
+
all(sql, ...params) {
|
|
38
|
+
return this.#driver.prepare(sql).all(...params);
|
|
39
|
+
}
|
|
40
|
+
run(sql, ...params) {
|
|
41
|
+
this.#driver.prepare(sql).run(...params);
|
|
42
|
+
}
|
|
43
|
+
exec(sql) {
|
|
44
|
+
this.#driver.exec(sql);
|
|
45
|
+
}
|
|
46
|
+
userVersion() {
|
|
47
|
+
return Number(this.get("PRAGMA user_version")?.user_version);
|
|
48
|
+
}
|
|
49
|
+
dataVersion() {
|
|
50
|
+
return Number(this.get("PRAGMA data_version")?.data_version);
|
|
51
|
+
}
|
|
52
|
+
tx(body) {
|
|
53
|
+
this.#driver.exec("BEGIN IMMEDIATE");
|
|
54
|
+
try {
|
|
55
|
+
const result = body();
|
|
56
|
+
this.#driver.exec("COMMIT");
|
|
57
|
+
return result;
|
|
58
|
+
} catch (error) {
|
|
59
|
+
this.#driver.exec("ROLLBACK");
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
close() {
|
|
64
|
+
this.#driver.close();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// packages/shared/src/index.ts
|
|
69
|
+
var SCHEMA_VERSION = 1;
|
|
70
|
+
function bootId() {
|
|
71
|
+
if (process.platform === "darwin")
|
|
72
|
+
return execFileSync("/usr/sbin/sysctl", ["-n", "kern.bootsessionuuid"], { encoding: "utf8" }).trim();
|
|
73
|
+
return readFileSync("/proc/sys/kernel/random/boot_id", "utf8").trim();
|
|
74
|
+
}
|
|
75
|
+
function pidAlive(pid) {
|
|
76
|
+
try {
|
|
77
|
+
process.kill(pid, 0);
|
|
78
|
+
return true;
|
|
79
|
+
} catch (error) {
|
|
80
|
+
return error.code === "EPERM";
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function git(cwd, ...args) {
|
|
84
|
+
try {
|
|
85
|
+
return execFileSync("git", ["-C", cwd, ...args], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
|
|
86
|
+
} catch {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function projectRoot(cwd) {
|
|
91
|
+
const common = git(cwd, "rev-parse", "--path-format=absolute", "--git-common-dir");
|
|
92
|
+
return common ? dirname2(common) : cwd;
|
|
93
|
+
}
|
|
94
|
+
function branch(cwd) {
|
|
95
|
+
const name = git(cwd, "rev-parse", "--abbrev-ref", "HEAD");
|
|
96
|
+
return name === "HEAD" ? git(cwd, "rev-parse", "--short", "HEAD") : name;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// packages/pi/src/index.ts
|
|
100
|
+
var KEY = Symbol.for("agent-switchboard.recorder");
|
|
101
|
+
var store = globalThis;
|
|
102
|
+
function claim() {
|
|
103
|
+
const env = process.env;
|
|
104
|
+
if (env.SWB_MANAGED === "1" && env.SWB_DB && env.SWB_TMUX_SESSION) {
|
|
105
|
+
store[KEY] = { launch: Object.freeze({ db: env.SWB_DB, tmuxSession: env.SWB_TMUX_SESSION }), attention: new Set };
|
|
106
|
+
}
|
|
107
|
+
delete env.SWB_MANAGED;
|
|
108
|
+
delete env.SWB_DB;
|
|
109
|
+
delete env.SWB_TMUX_SESSION;
|
|
110
|
+
return store[KEY];
|
|
111
|
+
}
|
|
112
|
+
function liveHost(row) {
|
|
113
|
+
if (row && row.boot_id === bootId() && pidAlive(row.pid))
|
|
114
|
+
return row.tmux_session;
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
function hostOfSession(db, proc, sessionId) {
|
|
118
|
+
return liveHost(db.get("SELECT tmux_session, boot_id, pid FROM runtimes WHERE session_id = ? AND tmux_session != ?", sessionId, proc.launch.tmuxSession));
|
|
119
|
+
}
|
|
120
|
+
function hostOfTranscript(db, proc, transcript) {
|
|
121
|
+
return liveHost(db.get("SELECT r.tmux_session, r.boot_id, r.pid FROM runtimes r JOIN sessions s USING (session_id) WHERE s.transcript = ? AND r.tmux_session != ?", transcript, proc.launch.tmuxSession));
|
|
122
|
+
}
|
|
123
|
+
function src_default(pi) {
|
|
124
|
+
const claimed = claim();
|
|
125
|
+
if (!claimed)
|
|
126
|
+
return;
|
|
127
|
+
const proc = claimed;
|
|
128
|
+
let ui;
|
|
129
|
+
const showFailure = () => ui?.setStatus("swb", ui.theme.fg("error", `swb: not recording: ${proc.failure}`));
|
|
130
|
+
const connect = () => {
|
|
131
|
+
proc.db ??= new Db(proc.launch.db, { create: false });
|
|
132
|
+
return proc.db;
|
|
133
|
+
};
|
|
134
|
+
const fail = (error) => {
|
|
135
|
+
proc.failure = error instanceof Error ? error.message : String(error);
|
|
136
|
+
showFailure();
|
|
137
|
+
ui?.notify(`swb: not recording: ${proc.failure}`, "error");
|
|
138
|
+
};
|
|
139
|
+
const write = (sessionId, body) => {
|
|
140
|
+
if (proc.failure) {
|
|
141
|
+
showFailure();
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
const db = connect();
|
|
146
|
+
db.tx(() => {
|
|
147
|
+
const version = db.userVersion();
|
|
148
|
+
if (version !== SCHEMA_VERSION)
|
|
149
|
+
throw new Error(`db schema is v${version}, this recorder writes v${SCHEMA_VERSION}`);
|
|
150
|
+
body(db, sessionId);
|
|
151
|
+
});
|
|
152
|
+
} catch (error) {
|
|
153
|
+
fail(error);
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
const writeFor = (ctx, body) => {
|
|
157
|
+
ui = ctx.ui;
|
|
158
|
+
write(ctx.sessionManager.getSessionId(), body);
|
|
159
|
+
};
|
|
160
|
+
let current;
|
|
161
|
+
pi.on("session_before_switch", (event, ctx) => {
|
|
162
|
+
if (event.reason !== "resume" || !event.targetSessionFile || proc.failure)
|
|
163
|
+
return;
|
|
164
|
+
ui = ctx.ui;
|
|
165
|
+
let host;
|
|
166
|
+
try {
|
|
167
|
+
host = hostOfTranscript(connect(), proc, event.targetSessionFile);
|
|
168
|
+
} catch (error) {
|
|
169
|
+
fail(error);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (!host)
|
|
173
|
+
return;
|
|
174
|
+
ctx.ui.notify(`swb: that session is already open in another pi (${host})`, "error");
|
|
175
|
+
return { cancel: true };
|
|
176
|
+
});
|
|
177
|
+
pi.on("session_start", (_event, ctx) => {
|
|
178
|
+
current = ctx.sessionManager.getSessionId();
|
|
179
|
+
proc.attention.clear();
|
|
180
|
+
writeFor(ctx, (db, id) => {
|
|
181
|
+
const host = hostOfSession(db, proc, id);
|
|
182
|
+
if (host)
|
|
183
|
+
throw new Error(`session ${id} is already open in another pi (${host})`);
|
|
184
|
+
db.run("DELETE FROM runtimes WHERE session_id = ? AND tmux_session != ?", id, proc.launch.tmuxSession);
|
|
185
|
+
db.run(`INSERT INTO runtimes (tmux_session, session_id, cwd, project_root, boot_id, pid, started_at) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
186
|
+
ON CONFLICT (tmux_session) DO UPDATE SET session_id = excluded.session_id, cwd = excluded.cwd,
|
|
187
|
+
project_root = excluded.project_root, boot_id = excluded.boot_id, pid = excluded.pid`, proc.launch.tmuxSession, id, ctx.cwd, projectRoot(ctx.cwd), bootId(), process.pid, Date.now());
|
|
188
|
+
const transcript = ctx.sessionManager.getSessionFile();
|
|
189
|
+
if (!transcript)
|
|
190
|
+
throw new Error("pi is running without a session file");
|
|
191
|
+
db.run("UPDATE sessions SET phase = 'idle', cwd = ?, project_root = ?, branch = ?, transcript = ? WHERE session_id = ?", ctx.cwd, projectRoot(ctx.cwd), branch(ctx.cwd), transcript, id);
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
pi.on("message_start", (event, ctx) => {
|
|
195
|
+
if (event.message.role !== "user")
|
|
196
|
+
return;
|
|
197
|
+
writeFor(ctx, (db, id) => {
|
|
198
|
+
const transcript = ctx.sessionManager.getSessionFile();
|
|
199
|
+
if (!transcript)
|
|
200
|
+
throw new Error("pi is running without a session file");
|
|
201
|
+
const now = Date.now();
|
|
202
|
+
db.run(`INSERT INTO sessions (session_id, tool, cwd, project_root, transcript, title, branch, phase, created_at, last_prompt_at)
|
|
203
|
+
VALUES (?, 'pi', ?, ?, ?, ?, ?, 'working', ?, ?)
|
|
204
|
+
ON CONFLICT (session_id) DO UPDATE SET phase = 'working', last_prompt_at = excluded.last_prompt_at`, id, ctx.cwd, projectRoot(ctx.cwd), transcript, ctx.sessionManager.getSessionName() ?? null, branch(ctx.cwd), now, now);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
pi.on("agent_start", (_event, ctx) => {
|
|
208
|
+
writeFor(ctx, (db, id) => db.run("UPDATE sessions SET phase = 'working' WHERE session_id = ?", id));
|
|
209
|
+
});
|
|
210
|
+
const attention = (open) => (data) => {
|
|
211
|
+
const attentionId = data.attentionId;
|
|
212
|
+
if (typeof attentionId !== "string" || !current)
|
|
213
|
+
return;
|
|
214
|
+
if (open)
|
|
215
|
+
proc.attention.add(attentionId);
|
|
216
|
+
else
|
|
217
|
+
proc.attention.delete(attentionId);
|
|
218
|
+
const phase = proc.attention.size > 0 ? "blocked" : "working";
|
|
219
|
+
write(current, (db, id) => db.run("UPDATE sessions SET phase = ? WHERE session_id = ? AND phase != 'idle'", phase, id));
|
|
220
|
+
};
|
|
221
|
+
pi.events.on("glimpseui:attention:request", attention(true));
|
|
222
|
+
pi.events.on("glimpseui:attention:resolve", attention(false));
|
|
223
|
+
pi.on("agent_settled", (_event, ctx) => {
|
|
224
|
+
proc.attention.clear();
|
|
225
|
+
writeFor(ctx, (db, id) => {
|
|
226
|
+
db.run("UPDATE sessions SET phase = 'idle', last_settled_at = ?, cwd = ?, project_root = ?, branch = ?, title = ? WHERE session_id = ?", Date.now(), ctx.cwd, projectRoot(ctx.cwd), branch(ctx.cwd), ctx.sessionManager.getSessionName() ?? null, id);
|
|
227
|
+
db.run("UPDATE runtimes SET cwd = ?, project_root = ? WHERE tmux_session = ?", ctx.cwd, projectRoot(ctx.cwd), proc.launch.tmuxSession);
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
pi.on("session_info_changed", (event, ctx) => {
|
|
231
|
+
writeFor(ctx, (db, id) => db.run("UPDATE sessions SET title = ? WHERE session_id = ?", event.name ?? null, id));
|
|
232
|
+
});
|
|
233
|
+
pi.registerCommand("archive", {
|
|
234
|
+
description: "Archive this session in swb and quit",
|
|
235
|
+
handler: async (_args, ctx) => {
|
|
236
|
+
if (!ctx.isIdle()) {
|
|
237
|
+
ctx.ui.notify("swb: turn running: wait for it to complete", "error");
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
let archived = false;
|
|
241
|
+
writeFor(ctx, (db, id) => {
|
|
242
|
+
if (!db.get("SELECT 1 FROM sessions WHERE session_id = ?", id))
|
|
243
|
+
return;
|
|
244
|
+
db.run("INSERT INTO marks (session_id, archived_at) VALUES (?, ?) ON CONFLICT (session_id) DO UPDATE SET archived_at = excluded.archived_at", id, Date.now());
|
|
245
|
+
archived = true;
|
|
246
|
+
});
|
|
247
|
+
if (archived)
|
|
248
|
+
ctx.shutdown();
|
|
249
|
+
else if (!proc.failure)
|
|
250
|
+
ctx.ui.notify("swb: nothing to archive before the first prompt", "error");
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
pi.on("session_shutdown", (event, ctx) => {
|
|
254
|
+
if (event.reason !== "quit")
|
|
255
|
+
return;
|
|
256
|
+
writeFor(ctx, (db) => db.run("DELETE FROM runtimes WHERE tmux_session = ?", proc.launch.tmuxSession));
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
export {
|
|
260
|
+
src_default as default
|
|
261
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@thurstonsand/pi-agent-switchboard",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Records managed pi sessions for Agent Switchboard (swb)",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/thurstonsand/agent-switchboard.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"pi-package"
|
|
13
|
+
],
|
|
14
|
+
"pi": {
|
|
15
|
+
"extensions": [
|
|
16
|
+
"./index.js"
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
21
|
+
}
|
|
22
|
+
}
|
package/README.md
DELETED