@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
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Region-aware locale matching for platform voices (#36).
|
|
3
|
+
*
|
|
4
|
+
* Matching only the language subtag picks `es-ES` for an `es-419` request whenever the browser lists
|
|
5
|
+
* Spain first, which is what the 2026-11-19 acceptance run recorded in Firefox and Safari. The rule
|
|
6
|
+
* here is: **exact tag > same region group > same language**, with an explicit preference order
|
|
7
|
+
* inside a group. For Latin-American Spanish that order is `es-419`, `es-MX`, `es-US`, `es-AR`, then
|
|
8
|
+
* the other Latin-American regions, and `es-ES` only last.
|
|
9
|
+
*
|
|
10
|
+
* Pure functions over tags: no browser, no voices list.
|
|
11
|
+
*/
|
|
12
|
+
import type { VoiceLocaleMatch } from "../../contract/contract";
|
|
13
|
+
|
|
14
|
+
/** A parsed BCP-47 tag: language, optional script, optional region (upper case or 3 digits). */
|
|
15
|
+
export interface ParsedLocale {
|
|
16
|
+
readonly language: string;
|
|
17
|
+
readonly script?: string;
|
|
18
|
+
readonly region?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function parseLocale(tag: string): ParsedLocale {
|
|
22
|
+
const parts = tag.trim().replace(/_/g, "-").split("-").filter((part) => part.length > 0);
|
|
23
|
+
const language = (parts[0] ?? "").toLowerCase();
|
|
24
|
+
let script: string | undefined;
|
|
25
|
+
let region: string | undefined;
|
|
26
|
+
for (const part of parts.slice(1)) {
|
|
27
|
+
if (script === undefined && region === undefined && /^[a-z]{4}$/i.test(part)) {
|
|
28
|
+
script = part[0]!.toUpperCase() + part.slice(1).toLowerCase();
|
|
29
|
+
} else if (region === undefined && (/^[a-z]{2}$/i.test(part) || /^\d{3}$/.test(part))) {
|
|
30
|
+
region = part.toUpperCase();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return { language, ...(script ? { script } : {}), ...(region ? { region } : {}) };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Region groups per language, each in preference order. A region's group is the first group that
|
|
38
|
+
* lists it; the first group of a language is not privileged. Chinese groups by script as well, so
|
|
39
|
+
* `zh-Hant` finds `zh-TW` and never `zh-CN`.
|
|
40
|
+
*/
|
|
41
|
+
export const REGION_GROUPS: Readonly<Record<string, readonly (readonly string[])[]>> = {
|
|
42
|
+
es: [
|
|
43
|
+
// Latin America and the US: `es-419` first, then the largest audiences, then the rest.
|
|
44
|
+
["419", "MX", "US", "AR", "CO", "CL", "PE", "VE", "EC", "GT", "CU", "BO", "DO", "HN", "PY", "SV", "NI", "CR", "PA", "UY", "PR"],
|
|
45
|
+
["ES", "GQ", "150"],
|
|
46
|
+
],
|
|
47
|
+
pt: [
|
|
48
|
+
["BR"],
|
|
49
|
+
["PT", "AO", "MZ", "CV", "GW", "ST", "TL", "MO", "150"],
|
|
50
|
+
],
|
|
51
|
+
en: [
|
|
52
|
+
["US", "CA", "PH"],
|
|
53
|
+
["GB", "IE", "AU", "NZ", "ZA", "IN", "SG", "HK", "150"],
|
|
54
|
+
],
|
|
55
|
+
fr: [
|
|
56
|
+
["FR", "BE", "CH", "LU", "MC", "150"],
|
|
57
|
+
["CA"],
|
|
58
|
+
],
|
|
59
|
+
zh: [
|
|
60
|
+
["HANS", "CN", "SG", "MY"],
|
|
61
|
+
["HANT", "TW", "HK", "MO"],
|
|
62
|
+
],
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/** The region-group key of a parsed tag: Chinese groups by script first, everything else by region. */
|
|
66
|
+
function groupKey(locale: ParsedLocale): string | undefined {
|
|
67
|
+
if (locale.language === "zh" && locale.script) return locale.script.toUpperCase();
|
|
68
|
+
return locale.region;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function groupOf(locale: ParsedLocale): { group: readonly string[] } | undefined {
|
|
72
|
+
const key = groupKey(locale);
|
|
73
|
+
if (key === undefined) return undefined;
|
|
74
|
+
const group = REGION_GROUPS[locale.language]?.find((candidate) => candidate.includes(key));
|
|
75
|
+
return group ? { group } : undefined;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface LocaleMatch {
|
|
79
|
+
readonly match: VoiceLocaleMatch;
|
|
80
|
+
/** Lower is better. Stable across calls, so it can sort a list of voices. */
|
|
81
|
+
readonly rank: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* How well a voice's language tag serves a requested locale, or undefined for another language.
|
|
86
|
+
*
|
|
87
|
+
* - exact tag (case and `_`/`-` insensitive) is `exact`, rank 0;
|
|
88
|
+
* - a voice in the requested region's group is `region`, ranked by the group's preference order;
|
|
89
|
+
* - a request without a region accepts every voice of the language as `region`;
|
|
90
|
+
* - anything else of the same language is `language`: a region-less voice (`es`) before an
|
|
91
|
+
* off-region one (`es-ES` for `es-419`).
|
|
92
|
+
*/
|
|
93
|
+
export function matchLocale(requested: string, voiceLang: string): LocaleMatch | undefined {
|
|
94
|
+
const want = parseLocale(requested);
|
|
95
|
+
const have = parseLocale(voiceLang);
|
|
96
|
+
if (want.language.length === 0 || want.language !== have.language) return undefined;
|
|
97
|
+
if (normalize(requested) === normalize(voiceLang)) return { match: "exact", rank: 0 };
|
|
98
|
+
const wantKey = groupKey(want);
|
|
99
|
+
const haveKey = groupKey(have);
|
|
100
|
+
if (wantKey === undefined) {
|
|
101
|
+
// The request names no region: every voice of the language serves it. Region-less voices first.
|
|
102
|
+
return { match: "region", rank: haveKey === undefined ? 100 : 200 + preferenceIndex(have) };
|
|
103
|
+
}
|
|
104
|
+
if (haveKey === wantKey && (want.region === have.region || want.region === undefined || have.region === undefined)) {
|
|
105
|
+
return { match: "exact", rank: 1 };
|
|
106
|
+
}
|
|
107
|
+
const wanted = groupOf(want);
|
|
108
|
+
if (wanted && haveKey !== undefined && wanted.group.includes(haveKey)) {
|
|
109
|
+
return { match: "region", rank: 100 + wanted.group.indexOf(haveKey) };
|
|
110
|
+
}
|
|
111
|
+
if (have.region === undefined && haveKey === undefined) return { match: "language", rank: 1_000 };
|
|
112
|
+
return { match: "language", rank: 1_100 + preferenceIndex(have) };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** A voice's position in its language's preference lists, for a deterministic tie-break. */
|
|
116
|
+
function preferenceIndex(locale: ParsedLocale): number {
|
|
117
|
+
const key = groupKey(locale);
|
|
118
|
+
const groups = REGION_GROUPS[locale.language];
|
|
119
|
+
if (key === undefined || groups === undefined) return 99;
|
|
120
|
+
let offset = 0;
|
|
121
|
+
for (const group of groups) {
|
|
122
|
+
const index = group.indexOf(key);
|
|
123
|
+
if (index >= 0) return offset + index;
|
|
124
|
+
offset += group.length;
|
|
125
|
+
}
|
|
126
|
+
return 99;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function normalize(tag: string): string {
|
|
130
|
+
return tag.trim().replace(/_/g, "-").toLowerCase();
|
|
131
|
+
}
|
|
@@ -11,14 +11,16 @@
|
|
|
11
11
|
* the person declines the model download.
|
|
12
12
|
*/
|
|
13
13
|
import {
|
|
14
|
-
AVAILABLE,
|
|
15
14
|
VOICE_CONTRACT,
|
|
15
|
+
availableWithMatch,
|
|
16
16
|
unavailable,
|
|
17
|
+
type VoiceLocaleMatch,
|
|
17
18
|
type VoiceProvider,
|
|
18
19
|
type VoiceProviderAvailability,
|
|
19
20
|
type VoiceProviderDescriptor,
|
|
20
21
|
} from "../../contract/contract";
|
|
21
22
|
import { VoiceError } from "../../contract/errors";
|
|
23
|
+
import { matchLocale } from "./locale-match";
|
|
22
24
|
|
|
23
25
|
export interface SynthesisVoiceLike {
|
|
24
26
|
readonly name: string;
|
|
@@ -79,7 +81,8 @@ export interface SpeakWithPlatformVoiceOptions {
|
|
|
79
81
|
readonly volume?: number;
|
|
80
82
|
/** Whether the host has consent for a remote voice. Default false. */
|
|
81
83
|
readonly allowNetwork?: boolean;
|
|
82
|
-
|
|
84
|
+
/** Called when the audio starts, with the voice that speaks (also on `handle.voice`). */
|
|
85
|
+
readonly onStart?: (voice: ChosenVoice | undefined) => void;
|
|
83
86
|
readonly onEnd?: () => void;
|
|
84
87
|
/**
|
|
85
88
|
* Reject with `failed` if the browser never reports an end (Chrome can drop `end` events).
|
|
@@ -88,8 +91,33 @@ export interface SpeakWithPlatformVoiceOptions {
|
|
|
88
91
|
readonly timeoutMs?: number;
|
|
89
92
|
}
|
|
90
93
|
|
|
94
|
+
/**
|
|
95
|
+
* How a voice was chosen:
|
|
96
|
+
* - `exact` / `region` / `language`: matched against `locale` (see `VoiceLocaleMatch`);
|
|
97
|
+
* - `named`: the host named it with `voiceName` and gave no locale to compare with;
|
|
98
|
+
* - `default`: no locale and no name, so the browser's default local voice.
|
|
99
|
+
*/
|
|
100
|
+
export type VoiceSelectionMatch = VoiceLocaleMatch | "named" | "default";
|
|
101
|
+
|
|
102
|
+
/** The voice a request speaks with, for a host to show ("Paulina, Spanish (Mexico), on device"). */
|
|
103
|
+
export interface ChosenVoice {
|
|
104
|
+
readonly name: string;
|
|
105
|
+
readonly voiceURI?: string;
|
|
106
|
+
readonly lang: string;
|
|
107
|
+
/** Whether the voice runs on the device. A remote voice is only chosen with `allowNetwork`. */
|
|
108
|
+
readonly localService: boolean;
|
|
109
|
+
readonly match: VoiceSelectionMatch;
|
|
110
|
+
/**
|
|
111
|
+
* True when the requested locale names a region and the only usable voice is from another region
|
|
112
|
+
* of the language (`es-ES` for `es-419`). The host should say so rather than imply a match.
|
|
113
|
+
*/
|
|
114
|
+
readonly offRegion: boolean;
|
|
115
|
+
}
|
|
116
|
+
|
|
91
117
|
export interface PlatformSpeechHandle {
|
|
92
118
|
readonly done: Promise<void>;
|
|
119
|
+
/** The voice that speaks this request; undefined when the request was refused before speaking. */
|
|
120
|
+
readonly voice?: ChosenVoice;
|
|
93
121
|
cancel(): void;
|
|
94
122
|
}
|
|
95
123
|
|
|
@@ -101,6 +129,7 @@ const DEFAULT_VOICES_TIMEOUT_MS = 1_500;
|
|
|
101
129
|
interface Request {
|
|
102
130
|
readonly build: () => SynthesisUtteranceLike;
|
|
103
131
|
readonly options: SpeakWithPlatformVoiceOptions;
|
|
132
|
+
readonly voice: ChosenVoice | undefined;
|
|
104
133
|
utterance: SynthesisUtteranceLike;
|
|
105
134
|
started: boolean;
|
|
106
135
|
settled: boolean;
|
|
@@ -138,18 +167,37 @@ export class SystemVoiceProvider implements VoiceProvider {
|
|
|
138
167
|
return this.environment.Utterance ?? this.environment.SpeechSynthesisUtterance;
|
|
139
168
|
}
|
|
140
169
|
|
|
141
|
-
/**
|
|
170
|
+
/**
|
|
171
|
+
* Voices the browser exposes for a locale's language, split by where they run, each list in
|
|
172
|
+
* preference order: exact tag, then the requested region group (for `es-419`: es-MX, es-US,
|
|
173
|
+
* es-AR, other Latin-American regions), then the rest of the language (`es-ES` last).
|
|
174
|
+
*/
|
|
142
175
|
voices(locale: string): { local: SynthesisVoiceLike[]; remote: SynthesisVoiceLike[] } {
|
|
143
|
-
const
|
|
144
|
-
if (synthesis === undefined) return { local: [], remote: [] };
|
|
145
|
-
const wanted = language(locale);
|
|
146
|
-
const matches = synthesis.getVoices().filter((voice) => language(voice.lang) === wanted);
|
|
176
|
+
const ranked = this.rankVoices(locale);
|
|
147
177
|
return {
|
|
148
|
-
local:
|
|
149
|
-
remote:
|
|
178
|
+
local: ranked.filter((entry) => entry.voice.localService === true).map((entry) => entry.voice),
|
|
179
|
+
remote: ranked.filter((entry) => entry.voice.localService !== true).map((entry) => entry.voice),
|
|
150
180
|
};
|
|
151
181
|
}
|
|
152
182
|
|
|
183
|
+
/** Every voice of the locale's language with its match quality, best first. */
|
|
184
|
+
rankVoices(locale: string): Array<{ voice: SynthesisVoiceLike; match: VoiceLocaleMatch }> {
|
|
185
|
+
const synthesis = this.environment.speechSynthesis;
|
|
186
|
+
if (synthesis === undefined) return [];
|
|
187
|
+
return rank(synthesis.getVoices(), locale).map(({ voice, match }) => ({ voice, match: match.match }));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The voice `speak()` would use for these options, without speaking: lets a host show the voice
|
|
192
|
+
* (and an off-region warning) before the first utterance. A `VoiceError` when it would refuse.
|
|
193
|
+
*/
|
|
194
|
+
chooseVoice(options: SpeakWithPlatformVoiceOptions = {}): ChosenVoice | undefined | VoiceError {
|
|
195
|
+
const synthesis = this.environment.speechSynthesis;
|
|
196
|
+
if (synthesis === undefined) return new VoiceError("unavailable");
|
|
197
|
+
const selection = this.selectVoice(synthesis.getVoices(), options);
|
|
198
|
+
return selection === undefined || selection instanceof VoiceError ? selection : describe(selection);
|
|
199
|
+
}
|
|
200
|
+
|
|
153
201
|
/**
|
|
154
202
|
* Resolves once the browser has a voice list, or after `voicesTimeoutMs`. Chrome returns an empty
|
|
155
203
|
* list until `voiceschanged`, which would otherwise read as "no voice installed".
|
|
@@ -183,8 +231,20 @@ export class SystemVoiceProvider implements VoiceProvider {
|
|
|
183
231
|
});
|
|
184
232
|
}
|
|
185
233
|
await this.waitForVoices();
|
|
186
|
-
const
|
|
187
|
-
|
|
234
|
+
const ranked = this.rankVoices(locale);
|
|
235
|
+
const local = ranked.filter((entry) => entry.voice.localService === true);
|
|
236
|
+
const remote = ranked.filter((entry) => entry.voice.localService !== true);
|
|
237
|
+
const best = local[0];
|
|
238
|
+
if (best !== undefined) {
|
|
239
|
+
if (best.match === "language") {
|
|
240
|
+
// Region-level truth: `pt-PT` with only `pt-BR` installed is the language, not the locale.
|
|
241
|
+
return availableWithMatch(
|
|
242
|
+
"language",
|
|
243
|
+
`no local ${locale} voice; only ${best.voice.lang} (${best.voice.name}) of the language is installed`,
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
return availableWithMatch(best.match);
|
|
247
|
+
}
|
|
188
248
|
if (remote.length > 0) {
|
|
189
249
|
return unavailable("networkRequired", `only remote ${locale} voices are installed`, {
|
|
190
250
|
kind: "grantPermission",
|
|
@@ -199,7 +259,8 @@ export class SystemVoiceProvider implements VoiceProvider {
|
|
|
199
259
|
|
|
200
260
|
/**
|
|
201
261
|
* Speaks with a platform voice. Local voices only unless `allowNetwork` is set. With `locale`,
|
|
202
|
-
* the voice is
|
|
262
|
+
* the voice is the best local voice of that language: exact tag, then the region group, then
|
|
263
|
+
* another region (flagged `offRegion` on `handle.voice`).
|
|
203
264
|
*/
|
|
204
265
|
speak(text: string, options: SpeakWithPlatformVoiceOptions = {}): PlatformSpeechHandle {
|
|
205
266
|
const synthesis = this.environment.speechSynthesis;
|
|
@@ -214,7 +275,8 @@ export class SystemVoiceProvider implements VoiceProvider {
|
|
|
214
275
|
if (selection instanceof VoiceError) {
|
|
215
276
|
return { done: Promise.reject(selection), cancel: () => {} };
|
|
216
277
|
}
|
|
217
|
-
const
|
|
278
|
+
const chosen = selection;
|
|
279
|
+
const voice = chosen?.voice;
|
|
218
280
|
const build = () => {
|
|
219
281
|
const utterance = new Utterance(text);
|
|
220
282
|
utterance.lang = voice?.lang ?? options.locale ?? "";
|
|
@@ -225,9 +287,10 @@ export class SystemVoiceProvider implements VoiceProvider {
|
|
|
225
287
|
return utterance;
|
|
226
288
|
};
|
|
227
289
|
|
|
290
|
+
const info = chosen === undefined ? undefined : describe(chosen);
|
|
228
291
|
let request!: Request;
|
|
229
292
|
const done = new Promise<void>((resolve, reject) => {
|
|
230
|
-
request = { build, options, utterance: build(), started: false, settled: false, zombie: false, watchdog: undefined, resolve, reject };
|
|
293
|
+
request = { build, options, voice: info, utterance: build(), started: false, settled: false, zombie: false, watchdog: undefined, resolve, reject };
|
|
231
294
|
});
|
|
232
295
|
this.inFlight.push(request);
|
|
233
296
|
this.bind(request);
|
|
@@ -241,7 +304,11 @@ export class SystemVoiceProvider implements VoiceProvider {
|
|
|
241
304
|
this.settle(request, VoiceError.wrapping(error));
|
|
242
305
|
}
|
|
243
306
|
|
|
244
|
-
return {
|
|
307
|
+
return {
|
|
308
|
+
done,
|
|
309
|
+
...(info === undefined ? {} : { voice: info }),
|
|
310
|
+
cancel: () => this.abandon(request, new VoiceError("cancelled")),
|
|
311
|
+
};
|
|
245
312
|
}
|
|
246
313
|
|
|
247
314
|
/** Whether a synthesis request is in flight. */
|
|
@@ -252,7 +319,7 @@ export class SystemVoiceProvider implements VoiceProvider {
|
|
|
252
319
|
private selectVoice(
|
|
253
320
|
available: SynthesisVoiceLike[],
|
|
254
321
|
options: SpeakWithPlatformVoiceOptions,
|
|
255
|
-
):
|
|
322
|
+
): Selection | undefined | VoiceError {
|
|
256
323
|
const allowNetwork = options.allowNetwork === true;
|
|
257
324
|
if (options.voiceName) {
|
|
258
325
|
const requested = available.find((voice) => voice.name === options.voiceName);
|
|
@@ -260,22 +327,24 @@ export class SystemVoiceProvider implements VoiceProvider {
|
|
|
260
327
|
if (requested.localService !== true && !allowNetwork) {
|
|
261
328
|
return new VoiceError("denied", "a remote voice needs explicit consent");
|
|
262
329
|
}
|
|
263
|
-
|
|
330
|
+
const match = options.locale ? matchLocale(options.locale, requested.lang)?.match ?? "language" : "named";
|
|
331
|
+
return { voice: requested, match, offRegion: options.locale ? isOffRegion(options.locale, match) : false };
|
|
264
332
|
}
|
|
265
333
|
}
|
|
266
334
|
if (options.locale) {
|
|
267
|
-
const
|
|
268
|
-
const
|
|
269
|
-
const
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
if (
|
|
277
|
-
|
|
278
|
-
|
|
335
|
+
const ranked = rank(available, options.locale);
|
|
336
|
+
const local = ranked.find((entry) => entry.voice.localService === true);
|
|
337
|
+
const remote = ranked.find((entry) => entry.voice.localService !== true);
|
|
338
|
+
// Local first. With consent, a remote voice wins only on a strictly better match tier
|
|
339
|
+
// (an es-MX cloud voice over a local es-ES one); within a tier the local voice keeps it.
|
|
340
|
+
let pick = local;
|
|
341
|
+
if (allowNetwork && remote !== undefined && (pick === undefined || tier(remote.match.match) < tier(pick.match.match))) {
|
|
342
|
+
pick = remote;
|
|
343
|
+
}
|
|
344
|
+
if (pick === undefined) {
|
|
345
|
+
return new VoiceError("language_unsupported", `no local ${options.locale} voice is installed`);
|
|
346
|
+
}
|
|
347
|
+
return { voice: pick.voice, match: pick.match.match, offRegion: isOffRegion(options.locale, pick.match.match) };
|
|
279
348
|
}
|
|
280
349
|
const fallback =
|
|
281
350
|
available.find((voice) => voice.localService === true && voice.default === true) ??
|
|
@@ -283,7 +352,7 @@ export class SystemVoiceProvider implements VoiceProvider {
|
|
|
283
352
|
if (fallback === undefined && !allowNetwork) {
|
|
284
353
|
return new VoiceError("language_unsupported", "no local voice is installed");
|
|
285
354
|
}
|
|
286
|
-
return fallback;
|
|
355
|
+
return fallback === undefined ? undefined : { voice: fallback, match: "default", offRegion: false };
|
|
287
356
|
}
|
|
288
357
|
|
|
289
358
|
/** Wires one utterance object's events to its request. A rebuilt utterance is wired again. */
|
|
@@ -298,10 +367,16 @@ export class SystemVoiceProvider implements VoiceProvider {
|
|
|
298
367
|
return;
|
|
299
368
|
}
|
|
300
369
|
request.started = true;
|
|
301
|
-
options.onStart?.();
|
|
370
|
+
options.onStart?.(request.voice);
|
|
302
371
|
};
|
|
303
372
|
utterance.onend = () => {
|
|
304
373
|
if (request.utterance !== utterance || request.settled) return;
|
|
374
|
+
if (!request.started) {
|
|
375
|
+
// Firefox reports a cancelled utterance with `end`, not `error: canceled`. An utterance that
|
|
376
|
+
// ends without ever starting was not heard, so it is a cancel, never a successful finish.
|
|
377
|
+
this.settle(request, new VoiceError("cancelled", "the platform voice was cancelled before it started"));
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
305
380
|
options.onEnd?.();
|
|
306
381
|
this.settle(request);
|
|
307
382
|
};
|
|
@@ -376,12 +451,46 @@ function detach(utterance: SynthesisUtteranceLike): void {
|
|
|
376
451
|
utterance.onerror = null;
|
|
377
452
|
}
|
|
378
453
|
|
|
379
|
-
|
|
380
|
-
|
|
454
|
+
interface Selection {
|
|
455
|
+
readonly voice: SynthesisVoiceLike;
|
|
456
|
+
readonly match: VoiceSelectionMatch;
|
|
457
|
+
readonly offRegion: boolean;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Voices of the locale's language, best first: match rank, then the browser's default flag, then
|
|
462
|
+
* the browser's own order (the sort is stable).
|
|
463
|
+
*/
|
|
464
|
+
function rank(available: SynthesisVoiceLike[], locale: string) {
|
|
465
|
+
return available
|
|
466
|
+
.map((voice, index) => ({ voice, index, match: matchLocale(locale, voice.lang) }))
|
|
467
|
+
.filter((entry): entry is { voice: SynthesisVoiceLike; index: number; match: NonNullable<typeof entry.match> } => entry.match !== undefined)
|
|
468
|
+
.sort(
|
|
469
|
+
(a, b) =>
|
|
470
|
+
a.match.rank - b.match.rank ||
|
|
471
|
+
Number(b.voice.default === true) - Number(a.voice.default === true) ||
|
|
472
|
+
a.index - b.index,
|
|
473
|
+
);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function tier(match: VoiceLocaleMatch): number {
|
|
477
|
+
return match === "exact" ? 0 : match === "region" ? 1 : 2;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function isOffRegion(locale: string, match: VoiceSelectionMatch): boolean {
|
|
481
|
+
return match === "language" && /[-_]/.test(locale.trim());
|
|
381
482
|
}
|
|
382
483
|
|
|
383
|
-
function
|
|
384
|
-
|
|
484
|
+
function describe(selection: Selection): ChosenVoice {
|
|
485
|
+
const voice = selection.voice;
|
|
486
|
+
return {
|
|
487
|
+
name: voice.name,
|
|
488
|
+
...(voice.voiceURI === undefined ? {} : { voiceURI: voice.voiceURI }),
|
|
489
|
+
lang: voice.lang,
|
|
490
|
+
localService: voice.localService === true,
|
|
491
|
+
match: selection.match,
|
|
492
|
+
offRegion: selection.offRegion,
|
|
493
|
+
};
|
|
385
494
|
}
|
|
386
495
|
|
|
387
496
|
function mapSynthesisError(error: string): VoiceError {
|
package/src/web/turns.ts
CHANGED
|
@@ -12,24 +12,17 @@
|
|
|
12
12
|
* always ends in exactly one terminal event. Starting a new turn while the assistant is still
|
|
13
13
|
* talking is the common case on a page, so it is the default, not an option.
|
|
14
14
|
*/
|
|
15
|
-
import type {
|
|
15
|
+
import type { SessionEvent, TranscriptionProvider, TranscriptionSession } from "./stt/speech-recognition";
|
|
16
16
|
import { VoiceError } from "../contract/errors";
|
|
17
|
+
import type { TurnEvent } from "../contract/turns";
|
|
17
18
|
|
|
18
19
|
/** What the assistant does with its answer. The manager never knows which engine is behind it. */
|
|
19
20
|
export interface SpeechSink {
|
|
20
21
|
speak(text: string, options?: { signal?: AbortSignal }): { done: Promise<unknown>; cancel(): void };
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
| { readonly type: "partial"; readonly partial: PartialTranscript }
|
|
26
|
-
| { readonly type: "transcript"; readonly text: string }
|
|
27
|
-
| { readonly type: "speaking"; readonly text: string }
|
|
28
|
-
| { readonly type: "speech-finished" }
|
|
29
|
-
| { readonly type: "speech-cancelled" }
|
|
30
|
-
| { readonly type: "barge-in" }
|
|
31
|
-
| { readonly type: "failed"; readonly error: VoiceError }
|
|
32
|
-
| { readonly type: "cancelled" };
|
|
24
|
+
/** The shared turn vocabulary (`@adea-ai/voice/contract`), re-exported for existing imports. */
|
|
25
|
+
export type { TurnEvent } from "../contract/turns";
|
|
33
26
|
|
|
34
27
|
export interface TurnManagerOptions {
|
|
35
28
|
readonly transcription: TranscriptionProvider;
|
|
@@ -88,6 +81,10 @@ export class VoiceTurnManager {
|
|
|
88
81
|
session = await this.options.transcription.createSession({
|
|
89
82
|
locale: this.options.locale,
|
|
90
83
|
allowNetwork: this.options.allowNetwork === true,
|
|
84
|
+
// The recognizer's speech start is barge-in: no host glue needed.
|
|
85
|
+
onSpeechStart: () => {
|
|
86
|
+
if (!this.finished && generation === this.generation) this.notifySpeechDetected();
|
|
87
|
+
},
|
|
91
88
|
});
|
|
92
89
|
} catch (error) {
|
|
93
90
|
if (this.finished) throw new VoiceError("cancelled", "the turn manager was cancelled");
|
|
@@ -222,3 +219,17 @@ export class VoiceTurnManager {
|
|
|
222
219
|
function defaultListenerError(error: unknown): void {
|
|
223
220
|
console.error("voice turn listener threw", error);
|
|
224
221
|
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* For hosts that create recognition sessions themselves: spread into the session options so the
|
|
225
|
+
* recognizer's speech start stops the manager's answer (`notifySpeechDetected()`).
|
|
226
|
+
*
|
|
227
|
+
* ```ts
|
|
228
|
+
* const session = await provider.createSession({ locale, allowNetwork: false, ...bargeInOnSpeechStart(turns) });
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
export function bargeInOnSpeechStart(manager: Pick<VoiceTurnManager, "notifySpeechDetected">): {
|
|
232
|
+
readonly onSpeechStart: () => void;
|
|
233
|
+
} {
|
|
234
|
+
return { onSpeechStart: () => manager.notifySpeechDetected() };
|
|
235
|
+
}
|