@gwro/codeaside 0.0.0-stage → 0.1.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/CHANGELOG.md +205 -0
- package/LICENSE +22 -0
- package/README.md +387 -2
- package/SECURITY.md +41 -0
- package/app/dist/assets/index-B2xgr0mA.js +20 -0
- package/app/dist/index.html +40 -0
- package/bin/codeaside +603 -0
- package/bin/codeaside-hook.mjs +28 -0
- package/docs/privacy.md +60 -0
- package/docs/windows-design.md +532 -0
- package/host/client.mjs +346 -0
- package/host/host.mjs +336 -0
- package/hub/awake.mjs +45 -0
- package/hub/brand.mjs +17 -0
- package/hub/config.mjs +129 -0
- package/hub/feed.mjs +30 -0
- package/hub/notify.mjs +39 -0
- package/hub/pane.mjs +326 -0
- package/hub/procs.mjs +24 -0
- package/hub/server.mjs +441 -0
- package/hub/service.mjs +181 -0
- package/hub/sessions.mjs +507 -0
- package/hub/stt.mjs +149 -0
- package/hub/transcript.mjs +318 -0
- package/hub/voice.mjs +151 -0
- package/package.json +62 -4
package/hub/sessions.mjs
ADDED
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
// Session registry: fed by Claude Code hooks, enriched from transcripts,
|
|
2
|
+
// checked against live processes and host panes.
|
|
3
|
+
import { EventEmitter } from "node:events";
|
|
4
|
+
import { existsSync, readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { basename, join } from "node:path";
|
|
6
|
+
import { CLAUDE_PROJECTS_DIR, STATE_FILE } from "./config.mjs";
|
|
7
|
+
import { entryMeta, entryToItems, parseLine, readTailLines, toolLabel, TranscriptTail } from "./transcript.mjs";
|
|
8
|
+
import { capture, paneAlive, paneKey, readDialog, readSuggestion, targetFromHook } from "./pane.mjs";
|
|
9
|
+
|
|
10
|
+
const MAX_ITEMS = 300;
|
|
11
|
+
const HISTORY_ITEMS = 150; // how much history to load when the hub starts watching a session
|
|
12
|
+
|
|
13
|
+
export function pidAlive(pid) {
|
|
14
|
+
if (!pid) return false;
|
|
15
|
+
try {
|
|
16
|
+
process.kill(pid, 0);
|
|
17
|
+
return true;
|
|
18
|
+
} catch (err) {
|
|
19
|
+
return err.code === "EPERM";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @typedef {"starting"|"working"|"idle"|"waiting"|"ended"} State
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
export class Session {
|
|
28
|
+
constructor({ id, cwd, transcriptPath }) {
|
|
29
|
+
this.id = id;
|
|
30
|
+
this.agent = "claude";
|
|
31
|
+
this.cwd = cwd;
|
|
32
|
+
this.project = basename(cwd || "") || "claude";
|
|
33
|
+
this.transcriptPath = transcriptPath;
|
|
34
|
+
/** @type {import("./pane.mjs").Target | null} the host pane, when Claude runs in one */
|
|
35
|
+
this.pane = null;
|
|
36
|
+
this.pid = null;
|
|
37
|
+
/** @type {State} */
|
|
38
|
+
this.state = "starting";
|
|
39
|
+
this.activity = ""; // "Edit foo.ts"
|
|
40
|
+
/** The prompt Claude Code suggests in its input box while the session is idle, if any. */
|
|
41
|
+
this.suggestion = null;
|
|
42
|
+
/** @type {null | {kind: "permission"|"question", tool?: string, detail?: string, questions?: any[], questionIndex?: number}} */
|
|
43
|
+
this.waiting = null;
|
|
44
|
+
this.model = null;
|
|
45
|
+
this.context = null;
|
|
46
|
+
this.permissionMode = null;
|
|
47
|
+
this.title = null;
|
|
48
|
+
this.summary = null;
|
|
49
|
+
this.origin = "terminal";
|
|
50
|
+
this.paneName = null; // the host's session name, shown as the terminal tab name
|
|
51
|
+
this.lastActivity = Date.now();
|
|
52
|
+
this.backgroundTasks = 0;
|
|
53
|
+
/** @type {import("./transcript.mjs").Item[]} */
|
|
54
|
+
this.items = [];
|
|
55
|
+
this.itemKeys = new Set();
|
|
56
|
+
this.tail = null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
get controllable() {
|
|
60
|
+
return !!this.pane && this.state !== "ended";
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
summaryJSON() {
|
|
64
|
+
return {
|
|
65
|
+
id: this.id,
|
|
66
|
+
agent: this.agent,
|
|
67
|
+
project: this.project,
|
|
68
|
+
cwd: this.cwd,
|
|
69
|
+
state: this.state,
|
|
70
|
+
activity: this.activity,
|
|
71
|
+
waiting: this.waiting,
|
|
72
|
+
model: this.model,
|
|
73
|
+
context: this.context,
|
|
74
|
+
permissionMode: this.permissionMode,
|
|
75
|
+
title: this.title,
|
|
76
|
+
summary: this.summary,
|
|
77
|
+
origin: this.origin,
|
|
78
|
+
controllable: this.controllable,
|
|
79
|
+
tmuxName: this.paneName, // wire name kept for the glasses app
|
|
80
|
+
lastActivity: this.lastActivity,
|
|
81
|
+
backgroundTasks: this.backgroundTasks,
|
|
82
|
+
itemCount: this.items.length,
|
|
83
|
+
suggestion: this.suggestion,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
persistJSON() {
|
|
88
|
+
return {
|
|
89
|
+
id: this.id,
|
|
90
|
+
cwd: this.cwd,
|
|
91
|
+
transcriptPath: this.transcriptPath,
|
|
92
|
+
pane: this.pane,
|
|
93
|
+
pid: this.pid,
|
|
94
|
+
origin: this.origin,
|
|
95
|
+
paneName: this.paneName,
|
|
96
|
+
state: this.state,
|
|
97
|
+
lastActivity: this.lastActivity,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export class Registry extends EventEmitter {
|
|
103
|
+
constructor() {
|
|
104
|
+
super();
|
|
105
|
+
/** @type {Map<string, Session>} */
|
|
106
|
+
this.sessions = new Map();
|
|
107
|
+
this.saveTimer = null;
|
|
108
|
+
/** Look for a suggested prompt in idle sessions' input boxes on each sweep. */
|
|
109
|
+
this.readSuggestions = true;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ---------- persistence ----------
|
|
113
|
+
|
|
114
|
+
load() {
|
|
115
|
+
if (!existsSync(STATE_FILE)) return;
|
|
116
|
+
let saved;
|
|
117
|
+
try {
|
|
118
|
+
saved = JSON.parse(readFileSync(STATE_FILE, "utf8"));
|
|
119
|
+
} catch {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
for (const s of saved.sessions || []) {
|
|
123
|
+
if (!s.id || !s.transcriptPath) continue;
|
|
124
|
+
// Restore a session in a pane, and the first sweep asks the host whether it is
|
|
125
|
+
// still there; its saved pid says nothing, since it can be a process that has
|
|
126
|
+
// since exited. Without a pane, restore it if its process still runs.
|
|
127
|
+
if (!s.pane && !(s.pid && pidAlive(s.pid))) continue;
|
|
128
|
+
const session = new Session(s);
|
|
129
|
+
Object.assign(session, { pane: s.pane || null, pid: s.pid || null, origin: s.origin || "terminal", paneName: s.paneName || null });
|
|
130
|
+
session.state = "idle"; // waiting details are not persisted; the next hook or sweep corrects it
|
|
131
|
+
session.lastActivity = s.lastActivity || Date.now();
|
|
132
|
+
this.attach(session);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
scheduleSave() {
|
|
137
|
+
clearTimeout(this.saveTimer);
|
|
138
|
+
this.saveTimer = setTimeout(() => {
|
|
139
|
+
const sessions = [...this.sessions.values()].filter((s) => s.state !== "ended").map((s) => s.persistJSON());
|
|
140
|
+
try {
|
|
141
|
+
writeFileSync(STATE_FILE, JSON.stringify({ sessions }, null, 2));
|
|
142
|
+
} catch {
|
|
143
|
+
/* non-fatal */
|
|
144
|
+
}
|
|
145
|
+
}, 500);
|
|
146
|
+
this.saveTimer.unref?.();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ---------- transcript ----------
|
|
150
|
+
|
|
151
|
+
attach(session) {
|
|
152
|
+
this.sessions.set(session.id, session);
|
|
153
|
+
this.loadHistory(session);
|
|
154
|
+
this.scheduleSave();
|
|
155
|
+
this.changed(session);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Add display items, skipping ones already shown, and publish the new ones when live. */
|
|
159
|
+
addItems(session, items, live) {
|
|
160
|
+
const fresh = [];
|
|
161
|
+
for (const item of items) {
|
|
162
|
+
if (session.itemKeys.has(item.key)) continue;
|
|
163
|
+
session.itemKeys.add(item.key);
|
|
164
|
+
session.items.push(item);
|
|
165
|
+
fresh.push(item);
|
|
166
|
+
}
|
|
167
|
+
if (session.items.length > MAX_ITEMS) {
|
|
168
|
+
const drop = session.items.splice(0, session.items.length - MAX_ITEMS);
|
|
169
|
+
for (const d of drop) session.itemKeys.delete(d.key);
|
|
170
|
+
}
|
|
171
|
+
if (live && fresh.length) {
|
|
172
|
+
session.lastActivity = Date.now();
|
|
173
|
+
this.emit("items", session, fresh);
|
|
174
|
+
}
|
|
175
|
+
return fresh;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
loadHistory(session) {
|
|
179
|
+
const path = session.transcriptPath;
|
|
180
|
+
if (!path || !existsSync(path)) {
|
|
181
|
+
session.tail = new TranscriptTail(path, { fromOffset: 0 }).start();
|
|
182
|
+
} else {
|
|
183
|
+
// Screenshots and big tool outputs can make a few megabytes cover only minutes,
|
|
184
|
+
// so widen the window until there is enough to show or the whole file is read.
|
|
185
|
+
let end = 0;
|
|
186
|
+
for (const maxBytes of [2e6, 8e6, 32e6, 128e6]) {
|
|
187
|
+
session.items = [];
|
|
188
|
+
session.itemKeys = new Set();
|
|
189
|
+
const tail = readTailLines(path, maxBytes);
|
|
190
|
+
end = tail.end;
|
|
191
|
+
for (const line of tail.lines) this.applyEntry(session, parseLine(line), false);
|
|
192
|
+
if (session.items.length >= HISTORY_ITEMS || maxBytes >= tail.end) break;
|
|
193
|
+
}
|
|
194
|
+
session.tail = new TranscriptTail(path, { fromOffset: end }).start();
|
|
195
|
+
}
|
|
196
|
+
session.tail.on("entry", (entry) => this.applyEntry(session, entry, true));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
applyEntry(session, entry, live) {
|
|
200
|
+
if (!entry) return;
|
|
201
|
+
// A resumed session appends to a file whose entries carry the same session id,
|
|
202
|
+
// but forks write other ids; ignore entries that belong to another session.
|
|
203
|
+
if (entry.sessionId && entry.sessionId !== session.id) return;
|
|
204
|
+
const meta = entryMeta(entry);
|
|
205
|
+
if (meta) {
|
|
206
|
+
if (meta.title) session.title = meta.title;
|
|
207
|
+
if (meta.model) session.model = meta.model;
|
|
208
|
+
if (meta.context) session.context = meta.context;
|
|
209
|
+
if (meta.summary) session.summary = meta.summary;
|
|
210
|
+
if (meta.permissionMode) session.permissionMode = meta.permissionMode;
|
|
211
|
+
}
|
|
212
|
+
const fresh = this.addItems(session, entryToItems(entry), false);
|
|
213
|
+
if (live && fresh.length) {
|
|
214
|
+
session.lastActivity = Date.now();
|
|
215
|
+
// Interrupts and declined permissions end the turn without a Stop hook.
|
|
216
|
+
if (fresh.some((i) => i.kind === "notice" && i.text === "Interrupted")) {
|
|
217
|
+
session.state = "idle";
|
|
218
|
+
session.waiting = null;
|
|
219
|
+
session.activity = "";
|
|
220
|
+
session.stoppedAt = Date.now();
|
|
221
|
+
this.changed(session);
|
|
222
|
+
}
|
|
223
|
+
// Output written after the last Stop means the model woke on its own (a background
|
|
224
|
+
// task finished). Output from before the Stop is just the tail catching up.
|
|
225
|
+
const wokeUp = fresh.some((i) => (i.kind === "assistant" || i.kind === "tool") && Date.parse(i.ts || "") > (session.stoppedAt || 0));
|
|
226
|
+
if (session.state === "idle" && wokeUp) {
|
|
227
|
+
session.state = "working";
|
|
228
|
+
this.changed(session);
|
|
229
|
+
}
|
|
230
|
+
this.emit("items", session, fresh);
|
|
231
|
+
}
|
|
232
|
+
if (live && meta) this.changed(session);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// ---------- hooks ----------
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* @param {object} payload hook stdin JSON
|
|
239
|
+
* @param {{host?: string, pane?: string, pid?: number}} env what the hook script knew
|
|
240
|
+
* about its surroundings: the host pane it ran in, and Claude's process id if known
|
|
241
|
+
* @returns {Session | null} the session the event applied to
|
|
242
|
+
*/
|
|
243
|
+
handleHook(payload, env = {}) {
|
|
244
|
+
const id = payload?.session_id;
|
|
245
|
+
const event = payload?.hook_event_name;
|
|
246
|
+
if (!id || !event) return null;
|
|
247
|
+
if (payload.agent_id) return null; // subagent-level hooks: the parent session's state is what matters
|
|
248
|
+
|
|
249
|
+
let session = this.sessions.get(id);
|
|
250
|
+
const target = targetFromHook(env);
|
|
251
|
+
if (!session) {
|
|
252
|
+
if (event === "SessionEnd") return null;
|
|
253
|
+
session = new Session({ id, cwd: payload.cwd, transcriptPath: payload.transcript_path });
|
|
254
|
+
const pending = this.pendingLaunch(target);
|
|
255
|
+
if (pending) {
|
|
256
|
+
session.origin = pending.origin;
|
|
257
|
+
session.paneName = pending.name;
|
|
258
|
+
}
|
|
259
|
+
this.attach(session);
|
|
260
|
+
}
|
|
261
|
+
if (target) session.pane = target;
|
|
262
|
+
if (env.pid > 1) session.pid = env.pid;
|
|
263
|
+
if (payload.cwd && payload.cwd !== session.cwd) {
|
|
264
|
+
session.cwd = payload.cwd;
|
|
265
|
+
session.project = basename(payload.cwd);
|
|
266
|
+
}
|
|
267
|
+
if (payload.permission_mode) session.permissionMode = payload.permission_mode;
|
|
268
|
+
if (payload.model) session.model = payload.model;
|
|
269
|
+
session.lastActivity = Date.now();
|
|
270
|
+
|
|
271
|
+
switch (event) {
|
|
272
|
+
case "SessionStart":
|
|
273
|
+
session.state = "idle";
|
|
274
|
+
session.waiting = null;
|
|
275
|
+
break;
|
|
276
|
+
case "UserPromptSubmit":
|
|
277
|
+
session.state = "working";
|
|
278
|
+
session.activity = "";
|
|
279
|
+
session.waiting = null;
|
|
280
|
+
session.suggestion = null;
|
|
281
|
+
break;
|
|
282
|
+
case "PreToolUse":
|
|
283
|
+
session.state = "working";
|
|
284
|
+
session.activity = `${payload.tool_name} ${payload.tool_label ?? toolLabel(payload.tool_name, payload.tool_input)}`.trim();
|
|
285
|
+
if (payload.tool_name === "AskUserQuestion") {
|
|
286
|
+
session.waiting = { kind: "question", questions: payload.tool_input?.questions || [], questionIndex: 0 };
|
|
287
|
+
session.state = "waiting";
|
|
288
|
+
} else {
|
|
289
|
+
session.lastTool = { tool: payload.tool_name, detail: payload.tool_label ?? toolLabel(payload.tool_name, payload.tool_input), input: payload.tool_input };
|
|
290
|
+
}
|
|
291
|
+
break;
|
|
292
|
+
case "PostToolUse":
|
|
293
|
+
case "PostToolUseFailure":
|
|
294
|
+
if (session.waiting?.kind === "question" && payload.tool_name === "AskUserQuestion") session.waiting = null;
|
|
295
|
+
if (session.waiting?.kind === "permission") session.waiting = null;
|
|
296
|
+
session.state = "working";
|
|
297
|
+
break;
|
|
298
|
+
case "PermissionDenied":
|
|
299
|
+
if (session.waiting?.kind === "permission") session.waiting = null;
|
|
300
|
+
break;
|
|
301
|
+
case "Notification":
|
|
302
|
+
if (payload.notification_type === "permission_prompt" && session.waiting?.kind !== "question") {
|
|
303
|
+
session.state = "waiting";
|
|
304
|
+
session.waiting = { kind: "permission", tool: session.lastTool?.tool, detail: session.lastTool?.detail || payload.message };
|
|
305
|
+
this.emit("attention", session, "permission");
|
|
306
|
+
} else if (payload.notification_type === "elicitation_dialog" || payload.notification_type === "agent_needs_input") {
|
|
307
|
+
session.state = "waiting";
|
|
308
|
+
session.waiting = { kind: "question", questions: [], questionIndex: 0, detail: payload.message };
|
|
309
|
+
this.emit("attention", session, "question");
|
|
310
|
+
}
|
|
311
|
+
break;
|
|
312
|
+
case "Stop":
|
|
313
|
+
session.stoppedAt = Date.now();
|
|
314
|
+
session.state = "idle";
|
|
315
|
+
session.waiting = null;
|
|
316
|
+
session.activity = "";
|
|
317
|
+
session.backgroundTasks = Array.isArray(payload.background_tasks) ? payload.background_tasks.length : 0;
|
|
318
|
+
this.emit("finished", session, payload.last_assistant_message || "");
|
|
319
|
+
break;
|
|
320
|
+
case "StopFailure":
|
|
321
|
+
session.stoppedAt = Date.now();
|
|
322
|
+
session.state = "idle";
|
|
323
|
+
session.waiting = null;
|
|
324
|
+
session.activity = "";
|
|
325
|
+
this.emit("finished", session, "Turn ended with an API error");
|
|
326
|
+
break;
|
|
327
|
+
case "SessionEnd":
|
|
328
|
+
session.state = "ended";
|
|
329
|
+
session.waiting = null;
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
if (session.waiting?.kind === "question" && event === "PreToolUse") this.emit("attention", session, "question");
|
|
333
|
+
session.transcriptPath = payload.transcript_path || session.transcriptPath;
|
|
334
|
+
this.scheduleSave();
|
|
335
|
+
this.changed(session);
|
|
336
|
+
return session;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// ---------- launches from the glasses ----------
|
|
340
|
+
|
|
341
|
+
pendingLaunches = new Map(); // paneKey -> {name, origin}
|
|
342
|
+
|
|
343
|
+
expectLaunch(target, info) {
|
|
344
|
+
const key = paneKey(target);
|
|
345
|
+
if (!key) return;
|
|
346
|
+
this.pendingLaunches.set(key, info);
|
|
347
|
+
setTimeout(() => this.pendingLaunches.delete(key), 120_000).unref?.();
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
pendingLaunch(target) {
|
|
351
|
+
const key = paneKey(target);
|
|
352
|
+
const p = key && this.pendingLaunches.get(key);
|
|
353
|
+
if (p) this.pendingLaunches.delete(key);
|
|
354
|
+
return p || null;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// ---------- liveness ----------
|
|
358
|
+
|
|
359
|
+
async sweep() {
|
|
360
|
+
for (const s of this.sessions.values()) {
|
|
361
|
+
if (s.state === "ended") continue;
|
|
362
|
+
// A pane that's gone means the terminal is gone, and Claude Code with it.
|
|
363
|
+
// Without a pane, the process id is all there is.
|
|
364
|
+
let alive = s.pane ? await paneAlive(s.pane) : s.pid ? pidAlive(s.pid) : true;
|
|
365
|
+
if (s.pane && !alive) s.pane = null;
|
|
366
|
+
if (alive && s.pane) {
|
|
367
|
+
try {
|
|
368
|
+
const dialog = readDialog(await capture(s.pane, 40));
|
|
369
|
+
if (s.waiting) {
|
|
370
|
+
// A waiting state with no dialog on screen is stale (answered at the desk,
|
|
371
|
+
// declined, or interrupted without a hook). Clear it after two checks.
|
|
372
|
+
const onScreen = dialog.kind === "permission" || dialog.kind === "question" || dialog.kind === "unknown";
|
|
373
|
+
s.staleWaitingChecks = onScreen ? 0 : (s.staleWaitingChecks || 0) + 1;
|
|
374
|
+
if (s.staleWaitingChecks >= 2) {
|
|
375
|
+
s.waiting = null;
|
|
376
|
+
s.staleWaitingChecks = 0;
|
|
377
|
+
if (s.state === "waiting") s.state = dialog.kind === "input" ? "idle" : "working";
|
|
378
|
+
this.changed(s);
|
|
379
|
+
}
|
|
380
|
+
} else if (dialog.kind === "permission" || dialog.kind === "question") {
|
|
381
|
+
// A dialog the hub never heard about: it was restarted while Claude waited,
|
|
382
|
+
// or the hook was lost. Two checks, so a mid-redraw read doesn't count.
|
|
383
|
+
s.unseenDialogChecks = (s.unseenDialogChecks || 0) + 1;
|
|
384
|
+
if (s.unseenDialogChecks >= 2) {
|
|
385
|
+
s.unseenDialogChecks = 0;
|
|
386
|
+
s.waiting =
|
|
387
|
+
dialog.kind === "permission"
|
|
388
|
+
? { kind: "permission", tool: s.lastTool?.tool, detail: s.lastTool?.detail || s.activity || dialog.prompt || "" }
|
|
389
|
+
: { kind: "question", questions: [], questionIndex: 0, detail: "" };
|
|
390
|
+
s.state = "waiting";
|
|
391
|
+
this.changed(s);
|
|
392
|
+
this.emit("attention", s, dialog.kind);
|
|
393
|
+
}
|
|
394
|
+
} else {
|
|
395
|
+
s.unseenDialogChecks = 0;
|
|
396
|
+
}
|
|
397
|
+
} catch {
|
|
398
|
+
/* pane busy or gone */
|
|
399
|
+
}
|
|
400
|
+
// Claude Code sometimes suggests the next prompt in its input box, a while
|
|
401
|
+
// after the turn ends. It is nowhere but on the screen.
|
|
402
|
+
const suggestion = this.readSuggestions && s.state === "idle" && !s.waiting ? await readSuggestion(s.pane).catch(() => null) : null;
|
|
403
|
+
if (suggestion !== s.suggestion) {
|
|
404
|
+
s.suggestion = suggestion;
|
|
405
|
+
this.changed(s);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
if (!alive) {
|
|
409
|
+
s.state = "ended";
|
|
410
|
+
s.waiting = null;
|
|
411
|
+
this.changed(s);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
// Forget ended sessions after 10 minutes.
|
|
415
|
+
for (const [id, s] of this.sessions) {
|
|
416
|
+
if (s.state === "ended" && Date.now() - s.lastActivity > 10 * 60_000) {
|
|
417
|
+
s.tail?.close();
|
|
418
|
+
this.sessions.delete(id);
|
|
419
|
+
this.emit("removed", s);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
this.scheduleSave();
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
changed(session) {
|
|
426
|
+
this.emit("session", session);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
list() {
|
|
430
|
+
return [...this.sessions.values()].sort((a, b) => b.lastActivity - a.lastActivity);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Recent sessions on disk that are not live, for "resume" on the glasses.
|
|
436
|
+
* @returns {{id: string, cwd: string, project: string, title: string, mtime: number}[]}
|
|
437
|
+
*/
|
|
438
|
+
export function recentTranscripts({ liveIds = new Set(), days = 14, limit = 30 } = {}) {
|
|
439
|
+
const out = [];
|
|
440
|
+
const cutoff = Date.now() - days * 86_400_000;
|
|
441
|
+
let dirs = [];
|
|
442
|
+
try {
|
|
443
|
+
dirs = readdirSync(CLAUDE_PROJECTS_DIR);
|
|
444
|
+
} catch {
|
|
445
|
+
return out;
|
|
446
|
+
}
|
|
447
|
+
for (const d of dirs) {
|
|
448
|
+
const dir = join(CLAUDE_PROJECTS_DIR, d);
|
|
449
|
+
let files;
|
|
450
|
+
try {
|
|
451
|
+
files = readdirSync(dir).filter((f) => f.endsWith(".jsonl"));
|
|
452
|
+
} catch {
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
for (const f of files) {
|
|
456
|
+
const path = join(dir, f);
|
|
457
|
+
let st;
|
|
458
|
+
try {
|
|
459
|
+
st = statSync(path);
|
|
460
|
+
} catch {
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
if (st.mtimeMs < cutoff || st.size < 200) continue;
|
|
464
|
+
const id = f.slice(0, -6);
|
|
465
|
+
if (liveIds.has(id)) continue;
|
|
466
|
+
out.push({ id, path, mtime: st.mtimeMs });
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
out.sort((a, b) => b.mtime - a.mtime);
|
|
470
|
+
return out.slice(0, limit).map(({ id, path, mtime }) => {
|
|
471
|
+
let cwd = "";
|
|
472
|
+
let title = "";
|
|
473
|
+
let firstPrompt = "";
|
|
474
|
+
try {
|
|
475
|
+
const { lines } = readTailLines(path, 400_000);
|
|
476
|
+
for (let i = lines.length - 1; i >= 0 && !(cwd && title); i--) {
|
|
477
|
+
const e = parseLine(lines[i]);
|
|
478
|
+
if (!e) continue;
|
|
479
|
+
if (!cwd && e.cwd) cwd = e.cwd;
|
|
480
|
+
if (!title && e.type === "ai-title" && e.aiTitle) title = e.aiTitle;
|
|
481
|
+
if (!title && e.type === "custom-title" && e.customTitle) title = e.customTitle;
|
|
482
|
+
}
|
|
483
|
+
if (!title) {
|
|
484
|
+
for (const line of lines) {
|
|
485
|
+
const e = parseLine(line);
|
|
486
|
+
const t = e?.type === "user" && typeof e.message?.content === "string" ? e.message.content : "";
|
|
487
|
+
if (t && !t.startsWith("<")) {
|
|
488
|
+
firstPrompt = t;
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
} catch {
|
|
494
|
+
/* unreadable */
|
|
495
|
+
}
|
|
496
|
+
return { id, cwd, project: basename(cwd || "") || "?", title: title || firstPrompt.slice(0, 60), mtime };
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/** Distinct project folders from recent transcripts, most recent first. */
|
|
501
|
+
export function recentProjects(limit = 15) {
|
|
502
|
+
const seen = new Map();
|
|
503
|
+
for (const t of recentTranscripts({ days: 60, limit: 200 })) {
|
|
504
|
+
if (t.cwd && !seen.has(t.cwd) && existsSync(t.cwd)) seen.set(t.cwd, t.mtime);
|
|
505
|
+
}
|
|
506
|
+
return [...seen.entries()].slice(0, limit).map(([cwd, mtime]) => ({ cwd, project: basename(cwd), mtime }));
|
|
507
|
+
}
|
package/hub/stt.mjs
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// Local speech-to-text with whisper.cpp's whisper-server, kept warm between requests.
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
|
|
5
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
6
|
+
|
|
7
|
+
/** Wrap raw 16 kHz mono s16le PCM in a WAV header. */
|
|
8
|
+
export function pcmToWav(pcm, sampleRate = 16000, channels = 1, bits = 16) {
|
|
9
|
+
const header = Buffer.alloc(44);
|
|
10
|
+
const byteRate = (sampleRate * channels * bits) / 8;
|
|
11
|
+
header.write("RIFF", 0);
|
|
12
|
+
header.writeUInt32LE(36 + pcm.length, 4);
|
|
13
|
+
header.write("WAVE", 8);
|
|
14
|
+
header.write("fmt ", 12);
|
|
15
|
+
header.writeUInt32LE(16, 16);
|
|
16
|
+
header.writeUInt16LE(1, 20);
|
|
17
|
+
header.writeUInt16LE(channels, 22);
|
|
18
|
+
header.writeUInt32LE(sampleRate, 24);
|
|
19
|
+
header.writeUInt32LE(byteRate, 28);
|
|
20
|
+
header.writeUInt16LE((channels * bits) / 8, 32);
|
|
21
|
+
header.writeUInt16LE(bits, 34);
|
|
22
|
+
header.write("data", 36);
|
|
23
|
+
header.writeUInt32LE(pcm.length, 40);
|
|
24
|
+
return Buffer.concat([header, pcm]);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** RMS level of s16le PCM, 0..1, used to skip silent recordings. */
|
|
28
|
+
export function pcmLevel(pcm) {
|
|
29
|
+
const n = Math.floor(pcm.length / 2);
|
|
30
|
+
if (!n) return 0;
|
|
31
|
+
let sum = 0;
|
|
32
|
+
for (let i = 0; i < n; i++) {
|
|
33
|
+
const v = pcm.readInt16LE(i * 2) / 32768;
|
|
34
|
+
sum += v * v;
|
|
35
|
+
}
|
|
36
|
+
return Math.sqrt(sum / n);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Whisper hallucinates these on silence or noise.
|
|
40
|
+
const HALLUCINATIONS = [/^\[?(blank_audio|silence|music|inaudible)\]?$/i, /^(thank you\.?|thanks for watching!?|you)$/i];
|
|
41
|
+
// Sound-effect captions whisper invents around speech: "*phone rings*", "[music]", "(laughs)".
|
|
42
|
+
const SOUND_TAG = /\s*(\*[^*]{1,40}\*|\[[^\]]{1,40}\]|\([^)]{1,40}\))\s*/g;
|
|
43
|
+
|
|
44
|
+
export function cleanTranscript(raw) {
|
|
45
|
+
let text = String(raw || "").replace(/\s+/g, " ").trim();
|
|
46
|
+
text = text.replace(SOUND_TAG, " ").replace(/\s+/g, " ").trim();
|
|
47
|
+
if (HALLUCINATIONS.some((re) => re.test(text))) return "";
|
|
48
|
+
return text;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export class Transcriber {
|
|
52
|
+
constructor({ port, model, bin = "whisper-server", threads = 6, log = () => {} }) {
|
|
53
|
+
this.port = port;
|
|
54
|
+
this.model = model;
|
|
55
|
+
this.bin = bin; // a command on PATH, or the full path to whisper-server.exe
|
|
56
|
+
this.threads = threads;
|
|
57
|
+
this.log = log;
|
|
58
|
+
this.proc = null;
|
|
59
|
+
this.ready = null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
get available() {
|
|
63
|
+
const binPresent = /[\\/]/.test(this.bin) ? existsSync(this.bin) : true;
|
|
64
|
+
return binPresent && existsSync(this.model);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async ensure() {
|
|
68
|
+
if (this.ready) return this.ready;
|
|
69
|
+
if (!this.available) throw new Error(`speech model or whisper-server not found (${this.model}, ${this.bin}); run: codeaside setup-voice`);
|
|
70
|
+
this.ready = (async () => {
|
|
71
|
+
if (await this.ping()) return; // already running (for example, a previous hub)
|
|
72
|
+
this.proc = spawn(this.bin, ["-m", this.model, "--host", "127.0.0.1", "--port", String(this.port), "-l", "en", "-nt", "-t", String(this.threads)], {
|
|
73
|
+
stdio: ["ignore", "ignore", "pipe"],
|
|
74
|
+
windowsHide: true,
|
|
75
|
+
});
|
|
76
|
+
this.proc.on("error", (err) => this.log(`whisper-server could not start: ${err.message}`));
|
|
77
|
+
this.proc.stderr.on("data", () => {});
|
|
78
|
+
this.proc.on("exit", (code) => {
|
|
79
|
+
this.log(`whisper-server exited (${code})`);
|
|
80
|
+
this.proc = null;
|
|
81
|
+
this.ready = null;
|
|
82
|
+
});
|
|
83
|
+
const until = Date.now() + 60_000;
|
|
84
|
+
while (Date.now() < until) {
|
|
85
|
+
if (await this.ping()) {
|
|
86
|
+
await this.warmUp();
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
await sleep(300);
|
|
90
|
+
}
|
|
91
|
+
throw new Error("whisper-server did not start within 60s");
|
|
92
|
+
})().catch((err) => {
|
|
93
|
+
this.ready = null;
|
|
94
|
+
throw err;
|
|
95
|
+
});
|
|
96
|
+
return this.ready;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The first inference after a start is slow: the model is paged in, and a CUDA
|
|
101
|
+
* build compiles its kernels for the GPU (half a minute on a new card). Spend
|
|
102
|
+
* that on a second of silence at startup rather than on someone's first words.
|
|
103
|
+
*/
|
|
104
|
+
async warmUp() {
|
|
105
|
+
const started = Date.now();
|
|
106
|
+
try {
|
|
107
|
+
const form = new FormData();
|
|
108
|
+
form.append("file", new Blob([pcmToWav(Buffer.alloc(16000 * 2))], { type: "audio/wav" }), "warmup.wav");
|
|
109
|
+
form.append("response_format", "json");
|
|
110
|
+
await fetch(`http://127.0.0.1:${this.port}/inference`, { method: "POST", body: form, signal: AbortSignal.timeout(180_000) });
|
|
111
|
+
this.log(`whisper warm-up took ${((Date.now() - started) / 1000).toFixed(1)}s`);
|
|
112
|
+
} catch (err) {
|
|
113
|
+
this.log(`whisper warm-up failed: ${err.message}`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async ping() {
|
|
118
|
+
try {
|
|
119
|
+
const res = await fetch(`http://127.0.0.1:${this.port}/`, { signal: AbortSignal.timeout(800) });
|
|
120
|
+
return res.status < 500;
|
|
121
|
+
} catch {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @param {Buffer} pcm 16 kHz mono s16le
|
|
128
|
+
* @param {string} vocabulary words to bias toward
|
|
129
|
+
*/
|
|
130
|
+
async transcribe(pcm, vocabulary = "") {
|
|
131
|
+
if (pcm.length < 16000 * 2 * 0.3) return { text: "", reason: "too short" };
|
|
132
|
+
if (pcmLevel(pcm) < 0.0015) return { text: "", reason: "silent" }; // about -56 dBFS; whisper gets the quiet cases
|
|
133
|
+
await this.ensure();
|
|
134
|
+
const form = new FormData();
|
|
135
|
+
form.append("file", new Blob([pcmToWav(pcm)], { type: "audio/wav" }), "speech.wav");
|
|
136
|
+
form.append("response_format", "json");
|
|
137
|
+
form.append("temperature", "0");
|
|
138
|
+
if (vocabulary) form.append("prompt", vocabulary);
|
|
139
|
+
const started = Date.now();
|
|
140
|
+
const res = await fetch(`http://127.0.0.1:${this.port}/inference`, { method: "POST", body: form, signal: AbortSignal.timeout(60_000) });
|
|
141
|
+
if (!res.ok) throw new Error(`whisper-server ${res.status}: ${await res.text()}`);
|
|
142
|
+
const body = await res.json();
|
|
143
|
+
return { text: cleanTranscript(body.text), ms: Date.now() - started };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
stop() {
|
|
147
|
+
this.proc?.kill();
|
|
148
|
+
}
|
|
149
|
+
}
|