@micdrop/client 3.5.0 → 3.6.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.d.mts CHANGED
@@ -703,6 +703,7 @@ declare class SileroVAD extends VAD {
703
703
  private silenceFrames;
704
704
  private processing;
705
705
  private round;
706
+ private startId;
706
707
  constructor(options?: Partial<SileroVADOptions>);
707
708
  get isStarted(): boolean;
708
709
  get isPaused(): boolean;
@@ -755,6 +756,63 @@ declare class VolumeVAD extends VAD {
755
756
  private reset;
756
757
  }
757
758
 
759
+ /** Anything a runtime hands back that has to be released once done with */
760
+ interface Releasable {
761
+ release(): Promise<void>;
762
+ }
763
+ type SessionState<S> = {
764
+ status: 'unloaded';
765
+ } | {
766
+ status: 'loading';
767
+ promise: Promise<S>;
768
+ } | {
769
+ status: 'loaded';
770
+ session: S;
771
+ } | {
772
+ status: 'released';
773
+ };
774
+ /**
775
+ * An inference session created on first use, and released once.
776
+ *
777
+ * Every caller asking while it loads shares the same load, so two of them
778
+ * never create two sessions and leak one. A release during the load disposes
779
+ * of the session as soon as it arrives instead of letting it come back to life,
780
+ * and waits for the runs in flight before freeing the runtime under them. A
781
+ * failed load is forgotten, so the next call tries again.
782
+ *
783
+ * Shared by the Silero models of `@micdrop/web` and `@micdrop/react-native`.
784
+ * `@micdrop/smart-turn` holds the same few lines for its own models, as it
785
+ * depends on no other package.
786
+ */
787
+ declare class LazySession<S extends Releasable> {
788
+ private name;
789
+ private create;
790
+ private state;
791
+ private runs;
792
+ /**
793
+ * @param name - What the session runs, to name it in errors
794
+ * @param create - Creates the session, called once unless it fails
795
+ */
796
+ constructor(name: string, create: () => Promise<S>);
797
+ get status(): SessionState<S>['status'];
798
+ /** The session, loaded on the first call */
799
+ get(): Promise<S>;
800
+ /**
801
+ * Runs something on the session, which a release waits for
802
+ * @param run - Uses the session, and only until its promise settles
803
+ */
804
+ use<T>(run: (session: S) => Promise<T>): Promise<T>;
805
+ /** Releases the session, for good */
806
+ release(): Promise<void>;
807
+ private isLoading;
808
+ private releasedError;
809
+ }
810
+ /**
811
+ * Fetches each address once for as long as the app runs, however many models
812
+ * ask for it. A failed download is forgotten, so a later call tries again.
813
+ */
814
+ declare function createDownloadCache<T>(download: (url: string) => Promise<T>): (url: string) => Promise<T>;
815
+
758
816
  /**
759
817
  * Measures how loud the microphone is, the way a browser AnalyserNode does.
760
818
  *
@@ -870,7 +928,30 @@ interface MicdropOptions {
870
928
  debugLog?: boolean;
871
929
  reconnect?: MicdropReconnectOptions;
872
930
  }
931
+ /**
932
+ * Where the call stands.
933
+ *
934
+ * - `stopped`: no call, the microphone may still be on for a test
935
+ * - `starting`: opening the microphone and the connection
936
+ * - `started`: the call is on
937
+ * - `reconnecting`: the connection dropped, the call waits for a new one
938
+ */
939
+ type MicdropStatus = 'stopped' | 'starting' | 'started' | 'reconnecting';
940
+ /**
941
+ * Who has the floor, one at a time.
942
+ *
943
+ * - `idle`: nothing is heard nor awaited, when stopped, paused or muted
944
+ * - `listening`: waiting for the user to speak
945
+ * - `userSpeaking`: the user speaks
946
+ * - `processing`: the server prepares the answer
947
+ * - `assistantSpeaking`: the answer is playing
948
+ */
949
+ type MicdropActivity = 'idle' | 'listening' | 'userSpeaking' | 'processing' | 'assistantSpeaking';
873
950
  interface MicdropState {
951
+ /** Where the call stands, which `isStarting`, `isStarted` and `isReconnecting` spell out */
952
+ status: MicdropStatus;
953
+ /** Who has the floor, which `isListening`, `isUserSpeaking`, `isProcessing` and `isAssistantSpeaking` spell out */
954
+ activity: MicdropActivity;
874
955
  isStarting: boolean;
875
956
  isStarted: boolean;
876
957
  isMuted: boolean;
@@ -909,14 +990,17 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
909
990
  private _isProcessing;
910
991
  private _isMuted;
911
992
  private _isPaused;
912
- private _isReconnecting;
913
- private _isStartingCall;
993
+ private callStatus;
914
994
  private reconnectAttempt;
915
995
  private reconnectTimer?;
916
996
  private connectionTimer?;
917
997
  private echoGuard;
998
+ private pendingStop?;
918
999
  constructor(options?: MicdropOptions);
919
1000
  get vad(): VAD | undefined;
1001
+ get status(): MicdropStatus;
1002
+ get activity(): MicdropActivity;
1003
+ /** The call is on, reconnecting included */
920
1004
  get isStarted(): boolean;
921
1005
  get isStarting(): boolean;
922
1006
  get isReconnecting(): boolean;
@@ -944,6 +1028,19 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
944
1028
  get state(): MicdropState;
945
1029
  start: (options: MicdropOptions) => Promise<void>;
946
1030
  stop: () => Promise<void>;
1031
+ /** The assistant prepares or speaks an answer */
1032
+ private get isAnswering();
1033
+ /**
1034
+ * Stops the call once the assistant finished its answer, the one it is
1035
+ * still preparing included, such as the goodbye that follows an `EndCall`.
1036
+ * Stops right away when no answer is under way.
1037
+ *
1038
+ * A `start()` or a `stop()` meanwhile cancels the wait, so it never acts on
1039
+ * the next call.
1040
+ * @returns Resolves once the call is stopped, or the wait cancelled
1041
+ */
1042
+ stopAfterAnswer: () => Promise<void>;
1043
+ private cancelPendingStop;
947
1044
  mute: () => void;
948
1045
  unmute: () => void;
949
1046
  pause: () => void;
@@ -970,13 +1067,20 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
970
1067
  changeSpeakerDevice: (deviceId: string) => Promise<void>;
971
1068
  private stopMic;
972
1069
  private updateDevices;
973
- private startWS;
1070
+ /**
1071
+ * Opens the connection, unless one is already open or opening.
1072
+ *
1073
+ * The socket answers through handlers bound to it, so one left behind by a
1074
+ * stop or a reconnection never acts on the call that followed.
1075
+ */
1076
+ private openWS;
974
1077
  private onWSOpen;
975
1078
  private onWSMessage;
976
1079
  private onWSClose;
977
1080
  private onWSError;
978
1081
  private stopWS;
979
1082
  private scheduleReconnect;
1083
+ private clearReconnectTimer;
980
1084
  private onSpeakerStartPlaying;
981
1085
  private onSpeakerStopPlaying;
982
1086
  /**
@@ -1019,4 +1123,4 @@ declare enum MicdropStorageKeys {
1019
1123
  declare function setMicdropStorage(storage: MicdropStorage): void;
1020
1124
  declare const storage: MicdropStorage;
1021
1125
 
1022
- export { type AudioSink, CONFIRM_MARGIN, DEFAULT_TURN_MAX_WAIT, type DeepPartial, ECHO_TAIL, Mic, MicController, MicDriver, type MicDriverEvents, type MicEvents, MicRecorder, type MicRecorderEvents, type MicRecorderState, type MicSource, Micdrop, type MicdropAnswerMetadata, type MicdropCallSummary, type MicdropClassification, MicdropClient, MicdropClientCommands, MicdropClientError, MicdropClientErrorCode, type MicdropConversation, type MicdropConversationItem, type MicdropConversationMessage, type MicdropConversationToolCall, type MicdropConversationToolResult, type MicdropDevice, type MicdropEvents, type MicdropOptions, type MicdropReconnectOptions, MicdropServerCommands, type MicdropState, type MicdropStorage, MicdropStorageKeys, type MicdropToolCall, MultipleVAD, Pcm16AudioStream, type Pcm16AudioStreamEvents, SILERO_FRAME_SAMPLES, SILERO_SAMPLE_RATE, type ScheduledAudio, type SileroModel, type SileroModelLoader, SileroVAD, type SileroVADOptions, Speaker, SpeakerController, SpeakerDriver, type SpeakerDriverEvents, type SpeakerEvents, type TurnDetector, VAD, type VADConfig, type VADConfigName, type VADEvents, VADStatus, VOLUME_SAMPLE_RATE, VolumeMeter, VolumeVAD, type VolumeVADOptions, type WSCloseEvent, concatFloat32, currentTurn, equalVADConfig, floatToPcm16, getClientErrorFromWSCloseEvent, getClientErrorFromWSCloseEventCode, getTurnClassification, getVAD, getVolume, isRecoverableError, pcm16ToArrayBuffer, pcm16ToFloat, resample, setMicdropStorage, setSileroModelLoader, storage };
1126
+ export { type AudioSink, CONFIRM_MARGIN, DEFAULT_TURN_MAX_WAIT, type DeepPartial, ECHO_TAIL, LazySession, Mic, MicController, MicDriver, type MicDriverEvents, type MicEvents, MicRecorder, type MicRecorderEvents, type MicRecorderState, type MicSource, Micdrop, type MicdropActivity, type MicdropAnswerMetadata, type MicdropCallSummary, type MicdropClassification, MicdropClient, MicdropClientCommands, MicdropClientError, MicdropClientErrorCode, type MicdropConversation, type MicdropConversationItem, type MicdropConversationMessage, type MicdropConversationToolCall, type MicdropConversationToolResult, type MicdropDevice, type MicdropEvents, type MicdropOptions, type MicdropReconnectOptions, MicdropServerCommands, type MicdropState, type MicdropStatus, type MicdropStorage, MicdropStorageKeys, type MicdropToolCall, MultipleVAD, Pcm16AudioStream, type Pcm16AudioStreamEvents, type Releasable, SILERO_FRAME_SAMPLES, SILERO_SAMPLE_RATE, type ScheduledAudio, type SileroModel, type SileroModelLoader, SileroVAD, type SileroVADOptions, Speaker, SpeakerController, SpeakerDriver, type SpeakerDriverEvents, type SpeakerEvents, type TurnDetector, VAD, type VADConfig, type VADConfigName, type VADEvents, VADStatus, VOLUME_SAMPLE_RATE, VolumeMeter, VolumeVAD, type VolumeVADOptions, type WSCloseEvent, concatFloat32, createDownloadCache, currentTurn, equalVADConfig, floatToPcm16, getClientErrorFromWSCloseEvent, getClientErrorFromWSCloseEventCode, getTurnClassification, getVAD, getVolume, isRecoverableError, pcm16ToArrayBuffer, pcm16ToFloat, resample, setMicdropStorage, setSileroModelLoader, storage };
package/dist/index.d.ts CHANGED
@@ -703,6 +703,7 @@ declare class SileroVAD extends VAD {
703
703
  private silenceFrames;
704
704
  private processing;
705
705
  private round;
706
+ private startId;
706
707
  constructor(options?: Partial<SileroVADOptions>);
707
708
  get isStarted(): boolean;
708
709
  get isPaused(): boolean;
@@ -755,6 +756,63 @@ declare class VolumeVAD extends VAD {
755
756
  private reset;
756
757
  }
757
758
 
759
+ /** Anything a runtime hands back that has to be released once done with */
760
+ interface Releasable {
761
+ release(): Promise<void>;
762
+ }
763
+ type SessionState<S> = {
764
+ status: 'unloaded';
765
+ } | {
766
+ status: 'loading';
767
+ promise: Promise<S>;
768
+ } | {
769
+ status: 'loaded';
770
+ session: S;
771
+ } | {
772
+ status: 'released';
773
+ };
774
+ /**
775
+ * An inference session created on first use, and released once.
776
+ *
777
+ * Every caller asking while it loads shares the same load, so two of them
778
+ * never create two sessions and leak one. A release during the load disposes
779
+ * of the session as soon as it arrives instead of letting it come back to life,
780
+ * and waits for the runs in flight before freeing the runtime under them. A
781
+ * failed load is forgotten, so the next call tries again.
782
+ *
783
+ * Shared by the Silero models of `@micdrop/web` and `@micdrop/react-native`.
784
+ * `@micdrop/smart-turn` holds the same few lines for its own models, as it
785
+ * depends on no other package.
786
+ */
787
+ declare class LazySession<S extends Releasable> {
788
+ private name;
789
+ private create;
790
+ private state;
791
+ private runs;
792
+ /**
793
+ * @param name - What the session runs, to name it in errors
794
+ * @param create - Creates the session, called once unless it fails
795
+ */
796
+ constructor(name: string, create: () => Promise<S>);
797
+ get status(): SessionState<S>['status'];
798
+ /** The session, loaded on the first call */
799
+ get(): Promise<S>;
800
+ /**
801
+ * Runs something on the session, which a release waits for
802
+ * @param run - Uses the session, and only until its promise settles
803
+ */
804
+ use<T>(run: (session: S) => Promise<T>): Promise<T>;
805
+ /** Releases the session, for good */
806
+ release(): Promise<void>;
807
+ private isLoading;
808
+ private releasedError;
809
+ }
810
+ /**
811
+ * Fetches each address once for as long as the app runs, however many models
812
+ * ask for it. A failed download is forgotten, so a later call tries again.
813
+ */
814
+ declare function createDownloadCache<T>(download: (url: string) => Promise<T>): (url: string) => Promise<T>;
815
+
758
816
  /**
759
817
  * Measures how loud the microphone is, the way a browser AnalyserNode does.
760
818
  *
@@ -870,7 +928,30 @@ interface MicdropOptions {
870
928
  debugLog?: boolean;
871
929
  reconnect?: MicdropReconnectOptions;
872
930
  }
931
+ /**
932
+ * Where the call stands.
933
+ *
934
+ * - `stopped`: no call, the microphone may still be on for a test
935
+ * - `starting`: opening the microphone and the connection
936
+ * - `started`: the call is on
937
+ * - `reconnecting`: the connection dropped, the call waits for a new one
938
+ */
939
+ type MicdropStatus = 'stopped' | 'starting' | 'started' | 'reconnecting';
940
+ /**
941
+ * Who has the floor, one at a time.
942
+ *
943
+ * - `idle`: nothing is heard nor awaited, when stopped, paused or muted
944
+ * - `listening`: waiting for the user to speak
945
+ * - `userSpeaking`: the user speaks
946
+ * - `processing`: the server prepares the answer
947
+ * - `assistantSpeaking`: the answer is playing
948
+ */
949
+ type MicdropActivity = 'idle' | 'listening' | 'userSpeaking' | 'processing' | 'assistantSpeaking';
873
950
  interface MicdropState {
951
+ /** Where the call stands, which `isStarting`, `isStarted` and `isReconnecting` spell out */
952
+ status: MicdropStatus;
953
+ /** Who has the floor, which `isListening`, `isUserSpeaking`, `isProcessing` and `isAssistantSpeaking` spell out */
954
+ activity: MicdropActivity;
874
955
  isStarting: boolean;
875
956
  isStarted: boolean;
876
957
  isMuted: boolean;
@@ -909,14 +990,17 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
909
990
  private _isProcessing;
910
991
  private _isMuted;
911
992
  private _isPaused;
912
- private _isReconnecting;
913
- private _isStartingCall;
993
+ private callStatus;
914
994
  private reconnectAttempt;
915
995
  private reconnectTimer?;
916
996
  private connectionTimer?;
917
997
  private echoGuard;
998
+ private pendingStop?;
918
999
  constructor(options?: MicdropOptions);
919
1000
  get vad(): VAD | undefined;
1001
+ get status(): MicdropStatus;
1002
+ get activity(): MicdropActivity;
1003
+ /** The call is on, reconnecting included */
920
1004
  get isStarted(): boolean;
921
1005
  get isStarting(): boolean;
922
1006
  get isReconnecting(): boolean;
@@ -944,6 +1028,19 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
944
1028
  get state(): MicdropState;
945
1029
  start: (options: MicdropOptions) => Promise<void>;
946
1030
  stop: () => Promise<void>;
1031
+ /** The assistant prepares or speaks an answer */
1032
+ private get isAnswering();
1033
+ /**
1034
+ * Stops the call once the assistant finished its answer, the one it is
1035
+ * still preparing included, such as the goodbye that follows an `EndCall`.
1036
+ * Stops right away when no answer is under way.
1037
+ *
1038
+ * A `start()` or a `stop()` meanwhile cancels the wait, so it never acts on
1039
+ * the next call.
1040
+ * @returns Resolves once the call is stopped, or the wait cancelled
1041
+ */
1042
+ stopAfterAnswer: () => Promise<void>;
1043
+ private cancelPendingStop;
947
1044
  mute: () => void;
948
1045
  unmute: () => void;
949
1046
  pause: () => void;
@@ -970,13 +1067,20 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
970
1067
  changeSpeakerDevice: (deviceId: string) => Promise<void>;
971
1068
  private stopMic;
972
1069
  private updateDevices;
973
- private startWS;
1070
+ /**
1071
+ * Opens the connection, unless one is already open or opening.
1072
+ *
1073
+ * The socket answers through handlers bound to it, so one left behind by a
1074
+ * stop or a reconnection never acts on the call that followed.
1075
+ */
1076
+ private openWS;
974
1077
  private onWSOpen;
975
1078
  private onWSMessage;
976
1079
  private onWSClose;
977
1080
  private onWSError;
978
1081
  private stopWS;
979
1082
  private scheduleReconnect;
1083
+ private clearReconnectTimer;
980
1084
  private onSpeakerStartPlaying;
981
1085
  private onSpeakerStopPlaying;
982
1086
  /**
@@ -1019,4 +1123,4 @@ declare enum MicdropStorageKeys {
1019
1123
  declare function setMicdropStorage(storage: MicdropStorage): void;
1020
1124
  declare const storage: MicdropStorage;
1021
1125
 
1022
- export { type AudioSink, CONFIRM_MARGIN, DEFAULT_TURN_MAX_WAIT, type DeepPartial, ECHO_TAIL, Mic, MicController, MicDriver, type MicDriverEvents, type MicEvents, MicRecorder, type MicRecorderEvents, type MicRecorderState, type MicSource, Micdrop, type MicdropAnswerMetadata, type MicdropCallSummary, type MicdropClassification, MicdropClient, MicdropClientCommands, MicdropClientError, MicdropClientErrorCode, type MicdropConversation, type MicdropConversationItem, type MicdropConversationMessage, type MicdropConversationToolCall, type MicdropConversationToolResult, type MicdropDevice, type MicdropEvents, type MicdropOptions, type MicdropReconnectOptions, MicdropServerCommands, type MicdropState, type MicdropStorage, MicdropStorageKeys, type MicdropToolCall, MultipleVAD, Pcm16AudioStream, type Pcm16AudioStreamEvents, SILERO_FRAME_SAMPLES, SILERO_SAMPLE_RATE, type ScheduledAudio, type SileroModel, type SileroModelLoader, SileroVAD, type SileroVADOptions, Speaker, SpeakerController, SpeakerDriver, type SpeakerDriverEvents, type SpeakerEvents, type TurnDetector, VAD, type VADConfig, type VADConfigName, type VADEvents, VADStatus, VOLUME_SAMPLE_RATE, VolumeMeter, VolumeVAD, type VolumeVADOptions, type WSCloseEvent, concatFloat32, currentTurn, equalVADConfig, floatToPcm16, getClientErrorFromWSCloseEvent, getClientErrorFromWSCloseEventCode, getTurnClassification, getVAD, getVolume, isRecoverableError, pcm16ToArrayBuffer, pcm16ToFloat, resample, setMicdropStorage, setSileroModelLoader, storage };
1126
+ export { type AudioSink, CONFIRM_MARGIN, DEFAULT_TURN_MAX_WAIT, type DeepPartial, ECHO_TAIL, LazySession, Mic, MicController, MicDriver, type MicDriverEvents, type MicEvents, MicRecorder, type MicRecorderEvents, type MicRecorderState, type MicSource, Micdrop, type MicdropActivity, type MicdropAnswerMetadata, type MicdropCallSummary, type MicdropClassification, MicdropClient, MicdropClientCommands, MicdropClientError, MicdropClientErrorCode, type MicdropConversation, type MicdropConversationItem, type MicdropConversationMessage, type MicdropConversationToolCall, type MicdropConversationToolResult, type MicdropDevice, type MicdropEvents, type MicdropOptions, type MicdropReconnectOptions, MicdropServerCommands, type MicdropState, type MicdropStatus, type MicdropStorage, MicdropStorageKeys, type MicdropToolCall, MultipleVAD, Pcm16AudioStream, type Pcm16AudioStreamEvents, type Releasable, SILERO_FRAME_SAMPLES, SILERO_SAMPLE_RATE, type ScheduledAudio, type SileroModel, type SileroModelLoader, SileroVAD, type SileroVADOptions, Speaker, SpeakerController, SpeakerDriver, type SpeakerDriverEvents, type SpeakerEvents, type TurnDetector, VAD, type VADConfig, type VADConfigName, type VADEvents, VADStatus, VOLUME_SAMPLE_RATE, VolumeMeter, VolumeVAD, type VolumeVADOptions, type WSCloseEvent, concatFloat32, createDownloadCache, currentTurn, equalVADConfig, floatToPcm16, getClientErrorFromWSCloseEvent, getClientErrorFromWSCloseEventCode, getTurnClassification, getVAD, getVolume, isRecoverableError, pcm16ToArrayBuffer, pcm16ToFloat, resample, setMicdropStorage, setSileroModelLoader, storage };