@adea-ai/voice 0.2.0 → 0.4.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.
Files changed (45) hide show
  1. package/README.md +86 -1
  2. package/dist/cli/helpers-cli.js +17 -2
  3. package/dist/contract/contract.d.ts +21 -2
  4. package/dist/contract/index.d.ts +1 -0
  5. package/dist/contract/index.js +12 -2
  6. package/dist/contract/session.d.ts +13 -0
  7. package/dist/contract/turns.d.ts +91 -0
  8. package/dist/contract/wire.d.ts +92 -1
  9. package/dist/desktop/conversation.d.ts +86 -0
  10. package/dist/desktop/desktop-voice.d.ts +61 -1
  11. package/dist/desktop/helper-lock.d.ts +45 -7
  12. package/dist/desktop/helper-process.d.ts +4 -0
  13. package/dist/desktop/index.d.ts +2 -0
  14. package/dist/desktop/index.js +427 -15
  15. package/dist/desktop/macho-digest.d.ts +9 -0
  16. package/dist/{index-rehs788p.js → index-7n7epq1a.js} +7 -2
  17. package/dist/{index-s270cw30.js → index-a7d617zr.js} +1 -1
  18. package/dist/{index-9kerqn9r.js → index-ght8wwpd.js} +9 -3
  19. package/dist/{index-acm1yvvv.js → index-m4g2tpeq.js} +258 -26
  20. package/dist/index.js +13 -3
  21. package/dist/settings/index.js +1 -1
  22. package/dist/web/index.d.ts +1 -0
  23. package/dist/web/index.js +459 -49
  24. package/dist/web/stt/speech-recognition.d.ts +128 -7
  25. package/dist/web/tts/locale-match.d.ts +40 -0
  26. package/dist/web/tts/system-voices.d.ts +44 -4
  27. package/dist/web/turns.d.ts +15 -25
  28. package/package.json +1 -1
  29. package/src/cli/helpers-cli.ts +18 -2
  30. package/src/contract/contract.ts +32 -4
  31. package/src/contract/index.ts +1 -0
  32. package/src/contract/session.ts +14 -2
  33. package/src/contract/turns.ts +48 -0
  34. package/src/contract/wire.ts +123 -2
  35. package/src/desktop/conversation.ts +347 -0
  36. package/src/desktop/desktop-voice.ts +197 -1
  37. package/src/desktop/helper-lock.ts +195 -20
  38. package/src/desktop/helper-process.ts +17 -10
  39. package/src/desktop/index.ts +2 -0
  40. package/src/desktop/macho-digest.ts +135 -0
  41. package/src/web/index.ts +1 -0
  42. package/src/web/stt/speech-recognition.ts +454 -24
  43. package/src/web/tts/locale-match.ts +131 -0
  44. package/src/web/tts/system-voices.ts +144 -35
  45. package/src/web/turns.ts +22 -11
@@ -39,6 +39,8 @@ export type HelperSpawner = (path: string, args: readonly string[], options: Hel
39
39
 
40
40
  export interface HelperConnectionOptions {
41
41
  readonly path: string;
42
+ /** Names the helper in errors and logs, e.g. `adea-transcribe-helper`. Default "voice helper". */
43
+ readonly name?: string;
42
44
  readonly spawn: HelperSpawner;
43
45
  readonly env?: Readonly<Record<string, string | undefined>>;
44
46
  /** How long `--version` may take. Default 5 s. */
@@ -126,6 +128,11 @@ export class HelperConnection {
126
128
 
127
129
  constructor(private readonly options: HelperConnectionOptions) {}
128
130
 
131
+ /** "the voice helper", or the configured helper name. */
132
+ private get label(): string {
133
+ return this.options.name ?? "the voice helper";
134
+ }
135
+
129
136
  /** The handshake the running helper answered, once it has started. */
130
137
  get version(): VersionEvent | undefined {
131
138
  return this.handshake;
@@ -211,7 +218,7 @@ export class HelperConnection {
211
218
  this.child = undefined;
212
219
  this.starting = undefined;
213
220
  if (!child) return;
214
- this.failAll(new VoiceError("cancelled", "the voice helper was stopped"));
221
+ this.failAll(new VoiceError("cancelled", `${this.label} was stopped`));
215
222
  await new Promise<void>((resolve) => {
216
223
  let exited = false;
217
224
  child.onExit(() => {
@@ -239,13 +246,13 @@ export class HelperConnection {
239
246
  async dispose(): Promise<void> {
240
247
  this.disposed = true;
241
248
  await this.stop();
242
- this.failAll(new VoiceError("cancelled", "the voice helper was shut down"));
249
+ this.failAll(new VoiceError("cancelled", `${this.label} was shut down`));
243
250
  }
244
251
 
245
252
  // MARK: - Process
246
253
 
247
254
  private async ensureStarted(): Promise<HelperChild> {
248
- if (this.disposed) throw new VoiceError("unavailable", "the voice helper was shut down");
255
+ if (this.disposed) throw new VoiceError("unavailable", `${this.label} was shut down`);
249
256
  if (this.incompatible) throw this.incompatible;
250
257
  if (this.child) return this.child;
251
258
  if (!this.starting) {
@@ -261,7 +268,7 @@ export class HelperConnection {
261
268
  const window = this.options.crashWindowMs ?? 60_000;
262
269
  this.crashes = this.crashes.filter((at) => now() - at < window);
263
270
  if (this.crashes.length >= (this.options.maxCrashes ?? 3)) {
264
- throw new VoiceError("unavailable", "the voice helper keeps crashing; restart the app to try again");
271
+ throw new VoiceError("unavailable", `${this.label} keeps crashing; restart the app to try again`);
265
272
  }
266
273
  let version: VersionEvent;
267
274
  try {
@@ -274,7 +281,7 @@ export class HelperConnection {
274
281
  }
275
282
  if (!isCompatibleHandshake(version)) {
276
283
  this.incompatible = new IncompatibleHelperError(
277
- `voice helper speaks wire ${version.wire} / contract ${version.contract}; this app needs wire 1 / contract 1`,
284
+ `${this.options.name ?? "voice helper"} speaks wire ${version.wire} / contract ${version.contract}; this app needs wire 1 / contract 1`,
278
285
  );
279
286
  throw this.incompatible;
280
287
  }
@@ -284,13 +291,13 @@ export class HelperConnection {
284
291
  child.onStdout((chunk) => {
285
292
  for (const line of splitter.push(chunk)) this.dispatch(line);
286
293
  });
287
- child.onStderr?.((chunk) => this.options.log?.(`voice helper: ${chunk.trimEnd().slice(0, 500)}`));
294
+ child.onStderr?.((chunk) => this.options.log?.(`${this.options.name ?? "voice helper"}: ${chunk.trimEnd().slice(0, 500)}`));
288
295
  child.onExit((code, signal) => {
289
296
  if (this.child !== child) return;
290
297
  this.child = undefined;
291
298
  if (!this.disposed) this.crashes.push(now());
292
299
  this.failAll(
293
- new VoiceError("failed", `the voice helper exited (${signal ?? `code ${String(code)}`}); it restarts on the next request`),
300
+ new VoiceError("failed", `${this.label} exited (${signal ?? `code ${String(code)}`}); it restarts on the next request`),
294
301
  );
295
302
  });
296
303
  this.child = child;
@@ -309,7 +316,7 @@ export class HelperConnection {
309
316
  outcome();
310
317
  };
311
318
  const timer = setTimeout(() => {
312
- settle(() => reject(new VoiceError("unavailable", "the voice helper did not answer its version handshake")));
319
+ settle(() => reject(new VoiceError("unavailable", `${this.label} did not answer its version handshake`)));
313
320
  child.kill("SIGKILL");
314
321
  }, this.options.handshakeTimeoutMs ?? 5_000);
315
322
  child.onStdout((chunk) => {
@@ -319,7 +326,7 @@ export class HelperConnection {
319
326
  }
320
327
  });
321
328
  child.onExit((code) => {
322
- settle(() => reject(new VoiceError("unavailable", `the voice helper failed to start (exit ${String(code)})`)));
329
+ settle(() => reject(new VoiceError("unavailable", `${this.label} failed to start (exit ${String(code)})`)));
323
330
  });
324
331
  try {
325
332
  child.closeInput();
@@ -332,7 +339,7 @@ export class HelperConnection {
332
339
  private dispatch(line: string): void {
333
340
  const event = parseWireLine(line);
334
341
  if (!event) {
335
- this.options.log?.(`voice helper: ignored a malformed line (${line.slice(0, 80)})`);
342
+ this.options.log?.(`${this.options.name ?? "voice helper"}: ignored a malformed line (${line.slice(0, 80)})`);
336
343
  return;
337
344
  }
338
345
  const id = correlationId(event);
@@ -3,6 +3,7 @@
3
3
  * from a desktop shell process only (it spawns processes); a renderer talks to the shell.
4
4
  */
5
5
  export * from "./desktop-voice";
6
+ export type { ConversationRespondOptions, DesktopConversation } from "./conversation";
6
7
  export {
7
8
  HelperConnection,
8
9
  IncompatibleHelperError,
@@ -14,3 +15,4 @@ export {
14
15
  type HelperSpawner,
15
16
  } from "./helper-process";
16
17
  export * from "./helper-lock";
18
+ export { isMachO, machoCodeDigest, machoCodeDigestFile } from "./macho-digest";
@@ -0,0 +1,135 @@
1
+ /**
2
+ * A digest of a Mach-O file's code that does not change when the file is signed again.
3
+ *
4
+ * A host app's release build signs every nested executable again (its own identity, the hardened
5
+ * runtime), so the packaged helper's file sha256 never equals the one the lock pins. This digest
6
+ * covers everything except the code signature: the signature blob, the `LC_CODE_SIGNATURE` load
7
+ * command that points at it, the `__LINKEDIT` sizes that grow with it, and trailing zero padding
8
+ * are left out. Any other byte that differs, code or data, changes it.
9
+ *
10
+ * Handles thin 64-bit little-endian images with the signature command anywhere in the table (the
11
+ * transcribe helper gains rpaths after it was first signed, so its signature command is not last),
12
+ * and universal (fat) files, whose slices are digested one by one: re-signing moves slices, so the
13
+ * fat header's offsets and sizes are not part of the digest.
14
+ *
15
+ * For a thin image whose signature command is last, the result equals the digest Adea's
16
+ * `macho-code-digest.mjs` computes for Herdr Core.
17
+ */
18
+ import { createHash } from "node:crypto";
19
+ import { readFileSync } from "node:fs";
20
+
21
+ const MH_MAGIC_64 = 0xfeedfacf;
22
+ const FAT_MAGIC = 0xcafebabe;
23
+ const FAT_MAGIC_64 = 0xcafebabf;
24
+ const LC_SEGMENT_64 = 0x19;
25
+ const LC_CODE_SIGNATURE = 0x1d;
26
+ const HEADER_SIZE = 32;
27
+ const SIGNATURE_COMMAND_SIZE = 16;
28
+ /** More slices than this is not a Mach-O universal file (Java class files share the magic). */
29
+ const MAX_FAT_ARCHS = 16;
30
+
31
+ /** True when `bytes` starts like a Mach-O image or a universal file. */
32
+ export function isMachO(bytes: Uint8Array): boolean {
33
+ if (bytes.byteLength < 8) return false;
34
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
35
+ const little = view.getUint32(0, true);
36
+ if (little === MH_MAGIC_64 || little === 0xfeedface) return true;
37
+ const big = view.getUint32(0, false);
38
+ return (big === FAT_MAGIC || big === FAT_MAGIC_64) && view.getUint32(4, false) > 0 && view.getUint32(4, false) <= MAX_FAT_ARCHS;
39
+ }
40
+
41
+ /**
42
+ * The sha256 (hex) of a Mach-O file with its code signature left out, or `undefined` for a file
43
+ * this cannot digest (not Mach-O, 32-bit, big-endian, or malformed).
44
+ */
45
+ export function machoCodeDigest(bytes: Uint8Array): string | undefined {
46
+ if (bytes.byteLength < 8) return undefined;
47
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
48
+ const magic = view.getUint32(0, false);
49
+ if (magic === FAT_MAGIC || magic === FAT_MAGIC_64) return fatDigest(bytes, view, magic === FAT_MAGIC_64);
50
+ return thinDigest(bytes);
51
+ }
52
+
53
+ /** `machoCodeDigest` of a file on disk. */
54
+ export function machoCodeDigestFile(path: string): string | undefined {
55
+ return machoCodeDigest(new Uint8Array(readFileSync(path)));
56
+ }
57
+
58
+ function fatDigest(bytes: Uint8Array, view: DataView, wide: boolean): string | undefined {
59
+ const count = view.getUint32(4, false);
60
+ if (count === 0 || count > MAX_FAT_ARCHS) return undefined;
61
+ const entrySize = wide ? 32 : 20;
62
+ if (8 + count * entrySize > bytes.byteLength) return undefined;
63
+ const lines: string[] = [];
64
+ for (let index = 0; index < count; index += 1) {
65
+ const at = 8 + index * entrySize;
66
+ const cpuType = view.getUint32(at, false);
67
+ const cpuSubtype = view.getUint32(at + 4, false);
68
+ const offset = wide ? Number(view.getBigUint64(at + 8, false)) : view.getUint32(at + 8, false);
69
+ const size = wide ? Number(view.getBigUint64(at + 16, false)) : view.getUint32(at + 12, false);
70
+ if (offset + size > bytes.byteLength || size < HEADER_SIZE) return undefined;
71
+ const slice = thinDigest(bytes.subarray(offset, offset + size));
72
+ if (!slice) return undefined;
73
+ lines.push(`${cpuType}:${cpuSubtype}:${slice}\n`);
74
+ }
75
+ lines.sort();
76
+ return createHash("sha256").update(`fat\n${lines.join("")}`).digest("hex");
77
+ }
78
+
79
+ function thinDigest(bytes: Uint8Array): string | undefined {
80
+ if (bytes.byteLength < HEADER_SIZE) return undefined;
81
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
82
+ if (view.getUint32(0, true) !== MH_MAGIC_64) return undefined;
83
+ const commandCount = view.getUint32(16, true);
84
+ const commandsSize = view.getUint32(20, true);
85
+ const commandsEnd = HEADER_SIZE + commandsSize;
86
+ if (commandsEnd > bytes.byteLength) return undefined;
87
+
88
+ let signature: { offset: number; dataOffset: number } | undefined;
89
+ const linkedit: number[] = [];
90
+ let offset = HEADER_SIZE;
91
+ for (let index = 0; index < commandCount; index += 1) {
92
+ if (offset + 8 > commandsEnd) return undefined;
93
+ const command = view.getUint32(offset, true);
94
+ const size = view.getUint32(offset + 4, true);
95
+ if (size < 8 || offset + size > commandsEnd) return undefined;
96
+ if (command === LC_CODE_SIGNATURE) {
97
+ if (signature || size !== SIGNATURE_COMMAND_SIZE) return undefined;
98
+ signature = { offset, dataOffset: view.getUint32(offset + 8, true) };
99
+ } else if (command === LC_SEGMENT_64 && size >= 72 && segmentName(bytes, offset + 8) === "__LINKEDIT") {
100
+ linkedit.push(offset);
101
+ }
102
+ offset += size;
103
+ }
104
+ if (offset !== commandsEnd) return undefined;
105
+
106
+ const end = signature ? signature.dataOffset : bytes.byteLength;
107
+ if (end > bytes.byteLength || end < commandsEnd) return undefined;
108
+ const code = Buffer.from(bytes.subarray(0, end));
109
+ // __LINKEDIT's vmsize and filesize include the signature; zero them before moving commands.
110
+ for (const segment of linkedit) {
111
+ code.fill(0, segment + 32, segment + 40);
112
+ code.fill(0, segment + 48, segment + 56);
113
+ }
114
+ if (signature) {
115
+ // Take the signature command out of the table: later commands move up, the freed bytes at the
116
+ // end of the table become zero, as if the file had never been signed.
117
+ code.copy(code, signature.offset, signature.offset + SIGNATURE_COMMAND_SIZE, commandsEnd);
118
+ code.fill(0, commandsEnd - SIGNATURE_COMMAND_SIZE, commandsEnd);
119
+ code.writeUInt32LE(commandCount - 1, 16);
120
+ code.writeUInt32LE(commandsSize - SIGNATURE_COMMAND_SIZE, 20);
121
+ }
122
+ let length = code.length;
123
+ while (length > 0 && code[length - 1] === 0) length -= 1;
124
+ return createHash("sha256").update(code.subarray(0, length)).digest("hex");
125
+ }
126
+
127
+ function segmentName(bytes: Uint8Array, at: number): string {
128
+ let name = "";
129
+ for (let index = 0; index < 16; index += 1) {
130
+ const byte = bytes[at + index]!;
131
+ if (byte === 0) break;
132
+ name += String.fromCharCode(byte);
133
+ }
134
+ return name;
135
+ }
package/src/web/index.ts CHANGED
@@ -10,6 +10,7 @@
10
10
  */
11
11
  export * from "./stt/speech-recognition";
12
12
  export * from "./tts/phonon";
13
+ export * from "./tts/locale-match";
13
14
  export * from "./tts/player";
14
15
  export * from "./tts/system-voices";
15
16
  export * from "./turns";