@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.mjs CHANGED
@@ -609,6 +609,9 @@ var SileroVAD = class extends VAD {
609
609
  // Bumped whenever the state is reset, so a window scored before that is
610
610
  // dropped rather than reopening a turn
611
611
  this.round = 0;
612
+ // Bumped by every start and stop, so a start overtaken while its model
613
+ // loaded lets go of it
614
+ this.startId = 0;
612
615
  this.onFrames = (frames, sampleRate) => {
613
616
  if (this._isPaused || !this.model) return;
614
617
  if (sampleRate !== this.bufferRate) {
@@ -644,19 +647,33 @@ var SileroVAD = class extends VAD {
644
647
  "No Silero model. Add `import '@micdrop/web/silero'` in a browser or `import '@micdrop/react-native/silero'` on a phone, or call setSileroModelLoader()."
645
648
  );
646
649
  }
647
- this.model = await loadModel();
650
+ this.mic = mic;
651
+ const startId = ++this.startId;
652
+ let model;
653
+ try {
654
+ model = await loadModel();
655
+ } catch (error) {
656
+ if (startId === this.startId) this.mic = void 0;
657
+ throw error;
658
+ }
659
+ if (startId !== this.startId) {
660
+ await model.release();
661
+ return;
662
+ }
663
+ this.model = model;
648
664
  this.resetState();
649
665
  this.setStatus(0 /* Silence */);
650
- this.mic = mic;
651
666
  mic.on("Frames", this.onFrames);
652
667
  }
653
668
  async stop() {
669
+ this.startId++;
654
670
  this.mic?.off("Frames", this.onFrames);
655
671
  this.mic = void 0;
656
672
  this.closeOpenTurn();
657
673
  this.resetState();
658
- await this.model?.release();
674
+ const model = this.model;
659
675
  this.model = void 0;
676
+ await model?.release();
660
677
  }
661
678
  async pause() {
662
679
  if (this._isPaused) return;
@@ -976,6 +993,9 @@ var MicRecorder = class extends EventEmitter4 {
976
993
  isStarted: false,
977
994
  isSpeaking: false
978
995
  });
996
+ if (this.turnOpen) {
997
+ this.endTurn();
998
+ }
979
999
  this.cancelPendingClose();
980
1000
  try {
981
1001
  this.mic?.off("Frames", this.onFrames);
@@ -1466,6 +1486,92 @@ var SpeakerDriver = class extends EventEmitter6 {
1466
1486
  }
1467
1487
  };
1468
1488
 
1489
+ // src/audio/vad/lazySession.ts
1490
+ var LazySession = class {
1491
+ /**
1492
+ * @param name - What the session runs, to name it in errors
1493
+ * @param create - Creates the session, called once unless it fails
1494
+ */
1495
+ constructor(name, create) {
1496
+ this.name = name;
1497
+ this.create = create;
1498
+ this.state = { status: "unloaded" };
1499
+ this.runs = /* @__PURE__ */ new Set();
1500
+ }
1501
+ get status() {
1502
+ return this.state.status;
1503
+ }
1504
+ /** The session, loaded on the first call */
1505
+ get() {
1506
+ const state = this.state;
1507
+ if (state.status === "loaded") return Promise.resolve(state.session);
1508
+ if (state.status === "loading") return state.promise;
1509
+ if (state.status === "released") return Promise.reject(this.releasedError());
1510
+ const promise = this.create().then(
1511
+ async (session) => {
1512
+ if (this.isLoading(promise)) {
1513
+ this.state = { status: "loaded", session };
1514
+ return session;
1515
+ }
1516
+ await session.release();
1517
+ throw this.releasedError();
1518
+ },
1519
+ (error) => {
1520
+ if (this.isLoading(promise)) this.state = { status: "unloaded" };
1521
+ throw error;
1522
+ }
1523
+ );
1524
+ this.state = { status: "loading", promise };
1525
+ return promise;
1526
+ }
1527
+ /**
1528
+ * Runs something on the session, which a release waits for
1529
+ * @param run - Uses the session, and only until its promise settles
1530
+ */
1531
+ async use(run) {
1532
+ const session = await this.get();
1533
+ if (this.state.status !== "loaded" || this.state.session !== session) {
1534
+ throw this.releasedError();
1535
+ }
1536
+ const pending = run(session);
1537
+ this.runs.add(pending);
1538
+ try {
1539
+ return await pending;
1540
+ } finally {
1541
+ this.runs.delete(pending);
1542
+ }
1543
+ }
1544
+ /** Releases the session, for good */
1545
+ async release() {
1546
+ const state = this.state;
1547
+ this.state = { status: "released" };
1548
+ if (state.status !== "loaded") return;
1549
+ await Promise.allSettled(this.runs);
1550
+ await state.session.release();
1551
+ }
1552
+ isLoading(promise) {
1553
+ return this.state.status === "loading" && this.state.promise === promise;
1554
+ }
1555
+ releasedError() {
1556
+ return new Error(`The ${this.name} model was released`);
1557
+ }
1558
+ };
1559
+ function createDownloadCache(download) {
1560
+ const downloads = /* @__PURE__ */ new Map();
1561
+ return (url) => {
1562
+ let pending = downloads.get(url);
1563
+ if (!pending) {
1564
+ const started = download(url);
1565
+ downloads.set(url, started);
1566
+ started.catch(() => {
1567
+ if (downloads.get(url) === started) downloads.delete(url);
1568
+ });
1569
+ pending = started;
1570
+ }
1571
+ return pending;
1572
+ };
1573
+ }
1574
+
1469
1575
  // src/audio/index.ts
1470
1576
  var globalScope = globalThis;
1471
1577
  if (!globalScope.micdropMic) {
@@ -1627,13 +1733,13 @@ var MicdropClient = class extends EventEmitter7 {
1627
1733
  this._isProcessing = false;
1628
1734
  this._isMuted = false;
1629
1735
  this._isPaused = false;
1630
- this._isReconnecting = false;
1631
- // From start() until the connection opens, the microphone included: opening
1632
- // it can take a while, for the permission prompt or the browser itself
1633
- this._isStartingCall = false;
1736
+ // The call as start() and the connection leave it. The public status also
1737
+ // weighs the microphone, which a device change restarts during a call.
1738
+ this.callStatus = "stopped";
1634
1739
  this.reconnectAttempt = 0;
1635
1740
  this.echoGuard = new EchoGuard();
1636
1741
  this.start = async (options) => {
1742
+ this.cancelPendingStop();
1637
1743
  this.error = void 0;
1638
1744
  this.options = { ...this.options, ...options };
1639
1745
  this.startTime = Date.now();
@@ -1642,34 +1748,36 @@ var MicdropClient = class extends EventEmitter7 {
1642
1748
  this._isProcessing = true;
1643
1749
  this._isMuted = false;
1644
1750
  this._isPaused = false;
1645
- this._isReconnecting = false;
1646
- this._isStartingCall = true;
1751
+ this.callStatus = "starting";
1647
1752
  this.reconnectAttempt = 0;
1648
- if (this.reconnectTimer) {
1649
- clearTimeout(this.reconnectTimer);
1650
- this.reconnectTimer = void 0;
1651
- }
1753
+ this.clearReconnectTimer();
1652
1754
  this.notifyStateChange();
1653
- if (!this.micRecorder || options?.vad) {
1654
- await this.startMic({ vad: options?.vad });
1755
+ try {
1756
+ if (!this.micRecorder || options?.vad) {
1757
+ await this.startMic({ vad: options?.vad });
1758
+ }
1759
+ if (this.callStatus !== "starting") return;
1760
+ this.openWS();
1761
+ } catch (error) {
1762
+ if (this.callStatus !== "stopped") {
1763
+ this.setError(toClientError(error));
1764
+ await this.stop();
1765
+ }
1766
+ throw error;
1655
1767
  }
1656
- await this.startWS();
1657
- if (this.isWSStarted && this._isStartingCall) {
1658
- this._isStartingCall = false;
1768
+ if (this.isWSStarted) {
1769
+ this.callStatus = "started";
1659
1770
  this.notifyStateChange();
1660
1771
  }
1661
1772
  };
1662
1773
  this.stop = async () => {
1774
+ this.cancelPendingStop();
1663
1775
  this.partialAssistantMessage = "";
1664
- this._isStartingCall = false;
1776
+ this.callStatus = "stopped";
1665
1777
  this._isProcessing = false;
1666
1778
  this._isMuted = false;
1667
1779
  this._isPaused = false;
1668
- this._isReconnecting = false;
1669
- if (this.reconnectTimer) {
1670
- clearTimeout(this.reconnectTimer);
1671
- this.reconnectTimer = void 0;
1672
- }
1780
+ this.clearReconnectTimer();
1673
1781
  try {
1674
1782
  this.stopWS();
1675
1783
  } catch (error) {
@@ -1684,6 +1792,35 @@ var MicdropClient = class extends EventEmitter7 {
1684
1792
  console.error("[MicdropClient] Error stopping microphone", error);
1685
1793
  }
1686
1794
  };
1795
+ /**
1796
+ * Stops the call once the assistant finished its answer, the one it is
1797
+ * still preparing included, such as the goodbye that follows an `EndCall`.
1798
+ * Stops right away when no answer is under way.
1799
+ *
1800
+ * A `start()` or a `stop()` meanwhile cancels the wait, so it never acts on
1801
+ * the next call.
1802
+ * @returns Resolves once the call is stopped, or the wait cancelled
1803
+ */
1804
+ this.stopAfterAnswer = async () => {
1805
+ if (!this.isAnswering) return this.stop();
1806
+ this.cancelPendingStop();
1807
+ return new Promise((resolve) => {
1808
+ const detach = () => {
1809
+ this.off("StateChange", onStateChange);
1810
+ this.pendingStop = void 0;
1811
+ };
1812
+ const onStateChange = () => {
1813
+ if (this.isAnswering) return;
1814
+ detach();
1815
+ this.stop().then(resolve);
1816
+ };
1817
+ this.on("StateChange", onStateChange);
1818
+ this.pendingStop = () => {
1819
+ detach();
1820
+ resolve();
1821
+ };
1822
+ });
1823
+ };
1687
1824
  this.mute = () => {
1688
1825
  this.vad?.pause();
1689
1826
  this._isMuted = true;
@@ -1760,7 +1897,9 @@ var MicdropClient = class extends EventEmitter7 {
1760
1897
  });
1761
1898
  }
1762
1899
  const deviceId = options.deviceId ?? storage.getItem("micdrop.micDevice" /* MicDevice */) ?? void 0;
1900
+ const recorder = this.micRecorder;
1763
1901
  await Mic.start(deviceId);
1902
+ if (this.micRecorder !== recorder || !Mic.isStarted) return;
1764
1903
  if (Mic.deviceId) {
1765
1904
  storage.setItem("micdrop.micDevice" /* MicDevice */, Mic.deviceId);
1766
1905
  } else {
@@ -1828,8 +1967,7 @@ var MicdropClient = class extends EventEmitter7 {
1828
1967
  clearTimeout(this.connectionTimer);
1829
1968
  this.connectionTimer = void 0;
1830
1969
  }
1831
- this._isReconnecting = false;
1832
- this._isStartingCall = false;
1970
+ this.callStatus = "started";
1833
1971
  this.reconnectAttempt = 0;
1834
1972
  this.error = void 0;
1835
1973
  this.notifyStateChange();
@@ -1914,7 +2052,7 @@ var MicdropClient = class extends EventEmitter7 {
1914
2052
  const config = { ...DEFAULT_RECONNECT_OPTIONS, ...this.options.reconnect };
1915
2053
  const canReconnect = error && isRecoverableError(error) && config.maxAttempts > 0;
1916
2054
  if (canReconnect) {
1917
- this._isReconnecting = true;
2055
+ this.callStatus = "reconnecting";
1918
2056
  this.stopWS();
1919
2057
  this.scheduleReconnect(config);
1920
2058
  } else {
@@ -1979,14 +2117,30 @@ var MicdropClient = class extends EventEmitter7 {
1979
2117
  get vad() {
1980
2118
  return this.micRecorder?.vad;
1981
2119
  }
2120
+ get status() {
2121
+ const mic = this.micRecorder?.state;
2122
+ if (this.callStatus === "reconnecting") return "reconnecting";
2123
+ if (this.callStatus === "started" && mic?.isStarted) return "started";
2124
+ if (this.callStatus !== "stopped" || mic?.isStarting) return "starting";
2125
+ return "stopped";
2126
+ }
2127
+ get activity() {
2128
+ if (this.isUserSpeaking) return "userSpeaking";
2129
+ if (this.isAssistantSpeaking) return "assistantSpeaking";
2130
+ if (this.isProcessing) return "processing";
2131
+ if (this.isListening) return "listening";
2132
+ return "idle";
2133
+ }
2134
+ /** The call is on, reconnecting included */
1982
2135
  get isStarted() {
1983
- return (this.isWSStarted || this._isReconnecting) && (this.micRecorder?.state.isStarted || false);
2136
+ const status = this.status;
2137
+ return status === "started" || status === "reconnecting";
1984
2138
  }
1985
2139
  get isStarting() {
1986
- return (this._isStartingCall || this.isWSStarting || this.micRecorder?.state.isStarting || false) && !this._isReconnecting;
2140
+ return this.status === "starting";
1987
2141
  }
1988
2142
  get isReconnecting() {
1989
- return this._isReconnecting;
2143
+ return this.status === "reconnecting";
1990
2144
  }
1991
2145
  get isMuted() {
1992
2146
  return this._isMuted;
@@ -2039,6 +2193,8 @@ var MicdropClient = class extends EventEmitter7 {
2039
2193
  }
2040
2194
  get state() {
2041
2195
  return {
2196
+ status: this.status,
2197
+ activity: this.activity,
2042
2198
  isStarting: this.isStarting,
2043
2199
  isStarted: this.isStarted,
2044
2200
  isReconnecting: this.isReconnecting,
@@ -2059,62 +2215,69 @@ var MicdropClient = class extends EventEmitter7 {
2059
2215
  speakerDevices: this.speakerDevices
2060
2216
  };
2061
2217
  }
2218
+ /** The assistant prepares or speaks an answer */
2219
+ get isAnswering() {
2220
+ const { activity } = this;
2221
+ return activity === "processing" || activity === "assistantSpeaking";
2222
+ }
2223
+ cancelPendingStop() {
2224
+ this.pendingStop?.();
2225
+ }
2062
2226
  async stopMic() {
2063
2227
  this.micRecorder?.stop();
2064
2228
  this.micRecorder = void 0;
2065
2229
  await Mic.stop();
2066
2230
  this.notifyStateChange();
2067
2231
  }
2068
- async startWS() {
2069
- try {
2070
- if (this.ws) {
2071
- this.log("WebSocket is already started");
2072
- return;
2073
- }
2074
- if (!this.isMicStarted) {
2075
- throw new MicdropClientError("Mic" /* Mic */);
2076
- }
2077
- if (!this.options.url) {
2078
- throw new MicdropClientError("MissingUrl" /* MissingUrl */);
2079
- }
2080
- this.ws = new WebSocket(this.options.url);
2081
- this.ws.binaryType = "arraybuffer";
2082
- this.notifyStateChange();
2083
- this.ws.onopen = this.onWSOpen;
2084
- this.ws.onmessage = this.onWSMessage;
2085
- this.ws.onclose = this.onWSClose;
2086
- this.ws.onerror = this.onWSError;
2087
- this.connectionTimer = setTimeout(() => {
2088
- this.connectionTimer = void 0;
2089
- if (this.ws?.readyState === 0) {
2090
- this.log("WebSocket connection timeout");
2091
- this.ws.close();
2092
- }
2093
- }, this.options.reconnect?.connectionTimeout ?? DEFAULT_RECONNECT_OPTIONS.connectionTimeout);
2094
- } catch (error) {
2095
- this.setError(
2096
- error instanceof MicdropClientError ? error : new MicdropClientError(
2097
- "Connection" /* Connection */,
2098
- error?.message
2099
- )
2100
- );
2101
- await this.stop();
2102
- throw error;
2232
+ /**
2233
+ * Opens the connection, unless one is already open or opening.
2234
+ *
2235
+ * The socket answers through handlers bound to it, so one left behind by a
2236
+ * stop or a reconnection never acts on the call that followed.
2237
+ */
2238
+ openWS() {
2239
+ if (this.ws) {
2240
+ this.log("WebSocket is already started");
2241
+ return;
2103
2242
  }
2243
+ if (!this.isMicStarted) {
2244
+ throw new MicdropClientError("Mic" /* Mic */);
2245
+ }
2246
+ if (!this.options.url) {
2247
+ throw new MicdropClientError("MissingUrl" /* MissingUrl */);
2248
+ }
2249
+ const ws = new WebSocket(this.options.url);
2250
+ ws.binaryType = "arraybuffer";
2251
+ this.ws = ws;
2252
+ this.notifyStateChange();
2253
+ const isCurrent = () => this.ws === ws;
2254
+ ws.onopen = () => isCurrent() && this.onWSOpen();
2255
+ ws.onmessage = (event) => isCurrent() && this.onWSMessage(event);
2256
+ ws.onclose = (event) => isCurrent() && this.onWSClose(event);
2257
+ ws.onerror = () => isCurrent() && this.onWSError();
2258
+ this.connectionTimer = setTimeout(() => {
2259
+ this.connectionTimer = void 0;
2260
+ if (isCurrent() && ws.readyState === 0) {
2261
+ this.log("WebSocket connection timeout");
2262
+ ws.close();
2263
+ }
2264
+ }, this.options.reconnect?.connectionTimeout ?? DEFAULT_RECONNECT_OPTIONS.connectionTimeout);
2104
2265
  }
2105
2266
  stopWS() {
2106
2267
  if (this.connectionTimer) {
2107
2268
  clearTimeout(this.connectionTimer);
2108
2269
  this.connectionTimer = void 0;
2109
2270
  }
2110
- if (!this.ws) return;
2111
- if (this.ws.readyState === 1 || this.ws.readyState === 0) {
2112
- this.ws.close();
2113
- }
2271
+ const ws = this.ws;
2272
+ if (!ws) return;
2114
2273
  this.ws = void 0;
2274
+ if (ws.readyState === 1 || ws.readyState === 0) {
2275
+ ws.close();
2276
+ }
2115
2277
  this.notifyStateChange();
2116
2278
  }
2117
2279
  scheduleReconnect(config) {
2280
+ if (this.callStatus !== "reconnecting") return;
2118
2281
  if (this.reconnectAttempt >= config.maxAttempts) {
2119
2282
  this.stop();
2120
2283
  this.setError(new MicdropClientError("Connection" /* Connection */));
@@ -2127,17 +2290,23 @@ var MicdropClient = class extends EventEmitter7 {
2127
2290
  if (!this.isPaused && !this.isMuted) {
2128
2291
  this.vad?.pause?.();
2129
2292
  }
2130
- if (this.reconnectTimer) {
2131
- clearTimeout(this.reconnectTimer);
2132
- }
2133
- this.reconnectTimer = setTimeout(async () => {
2293
+ this.clearReconnectTimer();
2294
+ this.reconnectTimer = setTimeout(() => {
2295
+ this.reconnectTimer = void 0;
2296
+ if (this.callStatus !== "reconnecting") return;
2134
2297
  try {
2135
- await this.startWS();
2136
- } catch {
2137
- this.scheduleReconnect(config);
2298
+ this.openWS();
2299
+ } catch (error) {
2300
+ this.setError(toClientError(error));
2301
+ this.stop();
2138
2302
  }
2139
2303
  }, config.delayMs);
2140
2304
  }
2305
+ clearReconnectTimer() {
2306
+ if (!this.reconnectTimer) return;
2307
+ clearTimeout(this.reconnectTimer);
2308
+ this.reconnectTimer = void 0;
2309
+ }
2141
2310
  // The same as when the speaker starts on a call without interruptions
2142
2311
  onInterruptionsGivenUp() {
2143
2312
  this.log("Interruptions disabled until the end of the call");
@@ -2182,6 +2351,13 @@ var MicdropClient = class extends EventEmitter7 {
2182
2351
  console.log(`[MicdropClient ${Date.now() - this.startTime}]`, ...message);
2183
2352
  }
2184
2353
  };
2354
+ function toClientError(error) {
2355
+ if (error instanceof MicdropClientError) return error;
2356
+ return new MicdropClientError(
2357
+ "Connection" /* Connection */,
2358
+ error?.message
2359
+ );
2360
+ }
2185
2361
 
2186
2362
  // src/Micdrop.ts
2187
2363
  var globalScope2 = globalThis;
@@ -2193,6 +2369,7 @@ export {
2193
2369
  CONFIRM_MARGIN,
2194
2370
  DEFAULT_TURN_MAX_WAIT,
2195
2371
  ECHO_TAIL,
2372
+ LazySession,
2196
2373
  Mic,
2197
2374
  MicController,
2198
2375
  MicDriver,
@@ -2218,6 +2395,7 @@ export {
2218
2395
  VolumeMeter,
2219
2396
  VolumeVAD,
2220
2397
  concatFloat32,
2398
+ createDownloadCache,
2221
2399
  currentTurn,
2222
2400
  equalVADConfig,
2223
2401
  floatToPcm16,