@oxvo/ai-live-assist 7.4.50 → 7.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/cjs/AiLiveAssist.d.ts +76 -11
  2. package/cjs/AiLiveAssist.js +1771 -298
  3. package/cjs/client.d.ts +19 -0
  4. package/cjs/client.js +51 -0
  5. package/cjs/context.d.ts +23 -1
  6. package/cjs/context.js +713 -126
  7. package/cjs/control.d.ts +2 -0
  8. package/cjs/control.js +42 -3
  9. package/cjs/ctaOffer.d.ts +11 -0
  10. package/cjs/ctaOffer.js +185 -0
  11. package/cjs/dialogFocusScope.js +1 -1
  12. package/cjs/guideAdmission.d.ts +46 -0
  13. package/cjs/guideAdmission.js +397 -0
  14. package/cjs/guideHost.d.ts +47 -0
  15. package/cjs/guideHost.js +48 -0
  16. package/cjs/guideInstallation.d.ts +12 -0
  17. package/cjs/guideInstallation.js +60 -0
  18. package/cjs/guideInstaller.d.ts +4 -0
  19. package/cjs/guideInstaller.js +116 -0
  20. package/cjs/guideMount.d.ts +22 -0
  21. package/cjs/guideMount.js +84 -0
  22. package/cjs/guideRecording.d.ts +64 -0
  23. package/cjs/guideRecording.js +176 -0
  24. package/cjs/guideRecordingSession.d.ts +35 -0
  25. package/cjs/guideRecordingSession.js +167 -0
  26. package/cjs/guideSubjectStore.d.ts +32 -0
  27. package/cjs/guideSubjectStore.js +105 -0
  28. package/cjs/guideTypes.d.ts +117 -0
  29. package/cjs/guideTypes.js +15 -0
  30. package/cjs/index.d.ts +6 -0
  31. package/cjs/index.js +25 -11
  32. package/cjs/liveAudioProcessor.d.ts +92 -0
  33. package/cjs/liveAudioProcessor.js +269 -0
  34. package/cjs/liveAudioQueue.d.ts +40 -0
  35. package/cjs/liveAudioQueue.js +130 -0
  36. package/cjs/liveAudioWorklet.d.ts +1 -0
  37. package/cjs/liveAudioWorklet.js +63 -0
  38. package/cjs/liveBrowserDetector.d.ts +16 -0
  39. package/cjs/liveBrowserDetector.js +75 -0
  40. package/cjs/liveBrowserDetectorWorker.d.ts +1 -0
  41. package/cjs/liveBrowserDetectorWorker.js +72 -0
  42. package/cjs/liveControlChannel.d.ts +176 -0
  43. package/cjs/liveControlChannel.js +829 -0
  44. package/cjs/liveDuplexMedia.d.ts +204 -0
  45. package/cjs/liveDuplexMedia.js +740 -0
  46. package/cjs/liveGuideAdmission.d.ts +21 -0
  47. package/cjs/liveGuideAdmission.js +94 -0
  48. package/cjs/liveGuideWidget.d.ts +68 -0
  49. package/cjs/liveGuideWidget.js +451 -0
  50. package/cjs/liveSpeechAssets.d.ts +36 -0
  51. package/cjs/liveSpeechAssets.js +39 -0
  52. package/cjs/liveSpeechCaptureBridge.d.ts +32 -0
  53. package/cjs/liveSpeechCaptureBridge.js +140 -0
  54. package/cjs/liveSpeechDetector.d.ts +62 -0
  55. package/cjs/liveSpeechDetector.js +206 -0
  56. package/cjs/liveSpeechModel.d.ts +46 -0
  57. package/cjs/liveSpeechModel.js +105 -0
  58. package/cjs/liveSpeechResampler.d.ts +22 -0
  59. package/cjs/liveSpeechResampler.js +93 -0
  60. package/cjs/liveSpeechWorker.d.ts +56 -0
  61. package/cjs/liveSpeechWorker.js +143 -0
  62. package/cjs/locales/resources.js +568 -8
  63. package/cjs/media.d.ts +11 -0
  64. package/cjs/media.js +78 -9
  65. package/cjs/messages.js +22 -8
  66. package/cjs/pageAssistancePreference.d.ts +15 -3
  67. package/cjs/pageAssistancePreference.js +46 -7
  68. package/cjs/preview.d.ts +4 -1
  69. package/cjs/preview.js +42 -1
  70. package/cjs/recording.d.ts +12 -0
  71. package/cjs/recording.js +150 -0
  72. package/cjs/standalone.d.ts +6 -0
  73. package/cjs/standalone.js +12 -0
  74. package/cjs/types.d.ts +31 -5
  75. package/cjs/types.js +14 -0
  76. package/cjs/ui.d.ts +128 -4
  77. package/cjs/ui.js +1918 -276
  78. package/cjs/version.d.ts +1 -1
  79. package/cjs/version.js +1 -1
  80. package/cjs/voicePresenceUi.d.ts +52 -4
  81. package/cjs/voicePresenceUi.js +659 -57
  82. package/lib/AiLiveAssist.d.ts +76 -11
  83. package/lib/AiLiveAssist.js +1772 -299
  84. package/lib/client.d.ts +19 -0
  85. package/lib/client.js +51 -0
  86. package/lib/context.d.ts +23 -1
  87. package/lib/context.js +713 -126
  88. package/lib/control.d.ts +2 -0
  89. package/lib/control.js +42 -3
  90. package/lib/ctaOffer.d.ts +11 -0
  91. package/lib/ctaOffer.js +179 -0
  92. package/lib/dialogFocusScope.js +1 -1
  93. package/lib/guideAdmission.d.ts +46 -0
  94. package/lib/guideAdmission.js +392 -0
  95. package/lib/guideHost.d.ts +47 -0
  96. package/lib/guideHost.js +42 -0
  97. package/lib/guideInstallation.d.ts +12 -0
  98. package/lib/guideInstallation.js +56 -0
  99. package/lib/guideInstaller.d.ts +4 -0
  100. package/lib/guideInstaller.js +113 -0
  101. package/lib/guideMount.d.ts +22 -0
  102. package/lib/guideMount.js +77 -0
  103. package/lib/guideRecording.d.ts +64 -0
  104. package/lib/guideRecording.js +172 -0
  105. package/lib/guideRecordingSession.d.ts +35 -0
  106. package/lib/guideRecordingSession.js +163 -0
  107. package/lib/guideSubjectStore.d.ts +32 -0
  108. package/lib/guideSubjectStore.js +101 -0
  109. package/lib/guideTypes.d.ts +117 -0
  110. package/lib/guideTypes.js +11 -0
  111. package/lib/index.d.ts +6 -0
  112. package/lib/index.js +21 -10
  113. package/lib/liveAudioProcessor.d.ts +92 -0
  114. package/lib/liveAudioProcessor.js +265 -0
  115. package/lib/liveAudioQueue.d.ts +40 -0
  116. package/lib/liveAudioQueue.js +126 -0
  117. package/lib/liveAudioWorklet.d.ts +1 -0
  118. package/lib/liveAudioWorklet.js +61 -0
  119. package/lib/liveBrowserDetector.d.ts +16 -0
  120. package/lib/liveBrowserDetector.js +71 -0
  121. package/lib/liveBrowserDetectorWorker.d.ts +1 -0
  122. package/lib/liveBrowserDetectorWorker.js +70 -0
  123. package/lib/liveControlChannel.d.ts +176 -0
  124. package/lib/liveControlChannel.js +825 -0
  125. package/lib/liveDuplexMedia.d.ts +204 -0
  126. package/lib/liveDuplexMedia.js +736 -0
  127. package/lib/liveGuideAdmission.d.ts +21 -0
  128. package/lib/liveGuideAdmission.js +90 -0
  129. package/lib/liveGuideWidget.d.ts +68 -0
  130. package/lib/liveGuideWidget.js +447 -0
  131. package/lib/liveSpeechAssets.d.ts +36 -0
  132. package/lib/liveSpeechAssets.js +35 -0
  133. package/lib/liveSpeechCaptureBridge.d.ts +32 -0
  134. package/lib/liveSpeechCaptureBridge.js +136 -0
  135. package/lib/liveSpeechDetector.d.ts +62 -0
  136. package/lib/liveSpeechDetector.js +202 -0
  137. package/lib/liveSpeechModel.d.ts +46 -0
  138. package/lib/liveSpeechModel.js +101 -0
  139. package/lib/liveSpeechResampler.d.ts +22 -0
  140. package/lib/liveSpeechResampler.js +89 -0
  141. package/lib/liveSpeechWorker.d.ts +56 -0
  142. package/lib/liveSpeechWorker.js +137 -0
  143. package/lib/locales/resources.js +568 -8
  144. package/lib/media.d.ts +11 -0
  145. package/lib/media.js +77 -8
  146. package/lib/messages.js +22 -8
  147. package/lib/pageAssistancePreference.d.ts +15 -3
  148. package/lib/pageAssistancePreference.js +46 -7
  149. package/lib/preview.d.ts +4 -1
  150. package/lib/preview.js +42 -1
  151. package/lib/recording.d.ts +12 -0
  152. package/lib/recording.js +146 -0
  153. package/lib/standalone.d.ts +6 -0
  154. package/lib/standalone.js +6 -0
  155. package/lib/types.d.ts +31 -5
  156. package/lib/types.js +14 -0
  157. package/lib/ui.d.ts +128 -4
  158. package/lib/ui.js +1919 -277
  159. package/lib/version.d.ts +1 -1
  160. package/lib/version.js +1 -1
  161. package/lib/voicePresenceUi.d.ts +52 -4
  162. package/lib/voicePresenceUi.js +659 -57
  163. package/package.json +16 -1
@@ -0,0 +1,35 @@
1
+ /** Immutable qualification manifest. Not loaded by the existing widget entrypoint. */
2
+ export const LIVE_SPEECH_ASSETS = Object.freeze({
3
+ model: Object.freeze({ name: 'silero_vad_v5.onnx', bytes: 2327524,
4
+ sha256: '2623a2953f6ff3d2c1e61740c6cdb7168133479b267dfef114a4a3cc5bdd788f',
5
+ source: 'https://cdn.jsdelivr.net/npm/@ricky0123/vad-web@0.0.30/dist/silero_vad_v5.onnx' }),
6
+ runtime: Object.freeze({ name: 'ort.wasm.min.mjs', bytes: 48259,
7
+ sha256: '6ef726f355b791129f7ddf3f62e7877a16b9b777abe87004b8ab306e6b9cf265',
8
+ source: 'https://cdn.jsdelivr.net/npm/onnxruntime-web@1.22.0/dist/ort.wasm.min.mjs' }),
9
+ glue: Object.freeze({ name: 'ort-wasm-simd-threaded.mjs', bytes: 20856,
10
+ sha256: '30dd851d9c00622940500f71ddd2ff8820c5cb65270816080175b958705385a8',
11
+ source: 'https://cdn.jsdelivr.net/npm/onnxruntime-web@1.22.0/dist/ort-wasm-simd-threaded.mjs' }),
12
+ wasm: Object.freeze({ name: 'ort-wasm-simd-threaded.wasm', bytes: 11210254,
13
+ sha256: '71aef04959c5c1b6de461b6538e2058e306610034a85aad2742d0c7fd4533fe4',
14
+ source: 'https://cdn.jsdelivr.net/npm/onnxruntime-web@1.22.0/dist/ort-wasm-simd-threaded.wasm' }),
15
+ });
16
+ /** Verify before importing code/creating a model; callers control self-hosted URLs and CSP. */
17
+ export async function verifyLiveSpeechAsset(name, bytes, digest) {
18
+ const expected = LIVE_SPEECH_ASSETS[name];
19
+ if (!expected || bytes.byteLength !== expected.bytes || await digest(bytes) !== expected.sha256) {
20
+ throw new Error('Detector asset verification failed.');
21
+ }
22
+ }
23
+ // Distribute with the selected artifacts, including upstream package third-party
24
+ // notices. The browser wrapper is not installed or used for capture.
25
+ export const LIVE_SPEECH_LICENSE_SOURCES = Object.freeze([
26
+ { name: 'silero-vad-LICENSE.txt', bytes: 1896,
27
+ sha256: '239ebca4207803d3507124517c0dd6324c0d5cf73e1c486ade16bac1788f4a79',
28
+ source: 'https://raw.githubusercontent.com/ricky0123/vad/d08d8be6604cc81aa367ea10218b8120c4505c04/LICENSE' },
29
+ { name: 'onnxruntime-LICENSE.txt', bytes: 1073,
30
+ sha256: '2f07c72751aed99790b8a4869cf2311df85a860b22ded05fa22803587a48922c',
31
+ source: 'https://raw.githubusercontent.com/microsoft/onnxruntime/v1.22.0/LICENSE' },
32
+ { name: 'onnxruntime-ThirdPartyNotices.txt', bytes: 326866,
33
+ sha256: 'e9e90971a8e75a9a8ac0c6412e29c1202d079998389915aa485f46c816c3b4cc',
34
+ source: 'https://raw.githubusercontent.com/microsoft/onnxruntime/v1.22.0/ThirdPartyNotices.txt' },
35
+ ]);
@@ -0,0 +1,32 @@
1
+ import { type LiveSpeechDetectorOptions } from './liveSpeechDetector.js';
2
+ import type { LocalSpeechModel } from './liveSpeechModel.js';
3
+ export interface LiveSpeechCapturePort {
4
+ postMessage(value: unknown): void;
5
+ addEventListener(type: 'message', listener: (event: MessageEvent) => void): void;
6
+ removeEventListener(type: 'message', listener: (event: MessageEvent) => void): void;
7
+ start(): void;
8
+ close(): void;
9
+ }
10
+ /**
11
+ * Instantiate INSIDE a dedicated detector worker, not in the UI or AudioWorklet.
12
+ * Native capture crosses a direct MessageChannel; only metadata goes to the UI.
13
+ * Neither this bridge nor its VAD results authorize a paid provider generation.
14
+ */
15
+ export declare class LiveSpeechCaptureBridge {
16
+ private readonly capturePort;
17
+ private readonly options;
18
+ private readonly detector;
19
+ private epoch;
20
+ private sequence;
21
+ private waiting;
22
+ private closed;
23
+ constructor(capturePort: LiveSpeechCapturePort, createModel: () => Promise<LocalSpeechModel>, options: LiveSpeechDetectorOptions);
24
+ get status(): 'starting' | 'ready' | 'unhealthy' | 'closed';
25
+ /** Owner must wait for onReady(epoch) before connecting/resuming that capture epoch. */
26
+ reset(epoch: number): boolean;
27
+ close(): void;
28
+ private receive;
29
+ private acknowledge;
30
+ private failCapture;
31
+ private stopCapture;
32
+ }
@@ -0,0 +1,136 @@
1
+ import { LiveSpeechDetector } from './liveSpeechDetector.js';
2
+ import { LiveSpeechWorkerEngine, isLiveSpeechEpoch } from './liveSpeechWorker.js';
3
+ /** In-worker asynchronous model transport. It creates no second worker. */
4
+ class ModelPort {
5
+ constructor(createModel) {
6
+ this.listeners = new Map();
7
+ this.closed = false;
8
+ this.engine = new LiveSpeechWorkerEngine(createModel, reply => {
9
+ queueMicrotask(() => {
10
+ if (!this.closed)
11
+ this.listeners.get('message')?.({ data: reply });
12
+ });
13
+ });
14
+ }
15
+ postMessage(message) {
16
+ if (!this.closed)
17
+ this.engine.receive(message);
18
+ }
19
+ addEventListener(type, listener) { this.listeners.set(type, listener); }
20
+ removeEventListener(type) { this.listeners.delete(type); }
21
+ terminate() {
22
+ this.closed = true;
23
+ this.listeners.clear();
24
+ this.engine.receive({ type: 'close' });
25
+ }
26
+ }
27
+ /**
28
+ * Instantiate INSIDE a dedicated detector worker, not in the UI or AudioWorklet.
29
+ * Native capture crosses a direct MessageChannel; only metadata goes to the UI.
30
+ * Neither this bridge nor its VAD results authorize a paid provider generation.
31
+ */
32
+ export class LiveSpeechCaptureBridge {
33
+ constructor(capturePort, createModel, options) {
34
+ this.capturePort = capturePort;
35
+ this.options = options;
36
+ this.sequence = 0;
37
+ this.waiting = null;
38
+ this.closed = false;
39
+ this.receive = (event) => {
40
+ if (this.closed)
41
+ return;
42
+ const raw = event.data;
43
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
44
+ this.failCapture();
45
+ return;
46
+ }
47
+ const message = raw;
48
+ const pcm = message.pcm;
49
+ if (isLiveSpeechEpoch(message.captureEpoch) && message.captureEpoch < this.epoch) {
50
+ if (pcm instanceof Float32Array)
51
+ pcm.fill(0);
52
+ return;
53
+ }
54
+ if (message.type !== 'capture' || message.captureEpoch !== this.epoch || this.detector.status !== 'ready' ||
55
+ message.sequence !== this.sequence || this.waiting !== null ||
56
+ !isLiveSpeechEpoch(message.sourceSampleOffset) || !(pcm instanceof Float32Array) ||
57
+ pcm.length !== Math.ceil(this.options.sampleRate * 0.032)) {
58
+ if (pcm instanceof Float32Array)
59
+ pcm.fill(0);
60
+ this.failCapture();
61
+ return;
62
+ }
63
+ this.waiting = this.sequence++;
64
+ const accepted = this.detector.push(pcm, this.epoch, message.sourceSampleOffset);
65
+ pcm.fill(0); // Only the transferred detector copy, never the source FIFO.
66
+ if (!accepted) {
67
+ if (!this.closed)
68
+ this.failCapture();
69
+ return;
70
+ }
71
+ if (this.detector.pendingFrames === 0)
72
+ this.acknowledge();
73
+ };
74
+ this.epoch = options.epoch;
75
+ this.detector = new LiveSpeechDetector(new ModelPort(createModel), {
76
+ ...options,
77
+ onReady: epoch => options.onReady?.(epoch),
78
+ onResult: result => {
79
+ options.onResult(result);
80
+ if (!this.closed && this.detector.pendingFrames === 0)
81
+ this.acknowledge();
82
+ },
83
+ onUnhealthy: reason => {
84
+ this.stopCapture();
85
+ options.onUnhealthy(reason);
86
+ },
87
+ });
88
+ capturePort.addEventListener('message', this.receive);
89
+ capturePort.start();
90
+ }
91
+ get status() { return this.detector.status; }
92
+ /** Owner must wait for onReady(epoch) before connecting/resuming that capture epoch. */
93
+ reset(epoch) {
94
+ if (this.closed || !isLiveSpeechEpoch(epoch) || epoch <= this.epoch)
95
+ return false;
96
+ this.epoch = epoch;
97
+ this.sequence = 0;
98
+ this.waiting = null;
99
+ return this.detector.reset(epoch);
100
+ }
101
+ close() {
102
+ this.detector.close();
103
+ this.stopCapture();
104
+ }
105
+ acknowledge() {
106
+ if (this.waiting === null || this.closed)
107
+ return;
108
+ const sequence = this.waiting;
109
+ this.waiting = null;
110
+ try {
111
+ this.capturePort.postMessage({ type: 'detector_ack', captureEpoch: this.epoch, sequence });
112
+ }
113
+ catch {
114
+ this.failCapture();
115
+ }
116
+ }
117
+ failCapture() {
118
+ if (this.closed)
119
+ return;
120
+ this.detector.close();
121
+ this.stopCapture();
122
+ this.options.onUnhealthy('capture');
123
+ }
124
+ stopCapture() {
125
+ if (this.closed)
126
+ return;
127
+ this.closed = true;
128
+ this.waiting = null;
129
+ this.capturePort.removeEventListener('message', this.receive);
130
+ try {
131
+ this.capturePort.postMessage({ type: 'close', captureEpoch: this.epoch });
132
+ }
133
+ catch { /* Peer may have stopped. */ }
134
+ this.capturePort.close();
135
+ }
136
+ }
@@ -0,0 +1,62 @@
1
+ import { type LiveSpeechRequest } from './liveSpeechWorker.js';
2
+ export interface LiveSpeechPort {
3
+ postMessage(message: LiveSpeechRequest, transfer?: Transferable[]): void;
4
+ addEventListener(type: 'message' | 'error', listener: (event: MessageEvent) => void): void;
5
+ removeEventListener(type: 'message' | 'error', listener: (event: MessageEvent) => void): void;
6
+ terminate(): void;
7
+ }
8
+ export type LiveSpeechFailure = 'protocol' | 'model' | 'timeout' | 'backpressure' | 'capture' | 'clock' | 'worker';
9
+ export interface LiveSpeechResult {
10
+ epoch: number;
11
+ sequence: number;
12
+ sourceEnd: number;
13
+ probability: number;
14
+ }
15
+ export interface LiveSpeechDetectorOptions {
16
+ sampleRate: number;
17
+ epoch: number;
18
+ now: () => number;
19
+ schedule: (callback: () => void, milliseconds: number) => unknown;
20
+ cancel: (timer: unknown) => void;
21
+ onResult: (result: LiveSpeechResult) => void;
22
+ onUnhealthy: (reason: LiveSpeechFailure) => void;
23
+ onReady?: (epoch: number) => void;
24
+ startupTimeoutMs?: number;
25
+ frameTimeoutMs?: number;
26
+ }
27
+ /** A detector client, not an automatic-wake authority. No audio-device APIs. */
28
+ export declare class LiveSpeechDetector {
29
+ private readonly port;
30
+ private readonly options;
31
+ private resampler;
32
+ private epoch;
33
+ private phase;
34
+ private sequence;
35
+ private lastCompleted;
36
+ private readonly queued;
37
+ private pending;
38
+ private timer;
39
+ private timerEpoch;
40
+ private deadline;
41
+ private lastNow;
42
+ private readonly startupTimeout;
43
+ private readonly frameTimeout;
44
+ constructor(port: LiveSpeechPort, options: LiveSpeechDetectorOptions);
45
+ get status(): 'starting' | 'ready' | 'unhealthy' | 'closed';
46
+ get pendingFrames(): number;
47
+ get pcmBytes(): number;
48
+ /** First accepted offset establishes the epoch's origin. Input is never transferred or changed. */
49
+ push(input: Float32Array, epoch: number, sourceStart: number): boolean;
50
+ reset(epoch: number): boolean;
51
+ close(): void;
52
+ private message;
53
+ private error;
54
+ private dispatch;
55
+ private send;
56
+ private checkClock;
57
+ private arm;
58
+ private cancelTimer;
59
+ private erase;
60
+ private stop;
61
+ private fail;
62
+ }
@@ -0,0 +1,202 @@
1
+ import { LiveSpeechResampler } from './liveSpeechResampler.js';
2
+ import { isLiveSpeechEpoch } from './liveSpeechWorker.js';
3
+ /** A detector client, not an automatic-wake authority. No audio-device APIs. */
4
+ export class LiveSpeechDetector {
5
+ constructor(port, options) {
6
+ this.port = port;
7
+ this.options = options;
8
+ this.phase = 'starting';
9
+ this.sequence = 0;
10
+ this.lastCompleted = -1;
11
+ this.queued = [];
12
+ this.pending = null;
13
+ this.timer = null;
14
+ this.timerEpoch = 0;
15
+ this.deadline = Infinity;
16
+ this.message = (event) => {
17
+ if (this.phase === 'closed' || this.phase === 'unhealthy')
18
+ return;
19
+ if (!this.checkClock())
20
+ return;
21
+ if (this.lastNow >= this.deadline) {
22
+ this.fail('timeout');
23
+ return;
24
+ }
25
+ const raw = event.data;
26
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
27
+ this.fail('protocol');
28
+ return;
29
+ }
30
+ const data = raw;
31
+ if (data.type === 'unhealthy') {
32
+ this.fail(data.reason === 'model' ? 'model' : 'protocol');
33
+ return;
34
+ }
35
+ if (!isLiveSpeechEpoch(data.epoch)) {
36
+ this.fail('protocol');
37
+ return;
38
+ }
39
+ if (data.epoch < this.epoch)
40
+ return;
41
+ if (data.epoch !== this.epoch) {
42
+ this.fail('protocol');
43
+ return;
44
+ }
45
+ if (data.type === 'ready') {
46
+ if (this.phase === 'ready')
47
+ return;
48
+ this.cancelTimer();
49
+ this.phase = 'ready';
50
+ this.options.onReady?.(this.epoch);
51
+ return;
52
+ }
53
+ if (data.type !== 'result' || this.phase !== 'ready' || !isLiveSpeechEpoch(data.sequence)) {
54
+ this.fail('protocol');
55
+ return;
56
+ }
57
+ if (data.sequence <= this.lastCompleted)
58
+ return;
59
+ if (!this.pending || data.sequence !== this.pending.sequence || data.sourceEnd !== this.pending.sourceEnd ||
60
+ typeof data.probability !== 'number' || !Number.isFinite(data.probability) || data.probability < 0 || data.probability > 1) {
61
+ this.fail('protocol');
62
+ return;
63
+ }
64
+ const result = { epoch: this.epoch, sequence: data.sequence, sourceEnd: this.pending.sourceEnd, probability: data.probability };
65
+ this.lastCompleted = data.sequence;
66
+ this.pending = null;
67
+ this.cancelTimer();
68
+ this.options.onResult(result);
69
+ if (this.phase !== 'ready')
70
+ return;
71
+ const next = this.queued.shift();
72
+ if (next)
73
+ this.dispatch(next);
74
+ };
75
+ this.error = () => { this.fail('worker'); };
76
+ this.epoch = options.epoch;
77
+ this.lastNow = options.now();
78
+ this.startupTimeout = options.startupTimeoutMs ?? 10000;
79
+ this.frameTimeout = options.frameTimeoutMs ?? 128;
80
+ if (!isLiveSpeechEpoch(this.epoch) || !Number.isFinite(this.lastNow) || this.lastNow < 0 ||
81
+ !Number.isFinite(this.startupTimeout) || this.startupTimeout < 100 || this.startupTimeout > 30000 ||
82
+ !Number.isFinite(this.frameTimeout) || this.frameTimeout < 32 || this.frameTimeout > 1000) {
83
+ throw new Error('Invalid detector configuration.');
84
+ }
85
+ this.resampler = new LiveSpeechResampler(options.sampleRate);
86
+ port.addEventListener('message', this.message);
87
+ port.addEventListener('error', this.error);
88
+ this.arm(this.startupTimeout);
89
+ this.send({ type: 'init', epoch: this.epoch });
90
+ }
91
+ get status() { return this.phase; }
92
+ get pendingFrames() { return this.queued.length + (this.pending ? 1 : 0); }
93
+ get pcmBytes() { return this.resampler.pcmBytes + this.queued.length * 512 * 4; }
94
+ /** First accepted offset establishes the epoch's origin. Input is never transferred or changed. */
95
+ push(input, epoch, sourceStart) {
96
+ if (epoch !== this.epoch || this.phase !== 'ready')
97
+ return false;
98
+ if (!this.checkClock())
99
+ return false;
100
+ const accepted = this.resampler.push(input, sourceStart, frame => {
101
+ if (this.pending) {
102
+ if (this.queued.length === 2)
103
+ return this.fail('backpressure');
104
+ this.queued.push(frame);
105
+ }
106
+ else
107
+ this.dispatch(frame);
108
+ return this.phase === 'ready';
109
+ });
110
+ if (!accepted && this.phase === 'ready')
111
+ return this.fail('capture');
112
+ return accepted;
113
+ }
114
+ reset(epoch) {
115
+ if (this.phase === 'closed' || this.phase === 'unhealthy' || !isLiveSpeechEpoch(epoch) || epoch <= this.epoch)
116
+ return false;
117
+ if (!this.checkClock())
118
+ return false;
119
+ this.erase();
120
+ this.resampler = new LiveSpeechResampler(this.options.sampleRate);
121
+ this.epoch = epoch;
122
+ this.sequence = 0;
123
+ this.lastCompleted = -1;
124
+ this.phase = 'starting';
125
+ this.arm(this.startupTimeout);
126
+ this.send({ type: 'reset', epoch });
127
+ return this.phase === 'starting';
128
+ }
129
+ close() {
130
+ if (this.phase === 'closed')
131
+ return;
132
+ const wasFailed = this.phase === 'unhealthy';
133
+ this.phase = 'closed';
134
+ if (!wasFailed)
135
+ this.stop();
136
+ }
137
+ dispatch(frame) {
138
+ const sequence = this.sequence++;
139
+ this.pending = { sequence, sourceEnd: frame.sourceEnd };
140
+ this.arm(this.frameTimeout);
141
+ this.send({ type: 'frame', epoch: this.epoch, sequence, sourceEnd: frame.sourceEnd, samples: frame.samples.buffer }, [frame.samples.buffer]);
142
+ }
143
+ send(message, transfer) {
144
+ try {
145
+ this.port.postMessage(message, transfer);
146
+ }
147
+ catch {
148
+ this.fail('worker');
149
+ }
150
+ }
151
+ checkClock() {
152
+ const now = this.options.now();
153
+ if (!Number.isFinite(now) || now < this.lastNow)
154
+ return this.fail('clock');
155
+ this.lastNow = now;
156
+ if (now >= this.deadline)
157
+ return this.fail('timeout');
158
+ return true;
159
+ }
160
+ arm(milliseconds) {
161
+ this.cancelTimer();
162
+ this.deadline = this.lastNow + milliseconds;
163
+ const epoch = this.timerEpoch;
164
+ this.timer = this.options.schedule(() => {
165
+ if (epoch === this.timerEpoch)
166
+ this.fail('timeout');
167
+ }, milliseconds);
168
+ }
169
+ cancelTimer() {
170
+ this.timerEpoch++;
171
+ if (this.timer !== null)
172
+ this.options.cancel(this.timer);
173
+ this.timer = null;
174
+ this.deadline = Infinity;
175
+ }
176
+ erase() {
177
+ this.cancelTimer();
178
+ this.resampler.close();
179
+ for (const frame of this.queued)
180
+ frame.samples.fill(0);
181
+ this.queued.length = 0;
182
+ this.pending = null;
183
+ }
184
+ stop() {
185
+ this.erase();
186
+ this.port.removeEventListener('message', this.message);
187
+ this.port.removeEventListener('error', this.error);
188
+ try {
189
+ this.port.postMessage({ type: 'close' });
190
+ }
191
+ catch { /* Worker may already be gone. */ }
192
+ this.port.terminate();
193
+ }
194
+ fail(reason) {
195
+ if (this.phase === 'closed' || this.phase === 'unhealthy')
196
+ return false;
197
+ this.phase = 'unhealthy';
198
+ this.stop();
199
+ this.options.onUnhealthy(reason);
200
+ return false;
201
+ }
202
+ }
@@ -0,0 +1,46 @@
1
+ export interface LocalSpeechModel {
2
+ process(frame: Float32Array): Promise<number>;
3
+ reset(): Promise<void>;
4
+ close(): Promise<void>;
5
+ }
6
+ export interface LiveSpeechTensor {
7
+ readonly type: string;
8
+ readonly dims: readonly number[];
9
+ readonly data: Float32Array | BigInt64Array;
10
+ dispose(): void;
11
+ }
12
+ export interface LiveSpeechOrt {
13
+ env: {
14
+ logLevel: string;
15
+ wasm: {
16
+ numThreads: number;
17
+ proxy: boolean;
18
+ };
19
+ };
20
+ Tensor: new (type: 'float32' | 'int64', data: Float32Array | BigInt64Array, dimensions: number[]) => LiveSpeechTensor;
21
+ InferenceSession: {
22
+ create(model: Uint8Array, options: {
23
+ executionProviders: string[];
24
+ }): Promise<{
25
+ run(inputs: Record<string, LiveSpeechTensor>): Promise<Record<string, LiveSpeechTensor>>;
26
+ release(): Promise<void>;
27
+ }>;
28
+ };
29
+ }
30
+ /** Separate worker-owned ORT instance only. This class does not load URLs or audio devices. */
31
+ export declare class SileroLiveSpeechModel implements LocalSpeechModel {
32
+ private readonly ort;
33
+ private readonly session;
34
+ private state;
35
+ private readonly rate;
36
+ private pending;
37
+ private closing;
38
+ private closed;
39
+ private constructor();
40
+ /** Caller must verify the exact model bytes and matching runtime assets first. */
41
+ static create(ort: LiveSpeechOrt, verifiedModel: Uint8Array): Promise<SileroLiveSpeechModel>;
42
+ process(frame: Float32Array): Promise<number>;
43
+ reset(): Promise<void>;
44
+ close(): Promise<void>;
45
+ private newState;
46
+ }
@@ -0,0 +1,101 @@
1
+ /** Separate worker-owned ORT instance only. This class does not load URLs or audio devices. */
2
+ export class SileroLiveSpeechModel {
3
+ constructor(ort, session) {
4
+ this.ort = ort;
5
+ this.session = session;
6
+ this.pending = null;
7
+ this.closing = null;
8
+ this.closed = false;
9
+ this.state = this.newState();
10
+ this.rate = new ort.Tensor('int64', BigInt64Array.of(16000n), [1]);
11
+ }
12
+ /** Caller must verify the exact model bytes and matching runtime assets first. */
13
+ static async create(ort, verifiedModel) {
14
+ ort.env.logLevel = 'fatal';
15
+ ort.env.wasm.numThreads = 1;
16
+ ort.env.wasm.proxy = false;
17
+ const session = await ort.InferenceSession.create(verifiedModel, { executionProviders: ['wasm'] });
18
+ let model;
19
+ try {
20
+ model = new SileroLiveSpeechModel(ort, session);
21
+ // ORT lazily prepares its first inference. Complete that work before the
22
+ // worker announces readiness and live capture's bounded ACK clock starts.
23
+ // This is synthetic silence, never microphone audio; discard its state so
24
+ // the first visitor frame still starts from the model's original state.
25
+ await model.process(new Float32Array(512));
26
+ await model.reset();
27
+ return model;
28
+ }
29
+ catch {
30
+ if (model)
31
+ await model.close();
32
+ else
33
+ await session.release();
34
+ throw new Error('Detector initialization failed.');
35
+ }
36
+ }
37
+ async process(frame) {
38
+ if (this.closed || this.pending || !(frame instanceof Float32Array) || frame.length !== 512 ||
39
+ frame.some(value => !Number.isFinite(value) || Math.abs(value) > 1)) {
40
+ throw new Error('Invalid detector frame.');
41
+ }
42
+ const input = new this.ort.Tensor('float32', frame, [1, 512]);
43
+ const previousState = this.state;
44
+ let outputs = {};
45
+ const operation = (async () => {
46
+ try {
47
+ outputs = await this.session.run({ input, state: previousState, sr: this.rate });
48
+ const probability = outputs.output;
49
+ const next = outputs.stateN;
50
+ if (!probability || probability.type !== 'float32' || !(probability.data instanceof Float32Array) ||
51
+ probability.data.length !== 1 || !Number.isFinite(probability.data[0]) ||
52
+ probability.data[0] < 0 || probability.data[0] > 1 ||
53
+ !next || next === previousState || next.type !== 'float32' || !(next.data instanceof Float32Array) ||
54
+ next.data.length !== 256 || next.dims.join(',') !== '2,1,128' || next.data.some(value => !Number.isFinite(value))) {
55
+ throw new Error('Invalid detector output.');
56
+ }
57
+ if (this.closed)
58
+ throw new Error('Detector closed.');
59
+ this.state = next;
60
+ previousState.dispose();
61
+ return probability.data[0];
62
+ }
63
+ finally {
64
+ input.dispose();
65
+ for (const tensor of new Set(Object.values(outputs))) {
66
+ if (tensor !== this.state && tensor !== this.rate && tensor !== input && tensor !== previousState)
67
+ tensor.dispose();
68
+ }
69
+ }
70
+ })();
71
+ this.pending = operation;
72
+ try {
73
+ return await operation;
74
+ }
75
+ finally {
76
+ this.pending = null;
77
+ }
78
+ }
79
+ async reset() {
80
+ if (this.closed || this.pending)
81
+ throw new Error('Detector is unavailable.');
82
+ const next = this.newState();
83
+ this.state.dispose();
84
+ this.state = next;
85
+ }
86
+ close() {
87
+ if (this.closing)
88
+ return this.closing;
89
+ this.closed = true;
90
+ this.closing = (async () => {
91
+ await this.pending?.catch(() => undefined);
92
+ this.state.dispose();
93
+ this.rate.dispose();
94
+ await this.session.release();
95
+ })();
96
+ return this.closing;
97
+ }
98
+ newState() {
99
+ return new this.ort.Tensor('float32', new Float32Array(256), [2, 1, 128]);
100
+ }
101
+ }
@@ -0,0 +1,22 @@
1
+ export interface LiveSpeechFrame {
2
+ samples: Float32Array;
3
+ /** Exclusive original-rate sample offset available when this frame completed. */
4
+ sourceEnd: number;
5
+ }
6
+ /** Detector-only causal windowed-sinc resampler. Never modifies the source PCM. */
7
+ export declare class LiveSpeechResampler {
8
+ readonly sampleRate: number;
9
+ private readonly coefficients;
10
+ private readonly history;
11
+ private frame;
12
+ private frameSize;
13
+ private inputCount;
14
+ private sourceBase;
15
+ private outputCount;
16
+ private closed;
17
+ constructor(sampleRate: number);
18
+ get pcmBytes(): number;
19
+ push(input: Float32Array, sourceStart: number, emit: (frame: LiveSpeechFrame) => boolean): boolean;
20
+ close(): void;
21
+ private fail;
22
+ }