@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.
- package/cjs/AiLiveAssist.d.ts +76 -11
- package/cjs/AiLiveAssist.js +1771 -298
- package/cjs/client.d.ts +19 -0
- package/cjs/client.js +51 -0
- package/cjs/context.d.ts +23 -1
- package/cjs/context.js +713 -126
- package/cjs/control.d.ts +2 -0
- package/cjs/control.js +42 -3
- package/cjs/ctaOffer.d.ts +11 -0
- package/cjs/ctaOffer.js +185 -0
- package/cjs/dialogFocusScope.js +1 -1
- package/cjs/guideAdmission.d.ts +46 -0
- package/cjs/guideAdmission.js +397 -0
- package/cjs/guideHost.d.ts +47 -0
- package/cjs/guideHost.js +48 -0
- package/cjs/guideInstallation.d.ts +12 -0
- package/cjs/guideInstallation.js +60 -0
- package/cjs/guideInstaller.d.ts +4 -0
- package/cjs/guideInstaller.js +116 -0
- package/cjs/guideMount.d.ts +22 -0
- package/cjs/guideMount.js +84 -0
- package/cjs/guideRecording.d.ts +64 -0
- package/cjs/guideRecording.js +176 -0
- package/cjs/guideRecordingSession.d.ts +35 -0
- package/cjs/guideRecordingSession.js +167 -0
- package/cjs/guideSubjectStore.d.ts +32 -0
- package/cjs/guideSubjectStore.js +105 -0
- package/cjs/guideTypes.d.ts +117 -0
- package/cjs/guideTypes.js +15 -0
- package/cjs/index.d.ts +6 -0
- package/cjs/index.js +25 -11
- package/cjs/liveAudioProcessor.d.ts +92 -0
- package/cjs/liveAudioProcessor.js +269 -0
- package/cjs/liveAudioQueue.d.ts +40 -0
- package/cjs/liveAudioQueue.js +130 -0
- package/cjs/liveAudioWorklet.d.ts +1 -0
- package/cjs/liveAudioWorklet.js +63 -0
- package/cjs/liveBrowserDetector.d.ts +16 -0
- package/cjs/liveBrowserDetector.js +75 -0
- package/cjs/liveBrowserDetectorWorker.d.ts +1 -0
- package/cjs/liveBrowserDetectorWorker.js +72 -0
- package/cjs/liveControlChannel.d.ts +176 -0
- package/cjs/liveControlChannel.js +829 -0
- package/cjs/liveDuplexMedia.d.ts +204 -0
- package/cjs/liveDuplexMedia.js +740 -0
- package/cjs/liveGuideAdmission.d.ts +21 -0
- package/cjs/liveGuideAdmission.js +94 -0
- package/cjs/liveGuideWidget.d.ts +68 -0
- package/cjs/liveGuideWidget.js +451 -0
- package/cjs/liveSpeechAssets.d.ts +36 -0
- package/cjs/liveSpeechAssets.js +39 -0
- package/cjs/liveSpeechCaptureBridge.d.ts +32 -0
- package/cjs/liveSpeechCaptureBridge.js +140 -0
- package/cjs/liveSpeechDetector.d.ts +62 -0
- package/cjs/liveSpeechDetector.js +206 -0
- package/cjs/liveSpeechModel.d.ts +46 -0
- package/cjs/liveSpeechModel.js +105 -0
- package/cjs/liveSpeechResampler.d.ts +22 -0
- package/cjs/liveSpeechResampler.js +93 -0
- package/cjs/liveSpeechWorker.d.ts +56 -0
- package/cjs/liveSpeechWorker.js +143 -0
- package/cjs/locales/resources.js +568 -8
- package/cjs/media.d.ts +11 -0
- package/cjs/media.js +78 -9
- package/cjs/messages.js +22 -8
- package/cjs/pageAssistancePreference.d.ts +15 -3
- package/cjs/pageAssistancePreference.js +46 -7
- package/cjs/preview.d.ts +4 -1
- package/cjs/preview.js +42 -1
- package/cjs/recording.d.ts +12 -0
- package/cjs/recording.js +150 -0
- package/cjs/standalone.d.ts +6 -0
- package/cjs/standalone.js +12 -0
- package/cjs/types.d.ts +31 -5
- package/cjs/types.js +14 -0
- package/cjs/ui.d.ts +128 -4
- package/cjs/ui.js +1918 -276
- package/cjs/version.d.ts +1 -1
- package/cjs/version.js +1 -1
- package/cjs/voicePresenceUi.d.ts +52 -4
- package/cjs/voicePresenceUi.js +659 -57
- package/lib/AiLiveAssist.d.ts +76 -11
- package/lib/AiLiveAssist.js +1772 -299
- package/lib/client.d.ts +19 -0
- package/lib/client.js +51 -0
- package/lib/context.d.ts +23 -1
- package/lib/context.js +713 -126
- package/lib/control.d.ts +2 -0
- package/lib/control.js +42 -3
- package/lib/ctaOffer.d.ts +11 -0
- package/lib/ctaOffer.js +179 -0
- package/lib/dialogFocusScope.js +1 -1
- package/lib/guideAdmission.d.ts +46 -0
- package/lib/guideAdmission.js +392 -0
- package/lib/guideHost.d.ts +47 -0
- package/lib/guideHost.js +42 -0
- package/lib/guideInstallation.d.ts +12 -0
- package/lib/guideInstallation.js +56 -0
- package/lib/guideInstaller.d.ts +4 -0
- package/lib/guideInstaller.js +113 -0
- package/lib/guideMount.d.ts +22 -0
- package/lib/guideMount.js +77 -0
- package/lib/guideRecording.d.ts +64 -0
- package/lib/guideRecording.js +172 -0
- package/lib/guideRecordingSession.d.ts +35 -0
- package/lib/guideRecordingSession.js +163 -0
- package/lib/guideSubjectStore.d.ts +32 -0
- package/lib/guideSubjectStore.js +101 -0
- package/lib/guideTypes.d.ts +117 -0
- package/lib/guideTypes.js +11 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +21 -10
- package/lib/liveAudioProcessor.d.ts +92 -0
- package/lib/liveAudioProcessor.js +265 -0
- package/lib/liveAudioQueue.d.ts +40 -0
- package/lib/liveAudioQueue.js +126 -0
- package/lib/liveAudioWorklet.d.ts +1 -0
- package/lib/liveAudioWorklet.js +61 -0
- package/lib/liveBrowserDetector.d.ts +16 -0
- package/lib/liveBrowserDetector.js +71 -0
- package/lib/liveBrowserDetectorWorker.d.ts +1 -0
- package/lib/liveBrowserDetectorWorker.js +70 -0
- package/lib/liveControlChannel.d.ts +176 -0
- package/lib/liveControlChannel.js +825 -0
- package/lib/liveDuplexMedia.d.ts +204 -0
- package/lib/liveDuplexMedia.js +736 -0
- package/lib/liveGuideAdmission.d.ts +21 -0
- package/lib/liveGuideAdmission.js +90 -0
- package/lib/liveGuideWidget.d.ts +68 -0
- package/lib/liveGuideWidget.js +447 -0
- package/lib/liveSpeechAssets.d.ts +36 -0
- package/lib/liveSpeechAssets.js +35 -0
- package/lib/liveSpeechCaptureBridge.d.ts +32 -0
- package/lib/liveSpeechCaptureBridge.js +136 -0
- package/lib/liveSpeechDetector.d.ts +62 -0
- package/lib/liveSpeechDetector.js +202 -0
- package/lib/liveSpeechModel.d.ts +46 -0
- package/lib/liveSpeechModel.js +101 -0
- package/lib/liveSpeechResampler.d.ts +22 -0
- package/lib/liveSpeechResampler.js +89 -0
- package/lib/liveSpeechWorker.d.ts +56 -0
- package/lib/liveSpeechWorker.js +137 -0
- package/lib/locales/resources.js +568 -8
- package/lib/media.d.ts +11 -0
- package/lib/media.js +77 -8
- package/lib/messages.js +22 -8
- package/lib/pageAssistancePreference.d.ts +15 -3
- package/lib/pageAssistancePreference.js +46 -7
- package/lib/preview.d.ts +4 -1
- package/lib/preview.js +42 -1
- package/lib/recording.d.ts +12 -0
- package/lib/recording.js +146 -0
- package/lib/standalone.d.ts +6 -0
- package/lib/standalone.js +6 -0
- package/lib/types.d.ts +31 -5
- package/lib/types.js +14 -0
- package/lib/ui.d.ts +128 -4
- package/lib/ui.js +1919 -277
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/lib/voicePresenceUi.d.ts +52 -4
- package/lib/voicePresenceUi.js +659 -57
- package/package.json +16 -1
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LiveAudioProcessor = void 0;
|
|
4
|
+
const liveAudioQueue_js_1 = require("./liveAudioQueue.js");
|
|
5
|
+
/** Worklet-safe engine: no network, timers, speech inference or microphone
|
|
6
|
+
* ownership. Detection gets a bounded copy; original-rate audio remains intact.
|
|
7
|
+
* The outside owner alone may begin/activate/reset from current authority proofs.
|
|
8
|
+
*/
|
|
9
|
+
class LiveAudioProcessor {
|
|
10
|
+
constructor(sampleRate, epoch, emit) {
|
|
11
|
+
this.sampleRate = sampleRate;
|
|
12
|
+
this.epoch = epoch;
|
|
13
|
+
this.emit = emit;
|
|
14
|
+
this.phase = 'local';
|
|
15
|
+
this.deadlineFrame = null;
|
|
16
|
+
this.retiringOperationId = null;
|
|
17
|
+
this.completedRetirementId = null;
|
|
18
|
+
this.historyEnd = 0;
|
|
19
|
+
this.historySize = 0;
|
|
20
|
+
this.expectedFrame = null;
|
|
21
|
+
this.heartbeatFrame = null;
|
|
22
|
+
this.detectorSize = 0;
|
|
23
|
+
this.detectorStart = 0;
|
|
24
|
+
this.detectorSequence = 0;
|
|
25
|
+
this.captureEstablished = false;
|
|
26
|
+
this.captureStart = 0;
|
|
27
|
+
this.detectorProgressed = false;
|
|
28
|
+
this.detectorInFlight = null;
|
|
29
|
+
this.detectorPending = [];
|
|
30
|
+
this.queue = new liveAudioQueue_js_1.LiveAudioQueue(sampleRate, epoch);
|
|
31
|
+
this.history = new Float32Array(this.queue.prerollFrames);
|
|
32
|
+
this.detectorBuffer = new Float32Array(Math.ceil(sampleRate * 0.032));
|
|
33
|
+
this.emit({ type: 'ready', captureEpoch: epoch, sampleRate });
|
|
34
|
+
}
|
|
35
|
+
command(value, currentFrame) {
|
|
36
|
+
if (!value || typeof value !== 'object' || Array.isArray(value) || !Number.isSafeInteger(currentFrame) || currentFrame < 0)
|
|
37
|
+
return false;
|
|
38
|
+
const command = value;
|
|
39
|
+
if (!Number.isSafeInteger(command.captureEpoch) || typeof command.captureEpoch !== 'number' || command.captureEpoch < 0)
|
|
40
|
+
return false;
|
|
41
|
+
if (command.type === 'reset') {
|
|
42
|
+
if (command.captureEpoch <= this.epoch)
|
|
43
|
+
return false;
|
|
44
|
+
this.erase();
|
|
45
|
+
this.epoch = command.captureEpoch;
|
|
46
|
+
this.queue = new liveAudioQueue_js_1.LiveAudioQueue(this.sampleRate, this.epoch);
|
|
47
|
+
this.phase = 'local';
|
|
48
|
+
this.history = new Float32Array(this.queue.prerollFrames);
|
|
49
|
+
this.detectorBuffer = new Float32Array(Math.ceil(this.sampleRate * 0.032));
|
|
50
|
+
this.emit({ type: 'ready', captureEpoch: this.epoch, sampleRate: this.sampleRate });
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
if (command.captureEpoch !== this.epoch)
|
|
54
|
+
return false;
|
|
55
|
+
if (command.type === 'close') {
|
|
56
|
+
this.erase();
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
if (this.phase === 'closed')
|
|
60
|
+
return false;
|
|
61
|
+
if (command.type === 'detector_ack') {
|
|
62
|
+
if (this.detectorInFlight === null || command.sequence !== this.detectorInFlight)
|
|
63
|
+
return false;
|
|
64
|
+
this.detectorInFlight = null;
|
|
65
|
+
if (!this.detectorProgressed) {
|
|
66
|
+
this.detectorProgressed = true;
|
|
67
|
+
this.emit({ type: 'capture_ready', captureEpoch: this.epoch, sourceStart: this.captureStart });
|
|
68
|
+
}
|
|
69
|
+
const next = this.detectorPending.shift();
|
|
70
|
+
if (next)
|
|
71
|
+
this.sendDetector(next);
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
if (typeof command.operationId !== 'string')
|
|
75
|
+
return false;
|
|
76
|
+
if (command.type === 'begin_retirement') {
|
|
77
|
+
if (this.retiringOperationId !== command.operationId) {
|
|
78
|
+
if (this.retiringOperationId !== null)
|
|
79
|
+
return false;
|
|
80
|
+
const prefix = this.historySnapshot();
|
|
81
|
+
const retired = this.queue.retire(command.operationId, this.epoch, prefix);
|
|
82
|
+
prefix.fill(0);
|
|
83
|
+
if (!retired)
|
|
84
|
+
return false;
|
|
85
|
+
this.retiringOperationId = command.operationId;
|
|
86
|
+
this.phase = 'local';
|
|
87
|
+
this.deadlineFrame = null;
|
|
88
|
+
}
|
|
89
|
+
this.emit({ type: 'retirement_buffering', captureEpoch: this.epoch, operationId: command.operationId });
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
if (command.type === 'complete_retirement') {
|
|
93
|
+
if (this.retiringOperationId === command.operationId) {
|
|
94
|
+
this.completedRetirementId = command.operationId;
|
|
95
|
+
this.retiringOperationId = null;
|
|
96
|
+
}
|
|
97
|
+
else if (this.retiringOperationId !== null || this.completedRetirementId !== command.operationId)
|
|
98
|
+
return false;
|
|
99
|
+
// The owner sends this only after durable terminal proof, drained playback
|
|
100
|
+
// and closing the old peer. It does not reset capture or a pending wake.
|
|
101
|
+
this.emit({ type: 'retirement_complete', captureEpoch: this.epoch, operationId: command.operationId });
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
if (command.type === 'begin_wake' && this.detectorProgressed && command.operationId !== this.retiringOperationId &&
|
|
105
|
+
command.operationId !== this.completedRetirementId && this.queue.beginWake(command.operationId, this.epoch)) {
|
|
106
|
+
this.phase = 'waking';
|
|
107
|
+
this.deadlineFrame = currentFrame + this.sampleRate * 6;
|
|
108
|
+
this.emit({ type: 'wake_buffering', captureEpoch: this.epoch, operationId: command.operationId });
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
if (command.type === 'activate') {
|
|
112
|
+
if (this.deadlineFrame !== null && currentFrame >= this.deadlineFrame) {
|
|
113
|
+
this.fault('wake_deadline');
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
if (this.retiringOperationId !== null)
|
|
117
|
+
return false;
|
|
118
|
+
if (!this.queue.activate(command.operationId, this.epoch))
|
|
119
|
+
return false;
|
|
120
|
+
this.phase = 'active';
|
|
121
|
+
this.deadlineFrame = null;
|
|
122
|
+
this.emit({ type: 'active', captureEpoch: this.epoch, operationId: command.operationId });
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
/** Exactly one native-rate input/output quantum. A gap cannot become silence. */
|
|
128
|
+
process(input, output, sourceFrame) {
|
|
129
|
+
output.fill(0);
|
|
130
|
+
if (this.phase === 'closed')
|
|
131
|
+
return false;
|
|
132
|
+
try {
|
|
133
|
+
// Device graphs can render before the first capture track has PCM. This is
|
|
134
|
+
// preparation only: emit no heartbeat/detector readiness or paid start.
|
|
135
|
+
if (!input && this.expectedFrame === null && this.phase === 'local')
|
|
136
|
+
return true;
|
|
137
|
+
if (!input || input.length !== output.length || input.length === 0 || input.length > 4096 ||
|
|
138
|
+
!Number.isSafeInteger(sourceFrame) || sourceFrame < 0)
|
|
139
|
+
return this.fault('invalid_audio');
|
|
140
|
+
if (this.expectedFrame !== null && this.expectedFrame !== sourceFrame) {
|
|
141
|
+
if (this.captureEstablished || this.phase !== 'local')
|
|
142
|
+
return this.fault('capture_gap', { expectedFrame: this.expectedFrame, actualFrame: sourceFrame });
|
|
143
|
+
// Chromium can advance its context frame during initial device startup.
|
|
144
|
+
// Before the first complete detector packet, no wake is admissible: erase
|
|
145
|
+
// that partial preparation audio and require a contiguous fresh prefix.
|
|
146
|
+
this.queue.close();
|
|
147
|
+
this.queue = new liveAudioQueue_js_1.LiveAudioQueue(this.sampleRate, this.epoch);
|
|
148
|
+
this.history.fill(0);
|
|
149
|
+
this.historySize = 0;
|
|
150
|
+
this.historyEnd = 0;
|
|
151
|
+
this.detectorBuffer.fill(0);
|
|
152
|
+
this.detectorSize = 0;
|
|
153
|
+
this.heartbeatFrame = null;
|
|
154
|
+
}
|
|
155
|
+
this.expectedFrame = sourceFrame + input.length;
|
|
156
|
+
if (this.deadlineFrame !== null && sourceFrame >= this.deadlineFrame)
|
|
157
|
+
return this.fault('wake_deadline');
|
|
158
|
+
if (!this.queue.capture(input, this.epoch))
|
|
159
|
+
return this.fault(this.queue.isClosed ? 'wake_capacity' : 'invalid_audio');
|
|
160
|
+
this.remember(input);
|
|
161
|
+
if (!this.captureForDetector(input, sourceFrame))
|
|
162
|
+
return false;
|
|
163
|
+
this.queue.render(output, this.epoch);
|
|
164
|
+
if (this.heartbeatFrame === null || sourceFrame - this.heartbeatFrame >= this.sampleRate) {
|
|
165
|
+
this.heartbeatFrame = sourceFrame;
|
|
166
|
+
this.emit({ type: 'heartbeat', captureEpoch: this.epoch, sourceEnd: this.expectedFrame,
|
|
167
|
+
bufferedFrames: this.queue.bufferedFrames, active: this.phase === 'active' });
|
|
168
|
+
}
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
output.fill(0);
|
|
173
|
+
return this.fault('processor_failure');
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
captureForDetector(input, sourceFrame) {
|
|
177
|
+
let offset = 0;
|
|
178
|
+
while (offset < input.length) {
|
|
179
|
+
if (this.detectorSize === 0)
|
|
180
|
+
this.detectorStart = sourceFrame + offset;
|
|
181
|
+
const count = Math.min(this.detectorBuffer.length - this.detectorSize, input.length - offset);
|
|
182
|
+
this.detectorBuffer.set(input.subarray(offset, offset + count), this.detectorSize);
|
|
183
|
+
offset += count;
|
|
184
|
+
this.detectorSize += count;
|
|
185
|
+
if (this.detectorSize !== this.detectorBuffer.length)
|
|
186
|
+
continue;
|
|
187
|
+
const event = { type: 'capture', captureEpoch: this.epoch,
|
|
188
|
+
sequence: this.detectorSequence++, sourceSampleOffset: this.detectorStart, pcm: this.detectorBuffer };
|
|
189
|
+
this.detectorBuffer = new Float32Array(event.pcm.length);
|
|
190
|
+
this.detectorSize = 0;
|
|
191
|
+
if (this.detectorInFlight === null)
|
|
192
|
+
this.sendDetector(event);
|
|
193
|
+
else if (this.detectorPending.length < 2)
|
|
194
|
+
this.detectorPending.push(event);
|
|
195
|
+
else {
|
|
196
|
+
event.pcm.fill(0);
|
|
197
|
+
return this.fault('detector_backpressure');
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
/** Keep the most recent 600ms even after it was rendered to the old stream.
|
|
203
|
+
* This fixed ring never grows and is independent of detector scheduling.
|
|
204
|
+
*/
|
|
205
|
+
remember(input) {
|
|
206
|
+
const tail = input.subarray(Math.max(0, input.length - this.history.length));
|
|
207
|
+
const first = Math.min(tail.length, this.history.length - this.historyEnd);
|
|
208
|
+
this.history.set(tail.subarray(0, first), this.historyEnd);
|
|
209
|
+
if (first < tail.length)
|
|
210
|
+
this.history.set(tail.subarray(first), 0);
|
|
211
|
+
this.historyEnd = (this.historyEnd + tail.length) % this.history.length;
|
|
212
|
+
this.historySize = Math.min(this.history.length, this.historySize + tail.length);
|
|
213
|
+
}
|
|
214
|
+
historySnapshot() {
|
|
215
|
+
const prefix = new Float32Array(this.historySize);
|
|
216
|
+
const start = (this.historyEnd - this.historySize + this.history.length) % this.history.length;
|
|
217
|
+
const first = Math.min(this.historySize, this.history.length - start);
|
|
218
|
+
prefix.set(this.history.subarray(start, start + first));
|
|
219
|
+
if (first < this.historySize)
|
|
220
|
+
prefix.set(this.history.subarray(0, this.historySize - first), first);
|
|
221
|
+
return prefix;
|
|
222
|
+
}
|
|
223
|
+
sendDetector(event) {
|
|
224
|
+
if (!this.captureEstablished) {
|
|
225
|
+
this.captureEstablished = true;
|
|
226
|
+
this.captureStart = event.sourceSampleOffset;
|
|
227
|
+
}
|
|
228
|
+
// Sending fixes the detector's source continuity, but preparation needs a
|
|
229
|
+
// matching ACK proving its first real inference completed. Keep all native
|
|
230
|
+
// preroll intact while that bounded detector copy is in flight.
|
|
231
|
+
this.detectorInFlight = event.sequence;
|
|
232
|
+
this.emit(event);
|
|
233
|
+
}
|
|
234
|
+
fault(reason, diagnostic) {
|
|
235
|
+
if (this.phase === 'closed')
|
|
236
|
+
return false;
|
|
237
|
+
this.erase();
|
|
238
|
+
try {
|
|
239
|
+
this.emit({ type: 'fault', captureEpoch: this.epoch, reason, ...diagnostic });
|
|
240
|
+
}
|
|
241
|
+
catch { /* Audio remains erased and silent. */ }
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
erase() {
|
|
245
|
+
this.queue.close();
|
|
246
|
+
this.phase = 'closed';
|
|
247
|
+
this.deadlineFrame = null;
|
|
248
|
+
this.retiringOperationId = null;
|
|
249
|
+
this.completedRetirementId = null;
|
|
250
|
+
this.history.fill(0);
|
|
251
|
+
this.history = new Float32Array(0);
|
|
252
|
+
this.historySize = 0;
|
|
253
|
+
this.historyEnd = 0;
|
|
254
|
+
this.detectorBuffer.fill(0);
|
|
255
|
+
this.detectorBuffer = new Float32Array(0);
|
|
256
|
+
this.detectorSize = 0;
|
|
257
|
+
for (const pending of this.detectorPending)
|
|
258
|
+
pending.pcm.fill(0);
|
|
259
|
+
this.detectorPending = [];
|
|
260
|
+
this.detectorInFlight = null;
|
|
261
|
+
this.detectorSequence = 0;
|
|
262
|
+
this.captureEstablished = false;
|
|
263
|
+
this.captureStart = 0;
|
|
264
|
+
this.detectorProgressed = false;
|
|
265
|
+
this.expectedFrame = null;
|
|
266
|
+
this.heartbeatFrame = null;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
exports.LiveAudioProcessor = LiveAudioProcessor;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory mono PCM queue for the Live AudioWorklet. It neither opens a
|
|
3
|
+
* microphone nor authorizes a paid connection. The lifecycle owner must confirm
|
|
4
|
+
* provider closure and drained playback before activating a new paid generation.
|
|
5
|
+
* Physical capture may continue locally during retirement in the same epoch.
|
|
6
|
+
*
|
|
7
|
+
* Capture and playout use the SAME sample rate. A render call emits at most one
|
|
8
|
+
* output block: buffered speech is never burst, accelerated or spliced away.
|
|
9
|
+
*/
|
|
10
|
+
export declare class LiveAudioQueue {
|
|
11
|
+
readonly sampleRate: number;
|
|
12
|
+
readonly captureEpoch: number;
|
|
13
|
+
readonly capacityFrames: number;
|
|
14
|
+
readonly prerollFrames: number;
|
|
15
|
+
private samples;
|
|
16
|
+
private head;
|
|
17
|
+
private size;
|
|
18
|
+
private phase;
|
|
19
|
+
private operationId;
|
|
20
|
+
constructor(sampleRate: number, captureEpoch: number);
|
|
21
|
+
get bufferedFrames(): number;
|
|
22
|
+
get bufferBytes(): number;
|
|
23
|
+
get isClosed(): boolean;
|
|
24
|
+
/** Input is post-browser echo cancellation, not a VAD-filtered sample stream. */
|
|
25
|
+
capture(input: Float32Array, epoch: number): boolean;
|
|
26
|
+
beginWake(operationId: string, epoch: number): boolean;
|
|
27
|
+
activate(operationId: string, epoch: number): boolean;
|
|
28
|
+
/** Called only after durable retirement commitment. Already-rendered audio is
|
|
29
|
+
* supplied by the processor's bounded history; the old transmission backlog
|
|
30
|
+
* must not become the new generation's prefix.
|
|
31
|
+
*/
|
|
32
|
+
retire(operationId: string, epoch: number, prefix: Float32Array): boolean;
|
|
33
|
+
/** Call exactly once per AudioWorklet output quantum, including silence. */
|
|
34
|
+
render(output: Float32Array, epoch: number): number;
|
|
35
|
+
/** End, mute, cancellation, permission loss, timeout and overflow erase audio. */
|
|
36
|
+
close(): void;
|
|
37
|
+
private fail;
|
|
38
|
+
private append;
|
|
39
|
+
private eraseHead;
|
|
40
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LiveAudioQueue = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* In-memory mono PCM queue for the Live AudioWorklet. It neither opens a
|
|
6
|
+
* microphone nor authorizes a paid connection. The lifecycle owner must confirm
|
|
7
|
+
* provider closure and drained playback before activating a new paid generation.
|
|
8
|
+
* Physical capture may continue locally during retirement in the same epoch.
|
|
9
|
+
*
|
|
10
|
+
* Capture and playout use the SAME sample rate. A render call emits at most one
|
|
11
|
+
* output block: buffered speech is never burst, accelerated or spliced away.
|
|
12
|
+
*/
|
|
13
|
+
class LiveAudioQueue {
|
|
14
|
+
constructor(sampleRate, captureEpoch) {
|
|
15
|
+
this.sampleRate = sampleRate;
|
|
16
|
+
this.captureEpoch = captureEpoch;
|
|
17
|
+
this.head = 0;
|
|
18
|
+
this.size = 0;
|
|
19
|
+
this.phase = "local";
|
|
20
|
+
this.operationId = null;
|
|
21
|
+
if (!Number.isSafeInteger(sampleRate) || sampleRate < 8000 || sampleRate > 96000 ||
|
|
22
|
+
!Number.isSafeInteger(captureEpoch) || captureEpoch < 0)
|
|
23
|
+
throw new Error("Invalid Live audio queue configuration.");
|
|
24
|
+
this.capacityFrames = sampleRate * 8;
|
|
25
|
+
this.prerollFrames = Math.ceil(sampleRate * 0.6);
|
|
26
|
+
this.samples = new Float32Array(this.capacityFrames);
|
|
27
|
+
}
|
|
28
|
+
get bufferedFrames() { return this.size; }
|
|
29
|
+
get bufferBytes() { return this.samples.byteLength; }
|
|
30
|
+
get isClosed() { return this.phase === "closed"; }
|
|
31
|
+
/** Input is post-browser echo cancellation, not a VAD-filtered sample stream. */
|
|
32
|
+
capture(input, epoch) {
|
|
33
|
+
if (epoch !== this.captureEpoch || this.phase === "closed")
|
|
34
|
+
return false;
|
|
35
|
+
if (input.length > this.capacityFrames)
|
|
36
|
+
return this.fail();
|
|
37
|
+
for (const sample of input) {
|
|
38
|
+
if (!Number.isFinite(sample))
|
|
39
|
+
return this.fail();
|
|
40
|
+
}
|
|
41
|
+
if (this.phase === "local") {
|
|
42
|
+
// Idle retains only the short prefix; no visitor audio is stored elsewhere.
|
|
43
|
+
const tail = input.subarray(Math.max(0, input.length - this.prerollFrames));
|
|
44
|
+
const discard = Math.max(0, this.size + tail.length - this.prerollFrames);
|
|
45
|
+
this.eraseHead(discard);
|
|
46
|
+
this.append(tail);
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
// A partial suffix is never treated as a successful wake on overflow.
|
|
50
|
+
if (this.size + input.length > this.capacityFrames)
|
|
51
|
+
return this.fail();
|
|
52
|
+
this.append(input);
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
beginWake(operationId, epoch) {
|
|
56
|
+
if (epoch !== this.captureEpoch || this.phase !== "local" ||
|
|
57
|
+
typeof operationId !== "string" || operationId.length < 1 || operationId.length > 256)
|
|
58
|
+
return false;
|
|
59
|
+
this.operationId = operationId;
|
|
60
|
+
this.phase = "waking";
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
activate(operationId, epoch) {
|
|
64
|
+
if (epoch !== this.captureEpoch || this.phase !== "waking" || operationId !== this.operationId)
|
|
65
|
+
return false;
|
|
66
|
+
this.phase = "active";
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
/** Called only after durable retirement commitment. Already-rendered audio is
|
|
70
|
+
* supplied by the processor's bounded history; the old transmission backlog
|
|
71
|
+
* must not become the new generation's prefix.
|
|
72
|
+
*/
|
|
73
|
+
retire(operationId, epoch, prefix) {
|
|
74
|
+
if (epoch !== this.captureEpoch || this.phase !== "active" || operationId !== this.operationId ||
|
|
75
|
+
prefix.length > this.prerollFrames)
|
|
76
|
+
return false;
|
|
77
|
+
for (const sample of prefix)
|
|
78
|
+
if (!Number.isFinite(sample))
|
|
79
|
+
return false;
|
|
80
|
+
this.samples.fill(0);
|
|
81
|
+
this.head = 0;
|
|
82
|
+
this.size = 0;
|
|
83
|
+
this.operationId = null;
|
|
84
|
+
this.phase = "local";
|
|
85
|
+
this.append(prefix);
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
/** Call exactly once per AudioWorklet output quantum, including silence. */
|
|
89
|
+
render(output, epoch) {
|
|
90
|
+
output.fill(0);
|
|
91
|
+
if (epoch !== this.captureEpoch || this.phase !== "active")
|
|
92
|
+
return 0;
|
|
93
|
+
const count = Math.min(output.length, this.size);
|
|
94
|
+
const first = Math.min(count, this.capacityFrames - this.head);
|
|
95
|
+
output.set(this.samples.subarray(this.head, this.head + first));
|
|
96
|
+
if (first < count)
|
|
97
|
+
output.set(this.samples.subarray(0, count - first), first);
|
|
98
|
+
this.eraseHead(count);
|
|
99
|
+
return count;
|
|
100
|
+
}
|
|
101
|
+
/** End, mute, cancellation, permission loss, timeout and overflow erase audio. */
|
|
102
|
+
close() {
|
|
103
|
+
if (this.phase === "closed")
|
|
104
|
+
return;
|
|
105
|
+
this.samples.fill(0);
|
|
106
|
+
this.samples = new Float32Array(0);
|
|
107
|
+
this.head = 0;
|
|
108
|
+
this.size = 0;
|
|
109
|
+
this.operationId = null;
|
|
110
|
+
this.phase = "closed";
|
|
111
|
+
}
|
|
112
|
+
fail() { this.close(); return false; }
|
|
113
|
+
append(input) {
|
|
114
|
+
const tail = (this.head + this.size) % this.capacityFrames;
|
|
115
|
+
const first = Math.min(input.length, this.capacityFrames - tail);
|
|
116
|
+
this.samples.set(input.subarray(0, first), tail);
|
|
117
|
+
if (first < input.length)
|
|
118
|
+
this.samples.set(input.subarray(first), 0);
|
|
119
|
+
this.size += input.length;
|
|
120
|
+
}
|
|
121
|
+
eraseHead(count) {
|
|
122
|
+
const first = Math.min(count, this.capacityFrames - this.head);
|
|
123
|
+
this.samples.fill(0, this.head, this.head + first);
|
|
124
|
+
if (first < count)
|
|
125
|
+
this.samples.fill(0, 0, count - first);
|
|
126
|
+
this.head = (this.head + count) % this.capacityFrames;
|
|
127
|
+
this.size -= count;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
exports.LiveAudioQueue = LiveAudioQueue;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const liveAudioProcessor_js_1 = require("./liveAudioProcessor.js");
|
|
4
|
+
/** Separate immutable worklet entry, not executed by the regular widget bundle. */
|
|
5
|
+
class OxvoLiveAudioProcessor extends AudioWorkletProcessor {
|
|
6
|
+
constructor(options) {
|
|
7
|
+
super();
|
|
8
|
+
this.detectorPort = null;
|
|
9
|
+
const raw = options?.processorOptions;
|
|
10
|
+
const epoch = raw && typeof raw === 'object' && 'captureEpoch' in raw ? raw.captureEpoch : undefined;
|
|
11
|
+
if (typeof epoch !== 'number' || !Number.isSafeInteger(epoch) || epoch < 0)
|
|
12
|
+
throw new Error('Invalid Live capture epoch.');
|
|
13
|
+
this.epoch = epoch;
|
|
14
|
+
this.engine = new liveAudioProcessor_js_1.LiveAudioProcessor(sampleRate, epoch, event => {
|
|
15
|
+
if (event.type === 'capture') {
|
|
16
|
+
if (!this.detectorPort)
|
|
17
|
+
throw new Error('Live detector is not attached.');
|
|
18
|
+
this.detectorPort.postMessage(event, [event.pcm.buffer]);
|
|
19
|
+
}
|
|
20
|
+
else
|
|
21
|
+
this.port.postMessage(event);
|
|
22
|
+
});
|
|
23
|
+
this.port.onmessage = event => {
|
|
24
|
+
const data = event.data;
|
|
25
|
+
if (data && typeof data === 'object' && 'type' in data && data.type === 'attach_detector') {
|
|
26
|
+
if (this.detectorPort || !('captureEpoch' in data) || data.captureEpoch !== this.epoch || event.ports.length !== 1)
|
|
27
|
+
return;
|
|
28
|
+
const detector = event.ports[0];
|
|
29
|
+
if (!detector)
|
|
30
|
+
return;
|
|
31
|
+
this.detectorPort = detector;
|
|
32
|
+
detector.onmessage = message => {
|
|
33
|
+
const command = message.data;
|
|
34
|
+
if (command && typeof command === 'object' && 'type' in command &&
|
|
35
|
+
(command.type === 'detector_ack' || command.type === 'close'))
|
|
36
|
+
this.engine.command(command, currentFrame);
|
|
37
|
+
};
|
|
38
|
+
detector.start();
|
|
39
|
+
this.port.postMessage({ type: 'detector_attached', captureEpoch: this.epoch });
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (this.engine.command(data, currentFrame) && data && typeof data === 'object' && 'type' in data) {
|
|
43
|
+
if (data.type === 'reset' && 'captureEpoch' in data && typeof data.captureEpoch === 'number')
|
|
44
|
+
this.epoch = data.captureEpoch;
|
|
45
|
+
if (data.type === 'close') {
|
|
46
|
+
this.detectorPort?.close();
|
|
47
|
+
this.detectorPort = null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
process(inputs, outputs) {
|
|
53
|
+
const output = outputs[0]?.[0];
|
|
54
|
+
if (!output)
|
|
55
|
+
return false;
|
|
56
|
+
// The node is configured with channelCount=1 and channelCountMode=explicit.
|
|
57
|
+
for (const bus of outputs)
|
|
58
|
+
for (const channel of bus)
|
|
59
|
+
channel.fill(0);
|
|
60
|
+
return this.engine.process(inputs[0]?.[0], output, currentFrame);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
registerProcessor('oxvo-live-audio-v1', OxvoLiveAudioProcessor);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { LiveMediaDetector } from './liveDuplexMedia.js';
|
|
2
|
+
import type { LiveSpeechResult } from './liveSpeechDetector.js';
|
|
3
|
+
export type LiveBrowserAssets = {
|
|
4
|
+
baseUrl: string;
|
|
5
|
+
detectorWorkerUrl: string;
|
|
6
|
+
audioWorkletUrl: string;
|
|
7
|
+
};
|
|
8
|
+
export declare function validateLiveBrowserAssets(value: LiveBrowserAssets): LiveBrowserAssets;
|
|
9
|
+
export declare function createLiveBrowserDetector(input: {
|
|
10
|
+
assets: LiveBrowserAssets;
|
|
11
|
+
captureEpoch: number;
|
|
12
|
+
sampleRate: number;
|
|
13
|
+
signal: AbortSignal;
|
|
14
|
+
onFault(): void;
|
|
15
|
+
onResult(result: LiveSpeechResult): void;
|
|
16
|
+
}): Promise<LiveMediaDetector>;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateLiveBrowserAssets = validateLiveBrowserAssets;
|
|
4
|
+
exports.createLiveBrowserDetector = createLiveBrowserDetector;
|
|
5
|
+
function validateLiveBrowserAssets(value) {
|
|
6
|
+
const base = new URL(value.baseUrl);
|
|
7
|
+
if (!base.pathname.endsWith('/') || base.username || base.password || base.search || base.hash ||
|
|
8
|
+
(base.protocol !== 'https:' && !(base.protocol === 'http:' && ['localhost', '127.0.0.1', '[::1]'].includes(base.hostname))) ||
|
|
9
|
+
value.detectorWorkerUrl !== new URL('liveBrowserDetectorWorker.js', base).href ||
|
|
10
|
+
value.audioWorkletUrl !== new URL('liveAudioWorklet.js', base).href)
|
|
11
|
+
throw new Error('AI Guide voice assets are unavailable.');
|
|
12
|
+
return Object.freeze({ ...value });
|
|
13
|
+
}
|
|
14
|
+
function createLiveBrowserDetector(input) {
|
|
15
|
+
const assets = validateLiveBrowserAssets(input.assets);
|
|
16
|
+
if (input.signal.aborted)
|
|
17
|
+
return Promise.reject(new Error('AI Guide voice capture was cancelled.'));
|
|
18
|
+
// A module worker imported by a same-origin blob supports an explicitly
|
|
19
|
+
// configured CORS-enabled immutable asset host without a main-thread model.
|
|
20
|
+
const bootstrap = URL.createObjectURL(new Blob([`import ${JSON.stringify(assets.detectorWorkerUrl)};`], { type: 'text/javascript' }));
|
|
21
|
+
let worker;
|
|
22
|
+
try {
|
|
23
|
+
worker = new Worker(bootstrap, { type: 'module', name: 'oxvo-live-speech' });
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
URL.revokeObjectURL(bootstrap);
|
|
27
|
+
return Promise.reject(error);
|
|
28
|
+
}
|
|
29
|
+
const ports = new MessageChannel();
|
|
30
|
+
let closed = false;
|
|
31
|
+
let sequence = -1;
|
|
32
|
+
let ready = false;
|
|
33
|
+
let resolve;
|
|
34
|
+
let reject;
|
|
35
|
+
const readiness = new Promise((yes, no) => { resolve = yes; reject = no; });
|
|
36
|
+
void readiness.catch(() => undefined);
|
|
37
|
+
const close = () => {
|
|
38
|
+
if (closed)
|
|
39
|
+
return;
|
|
40
|
+
closed = true;
|
|
41
|
+
clearTimeout(timer);
|
|
42
|
+
input.signal.removeEventListener('abort', close);
|
|
43
|
+
reject(new Error('AI Guide voice detection stopped.'));
|
|
44
|
+
worker.terminate();
|
|
45
|
+
ports.port1.close();
|
|
46
|
+
ports.port2.close();
|
|
47
|
+
URL.revokeObjectURL(bootstrap);
|
|
48
|
+
};
|
|
49
|
+
const fail = () => { close(); input.onFault(); };
|
|
50
|
+
const timer = setTimeout(fail, 15000);
|
|
51
|
+
input.signal.addEventListener('abort', close, { once: true });
|
|
52
|
+
worker.onerror = fail;
|
|
53
|
+
worker.onmessage = event => {
|
|
54
|
+
if (closed)
|
|
55
|
+
return;
|
|
56
|
+
const p = event.data;
|
|
57
|
+
if (p?.type === 'ready' && !ready && p.captureEpoch === input.captureEpoch && p.sampleRate === input.sampleRate && Object.keys(p).length === 3) {
|
|
58
|
+
ready = true;
|
|
59
|
+
clearTimeout(timer);
|
|
60
|
+
resolve({ captureEpoch: input.captureEpoch, sampleRate: input.sampleRate });
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (!ready || p?.type !== 'result' || Object.keys(p).length !== 5 || p.epoch !== input.captureEpoch ||
|
|
64
|
+
p.sequence !== sequence + 1 || !Number.isSafeInteger(p.sourceEnd) || Number(p.sourceEnd) < 0 ||
|
|
65
|
+
typeof p.probability !== 'number' || !Number.isFinite(p.probability) || p.probability < 0 || p.probability > 1) {
|
|
66
|
+
fail();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
sequence++;
|
|
70
|
+
input.onResult({ epoch: input.captureEpoch, sequence, sourceEnd: Number(p.sourceEnd), probability: p.probability });
|
|
71
|
+
};
|
|
72
|
+
worker.postMessage({ type: 'attach', baseUrl: assets.baseUrl, captureEpoch: input.captureEpoch, sampleRate: input.sampleRate,
|
|
73
|
+
port: ports.port2 }, [ports.port2]);
|
|
74
|
+
return Promise.resolve({ port: ports.port1, ready: readiness, close });
|
|
75
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const liveSpeechCaptureBridge_js_1 = require("./liveSpeechCaptureBridge.js");
|
|
4
|
+
const liveSpeechModel_js_1 = require("./liveSpeechModel.js");
|
|
5
|
+
const liveSpeechAssets_js_1 = require("./liveSpeechAssets.js");
|
|
6
|
+
// Dedicated, separately bundled worker entry. Raw capture stays on its direct
|
|
7
|
+
// transferred port; only vetted readiness/results leave this worker.
|
|
8
|
+
const scope = self;
|
|
9
|
+
let bridge = null;
|
|
10
|
+
let initialized = false;
|
|
11
|
+
const urls = [];
|
|
12
|
+
const digest = async (bytes) => [...new Uint8Array(await crypto.subtle.digest('SHA-256', new Uint8Array(bytes).buffer))]
|
|
13
|
+
.map(byte => byte.toString(16).padStart(2, '0')).join('');
|
|
14
|
+
async function loadModel(baseUrl) {
|
|
15
|
+
const data = new Map();
|
|
16
|
+
await Promise.all(Object.keys(liveSpeechAssets_js_1.LIVE_SPEECH_ASSETS).map(async (name) => {
|
|
17
|
+
const asset = liveSpeechAssets_js_1.LIVE_SPEECH_ASSETS[name];
|
|
18
|
+
const response = await fetch(new URL(asset.name, baseUrl), { credentials: 'omit', cache: 'force-cache', redirect: 'error' });
|
|
19
|
+
if (!response.ok || (response.headers.has('content-length') && Number(response.headers.get('content-length')) > asset.bytes))
|
|
20
|
+
throw new Error('Detector asset unavailable.');
|
|
21
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
22
|
+
await (0, liveSpeechAssets_js_1.verifyLiveSpeechAsset)(name, bytes, digest);
|
|
23
|
+
data.set(name, bytes);
|
|
24
|
+
}));
|
|
25
|
+
const blob = (name, type) => {
|
|
26
|
+
const bytes = data.get(name);
|
|
27
|
+
if (!bytes)
|
|
28
|
+
throw new Error('Detector asset unavailable.');
|
|
29
|
+
const url = URL.createObjectURL(new Blob([new Uint8Array(bytes).buffer], { type }));
|
|
30
|
+
urls.push(url);
|
|
31
|
+
return url;
|
|
32
|
+
};
|
|
33
|
+
const ort = await Promise.resolve(`${blob('runtime', 'text/javascript')}`).then(s => require(s));
|
|
34
|
+
ort.env.wasm.wasmPaths = { mjs: blob('glue', 'text/javascript'), wasm: blob('wasm', 'application/wasm') };
|
|
35
|
+
const model = data.get('model');
|
|
36
|
+
if (!model)
|
|
37
|
+
throw new Error('Detector model unavailable.');
|
|
38
|
+
return liveSpeechModel_js_1.SileroLiveSpeechModel.create(ort, model);
|
|
39
|
+
}
|
|
40
|
+
scope.onmessage = event => {
|
|
41
|
+
if (event.data?.type === 'close') {
|
|
42
|
+
bridge?.close();
|
|
43
|
+
for (const url of urls)
|
|
44
|
+
URL.revokeObjectURL(url);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (initialized)
|
|
48
|
+
return;
|
|
49
|
+
initialized = true;
|
|
50
|
+
const value = event.data;
|
|
51
|
+
try {
|
|
52
|
+
if (value.type !== 'attach' || !(value.port instanceof MessagePort) || typeof value.baseUrl !== 'string' ||
|
|
53
|
+
!Number.isSafeInteger(value.captureEpoch) || value.captureEpoch < 0 || !Number.isSafeInteger(value.sampleRate) ||
|
|
54
|
+
value.sampleRate < 8000 || value.sampleRate > 96000)
|
|
55
|
+
throw new Error('Invalid detector attachment.');
|
|
56
|
+
const base = new URL(value.baseUrl);
|
|
57
|
+
if (!base.pathname.endsWith('/') || base.username || base.password || base.search || base.hash ||
|
|
58
|
+
(base.protocol !== 'https:' && !(base.protocol === 'http:' && ['localhost', '127.0.0.1', '[::1]'].includes(base.hostname))))
|
|
59
|
+
throw new Error('Invalid detector origin.');
|
|
60
|
+
bridge = new liveSpeechCaptureBridge_js_1.LiveSpeechCaptureBridge(value.port, () => loadModel(base.href), {
|
|
61
|
+
sampleRate: value.sampleRate, epoch: value.captureEpoch, now: () => performance.now(),
|
|
62
|
+
schedule: (callback, ms) => setTimeout(callback, ms), cancel: timer => clearTimeout(timer),
|
|
63
|
+
onReady: epoch => scope.postMessage({ type: 'ready', captureEpoch: epoch, sampleRate: value.sampleRate }),
|
|
64
|
+
onResult: result => scope.postMessage({ type: 'result', ...result }),
|
|
65
|
+
onUnhealthy: () => scope.postMessage({ type: 'unhealthy' }),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
value.port?.close();
|
|
70
|
+
scope.postMessage({ type: 'unhealthy' });
|
|
71
|
+
}
|
|
72
|
+
};
|