screenreader-speech-capture 0.0.0-stage → 0.0.1-preview.1
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/dist/cli.mjs +685 -0
- package/package.json +25 -3
- package/README.md +0 -3
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,685 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import net from "node:net";
|
|
3
|
+
import { TextDecoder as TextDecoder$1 } from "node:util";
|
|
4
|
+
import { performance } from "node:perf_hooks";
|
|
5
|
+
import { monotonicFactory } from "ulid";
|
|
6
|
+
// package.json
|
|
7
|
+
var version = "0.0.1-preview.1";
|
|
8
|
+
// src/adapters/in/tcp/text-to-socket-parser.ts
|
|
9
|
+
const LF = 10;
|
|
10
|
+
const CR = 13;
|
|
11
|
+
const HEADER = /^voice=(\S.*)$/;
|
|
12
|
+
/**
|
|
13
|
+
* Parser for the `text-to-socket` wire format (docs/speech-capture.md#framing-and-decoding),
|
|
14
|
+
* one instance per connection. Bytes are split at `\n` *before* decoding, so a multi-byte
|
|
15
|
+
* character split across two TCP packets is decoded correctly.
|
|
16
|
+
*/
|
|
17
|
+
var TextToSocketParser = class {
|
|
18
|
+
maxLineBytes;
|
|
19
|
+
events;
|
|
20
|
+
decoder;
|
|
21
|
+
pending = [];
|
|
22
|
+
pendingBytes = 0;
|
|
23
|
+
/** After a cut: drop bytes until the next `\n`. */
|
|
24
|
+
discarding = false;
|
|
25
|
+
firstLine = true;
|
|
26
|
+
constructor(encoding, maxLineBytes, events) {
|
|
27
|
+
this.maxLineBytes = maxLineBytes;
|
|
28
|
+
this.events = events;
|
|
29
|
+
this.decoder = new TextDecoder$1(encoding);
|
|
30
|
+
}
|
|
31
|
+
push(chunk) {
|
|
32
|
+
let start = 0;
|
|
33
|
+
while (start < chunk.length) {
|
|
34
|
+
const newline = chunk.indexOf(LF, start);
|
|
35
|
+
const end = newline === -1 ? chunk.length : newline;
|
|
36
|
+
this.append(chunk.subarray(start, end));
|
|
37
|
+
if (newline === -1) return;
|
|
38
|
+
this.completeLine();
|
|
39
|
+
start = newline + 1;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** The connection closed: text still in the buffer becomes a last line. */
|
|
43
|
+
end() {
|
|
44
|
+
if (!this.discarding && this.pendingBytes > 0) this.emit();
|
|
45
|
+
this.pending = [];
|
|
46
|
+
this.pendingBytes = 0;
|
|
47
|
+
this.discarding = false;
|
|
48
|
+
}
|
|
49
|
+
append(bytes) {
|
|
50
|
+
if (this.discarding || bytes.length === 0) return;
|
|
51
|
+
const room = this.maxLineBytes - this.pendingBytes;
|
|
52
|
+
if (bytes.length <= room) {
|
|
53
|
+
this.pending.push(bytes);
|
|
54
|
+
this.pendingBytes += bytes.length;
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
this.pending.push(bytes.subarray(0, room));
|
|
58
|
+
this.pendingBytes += room;
|
|
59
|
+
this.events.lineCut(this.maxLineBytes);
|
|
60
|
+
this.emit();
|
|
61
|
+
this.discarding = true;
|
|
62
|
+
}
|
|
63
|
+
completeLine() {
|
|
64
|
+
if (this.discarding) {
|
|
65
|
+
this.discarding = false;
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
this.emit();
|
|
69
|
+
}
|
|
70
|
+
emit() {
|
|
71
|
+
let bytes = Buffer.concat(this.pending, this.pendingBytes);
|
|
72
|
+
this.pending = [];
|
|
73
|
+
this.pendingBytes = 0;
|
|
74
|
+
if (bytes.at(-1) === CR) bytes = bytes.subarray(0, -1);
|
|
75
|
+
const text = this.decoder.decode(bytes);
|
|
76
|
+
const isFirst = this.firstLine;
|
|
77
|
+
this.firstLine = false;
|
|
78
|
+
const header = isFirst ? HEADER.exec(text) : null;
|
|
79
|
+
if (header) {
|
|
80
|
+
this.events.header(header[1].trim());
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (text.trim() === "") return;
|
|
84
|
+
this.events.line(text);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
// src/adapters/in/tcp/tcp-listener.ts
|
|
88
|
+
/** Thrown by `start()` when the port can't be used (for example `EADDRINUSE`). */
|
|
89
|
+
var ListenError = class extends Error {
|
|
90
|
+
code;
|
|
91
|
+
name = "ListenError";
|
|
92
|
+
constructor(message, code) {
|
|
93
|
+
super(message);
|
|
94
|
+
this.code = code;
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Driving adapter for the socket voice: a TCP server that accepts any number of connections
|
|
99
|
+
* and feeds each one through its own parser into `RecordSpeech`
|
|
100
|
+
* (docs/speech-capture.md#listening-and-connections).
|
|
101
|
+
*/
|
|
102
|
+
var TcpListener = class {
|
|
103
|
+
recordSpeech;
|
|
104
|
+
logger;
|
|
105
|
+
server = null;
|
|
106
|
+
sockets = /* @__PURE__ */ new Set();
|
|
107
|
+
constructor(recordSpeech, logger) {
|
|
108
|
+
this.recordSpeech = recordSpeech;
|
|
109
|
+
this.logger = logger;
|
|
110
|
+
}
|
|
111
|
+
/** Starts listening. Resolves with the bound port (useful with port 0). */
|
|
112
|
+
async start(settings) {
|
|
113
|
+
if (this.server) throw new Error("TCP listener is already running");
|
|
114
|
+
const server = net.createServer((socket) => this.accept(socket, settings));
|
|
115
|
+
await new Promise((resolve, reject) => {
|
|
116
|
+
const onError = (error) => {
|
|
117
|
+
const reason = error.code === "EADDRINUSE" ? "the port is already in use (is another bridge running?)" : error.message;
|
|
118
|
+
reject(new ListenError(`Cannot listen on ${settings.host}:${settings.port}: ${reason}`, error.code));
|
|
119
|
+
};
|
|
120
|
+
server.once("error", onError);
|
|
121
|
+
server.listen(settings.port, settings.host, () => {
|
|
122
|
+
server.off("error", onError);
|
|
123
|
+
resolve();
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
server.on("error", (error) => this.logger.error(`TCP listener error: ${error.message}`));
|
|
127
|
+
this.server = server;
|
|
128
|
+
const port = server.address().port;
|
|
129
|
+
this.recordSpeech.listenerStarted({
|
|
130
|
+
host: settings.host,
|
|
131
|
+
port,
|
|
132
|
+
encoding: settings.encoding
|
|
133
|
+
});
|
|
134
|
+
return port;
|
|
135
|
+
}
|
|
136
|
+
/** Stops listening and closes all open connections. */
|
|
137
|
+
async stop() {
|
|
138
|
+
const server = this.server;
|
|
139
|
+
if (!server) return;
|
|
140
|
+
this.server = null;
|
|
141
|
+
const closed = new Promise((resolve) => server.close(() => resolve()));
|
|
142
|
+
for (const socket of this.sockets) socket.destroy();
|
|
143
|
+
await closed;
|
|
144
|
+
this.recordSpeech.listenerStopped();
|
|
145
|
+
}
|
|
146
|
+
accept(socket, settings) {
|
|
147
|
+
this.sockets.add(socket);
|
|
148
|
+
const id = this.recordSpeech.connectionOpened();
|
|
149
|
+
const parser = new TextToSocketParser(settings.encoding, settings.maxLineBytes, {
|
|
150
|
+
header: (voice) => this.recordSpeech.setVoice(id, voice),
|
|
151
|
+
line: (text) => this.recordSpeech.record(id, text),
|
|
152
|
+
lineCut: (max) => this.logger.warn(`connection ${id}: line longer than ${max} bytes, cut it`)
|
|
153
|
+
});
|
|
154
|
+
socket.on("data", (chunk) => parser.push(chunk));
|
|
155
|
+
socket.on("error", (error) => this.logger.debug(`connection ${id}: ${error.message}`));
|
|
156
|
+
socket.on("close", () => {
|
|
157
|
+
parser.end();
|
|
158
|
+
this.sockets.delete(socket);
|
|
159
|
+
this.recordSpeech.connectionClosed(id);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
// src/adapters/out/log/log.ts
|
|
164
|
+
const ESC = "\x1B[";
|
|
165
|
+
const RESET = `${ESC}0m`;
|
|
166
|
+
const PREFIX_COLOURS = {
|
|
167
|
+
bridge: `${ESC}1;37m`,
|
|
168
|
+
config: `${ESC}90m`,
|
|
169
|
+
"speech-capture": `${ESC}32m`,
|
|
170
|
+
"http-api": `${ESC}34m`,
|
|
171
|
+
"native-input": `${ESC}35m`,
|
|
172
|
+
"playwright-server": `${ESC}36m`,
|
|
173
|
+
"playwright-mcp": `${ESC}94m`
|
|
174
|
+
};
|
|
175
|
+
const LEVEL_COLOURS = {
|
|
176
|
+
error: `${ESC}31m`,
|
|
177
|
+
warn: `${ESC}33m`,
|
|
178
|
+
info: null,
|
|
179
|
+
debug: `${ESC}90m`
|
|
180
|
+
};
|
|
181
|
+
const LEVEL_RANK = {
|
|
182
|
+
error: 0,
|
|
183
|
+
warn: 1,
|
|
184
|
+
info: 2,
|
|
185
|
+
debug: 3
|
|
186
|
+
};
|
|
187
|
+
/** Longest prefix (`[playwright-server]`, 19 characters) plus one space. Fixed, so lines align. */
|
|
188
|
+
const PREFIX_WIDTH = Math.max(...Object.keys(PREFIX_COLOURS).map((p) => p.length + 2)) + 1;
|
|
189
|
+
function paint(text, colour, enabled) {
|
|
190
|
+
return enabled && colour !== null ? `${colour}${text}${RESET}` : text;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Formats one message as one or more log lines (without line breaks). Every line of a
|
|
194
|
+
* multi-line message gets the full prefix, timestamp and level. Padding is counted without
|
|
195
|
+
* colour codes.
|
|
196
|
+
*/
|
|
197
|
+
function formatLogLines(prefix, level, time, message, colour) {
|
|
198
|
+
const tag = `[${prefix}]`;
|
|
199
|
+
const head = paint(tag, PREFIX_COLOURS[prefix], colour) + " ".repeat(PREFIX_WIDTH - tag.length) + `${time.toISOString()} ` + paint(level.toUpperCase(), LEVEL_COLOURS[level], colour) + " ".repeat(6 - level.length);
|
|
200
|
+
return message.split(/\r?\n/).map((line) => head + line);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Decides whether a console stream gets colours: `NO_COLOR` turns them off, `FORCE_COLOR`
|
|
204
|
+
* turns them on, otherwise only on a terminal.
|
|
205
|
+
*/
|
|
206
|
+
function wantsColour(isTTY, env) {
|
|
207
|
+
if (env.NO_COLOR !== void 0 && env.NO_COLOR !== "") return false;
|
|
208
|
+
if (env.FORCE_COLOR !== void 0 && env.FORCE_COLOR !== "" && env.FORCE_COLOR !== "0") return true;
|
|
209
|
+
return isTTY === true;
|
|
210
|
+
}
|
|
211
|
+
/** Console sink on stdout. */
|
|
212
|
+
function consoleSink(stream = process.stdout, env = process.env) {
|
|
213
|
+
return {
|
|
214
|
+
colour: wantsColour(stream.isTTY, env),
|
|
215
|
+
write: (line) => void stream.write(`${line}\n`)
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
/** The bridge log: one level, several sinks, one `Logger` per prefix. */
|
|
219
|
+
var BridgeLog = class {
|
|
220
|
+
level;
|
|
221
|
+
now;
|
|
222
|
+
sinks;
|
|
223
|
+
constructor(level, sinks, now = () => /* @__PURE__ */ new Date()) {
|
|
224
|
+
this.level = level;
|
|
225
|
+
this.now = now;
|
|
226
|
+
this.sinks = [...sinks];
|
|
227
|
+
}
|
|
228
|
+
setLevel(level) {
|
|
229
|
+
this.level = level;
|
|
230
|
+
}
|
|
231
|
+
addSink(sink) {
|
|
232
|
+
this.sinks.push(sink);
|
|
233
|
+
}
|
|
234
|
+
removeSink(sink) {
|
|
235
|
+
const index = this.sinks.indexOf(sink);
|
|
236
|
+
if (index !== -1) this.sinks.splice(index, 1);
|
|
237
|
+
}
|
|
238
|
+
for(prefix) {
|
|
239
|
+
const log = (level) => (message) => this.write(prefix, level, message);
|
|
240
|
+
return {
|
|
241
|
+
error: log("error"),
|
|
242
|
+
warn: log("warn"),
|
|
243
|
+
info: log("info"),
|
|
244
|
+
debug: log("debug")
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
write(prefix, level, message) {
|
|
248
|
+
if (LEVEL_RANK[level] > LEVEL_RANK[this.level]) return;
|
|
249
|
+
const time = this.now();
|
|
250
|
+
for (const sink of this.sinks) for (const line of formatLogLines(prefix, level, time, message, sink.colour)) sink.write(line);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
// src/domain/errors.ts
|
|
254
|
+
/** An expected error with a stable code, for example a bad request or a timeout. */
|
|
255
|
+
var AppError = class extends Error {
|
|
256
|
+
code;
|
|
257
|
+
details;
|
|
258
|
+
name = "AppError";
|
|
259
|
+
constructor(code, message, details) {
|
|
260
|
+
super(message);
|
|
261
|
+
this.code = code;
|
|
262
|
+
this.details = details;
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
/** `INVALID_REQUEST` for one field. */
|
|
266
|
+
function invalidField(field, message) {
|
|
267
|
+
return new AppError("INVALID_REQUEST", `${field}: ${message}`, { field });
|
|
268
|
+
}
|
|
269
|
+
// src/domain/config.ts
|
|
270
|
+
const LOG_LEVELS = [
|
|
271
|
+
"error",
|
|
272
|
+
"warn",
|
|
273
|
+
"info",
|
|
274
|
+
"debug"
|
|
275
|
+
];
|
|
276
|
+
const DEFAULT_CONFIG = {
|
|
277
|
+
tcp: {
|
|
278
|
+
host: "127.0.0.1",
|
|
279
|
+
port: 4449,
|
|
280
|
+
format: "text-to-socket",
|
|
281
|
+
encoding: "windows-1252",
|
|
282
|
+
maxLineBytes: 65536
|
|
283
|
+
},
|
|
284
|
+
http: {
|
|
285
|
+
host: "0.0.0.0",
|
|
286
|
+
port: 5151
|
|
287
|
+
},
|
|
288
|
+
playwright: {
|
|
289
|
+
enabled: true,
|
|
290
|
+
port: 3e3,
|
|
291
|
+
version: null
|
|
292
|
+
},
|
|
293
|
+
mcp: {
|
|
294
|
+
enabled: true,
|
|
295
|
+
port: 8931,
|
|
296
|
+
version: null
|
|
297
|
+
},
|
|
298
|
+
input: { enabled: true },
|
|
299
|
+
buffer: { maxEntries: 1e4 },
|
|
300
|
+
log: {
|
|
301
|
+
level: "info",
|
|
302
|
+
file: null
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
const integer = (min, max) => (value) => typeof value === "number" && Number.isInteger(value) && value >= min && value <= max ? void 0 : `must be an integer from ${min} to ${max}`;
|
|
306
|
+
const port = integer(0, 65535);
|
|
307
|
+
const boolean = (value) => typeof value === "boolean" ? void 0 : "must be true or false";
|
|
308
|
+
const nonEmptyString = (value) => typeof value === "string" && value.trim() !== "" ? void 0 : "must be a non-empty string";
|
|
309
|
+
const nullableString = (value) => value === null ? void 0 : nonEmptyString(value) && "must be a non-empty string or null";
|
|
310
|
+
const oneOf = (values) => (value) => typeof value === "string" && values.includes(value) ? void 0 : `must be one of ${values.join(", ")}`;
|
|
311
|
+
const encoding = (value) => {
|
|
312
|
+
if (typeof value !== "string") return "must be a string";
|
|
313
|
+
try {
|
|
314
|
+
new TextDecoder(value);
|
|
315
|
+
return;
|
|
316
|
+
} catch {
|
|
317
|
+
return `unknown encoding "${value}"`;
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
const SCHEMA = {
|
|
321
|
+
tcp: {
|
|
322
|
+
host: nonEmptyString,
|
|
323
|
+
port,
|
|
324
|
+
format: oneOf(["text-to-socket"]),
|
|
325
|
+
encoding,
|
|
326
|
+
maxLineBytes: integer(1024, 16777216)
|
|
327
|
+
},
|
|
328
|
+
http: {
|
|
329
|
+
host: nonEmptyString,
|
|
330
|
+
port
|
|
331
|
+
},
|
|
332
|
+
playwright: {
|
|
333
|
+
enabled: boolean,
|
|
334
|
+
port,
|
|
335
|
+
version: nullableString
|
|
336
|
+
},
|
|
337
|
+
mcp: {
|
|
338
|
+
enabled: boolean,
|
|
339
|
+
port,
|
|
340
|
+
version: nullableString
|
|
341
|
+
},
|
|
342
|
+
input: { enabled: boolean },
|
|
343
|
+
buffer: { maxEntries: integer(1, 1e6) },
|
|
344
|
+
log: {
|
|
345
|
+
level: oneOf(LOG_LEVELS),
|
|
346
|
+
file: nullableString
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
function isPlainObject(value) {
|
|
350
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Applies a partial configuration to `base` and validates the result.
|
|
354
|
+
* Throws `INVALID_REQUEST` naming the first invalid or unknown field (`details.field`).
|
|
355
|
+
*/
|
|
356
|
+
function mergeConfig(base, patch) {
|
|
357
|
+
if (patch === void 0 || patch === null) return base;
|
|
358
|
+
if (!isPlainObject(patch)) throw invalidField("config", "must be an object");
|
|
359
|
+
const result = {};
|
|
360
|
+
for (const section of Object.keys(base)) result[section] = { ...base[section] };
|
|
361
|
+
for (const [section, values] of Object.entries(patch)) {
|
|
362
|
+
const sectionSchema = SCHEMA[section];
|
|
363
|
+
if (sectionSchema === void 0) throw invalidField(section, "unknown setting");
|
|
364
|
+
if (values === void 0) continue;
|
|
365
|
+
if (!isPlainObject(values)) throw invalidField(section, "must be an object");
|
|
366
|
+
for (const [key, value] of Object.entries(values)) {
|
|
367
|
+
const field = `${section}.${key}`;
|
|
368
|
+
const check = sectionSchema[key];
|
|
369
|
+
if (check === void 0) throw invalidField(field, "unknown setting");
|
|
370
|
+
if (value === void 0) continue;
|
|
371
|
+
const problem = check(value);
|
|
372
|
+
if (problem !== void 0) throw invalidField(field, problem);
|
|
373
|
+
result[section][key] = value;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
return result;
|
|
377
|
+
}
|
|
378
|
+
// src/adapters/out/clock/system-clock.ts
|
|
379
|
+
var SystemClock = class {
|
|
380
|
+
now() {
|
|
381
|
+
return /* @__PURE__ */ new Date();
|
|
382
|
+
}
|
|
383
|
+
monotonicMs() {
|
|
384
|
+
return performance.now();
|
|
385
|
+
}
|
|
386
|
+
setTimeout(callback, ms) {
|
|
387
|
+
return setTimeout(callback, ms);
|
|
388
|
+
}
|
|
389
|
+
clearTimeout(handle) {
|
|
390
|
+
clearTimeout(handle);
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
// src/adapters/out/clock/ulid-generator.ts
|
|
394
|
+
/** ULIDs, strictly increasing even within the same millisecond. */
|
|
395
|
+
var UlidGenerator = class {
|
|
396
|
+
ulid = monotonicFactory();
|
|
397
|
+
next() {
|
|
398
|
+
return this.ulid();
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
// src/adapters/out/memory/in-process-event-bus.ts
|
|
402
|
+
/** Synchronous in-process event bus. A failing listener doesn't stop the others. */
|
|
403
|
+
var InProcessEventBus = class {
|
|
404
|
+
onListenerError;
|
|
405
|
+
listeners = /* @__PURE__ */ new Set();
|
|
406
|
+
constructor(onListenerError = () => {}) {
|
|
407
|
+
this.onListenerError = onListenerError;
|
|
408
|
+
}
|
|
409
|
+
publish(event) {
|
|
410
|
+
for (const listener of [...this.listeners]) try {
|
|
411
|
+
listener(event);
|
|
412
|
+
} catch (error) {
|
|
413
|
+
this.onListenerError(error);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
subscribe(listener) {
|
|
417
|
+
this.listeners.add(listener);
|
|
418
|
+
return () => this.listeners.delete(listener);
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
// src/adapters/out/memory/ring-buffer-repository.ts
|
|
422
|
+
/**
|
|
423
|
+
* In-memory ring buffer (docs/speech-capture.md#buffer). When it is full, the oldest entries
|
|
424
|
+
* are dropped. `seq` keeps counting across clears.
|
|
425
|
+
*/
|
|
426
|
+
var RingBufferRepository = class {
|
|
427
|
+
maxEntries;
|
|
428
|
+
/** Entries in `seq` order. Indexes before `start` are dropped and compacted away later. */
|
|
429
|
+
entries = [];
|
|
430
|
+
start = 0;
|
|
431
|
+
nextSeq = 1;
|
|
432
|
+
/** Highest `seq` dropped because the buffer was full, since the last clear. */
|
|
433
|
+
droppedUpToSeq = 0;
|
|
434
|
+
constructor(maxEntries) {
|
|
435
|
+
this.maxEntries = maxEntries;
|
|
436
|
+
}
|
|
437
|
+
add(entry) {
|
|
438
|
+
const stored = {
|
|
439
|
+
...entry,
|
|
440
|
+
seq: this.nextSeq++
|
|
441
|
+
};
|
|
442
|
+
this.entries.push(stored);
|
|
443
|
+
this.dropOverflow();
|
|
444
|
+
return stored;
|
|
445
|
+
}
|
|
446
|
+
list(since, limit) {
|
|
447
|
+
const from = this.indexAfter(since);
|
|
448
|
+
return {
|
|
449
|
+
entries: this.entries.slice(from, from + limit),
|
|
450
|
+
truncated: since < this.droppedUpToSeq
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
clear() {
|
|
454
|
+
this.entries = [];
|
|
455
|
+
this.start = 0;
|
|
456
|
+
this.droppedUpToSeq = 0;
|
|
457
|
+
}
|
|
458
|
+
resize(maxEntries) {
|
|
459
|
+
this.maxEntries = maxEntries;
|
|
460
|
+
this.dropOverflow();
|
|
461
|
+
}
|
|
462
|
+
stats() {
|
|
463
|
+
const size = this.entries.length - this.start;
|
|
464
|
+
return {
|
|
465
|
+
size,
|
|
466
|
+
maxEntries: this.maxEntries,
|
|
467
|
+
firstSeq: size > 0 ? this.entries[this.start].seq : null,
|
|
468
|
+
lastSeq: size > 0 ? this.entries[this.entries.length - 1].seq : null
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
dropOverflow() {
|
|
472
|
+
while (this.entries.length - this.start > this.maxEntries) {
|
|
473
|
+
this.droppedUpToSeq = this.entries[this.start].seq;
|
|
474
|
+
this.start++;
|
|
475
|
+
}
|
|
476
|
+
if (this.start > 0 && this.start >= this.entries.length - this.start) {
|
|
477
|
+
this.entries = this.entries.slice(this.start);
|
|
478
|
+
this.start = 0;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
/** Index of the first live entry with `seq > since` (binary search, entries are sorted). */
|
|
482
|
+
indexAfter(since) {
|
|
483
|
+
let low = this.start;
|
|
484
|
+
let high = this.entries.length;
|
|
485
|
+
while (low < high) {
|
|
486
|
+
const mid = low + high >>> 1;
|
|
487
|
+
if (this.entries[mid].seq <= since) low = mid + 1;
|
|
488
|
+
else high = mid;
|
|
489
|
+
}
|
|
490
|
+
return low;
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
// src/application/services/record-speech-service.ts
|
|
494
|
+
/**
|
|
495
|
+
* Turns lines from the socket voice into speech entries (docs/speech-capture.md#from-line-to-speech-entry)
|
|
496
|
+
* and keeps track of the listener and its open connections for the status.
|
|
497
|
+
*/
|
|
498
|
+
var RecordSpeechService = class {
|
|
499
|
+
deps;
|
|
500
|
+
open = /* @__PURE__ */ new Map();
|
|
501
|
+
nextConnectionId = 1;
|
|
502
|
+
listener = null;
|
|
503
|
+
constructor(deps) {
|
|
504
|
+
this.deps = deps;
|
|
505
|
+
}
|
|
506
|
+
listenerStarted(info) {
|
|
507
|
+
this.listener = {
|
|
508
|
+
listening: true,
|
|
509
|
+
...info
|
|
510
|
+
};
|
|
511
|
+
this.deps.logger.info(`listening on ${info.host}:${info.port}, encoding ${info.encoding}`);
|
|
512
|
+
}
|
|
513
|
+
listenerStopped() {
|
|
514
|
+
if (this.listener) this.listener = {
|
|
515
|
+
...this.listener,
|
|
516
|
+
listening: false
|
|
517
|
+
};
|
|
518
|
+
this.deps.logger.info("stopped listening");
|
|
519
|
+
}
|
|
520
|
+
connectionOpened() {
|
|
521
|
+
const id = this.nextConnectionId++;
|
|
522
|
+
this.open.set(id, {
|
|
523
|
+
id,
|
|
524
|
+
voice: null,
|
|
525
|
+
connectedAt: this.deps.clock.now()
|
|
526
|
+
});
|
|
527
|
+
this.deps.logger.info(`connection ${id} opened`);
|
|
528
|
+
return id;
|
|
529
|
+
}
|
|
530
|
+
setVoice(connectionId, voice) {
|
|
531
|
+
const connection = this.open.get(connectionId);
|
|
532
|
+
if (connection) connection.voice = voice;
|
|
533
|
+
this.deps.logger.info(`connection ${connectionId}: voice=${voice}`);
|
|
534
|
+
}
|
|
535
|
+
record(connectionId, text) {
|
|
536
|
+
const { repository, events, clock, ids, logger } = this.deps;
|
|
537
|
+
const entry = repository.add({
|
|
538
|
+
id: ids.next(),
|
|
539
|
+
kind: "speech",
|
|
540
|
+
text,
|
|
541
|
+
receivedAt: clock.now(),
|
|
542
|
+
monotonicMs: clock.monotonicMs(),
|
|
543
|
+
source: "tcp",
|
|
544
|
+
voice: this.open.get(connectionId)?.voice ?? null,
|
|
545
|
+
connectionId
|
|
546
|
+
});
|
|
547
|
+
logger.debug(`#${entry.seq} (connection ${connectionId}) ${text}`);
|
|
548
|
+
events.publish({
|
|
549
|
+
type: "entry",
|
|
550
|
+
entry
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
connectionClosed(connectionId) {
|
|
554
|
+
this.open.delete(connectionId);
|
|
555
|
+
this.deps.logger.info(`connection ${connectionId} closed`);
|
|
556
|
+
}
|
|
557
|
+
/** The listener, or `null` before it was started for the first time. */
|
|
558
|
+
listenerState() {
|
|
559
|
+
return this.listener && { ...this.listener };
|
|
560
|
+
}
|
|
561
|
+
/** Open connections, oldest first. */
|
|
562
|
+
connections() {
|
|
563
|
+
return [...this.open.values()].map((connection) => ({ ...connection }));
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
// src/application/services/speech-service.ts
|
|
567
|
+
const MAX_MARKER_LENGTH = 1e3;
|
|
568
|
+
/** Reading, clearing and marking the recorded speech. */
|
|
569
|
+
var SpeechService = class {
|
|
570
|
+
deps;
|
|
571
|
+
constructor(deps) {
|
|
572
|
+
this.deps = deps;
|
|
573
|
+
}
|
|
574
|
+
list(options) {
|
|
575
|
+
return this.deps.repository.list(options.since, options.limit);
|
|
576
|
+
}
|
|
577
|
+
clear() {
|
|
578
|
+
this.deps.repository.clear();
|
|
579
|
+
this.deps.events.publish({ type: "clear" });
|
|
580
|
+
}
|
|
581
|
+
addMarker(label) {
|
|
582
|
+
const text = label.trim();
|
|
583
|
+
if (text === "") throw invalidField("label", "must not be empty");
|
|
584
|
+
if (text.length > 1e3) throw invalidField("label", `must be at most ${MAX_MARKER_LENGTH} characters`);
|
|
585
|
+
const { repository, events, clock, ids } = this.deps;
|
|
586
|
+
const entry = repository.add({
|
|
587
|
+
id: ids.next(),
|
|
588
|
+
kind: "marker",
|
|
589
|
+
text,
|
|
590
|
+
receivedAt: clock.now(),
|
|
591
|
+
monotonicMs: clock.monotonicMs(),
|
|
592
|
+
source: "api",
|
|
593
|
+
voice: null,
|
|
594
|
+
connectionId: null
|
|
595
|
+
});
|
|
596
|
+
events.publish({
|
|
597
|
+
type: "entry",
|
|
598
|
+
entry
|
|
599
|
+
});
|
|
600
|
+
return entry;
|
|
601
|
+
}
|
|
602
|
+
};
|
|
603
|
+
// src/main.ts
|
|
604
|
+
async function startBridge(options) {
|
|
605
|
+
const { config, version } = options;
|
|
606
|
+
const log = options.log ?? new BridgeLog(config.log.level, [consoleSink()]);
|
|
607
|
+
const bridgeLogger = log.for("bridge");
|
|
608
|
+
bridgeLogger.info(`starting, version ${version}`);
|
|
609
|
+
const clock = options.clock ?? new SystemClock();
|
|
610
|
+
const ids = new UlidGenerator();
|
|
611
|
+
const repository = new RingBufferRepository(config.buffer.maxEntries);
|
|
612
|
+
const events = new InProcessEventBus((error) => bridgeLogger.error(`event listener failed: ${String(error)}`));
|
|
613
|
+
const recorder = new RecordSpeechService({
|
|
614
|
+
repository,
|
|
615
|
+
events,
|
|
616
|
+
clock,
|
|
617
|
+
ids,
|
|
618
|
+
logger: log.for("speech-capture")
|
|
619
|
+
});
|
|
620
|
+
const speech = new SpeechService({
|
|
621
|
+
repository,
|
|
622
|
+
events,
|
|
623
|
+
clock,
|
|
624
|
+
ids
|
|
625
|
+
});
|
|
626
|
+
const tcp = new TcpListener(recorder, log.for("speech-capture"));
|
|
627
|
+
const tcpPort = await tcp.start(config.tcp);
|
|
628
|
+
bridgeLogger.info("ready");
|
|
629
|
+
return {
|
|
630
|
+
log,
|
|
631
|
+
tcpPort,
|
|
632
|
+
speech,
|
|
633
|
+
recorder,
|
|
634
|
+
async stop() {
|
|
635
|
+
bridgeLogger.info("stopping");
|
|
636
|
+
await tcp.stop();
|
|
637
|
+
bridgeLogger.info("stopped");
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
// src/cli.ts
|
|
642
|
+
function configFromEnv(env) {
|
|
643
|
+
const port = env.SR_SPEECH_TCP_PORT;
|
|
644
|
+
return mergeConfig(DEFAULT_CONFIG, {
|
|
645
|
+
tcp: {
|
|
646
|
+
port: port === void 0 ? void 0 : Number(port),
|
|
647
|
+
encoding: env.SR_SPEECH_TCP_ENCODING
|
|
648
|
+
},
|
|
649
|
+
log: { level: env.SR_SPEECH_LOG_LEVEL }
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
let config;
|
|
653
|
+
try {
|
|
654
|
+
config = configFromEnv(process.env);
|
|
655
|
+
} catch (error) {
|
|
656
|
+
process.stderr.write(`screenreader-speech-capture: ${error.message}\n`);
|
|
657
|
+
process.exit(2);
|
|
658
|
+
}
|
|
659
|
+
const log = new BridgeLog(config.log.level, [consoleSink()]);
|
|
660
|
+
try {
|
|
661
|
+
const bridge = await startBridge({
|
|
662
|
+
config,
|
|
663
|
+
version,
|
|
664
|
+
log
|
|
665
|
+
});
|
|
666
|
+
let stopping = false;
|
|
667
|
+
const shutdown = () => {
|
|
668
|
+
if (stopping) return;
|
|
669
|
+
stopping = true;
|
|
670
|
+
bridge.stop().then(() => process.exit(0), (error) => {
|
|
671
|
+
log.for("bridge").error(`shutdown failed: ${String(error)}`);
|
|
672
|
+
process.exit(1);
|
|
673
|
+
});
|
|
674
|
+
};
|
|
675
|
+
for (const signal of [
|
|
676
|
+
"SIGINT",
|
|
677
|
+
"SIGTERM",
|
|
678
|
+
"SIGHUP"
|
|
679
|
+
]) process.on(signal, shutdown);
|
|
680
|
+
} catch (error) {
|
|
681
|
+
const message = error instanceof ListenError ? error.message : String(error);
|
|
682
|
+
log.for("bridge").error(message);
|
|
683
|
+
process.exit(1);
|
|
684
|
+
}
|
|
685
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "screenreader-speech-capture",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.1-preview.1",
|
|
4
|
+
"description": "Capture what a screen reader says (JAWS / NVDA via a SAPI 5 socket voice) and expose it over HTTP for automated tests.",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=22.12"
|
|
9
|
+
},
|
|
10
|
+
"bin": {
|
|
11
|
+
"screenreader-speech-capture": "./dist/cli.mjs"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"ulid": "^3.0.2"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@screenreader-speech-capture/shared": "0.0.0",
|
|
21
|
+
"tsdown": "^0.23.7"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsdown",
|
|
25
|
+
"dev": "tsx watch src/cli.ts",
|
|
26
|
+
"typecheck": "tsc --noEmit"
|
|
27
|
+
}
|
|
6
28
|
}
|
package/README.md
DELETED