pi-reply-reader 0.0.0-stage → 0.2.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 +200 -0
- package/LICENSE +21 -0
- package/README.md +710 -2
- package/dist/bin/reply-reader.js +49 -0
- package/dist/src/cli.js +329 -0
- package/dist/src/listener.js +532 -0
- package/dist/src/music.js +452 -0
- package/dist/src/outputs.js +86 -0
- package/dist/src/playback.js +369 -0
- package/dist/src/player.js +265 -0
- package/dist/src/project.js +50 -0
- package/dist/src/reading-lock.js +371 -0
- package/dist/src/settings.js +126 -0
- package/dist/src/speech-text.js +1028 -0
- package/dist/src/voice.js +173 -0
- package/examples/reply-reader-desktop-audio.service +57 -0
- package/examples/reply-reader-vps-audio.service +57 -0
- package/extensions/reply-reader.ts +1278 -0
- package/package.json +71 -4
- package/src/auto-queue.ts +123 -0
- package/src/cli.ts +346 -0
- package/src/listener.ts +522 -0
- package/src/music.ts +474 -0
- package/src/outputs.ts +105 -0
- package/src/pause-key.ts +105 -0
- package/src/playback.ts +423 -0
- package/src/player.ts +255 -0
- package/src/project.ts +46 -0
- package/src/prompts.ts +142 -0
- package/src/questions.ts +19 -0
- package/src/reader-events.ts +44 -0
- package/src/reading-lock.ts +465 -0
- package/src/replies.ts +64 -0
- package/src/sections.ts +47 -0
- package/src/settings.ts +125 -0
- package/src/sherpa-onnx-node.d.ts +29 -0
- package/src/speech-text.ts +1215 -0
- package/src/voice.ts +195 -0
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { closeSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync, writeSync } from "node:fs";
|
|
3
|
+
import { isAbsolute, join } from "node:path";
|
|
4
|
+
import { isPlayer, sameProcess, startTime, terminatePid } from "./player.ts";
|
|
5
|
+
|
|
6
|
+
export { PLAYER_MARK } from "./player.ts";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The reading playing on this machine, shared by every Pi session and process of this user (S1, T21):
|
|
10
|
+
* `$XDG_RUNTIME_DIR/pi-reply-reader/reading.json`. `token` names one reading; `stopped` marks an entry left by
|
|
11
|
+
* `/read-stop`, which tells the reading it named that it was stopped. Start times come from /proc, so a pid that was
|
|
12
|
+
* reused by a newer process does not count as the one recorded. Next to it, `waiting.json` holds the waiting mark of
|
|
13
|
+
* the auto-read queue (S8, T32) and `reading.lock` is a short mutex around each check-and-write. Each Pi session
|
|
14
|
+
* publishes what waits in its line as `line-<token>.json` (S16), so `/read-queue` and the footer see the machine's.
|
|
15
|
+
* `project.json` remembers the project of the last reading that started (S19).
|
|
16
|
+
*
|
|
17
|
+
* Pause (S15): `paused` asks the reading's own session to pause it (its player ends, the entry keeps the machine with
|
|
18
|
+
* no player) or, once cleared, to resume it. Only entries marked `pausable` (a Pi session that knows how) take it; a
|
|
19
|
+
* `prompt` reading (S18) is ended instead.
|
|
20
|
+
*/
|
|
21
|
+
interface Entry {
|
|
22
|
+
token: string;
|
|
23
|
+
pid: number;
|
|
24
|
+
pidStart?: string;
|
|
25
|
+
player?: number;
|
|
26
|
+
playerStart?: string;
|
|
27
|
+
started: number;
|
|
28
|
+
stopped?: true;
|
|
29
|
+
paused?: true;
|
|
30
|
+
pausable?: true;
|
|
31
|
+
prompt?: true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** What a reading's entry says about it when it is claimed (S15). */
|
|
35
|
+
export interface ClaimKind {
|
|
36
|
+
/** Its session pauses and resumes it when the entry asks (a Pi session; not the CLI). */
|
|
37
|
+
pausable?: boolean;
|
|
38
|
+
/** A blocking prompt's reading (S18): a pause ends it. */
|
|
39
|
+
prompt?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** One reading waiting in a session's line, as /read-queue lists it (S16). */
|
|
43
|
+
export interface WaitingItem {
|
|
44
|
+
name: string;
|
|
45
|
+
/** When it joined the line, in ms; the machine's list is in this order. */
|
|
46
|
+
at: number;
|
|
47
|
+
/** The waiting mark it was queued under; once an explicit action changed it, it no longer waits. */
|
|
48
|
+
mark: string | undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A waiting reading as seen machine-wide: with its session's label and token. */
|
|
52
|
+
export interface MachineWaiting extends WaitingItem {
|
|
53
|
+
session: string;
|
|
54
|
+
token: string;
|
|
55
|
+
/** When its session last published its line (its heartbeat, S16); undefined from a version that does not beat. */
|
|
56
|
+
beat?: number;
|
|
57
|
+
/** Its session waits on a human (the output question, a voice download confirm): nobody yields to it (S16, L138). */
|
|
58
|
+
asking?: true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** One reading's hold on the machine. Nothing here throws. */
|
|
62
|
+
export interface Claim {
|
|
63
|
+
/** Records the reading's player; false when a newer reading or `/read-stop` took over before it started. */
|
|
64
|
+
setPlayer(pid: number): boolean;
|
|
65
|
+
/** Whether a newer reading or `/read-stop` from any session took over. */
|
|
66
|
+
lost(): boolean;
|
|
67
|
+
/** Drops the entry if it is still this reading's. */
|
|
68
|
+
release(): void;
|
|
69
|
+
/** Whether the entry asks for this reading to be paused (S15); undefined when it is no longer this reading's. */
|
|
70
|
+
paused(): boolean | undefined;
|
|
71
|
+
/** Marks this reading paused (its player cleared, as none runs) or playing again (S15). */
|
|
72
|
+
setPaused(paused: boolean): void;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface ReadingLock {
|
|
76
|
+
/**
|
|
77
|
+
* Makes a new reading the one playing on the machine, stopping the one playing in any other session. With
|
|
78
|
+
* `dropWaiting` (an explicit /read, S8) it also drops every auto-read waiting on the machine at that moment.
|
|
79
|
+
*/
|
|
80
|
+
claim(options?: { dropWaiting?: boolean } & ClaimKind): Claim;
|
|
81
|
+
/**
|
|
82
|
+
* For auto-read (S8 Queue, T32): claims the machine only when no reading plays, and only when no explicit action
|
|
83
|
+
* dropped the waiting replies since `mark` was taken. Never stops a reading.
|
|
84
|
+
*/
|
|
85
|
+
claimIfFree(mark: string | undefined, kind?: ClaimKind): Claim | "busy" | "dropped";
|
|
86
|
+
/** Whether a reading plays (or is starting) in any session; stale entries do not count. */
|
|
87
|
+
busy(): boolean;
|
|
88
|
+
/** The current waiting mark; a reply queued now remembers it, and drops itself once it changes. */
|
|
89
|
+
waitingMark(): string | undefined;
|
|
90
|
+
/** Drops every auto-read waiting on the machine (Esc, S8). */
|
|
91
|
+
dropWaiting(): void;
|
|
92
|
+
/** Stops the reading playing in another session, if any, and drops every waiting auto-read; true when one was playing. */
|
|
93
|
+
stopOthers(): boolean;
|
|
94
|
+
/** Ends the reading playing or paused in another session, keeping what waits (/read-next, S16); true when there was one. */
|
|
95
|
+
skipOthers(): boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Asks another session to pause its reading, or to resume it when paused (S15). "stopped" when it was a prompt's
|
|
98
|
+
* reading (ended instead), "unsupported" when its reader cannot pause (the CLI, an older version), undefined when
|
|
99
|
+
* nothing is being read.
|
|
100
|
+
*/
|
|
101
|
+
togglePause(): "paused" | "resumed" | "stopped" | "unsupported" | undefined;
|
|
102
|
+
/**
|
|
103
|
+
* Publishes the line of the session `token` (S16), stamped with the time as its heartbeat; an empty line removes it.
|
|
104
|
+
* `tag` (a few characters of Pi's session id) tells it apart from another session with the same label; the token's
|
|
105
|
+
* last four characters otherwise. `asking`: the session waits on a human before its next reading can start.
|
|
106
|
+
*/
|
|
107
|
+
publishLine(token: string, session: string, items: readonly WaitingItem[], tag?: string, asking?: boolean): void;
|
|
108
|
+
/**
|
|
109
|
+
* Every reading waiting on the machine, in the order each joined its line; dropped ones and dead sessions' are left
|
|
110
|
+
* out. Two sessions with the same label are told apart as `<label> #<tag>` (S16, L135).
|
|
111
|
+
*/
|
|
112
|
+
waitingList(): MachineWaiting[];
|
|
113
|
+
/**
|
|
114
|
+
* Makes `project` the one of the last reading that started on the machine (S19) and returns the one remembered
|
|
115
|
+
* before it, undefined when none was.
|
|
116
|
+
*/
|
|
117
|
+
swapProject(project: string): string | undefined;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** How long an explicit action waits for the mutex before acting anyway; an auto-read just tries later. */
|
|
121
|
+
const MUTEX_WAIT_MS = 200;
|
|
122
|
+
/** A mutex file still empty after this long was left by a process that died while taking it. */
|
|
123
|
+
const MUTEX_STALE_MS = 1000;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The machine-wide lock under `$XDG_RUNTIME_DIR`, or undefined when it is unset, relative or unusable: then each
|
|
127
|
+
* session only stops its own readings, as before.
|
|
128
|
+
*/
|
|
129
|
+
export function openReadingLock(env: NodeJS.ProcessEnv): ReadingLock | undefined {
|
|
130
|
+
const runtime = env.XDG_RUNTIME_DIR;
|
|
131
|
+
if (!runtime || !isAbsolute(runtime)) return undefined;
|
|
132
|
+
const dir = join(runtime, "pi-reply-reader");
|
|
133
|
+
try {
|
|
134
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
135
|
+
if (!statSync(dir).isDirectory()) return undefined;
|
|
136
|
+
} catch {
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
const file = join(dir, "reading.json");
|
|
140
|
+
/** `{ mark }`: rewritten by every explicit action, so replies queued before it know they were dropped (S8). */
|
|
141
|
+
const marks = join(dir, "waiting.json");
|
|
142
|
+
/** Held for the few microseconds of a check-and-write, so two sessions never both take a free machine (T32). */
|
|
143
|
+
const mutex = join(dir, "reading.lock");
|
|
144
|
+
/** `{ project }`: the project of the last reading that started, by any session or the command (S19). */
|
|
145
|
+
const projects = join(dir, "project.json");
|
|
146
|
+
|
|
147
|
+
const read = (): Entry | undefined => {
|
|
148
|
+
try {
|
|
149
|
+
const entry = JSON.parse(readFileSync(file, "utf8")) as Entry;
|
|
150
|
+
if (typeof entry?.token !== "string" || !Number.isInteger(entry.pid)) return undefined;
|
|
151
|
+
if (entry.player !== undefined && !Number.isInteger(entry.player)) return undefined;
|
|
152
|
+
return entry;
|
|
153
|
+
} catch {
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
/** Written whole, then renamed into place, so a reader never sees half a file. */
|
|
159
|
+
const put = (path: string, token: string, data: unknown): void => {
|
|
160
|
+
const temp = `${path}.${process.pid}.${token}.tmp`;
|
|
161
|
+
try {
|
|
162
|
+
writeFileSync(temp, JSON.stringify(data), { mode: 0o600 });
|
|
163
|
+
renameSync(temp, path);
|
|
164
|
+
} catch {
|
|
165
|
+
try {
|
|
166
|
+
unlinkSync(temp);
|
|
167
|
+
} catch {}
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
const write = (entry: Entry): void => put(file, entry.token, entry);
|
|
171
|
+
const lineFile = (token: string): string => join(dir, `line-${token}.json`);
|
|
172
|
+
|
|
173
|
+
const waitingMark = (): string | undefined => {
|
|
174
|
+
try {
|
|
175
|
+
const mark = (JSON.parse(readFileSync(marks, "utf8")) as { mark?: unknown })?.mark;
|
|
176
|
+
return typeof mark === "string" ? mark : undefined;
|
|
177
|
+
} catch {
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
const dropWaiting = (): void => {
|
|
182
|
+
const mark = randomUUID();
|
|
183
|
+
put(marks, mark, { mark });
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Runs `body` holding the machine's mutex. When it cannot be had in time, `timeout` decides: explicit actions act
|
|
188
|
+
* anyway (as before T32), auto-read tries again later. A mutex left by a dead process is broken.
|
|
189
|
+
*/
|
|
190
|
+
const exclusive = <T>(body: () => T, timeout: () => T): T => {
|
|
191
|
+
const deadline = Date.now() + MUTEX_WAIT_MS;
|
|
192
|
+
for (;;) {
|
|
193
|
+
let fd: number;
|
|
194
|
+
try {
|
|
195
|
+
fd = openSync(mutex, "wx", 0o600);
|
|
196
|
+
} catch (error) {
|
|
197
|
+
// Anything but "held" (a read-only folder, say) means no mutex at all, as before T32.
|
|
198
|
+
if ((error as NodeJS.ErrnoException).code !== "EEXIST") return body();
|
|
199
|
+
if (abandoned()) {
|
|
200
|
+
try {
|
|
201
|
+
unlinkSync(mutex);
|
|
202
|
+
continue;
|
|
203
|
+
} catch {
|
|
204
|
+
// Not deletable (a read-only folder, say): it counts as held, so the deadline still bounds the wait.
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (Date.now() > deadline) return timeout();
|
|
208
|
+
pause(2);
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
try {
|
|
212
|
+
try {
|
|
213
|
+
writeSync(fd, `${process.pid} ${startTime(process.pid) ?? ""}`);
|
|
214
|
+
} finally {
|
|
215
|
+
closeSync(fd);
|
|
216
|
+
}
|
|
217
|
+
return body();
|
|
218
|
+
} finally {
|
|
219
|
+
try {
|
|
220
|
+
unlinkSync(mutex);
|
|
221
|
+
} catch {}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
/** Whether the mutex was left by a process that is gone (or never wrote its pid). */
|
|
226
|
+
const abandoned = (): boolean => {
|
|
227
|
+
try {
|
|
228
|
+
const [pid, start] = readFileSync(mutex, "utf8").split(" ");
|
|
229
|
+
if (pid && Number.isInteger(Number(pid)) && Number(pid) > 0) return !sameProcess(Number(pid), start || undefined);
|
|
230
|
+
return Date.now() - statSync(mutex).mtimeMs > MUTEX_STALE_MS;
|
|
231
|
+
} catch {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Writes `entry` over the current one (always for a new reading; for `/read-stop` only when a reading plays) and
|
|
238
|
+
* stops the reading the current one names, if it still plays. The entry is written first, so the stopped
|
|
239
|
+
* session finds it lost when its player dies.
|
|
240
|
+
*/
|
|
241
|
+
const takeOver = (entry: Entry, always: boolean): boolean => {
|
|
242
|
+
const previous = read();
|
|
243
|
+
const live = previous !== undefined && playing(previous);
|
|
244
|
+
if (always || live) write(entry);
|
|
245
|
+
if (!live) return false;
|
|
246
|
+
// SIGTERM, then SIGKILL if that player ignores it (S13): its own session may be hung or gone.
|
|
247
|
+
if (previous.player !== undefined) terminatePid(previous.player, previous.playerStart);
|
|
248
|
+
return true;
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
const self = (): Pick<Entry, "pid" | "pidStart" | "started"> => ({ pid: process.pid, pidStart: startTime(process.pid), started: Date.now() });
|
|
252
|
+
|
|
253
|
+
/** The hold of the reading whose entry `mine` was just written. */
|
|
254
|
+
const held = (mine: Entry): Claim => {
|
|
255
|
+
/**
|
|
256
|
+
* Whether the entry was written. Once it was, a missing entry means another reading took over and already ended
|
|
257
|
+
* (a paused reading has no player to kill, so this is how it learns, S15/S16); if it never was (writes fail), the
|
|
258
|
+
* reading keeps to itself as without a lock.
|
|
259
|
+
*/
|
|
260
|
+
const written = read()?.token === mine.token;
|
|
261
|
+
const lost = (): boolean => {
|
|
262
|
+
const entry = read();
|
|
263
|
+
return entry === undefined ? written : entry.token !== mine.token;
|
|
264
|
+
};
|
|
265
|
+
/** The entry as it is now (a pause request may have changed it), when still this reading's. */
|
|
266
|
+
const own = (): Entry | undefined => {
|
|
267
|
+
const entry = read();
|
|
268
|
+
if (entry === undefined) return written ? undefined : mine;
|
|
269
|
+
return entry.token === mine.token ? entry : undefined;
|
|
270
|
+
};
|
|
271
|
+
const setPlayer = (pid: number): boolean => {
|
|
272
|
+
const entry = own();
|
|
273
|
+
if (!entry) return false;
|
|
274
|
+
write({ ...entry, player: pid, playerStart: startTime(pid) });
|
|
275
|
+
return true;
|
|
276
|
+
};
|
|
277
|
+
const setPaused = (paused: boolean): void => {
|
|
278
|
+
const entry = own();
|
|
279
|
+
if (!entry) return;
|
|
280
|
+
const { paused: _was, player: _player, playerStart: _start, ...rest } = entry;
|
|
281
|
+
// Paused, no player runs: the entry keeps the machine through the reader's own process (S15).
|
|
282
|
+
write(paused ? { ...rest, paused: true } : entry.player === undefined ? rest : { ...rest, player: entry.player, playerStart: entry.playerStart });
|
|
283
|
+
};
|
|
284
|
+
const release = (): void => {
|
|
285
|
+
if (read()?.token !== mine.token) return;
|
|
286
|
+
try {
|
|
287
|
+
unlinkSync(file);
|
|
288
|
+
} catch {}
|
|
289
|
+
};
|
|
290
|
+
return {
|
|
291
|
+
setPlayer: (pid) => exclusive(() => setPlayer(pid), () => setPlayer(pid)),
|
|
292
|
+
lost,
|
|
293
|
+
release: () => exclusive(release, release),
|
|
294
|
+
paused: () => {
|
|
295
|
+
const entry = read();
|
|
296
|
+
if (entry === undefined || entry.token !== mine.token) return undefined;
|
|
297
|
+
return entry.paused === true;
|
|
298
|
+
},
|
|
299
|
+
setPaused: (paused) => exclusive(() => setPaused(paused), () => setPaused(paused)),
|
|
300
|
+
};
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
const busy = (): boolean => {
|
|
304
|
+
const entry = read();
|
|
305
|
+
return entry !== undefined && playing(entry);
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
const kindOf = (kind: ClaimKind = {}): Pick<Entry, "pausable" | "prompt"> => ({
|
|
309
|
+
...(kind.pausable ? { pausable: true as const } : {}),
|
|
310
|
+
...(kind.prompt ? { prompt: true as const } : {}),
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
/** Ends the reading in another session without touching what waits; true when one was playing or paused. */
|
|
314
|
+
const skip = (): boolean => takeOver({ token: randomUUID(), ...self(), stopped: true }, false);
|
|
315
|
+
|
|
316
|
+
return {
|
|
317
|
+
claim(options = {}): Claim {
|
|
318
|
+
const mine: Entry = { token: randomUUID(), ...self(), ...kindOf(options) };
|
|
319
|
+
const take = (): void => {
|
|
320
|
+
if (options.dropWaiting) dropWaiting();
|
|
321
|
+
takeOver(mine, true);
|
|
322
|
+
};
|
|
323
|
+
exclusive(take, take);
|
|
324
|
+
return held(mine);
|
|
325
|
+
},
|
|
326
|
+
claimIfFree(mark, kind) {
|
|
327
|
+
return exclusive<Claim | "busy" | "dropped">(
|
|
328
|
+
() => {
|
|
329
|
+
if (waitingMark() !== mark) return "dropped";
|
|
330
|
+
if (busy()) return "busy";
|
|
331
|
+
const mine: Entry = { token: randomUUID(), ...self(), ...kindOf(kind) };
|
|
332
|
+
write(mine);
|
|
333
|
+
return held(mine);
|
|
334
|
+
},
|
|
335
|
+
() => "busy",
|
|
336
|
+
);
|
|
337
|
+
},
|
|
338
|
+
swapProject(project) {
|
|
339
|
+
const swap = (): string | undefined => {
|
|
340
|
+
let previous: string | undefined;
|
|
341
|
+
try {
|
|
342
|
+
const saved = (JSON.parse(readFileSync(projects, "utf8")) as { project?: unknown })?.project;
|
|
343
|
+
if (typeof saved === "string") previous = saved;
|
|
344
|
+
} catch {}
|
|
345
|
+
if (previous !== project) put(projects, randomUUID(), { project });
|
|
346
|
+
return previous;
|
|
347
|
+
};
|
|
348
|
+
return exclusive(swap, swap);
|
|
349
|
+
},
|
|
350
|
+
busy,
|
|
351
|
+
waitingMark,
|
|
352
|
+
dropWaiting: () => exclusive(dropWaiting, dropWaiting),
|
|
353
|
+
stopOthers: () => {
|
|
354
|
+
const stop = (): boolean => {
|
|
355
|
+
dropWaiting();
|
|
356
|
+
return takeOver({ token: randomUUID(), ...self(), stopped: true }, false);
|
|
357
|
+
};
|
|
358
|
+
return exclusive(stop, stop);
|
|
359
|
+
},
|
|
360
|
+
skipOthers: () => exclusive(skip, skip),
|
|
361
|
+
togglePause: () => {
|
|
362
|
+
const toggle = (): "paused" | "resumed" | "stopped" | "unsupported" | undefined => {
|
|
363
|
+
const entry = read();
|
|
364
|
+
if (entry === undefined || !playing(entry)) return undefined;
|
|
365
|
+
if (entry.prompt) return skip() ? "stopped" : undefined;
|
|
366
|
+
if (!entry.pausable) return "unsupported";
|
|
367
|
+
if (entry.paused) {
|
|
368
|
+
const { paused: _paused, ...rest } = entry;
|
|
369
|
+
write(rest);
|
|
370
|
+
return "resumed";
|
|
371
|
+
}
|
|
372
|
+
write({ ...entry, paused: true });
|
|
373
|
+
return "paused";
|
|
374
|
+
};
|
|
375
|
+
return exclusive(toggle, toggle);
|
|
376
|
+
},
|
|
377
|
+
publishLine(token, session, items, tag, asking) {
|
|
378
|
+
if (items.length === 0) {
|
|
379
|
+
try {
|
|
380
|
+
unlinkSync(lineFile(token));
|
|
381
|
+
} catch {}
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
put(lineFile(token), token, {
|
|
385
|
+
token,
|
|
386
|
+
session,
|
|
387
|
+
tag: tag || token.slice(-4),
|
|
388
|
+
beat: Date.now(),
|
|
389
|
+
...(asking ? { asking: true } : {}),
|
|
390
|
+
pid: process.pid,
|
|
391
|
+
pidStart: startTime(process.pid),
|
|
392
|
+
items,
|
|
393
|
+
});
|
|
394
|
+
},
|
|
395
|
+
waitingList() {
|
|
396
|
+
let names: string[];
|
|
397
|
+
try {
|
|
398
|
+
names = readdirSync(dir).filter((name) => /^line-[\w-]+\.json$/.test(name));
|
|
399
|
+
} catch {
|
|
400
|
+
return [];
|
|
401
|
+
}
|
|
402
|
+
const mark = waitingMark();
|
|
403
|
+
const all: MachineWaiting[] = [];
|
|
404
|
+
/** Each listed session's tag, by token, and the tokens under each label. */
|
|
405
|
+
const tags = new Map<string, string>();
|
|
406
|
+
const labels = new Map<string, Set<string>>();
|
|
407
|
+
for (const name of names) {
|
|
408
|
+
try {
|
|
409
|
+
const line = JSON.parse(readFileSync(join(dir, name), "utf8")) as {
|
|
410
|
+
token?: unknown;
|
|
411
|
+
session?: unknown;
|
|
412
|
+
tag?: unknown;
|
|
413
|
+
beat?: unknown;
|
|
414
|
+
asking?: unknown;
|
|
415
|
+
pid?: unknown;
|
|
416
|
+
pidStart?: unknown;
|
|
417
|
+
items?: unknown;
|
|
418
|
+
};
|
|
419
|
+
if (typeof line.token !== "string" || typeof line.session !== "string" || !Number.isInteger(line.pid) || !Array.isArray(line.items)) continue;
|
|
420
|
+
// A session whose process is gone left its line behind: it waits for nothing.
|
|
421
|
+
if (!sameProcess(line.pid as number, typeof line.pidStart === "string" ? line.pidStart : undefined)) {
|
|
422
|
+
try {
|
|
423
|
+
unlinkSync(join(dir, name));
|
|
424
|
+
} catch {}
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
for (const item of line.items as Partial<WaitingItem>[]) {
|
|
428
|
+
if (typeof item?.name !== "string" || typeof item.at !== "number") continue;
|
|
429
|
+
const itemMark = typeof item.mark === "string" ? item.mark : undefined;
|
|
430
|
+
// Dropped by an explicit action since it was queued (S8): it will never play.
|
|
431
|
+
if (itemMark !== mark) continue;
|
|
432
|
+
const beat = typeof line.beat === "number" ? line.beat : undefined;
|
|
433
|
+
all.push({
|
|
434
|
+
name: item.name,
|
|
435
|
+
at: item.at,
|
|
436
|
+
mark: itemMark,
|
|
437
|
+
session: line.session,
|
|
438
|
+
token: line.token,
|
|
439
|
+
beat,
|
|
440
|
+
...(line.asking === true ? { asking: true as const } : {}),
|
|
441
|
+
});
|
|
442
|
+
tags.set(line.token, typeof line.tag === "string" && line.tag ? line.tag : line.token.slice(-4));
|
|
443
|
+
labels.set(line.session, (labels.get(line.session) ?? new Set<string>()).add(line.token));
|
|
444
|
+
}
|
|
445
|
+
} catch {}
|
|
446
|
+
}
|
|
447
|
+
for (const item of all) if ((labels.get(item.session)?.size ?? 0) > 1) item.session = `${item.session} #${tags.get(item.token)}`;
|
|
448
|
+
return all.sort((a, b) => a.at - b.at);
|
|
449
|
+
},
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Whether `entry` names a reading that may still play: not a `/read-stop` mark, its process alive, and its player,
|
|
455
|
+
* once started, still that player of ours.
|
|
456
|
+
*/
|
|
457
|
+
function playing(entry: Entry): boolean {
|
|
458
|
+
if (entry.stopped || !sameProcess(entry.pid, entry.pidStart)) return false;
|
|
459
|
+
return entry.player === undefined || isPlayer(entry.player, entry.playerStart);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/** Blocks this thread for `ms`; only ever a few milliseconds, while another process holds the mutex. */
|
|
463
|
+
function pause(ms: number): void {
|
|
464
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
465
|
+
}
|
package/src/replies.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
export interface Reply {
|
|
4
|
+
/** `P<prompt>.R<n>`, numbered exactly like pi-reply-thread's `/replies`. */
|
|
5
|
+
id: string;
|
|
6
|
+
/** The Pi session entry ID of the assistant message. */
|
|
7
|
+
entryId: string;
|
|
8
|
+
text: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Stop reasons of an assistant message that finished speaking instead of calling tools or failing. */
|
|
12
|
+
const FINISHED = new Set(["stop", "length"]);
|
|
13
|
+
|
|
14
|
+
/** gentle-agents wakes the session with a user-role message; it is not a prompt. */
|
|
15
|
+
const WAKE_PREFIX = "[System-generated Gentle Agents notification, not written by the user]";
|
|
16
|
+
|
|
17
|
+
type Content = string | readonly { type: string; text?: string }[] | undefined;
|
|
18
|
+
|
|
19
|
+
function textOf(content: Content): string {
|
|
20
|
+
if (typeof content === "string") return content;
|
|
21
|
+
if (!content) return "";
|
|
22
|
+
return content
|
|
23
|
+
.filter((block) => block.type === "text" && typeof block.text === "string")
|
|
24
|
+
.map((block) => block.text)
|
|
25
|
+
.join("\n");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The final replies of a session branch (pass `ctx.sessionManager.getBranch()`), numbered under the prompt
|
|
30
|
+
* that preceded them. Replies before any prompt fall under P0.
|
|
31
|
+
*/
|
|
32
|
+
export function listReplies(entries: readonly SessionEntry[]): Reply[] {
|
|
33
|
+
const replies: Reply[] = [];
|
|
34
|
+
let prompt = 0;
|
|
35
|
+
let index = 0;
|
|
36
|
+
for (const entry of entries) {
|
|
37
|
+
if (entry.type !== "message") continue;
|
|
38
|
+
const message = entry.message as { role?: string; content?: Content; stopReason?: string };
|
|
39
|
+
if (message.role === "user") {
|
|
40
|
+
if (textOf(message.content).startsWith(WAKE_PREFIX)) continue;
|
|
41
|
+
prompt += 1;
|
|
42
|
+
index = 0;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (message.role !== "assistant" || !FINISHED.has(message.stopReason ?? "")) continue;
|
|
46
|
+
const text = textOf(message.content).trim();
|
|
47
|
+
if (!text) continue;
|
|
48
|
+
index += 1;
|
|
49
|
+
replies.push({ id: `P${prompt}.R${index}`, entryId: entry.id, text });
|
|
50
|
+
}
|
|
51
|
+
return replies;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** By ordered ID in any case (a `.Q<n>` question suffix is accepted) or by exact entry ID. */
|
|
55
|
+
export function findReply(replies: readonly Reply[], id: string): Reply | undefined {
|
|
56
|
+
const wanted = id.trim();
|
|
57
|
+
if (!wanted) return undefined;
|
|
58
|
+
const ordered = /^(p\d+\.r\d+)(?:\.q\d+)?$/i.exec(wanted)?.[1].toUpperCase();
|
|
59
|
+
return replies.find((reply) => reply.id === ordered || reply.entryId === wanted);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function latestReply(replies: readonly Reply[]): Reply | undefined {
|
|
63
|
+
return replies.at(-1);
|
|
64
|
+
}
|
package/src/sections.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** A fence line opening or closing a code block; headings inside one are not headings. */
|
|
2
|
+
const FENCE = /^\s{0,3}(```|~~~)/;
|
|
3
|
+
/** An ATX heading: up to three spaces, one to six `#` (its level), then its text (a closing run of `#` is dropped). */
|
|
4
|
+
const HEADING = /^\s{0,3}(#{1,6})\s+(.*?)(?:\s+#+)?\s*$/;
|
|
5
|
+
/** Leading numbering of a heading or a section name: `2.`, `5.2.`, `3)`, `1:`. */
|
|
6
|
+
const NUMBERING = /^\d+(?:\.\d+)*[.):]?\s+/;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* How a heading or a wanted section compares (S9): lower case, no accents, no leading numbering, `-`, `_` and `/` as
|
|
10
|
+
* spaces, other punctuation dropped, spaces collapsed.
|
|
11
|
+
*/
|
|
12
|
+
export function sectionKey(text: string): string {
|
|
13
|
+
return text
|
|
14
|
+
.normalize("NFD")
|
|
15
|
+
.replace(/\p{M}/gu, "")
|
|
16
|
+
.toLowerCase()
|
|
17
|
+
.replace(/[-_/]/g, " ")
|
|
18
|
+
.replace(/[^\p{L}\p{N}\s]/gu, "")
|
|
19
|
+
.replace(/\s+/g, " ")
|
|
20
|
+
.trim()
|
|
21
|
+
.replace(NUMBERING, "");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The section of `markdown` whose heading matches `section` (S9): from that heading up to the next heading of the same
|
|
26
|
+
* or a higher level, or to the end with `rest`. Undefined when no heading matches. An exact match wins over a prefix
|
|
27
|
+
* match; a prefix match counts only when exactly one heading has it; of several exact matches the first wins.
|
|
28
|
+
*/
|
|
29
|
+
export function sectionFrom(markdown: string, section: string, options: { rest?: boolean } = {}): string | undefined {
|
|
30
|
+
const wanted = sectionKey(section);
|
|
31
|
+
if (!wanted) return undefined;
|
|
32
|
+
const lines = markdown.split("\n");
|
|
33
|
+
const headings: { line: number; level: number; key: string }[] = [];
|
|
34
|
+
let fenced = false;
|
|
35
|
+
lines.forEach((line, index) => {
|
|
36
|
+
if (FENCE.test(line)) fenced = !fenced;
|
|
37
|
+
if (fenced) return;
|
|
38
|
+
const heading = HEADING.exec(line);
|
|
39
|
+
if (heading) headings.push({ line: index, level: heading[1].length, key: sectionKey(heading[2]) });
|
|
40
|
+
});
|
|
41
|
+
const exact = headings.find((heading) => heading.key === wanted);
|
|
42
|
+
const prefixes = headings.filter((heading) => heading.key.startsWith(wanted));
|
|
43
|
+
const match = exact ?? (prefixes.length === 1 ? prefixes[0] : undefined);
|
|
44
|
+
if (!match) return undefined;
|
|
45
|
+
const end = options.rest ? undefined : headings.find((heading) => heading.line > match.line && heading.level <= match.level)?.line;
|
|
46
|
+
return lines.slice(match.line, end).join("\n").trimEnd();
|
|
47
|
+
}
|