@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.d.mts CHANGED
@@ -213,6 +213,7 @@ declare enum MicdropClientCommands {
213
213
  }
214
214
  declare enum MicdropServerCommands {
215
215
  Message = "Message",
216
+ PartialAssistantMessage = "PartialAssistantMessage",
216
217
  CancelLastUserMessage = "CancelLastUserMessage",
217
218
  SkipAnswer = "SkipAnswer",
218
219
  EndCall = "EndCall",
@@ -333,7 +334,7 @@ declare abstract class VAD extends EventEmitter<VADEvents> {
333
334
  * Sets the status of the VAD
334
335
  * @param status - The status to set
335
336
  */
336
- private setStatus;
337
+ protected setStatus(status: VADStatus): void;
337
338
  }
338
339
 
339
340
  type VADConfigName = 'volume' | 'silero';
@@ -599,6 +600,7 @@ declare class SileroVAD extends VAD {
599
600
  private speechFrames;
600
601
  private silenceFrames;
601
602
  private processing;
603
+ private round;
602
604
  constructor(options?: Partial<SileroVADOptions>);
603
605
  get isStarted(): boolean;
604
606
  get isPaused(): boolean;
@@ -724,6 +726,10 @@ interface MicdropEvents {
724
726
  Error: [MicdropClientError];
725
727
  StateChange: [MicdropState, MicdropState];
726
728
  ToolCall: [MicdropToolCall];
729
+ /** A message was added to the conversation, by the user or the assistant */
730
+ Message: [MicdropConversationItem];
731
+ /** What the assistant is answering, before it is finished */
732
+ PartialAssistantMessage: [string];
727
733
  }
728
734
  interface MicdropReconnectOptions {
729
735
  maxAttempts?: number;
@@ -763,12 +769,19 @@ interface MicdropState {
763
769
  micDevices: MicdropDevice[];
764
770
  speakerDevices: MicdropDevice[];
765
771
  conversation: MicdropConversation;
772
+ /**
773
+ * Answer being written, sent by servers configured with `partialMessages`.
774
+ * Empty the rest of the time, and replaced by a conversation message once
775
+ * the answer is finished.
776
+ */
777
+ partialAssistantMessage: string;
766
778
  error: MicdropClientError | undefined;
767
779
  }
768
780
  declare class MicdropClient extends EventEmitter<MicdropEvents> implements MicdropState {
769
781
  options: MicdropOptions;
770
782
  micRecorder?: MicRecorder;
771
783
  conversation: MicdropConversation;
784
+ partialAssistantMessage: string;
772
785
  error: MicdropClientError | undefined;
773
786
  speakerDevices: MicdropDevice[];
774
787
  micDevices: MicdropDevice[];
@@ -779,6 +792,7 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
779
792
  private _isMuted;
780
793
  private _isPaused;
781
794
  private _isReconnecting;
795
+ private _isStartingCall;
782
796
  private reconnectAttempt;
783
797
  private reconnectTimer?;
784
798
  private connectionTimer?;
@@ -838,6 +852,7 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
838
852
  private onSpeakerStartPlaying;
839
853
  private onSpeakerStopPlaying;
840
854
  private addMessage;
855
+ private setPartialAssistantMessage;
841
856
  private setError;
842
857
  private notifyStateChange;
843
858
  private log;
package/dist/index.d.ts CHANGED
@@ -213,6 +213,7 @@ declare enum MicdropClientCommands {
213
213
  }
214
214
  declare enum MicdropServerCommands {
215
215
  Message = "Message",
216
+ PartialAssistantMessage = "PartialAssistantMessage",
216
217
  CancelLastUserMessage = "CancelLastUserMessage",
217
218
  SkipAnswer = "SkipAnswer",
218
219
  EndCall = "EndCall",
@@ -333,7 +334,7 @@ declare abstract class VAD extends EventEmitter<VADEvents> {
333
334
  * Sets the status of the VAD
334
335
  * @param status - The status to set
335
336
  */
336
- private setStatus;
337
+ protected setStatus(status: VADStatus): void;
337
338
  }
338
339
 
339
340
  type VADConfigName = 'volume' | 'silero';
@@ -599,6 +600,7 @@ declare class SileroVAD extends VAD {
599
600
  private speechFrames;
600
601
  private silenceFrames;
601
602
  private processing;
603
+ private round;
602
604
  constructor(options?: Partial<SileroVADOptions>);
603
605
  get isStarted(): boolean;
604
606
  get isPaused(): boolean;
@@ -724,6 +726,10 @@ interface MicdropEvents {
724
726
  Error: [MicdropClientError];
725
727
  StateChange: [MicdropState, MicdropState];
726
728
  ToolCall: [MicdropToolCall];
729
+ /** A message was added to the conversation, by the user or the assistant */
730
+ Message: [MicdropConversationItem];
731
+ /** What the assistant is answering, before it is finished */
732
+ PartialAssistantMessage: [string];
727
733
  }
728
734
  interface MicdropReconnectOptions {
729
735
  maxAttempts?: number;
@@ -763,12 +769,19 @@ interface MicdropState {
763
769
  micDevices: MicdropDevice[];
764
770
  speakerDevices: MicdropDevice[];
765
771
  conversation: MicdropConversation;
772
+ /**
773
+ * Answer being written, sent by servers configured with `partialMessages`.
774
+ * Empty the rest of the time, and replaced by a conversation message once
775
+ * the answer is finished.
776
+ */
777
+ partialAssistantMessage: string;
766
778
  error: MicdropClientError | undefined;
767
779
  }
768
780
  declare class MicdropClient extends EventEmitter<MicdropEvents> implements MicdropState {
769
781
  options: MicdropOptions;
770
782
  micRecorder?: MicRecorder;
771
783
  conversation: MicdropConversation;
784
+ partialAssistantMessage: string;
772
785
  error: MicdropClientError | undefined;
773
786
  speakerDevices: MicdropDevice[];
774
787
  micDevices: MicdropDevice[];
@@ -779,6 +792,7 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
779
792
  private _isMuted;
780
793
  private _isPaused;
781
794
  private _isReconnecting;
795
+ private _isStartingCall;
782
796
  private reconnectAttempt;
783
797
  private reconnectTimer?;
784
798
  private connectionTimer?;
@@ -838,6 +852,7 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
838
852
  private onSpeakerStartPlaying;
839
853
  private onSpeakerStopPlaying;
840
854
  private addMessage;
855
+ private setPartialAssistantMessage;
841
856
  private setError;
842
857
  private notifyStateChange;
843
858
  private log;
package/dist/index.js CHANGED
@@ -651,6 +651,9 @@ var SileroVAD = class extends VAD {
651
651
  this.speechFrames = 0;
652
652
  this.silenceFrames = 0;
653
653
  this.processing = false;
654
+ // Bumped whenever the state is reset, so a window scored before that is
655
+ // dropped rather than reopening a turn
656
+ this.round = 0;
654
657
  this.onFrames = (frames, sampleRate) => {
655
658
  if (this._isPaused || !this.model) return;
656
659
  if (sampleRate !== this.bufferRate) {
@@ -688,6 +691,7 @@ var SileroVAD = class extends VAD {
688
691
  }
689
692
  this.model = await loadModel();
690
693
  this.resetState();
694
+ this.setStatus(0 /* Silence */);
691
695
  this.mic = mic;
692
696
  mic.on("Frames", this.onFrames);
693
697
  }
@@ -734,9 +738,11 @@ var SileroVAD = class extends VAD {
734
738
  this.buffer = rest.length ? [rest] : [];
735
739
  this.bufferLength = rest.length;
736
740
  const frame = resample(window, this.bufferRate, SILERO_SAMPLE_RATE);
741
+ const round = this.round;
737
742
  const probability = await this.model.process(
738
743
  frame.length === SILERO_FRAME_SAMPLES ? frame : fit(frame, SILERO_FRAME_SAMPLES)
739
744
  );
745
+ if (round !== this.round) break;
740
746
  this.onProbability(probability);
741
747
  }
742
748
  } catch (error) {
@@ -777,6 +783,7 @@ var SileroVAD = class extends VAD {
777
783
  this.emit(confirmed ? "StopSpeaking" : "CancelSpeaking");
778
784
  }
779
785
  resetState() {
786
+ this.round++;
780
787
  this.model?.reset();
781
788
  this.buffer = [];
782
789
  this.bufferLength = 0;
@@ -1409,6 +1416,7 @@ var MicdropClientCommands = /* @__PURE__ */ ((MicdropClientCommands2) => {
1409
1416
  })(MicdropClientCommands || {});
1410
1417
  var MicdropServerCommands = /* @__PURE__ */ ((MicdropServerCommands2) => {
1411
1418
  MicdropServerCommands2["Message"] = "Message";
1419
+ MicdropServerCommands2["PartialAssistantMessage"] = "PartialAssistantMessage";
1412
1420
  MicdropServerCommands2["CancelLastUserMessage"] = "CancelLastUserMessage";
1413
1421
  MicdropServerCommands2["SkipAnswer"] = "SkipAnswer";
1414
1422
  MicdropServerCommands2["EndCall"] = "EndCall";
@@ -1471,6 +1479,7 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
1471
1479
  super();
1472
1480
  this.options = options;
1473
1481
  this.conversation = [];
1482
+ this.partialAssistantMessage = "";
1474
1483
  this.speakerDevices = [];
1475
1484
  this.micDevices = [];
1476
1485
  this.startTime = 0;
@@ -1479,27 +1488,39 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
1479
1488
  this._isMuted = false;
1480
1489
  this._isPaused = false;
1481
1490
  this._isReconnecting = false;
1491
+ // From start() until the connection opens, the microphone included: opening
1492
+ // it can take a while, for the permission prompt or the browser itself
1493
+ this._isStartingCall = false;
1482
1494
  this.reconnectAttempt = 0;
1483
1495
  this.start = async (options) => {
1484
1496
  this.error = void 0;
1485
1497
  this.options = { ...this.options, ...options };
1486
1498
  this.startTime = Date.now();
1487
1499
  this.conversation = [];
1500
+ this.partialAssistantMessage = "";
1488
1501
  this._isProcessing = true;
1489
1502
  this._isMuted = false;
1490
1503
  this._isPaused = false;
1491
1504
  this._isReconnecting = false;
1505
+ this._isStartingCall = true;
1492
1506
  this.reconnectAttempt = 0;
1493
1507
  if (this.reconnectTimer) {
1494
1508
  clearTimeout(this.reconnectTimer);
1495
1509
  this.reconnectTimer = void 0;
1496
1510
  }
1511
+ this.notifyStateChange();
1497
1512
  if (!this.micRecorder || options?.vad) {
1498
1513
  await this.startMic({ vad: options?.vad });
1499
1514
  }
1500
1515
  await this.startWS();
1516
+ if (this.isWSStarted && this._isStartingCall) {
1517
+ this._isStartingCall = false;
1518
+ this.notifyStateChange();
1519
+ }
1501
1520
  };
1502
1521
  this.stop = async () => {
1522
+ this.partialAssistantMessage = "";
1523
+ this._isStartingCall = false;
1503
1524
  this._isProcessing = false;
1504
1525
  this._isMuted = false;
1505
1526
  this._isPaused = false;
@@ -1577,6 +1598,7 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
1577
1598
  this.micRecorder.on("StartSpeaking", () => {
1578
1599
  this.log("User start speaking");
1579
1600
  this.ws?.send("StartSpeaking" /* StartSpeaking */);
1601
+ this.partialAssistantMessage = "";
1580
1602
  this._isProcessing = false;
1581
1603
  this.notifyStateChange();
1582
1604
  Speaker.stopAudio();
@@ -1657,6 +1679,7 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
1657
1679
  this.connectionTimer = void 0;
1658
1680
  }
1659
1681
  this._isReconnecting = false;
1682
+ this._isStartingCall = false;
1660
1683
  this.reconnectAttempt = 0;
1661
1684
  this.error = void 0;
1662
1685
  this.notifyStateChange();
@@ -1691,9 +1714,12 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
1691
1714
  } catch (error) {
1692
1715
  console.error("[MicdropClient] Error parsing message:", data, error);
1693
1716
  }
1717
+ } else if (data.startsWith("PartialAssistantMessage" /* PartialAssistantMessage */)) {
1718
+ this.setPartialAssistantMessage(data);
1694
1719
  } else if (data === "EndCall" /* EndCall */) {
1695
1720
  this.emit("EndCall");
1696
1721
  } else if (data === "SkipAnswer" /* SkipAnswer */) {
1722
+ this.partialAssistantMessage = "";
1697
1723
  this._isProcessing = false;
1698
1724
  this.notifyStateChange();
1699
1725
  } else if (data === "CancelLastUserMessage" /* CancelLastUserMessage */) {
@@ -1766,7 +1792,7 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
1766
1792
  return (this.isWSStarted || this._isReconnecting) && (this.micRecorder?.state.isStarted || false);
1767
1793
  }
1768
1794
  get isStarting() {
1769
- return (this.isWSStarting || this.micRecorder?.state.isStarting || false) && !this._isReconnecting;
1795
+ return (this._isStartingCall || this.isWSStarting || this.micRecorder?.state.isStarting || false) && !this._isReconnecting;
1770
1796
  }
1771
1797
  get isReconnecting() {
1772
1798
  return this._isReconnecting;
@@ -1821,6 +1847,7 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
1821
1847
  isMicStarted: this.isMicStarted,
1822
1848
  isMicMuted: this.isMicMuted,
1823
1849
  conversation: this.conversation,
1850
+ partialAssistantMessage: this.partialAssistantMessage,
1824
1851
  error: this.error,
1825
1852
  micDeviceId: this.micDeviceId,
1826
1853
  speakerDeviceId: this.speakerDeviceId,
@@ -1908,8 +1935,23 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
1908
1935
  }, config.delayMs);
1909
1936
  }
1910
1937
  addMessage(message) {
1938
+ if (message.role === "assistant") this.partialAssistantMessage = "";
1911
1939
  this.conversation = [...this.conversation, message];
1912
1940
  this.notifyStateChange();
1941
+ this.emit("Message", message);
1942
+ }
1943
+ setPartialAssistantMessage(data) {
1944
+ const command = "PartialAssistantMessage" /* PartialAssistantMessage */;
1945
+ let content;
1946
+ try {
1947
+ content = JSON.parse(data.substring(command.length + 1));
1948
+ } catch (error) {
1949
+ console.error("[MicdropClient] Error parsing partial answer:", data);
1950
+ return;
1951
+ }
1952
+ this.partialAssistantMessage = content;
1953
+ this.emit("PartialAssistantMessage", content);
1954
+ this.notifyStateChange();
1913
1955
  }
1914
1956
  setError(error) {
1915
1957
  console.error("[MicdropClient] Error:", error);