@micdrop/client 1.0.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 +111 -0
- package/dist/index.d.mts +135 -0
- package/dist/index.d.ts +135 -0
- package/dist/index.js +679 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +640 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +44 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
// ../server/src/types.ts
|
|
8
|
+
var CallClientCommands = /* @__PURE__ */ ((CallClientCommands3) => {
|
|
9
|
+
CallClientCommands3["StartSpeaking"] = "startSpeaking";
|
|
10
|
+
CallClientCommands3["StopSpeaking"] = "stopSpeaking";
|
|
11
|
+
CallClientCommands3["Mute"] = "mute";
|
|
12
|
+
return CallClientCommands3;
|
|
13
|
+
})(CallClientCommands || {});
|
|
14
|
+
var CallServerCommands = /* @__PURE__ */ ((CallServerCommands3) => {
|
|
15
|
+
CallServerCommands3["UserMessage"] = "userMessage";
|
|
16
|
+
CallServerCommands3["AssistantMessage"] = "assistantMessage";
|
|
17
|
+
CallServerCommands3["CancelLastAssistantMessage"] = "cancelLastAssistantMessage";
|
|
18
|
+
CallServerCommands3["EndInterview"] = "endInterview";
|
|
19
|
+
return CallServerCommands3;
|
|
20
|
+
})(CallServerCommands || {});
|
|
21
|
+
|
|
22
|
+
// src/audio/localStorage.ts
|
|
23
|
+
var LocalStorageKeys = /* @__PURE__ */ ((LocalStorageKeys2) => {
|
|
24
|
+
LocalStorageKeys2["MicDevice"] = "micdrop.micDevice";
|
|
25
|
+
LocalStorageKeys2["SpeakerDevice"] = "micdrop.speakerDevice";
|
|
26
|
+
LocalStorageKeys2["MicThreshold"] = "micdrop.micThreshold";
|
|
27
|
+
return LocalStorageKeys2;
|
|
28
|
+
})(LocalStorageKeys || {});
|
|
29
|
+
|
|
30
|
+
// src/audio/MicRecorder.ts
|
|
31
|
+
import EventEmitter2 from "eventemitter3";
|
|
32
|
+
import hark from "hark";
|
|
33
|
+
|
|
34
|
+
// src/audio/microphone.ts
|
|
35
|
+
var microphone_exports = {};
|
|
36
|
+
__export(microphone_exports, {
|
|
37
|
+
createDelayedStream: () => createDelayedStream,
|
|
38
|
+
defaultMicThreshold: () => defaultMicThreshold,
|
|
39
|
+
getMicConstraints: () => getMicConstraints,
|
|
40
|
+
micAnalyser: () => micAnalyser,
|
|
41
|
+
startMicrophone: () => startMicrophone,
|
|
42
|
+
stopMicrophone: () => stopMicrophone
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// src/audio/AudioAnalyser.ts
|
|
46
|
+
import EventEmitter from "eventemitter3";
|
|
47
|
+
var AudioAnalyser = class extends EventEmitter {
|
|
48
|
+
constructor(audioContext3, volumeInterval = 100) {
|
|
49
|
+
super();
|
|
50
|
+
this.volumeInterval = volumeInterval;
|
|
51
|
+
this.volume = -Infinity;
|
|
52
|
+
this.fftSize = 512;
|
|
53
|
+
this.fftBins = new Float32Array(this.fftSize);
|
|
54
|
+
this.node = audioContext3.createAnalyser();
|
|
55
|
+
this.node.fftSize = this.fftSize;
|
|
56
|
+
this.node.smoothingTimeConstant = 0.1;
|
|
57
|
+
this.interval = window.setInterval(() => {
|
|
58
|
+
this.volume = this.getMaxVolume();
|
|
59
|
+
this.emit("volume", this.volume);
|
|
60
|
+
}, this.volumeInterval);
|
|
61
|
+
}
|
|
62
|
+
stop() {
|
|
63
|
+
if (this.interval !== void 0) {
|
|
64
|
+
window.clearInterval(this.interval);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// Return values between -Infinity and 0
|
|
68
|
+
getMaxVolume() {
|
|
69
|
+
let maxVolume = -Infinity;
|
|
70
|
+
this.node.getFloatFrequencyData(this.fftBins);
|
|
71
|
+
for (let i = 4, ii = this.fftBins.length; i < ii; i++) {
|
|
72
|
+
if (this.fftBins[i] > maxVolume && this.fftBins[i] < 0) {
|
|
73
|
+
maxVolume = this.fftBins[i];
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return maxVolume;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// src/audio/stopStream.ts
|
|
81
|
+
function stopStream(stream) {
|
|
82
|
+
stream?.getTracks().forEach((track) => track.stop());
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/audio/microphone.ts
|
|
86
|
+
var currentDeviceId;
|
|
87
|
+
var audioStream;
|
|
88
|
+
var audioContext;
|
|
89
|
+
var sourceNode;
|
|
90
|
+
var micAnalyser;
|
|
91
|
+
var defaultMicThreshold = -50;
|
|
92
|
+
function init() {
|
|
93
|
+
if (audioContext) return;
|
|
94
|
+
audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
|
95
|
+
micAnalyser = new AudioAnalyser(audioContext);
|
|
96
|
+
}
|
|
97
|
+
init();
|
|
98
|
+
document.addEventListener("click", () => {
|
|
99
|
+
if (audioContext?.state === "suspended") {
|
|
100
|
+
audioContext?.resume();
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
function getMicConstraints(deviceId) {
|
|
104
|
+
return {
|
|
105
|
+
audio: {
|
|
106
|
+
deviceId: { ideal: deviceId },
|
|
107
|
+
sampleRate: 16e3,
|
|
108
|
+
// not working, it will follow device settings, usually 44.1kHz
|
|
109
|
+
sampleSize: 16,
|
|
110
|
+
channelCount: 1,
|
|
111
|
+
echoCancellation: true,
|
|
112
|
+
noiseSuppression: true
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
async function startMicrophone(deviceId) {
|
|
117
|
+
if (!audioContext || !micAnalyser) {
|
|
118
|
+
throw new Error("AudioContext not initialized");
|
|
119
|
+
}
|
|
120
|
+
if (!deviceId) {
|
|
121
|
+
deviceId = localStorage.getItem("micdrop.micDevice" /* MicDevice */) || void 0;
|
|
122
|
+
}
|
|
123
|
+
if (audioStream) {
|
|
124
|
+
if (currentDeviceId === deviceId) {
|
|
125
|
+
return audioStream;
|
|
126
|
+
}
|
|
127
|
+
stopStream(audioStream);
|
|
128
|
+
}
|
|
129
|
+
audioStream = await navigator.mediaDevices.getUserMedia(
|
|
130
|
+
getMicConstraints(deviceId)
|
|
131
|
+
);
|
|
132
|
+
const track = audioStream.getTracks()[0];
|
|
133
|
+
const newDeviceId = track && track.getCapabilities && track.getCapabilities().deviceId;
|
|
134
|
+
if (newDeviceId) {
|
|
135
|
+
localStorage.setItem("micdrop.micDevice" /* MicDevice */, newDeviceId);
|
|
136
|
+
currentDeviceId = newDeviceId;
|
|
137
|
+
} else {
|
|
138
|
+
localStorage.removeItem("micdrop.micDevice" /* MicDevice */);
|
|
139
|
+
currentDeviceId = void 0;
|
|
140
|
+
}
|
|
141
|
+
if (sourceNode) sourceNode.disconnect();
|
|
142
|
+
sourceNode = audioContext.createMediaStreamSource(audioStream);
|
|
143
|
+
sourceNode.connect(micAnalyser.node);
|
|
144
|
+
return audioStream;
|
|
145
|
+
}
|
|
146
|
+
function stopMicrophone() {
|
|
147
|
+
if (sourceNode) {
|
|
148
|
+
sourceNode.disconnect();
|
|
149
|
+
sourceNode = void 0;
|
|
150
|
+
}
|
|
151
|
+
if (audioStream) {
|
|
152
|
+
stopStream(audioStream);
|
|
153
|
+
audioStream = void 0;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function createDelayedStream(stream) {
|
|
157
|
+
if (!audioContext) {
|
|
158
|
+
throw new Error("AudioContext not initialized");
|
|
159
|
+
}
|
|
160
|
+
const audioSource = audioContext.createMediaStreamSource(stream);
|
|
161
|
+
const delayNode = audioContext.createDelay();
|
|
162
|
+
delayNode.delayTime.value = 0.3;
|
|
163
|
+
const audioDestination = audioContext.createMediaStreamDestination();
|
|
164
|
+
audioSource.connect(delayNode);
|
|
165
|
+
delayNode.connect(audioDestination);
|
|
166
|
+
return audioDestination.stream;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// src/audio/MicRecorder.ts
|
|
170
|
+
var timeSlice = 500;
|
|
171
|
+
var defaultMicRecorderState = {
|
|
172
|
+
isStarting: false,
|
|
173
|
+
isStarted: false,
|
|
174
|
+
isMuted: false,
|
|
175
|
+
isSpeaking: false,
|
|
176
|
+
threshold: defaultMicThreshold
|
|
177
|
+
};
|
|
178
|
+
var MicRecorder = class extends EventEmitter2 {
|
|
179
|
+
constructor() {
|
|
180
|
+
super();
|
|
181
|
+
this.audioInfo = this.getAudioInfo();
|
|
182
|
+
this.onStartSpeaking = (async () => {
|
|
183
|
+
if (!this.recorder || this.state.isMuted) return;
|
|
184
|
+
this.recorder.start(timeSlice);
|
|
185
|
+
this.emit("StartSpeaking");
|
|
186
|
+
this.changeState({ isSpeaking: true });
|
|
187
|
+
}).bind(this);
|
|
188
|
+
this.onStopSpeaking = (async () => {
|
|
189
|
+
if (!this.recorder || this.state.isMuted) return;
|
|
190
|
+
this.recorder.stop();
|
|
191
|
+
this.changeState({ isSpeaking: false });
|
|
192
|
+
}).bind(this);
|
|
193
|
+
const threshold = parseFloat(localStorage.getItem("micdrop.micThreshold" /* MicThreshold */) || "0") || defaultMicThreshold;
|
|
194
|
+
this.state = { ...defaultMicRecorderState, threshold };
|
|
195
|
+
}
|
|
196
|
+
async start(stream) {
|
|
197
|
+
if (this.state.isStarted) {
|
|
198
|
+
throw new Error("MicRecorder is already started");
|
|
199
|
+
}
|
|
200
|
+
try {
|
|
201
|
+
this.changeState({
|
|
202
|
+
isStarting: true,
|
|
203
|
+
isMuted: false,
|
|
204
|
+
isSpeaking: false
|
|
205
|
+
});
|
|
206
|
+
const delayedStream = createDelayedStream(stream);
|
|
207
|
+
this.delayedStream = delayedStream;
|
|
208
|
+
if (!this.recorder) {
|
|
209
|
+
this.recorder = new window.MediaRecorder(delayedStream, {
|
|
210
|
+
mimeType: this.audioInfo.mimeType,
|
|
211
|
+
audioBitsPerSecond: 128e3
|
|
212
|
+
});
|
|
213
|
+
this.recorder.ondataavailable = this.onDataAvailable.bind(this);
|
|
214
|
+
}
|
|
215
|
+
await this.startSpeakingDetection(stream);
|
|
216
|
+
this.changeState({
|
|
217
|
+
isStarting: false,
|
|
218
|
+
isStarted: true
|
|
219
|
+
});
|
|
220
|
+
} catch (err) {
|
|
221
|
+
this.stop();
|
|
222
|
+
console.error(err);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
mute() {
|
|
226
|
+
this.changeState({ isMuted: true, isSpeaking: false });
|
|
227
|
+
this.recorder?.stop();
|
|
228
|
+
}
|
|
229
|
+
unmute() {
|
|
230
|
+
this.changeState({ isMuted: false });
|
|
231
|
+
}
|
|
232
|
+
stop() {
|
|
233
|
+
this.changeState({
|
|
234
|
+
isStarting: false,
|
|
235
|
+
isStarted: false,
|
|
236
|
+
isMuted: false,
|
|
237
|
+
isSpeaking: false
|
|
238
|
+
});
|
|
239
|
+
try {
|
|
240
|
+
this.stopSpeakingDetection();
|
|
241
|
+
if (this.recorder) {
|
|
242
|
+
this.recorder.stop();
|
|
243
|
+
this.recorder = void 0;
|
|
244
|
+
}
|
|
245
|
+
if (this.delayedStream) {
|
|
246
|
+
stopStream(this.delayedStream);
|
|
247
|
+
this.delayedStream = void 0;
|
|
248
|
+
}
|
|
249
|
+
} catch (err) {
|
|
250
|
+
console.error(err);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
setThreshold(threshold) {
|
|
254
|
+
this.changeState({ threshold });
|
|
255
|
+
localStorage.setItem("micdrop.micThreshold" /* MicThreshold */, threshold.toString());
|
|
256
|
+
this.hark?.setThreshold(threshold);
|
|
257
|
+
}
|
|
258
|
+
async startSpeakingDetection(stream) {
|
|
259
|
+
if (this.hark) return;
|
|
260
|
+
this.hark = hark(stream, {
|
|
261
|
+
interval: 100,
|
|
262
|
+
play: false,
|
|
263
|
+
threshold: this.state.threshold
|
|
264
|
+
});
|
|
265
|
+
this.hark.on("speaking", this.onStartSpeaking);
|
|
266
|
+
this.hark.on("stopped_speaking", this.onStopSpeaking);
|
|
267
|
+
}
|
|
268
|
+
stopSpeakingDetection() {
|
|
269
|
+
if (!this.hark) return;
|
|
270
|
+
this.hark.off("speaking", this.onStartSpeaking);
|
|
271
|
+
this.hark.off("stopped_speaking", this.onStopSpeaking);
|
|
272
|
+
this.hark.stop();
|
|
273
|
+
this.hark = void 0;
|
|
274
|
+
}
|
|
275
|
+
async onDataAvailable(blobEvent) {
|
|
276
|
+
this.emit("Chunk", blobEvent.data);
|
|
277
|
+
if (!this.state.isSpeaking) {
|
|
278
|
+
this.emit("StopSpeaking");
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
changeState(state) {
|
|
282
|
+
const hasChanged = Object.keys(state).some(
|
|
283
|
+
(key) => this.state[key] !== state[key]
|
|
284
|
+
);
|
|
285
|
+
if (!hasChanged) return;
|
|
286
|
+
this.state = { ...this.state, ...state };
|
|
287
|
+
this.emit("StateChange", this.state);
|
|
288
|
+
}
|
|
289
|
+
getAudioInfo() {
|
|
290
|
+
if (window.MediaRecorder.isTypeSupported("audio/ogg")) {
|
|
291
|
+
return { mimeType: "audio/ogg", ext: "ogg" };
|
|
292
|
+
} else if (window.MediaRecorder.isTypeSupported("audio/webm")) {
|
|
293
|
+
return { mimeType: "audio/webm", ext: "webm" };
|
|
294
|
+
} else if (window.MediaRecorder.isTypeSupported("audio/mp4")) {
|
|
295
|
+
return { mimeType: "audio/mp4", ext: "m4a" };
|
|
296
|
+
}
|
|
297
|
+
return { mimeType: "audio/wav", ext: "wav" };
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
// src/audio/speaker.ts
|
|
302
|
+
var speaker_exports = {};
|
|
303
|
+
__export(speaker_exports, {
|
|
304
|
+
canChangeSpeakerDevice: () => canChangeSpeakerDevice,
|
|
305
|
+
changeSpeakerDevice: () => changeSpeakerDevice,
|
|
306
|
+
playAudioBlob: () => playAudioBlob,
|
|
307
|
+
speakerAnalyser: () => speakerAnalyser,
|
|
308
|
+
stopAudioBlob: () => stopAudioBlob
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
// src/audio/unlock.ts
|
|
312
|
+
function unlock(audioContext3) {
|
|
313
|
+
const events = ["touchstart", "touchend", "mousedown", "keydown"];
|
|
314
|
+
events.forEach((e) => document.body.addEventListener(e, unlock2, false));
|
|
315
|
+
async function unlock2() {
|
|
316
|
+
unlockSafari(audioContext3);
|
|
317
|
+
if (audioContext3.state === "suspended") {
|
|
318
|
+
await audioContext3.resume();
|
|
319
|
+
}
|
|
320
|
+
clean();
|
|
321
|
+
}
|
|
322
|
+
function clean() {
|
|
323
|
+
events.forEach((e) => document.body.removeEventListener(e, unlock2));
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
function unlockSafari(audioContext3) {
|
|
327
|
+
const source = audioContext3.createBufferSource();
|
|
328
|
+
source.buffer = audioContext3.createBuffer(1, 1, 22050);
|
|
329
|
+
source.connect(audioContext3.destination);
|
|
330
|
+
const onEnded = () => {
|
|
331
|
+
source.disconnect();
|
|
332
|
+
source.buffer = null;
|
|
333
|
+
source.removeEventListener("ended", onEnded);
|
|
334
|
+
};
|
|
335
|
+
source.addEventListener("ended", onEnded);
|
|
336
|
+
source.start();
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// src/audio/speaker.ts
|
|
340
|
+
var audioElement;
|
|
341
|
+
var audioContext2;
|
|
342
|
+
var audioBlobSourceNode;
|
|
343
|
+
var speakerAnalyser;
|
|
344
|
+
function init2() {
|
|
345
|
+
if (audioContext2) return;
|
|
346
|
+
audioContext2 = new (window.AudioContext || window.webkitAudioContext)();
|
|
347
|
+
speakerAnalyser = new AudioAnalyser(audioContext2);
|
|
348
|
+
speakerAnalyser.node.connect(audioContext2.destination);
|
|
349
|
+
unlock(audioContext2);
|
|
350
|
+
}
|
|
351
|
+
init2();
|
|
352
|
+
function canChangeSpeakerDevice() {
|
|
353
|
+
return window.HTMLMediaElement && "sinkId" in window.HTMLMediaElement.prototype;
|
|
354
|
+
}
|
|
355
|
+
function setSinkId(speakerId) {
|
|
356
|
+
if (speakerId && canChangeSpeakerDevice()) {
|
|
357
|
+
return audioElement?.setSinkId(speakerId);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
async function attachAudioElement(speakerId) {
|
|
361
|
+
if (!audioContext2 || !speakerAnalyser) return;
|
|
362
|
+
if (!speakerId) {
|
|
363
|
+
speakerId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */) || void 0;
|
|
364
|
+
}
|
|
365
|
+
if (!speakerId) return;
|
|
366
|
+
if (audioElement) {
|
|
367
|
+
await setSinkId(speakerId);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
audioElement = new Audio();
|
|
371
|
+
try {
|
|
372
|
+
await setSinkId(speakerId);
|
|
373
|
+
speakerAnalyser.node.disconnect(audioContext2.destination);
|
|
374
|
+
const destinationNode = audioContext2.createMediaStreamDestination();
|
|
375
|
+
speakerAnalyser.node.connect(destinationNode);
|
|
376
|
+
audioElement.srcObject = destinationNode.stream;
|
|
377
|
+
audioElement.play();
|
|
378
|
+
} catch (error) {
|
|
379
|
+
console.error(`Error setting Audio Output to ${speakerId}`, error);
|
|
380
|
+
audioElement = void 0;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
async function changeSpeakerDevice(speakerId) {
|
|
384
|
+
if (!canChangeSpeakerDevice()) return;
|
|
385
|
+
try {
|
|
386
|
+
await attachAudioElement(speakerId);
|
|
387
|
+
localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, speakerId);
|
|
388
|
+
} catch (error) {
|
|
389
|
+
console.error(`Error setting Audio Output to ${speakerId}`, error);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
async function playAudioBlob(blob) {
|
|
393
|
+
if (!audioContext2 || !speakerAnalyser) return;
|
|
394
|
+
try {
|
|
395
|
+
await attachAudioElement();
|
|
396
|
+
const sourceNode2 = audioContext2.createBufferSource();
|
|
397
|
+
audioBlobSourceNode = sourceNode2;
|
|
398
|
+
sourceNode2.onended = () => {
|
|
399
|
+
sourceNode2.disconnect();
|
|
400
|
+
audioBlobSourceNode = void 0;
|
|
401
|
+
};
|
|
402
|
+
sourceNode2.buffer = await audioContext2.decodeAudioData(
|
|
403
|
+
await blob.arrayBuffer()
|
|
404
|
+
);
|
|
405
|
+
sourceNode2.connect(speakerAnalyser.node);
|
|
406
|
+
sourceNode2.start();
|
|
407
|
+
} catch (error) {
|
|
408
|
+
console.error("Error playing audio blob", error, blob);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
function stopAudioBlob() {
|
|
412
|
+
if (!audioBlobSourceNode) return;
|
|
413
|
+
try {
|
|
414
|
+
audioBlobSourceNode.stop();
|
|
415
|
+
} catch (error) {
|
|
416
|
+
console.error("Error stopping audio blob", error, audioBlobSourceNode);
|
|
417
|
+
}
|
|
418
|
+
audioBlobSourceNode.disconnect();
|
|
419
|
+
audioBlobSourceNode = void 0;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// ../server/dist/index.mjs
|
|
423
|
+
var CallClientCommands2 = /* @__PURE__ */ ((CallClientCommands22) => {
|
|
424
|
+
CallClientCommands22["StartSpeaking"] = "startSpeaking";
|
|
425
|
+
CallClientCommands22["StopSpeaking"] = "stopSpeaking";
|
|
426
|
+
CallClientCommands22["Mute"] = "mute";
|
|
427
|
+
return CallClientCommands22;
|
|
428
|
+
})(CallClientCommands2 || {});
|
|
429
|
+
var CallServerCommands2 = /* @__PURE__ */ ((CallServerCommands22) => {
|
|
430
|
+
CallServerCommands22["UserMessage"] = "userMessage";
|
|
431
|
+
CallServerCommands22["AssistantMessage"] = "assistantMessage";
|
|
432
|
+
CallServerCommands22["CancelLastAssistantMessage"] = "cancelLastAssistantMessage";
|
|
433
|
+
CallServerCommands22["EndInterview"] = "endInterview";
|
|
434
|
+
return CallServerCommands22;
|
|
435
|
+
})(CallServerCommands2 || {});
|
|
436
|
+
|
|
437
|
+
// src/call/CallHandler.ts
|
|
438
|
+
import EventEmitter3 from "eventemitter3";
|
|
439
|
+
|
|
440
|
+
// src/call/CallHandlerError.ts
|
|
441
|
+
var CallHandlerErrorCode = /* @__PURE__ */ ((CallHandlerErrorCode2) => {
|
|
442
|
+
CallHandlerErrorCode2["Mic"] = "Mic";
|
|
443
|
+
CallHandlerErrorCode2["Unauthorized"] = "Unauthorized";
|
|
444
|
+
CallHandlerErrorCode2["Error"] = "Error";
|
|
445
|
+
return CallHandlerErrorCode2;
|
|
446
|
+
})(CallHandlerErrorCode || {});
|
|
447
|
+
var CallHandlerError = class extends Error {
|
|
448
|
+
constructor(code = "Error" /* Error */) {
|
|
449
|
+
super();
|
|
450
|
+
this.code = code;
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
// src/call/CallHandler.ts
|
|
455
|
+
var debugEnabled = false;
|
|
456
|
+
var CallHandler = class extends EventEmitter3 {
|
|
457
|
+
constructor() {
|
|
458
|
+
super();
|
|
459
|
+
this.micRecorder = new MicRecorder();
|
|
460
|
+
this.conversation = [];
|
|
461
|
+
this.startTime = 0;
|
|
462
|
+
this.micRecorder.on("StateChange", () => {
|
|
463
|
+
this.notifyStateChange();
|
|
464
|
+
});
|
|
465
|
+
this.micRecorder.on("Chunk", (blob) => {
|
|
466
|
+
this.debug(`[Mic] onChunk`, blob);
|
|
467
|
+
this.ws?.send(blob);
|
|
468
|
+
});
|
|
469
|
+
this.micRecorder.on("StartSpeaking", () => {
|
|
470
|
+
this.debug("[Mic] onStartSpeaking");
|
|
471
|
+
this.ws?.send(CallClientCommands2.StartSpeaking);
|
|
472
|
+
stopAudioBlob();
|
|
473
|
+
});
|
|
474
|
+
this.micRecorder.on("StopSpeaking", () => {
|
|
475
|
+
this.debug("[Mic] onSilence");
|
|
476
|
+
this.ws?.send(CallClientCommands2.StopSpeaking);
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
get isStarted() {
|
|
480
|
+
return this.isWSStarted && this.micRecorder.state.isStarted;
|
|
481
|
+
}
|
|
482
|
+
get isStarting() {
|
|
483
|
+
return this.isWSStarting || this.micRecorder.state.isStarting;
|
|
484
|
+
}
|
|
485
|
+
get isWSStarted() {
|
|
486
|
+
return this.ws?.readyState === WebSocket.OPEN;
|
|
487
|
+
}
|
|
488
|
+
get isWSStarting() {
|
|
489
|
+
return this.ws?.readyState === WebSocket.CONNECTING;
|
|
490
|
+
}
|
|
491
|
+
get isMicStarted() {
|
|
492
|
+
return !!this.micStream;
|
|
493
|
+
}
|
|
494
|
+
async start() {
|
|
495
|
+
this.startTime = Date.now();
|
|
496
|
+
this.conversation = [];
|
|
497
|
+
await this.startMic(void 0, true);
|
|
498
|
+
await this.startWS();
|
|
499
|
+
}
|
|
500
|
+
async stop() {
|
|
501
|
+
try {
|
|
502
|
+
this.stopWS();
|
|
503
|
+
} catch (error) {
|
|
504
|
+
console.error("[CallHandler] stop WS", error);
|
|
505
|
+
}
|
|
506
|
+
try {
|
|
507
|
+
this.micRecorder.stop();
|
|
508
|
+
this.stopMic();
|
|
509
|
+
} catch (error) {
|
|
510
|
+
console.error("[CallHandler] stop mic", error);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
pause() {
|
|
514
|
+
this.micRecorder.mute();
|
|
515
|
+
this.notifyStateChange();
|
|
516
|
+
}
|
|
517
|
+
resume() {
|
|
518
|
+
if (this.lastAudioBlob) {
|
|
519
|
+
playAudioBlob(this.lastAudioBlob);
|
|
520
|
+
}
|
|
521
|
+
this.micRecorder.unmute();
|
|
522
|
+
this.notifyStateChange();
|
|
523
|
+
}
|
|
524
|
+
async startMic(deviceId, record = true) {
|
|
525
|
+
try {
|
|
526
|
+
const isRecorderStarted = this.micRecorder.state.isStarted;
|
|
527
|
+
if (isRecorderStarted) {
|
|
528
|
+
this.micRecorder.stop();
|
|
529
|
+
}
|
|
530
|
+
this.micStream = await startMicrophone(deviceId);
|
|
531
|
+
this.notifyStateChange();
|
|
532
|
+
if (isRecorderStarted || record) {
|
|
533
|
+
await this.micRecorder.start(this.micStream);
|
|
534
|
+
}
|
|
535
|
+
} catch (error) {
|
|
536
|
+
console.error("[CallHandler] startMic", error);
|
|
537
|
+
this.emit("Error", new CallHandlerError("Mic" /* Mic */));
|
|
538
|
+
this.stop();
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
stopMic() {
|
|
542
|
+
stopMicrophone();
|
|
543
|
+
this.micStream = void 0;
|
|
544
|
+
this.notifyStateChange();
|
|
545
|
+
}
|
|
546
|
+
async startWS() {
|
|
547
|
+
if (this.ws) {
|
|
548
|
+
throw new Error("[CallHandler] startWS: WebSocket is already started");
|
|
549
|
+
}
|
|
550
|
+
if (!this.isMicStarted) {
|
|
551
|
+
throw new Error("[CallHandler] startWS: Microphone is not started");
|
|
552
|
+
}
|
|
553
|
+
if (!this.url) {
|
|
554
|
+
throw new Error("[CallHandler] startWS: URL is not set");
|
|
555
|
+
}
|
|
556
|
+
this.ws = new WebSocket(this.url);
|
|
557
|
+
this.ws.binaryType = "blob";
|
|
558
|
+
this.notifyStateChange();
|
|
559
|
+
this.ws.onopen = () => {
|
|
560
|
+
this.debug("[WS] Opened");
|
|
561
|
+
this.notifyStateChange();
|
|
562
|
+
if (this.params) {
|
|
563
|
+
this.ws?.send(JSON.stringify(this.params));
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
this.ws.onmessage = (event) => {
|
|
567
|
+
this.debug("[WS]", event.data);
|
|
568
|
+
if (event.data instanceof Blob) {
|
|
569
|
+
playAudioBlob(event.data);
|
|
570
|
+
this.lastAudioBlob = event.data;
|
|
571
|
+
} else if (typeof event.data !== "string") {
|
|
572
|
+
console.warn(`[WS] Unknown message type: ${event.data}`);
|
|
573
|
+
} else if (event.data.startsWith(CallServerCommands2.UserMessage)) {
|
|
574
|
+
this.addMessage(
|
|
575
|
+
"user",
|
|
576
|
+
event.data.substring(CallServerCommands2.UserMessage.length + 1)
|
|
577
|
+
);
|
|
578
|
+
} else if (event.data.startsWith(CallServerCommands2.AssistantMessage)) {
|
|
579
|
+
this.addMessage(
|
|
580
|
+
"assistant",
|
|
581
|
+
event.data.substring(CallServerCommands2.AssistantMessage.length + 1)
|
|
582
|
+
);
|
|
583
|
+
} else if (event.data === CallServerCommands2.EndInterview) {
|
|
584
|
+
setTimeout(() => {
|
|
585
|
+
this.emit("EndInterview");
|
|
586
|
+
}, 2e3);
|
|
587
|
+
} else if (event.data === CallServerCommands2.CancelLastAssistantMessage) {
|
|
588
|
+
this.conversation = this.conversation.slice(0, -1);
|
|
589
|
+
this.notifyStateChange();
|
|
590
|
+
}
|
|
591
|
+
};
|
|
592
|
+
this.ws.onclose = (event) => {
|
|
593
|
+
this.debug("[WS] Closed", event);
|
|
594
|
+
this.notifyStateChange();
|
|
595
|
+
if (event.code >= 1001 && event.code <= 1011 && event.code !== 1005) {
|
|
596
|
+
this.emit("Error", new CallHandlerError());
|
|
597
|
+
} else if (event.code === 4401) {
|
|
598
|
+
this.emit(
|
|
599
|
+
"Error",
|
|
600
|
+
new CallHandlerError("Unauthorized" /* Unauthorized */)
|
|
601
|
+
);
|
|
602
|
+
} else if (event.code >= 4e3) {
|
|
603
|
+
this.emit("Error", new CallHandlerError());
|
|
604
|
+
}
|
|
605
|
+
};
|
|
606
|
+
this.ws.onerror = (event) => {
|
|
607
|
+
console.error("[CallHandler] [WS] Error:", event);
|
|
608
|
+
this.stop();
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
stopWS() {
|
|
612
|
+
if (!this.ws) return;
|
|
613
|
+
this.ws.close();
|
|
614
|
+
this.ws = void 0;
|
|
615
|
+
this.notifyStateChange();
|
|
616
|
+
}
|
|
617
|
+
addMessage(role, message) {
|
|
618
|
+
this.conversation.push({ role, content: message });
|
|
619
|
+
this.notifyStateChange();
|
|
620
|
+
}
|
|
621
|
+
notifyStateChange() {
|
|
622
|
+
this.emit("StateChange");
|
|
623
|
+
}
|
|
624
|
+
debug(...message) {
|
|
625
|
+
if (!debugEnabled) return;
|
|
626
|
+
console.log(`[Debug ${Date.now() - this.startTime}]`, ...message);
|
|
627
|
+
}
|
|
628
|
+
};
|
|
629
|
+
export {
|
|
630
|
+
CallClientCommands,
|
|
631
|
+
CallHandler,
|
|
632
|
+
CallHandlerError,
|
|
633
|
+
CallHandlerErrorCode,
|
|
634
|
+
CallServerCommands,
|
|
635
|
+
LocalStorageKeys,
|
|
636
|
+
microphone_exports as Mic,
|
|
637
|
+
MicRecorder,
|
|
638
|
+
speaker_exports as Speaker
|
|
639
|
+
};
|
|
640
|
+
//# sourceMappingURL=index.mjs.map
|