@adea-ai/voice 0.0.0-stage → 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 +136 -2
- package/dist/cli/helpers-cli.d.ts +2 -0
- package/dist/cli/helpers-cli.js +117 -0
- package/dist/contract/contract.d.ts +157 -0
- package/dist/contract/errors.d.ts +26 -0
- package/dist/contract/index.d.ts +10 -0
- package/dist/contract/index.js +64 -0
- package/dist/contract/session.d.ts +71 -0
- package/dist/contract/turns.d.ts +91 -0
- package/dist/contract/wire.d.ts +279 -0
- package/dist/desktop/conversation.d.ts +86 -0
- package/dist/desktop/desktop-voice.d.ts +212 -0
- package/dist/desktop/helper-lock.d.ts +121 -0
- package/dist/desktop/helper-process.d.ts +96 -0
- package/dist/desktop/index.d.ts +9 -0
- package/dist/desktop/index.js +1454 -0
- package/dist/desktop/macho-digest.d.ts +9 -0
- package/dist/index-7n7epq1a.js +47 -0
- package/dist/index-a7d617zr.js +295 -0
- package/dist/index-ght8wwpd.js +156 -0
- package/dist/index-m4g2tpeq.js +522 -0
- package/dist/index-r4hz8gk0.js +10 -0
- package/dist/index-tvz9s9nz.js +77 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +96 -0
- package/dist/settings/index.d.ts +5 -0
- package/dist/settings/index.js +34 -0
- package/dist/settings/settings.d.ts +163 -0
- package/dist/web/index.d.ts +16 -0
- package/dist/web/index.js +1697 -0
- package/dist/web/stt/speech-recognition.d.ts +226 -0
- package/dist/web/tts/locale-match.d.ts +40 -0
- package/dist/web/tts/phonon.d.ts +139 -0
- package/dist/web/tts/player.d.ts +115 -0
- package/dist/web/tts/system-voices.d.ts +162 -0
- package/dist/web/turns.d.ts +91 -0
- package/package.json +68 -4
- package/src/cli/helpers-cli.ts +135 -0
- package/src/contract/contract.ts +393 -0
- package/src/contract/errors.ts +98 -0
- package/src/contract/index.ts +10 -0
- package/src/contract/session.ts +77 -0
- package/src/contract/turns.ts +48 -0
- package/src/contract/wire.ts +384 -0
- package/src/desktop/conversation.ts +347 -0
- package/src/desktop/desktop-voice.ts +1129 -0
- package/src/desktop/helper-lock.ts +550 -0
- package/src/desktop/helper-process.ts +408 -0
- package/src/desktop/index.ts +18 -0
- package/src/desktop/macho-digest.ts +135 -0
- package/src/index.ts +7 -0
- package/src/settings/index.ts +5 -0
- package/src/settings/settings.ts +478 -0
- package/src/web/index.ts +16 -0
- package/src/web/stt/speech-recognition.ts +1013 -0
- package/src/web/tts/locale-match.ts +131 -0
- package/src/web/tts/phonon.ts +386 -0
- package/src/web/tts/player.ts +271 -0
- package/src/web/tts/system-voices.ts +510 -0
- package/src/web/turns.ts +235 -0
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Octy-style voice conversations on the desktop: `DesktopVoice.conversation()` over the
|
|
3
|
+
* `adea-voice-helper` turn protocol (wire 1.2, #34).
|
|
4
|
+
*
|
|
5
|
+
* A conversation is a run of turns on one helper. Each turn listens, finalizes a transcript, and
|
|
6
|
+
* speaks the answer the host streams back with `respond()`; the next turn starts by itself when the
|
|
7
|
+
* answer has played (`continuous`, the default) or when the host calls `listen()`. Starting a turn
|
|
8
|
+
* while the answer plays is a barge-in: the helper stops the speech with a request cancel before
|
|
9
|
+
* the microphone opens. Every turn reports the shared `TurnEvent` vocabulary (the browser
|
|
10
|
+
* `VoiceTurnManager` speaks it too), tagged with the turn's id, and ends in exactly one `ended` or
|
|
11
|
+
* `failed`.
|
|
12
|
+
*/
|
|
13
|
+
import { VoiceError } from "../contract/errors";
|
|
14
|
+
import type { TurnEvent } from "../contract/turns";
|
|
15
|
+
import { isTurnTerminal, type WireEvent } from "../contract/wire";
|
|
16
|
+
import { nextRequestId, type HelperConnection } from "./helper-process";
|
|
17
|
+
|
|
18
|
+
export interface ConversationRespondOptions {
|
|
19
|
+
/** Whether this is the answer's last chunk. Default true: one call is one whole answer. */
|
|
20
|
+
readonly final?: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** A running conversation. */
|
|
24
|
+
export interface DesktopConversation {
|
|
25
|
+
readonly provider: string;
|
|
26
|
+
readonly locale: string;
|
|
27
|
+
/**
|
|
28
|
+
* Every turn's events in order, each tagged with `turn`. Finishes after the conversation's
|
|
29
|
+
* last terminal: a `failed` turn, `cancelled` (from `cancel()` or the helper), or the `ended`
|
|
30
|
+
* of the last turn after `end()`. Can be read once.
|
|
31
|
+
*/
|
|
32
|
+
events(): AsyncIterable<TurnEvent>;
|
|
33
|
+
/**
|
|
34
|
+
* Speaks (part of) the current turn's answer. Chunks queue without gaps, so a streaming model's
|
|
35
|
+
* text can be spoken as it arrives; send `{ final: true }` (an empty text is fine) to finish
|
|
36
|
+
* the answer. Responding while the person is still talking closes the microphone.
|
|
37
|
+
*/
|
|
38
|
+
respond(textChunk: string, options?: ConversationRespondOptions): Promise<void>;
|
|
39
|
+
/** Starts the next turn now; while the answer plays this is a barge-in. */
|
|
40
|
+
listen(): Promise<void>;
|
|
41
|
+
/** Push-to-talk release: stop needing audio; the transcript of what was heard follows. */
|
|
42
|
+
stopListening(): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Finishes after the current exchange: no further turns start. A turn waiting for an answer
|
|
45
|
+
* closes now; one that is listening finalizes first (then `end()` again, or `respond()`).
|
|
46
|
+
*/
|
|
47
|
+
end(): Promise<void>;
|
|
48
|
+
/** Stops everything now: speech, microphone, further turns. Ends with `cancelled`. Idempotent. */
|
|
49
|
+
cancel(): Promise<void>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface ConversationOptions {
|
|
53
|
+
readonly connection: HelperConnection;
|
|
54
|
+
readonly provider: string;
|
|
55
|
+
readonly locale: string;
|
|
56
|
+
readonly engine: "analyzer" | "legacy" | undefined;
|
|
57
|
+
readonly bargeIn: boolean;
|
|
58
|
+
readonly halfDuplex: boolean;
|
|
59
|
+
readonly continuous: boolean;
|
|
60
|
+
/** Voice fields sent with every `turn-respond` (already on the helper's scale). */
|
|
61
|
+
readonly voice: { readonly voice?: string; readonly locale?: string; readonly rate?: number };
|
|
62
|
+
/** After `cancel()`, how long the helper gets to report the terminal before it is restarted. */
|
|
63
|
+
readonly terminalGraceMs: number;
|
|
64
|
+
readonly onStuck: () => void;
|
|
65
|
+
readonly onFinished: () => void;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
interface Turn {
|
|
69
|
+
readonly id: string;
|
|
70
|
+
state: string | undefined;
|
|
71
|
+
/** The host sent the answer's last chunk. */
|
|
72
|
+
final: boolean;
|
|
73
|
+
ended: boolean;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export class Conversation implements DesktopConversation {
|
|
77
|
+
readonly provider: string;
|
|
78
|
+
readonly locale: string;
|
|
79
|
+
private current: Turn | undefined;
|
|
80
|
+
private readonly queue: TurnEvent[] = [];
|
|
81
|
+
private waiter: ((result: IteratorResult<TurnEvent>) => void) | undefined;
|
|
82
|
+
private consumed = false;
|
|
83
|
+
private finished = false;
|
|
84
|
+
private ending = false;
|
|
85
|
+
private cancelling = false;
|
|
86
|
+
private graceTimer: ReturnType<typeof setTimeout> | undefined;
|
|
87
|
+
|
|
88
|
+
constructor(private readonly options: ConversationOptions) {
|
|
89
|
+
this.provider = options.provider;
|
|
90
|
+
this.locale = options.locale;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
get isFinished(): boolean {
|
|
94
|
+
return this.finished;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Starts the first turn; resolves once the microphone is open, rejects if the turn failed. */
|
|
98
|
+
async begin(): Promise<void> {
|
|
99
|
+
await this.startTurn();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
events(): AsyncIterable<TurnEvent> {
|
|
103
|
+
if (this.consumed) throw new VoiceError("bad_request", "A conversation's events can be read once.");
|
|
104
|
+
this.consumed = true;
|
|
105
|
+
return {
|
|
106
|
+
[Symbol.asyncIterator]: () => ({
|
|
107
|
+
next: () => {
|
|
108
|
+
const next = this.queue.shift();
|
|
109
|
+
if (next) return Promise.resolve({ value: next, done: false });
|
|
110
|
+
if (this.finished) return Promise.resolve({ value: undefined, done: true } as IteratorResult<TurnEvent>);
|
|
111
|
+
return new Promise<IteratorResult<TurnEvent>>((resolve) => {
|
|
112
|
+
this.waiter = resolve;
|
|
113
|
+
});
|
|
114
|
+
},
|
|
115
|
+
return: async () => {
|
|
116
|
+
await this.cancel();
|
|
117
|
+
return { value: undefined, done: true } as IteratorResult<TurnEvent>;
|
|
118
|
+
},
|
|
119
|
+
}),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async respond(textChunk: string, options: ConversationRespondOptions = {}): Promise<void> {
|
|
124
|
+
const final = options.final ?? true;
|
|
125
|
+
if (this.finished || this.cancelling) throw new VoiceError("cancelled", "The conversation is over.");
|
|
126
|
+
const turn = this.current;
|
|
127
|
+
if (!turn || turn.ended) throw new VoiceError("bad_request", "No turn is waiting for an answer.");
|
|
128
|
+
if (turn.final) throw new VoiceError("bad_request", "This turn's answer is already complete.");
|
|
129
|
+
if (textChunk.length === 0 && !final) return;
|
|
130
|
+
if (final) turn.final = true;
|
|
131
|
+
await this.options.connection.send({
|
|
132
|
+
cmd: "turn-respond",
|
|
133
|
+
id: turn.id,
|
|
134
|
+
text: textChunk,
|
|
135
|
+
final,
|
|
136
|
+
...this.options.voice,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async listen(): Promise<void> {
|
|
141
|
+
if (this.finished || this.cancelling) throw new VoiceError("cancelled", "The conversation is over.");
|
|
142
|
+
const turn = this.current;
|
|
143
|
+
if (turn && !turn.ended && turn.state === "speaking" && !this.options.bargeIn && this.options.halfDuplex) {
|
|
144
|
+
// The helper would refuse the new turn; refusing here keeps the playing turn current.
|
|
145
|
+
throw new VoiceError("busy", "The answer is still playing and barge-in is off.");
|
|
146
|
+
}
|
|
147
|
+
this.ending = false;
|
|
148
|
+
await this.startTurn();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async stopListening(): Promise<void> {
|
|
152
|
+
const turn = this.current;
|
|
153
|
+
if (!turn || turn.ended || (turn.state !== "listening" && turn.state !== undefined)) return;
|
|
154
|
+
await this.options.connection.send({ cmd: "turn-end", id: turn.id }).catch(() => undefined);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async end(): Promise<void> {
|
|
158
|
+
if (this.finished) return;
|
|
159
|
+
this.ending = true;
|
|
160
|
+
const turn = this.current;
|
|
161
|
+
if (!turn || turn.ended) return this.finish();
|
|
162
|
+
await this.options.connection.send({ cmd: "turn-end", id: turn.id }).catch(() => undefined);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async cancel(): Promise<void> {
|
|
166
|
+
if (this.finished || this.cancelling) return;
|
|
167
|
+
this.cancelling = true;
|
|
168
|
+
const turn = this.current;
|
|
169
|
+
if (!turn || turn.ended) {
|
|
170
|
+
this.push({ type: "cancelled" });
|
|
171
|
+
return this.finish();
|
|
172
|
+
}
|
|
173
|
+
await this.options.connection.send({ cmd: "turn-cancel", id: turn.id }).catch(() => undefined);
|
|
174
|
+
// A helper that never answers is restarted; the conversation still ends exactly once.
|
|
175
|
+
this.graceTimer ??= setTimeout(() => {
|
|
176
|
+
if (this.finished) return;
|
|
177
|
+
this.closeTurn(turn, { type: "ended", reason: "cancelled", turn: turn.id });
|
|
178
|
+
this.push({ type: "cancelled" });
|
|
179
|
+
this.finish();
|
|
180
|
+
this.options.onStuck();
|
|
181
|
+
}, this.options.terminalGraceMs);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// MARK: - Turns
|
|
185
|
+
|
|
186
|
+
private startTurn(): Promise<void> {
|
|
187
|
+
const turn: Turn = { id: nextRequestId("turn-"), state: undefined, final: false, ended: false };
|
|
188
|
+
this.current = turn;
|
|
189
|
+
let settleStart!: { resolve: () => void; reject: (error: VoiceError) => void };
|
|
190
|
+
const started = new Promise<void>((resolve, reject) => {
|
|
191
|
+
settleStart = { resolve, reject };
|
|
192
|
+
});
|
|
193
|
+
this.options.connection
|
|
194
|
+
.request(
|
|
195
|
+
{
|
|
196
|
+
cmd: "turn-start",
|
|
197
|
+
id: turn.id,
|
|
198
|
+
locale: this.locale,
|
|
199
|
+
...(this.options.engine ? { engine: this.options.engine } : {}),
|
|
200
|
+
bargeIn: this.options.bargeIn,
|
|
201
|
+
halfDuplex: this.options.halfDuplex,
|
|
202
|
+
},
|
|
203
|
+
isTurnTerminal,
|
|
204
|
+
{
|
|
205
|
+
timeoutMs: null,
|
|
206
|
+
onEvent: (event) => {
|
|
207
|
+
this.forward(turn, event);
|
|
208
|
+
if (event.event === "turn-state" && event.state === "listening") settleStart.resolve();
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
)
|
|
212
|
+
.then(
|
|
213
|
+
(terminal) => {
|
|
214
|
+
if (terminal.event === "turn-error") {
|
|
215
|
+
settleStart.reject(new VoiceError(terminal.code, terminal.message ?? "The turn failed."));
|
|
216
|
+
} else {
|
|
217
|
+
settleStart.resolve();
|
|
218
|
+
}
|
|
219
|
+
this.afterTerminal(turn);
|
|
220
|
+
},
|
|
221
|
+
(error: unknown) => {
|
|
222
|
+
// The helper crashed, refused the command or was stopped: the turn still ends once.
|
|
223
|
+
const typed = VoiceError.wrapping(error);
|
|
224
|
+
this.closeTurn(turn, { type: "failed", error: typed, turn: turn.id });
|
|
225
|
+
settleStart.reject(typed);
|
|
226
|
+
this.afterTerminal(turn);
|
|
227
|
+
},
|
|
228
|
+
);
|
|
229
|
+
// `begin()`/`listen()` callers see the failure; the events stream has it too.
|
|
230
|
+
started.catch(() => undefined);
|
|
231
|
+
return started;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Maps one helper line of this turn to the shared vocabulary. */
|
|
235
|
+
private forward(turn: Turn, event: WireEvent): void {
|
|
236
|
+
if (turn.ended) return;
|
|
237
|
+
const tag = { turn: turn.id };
|
|
238
|
+
switch (event.event) {
|
|
239
|
+
case "turn-state":
|
|
240
|
+
turn.state = event.state;
|
|
241
|
+
switch (event.state) {
|
|
242
|
+
case "listening":
|
|
243
|
+
case "responding":
|
|
244
|
+
case "speaking":
|
|
245
|
+
case "idle":
|
|
246
|
+
if (event.state === "idle") turn.final = false; // the host may answer again
|
|
247
|
+
return this.push({ type: event.state, ...tag });
|
|
248
|
+
case "interrupted":
|
|
249
|
+
return; // `barge-in` already said it
|
|
250
|
+
case "ended":
|
|
251
|
+
return this.closeTurn(turn, { type: "ended", reason: event.reason ?? "completed", ...tag });
|
|
252
|
+
}
|
|
253
|
+
return;
|
|
254
|
+
case "turn-partial":
|
|
255
|
+
return this.push({
|
|
256
|
+
type: "partial",
|
|
257
|
+
partial: { finalizedText: event.finalizedText, volatileText: event.volatileText, sequence: event.sequence },
|
|
258
|
+
...tag,
|
|
259
|
+
});
|
|
260
|
+
case "turn-transcript":
|
|
261
|
+
return this.push({ type: "transcript", text: event.text, ...tag });
|
|
262
|
+
case "turn-barge-in":
|
|
263
|
+
return this.push({ type: "barge-in", ...tag });
|
|
264
|
+
case "turn-speech-started":
|
|
265
|
+
return this.push({ type: "speech-started", utterance: event.utterance, ...tag });
|
|
266
|
+
case "turn-speech-finished":
|
|
267
|
+
return this.push({ type: "speech-finished", utterance: event.utterance, ...tag });
|
|
268
|
+
case "turn-speech-cancelled":
|
|
269
|
+
return this.push({ type: "speech-cancelled", utterance: event.utterance, ...tag });
|
|
270
|
+
case "turn-metrics":
|
|
271
|
+
return this.push({
|
|
272
|
+
type: "metrics",
|
|
273
|
+
metrics: {
|
|
274
|
+
firstPartialMs: event.firstPartialMs,
|
|
275
|
+
transcriptMs: event.transcriptMs,
|
|
276
|
+
firstAudioMs: event.firstAudioMs,
|
|
277
|
+
speechFinishedMs: event.speechFinishedMs,
|
|
278
|
+
},
|
|
279
|
+
...tag,
|
|
280
|
+
});
|
|
281
|
+
case "turn-error":
|
|
282
|
+
return this.closeTurn(turn, {
|
|
283
|
+
type: "failed",
|
|
284
|
+
error: new VoiceError(event.code, event.message ?? "The turn failed."),
|
|
285
|
+
...tag,
|
|
286
|
+
});
|
|
287
|
+
default:
|
|
288
|
+
return; // `idle` answers to a late command; nothing to report
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
private closeTurn(turn: Turn, terminal: TurnEvent): void {
|
|
293
|
+
if (turn.ended) return;
|
|
294
|
+
turn.ended = true;
|
|
295
|
+
this.push(terminal);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** What the conversation does once a turn has ended. */
|
|
299
|
+
private afterTerminal(turn: Turn): void {
|
|
300
|
+
const reason = this.reasons.get(turn.id);
|
|
301
|
+
this.reasons.delete(turn.id);
|
|
302
|
+
// A turn that a newer one replaced (barge-in, `listen()`) has nothing more to decide.
|
|
303
|
+
if (turn !== this.current || this.finished) return;
|
|
304
|
+
this.current = undefined;
|
|
305
|
+
if (this.cancelling || reason === "cancelled") {
|
|
306
|
+
this.push({ type: "cancelled" });
|
|
307
|
+
return this.finish();
|
|
308
|
+
}
|
|
309
|
+
if (reason === "failed" || this.ending) return this.finish();
|
|
310
|
+
if (reason === "completed" && this.options.continuous) {
|
|
311
|
+
void this.startTurn().catch(() => undefined);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** How each turn ended (`failed` for an error), until the conversation has acted on it. */
|
|
316
|
+
private readonly reasons = new Map<string, string>();
|
|
317
|
+
|
|
318
|
+
private finish(): void {
|
|
319
|
+
if (this.finished) return;
|
|
320
|
+
this.finished = true;
|
|
321
|
+
clearTimeout(this.graceTimer);
|
|
322
|
+
this.options.onFinished();
|
|
323
|
+
if (this.queue.length === 0 && this.waiter) {
|
|
324
|
+
const waiter = this.waiter;
|
|
325
|
+
this.waiter = undefined;
|
|
326
|
+
waiter({ value: undefined, done: true } as IteratorResult<TurnEvent>);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
private push(event: TurnEvent): void {
|
|
331
|
+
if (event.type === "ended" && event.turn) this.reasons.set(event.turn, event.reason);
|
|
332
|
+
if (event.type === "failed" && event.turn) this.reasons.set(event.turn, "failed");
|
|
333
|
+
if (this.finished) return;
|
|
334
|
+
if (this.waiter) {
|
|
335
|
+
const waiter = this.waiter;
|
|
336
|
+
this.waiter = undefined;
|
|
337
|
+
waiter({ value: event, done: false });
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
// Bounded: partials replace each other, so only the latest pending partial is kept.
|
|
341
|
+
if (event.type === "partial") {
|
|
342
|
+
const last = this.queue.at(-1);
|
|
343
|
+
if (last?.type === "partial" && last.turn === event.turn) this.queue.pop();
|
|
344
|
+
}
|
|
345
|
+
this.queue.push(event);
|
|
346
|
+
}
|
|
347
|
+
}
|