@wcstack/speech 1.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,493 @@
1
+ interface ITagNames {
2
+ readonly speak: string;
3
+ readonly listen: string;
4
+ }
5
+ interface IWritableTagNames {
6
+ speak?: string;
7
+ listen?: string;
8
+ }
9
+ interface IConfig {
10
+ readonly autoTrigger: boolean;
11
+ /** DOM autoTrigger attribute for `<wcs-speak>` (click → speak). */
12
+ readonly triggerAttribute: string;
13
+ /** DOM autoTrigger attribute for `<wcs-listen>` (click → toggle start/stop). */
14
+ readonly listenTriggerAttribute: string;
15
+ readonly tagNames: ITagNames;
16
+ }
17
+ interface IWritableConfig {
18
+ autoTrigger?: boolean;
19
+ triggerAttribute?: string;
20
+ listenTriggerAttribute?: string;
21
+ tagNames?: IWritableTagNames;
22
+ }
23
+ interface IWcBindableProperty {
24
+ readonly name: string;
25
+ readonly event: string;
26
+ readonly getter?: (event: Event) => any;
27
+ }
28
+ interface IWcBindableInput {
29
+ readonly name: string;
30
+ readonly attribute?: string;
31
+ }
32
+ interface IWcBindableCommand {
33
+ readonly name: string;
34
+ readonly async?: boolean;
35
+ }
36
+ interface IWcBindable {
37
+ readonly protocol: "wc-bindable";
38
+ readonly version: number;
39
+ readonly properties: IWcBindableProperty[];
40
+ readonly inputs?: IWcBindableInput[];
41
+ readonly commands?: IWcBindableCommand[];
42
+ }
43
+ /**
44
+ * Structured-clone-friendly snapshot of a `SpeechSynthesisVoice`. The live voice
45
+ * objects are not serializable and cannot flow through data binding, so the Core
46
+ * exposes this plain copy. `name` is the selection key used by the `voice`
47
+ * input.
48
+ */
49
+ interface SpeechVoiceInfo {
50
+ name: string;
51
+ lang: string;
52
+ default: boolean;
53
+ localService: boolean;
54
+ voiceURI: string;
55
+ }
56
+ /**
57
+ * Normalized speech-synthesis failure. `error` mirrors
58
+ * `SpeechSynthesisErrorEvent.error` (e.g. `"canceled"`, `"interrupted"`,
59
+ * `"not-allowed"`, `"synthesis-failed"`); `"unsupported"` is surfaced when the
60
+ * SpeechSynthesis API is absent.
61
+ */
62
+ interface WcsSpeakErrorDetail {
63
+ error: string;
64
+ message: string;
65
+ }
66
+ /**
67
+ * Per-utterance parameters accepted by `speak()`, mirroring the settable fields
68
+ * of `SpeechSynthesisUtterance`. `voice` selects a voice by its `name`.
69
+ */
70
+ interface SpeakOptions {
71
+ rate?: number;
72
+ pitch?: number;
73
+ volume?: number;
74
+ voice?: string;
75
+ lang?: string;
76
+ }
77
+ /**
78
+ * Value types for SpeakCore (headless) — the observable state properties. Use
79
+ * with `bind()` from `@wc-bindable/core` for compile-time type checking.
80
+ */
81
+ interface WcsSpeakCoreValues {
82
+ voices: SpeechVoiceInfo[];
83
+ speaking: boolean;
84
+ paused: boolean;
85
+ pending: boolean;
86
+ charIndex: number | null;
87
+ spokenWord: string | null;
88
+ error: WcsSpeakErrorDetail | null;
89
+ unsupported: boolean;
90
+ }
91
+ /**
92
+ * Value types for the Shell (`<wcs-speak>`) — the Core's observable surface plus
93
+ * the reactive `say` command-property.
94
+ */
95
+ interface WcsSpeakValues extends WcsSpeakCoreValues {
96
+ say: string;
97
+ }
98
+ interface WcsSpeakInputs {
99
+ /**
100
+ * Reactive command-property (no mirrored attribute): writing a value speaks it.
101
+ * A same-value write is suppressed, so it fires only when the bound source
102
+ * actually changes — ideal for status / a11y announcements. Bind through a
103
+ * `|debounce` filter when wired to a rapidly-changing source (e.g. an
104
+ * `<input>` value). For "speak this on demand, even the same text again", use
105
+ * the imperative `speak` command instead.
106
+ */
107
+ say: string;
108
+ rate: number;
109
+ pitch: number;
110
+ volume: number;
111
+ voice: string;
112
+ lang: string;
113
+ /**
114
+ * Suppress the reactive `say` path. The imperative `speak` command still
115
+ * works. Also the hook used to mute speaking while listening, to avoid a
116
+ * recognition echo loop.
117
+ */
118
+ manual: boolean;
119
+ }
120
+ interface WcsSpeakCoreCommands {
121
+ speak(text: string, options?: SpeakOptions): void;
122
+ cancel(): void;
123
+ pause(): void;
124
+ resume(): void;
125
+ }
126
+ interface WcsSpeakCommands {
127
+ speak(text: string): void;
128
+ cancel(): void;
129
+ pause(): void;
130
+ resume(): void;
131
+ }
132
+ /**
133
+ * Permission state for the microphone, mirroring the Permissions API
134
+ * `PermissionState` plus `"unsupported"` for environments without
135
+ * `navigator.permissions` (or where the `microphone` permission cannot be
136
+ * queried).
137
+ */
138
+ type ListenPermissionState = "prompt" | "granted" | "denied" | "unsupported";
139
+ interface WcsListenAlternative {
140
+ transcript: string;
141
+ confidence: number;
142
+ }
143
+ /**
144
+ * Structured-clone-friendly snapshot of the most recent recognition result —
145
+ * the top alternative flattened (`transcript` / `confidence`) plus the full
146
+ * `alternatives` list and whether the result is final.
147
+ */
148
+ interface WcsListenResultDetail {
149
+ transcript: string;
150
+ confidence: number;
151
+ isFinal: boolean;
152
+ alternatives: WcsListenAlternative[];
153
+ }
154
+ /**
155
+ * Normalized recognition failure. `error` mirrors
156
+ * `SpeechRecognitionErrorEvent.error` (e.g. `"no-speech"`, `"not-allowed"`,
157
+ * `"network"`, `"aborted"`); `"unsupported"` is surfaced when the
158
+ * SpeechRecognition API is absent.
159
+ */
160
+ interface WcsListenErrorDetail {
161
+ error: string;
162
+ message: string;
163
+ }
164
+ /**
165
+ * Options accepted by `start()`, mirroring the settable fields of
166
+ * `SpeechRecognition`.
167
+ */
168
+ interface ListenOptions {
169
+ lang?: string;
170
+ continuous?: boolean;
171
+ interimResults?: boolean;
172
+ maxAlternatives?: number;
173
+ /**
174
+ * Maximum number of automatic session restarts in continuous mode before
175
+ * giving up. Bounds the auto-restart loop so a persistent failure cannot spin
176
+ * forever or exhaust quota.
177
+ */
178
+ maxRestarts?: number;
179
+ }
180
+ /**
181
+ * Value types for ListenCore (headless) — the observable state properties.
182
+ */
183
+ interface WcsListenCoreValues {
184
+ interimTranscript: string;
185
+ finalTranscript: string;
186
+ result: WcsListenResultDetail | null;
187
+ listening: boolean;
188
+ permission: ListenPermissionState;
189
+ error: WcsListenErrorDetail | null;
190
+ unsupported: boolean;
191
+ }
192
+ /**
193
+ * Value types for the Shell (`<wcs-listen>`) — the Core's observable surface plus
194
+ * the DOM-driven `trigger` command-property.
195
+ */
196
+ interface WcsListenValues extends WcsListenCoreValues {
197
+ trigger: boolean;
198
+ }
199
+ interface WcsListenInputs {
200
+ lang: string;
201
+ continuous: boolean;
202
+ interim: boolean;
203
+ maxRestarts: number;
204
+ manual: boolean;
205
+ /**
206
+ * Momentary command-property (no mirrored attribute): a `false`→`true` write
207
+ * starts a recognition session, then the flag immediately resets to `false`.
208
+ */
209
+ trigger: boolean;
210
+ }
211
+ interface WcsListenCoreCommands {
212
+ start(options?: ListenOptions): void;
213
+ stop(): void;
214
+ abort(): void;
215
+ }
216
+ interface WcsListenCommands {
217
+ start(): void;
218
+ stop(): void;
219
+ abort(): void;
220
+ }
221
+
222
+ declare function bootstrapSpeech(userConfig?: IWritableConfig): void;
223
+
224
+ declare function getConfig(): IConfig;
225
+
226
+ /**
227
+ * Headless text-to-speech primitive. A thin, framework-agnostic wrapper around
228
+ * the SpeechSynthesis API exposed through the wc-bindable protocol.
229
+ *
230
+ * It is the "command" half of the speech package (the recognition half is
231
+ * ListenCore): state drives the element, never the reverse, except for the
232
+ * observable progress/status it publishes back.
233
+ *
234
+ * - **speak(text, options)** queues an utterance. Like the native API, multiple
235
+ * calls queue; `cancel()` clears the queue and stops the current utterance.
236
+ * - **pause() / resume()** suspend and resume the queue.
237
+ * - The observable surface mirrors the live SpeechSynthesis flags
238
+ * (`speaking` / `paused` / `pending`) and exposes voice-list loading
239
+ * (`voices`, which the API populates asynchronously via `voiceschanged`) plus
240
+ * word-boundary progress (`charIndex` / `spokenWord`) for karaoke-style
241
+ * highlighting.
242
+ *
243
+ * Unlike geolocation/clipboard there is no permission gate — synthesis needs no
244
+ * user grant. Failures never throw: they surface through the `error` property so
245
+ * they flow into the declarative state.
246
+ */
247
+ declare class SpeakCore extends EventTarget {
248
+ static wcBindable: IWcBindable;
249
+ private _target;
250
+ private _voices;
251
+ private _rawVoices;
252
+ private _speaking;
253
+ private _paused;
254
+ private _pending;
255
+ private _charIndex;
256
+ private _spokenWord;
257
+ private _error;
258
+ private _unsupported;
259
+ private _queued;
260
+ private _started;
261
+ private _gen;
262
+ private _voicesSubscribed;
263
+ constructor(target?: EventTarget);
264
+ get voices(): SpeechVoiceInfo[];
265
+ get speaking(): boolean;
266
+ get paused(): boolean;
267
+ get pending(): boolean;
268
+ get charIndex(): number | null;
269
+ get spokenWord(): string | null;
270
+ get error(): WcsSpeakErrorDetail | null;
271
+ get unsupported(): boolean;
272
+ private _setVoices;
273
+ private _voicesEqual;
274
+ private _setSpeaking;
275
+ private _setPaused;
276
+ private _setPending;
277
+ private _setBoundary;
278
+ private _setError;
279
+ private _setUnsupported;
280
+ /**
281
+ * Queue an utterance for `text` with optional per-utterance parameters. Never
282
+ * throws: when the API is unavailable it surfaces an `error` and returns. An
283
+ * empty/whitespace-only `text` is a no-op (the browser would not fire start).
284
+ */
285
+ speak(text: string, options?: SpeakOptions): void;
286
+ /**
287
+ * Clear the queue and stop the current utterance immediately. Resets all
288
+ * progress state synchronously and invalidates in-flight utterance callbacks
289
+ * (the browser fires a "canceled" error per utterance) so they do not surface
290
+ * as real errors.
291
+ */
292
+ cancel(): void;
293
+ pause(): void;
294
+ resume(): void;
295
+ /**
296
+ * Re-establish the voiceschanged subscription after a dispose() — e.g. the
297
+ * Shell element was disconnected and then reconnected (reparented). No-op while
298
+ * a subscription is already live, so the first connect after construction does
299
+ * not double-subscribe.
300
+ */
301
+ reinitVoices(): void;
302
+ /**
303
+ * Detach the live voiceschanged listener and neutralize any in-flight
304
+ * utterance callbacks. Call from the Shell's `disconnectedCallback`.
305
+ */
306
+ dispose(): void;
307
+ private _finishUtterance;
308
+ private _hasApi;
309
+ private _initVoices;
310
+ private _onVoicesChanged;
311
+ private _loadVoices;
312
+ private _normalizeVoice;
313
+ private _normalizeError;
314
+ private _unsupportedError;
315
+ }
316
+
317
+ /**
318
+ * `<wcs-speak>` — declarative text-to-speech. Wraps SpeakCore and exposes:
319
+ *
320
+ * - **`say`** (reactive input): writing a value speaks it, suppressing same-value
321
+ * writes so it fires only when the bound source actually changes. The
322
+ * imperative `speak` command instead speaks on demand (even the same text
323
+ * again). See `docs/speech-tag-design.md` § 5.
324
+ * - per-utterance parameters (`rate` / `pitch` / `volume` / `voice` / `lang`) as
325
+ * mirrored attributes.
326
+ * - the Core's observable surface (voices / speaking / paused / pending /
327
+ * charIndex / spokenWord / error / unsupported) via delegated getters.
328
+ */
329
+ declare class WcsSpeak extends HTMLElement {
330
+ static wcBindable: IWcBindable;
331
+ private _core;
332
+ private _say;
333
+ constructor();
334
+ get rate(): number;
335
+ set rate(value: number);
336
+ get pitch(): number;
337
+ set pitch(value: number);
338
+ get volume(): number;
339
+ set volume(value: number);
340
+ get voice(): string;
341
+ set voice(value: string | null);
342
+ get lang(): string;
343
+ set lang(value: string | null);
344
+ get manual(): boolean;
345
+ set manual(value: boolean);
346
+ get say(): string;
347
+ set say(value: string | null);
348
+ get voices(): SpeechVoiceInfo[];
349
+ get speaking(): boolean;
350
+ get paused(): boolean;
351
+ get pending(): boolean;
352
+ get charIndex(): number | null;
353
+ get spokenWord(): string | null;
354
+ get error(): WcsSpeakErrorDetail | null;
355
+ get unsupported(): boolean;
356
+ speak(text: string): void;
357
+ cancel(): void;
358
+ pause(): void;
359
+ resume(): void;
360
+ private _numberAttr;
361
+ private _options;
362
+ connectedCallback(): void;
363
+ disconnectedCallback(): void;
364
+ }
365
+
366
+ /**
367
+ * Headless speech-to-text primitive. A thin, framework-agnostic wrapper around
368
+ * the SpeechRecognition API (vendor-prefixed `webkitSpeechRecognition` in
369
+ * Chrome) exposed through the wc-bindable protocol.
370
+ *
371
+ * It is the "event" half of the speech package (the synthesis half is
372
+ * SpeakCore): recognition results flow element → state.
373
+ *
374
+ * Two phases mirror geolocation:
375
+ * - **one-shot** (`continuous = false`) — recognize until the first `end`.
376
+ * - **continuous** (`continuous = true`) — keep a single session open across
377
+ * phrases. The browser still ends a session on silence; auto-restart bridges
378
+ * that gap **but is opt-in via `maxRestarts`**: with the default `maxRestarts
379
+ * = 0` a continuous session is *not* restarted on `end` (the safe default —
380
+ * unbounded restart is the infinite-loop risk we guard against). Set
381
+ * `maxRestarts > 0` to bridge N silences. The cap also stops a persistent
382
+ * failure (e.g. `not-allowed`) from spinning forever or exhausting quota; a
383
+ * real result resets the budget so only consecutive empty restarts count.
384
+ *
385
+ * A microphone permission gate (like geolocation's) reflects
386
+ * `navigator.permissions.query({ name: "microphone" })`. Failures never throw —
387
+ * they surface through the `error` property.
388
+ */
389
+ declare class ListenCore extends EventTarget {
390
+ static wcBindable: IWcBindable;
391
+ private _target;
392
+ private _recognition;
393
+ private _interimTranscript;
394
+ private _finalTranscript;
395
+ private _result;
396
+ private _listening;
397
+ private _permission;
398
+ private _error;
399
+ private _unsupported;
400
+ private _active;
401
+ private _continuous;
402
+ private _maxRestarts;
403
+ private _restartCount;
404
+ private _permissionStatus;
405
+ private _permissionSubscribed;
406
+ private _permGen;
407
+ constructor(target?: EventTarget);
408
+ get interimTranscript(): string;
409
+ get finalTranscript(): string;
410
+ get result(): WcsListenResultDetail | null;
411
+ get listening(): boolean;
412
+ get permission(): ListenPermissionState;
413
+ get error(): WcsListenErrorDetail | null;
414
+ get unsupported(): boolean;
415
+ private _setInterim;
416
+ private _setFinal;
417
+ private _setResult;
418
+ private _setListening;
419
+ private _setPermission;
420
+ private _setError;
421
+ private _setUnsupported;
422
+ /**
423
+ * Begin a recognition session. Resets the transcripts (a fresh, user-initiated
424
+ * listen), applies options, and starts. Idempotent while already listening: a
425
+ * redundant start() is ignored so the browser does not throw "recognition has
426
+ * already started".
427
+ */
428
+ start(options?: ListenOptions): void;
429
+ stop(): void;
430
+ abort(): void;
431
+ /**
432
+ * Re-establish the permission `change` subscription after a dispose().
433
+ */
434
+ reinitPermission(): void;
435
+ /**
436
+ * Stop recognition and detach the live permission listener. Call from the
437
+ * Shell's `disconnectedCallback`.
438
+ */
439
+ dispose(): void;
440
+ private _attachHandlers;
441
+ private _handleResult;
442
+ private _normalizeResult;
443
+ private _safeStart;
444
+ private _getCtor;
445
+ private _initPermission;
446
+ private _onPermissionChange;
447
+ private _normalizeError;
448
+ private _unsupportedError;
449
+ }
450
+
451
+ /**
452
+ * `<wcs-listen>` — declarative speech-to-text. Wraps ListenCore and exposes the
453
+ * recognition surface (interim/final transcripts, structured result, listening
454
+ * flag, microphone permission, error) plus the two-phase start/stop/abort
455
+ * commands and a momentary `trigger` for DOM-driven starts.
456
+ *
457
+ * Mirrors `<wcs-geo>`: `manual` suppresses the connect-time auto-start, and the
458
+ * `continuous` attribute selects the auto-restarting session phase.
459
+ */
460
+ declare class WcsListen extends HTMLElement {
461
+ static wcBindable: IWcBindable;
462
+ private _core;
463
+ private _trigger;
464
+ constructor();
465
+ get lang(): string;
466
+ set lang(value: string | null);
467
+ get continuous(): boolean;
468
+ set continuous(value: boolean);
469
+ get interim(): boolean;
470
+ set interim(value: boolean);
471
+ get maxRestarts(): number;
472
+ set maxRestarts(value: number);
473
+ get manual(): boolean;
474
+ set manual(value: boolean);
475
+ get interimTranscript(): string;
476
+ get finalTranscript(): string;
477
+ get result(): WcsListenResultDetail | null;
478
+ get listening(): boolean;
479
+ get permission(): ListenPermissionState;
480
+ get error(): WcsListenErrorDetail | null;
481
+ get unsupported(): boolean;
482
+ get trigger(): boolean;
483
+ set trigger(value: boolean);
484
+ start(): void;
485
+ stop(): void;
486
+ abort(): void;
487
+ private _options;
488
+ connectedCallback(): void;
489
+ disconnectedCallback(): void;
490
+ }
491
+
492
+ export { ListenCore, SpeakCore, WcsListen, WcsSpeak, bootstrapSpeech, getConfig };
493
+ export type { IWritableConfig, IWritableTagNames, ListenOptions, ListenPermissionState, SpeakOptions, SpeechVoiceInfo, WcsListenAlternative, WcsListenCommands, WcsListenCoreCommands, WcsListenCoreValues, WcsListenErrorDetail, WcsListenInputs, WcsListenResultDetail, WcsListenValues, WcsSpeakCommands, WcsSpeakCoreCommands, WcsSpeakCoreValues, WcsSpeakErrorDetail, WcsSpeakInputs, WcsSpeakValues };