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