@micdrop/client 2.1.1 → 2.2.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
@@ -298,6 +298,7 @@ declare class MicdropClientError extends Error {
298
298
  }
299
299
  declare function getClientErrorFromWSCloseEvent(event: CloseEvent): MicdropClientError | undefined;
300
300
  declare function getClientErrorFromWSCloseEventCode(code: number): MicdropClientErrorCode | undefined;
301
+ declare function isRecoverableError(error: MicdropClientError): boolean;
301
302
 
302
303
  interface MicdropEvents {
303
304
  EndCall: [];
@@ -305,18 +306,24 @@ interface MicdropEvents {
305
306
  StateChange: [MicdropState, MicdropState];
306
307
  ToolCall: [MicdropToolCall];
307
308
  }
309
+ interface MicdropReconnectOptions {
310
+ maxAttempts?: number;
311
+ delayMs?: number;
312
+ }
308
313
  interface MicdropOptions {
309
314
  url?: string;
310
315
  params?: Record<string, any>;
311
316
  vad?: VADConfig;
312
317
  disableInterruption?: boolean;
313
318
  debugLog?: boolean;
319
+ reconnect?: MicdropReconnectOptions;
314
320
  }
315
321
  interface MicdropState {
316
322
  isStarting: boolean;
317
323
  isStarted: boolean;
318
324
  isMuted: boolean;
319
325
  isPaused: boolean;
326
+ isReconnecting: boolean;
320
327
  isListening: boolean;
321
328
  isProcessing: boolean;
322
329
  isUserSpeaking: boolean;
@@ -344,10 +351,14 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
344
351
  private _isProcessing;
345
352
  private _isMuted;
346
353
  private _isPaused;
354
+ private _isReconnecting;
355
+ private reconnectAttempt;
356
+ private reconnectTimer?;
347
357
  constructor(options?: MicdropOptions);
348
358
  get vad(): VAD | undefined;
349
359
  get isStarted(): boolean;
350
360
  get isStarting(): boolean;
361
+ get isReconnecting(): boolean;
351
362
  get isMuted(): boolean;
352
363
  get isPaused(): boolean;
353
364
  get isProcessing(): boolean;
@@ -361,7 +372,7 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
361
372
  get micDeviceId(): string | undefined;
362
373
  get speakerDeviceId(): string | undefined;
363
374
  get state(): MicdropState;
364
- start: (options?: MicdropOptions) => Promise<void>;
375
+ start: (options: MicdropOptions) => Promise<void>;
365
376
  stop: () => Promise<void>;
366
377
  mute: () => void;
367
378
  unmute: () => void;
@@ -381,6 +392,7 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
381
392
  private onWSClose;
382
393
  private onWSError;
383
394
  private stopWS;
395
+ private scheduleReconnect;
384
396
  private onSpeakerStartPlaying;
385
397
  private onSpeakerStopPlaying;
386
398
  private addMessage;
@@ -391,4 +403,4 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
391
403
 
392
404
  declare const Micdrop: MicdropClient;
393
405
 
394
- export { type DeepPartial, LocalStorageKeys, Mic, MicRecorder, type MicRecorderEvents, type MicRecorderState, Micdrop, type MicdropAnswerMetadata, type MicdropCallSummary, MicdropClient, MicdropClientCommands, MicdropClientError, MicdropClientErrorCode, type MicdropConversation, type MicdropConversationItem, type MicdropConversationMessage, type MicdropConversationToolCall, type MicdropConversationToolResult, type MicdropEvents, type MicdropOptions, MicdropServerCommands, type MicdropState, type MicdropToolCall, MultipleVAD, SileroVAD, type SileroVADOptions, Speaker, VAD, type VADConfig, type VADConfigName, type VADEvents, VADStatus, VolumeVAD, type VolumeVADOptions, equalVADConfig, getClientErrorFromWSCloseEvent, getClientErrorFromWSCloseEventCode, getVAD };
406
+ export { type DeepPartial, LocalStorageKeys, Mic, MicRecorder, type MicRecorderEvents, type MicRecorderState, Micdrop, type MicdropAnswerMetadata, type MicdropCallSummary, MicdropClient, MicdropClientCommands, MicdropClientError, MicdropClientErrorCode, type MicdropConversation, type MicdropConversationItem, type MicdropConversationMessage, type MicdropConversationToolCall, type MicdropConversationToolResult, type MicdropEvents, type MicdropOptions, type MicdropReconnectOptions, MicdropServerCommands, type MicdropState, type MicdropToolCall, MultipleVAD, SileroVAD, type SileroVADOptions, Speaker, VAD, type VADConfig, type VADConfigName, type VADEvents, VADStatus, VolumeVAD, type VolumeVADOptions, equalVADConfig, getClientErrorFromWSCloseEvent, getClientErrorFromWSCloseEventCode, getVAD, isRecoverableError };
package/dist/index.d.ts CHANGED
@@ -298,6 +298,7 @@ declare class MicdropClientError extends Error {
298
298
  }
299
299
  declare function getClientErrorFromWSCloseEvent(event: CloseEvent): MicdropClientError | undefined;
300
300
  declare function getClientErrorFromWSCloseEventCode(code: number): MicdropClientErrorCode | undefined;
301
+ declare function isRecoverableError(error: MicdropClientError): boolean;
301
302
 
302
303
  interface MicdropEvents {
303
304
  EndCall: [];
@@ -305,18 +306,24 @@ interface MicdropEvents {
305
306
  StateChange: [MicdropState, MicdropState];
306
307
  ToolCall: [MicdropToolCall];
307
308
  }
309
+ interface MicdropReconnectOptions {
310
+ maxAttempts?: number;
311
+ delayMs?: number;
312
+ }
308
313
  interface MicdropOptions {
309
314
  url?: string;
310
315
  params?: Record<string, any>;
311
316
  vad?: VADConfig;
312
317
  disableInterruption?: boolean;
313
318
  debugLog?: boolean;
319
+ reconnect?: MicdropReconnectOptions;
314
320
  }
315
321
  interface MicdropState {
316
322
  isStarting: boolean;
317
323
  isStarted: boolean;
318
324
  isMuted: boolean;
319
325
  isPaused: boolean;
326
+ isReconnecting: boolean;
320
327
  isListening: boolean;
321
328
  isProcessing: boolean;
322
329
  isUserSpeaking: boolean;
@@ -344,10 +351,14 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
344
351
  private _isProcessing;
345
352
  private _isMuted;
346
353
  private _isPaused;
354
+ private _isReconnecting;
355
+ private reconnectAttempt;
356
+ private reconnectTimer?;
347
357
  constructor(options?: MicdropOptions);
348
358
  get vad(): VAD | undefined;
349
359
  get isStarted(): boolean;
350
360
  get isStarting(): boolean;
361
+ get isReconnecting(): boolean;
351
362
  get isMuted(): boolean;
352
363
  get isPaused(): boolean;
353
364
  get isProcessing(): boolean;
@@ -361,7 +372,7 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
361
372
  get micDeviceId(): string | undefined;
362
373
  get speakerDeviceId(): string | undefined;
363
374
  get state(): MicdropState;
364
- start: (options?: MicdropOptions) => Promise<void>;
375
+ start: (options: MicdropOptions) => Promise<void>;
365
376
  stop: () => Promise<void>;
366
377
  mute: () => void;
367
378
  unmute: () => void;
@@ -381,6 +392,7 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
381
392
  private onWSClose;
382
393
  private onWSError;
383
394
  private stopWS;
395
+ private scheduleReconnect;
384
396
  private onSpeakerStartPlaying;
385
397
  private onSpeakerStopPlaying;
386
398
  private addMessage;
@@ -391,4 +403,4 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
391
403
 
392
404
  declare const Micdrop: MicdropClient;
393
405
 
394
- export { type DeepPartial, LocalStorageKeys, Mic, MicRecorder, type MicRecorderEvents, type MicRecorderState, Micdrop, type MicdropAnswerMetadata, type MicdropCallSummary, MicdropClient, MicdropClientCommands, MicdropClientError, MicdropClientErrorCode, type MicdropConversation, type MicdropConversationItem, type MicdropConversationMessage, type MicdropConversationToolCall, type MicdropConversationToolResult, type MicdropEvents, type MicdropOptions, MicdropServerCommands, type MicdropState, type MicdropToolCall, MultipleVAD, SileroVAD, type SileroVADOptions, Speaker, VAD, type VADConfig, type VADConfigName, type VADEvents, VADStatus, VolumeVAD, type VolumeVADOptions, equalVADConfig, getClientErrorFromWSCloseEvent, getClientErrorFromWSCloseEventCode, getVAD };
406
+ export { type DeepPartial, LocalStorageKeys, Mic, MicRecorder, type MicRecorderEvents, type MicRecorderState, Micdrop, type MicdropAnswerMetadata, type MicdropCallSummary, MicdropClient, MicdropClientCommands, MicdropClientError, MicdropClientErrorCode, type MicdropConversation, type MicdropConversationItem, type MicdropConversationMessage, type MicdropConversationToolCall, type MicdropConversationToolResult, type MicdropEvents, type MicdropOptions, type MicdropReconnectOptions, MicdropServerCommands, type MicdropState, type MicdropToolCall, MultipleVAD, SileroVAD, type SileroVADOptions, Speaker, VAD, type VADConfig, type VADConfigName, type VADEvents, VADStatus, VolumeVAD, type VolumeVADOptions, equalVADConfig, getClientErrorFromWSCloseEvent, getClientErrorFromWSCloseEventCode, getVAD, isRecoverableError };
package/dist/index.js CHANGED
@@ -38,7 +38,8 @@ __export(index_exports, {
38
38
  equalVADConfig: () => equalVADConfig,
39
39
  getClientErrorFromWSCloseEvent: () => getClientErrorFromWSCloseEvent,
40
40
  getClientErrorFromWSCloseEventCode: () => getClientErrorFromWSCloseEventCode,
41
- getVAD: () => getVAD
41
+ getVAD: () => getVAD,
42
+ isRecoverableError: () => isRecoverableError
42
43
  });
43
44
  module.exports = __toCommonJS(index_exports);
44
45
 
@@ -699,6 +700,14 @@ var VolumeVAD = class extends VAD {
699
700
  if (!this.running) return;
700
701
  this.running = false;
701
702
  this.resetHistory();
703
+ switch (this.status) {
704
+ case 2 /* Speaking */:
705
+ this.emit("StopSpeaking");
706
+ break;
707
+ case 1 /* MaybeSpeaking */:
708
+ this.emit("CancelSpeaking");
709
+ break;
710
+ }
702
711
  if (this.sourceNode) {
703
712
  this.sourceNode.disconnect();
704
713
  this.sourceNode = void 0;
@@ -991,8 +1000,15 @@ function getClientErrorFromWSCloseEventCode(code) {
991
1000
  return void 0;
992
1001
  }
993
1002
  }
1003
+ function isRecoverableError(error) {
1004
+ return error.code === "Connection" /* Connection */ || error.code === "InternalServer" /* InternalServer */ || error.code === "Unknown" /* Unknown */;
1005
+ }
994
1006
 
995
1007
  // src/client/MicdropClient.ts
1008
+ var DEFAULT_RECONNECT_OPTIONS = {
1009
+ maxAttempts: Infinity,
1010
+ delayMs: 1e3
1011
+ };
996
1012
  var MicdropClient = class extends import_eventemitter36.EventEmitter {
997
1013
  constructor(options = {}) {
998
1014
  super();
@@ -1005,6 +1021,8 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
1005
1021
  this._isProcessing = false;
1006
1022
  this._isMuted = false;
1007
1023
  this._isPaused = false;
1024
+ this._isReconnecting = false;
1025
+ this.reconnectAttempt = 0;
1008
1026
  this.start = async (options) => {
1009
1027
  this.error = void 0;
1010
1028
  this.options = { ...this.options, ...options };
@@ -1013,6 +1031,12 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
1013
1031
  this._isProcessing = true;
1014
1032
  this._isMuted = false;
1015
1033
  this._isPaused = false;
1034
+ this._isReconnecting = false;
1035
+ this.reconnectAttempt = 0;
1036
+ if (this.reconnectTimer) {
1037
+ window.clearTimeout(this.reconnectTimer);
1038
+ this.reconnectTimer = void 0;
1039
+ }
1016
1040
  if (!this.micRecorder || options?.vad) {
1017
1041
  await this.startMic({ vad: options?.vad });
1018
1042
  }
@@ -1022,6 +1046,11 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
1022
1046
  this._isProcessing = false;
1023
1047
  this._isMuted = false;
1024
1048
  this._isPaused = false;
1049
+ this._isReconnecting = false;
1050
+ if (this.reconnectTimer) {
1051
+ window.clearTimeout(this.reconnectTimer);
1052
+ this.reconnectTimer = void 0;
1053
+ }
1025
1054
  try {
1026
1055
  this.stopWS();
1027
1056
  } catch (error) {
@@ -1151,10 +1180,16 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
1151
1180
  };
1152
1181
  this.onWSOpen = () => {
1153
1182
  this.log("WebSocket opened");
1183
+ this._isReconnecting = false;
1184
+ this.reconnectAttempt = 0;
1185
+ this.error = void 0;
1154
1186
  this.notifyStateChange();
1155
1187
  if (this.options.params) {
1156
1188
  this.ws?.send(JSON.stringify(this.options.params));
1157
1189
  }
1190
+ if (!this.isPaused && !this.isMuted) {
1191
+ this.vad?.resume?.();
1192
+ }
1158
1193
  };
1159
1194
  this.onWSMessage = (event) => {
1160
1195
  this.log("Received message:", event.data);
@@ -1211,15 +1246,22 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
1211
1246
  };
1212
1247
  this.onWSClose = (event) => {
1213
1248
  this.log("WebSocket closed", event);
1214
- this.stop();
1215
1249
  const error = getClientErrorFromWSCloseEvent(event);
1216
1250
  if (error) {
1217
1251
  this.setError(error);
1218
1252
  }
1253
+ const config = { ...DEFAULT_RECONNECT_OPTIONS, ...this.options.reconnect };
1254
+ const canReconnect = error && isRecoverableError(error) && config.maxAttempts > 0;
1255
+ if (canReconnect) {
1256
+ this._isReconnecting = true;
1257
+ this.stopWS();
1258
+ this.scheduleReconnect(config);
1259
+ } else {
1260
+ this.stop();
1261
+ }
1219
1262
  };
1220
1263
  this.onWSError = (event) => {
1221
1264
  this.setError(new MicdropClientError("Connection" /* Connection */));
1222
- this.stop();
1223
1265
  };
1224
1266
  this.onSpeakerStartPlaying = () => {
1225
1267
  this.log("Speaker started");
@@ -1243,10 +1285,13 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
1243
1285
  return this.micRecorder?.vad;
1244
1286
  }
1245
1287
  get isStarted() {
1246
- return this.isWSStarted && this.micRecorder?.state.isStarted || false;
1288
+ return (this.isWSStarted || this._isReconnecting) && (this.micRecorder?.state.isStarted || false);
1247
1289
  }
1248
1290
  get isStarting() {
1249
- return this.isWSStarting || this.micRecorder?.state.isStarting || false;
1291
+ return (this.isWSStarting || this.micRecorder?.state.isStarting || false) && !this._isReconnecting;
1292
+ }
1293
+ get isReconnecting() {
1294
+ return this._isReconnecting;
1250
1295
  }
1251
1296
  get isMuted() {
1252
1297
  return this._isMuted;
@@ -1288,6 +1333,7 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
1288
1333
  return {
1289
1334
  isStarting: this.isStarting,
1290
1335
  isStarted: this.isStarted,
1336
+ isReconnecting: this.isReconnecting,
1291
1337
  isMuted: this.isMuted,
1292
1338
  isPaused: this.isPaused,
1293
1339
  isListening: this.isListening,
@@ -1349,6 +1395,30 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
1349
1395
  this.ws = void 0;
1350
1396
  this.notifyStateChange();
1351
1397
  }
1398
+ scheduleReconnect(config) {
1399
+ if (this.reconnectAttempt >= config.maxAttempts) {
1400
+ this.stop();
1401
+ this.setError(new MicdropClientError("Connection" /* Connection */));
1402
+ return;
1403
+ }
1404
+ this.reconnectAttempt++;
1405
+ this.log(
1406
+ `Reconnecting in ${config.delayMs}ms (attempt ${this.reconnectAttempt})`
1407
+ );
1408
+ if (!this.isPaused && !this.isMuted) {
1409
+ this.vad?.pause?.();
1410
+ }
1411
+ if (this.reconnectTimer) {
1412
+ window.clearTimeout(this.reconnectTimer);
1413
+ }
1414
+ this.reconnectTimer = window.setTimeout(async () => {
1415
+ try {
1416
+ await this.startWS();
1417
+ } catch {
1418
+ this.scheduleReconnect(config);
1419
+ }
1420
+ }, config.delayMs);
1421
+ }
1352
1422
  addMessage(message) {
1353
1423
  this.conversation = [...this.conversation, message];
1354
1424
  this.notifyStateChange();
@@ -1395,6 +1465,7 @@ var Micdrop = window.micdropClient;
1395
1465
  equalVADConfig,
1396
1466
  getClientErrorFromWSCloseEvent,
1397
1467
  getClientErrorFromWSCloseEventCode,
1398
- getVAD
1468
+ getVAD,
1469
+ isRecoverableError
1399
1470
  });
1400
1471
  //# sourceMappingURL=index.js.map