@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.js CHANGED
@@ -23,6 +23,7 @@ __export(index_exports, {
23
23
  CONFIRM_MARGIN: () => CONFIRM_MARGIN,
24
24
  DEFAULT_TURN_MAX_WAIT: () => DEFAULT_TURN_MAX_WAIT,
25
25
  ECHO_TAIL: () => ECHO_TAIL,
26
+ LazySession: () => LazySession,
26
27
  Mic: () => Mic,
27
28
  MicController: () => MicController,
28
29
  MicDriver: () => MicDriver,
@@ -48,6 +49,7 @@ __export(index_exports, {
48
49
  VolumeMeter: () => VolumeMeter,
49
50
  VolumeVAD: () => VolumeVAD,
50
51
  concatFloat32: () => concatFloat32,
52
+ createDownloadCache: () => createDownloadCache,
51
53
  currentTurn: () => currentTurn,
52
54
  equalVADConfig: () => equalVADConfig,
53
55
  floatToPcm16: () => floatToPcm16,
@@ -677,6 +679,9 @@ var SileroVAD = class extends VAD {
677
679
  // Bumped whenever the state is reset, so a window scored before that is
678
680
  // dropped rather than reopening a turn
679
681
  this.round = 0;
682
+ // Bumped by every start and stop, so a start overtaken while its model
683
+ // loaded lets go of it
684
+ this.startId = 0;
680
685
  this.onFrames = (frames, sampleRate) => {
681
686
  if (this._isPaused || !this.model) return;
682
687
  if (sampleRate !== this.bufferRate) {
@@ -712,19 +717,33 @@ var SileroVAD = class extends VAD {
712
717
  "No Silero model. Add `import '@micdrop/web/silero'` in a browser or `import '@micdrop/react-native/silero'` on a phone, or call setSileroModelLoader()."
713
718
  );
714
719
  }
715
- this.model = await loadModel();
720
+ this.mic = mic;
721
+ const startId = ++this.startId;
722
+ let model;
723
+ try {
724
+ model = await loadModel();
725
+ } catch (error) {
726
+ if (startId === this.startId) this.mic = void 0;
727
+ throw error;
728
+ }
729
+ if (startId !== this.startId) {
730
+ await model.release();
731
+ return;
732
+ }
733
+ this.model = model;
716
734
  this.resetState();
717
735
  this.setStatus(0 /* Silence */);
718
- this.mic = mic;
719
736
  mic.on("Frames", this.onFrames);
720
737
  }
721
738
  async stop() {
739
+ this.startId++;
722
740
  this.mic?.off("Frames", this.onFrames);
723
741
  this.mic = void 0;
724
742
  this.closeOpenTurn();
725
743
  this.resetState();
726
- await this.model?.release();
744
+ const model = this.model;
727
745
  this.model = void 0;
746
+ await model?.release();
728
747
  }
729
748
  async pause() {
730
749
  if (this._isPaused) return;
@@ -1044,6 +1063,9 @@ var MicRecorder = class extends import_eventemitter34.EventEmitter {
1044
1063
  isStarted: false,
1045
1064
  isSpeaking: false
1046
1065
  });
1066
+ if (this.turnOpen) {
1067
+ this.endTurn();
1068
+ }
1047
1069
  this.cancelPendingClose();
1048
1070
  try {
1049
1071
  this.mic?.off("Frames", this.onFrames);
@@ -1534,6 +1556,92 @@ var SpeakerDriver = class extends import_eventemitter36.EventEmitter {
1534
1556
  }
1535
1557
  };
1536
1558
 
1559
+ // src/audio/vad/lazySession.ts
1560
+ var LazySession = class {
1561
+ /**
1562
+ * @param name - What the session runs, to name it in errors
1563
+ * @param create - Creates the session, called once unless it fails
1564
+ */
1565
+ constructor(name, create) {
1566
+ this.name = name;
1567
+ this.create = create;
1568
+ this.state = { status: "unloaded" };
1569
+ this.runs = /* @__PURE__ */ new Set();
1570
+ }
1571
+ get status() {
1572
+ return this.state.status;
1573
+ }
1574
+ /** The session, loaded on the first call */
1575
+ get() {
1576
+ const state = this.state;
1577
+ if (state.status === "loaded") return Promise.resolve(state.session);
1578
+ if (state.status === "loading") return state.promise;
1579
+ if (state.status === "released") return Promise.reject(this.releasedError());
1580
+ const promise = this.create().then(
1581
+ async (session) => {
1582
+ if (this.isLoading(promise)) {
1583
+ this.state = { status: "loaded", session };
1584
+ return session;
1585
+ }
1586
+ await session.release();
1587
+ throw this.releasedError();
1588
+ },
1589
+ (error) => {
1590
+ if (this.isLoading(promise)) this.state = { status: "unloaded" };
1591
+ throw error;
1592
+ }
1593
+ );
1594
+ this.state = { status: "loading", promise };
1595
+ return promise;
1596
+ }
1597
+ /**
1598
+ * Runs something on the session, which a release waits for
1599
+ * @param run - Uses the session, and only until its promise settles
1600
+ */
1601
+ async use(run) {
1602
+ const session = await this.get();
1603
+ if (this.state.status !== "loaded" || this.state.session !== session) {
1604
+ throw this.releasedError();
1605
+ }
1606
+ const pending = run(session);
1607
+ this.runs.add(pending);
1608
+ try {
1609
+ return await pending;
1610
+ } finally {
1611
+ this.runs.delete(pending);
1612
+ }
1613
+ }
1614
+ /** Releases the session, for good */
1615
+ async release() {
1616
+ const state = this.state;
1617
+ this.state = { status: "released" };
1618
+ if (state.status !== "loaded") return;
1619
+ await Promise.allSettled(this.runs);
1620
+ await state.session.release();
1621
+ }
1622
+ isLoading(promise) {
1623
+ return this.state.status === "loading" && this.state.promise === promise;
1624
+ }
1625
+ releasedError() {
1626
+ return new Error(`The ${this.name} model was released`);
1627
+ }
1628
+ };
1629
+ function createDownloadCache(download) {
1630
+ const downloads = /* @__PURE__ */ new Map();
1631
+ return (url) => {
1632
+ let pending = downloads.get(url);
1633
+ if (!pending) {
1634
+ const started = download(url);
1635
+ downloads.set(url, started);
1636
+ started.catch(() => {
1637
+ if (downloads.get(url) === started) downloads.delete(url);
1638
+ });
1639
+ pending = started;
1640
+ }
1641
+ return pending;
1642
+ };
1643
+ }
1644
+
1537
1645
  // src/audio/index.ts
1538
1646
  var globalScope = globalThis;
1539
1647
  if (!globalScope.micdropMic) {
@@ -1695,13 +1803,13 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
1695
1803
  this._isProcessing = false;
1696
1804
  this._isMuted = false;
1697
1805
  this._isPaused = false;
1698
- this._isReconnecting = false;
1699
- // From start() until the connection opens, the microphone included: opening
1700
- // it can take a while, for the permission prompt or the browser itself
1701
- this._isStartingCall = false;
1806
+ // The call as start() and the connection leave it. The public status also
1807
+ // weighs the microphone, which a device change restarts during a call.
1808
+ this.callStatus = "stopped";
1702
1809
  this.reconnectAttempt = 0;
1703
1810
  this.echoGuard = new EchoGuard();
1704
1811
  this.start = async (options) => {
1812
+ this.cancelPendingStop();
1705
1813
  this.error = void 0;
1706
1814
  this.options = { ...this.options, ...options };
1707
1815
  this.startTime = Date.now();
@@ -1710,34 +1818,36 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
1710
1818
  this._isProcessing = true;
1711
1819
  this._isMuted = false;
1712
1820
  this._isPaused = false;
1713
- this._isReconnecting = false;
1714
- this._isStartingCall = true;
1821
+ this.callStatus = "starting";
1715
1822
  this.reconnectAttempt = 0;
1716
- if (this.reconnectTimer) {
1717
- clearTimeout(this.reconnectTimer);
1718
- this.reconnectTimer = void 0;
1719
- }
1823
+ this.clearReconnectTimer();
1720
1824
  this.notifyStateChange();
1721
- if (!this.micRecorder || options?.vad) {
1722
- await this.startMic({ vad: options?.vad });
1825
+ try {
1826
+ if (!this.micRecorder || options?.vad) {
1827
+ await this.startMic({ vad: options?.vad });
1828
+ }
1829
+ if (this.callStatus !== "starting") return;
1830
+ this.openWS();
1831
+ } catch (error) {
1832
+ if (this.callStatus !== "stopped") {
1833
+ this.setError(toClientError(error));
1834
+ await this.stop();
1835
+ }
1836
+ throw error;
1723
1837
  }
1724
- await this.startWS();
1725
- if (this.isWSStarted && this._isStartingCall) {
1726
- this._isStartingCall = false;
1838
+ if (this.isWSStarted) {
1839
+ this.callStatus = "started";
1727
1840
  this.notifyStateChange();
1728
1841
  }
1729
1842
  };
1730
1843
  this.stop = async () => {
1844
+ this.cancelPendingStop();
1731
1845
  this.partialAssistantMessage = "";
1732
- this._isStartingCall = false;
1846
+ this.callStatus = "stopped";
1733
1847
  this._isProcessing = false;
1734
1848
  this._isMuted = false;
1735
1849
  this._isPaused = false;
1736
- this._isReconnecting = false;
1737
- if (this.reconnectTimer) {
1738
- clearTimeout(this.reconnectTimer);
1739
- this.reconnectTimer = void 0;
1740
- }
1850
+ this.clearReconnectTimer();
1741
1851
  try {
1742
1852
  this.stopWS();
1743
1853
  } catch (error) {
@@ -1752,6 +1862,35 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
1752
1862
  console.error("[MicdropClient] Error stopping microphone", error);
1753
1863
  }
1754
1864
  };
1865
+ /**
1866
+ * Stops the call once the assistant finished its answer, the one it is
1867
+ * still preparing included, such as the goodbye that follows an `EndCall`.
1868
+ * Stops right away when no answer is under way.
1869
+ *
1870
+ * A `start()` or a `stop()` meanwhile cancels the wait, so it never acts on
1871
+ * the next call.
1872
+ * @returns Resolves once the call is stopped, or the wait cancelled
1873
+ */
1874
+ this.stopAfterAnswer = async () => {
1875
+ if (!this.isAnswering) return this.stop();
1876
+ this.cancelPendingStop();
1877
+ return new Promise((resolve) => {
1878
+ const detach = () => {
1879
+ this.off("StateChange", onStateChange);
1880
+ this.pendingStop = void 0;
1881
+ };
1882
+ const onStateChange = () => {
1883
+ if (this.isAnswering) return;
1884
+ detach();
1885
+ this.stop().then(resolve);
1886
+ };
1887
+ this.on("StateChange", onStateChange);
1888
+ this.pendingStop = () => {
1889
+ detach();
1890
+ resolve();
1891
+ };
1892
+ });
1893
+ };
1755
1894
  this.mute = () => {
1756
1895
  this.vad?.pause();
1757
1896
  this._isMuted = true;
@@ -1828,7 +1967,9 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
1828
1967
  });
1829
1968
  }
1830
1969
  const deviceId = options.deviceId ?? storage.getItem("micdrop.micDevice" /* MicDevice */) ?? void 0;
1970
+ const recorder = this.micRecorder;
1831
1971
  await Mic.start(deviceId);
1972
+ if (this.micRecorder !== recorder || !Mic.isStarted) return;
1832
1973
  if (Mic.deviceId) {
1833
1974
  storage.setItem("micdrop.micDevice" /* MicDevice */, Mic.deviceId);
1834
1975
  } else {
@@ -1896,8 +2037,7 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
1896
2037
  clearTimeout(this.connectionTimer);
1897
2038
  this.connectionTimer = void 0;
1898
2039
  }
1899
- this._isReconnecting = false;
1900
- this._isStartingCall = false;
2040
+ this.callStatus = "started";
1901
2041
  this.reconnectAttempt = 0;
1902
2042
  this.error = void 0;
1903
2043
  this.notifyStateChange();
@@ -1982,7 +2122,7 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
1982
2122
  const config = { ...DEFAULT_RECONNECT_OPTIONS, ...this.options.reconnect };
1983
2123
  const canReconnect = error && isRecoverableError(error) && config.maxAttempts > 0;
1984
2124
  if (canReconnect) {
1985
- this._isReconnecting = true;
2125
+ this.callStatus = "reconnecting";
1986
2126
  this.stopWS();
1987
2127
  this.scheduleReconnect(config);
1988
2128
  } else {
@@ -2047,14 +2187,30 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
2047
2187
  get vad() {
2048
2188
  return this.micRecorder?.vad;
2049
2189
  }
2190
+ get status() {
2191
+ const mic = this.micRecorder?.state;
2192
+ if (this.callStatus === "reconnecting") return "reconnecting";
2193
+ if (this.callStatus === "started" && mic?.isStarted) return "started";
2194
+ if (this.callStatus !== "stopped" || mic?.isStarting) return "starting";
2195
+ return "stopped";
2196
+ }
2197
+ get activity() {
2198
+ if (this.isUserSpeaking) return "userSpeaking";
2199
+ if (this.isAssistantSpeaking) return "assistantSpeaking";
2200
+ if (this.isProcessing) return "processing";
2201
+ if (this.isListening) return "listening";
2202
+ return "idle";
2203
+ }
2204
+ /** The call is on, reconnecting included */
2050
2205
  get isStarted() {
2051
- return (this.isWSStarted || this._isReconnecting) && (this.micRecorder?.state.isStarted || false);
2206
+ const status = this.status;
2207
+ return status === "started" || status === "reconnecting";
2052
2208
  }
2053
2209
  get isStarting() {
2054
- return (this._isStartingCall || this.isWSStarting || this.micRecorder?.state.isStarting || false) && !this._isReconnecting;
2210
+ return this.status === "starting";
2055
2211
  }
2056
2212
  get isReconnecting() {
2057
- return this._isReconnecting;
2213
+ return this.status === "reconnecting";
2058
2214
  }
2059
2215
  get isMuted() {
2060
2216
  return this._isMuted;
@@ -2107,6 +2263,8 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
2107
2263
  }
2108
2264
  get state() {
2109
2265
  return {
2266
+ status: this.status,
2267
+ activity: this.activity,
2110
2268
  isStarting: this.isStarting,
2111
2269
  isStarted: this.isStarted,
2112
2270
  isReconnecting: this.isReconnecting,
@@ -2127,62 +2285,69 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
2127
2285
  speakerDevices: this.speakerDevices
2128
2286
  };
2129
2287
  }
2288
+ /** The assistant prepares or speaks an answer */
2289
+ get isAnswering() {
2290
+ const { activity } = this;
2291
+ return activity === "processing" || activity === "assistantSpeaking";
2292
+ }
2293
+ cancelPendingStop() {
2294
+ this.pendingStop?.();
2295
+ }
2130
2296
  async stopMic() {
2131
2297
  this.micRecorder?.stop();
2132
2298
  this.micRecorder = void 0;
2133
2299
  await Mic.stop();
2134
2300
  this.notifyStateChange();
2135
2301
  }
2136
- async startWS() {
2137
- try {
2138
- if (this.ws) {
2139
- this.log("WebSocket is already started");
2140
- return;
2141
- }
2142
- if (!this.isMicStarted) {
2143
- throw new MicdropClientError("Mic" /* Mic */);
2144
- }
2145
- if (!this.options.url) {
2146
- throw new MicdropClientError("MissingUrl" /* MissingUrl */);
2147
- }
2148
- this.ws = new WebSocket(this.options.url);
2149
- this.ws.binaryType = "arraybuffer";
2150
- this.notifyStateChange();
2151
- this.ws.onopen = this.onWSOpen;
2152
- this.ws.onmessage = this.onWSMessage;
2153
- this.ws.onclose = this.onWSClose;
2154
- this.ws.onerror = this.onWSError;
2155
- this.connectionTimer = setTimeout(() => {
2156
- this.connectionTimer = void 0;
2157
- if (this.ws?.readyState === 0) {
2158
- this.log("WebSocket connection timeout");
2159
- this.ws.close();
2160
- }
2161
- }, this.options.reconnect?.connectionTimeout ?? DEFAULT_RECONNECT_OPTIONS.connectionTimeout);
2162
- } catch (error) {
2163
- this.setError(
2164
- error instanceof MicdropClientError ? error : new MicdropClientError(
2165
- "Connection" /* Connection */,
2166
- error?.message
2167
- )
2168
- );
2169
- await this.stop();
2170
- throw error;
2302
+ /**
2303
+ * Opens the connection, unless one is already open or opening.
2304
+ *
2305
+ * The socket answers through handlers bound to it, so one left behind by a
2306
+ * stop or a reconnection never acts on the call that followed.
2307
+ */
2308
+ openWS() {
2309
+ if (this.ws) {
2310
+ this.log("WebSocket is already started");
2311
+ return;
2171
2312
  }
2313
+ if (!this.isMicStarted) {
2314
+ throw new MicdropClientError("Mic" /* Mic */);
2315
+ }
2316
+ if (!this.options.url) {
2317
+ throw new MicdropClientError("MissingUrl" /* MissingUrl */);
2318
+ }
2319
+ const ws = new WebSocket(this.options.url);
2320
+ ws.binaryType = "arraybuffer";
2321
+ this.ws = ws;
2322
+ this.notifyStateChange();
2323
+ const isCurrent = () => this.ws === ws;
2324
+ ws.onopen = () => isCurrent() && this.onWSOpen();
2325
+ ws.onmessage = (event) => isCurrent() && this.onWSMessage(event);
2326
+ ws.onclose = (event) => isCurrent() && this.onWSClose(event);
2327
+ ws.onerror = () => isCurrent() && this.onWSError();
2328
+ this.connectionTimer = setTimeout(() => {
2329
+ this.connectionTimer = void 0;
2330
+ if (isCurrent() && ws.readyState === 0) {
2331
+ this.log("WebSocket connection timeout");
2332
+ ws.close();
2333
+ }
2334
+ }, this.options.reconnect?.connectionTimeout ?? DEFAULT_RECONNECT_OPTIONS.connectionTimeout);
2172
2335
  }
2173
2336
  stopWS() {
2174
2337
  if (this.connectionTimer) {
2175
2338
  clearTimeout(this.connectionTimer);
2176
2339
  this.connectionTimer = void 0;
2177
2340
  }
2178
- if (!this.ws) return;
2179
- if (this.ws.readyState === 1 || this.ws.readyState === 0) {
2180
- this.ws.close();
2181
- }
2341
+ const ws = this.ws;
2342
+ if (!ws) return;
2182
2343
  this.ws = void 0;
2344
+ if (ws.readyState === 1 || ws.readyState === 0) {
2345
+ ws.close();
2346
+ }
2183
2347
  this.notifyStateChange();
2184
2348
  }
2185
2349
  scheduleReconnect(config) {
2350
+ if (this.callStatus !== "reconnecting") return;
2186
2351
  if (this.reconnectAttempt >= config.maxAttempts) {
2187
2352
  this.stop();
2188
2353
  this.setError(new MicdropClientError("Connection" /* Connection */));
@@ -2195,17 +2360,23 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
2195
2360
  if (!this.isPaused && !this.isMuted) {
2196
2361
  this.vad?.pause?.();
2197
2362
  }
2198
- if (this.reconnectTimer) {
2199
- clearTimeout(this.reconnectTimer);
2200
- }
2201
- this.reconnectTimer = setTimeout(async () => {
2363
+ this.clearReconnectTimer();
2364
+ this.reconnectTimer = setTimeout(() => {
2365
+ this.reconnectTimer = void 0;
2366
+ if (this.callStatus !== "reconnecting") return;
2202
2367
  try {
2203
- await this.startWS();
2204
- } catch {
2205
- this.scheduleReconnect(config);
2368
+ this.openWS();
2369
+ } catch (error) {
2370
+ this.setError(toClientError(error));
2371
+ this.stop();
2206
2372
  }
2207
2373
  }, config.delayMs);
2208
2374
  }
2375
+ clearReconnectTimer() {
2376
+ if (!this.reconnectTimer) return;
2377
+ clearTimeout(this.reconnectTimer);
2378
+ this.reconnectTimer = void 0;
2379
+ }
2209
2380
  // The same as when the speaker starts on a call without interruptions
2210
2381
  onInterruptionsGivenUp() {
2211
2382
  this.log("Interruptions disabled until the end of the call");
@@ -2250,6 +2421,13 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
2250
2421
  console.log(`[MicdropClient ${Date.now() - this.startTime}]`, ...message);
2251
2422
  }
2252
2423
  };
2424
+ function toClientError(error) {
2425
+ if (error instanceof MicdropClientError) return error;
2426
+ return new MicdropClientError(
2427
+ "Connection" /* Connection */,
2428
+ error?.message
2429
+ );
2430
+ }
2253
2431
 
2254
2432
  // src/Micdrop.ts
2255
2433
  var globalScope2 = globalThis;
@@ -2262,6 +2440,7 @@ var Micdrop = globalScope2.micdropClient;
2262
2440
  CONFIRM_MARGIN,
2263
2441
  DEFAULT_TURN_MAX_WAIT,
2264
2442
  ECHO_TAIL,
2443
+ LazySession,
2265
2444
  Mic,
2266
2445
  MicController,
2267
2446
  MicDriver,
@@ -2287,6 +2466,7 @@ var Micdrop = globalScope2.micdropClient;
2287
2466
  VolumeMeter,
2288
2467
  VolumeVAD,
2289
2468
  concatFloat32,
2469
+ createDownloadCache,
2290
2470
  currentTurn,
2291
2471
  equalVADConfig,
2292
2472
  floatToPcm16,