@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.
- package/README.md +86 -1
- package/dist/cli/helpers-cli.js +17 -2
- package/dist/contract/contract.d.ts +21 -2
- package/dist/contract/index.d.ts +1 -0
- package/dist/contract/index.js +12 -2
- package/dist/contract/session.d.ts +13 -0
- package/dist/contract/turns.d.ts +91 -0
- package/dist/contract/wire.d.ts +92 -1
- package/dist/desktop/conversation.d.ts +86 -0
- package/dist/desktop/desktop-voice.d.ts +61 -1
- package/dist/desktop/helper-lock.d.ts +45 -7
- package/dist/desktop/helper-process.d.ts +4 -0
- package/dist/desktop/index.d.ts +2 -0
- package/dist/desktop/index.js +427 -15
- package/dist/desktop/macho-digest.d.ts +9 -0
- package/dist/{index-rehs788p.js → index-7n7epq1a.js} +7 -2
- package/dist/{index-s270cw30.js → index-a7d617zr.js} +1 -1
- package/dist/{index-9kerqn9r.js → index-ght8wwpd.js} +9 -3
- package/dist/{index-acm1yvvv.js → index-m4g2tpeq.js} +258 -26
- package/dist/index.js +13 -3
- package/dist/settings/index.js +1 -1
- package/dist/web/index.d.ts +1 -0
- package/dist/web/index.js +459 -49
- package/dist/web/stt/speech-recognition.d.ts +128 -7
- package/dist/web/tts/locale-match.d.ts +40 -0
- package/dist/web/tts/system-voices.d.ts +44 -4
- package/dist/web/turns.d.ts +15 -25
- package/package.json +1 -1
- package/src/cli/helpers-cli.ts +18 -2
- package/src/contract/contract.ts +32 -4
- package/src/contract/index.ts +1 -0
- package/src/contract/session.ts +14 -2
- package/src/contract/turns.ts +48 -0
- package/src/contract/wire.ts +123 -2
- package/src/desktop/conversation.ts +347 -0
- package/src/desktop/desktop-voice.ts +197 -1
- package/src/desktop/helper-lock.ts +195 -20
- package/src/desktop/helper-process.ts +17 -10
- package/src/desktop/index.ts +2 -0
- package/src/desktop/macho-digest.ts +135 -0
- package/src/web/index.ts +1 -0
- package/src/web/stt/speech-recognition.ts +454 -24
- package/src/web/tts/locale-match.ts +131 -0
- package/src/web/tts/system-voices.ts +144 -35
- package/src/web/turns.ts +22 -11
package/dist/desktop/index.js
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
2
|
routingPolicyFor2
|
|
3
|
-
} from "../index-
|
|
3
|
+
} from "../index-a7d617zr.js";
|
|
4
4
|
import {
|
|
5
5
|
VOICE_CONTRACT2,
|
|
6
6
|
AVAILABLE2,
|
|
7
7
|
unavailable2,
|
|
8
8
|
route2
|
|
9
|
-
} from "../index-
|
|
9
|
+
} from "../index-ght8wwpd.js";
|
|
10
10
|
import {
|
|
11
|
+
isMachO2,
|
|
12
|
+
machoCodeDigest2,
|
|
13
|
+
machoCodeDigestFile2,
|
|
11
14
|
HELPER_LOCK_SCHEMA2,
|
|
12
15
|
HELPER_NAMES2,
|
|
13
16
|
HELPER_TARGETS2,
|
|
@@ -24,14 +27,17 @@ import {
|
|
|
24
27
|
verifyStagedVoiceHelpers2,
|
|
25
28
|
readStagedRecord2,
|
|
26
29
|
assertHandshake2
|
|
27
|
-
} from "../index-
|
|
30
|
+
} from "../index-m4g2tpeq.js";
|
|
28
31
|
import {
|
|
32
|
+
TURN_WIRE_MINOR2,
|
|
33
|
+
RELEASE_ASSETS_WIRE_MINOR2,
|
|
29
34
|
MAX_WIRE_LINE_BYTES2,
|
|
35
|
+
isTurnTerminal2,
|
|
30
36
|
parseWireLine2,
|
|
31
37
|
encodeWireCommand2,
|
|
32
38
|
correlationId2,
|
|
33
39
|
isCompatibleHandshake2
|
|
34
|
-
} from "../index-
|
|
40
|
+
} from "../index-7n7epq1a.js";
|
|
35
41
|
import {
|
|
36
42
|
VoiceError2
|
|
37
43
|
} from "../index-tvz9s9nz.js";
|
|
@@ -103,6 +109,9 @@ class HelperConnection {
|
|
|
103
109
|
constructor(options) {
|
|
104
110
|
this.options = options;
|
|
105
111
|
}
|
|
112
|
+
get label() {
|
|
113
|
+
return this.options.name ?? "the voice helper";
|
|
114
|
+
}
|
|
106
115
|
get version() {
|
|
107
116
|
return this.handshake;
|
|
108
117
|
}
|
|
@@ -171,7 +180,7 @@ class HelperConnection {
|
|
|
171
180
|
this.starting = undefined;
|
|
172
181
|
if (!child)
|
|
173
182
|
return;
|
|
174
|
-
this.failAll(new VoiceError2("cancelled",
|
|
183
|
+
this.failAll(new VoiceError2("cancelled", `${this.label} was stopped`));
|
|
175
184
|
await new Promise((resolve) => {
|
|
176
185
|
let exited = false;
|
|
177
186
|
child.onExit(() => {
|
|
@@ -199,11 +208,11 @@ class HelperConnection {
|
|
|
199
208
|
async dispose() {
|
|
200
209
|
this.disposed = true;
|
|
201
210
|
await this.stop();
|
|
202
|
-
this.failAll(new VoiceError2("cancelled",
|
|
211
|
+
this.failAll(new VoiceError2("cancelled", `${this.label} was shut down`));
|
|
203
212
|
}
|
|
204
213
|
async ensureStarted() {
|
|
205
214
|
if (this.disposed)
|
|
206
|
-
throw new VoiceError2("unavailable",
|
|
215
|
+
throw new VoiceError2("unavailable", `${this.label} was shut down`);
|
|
207
216
|
if (this.incompatible)
|
|
208
217
|
throw this.incompatible;
|
|
209
218
|
if (this.child)
|
|
@@ -220,7 +229,7 @@ class HelperConnection {
|
|
|
220
229
|
const window = this.options.crashWindowMs ?? 60000;
|
|
221
230
|
this.crashes = this.crashes.filter((at) => now() - at < window);
|
|
222
231
|
if (this.crashes.length >= (this.options.maxCrashes ?? 3)) {
|
|
223
|
-
throw new VoiceError2("unavailable",
|
|
232
|
+
throw new VoiceError2("unavailable", `${this.label} keeps crashing; restart the app to try again`);
|
|
224
233
|
}
|
|
225
234
|
let version;
|
|
226
235
|
try {
|
|
@@ -230,7 +239,7 @@ class HelperConnection {
|
|
|
230
239
|
throw error;
|
|
231
240
|
}
|
|
232
241
|
if (!isCompatibleHandshake2(version)) {
|
|
233
|
-
this.incompatible = new IncompatibleHelperError(
|
|
242
|
+
this.incompatible = new IncompatibleHelperError(`${this.options.name ?? "voice helper"} speaks wire ${version.wire} / contract ${version.contract}; this app needs wire 1 / contract 1`);
|
|
234
243
|
throw this.incompatible;
|
|
235
244
|
}
|
|
236
245
|
this.handshake = version;
|
|
@@ -240,14 +249,14 @@ class HelperConnection {
|
|
|
240
249
|
for (const line of splitter.push(chunk))
|
|
241
250
|
this.dispatch(line);
|
|
242
251
|
});
|
|
243
|
-
child.onStderr?.((chunk) => this.options.log?.(
|
|
252
|
+
child.onStderr?.((chunk) => this.options.log?.(`${this.options.name ?? "voice helper"}: ${chunk.trimEnd().slice(0, 500)}`));
|
|
244
253
|
child.onExit((code, signal) => {
|
|
245
254
|
if (this.child !== child)
|
|
246
255
|
return;
|
|
247
256
|
this.child = undefined;
|
|
248
257
|
if (!this.disposed)
|
|
249
258
|
this.crashes.push(now());
|
|
250
|
-
this.failAll(new VoiceError2("failed",
|
|
259
|
+
this.failAll(new VoiceError2("failed", `${this.label} exited (${signal ?? `code ${String(code)}`}); it restarts on the next request`));
|
|
251
260
|
});
|
|
252
261
|
this.child = child;
|
|
253
262
|
return child;
|
|
@@ -265,7 +274,7 @@ class HelperConnection {
|
|
|
265
274
|
outcome();
|
|
266
275
|
};
|
|
267
276
|
const timer = setTimeout(() => {
|
|
268
|
-
settle(() => reject(new VoiceError2("unavailable",
|
|
277
|
+
settle(() => reject(new VoiceError2("unavailable", `${this.label} did not answer its version handshake`)));
|
|
269
278
|
child.kill("SIGKILL");
|
|
270
279
|
}, this.options.handshakeTimeoutMs ?? 5000);
|
|
271
280
|
child.onStdout((chunk) => {
|
|
@@ -276,7 +285,7 @@ class HelperConnection {
|
|
|
276
285
|
}
|
|
277
286
|
});
|
|
278
287
|
child.onExit((code) => {
|
|
279
|
-
settle(() => reject(new VoiceError2("unavailable",
|
|
288
|
+
settle(() => reject(new VoiceError2("unavailable", `${this.label} failed to start (exit ${String(code)})`)));
|
|
280
289
|
});
|
|
281
290
|
try {
|
|
282
291
|
child.closeInput();
|
|
@@ -286,7 +295,7 @@ class HelperConnection {
|
|
|
286
295
|
dispatch(line) {
|
|
287
296
|
const event = parseWireLine2(line);
|
|
288
297
|
if (!event) {
|
|
289
|
-
this.options.log?.(
|
|
298
|
+
this.options.log?.(`${this.options.name ?? "voice helper"}: ignored a malformed line (${line.slice(0, 80)})`);
|
|
290
299
|
return;
|
|
291
300
|
}
|
|
292
301
|
const id = correlationId2(event);
|
|
@@ -356,6 +365,280 @@ var nodeSpawner = (path, args, options) => {
|
|
|
356
365
|
}
|
|
357
366
|
};
|
|
358
367
|
|
|
368
|
+
// src/desktop/conversation.ts
|
|
369
|
+
class Conversation {
|
|
370
|
+
options;
|
|
371
|
+
provider;
|
|
372
|
+
locale;
|
|
373
|
+
current;
|
|
374
|
+
queue = [];
|
|
375
|
+
waiter;
|
|
376
|
+
consumed = false;
|
|
377
|
+
finished = false;
|
|
378
|
+
ending = false;
|
|
379
|
+
cancelling = false;
|
|
380
|
+
graceTimer;
|
|
381
|
+
constructor(options) {
|
|
382
|
+
this.options = options;
|
|
383
|
+
this.provider = options.provider;
|
|
384
|
+
this.locale = options.locale;
|
|
385
|
+
}
|
|
386
|
+
get isFinished() {
|
|
387
|
+
return this.finished;
|
|
388
|
+
}
|
|
389
|
+
async begin() {
|
|
390
|
+
await this.startTurn();
|
|
391
|
+
}
|
|
392
|
+
events() {
|
|
393
|
+
if (this.consumed)
|
|
394
|
+
throw new VoiceError2("bad_request", "A conversation's events can be read once.");
|
|
395
|
+
this.consumed = true;
|
|
396
|
+
return {
|
|
397
|
+
[Symbol.asyncIterator]: () => ({
|
|
398
|
+
next: () => {
|
|
399
|
+
const next = this.queue.shift();
|
|
400
|
+
if (next)
|
|
401
|
+
return Promise.resolve({ value: next, done: false });
|
|
402
|
+
if (this.finished)
|
|
403
|
+
return Promise.resolve({ value: undefined, done: true });
|
|
404
|
+
return new Promise((resolve) => {
|
|
405
|
+
this.waiter = resolve;
|
|
406
|
+
});
|
|
407
|
+
},
|
|
408
|
+
return: async () => {
|
|
409
|
+
await this.cancel();
|
|
410
|
+
return { value: undefined, done: true };
|
|
411
|
+
}
|
|
412
|
+
})
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
async respond(textChunk, options = {}) {
|
|
416
|
+
const final = options.final ?? true;
|
|
417
|
+
if (this.finished || this.cancelling)
|
|
418
|
+
throw new VoiceError2("cancelled", "The conversation is over.");
|
|
419
|
+
const turn = this.current;
|
|
420
|
+
if (!turn || turn.ended)
|
|
421
|
+
throw new VoiceError2("bad_request", "No turn is waiting for an answer.");
|
|
422
|
+
if (turn.final)
|
|
423
|
+
throw new VoiceError2("bad_request", "This turn's answer is already complete.");
|
|
424
|
+
if (textChunk.length === 0 && !final)
|
|
425
|
+
return;
|
|
426
|
+
if (final)
|
|
427
|
+
turn.final = true;
|
|
428
|
+
await this.options.connection.send({
|
|
429
|
+
cmd: "turn-respond",
|
|
430
|
+
id: turn.id,
|
|
431
|
+
text: textChunk,
|
|
432
|
+
final,
|
|
433
|
+
...this.options.voice
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
async listen() {
|
|
437
|
+
if (this.finished || this.cancelling)
|
|
438
|
+
throw new VoiceError2("cancelled", "The conversation is over.");
|
|
439
|
+
const turn = this.current;
|
|
440
|
+
if (turn && !turn.ended && turn.state === "speaking" && !this.options.bargeIn && this.options.halfDuplex) {
|
|
441
|
+
throw new VoiceError2("busy", "The answer is still playing and barge-in is off.");
|
|
442
|
+
}
|
|
443
|
+
this.ending = false;
|
|
444
|
+
await this.startTurn();
|
|
445
|
+
}
|
|
446
|
+
async stopListening() {
|
|
447
|
+
const turn = this.current;
|
|
448
|
+
if (!turn || turn.ended || turn.state !== "listening" && turn.state !== undefined)
|
|
449
|
+
return;
|
|
450
|
+
await this.options.connection.send({ cmd: "turn-end", id: turn.id }).catch(() => {
|
|
451
|
+
return;
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
async end() {
|
|
455
|
+
if (this.finished)
|
|
456
|
+
return;
|
|
457
|
+
this.ending = true;
|
|
458
|
+
const turn = this.current;
|
|
459
|
+
if (!turn || turn.ended)
|
|
460
|
+
return this.finish();
|
|
461
|
+
await this.options.connection.send({ cmd: "turn-end", id: turn.id }).catch(() => {
|
|
462
|
+
return;
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
async cancel() {
|
|
466
|
+
if (this.finished || this.cancelling)
|
|
467
|
+
return;
|
|
468
|
+
this.cancelling = true;
|
|
469
|
+
const turn = this.current;
|
|
470
|
+
if (!turn || turn.ended) {
|
|
471
|
+
this.push({ type: "cancelled" });
|
|
472
|
+
return this.finish();
|
|
473
|
+
}
|
|
474
|
+
await this.options.connection.send({ cmd: "turn-cancel", id: turn.id }).catch(() => {
|
|
475
|
+
return;
|
|
476
|
+
});
|
|
477
|
+
this.graceTimer ??= setTimeout(() => {
|
|
478
|
+
if (this.finished)
|
|
479
|
+
return;
|
|
480
|
+
this.closeTurn(turn, { type: "ended", reason: "cancelled", turn: turn.id });
|
|
481
|
+
this.push({ type: "cancelled" });
|
|
482
|
+
this.finish();
|
|
483
|
+
this.options.onStuck();
|
|
484
|
+
}, this.options.terminalGraceMs);
|
|
485
|
+
}
|
|
486
|
+
startTurn() {
|
|
487
|
+
const turn = { id: nextRequestId("turn-"), state: undefined, final: false, ended: false };
|
|
488
|
+
this.current = turn;
|
|
489
|
+
let settleStart;
|
|
490
|
+
const started = new Promise((resolve, reject) => {
|
|
491
|
+
settleStart = { resolve, reject };
|
|
492
|
+
});
|
|
493
|
+
this.options.connection.request({
|
|
494
|
+
cmd: "turn-start",
|
|
495
|
+
id: turn.id,
|
|
496
|
+
locale: this.locale,
|
|
497
|
+
...this.options.engine ? { engine: this.options.engine } : {},
|
|
498
|
+
bargeIn: this.options.bargeIn,
|
|
499
|
+
halfDuplex: this.options.halfDuplex
|
|
500
|
+
}, isTurnTerminal2, {
|
|
501
|
+
timeoutMs: null,
|
|
502
|
+
onEvent: (event) => {
|
|
503
|
+
this.forward(turn, event);
|
|
504
|
+
if (event.event === "turn-state" && event.state === "listening")
|
|
505
|
+
settleStart.resolve();
|
|
506
|
+
}
|
|
507
|
+
}).then((terminal) => {
|
|
508
|
+
if (terminal.event === "turn-error") {
|
|
509
|
+
settleStart.reject(new VoiceError2(terminal.code, terminal.message ?? "The turn failed."));
|
|
510
|
+
} else {
|
|
511
|
+
settleStart.resolve();
|
|
512
|
+
}
|
|
513
|
+
this.afterTerminal(turn);
|
|
514
|
+
}, (error) => {
|
|
515
|
+
const typed = VoiceError2.wrapping(error);
|
|
516
|
+
this.closeTurn(turn, { type: "failed", error: typed, turn: turn.id });
|
|
517
|
+
settleStart.reject(typed);
|
|
518
|
+
this.afterTerminal(turn);
|
|
519
|
+
});
|
|
520
|
+
started.catch(() => {
|
|
521
|
+
return;
|
|
522
|
+
});
|
|
523
|
+
return started;
|
|
524
|
+
}
|
|
525
|
+
forward(turn, event) {
|
|
526
|
+
if (turn.ended)
|
|
527
|
+
return;
|
|
528
|
+
const tag = { turn: turn.id };
|
|
529
|
+
switch (event.event) {
|
|
530
|
+
case "turn-state":
|
|
531
|
+
turn.state = event.state;
|
|
532
|
+
switch (event.state) {
|
|
533
|
+
case "listening":
|
|
534
|
+
case "responding":
|
|
535
|
+
case "speaking":
|
|
536
|
+
case "idle":
|
|
537
|
+
if (event.state === "idle")
|
|
538
|
+
turn.final = false;
|
|
539
|
+
return this.push({ type: event.state, ...tag });
|
|
540
|
+
case "interrupted":
|
|
541
|
+
return;
|
|
542
|
+
case "ended":
|
|
543
|
+
return this.closeTurn(turn, { type: "ended", reason: event.reason ?? "completed", ...tag });
|
|
544
|
+
}
|
|
545
|
+
return;
|
|
546
|
+
case "turn-partial":
|
|
547
|
+
return this.push({
|
|
548
|
+
type: "partial",
|
|
549
|
+
partial: { finalizedText: event.finalizedText, volatileText: event.volatileText, sequence: event.sequence },
|
|
550
|
+
...tag
|
|
551
|
+
});
|
|
552
|
+
case "turn-transcript":
|
|
553
|
+
return this.push({ type: "transcript", text: event.text, ...tag });
|
|
554
|
+
case "turn-barge-in":
|
|
555
|
+
return this.push({ type: "barge-in", ...tag });
|
|
556
|
+
case "turn-speech-started":
|
|
557
|
+
return this.push({ type: "speech-started", utterance: event.utterance, ...tag });
|
|
558
|
+
case "turn-speech-finished":
|
|
559
|
+
return this.push({ type: "speech-finished", utterance: event.utterance, ...tag });
|
|
560
|
+
case "turn-speech-cancelled":
|
|
561
|
+
return this.push({ type: "speech-cancelled", utterance: event.utterance, ...tag });
|
|
562
|
+
case "turn-metrics":
|
|
563
|
+
return this.push({
|
|
564
|
+
type: "metrics",
|
|
565
|
+
metrics: {
|
|
566
|
+
firstPartialMs: event.firstPartialMs,
|
|
567
|
+
transcriptMs: event.transcriptMs,
|
|
568
|
+
firstAudioMs: event.firstAudioMs,
|
|
569
|
+
speechFinishedMs: event.speechFinishedMs
|
|
570
|
+
},
|
|
571
|
+
...tag
|
|
572
|
+
});
|
|
573
|
+
case "turn-error":
|
|
574
|
+
return this.closeTurn(turn, {
|
|
575
|
+
type: "failed",
|
|
576
|
+
error: new VoiceError2(event.code, event.message ?? "The turn failed."),
|
|
577
|
+
...tag
|
|
578
|
+
});
|
|
579
|
+
default:
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
closeTurn(turn, terminal) {
|
|
584
|
+
if (turn.ended)
|
|
585
|
+
return;
|
|
586
|
+
turn.ended = true;
|
|
587
|
+
this.push(terminal);
|
|
588
|
+
}
|
|
589
|
+
afterTerminal(turn) {
|
|
590
|
+
const reason = this.reasons.get(turn.id);
|
|
591
|
+
this.reasons.delete(turn.id);
|
|
592
|
+
if (turn !== this.current || this.finished)
|
|
593
|
+
return;
|
|
594
|
+
this.current = undefined;
|
|
595
|
+
if (this.cancelling || reason === "cancelled") {
|
|
596
|
+
this.push({ type: "cancelled" });
|
|
597
|
+
return this.finish();
|
|
598
|
+
}
|
|
599
|
+
if (reason === "failed" || this.ending)
|
|
600
|
+
return this.finish();
|
|
601
|
+
if (reason === "completed" && this.options.continuous) {
|
|
602
|
+
this.startTurn().catch(() => {
|
|
603
|
+
return;
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
reasons = new Map;
|
|
608
|
+
finish() {
|
|
609
|
+
if (this.finished)
|
|
610
|
+
return;
|
|
611
|
+
this.finished = true;
|
|
612
|
+
clearTimeout(this.graceTimer);
|
|
613
|
+
this.options.onFinished();
|
|
614
|
+
if (this.queue.length === 0 && this.waiter) {
|
|
615
|
+
const waiter = this.waiter;
|
|
616
|
+
this.waiter = undefined;
|
|
617
|
+
waiter({ value: undefined, done: true });
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
push(event) {
|
|
621
|
+
if (event.type === "ended" && event.turn)
|
|
622
|
+
this.reasons.set(event.turn, event.reason);
|
|
623
|
+
if (event.type === "failed" && event.turn)
|
|
624
|
+
this.reasons.set(event.turn, "failed");
|
|
625
|
+
if (this.finished)
|
|
626
|
+
return;
|
|
627
|
+
if (this.waiter) {
|
|
628
|
+
const waiter = this.waiter;
|
|
629
|
+
this.waiter = undefined;
|
|
630
|
+
waiter({ value: event, done: false });
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
if (event.type === "partial") {
|
|
634
|
+
const last = this.queue.at(-1);
|
|
635
|
+
if (last?.type === "partial" && last.turn === event.turn)
|
|
636
|
+
this.queue.pop();
|
|
637
|
+
}
|
|
638
|
+
this.queue.push(event);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
359
642
|
// src/desktop/desktop-voice.ts
|
|
360
643
|
var APPLE_ANALYZER_ID = "apple.analyzer";
|
|
361
644
|
var APPLE_LEGACY_ID = "apple.legacy";
|
|
@@ -418,6 +701,7 @@ class DesktopVoiceService {
|
|
|
418
701
|
transcribeFolders = "";
|
|
419
702
|
transcribeConnectionFolders = "";
|
|
420
703
|
active;
|
|
704
|
+
activeConversation;
|
|
421
705
|
spawner;
|
|
422
706
|
constructor(options) {
|
|
423
707
|
this.options = options;
|
|
@@ -470,6 +754,9 @@ class DesktopVoiceService {
|
|
|
470
754
|
async listen(request) {
|
|
471
755
|
if (this.active && !this.active.isTerminal)
|
|
472
756
|
throw new VoiceError2("busy", "A dictation is already running.");
|
|
757
|
+
if (this.activeConversation && !this.activeConversation.isFinished) {
|
|
758
|
+
throw new VoiceError2("busy", "A voice conversation is running.");
|
|
759
|
+
}
|
|
473
760
|
if (request.signal?.aborted)
|
|
474
761
|
throw new VoiceError2("cancelled", "Dictation was cancelled.");
|
|
475
762
|
const locale = request.locale ?? defaultLocale(request.settings.stt.locale);
|
|
@@ -505,6 +792,62 @@ class DesktopVoiceService {
|
|
|
505
792
|
await session.started;
|
|
506
793
|
return session;
|
|
507
794
|
}
|
|
795
|
+
async conversation(request) {
|
|
796
|
+
const busy = () => this.active && !this.active.isTerminal || this.activeConversation && !this.activeConversation.isFinished;
|
|
797
|
+
if (busy())
|
|
798
|
+
throw new VoiceError2("busy", "A dictation or conversation is already running.");
|
|
799
|
+
if (request.signal?.aborted)
|
|
800
|
+
throw new VoiceError2("cancelled", "The conversation was cancelled.");
|
|
801
|
+
if (!this.bundled().voice) {
|
|
802
|
+
throw new VoiceError2("unavailable", "Voice conversations need the Apple voice helper, which this build does not include.");
|
|
803
|
+
}
|
|
804
|
+
const locale = request.locale ?? defaultLocale(request.settings.stt.locale);
|
|
805
|
+
const capabilities = await this.capabilities(request.settings, locale);
|
|
806
|
+
for (const decision of [capabilities.speechToText, capabilities.textToSpeech]) {
|
|
807
|
+
if (!decision.route || decision.availability.status !== "available")
|
|
808
|
+
throw unavailableError(decision);
|
|
809
|
+
}
|
|
810
|
+
const sttID = capabilities.speechToText.route.descriptor.id;
|
|
811
|
+
const ttsID = capabilities.textToSpeech.route.descriptor.id;
|
|
812
|
+
if (sttID !== APPLE_ANALYZER_ID && sttID !== APPLE_LEGACY_ID || ttsID !== APPLE_VOICES_ID) {
|
|
813
|
+
throw new VoiceError2("unavailable", "Voice conversations run on Apple speech and Apple voices; the selected engines do not support them yet.");
|
|
814
|
+
}
|
|
815
|
+
const connection = this.connection("voice");
|
|
816
|
+
const minor = connection.version?.wireMinor ?? 0;
|
|
817
|
+
if (minor < TURN_WIRE_MINOR2) {
|
|
818
|
+
throw new VoiceError2("unavailable", "This build's voice helper is too old for conversations (it needs wire 1.2).");
|
|
819
|
+
}
|
|
820
|
+
if (busy())
|
|
821
|
+
throw new VoiceError2("busy", "A dictation or conversation is already running.");
|
|
822
|
+
const voice = request.settings.tts.voice;
|
|
823
|
+
const rate = request.settings.tts.rate;
|
|
824
|
+
const ttsLocale = request.settings.tts.locale;
|
|
825
|
+
const conversation = new Conversation({
|
|
826
|
+
connection,
|
|
827
|
+
provider: sttID,
|
|
828
|
+
locale,
|
|
829
|
+
engine: sttID === APPLE_ANALYZER_ID ? "analyzer" : "legacy",
|
|
830
|
+
bargeIn: request.bargeIn ?? true,
|
|
831
|
+
halfDuplex: request.halfDuplex ?? true,
|
|
832
|
+
continuous: request.continuous ?? true,
|
|
833
|
+
voice: {
|
|
834
|
+
...voice ? { voice } : {},
|
|
835
|
+
...ttsLocale ? { locale: ttsLocale } : {},
|
|
836
|
+
...rate !== undefined ? { rate: appleRate(rate) } : {}
|
|
837
|
+
},
|
|
838
|
+
terminalGraceMs: this.options.terminalGraceMs ?? 5000,
|
|
839
|
+
onStuck: () => void connection.stop(),
|
|
840
|
+
onFinished: () => {
|
|
841
|
+
if (this.activeConversation === conversation)
|
|
842
|
+
this.activeConversation = undefined;
|
|
843
|
+
}
|
|
844
|
+
});
|
|
845
|
+
this.activeConversation = conversation;
|
|
846
|
+
if (request.signal)
|
|
847
|
+
request.signal.addEventListener("abort", () => void conversation.cancel(), { once: true });
|
|
848
|
+
await conversation.begin();
|
|
849
|
+
return conversation;
|
|
850
|
+
}
|
|
508
851
|
async speak(request) {
|
|
509
852
|
const text = request.text.trim();
|
|
510
853
|
if (text.length === 0)
|
|
@@ -523,6 +866,12 @@ class DesktopVoiceService {
|
|
|
523
866
|
const id = nextRequestId("speech-");
|
|
524
867
|
const voice = request.voice ?? request.settings.tts.voice;
|
|
525
868
|
const rate = request.rate ?? request.settings.tts.rate;
|
|
869
|
+
let markStarted = () => {
|
|
870
|
+
return;
|
|
871
|
+
};
|
|
872
|
+
const started = new Promise((resolve) => {
|
|
873
|
+
markStarted = resolve;
|
|
874
|
+
});
|
|
526
875
|
const done = connection.request({
|
|
527
876
|
cmd: "speak",
|
|
528
877
|
id,
|
|
@@ -531,7 +880,13 @@ class DesktopVoiceService {
|
|
|
531
880
|
...voice ? { voice } : {},
|
|
532
881
|
...rate !== undefined ? { rate: providerID === AUDIO_CPP_ID ? rate : appleRate(rate) } : {},
|
|
533
882
|
queue: request.queue ?? "replace"
|
|
534
|
-
}, (event) => event.event === "speech-finished" || event.event === "speech-cancelled" || event.event === "speech-error", {
|
|
883
|
+
}, (event) => event.event === "speech-finished" || event.event === "speech-cancelled" || event.event === "speech-error", {
|
|
884
|
+
timeoutMs: null,
|
|
885
|
+
onEvent: (event) => {
|
|
886
|
+
if (event.event === "speech-started")
|
|
887
|
+
markStarted(speechStart(event.voice));
|
|
888
|
+
}
|
|
889
|
+
}).finally(() => markStarted(undefined)).then((event) => {
|
|
535
890
|
if (event.event === "speech-error")
|
|
536
891
|
throw new VoiceError2(event.code, event.message ?? "Speech failed.");
|
|
537
892
|
return event.event === "speech-finished" ? "finished" : "cancelled";
|
|
@@ -542,6 +897,7 @@ class DesktopVoiceService {
|
|
|
542
897
|
return {
|
|
543
898
|
id,
|
|
544
899
|
provider: providerID,
|
|
900
|
+
started,
|
|
545
901
|
done,
|
|
546
902
|
cancel: async () => {
|
|
547
903
|
await connection.send({ cmd: "speech-stop" }).catch(() => {
|
|
@@ -615,10 +971,44 @@ class DesktopVoiceService {
|
|
|
615
971
|
}
|
|
616
972
|
}).finally(() => request.signal?.removeEventListener("abort", onAbort));
|
|
617
973
|
}
|
|
974
|
+
async releaseAssets(request) {
|
|
975
|
+
if (!this.bundled().voice)
|
|
976
|
+
throw new VoiceError2("unavailable", "Speech assets are managed by the system on this device.");
|
|
977
|
+
const connection = this.connection("voice");
|
|
978
|
+
const tooOld = () => new VoiceError2("unavailable", "This build's voice helper is too old to remove speech models (it needs wire 1.3).");
|
|
979
|
+
const minor = connection.version?.wireMinor;
|
|
980
|
+
if (minor !== undefined && minor < RELEASE_ASSETS_WIRE_MINOR2)
|
|
981
|
+
throw tooOld();
|
|
982
|
+
let answer;
|
|
983
|
+
await connection.request({ cmd: "release-assets", id: nextRequestId("release-"), locale: request.locale }, (event) => event.event === "asset-released", {
|
|
984
|
+
onEvent: (event) => {
|
|
985
|
+
if (event.event !== "asset-released")
|
|
986
|
+
return;
|
|
987
|
+
answer = {
|
|
988
|
+
locale: event.locale,
|
|
989
|
+
modelLocale: event.modelLocale.replace(/_/g, "-"),
|
|
990
|
+
released: event.released,
|
|
991
|
+
status: event.status,
|
|
992
|
+
canInstall: event.canInstall
|
|
993
|
+
};
|
|
994
|
+
}
|
|
995
|
+
}).catch((error) => {
|
|
996
|
+
if (error instanceof VoiceError2 && error.code === "bad_request" && (connection.version?.wireMinor ?? 0) < RELEASE_ASSETS_WIRE_MINOR2) {
|
|
997
|
+
throw tooOld();
|
|
998
|
+
}
|
|
999
|
+
throw error;
|
|
1000
|
+
});
|
|
1001
|
+
if (!answer)
|
|
1002
|
+
throw new VoiceError2("failed", "The voice helper did not report the removal.");
|
|
1003
|
+
return answer;
|
|
1004
|
+
}
|
|
618
1005
|
async dispose() {
|
|
619
1006
|
await this.active?.cancel().catch(() => {
|
|
620
1007
|
return;
|
|
621
1008
|
});
|
|
1009
|
+
await this.activeConversation?.cancel().catch(() => {
|
|
1010
|
+
return;
|
|
1011
|
+
});
|
|
622
1012
|
await Promise.all([...this.connections.values()].map((connection) => connection.stop()));
|
|
623
1013
|
}
|
|
624
1014
|
providers(probe) {
|
|
@@ -646,6 +1036,7 @@ class DesktopVoiceService {
|
|
|
646
1036
|
this.transcribeConnectionFolders = this.transcribeFolders;
|
|
647
1037
|
connection = new HelperConnection({
|
|
648
1038
|
path,
|
|
1039
|
+
name: HELPER_LABEL[name],
|
|
649
1040
|
spawn: (file, args, spawnOptions) => this.spawn(file, args, spawnOptions),
|
|
650
1041
|
env: name === "transcribe" ? { ...this.options.env, ...this.transcribeEnv() } : this.options.env,
|
|
651
1042
|
log: this.options.log
|
|
@@ -837,6 +1228,24 @@ function defaultLocale(chosen) {
|
|
|
837
1228
|
return "en-US";
|
|
838
1229
|
}
|
|
839
1230
|
}
|
|
1231
|
+
var VOICE_QUALITIES = new Set(["default", "enhanced", "premium", "unknown"]);
|
|
1232
|
+
function speechStart(voice) {
|
|
1233
|
+
if (voice === null || typeof voice !== "object")
|
|
1234
|
+
return {};
|
|
1235
|
+
const { id, name, language, locale, quality } = voice;
|
|
1236
|
+
const tag = typeof language === "string" ? language : typeof locale === "string" ? locale : undefined;
|
|
1237
|
+
if (typeof id !== "string" || typeof name !== "string" || tag === undefined)
|
|
1238
|
+
return {};
|
|
1239
|
+
return {
|
|
1240
|
+
voice: {
|
|
1241
|
+
id,
|
|
1242
|
+
name,
|
|
1243
|
+
language: tag,
|
|
1244
|
+
locale: typeof locale === "string" ? locale : tag,
|
|
1245
|
+
quality: typeof quality === "string" && VOICE_QUALITIES.has(quality) ? quality : "unknown"
|
|
1246
|
+
}
|
|
1247
|
+
};
|
|
1248
|
+
}
|
|
840
1249
|
function appleRate(rate) {
|
|
841
1250
|
return Math.min(1, Math.max(0.05, rate * 0.5));
|
|
842
1251
|
}
|
|
@@ -1030,7 +1439,10 @@ export {
|
|
|
1030
1439
|
desktopRoutingPolicy,
|
|
1031
1440
|
helperArtifactUrl2 as helperArtifactUrl,
|
|
1032
1441
|
helperPin2 as helperPin,
|
|
1442
|
+
isMachO2 as isMachO,
|
|
1033
1443
|
joinTranscript,
|
|
1444
|
+
machoCodeDigest2 as machoCodeDigest,
|
|
1445
|
+
machoCodeDigestFile2 as machoCodeDigestFile,
|
|
1034
1446
|
nodeSpawner,
|
|
1035
1447
|
readHelperLock2 as readHelperLock,
|
|
1036
1448
|
readStagedRecord2 as readStagedRecord,
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** True when `bytes` starts like a Mach-O image or a universal file. */
|
|
2
|
+
export declare function isMachO(bytes: Uint8Array): boolean;
|
|
3
|
+
/**
|
|
4
|
+
* The sha256 (hex) of a Mach-O file with its code signature left out, or `undefined` for a file
|
|
5
|
+
* this cannot digest (not Mach-O, 32-bit, big-endian, or malformed).
|
|
6
|
+
*/
|
|
7
|
+
export declare function machoCodeDigest(bytes: Uint8Array): string | undefined;
|
|
8
|
+
/** `machoCodeDigest` of a file on disk. */
|
|
9
|
+
export declare function machoCodeDigestFile(path: string): string | undefined;
|
|
@@ -4,8 +4,13 @@ import {
|
|
|
4
4
|
|
|
5
5
|
// src/contract/wire.ts
|
|
6
6
|
var WIRE_VERSION2 = 1;
|
|
7
|
+
var TURN_WIRE_MINOR2 = 2;
|
|
8
|
+
var RELEASE_ASSETS_WIRE_MINOR2 = 3;
|
|
7
9
|
var CONTRACT_MAJOR2 = 1;
|
|
8
10
|
var MAX_WIRE_LINE_BYTES2 = 1e6;
|
|
11
|
+
function isTurnTerminal2(event) {
|
|
12
|
+
return event.event === "turn-error" || event.event === "turn-state" && event.state === "ended";
|
|
13
|
+
}
|
|
9
14
|
function parseWireLine2(line) {
|
|
10
15
|
if (line.length === 0 || line.length > MAX_WIRE_LINE_BYTES2)
|
|
11
16
|
return;
|
|
@@ -20,7 +25,7 @@ function parseWireLine2(line) {
|
|
|
20
25
|
const object = value;
|
|
21
26
|
if (typeof object.event !== "string")
|
|
22
27
|
return;
|
|
23
|
-
if (object.event === "error" || object.event === "speech-error") {
|
|
28
|
+
if (object.event === "error" || object.event === "speech-error" || object.event === "turn-error") {
|
|
24
29
|
return { ...object, code: normalizeVoiceErrorCode2(object.code) };
|
|
25
30
|
}
|
|
26
31
|
return object;
|
|
@@ -39,4 +44,4 @@ function isCompatibleHandshake2(event) {
|
|
|
39
44
|
return event.wire === WIRE_VERSION2 && event.contractMajor === CONTRACT_MAJOR2;
|
|
40
45
|
}
|
|
41
46
|
|
|
42
|
-
export { WIRE_VERSION2, CONTRACT_MAJOR2, MAX_WIRE_LINE_BYTES2, parseWireLine2, encodeWireCommand2, correlationId2, isCompatibleHandshake2 };
|
|
47
|
+
export { WIRE_VERSION2, TURN_WIRE_MINOR2, RELEASE_ASSETS_WIRE_MINOR2, CONTRACT_MAJOR2, MAX_WIRE_LINE_BYTES2, isTurnTerminal2, parseWireLine2, encodeWireCommand2, correlationId2, isCompatibleHandshake2 };
|