@micdrop/client 2.1.2 → 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 +14 -2
- package/dist/index.d.ts +14 -2
- package/dist/index.js +69 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +67 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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
|
|
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
|
|
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");
|
|
@@ -1251,10 +1285,13 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
|
|
|
1251
1285
|
return this.micRecorder?.vad;
|
|
1252
1286
|
}
|
|
1253
1287
|
get isStarted() {
|
|
1254
|
-
return this.isWSStarted && this.micRecorder?.state.isStarted || false;
|
|
1288
|
+
return (this.isWSStarted || this._isReconnecting) && (this.micRecorder?.state.isStarted || false);
|
|
1255
1289
|
}
|
|
1256
1290
|
get isStarting() {
|
|
1257
|
-
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;
|
|
1258
1295
|
}
|
|
1259
1296
|
get isMuted() {
|
|
1260
1297
|
return this._isMuted;
|
|
@@ -1296,6 +1333,7 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
|
|
|
1296
1333
|
return {
|
|
1297
1334
|
isStarting: this.isStarting,
|
|
1298
1335
|
isStarted: this.isStarted,
|
|
1336
|
+
isReconnecting: this.isReconnecting,
|
|
1299
1337
|
isMuted: this.isMuted,
|
|
1300
1338
|
isPaused: this.isPaused,
|
|
1301
1339
|
isListening: this.isListening,
|
|
@@ -1357,6 +1395,30 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
|
|
|
1357
1395
|
this.ws = void 0;
|
|
1358
1396
|
this.notifyStateChange();
|
|
1359
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
|
+
}
|
|
1360
1422
|
addMessage(message) {
|
|
1361
1423
|
this.conversation = [...this.conversation, message];
|
|
1362
1424
|
this.notifyStateChange();
|
|
@@ -1403,6 +1465,7 @@ var Micdrop = window.micdropClient;
|
|
|
1403
1465
|
equalVADConfig,
|
|
1404
1466
|
getClientErrorFromWSCloseEvent,
|
|
1405
1467
|
getClientErrorFromWSCloseEventCode,
|
|
1406
|
-
getVAD
|
|
1468
|
+
getVAD,
|
|
1469
|
+
isRecoverableError
|
|
1407
1470
|
});
|
|
1408
1471
|
//# sourceMappingURL=index.js.map
|