@micdrop/client 2.1.2 → 2.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
@@ -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
 
@@ -999,8 +1000,15 @@ function getClientErrorFromWSCloseEventCode(code) {
999
1000
  return void 0;
1000
1001
  }
1001
1002
  }
1003
+ function isRecoverableError(error) {
1004
+ return error.code === "Connection" /* Connection */ || error.code === "InternalServer" /* InternalServer */ || error.code === "Unknown" /* Unknown */;
1005
+ }
1002
1006
 
1003
1007
  // src/client/MicdropClient.ts
1008
+ var DEFAULT_RECONNECT_OPTIONS = {
1009
+ maxAttempts: Infinity,
1010
+ delayMs: 1e3
1011
+ };
1004
1012
  var MicdropClient = class extends import_eventemitter36.EventEmitter {
1005
1013
  constructor(options = {}) {
1006
1014
  super();
@@ -1013,6 +1021,8 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
1013
1021
  this._isProcessing = false;
1014
1022
  this._isMuted = false;
1015
1023
  this._isPaused = false;
1024
+ this._isReconnecting = false;
1025
+ this.reconnectAttempt = 0;
1016
1026
  this.start = async (options) => {
1017
1027
  this.error = void 0;
1018
1028
  this.options = { ...this.options, ...options };
@@ -1021,6 +1031,12 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
1021
1031
  this._isProcessing = true;
1022
1032
  this._isMuted = false;
1023
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
+ }
1024
1040
  if (!this.micRecorder || options?.vad) {
1025
1041
  await this.startMic({ vad: options?.vad });
1026
1042
  }
@@ -1030,6 +1046,11 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
1030
1046
  this._isProcessing = false;
1031
1047
  this._isMuted = false;
1032
1048
  this._isPaused = false;
1049
+ this._isReconnecting = false;
1050
+ if (this.reconnectTimer) {
1051
+ window.clearTimeout(this.reconnectTimer);
1052
+ this.reconnectTimer = void 0;
1053
+ }
1033
1054
  try {
1034
1055
  this.stopWS();
1035
1056
  } catch (error) {
@@ -1159,10 +1180,16 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
1159
1180
  };
1160
1181
  this.onWSOpen = () => {
1161
1182
  this.log("WebSocket opened");
1183
+ this._isReconnecting = false;
1184
+ this.reconnectAttempt = 0;
1185
+ this.error = void 0;
1162
1186
  this.notifyStateChange();
1163
1187
  if (this.options.params) {
1164
1188
  this.ws?.send(JSON.stringify(this.options.params));
1165
1189
  }
1190
+ if (!this.isPaused && !this.isMuted) {
1191
+ this.vad?.resume?.();
1192
+ }
1166
1193
  };
1167
1194
  this.onWSMessage = (event) => {
1168
1195
  this.log("Received message:", event.data);
@@ -1219,15 +1246,22 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
1219
1246
  };
1220
1247
  this.onWSClose = (event) => {
1221
1248
  this.log("WebSocket closed", event);
1222
- this.stop();
1223
1249
  const error = getClientErrorFromWSCloseEvent(event);
1224
1250
  if (error) {
1225
1251
  this.setError(error);
1226
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
+ }
1227
1262
  };
1228
1263
  this.onWSError = (event) => {
1229
1264
  this.setError(new MicdropClientError("Connection" /* Connection */));
1230
- this.stop();
1231
1265
  };
1232
1266
  this.onSpeakerStartPlaying = () => {
1233
1267
  this.log("Speaker started");
@@ -1238,8 +1272,12 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
1238
1272
  };
1239
1273
  this.onSpeakerStopPlaying = () => {
1240
1274
  this.log("Speaker stopped");
1241
- if (this.options.disableInterruption && !this.isMuted) {
1242
- this.vad?.resume();
1275
+ if (this.options.disableInterruption) {
1276
+ setTimeout(() => {
1277
+ if (!this.isMuted) {
1278
+ this.vad?.resume();
1279
+ }
1280
+ }, 200);
1243
1281
  }
1244
1282
  this.notifyStateChange();
1245
1283
  };
@@ -1251,10 +1289,13 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
1251
1289
  return this.micRecorder?.vad;
1252
1290
  }
1253
1291
  get isStarted() {
1254
- return this.isWSStarted && this.micRecorder?.state.isStarted || false;
1292
+ return (this.isWSStarted || this._isReconnecting) && (this.micRecorder?.state.isStarted || false);
1255
1293
  }
1256
1294
  get isStarting() {
1257
- return this.isWSStarting || this.micRecorder?.state.isStarting || false;
1295
+ return (this.isWSStarting || this.micRecorder?.state.isStarting || false) && !this._isReconnecting;
1296
+ }
1297
+ get isReconnecting() {
1298
+ return this._isReconnecting;
1258
1299
  }
1259
1300
  get isMuted() {
1260
1301
  return this._isMuted;
@@ -1296,6 +1337,7 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
1296
1337
  return {
1297
1338
  isStarting: this.isStarting,
1298
1339
  isStarted: this.isStarted,
1340
+ isReconnecting: this.isReconnecting,
1299
1341
  isMuted: this.isMuted,
1300
1342
  isPaused: this.isPaused,
1301
1343
  isListening: this.isListening,
@@ -1357,6 +1399,30 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
1357
1399
  this.ws = void 0;
1358
1400
  this.notifyStateChange();
1359
1401
  }
1402
+ scheduleReconnect(config) {
1403
+ if (this.reconnectAttempt >= config.maxAttempts) {
1404
+ this.stop();
1405
+ this.setError(new MicdropClientError("Connection" /* Connection */));
1406
+ return;
1407
+ }
1408
+ this.reconnectAttempt++;
1409
+ this.log(
1410
+ `Reconnecting in ${config.delayMs}ms (attempt ${this.reconnectAttempt})`
1411
+ );
1412
+ if (!this.isPaused && !this.isMuted) {
1413
+ this.vad?.pause?.();
1414
+ }
1415
+ if (this.reconnectTimer) {
1416
+ window.clearTimeout(this.reconnectTimer);
1417
+ }
1418
+ this.reconnectTimer = window.setTimeout(async () => {
1419
+ try {
1420
+ await this.startWS();
1421
+ } catch {
1422
+ this.scheduleReconnect(config);
1423
+ }
1424
+ }, config.delayMs);
1425
+ }
1360
1426
  addMessage(message) {
1361
1427
  this.conversation = [...this.conversation, message];
1362
1428
  this.notifyStateChange();
@@ -1403,6 +1469,7 @@ var Micdrop = window.micdropClient;
1403
1469
  equalVADConfig,
1404
1470
  getClientErrorFromWSCloseEvent,
1405
1471
  getClientErrorFromWSCloseEventCode,
1406
- getVAD
1472
+ getVAD,
1473
+ isRecoverableError
1407
1474
  });
1408
1475
  //# sourceMappingURL=index.js.map