@micdrop/client 3.1.0 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -587,6 +587,9 @@ var SileroVAD = class extends VAD {
587
587
  this.speechFrames = 0;
588
588
  this.silenceFrames = 0;
589
589
  this.processing = false;
590
+ // Bumped whenever the state is reset, so a window scored before that is
591
+ // dropped rather than reopening a turn
592
+ this.round = 0;
590
593
  this.onFrames = (frames, sampleRate) => {
591
594
  if (this._isPaused || !this.model) return;
592
595
  if (sampleRate !== this.bufferRate) {
@@ -624,6 +627,7 @@ var SileroVAD = class extends VAD {
624
627
  }
625
628
  this.model = await loadModel();
626
629
  this.resetState();
630
+ this.setStatus(0 /* Silence */);
627
631
  this.mic = mic;
628
632
  mic.on("Frames", this.onFrames);
629
633
  }
@@ -670,9 +674,11 @@ var SileroVAD = class extends VAD {
670
674
  this.buffer = rest.length ? [rest] : [];
671
675
  this.bufferLength = rest.length;
672
676
  const frame = resample(window, this.bufferRate, SILERO_SAMPLE_RATE);
677
+ const round = this.round;
673
678
  const probability = await this.model.process(
674
679
  frame.length === SILERO_FRAME_SAMPLES ? frame : fit(frame, SILERO_FRAME_SAMPLES)
675
680
  );
681
+ if (round !== this.round) break;
676
682
  this.onProbability(probability);
677
683
  }
678
684
  } catch (error) {
@@ -713,6 +719,7 @@ var SileroVAD = class extends VAD {
713
719
  this.emit(confirmed ? "StopSpeaking" : "CancelSpeaking");
714
720
  }
715
721
  resetState() {
722
+ this.round++;
716
723
  this.model?.reset();
717
724
  this.buffer = [];
718
725
  this.bufferLength = 0;
@@ -1345,6 +1352,7 @@ var MicdropClientCommands = /* @__PURE__ */ ((MicdropClientCommands2) => {
1345
1352
  })(MicdropClientCommands || {});
1346
1353
  var MicdropServerCommands = /* @__PURE__ */ ((MicdropServerCommands2) => {
1347
1354
  MicdropServerCommands2["Message"] = "Message";
1355
+ MicdropServerCommands2["PartialAssistantMessage"] = "PartialAssistantMessage";
1348
1356
  MicdropServerCommands2["CancelLastUserMessage"] = "CancelLastUserMessage";
1349
1357
  MicdropServerCommands2["SkipAnswer"] = "SkipAnswer";
1350
1358
  MicdropServerCommands2["EndCall"] = "EndCall";
@@ -1407,6 +1415,7 @@ var MicdropClient = class extends EventEmitter7 {
1407
1415
  super();
1408
1416
  this.options = options;
1409
1417
  this.conversation = [];
1418
+ this.partialAssistantMessage = "";
1410
1419
  this.speakerDevices = [];
1411
1420
  this.micDevices = [];
1412
1421
  this.startTime = 0;
@@ -1415,27 +1424,39 @@ var MicdropClient = class extends EventEmitter7 {
1415
1424
  this._isMuted = false;
1416
1425
  this._isPaused = false;
1417
1426
  this._isReconnecting = false;
1427
+ // From start() until the connection opens, the microphone included: opening
1428
+ // it can take a while, for the permission prompt or the browser itself
1429
+ this._isStartingCall = false;
1418
1430
  this.reconnectAttempt = 0;
1419
1431
  this.start = async (options) => {
1420
1432
  this.error = void 0;
1421
1433
  this.options = { ...this.options, ...options };
1422
1434
  this.startTime = Date.now();
1423
1435
  this.conversation = [];
1436
+ this.partialAssistantMessage = "";
1424
1437
  this._isProcessing = true;
1425
1438
  this._isMuted = false;
1426
1439
  this._isPaused = false;
1427
1440
  this._isReconnecting = false;
1441
+ this._isStartingCall = true;
1428
1442
  this.reconnectAttempt = 0;
1429
1443
  if (this.reconnectTimer) {
1430
1444
  clearTimeout(this.reconnectTimer);
1431
1445
  this.reconnectTimer = void 0;
1432
1446
  }
1447
+ this.notifyStateChange();
1433
1448
  if (!this.micRecorder || options?.vad) {
1434
1449
  await this.startMic({ vad: options?.vad });
1435
1450
  }
1436
1451
  await this.startWS();
1452
+ if (this.isWSStarted && this._isStartingCall) {
1453
+ this._isStartingCall = false;
1454
+ this.notifyStateChange();
1455
+ }
1437
1456
  };
1438
1457
  this.stop = async () => {
1458
+ this.partialAssistantMessage = "";
1459
+ this._isStartingCall = false;
1439
1460
  this._isProcessing = false;
1440
1461
  this._isMuted = false;
1441
1462
  this._isPaused = false;
@@ -1513,6 +1534,7 @@ var MicdropClient = class extends EventEmitter7 {
1513
1534
  this.micRecorder.on("StartSpeaking", () => {
1514
1535
  this.log("User start speaking");
1515
1536
  this.ws?.send("StartSpeaking" /* StartSpeaking */);
1537
+ this.partialAssistantMessage = "";
1516
1538
  this._isProcessing = false;
1517
1539
  this.notifyStateChange();
1518
1540
  Speaker.stopAudio();
@@ -1593,6 +1615,7 @@ var MicdropClient = class extends EventEmitter7 {
1593
1615
  this.connectionTimer = void 0;
1594
1616
  }
1595
1617
  this._isReconnecting = false;
1618
+ this._isStartingCall = false;
1596
1619
  this.reconnectAttempt = 0;
1597
1620
  this.error = void 0;
1598
1621
  this.notifyStateChange();
@@ -1627,9 +1650,12 @@ var MicdropClient = class extends EventEmitter7 {
1627
1650
  } catch (error) {
1628
1651
  console.error("[MicdropClient] Error parsing message:", data, error);
1629
1652
  }
1653
+ } else if (data.startsWith("PartialAssistantMessage" /* PartialAssistantMessage */)) {
1654
+ this.setPartialAssistantMessage(data);
1630
1655
  } else if (data === "EndCall" /* EndCall */) {
1631
1656
  this.emit("EndCall");
1632
1657
  } else if (data === "SkipAnswer" /* SkipAnswer */) {
1658
+ this.partialAssistantMessage = "";
1633
1659
  this._isProcessing = false;
1634
1660
  this.notifyStateChange();
1635
1661
  } else if (data === "CancelLastUserMessage" /* CancelLastUserMessage */) {
@@ -1702,7 +1728,7 @@ var MicdropClient = class extends EventEmitter7 {
1702
1728
  return (this.isWSStarted || this._isReconnecting) && (this.micRecorder?.state.isStarted || false);
1703
1729
  }
1704
1730
  get isStarting() {
1705
- return (this.isWSStarting || this.micRecorder?.state.isStarting || false) && !this._isReconnecting;
1731
+ return (this._isStartingCall || this.isWSStarting || this.micRecorder?.state.isStarting || false) && !this._isReconnecting;
1706
1732
  }
1707
1733
  get isReconnecting() {
1708
1734
  return this._isReconnecting;
@@ -1757,6 +1783,7 @@ var MicdropClient = class extends EventEmitter7 {
1757
1783
  isMicStarted: this.isMicStarted,
1758
1784
  isMicMuted: this.isMicMuted,
1759
1785
  conversation: this.conversation,
1786
+ partialAssistantMessage: this.partialAssistantMessage,
1760
1787
  error: this.error,
1761
1788
  micDeviceId: this.micDeviceId,
1762
1789
  speakerDeviceId: this.speakerDeviceId,
@@ -1844,8 +1871,23 @@ var MicdropClient = class extends EventEmitter7 {
1844
1871
  }, config.delayMs);
1845
1872
  }
1846
1873
  addMessage(message) {
1874
+ if (message.role === "assistant") this.partialAssistantMessage = "";
1847
1875
  this.conversation = [...this.conversation, message];
1848
1876
  this.notifyStateChange();
1877
+ this.emit("Message", message);
1878
+ }
1879
+ setPartialAssistantMessage(data) {
1880
+ const command = "PartialAssistantMessage" /* PartialAssistantMessage */;
1881
+ let content;
1882
+ try {
1883
+ content = JSON.parse(data.substring(command.length + 1));
1884
+ } catch (error) {
1885
+ console.error("[MicdropClient] Error parsing partial answer:", data);
1886
+ return;
1887
+ }
1888
+ this.partialAssistantMessage = content;
1889
+ this.emit("PartialAssistantMessage", content);
1890
+ this.notifyStateChange();
1849
1891
  }
1850
1892
  setError(error) {
1851
1893
  console.error("[MicdropClient] Error:", error);