@micdrop/server 2.9.0 → 2.10.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
@@ -15,32 +15,65 @@ var Agent = class extends EventEmitter {
15
15
  constructor(options) {
16
16
  super();
17
17
  this.options = options;
18
+ /**
19
+ * @deprecated Read the signal given to `generateAnswer` instead. Still
20
+ * bumped by every answer and every cancel, for agents written before it.
21
+ */
18
22
  this.answerCount = 0;
19
- this.answering = false;
20
23
  this.conversation = [{ role: "system", content: options.systemPrompt }];
21
24
  this.tools = this.getDefaultTools();
22
25
  }
26
+ /** Whether an answer is being generated */
27
+ get isAnswering() {
28
+ return !!this.answerController;
29
+ }
30
+ /** @deprecated Read `isAnswering` instead */
31
+ get answering() {
32
+ return this.isAnswering;
33
+ }
34
+ /** @deprecated The answer is over once its stream ends or it is cancelled */
35
+ set answering(_value) {
36
+ }
37
+ /**
38
+ * Stops the answer being generated, by aborting its signal.
39
+ *
40
+ * An agent overriding it to stop more than the signal does calls
41
+ * `super.cancel()`.
42
+ */
43
+ cancel() {
44
+ this.answerCount++;
45
+ if (!this.answerController) return;
46
+ this.log("Cancel");
47
+ this.answerController.abort();
48
+ this.answerController = void 0;
49
+ }
23
50
  answer() {
24
51
  this.log("Start answering");
25
- const answerCount = ++this.answerCount;
52
+ this.answerCount++;
53
+ this.answerController?.abort();
54
+ const controller = new AbortController();
55
+ this.answerController = controller;
56
+ const { signal } = controller;
26
57
  const stream = new PassThrough();
27
- this.answering = true;
28
58
  Promise.resolve().then(
29
59
  () => this.options.onBeforeAnswer?.bind(this)(stream)
30
60
  ).then((result) => {
61
+ if (signal.aborted) return;
31
62
  if (typeof result === "string") {
32
63
  stream.write(result);
33
64
  this.addAssistantMessage(result);
34
65
  return;
35
66
  }
36
67
  if (result) return;
37
- return this.generateAnswer(stream);
68
+ return this.generateAnswer(stream, signal);
69
+ }).catch((error) => {
70
+ if (!signal.aborted) this.log("Error answering:", error);
38
71
  }).finally(() => {
39
72
  if (stream.writable) {
40
73
  stream.end();
41
74
  }
42
- if (answerCount === this.answerCount) {
43
- this.answering = false;
75
+ if (this.answerController === controller) {
76
+ this.answerController = void 0;
44
77
  }
45
78
  });
46
79
  return stream;
@@ -266,10 +299,10 @@ var FallbackAgent = class extends Agent {
266
299
  extract(message) {
267
300
  return this.agent ? this.agent.extract(message) : super.extract(message);
268
301
  }
269
- async generateAnswer(stream) {
302
+ async generateAnswer(stream, signal) {
270
303
  for (let attempt = 0; attempt < this.fallbackOptions.factories.length; attempt++) {
271
304
  const agent = this.agent;
272
- if (!agent) return;
305
+ if (!agent || signal.aborted) return;
273
306
  let failed = false;
274
307
  const onFailed = () => {
275
308
  failed = true;
@@ -280,7 +313,7 @@ var FallbackAgent = class extends Agent {
280
313
  } finally {
281
314
  agent.off("Failed", onFailed);
282
315
  }
283
- if (!failed) return;
316
+ if (!failed || signal.aborted) return;
284
317
  this.log("Agent failed, trying next agent");
285
318
  this.startNextAgent();
286
319
  }
@@ -288,6 +321,7 @@ var FallbackAgent = class extends Agent {
288
321
  this.emit("Failed");
289
322
  }
290
323
  cancel() {
324
+ super.cancel();
291
325
  this.agent?.cancel();
292
326
  }
293
327
  destroy() {
@@ -350,10 +384,8 @@ var MockAgent = class extends Agent {
350
384
  }
351
385
  async generateAnswer(stream) {
352
386
  const message = `Assistant Message ${this.i++}`;
353
- this.addAssistantMessage(message);
354
387
  stream.write(message);
355
- }
356
- cancel() {
388
+ this.addAssistantMessage(message);
357
389
  }
358
390
  };
359
391
 
@@ -593,6 +625,12 @@ var MicdropServer = class extends EventEmitter3 {
593
625
  this.socket = null;
594
626
  this.config = null;
595
627
  this.startTime = Date.now();
628
+ // Bumped by every cancel, so an answer cancelled before its first word can
629
+ // tell, and leave its message to the answer that replaces it
630
+ this.cancelRound = 0;
631
+ // Counts the assistant messages sent, so the partial answer of one already
632
+ // settled is not sent after it
633
+ this.assistantMessages = 0;
596
634
  // Holds the conversation when no agent does, and keeps it readable once the
597
635
  // call is over and the config is gone
598
636
  this.ownConversation = [];
@@ -600,10 +638,16 @@ var MicdropServer = class extends EventEmitter3 {
600
638
  this.operationQueue = [];
601
639
  this.isProcessingQueue = false;
602
640
  this.userSpeechChunks = 0;
603
- this.onClose = () => {
641
+ // Bumped whenever the user speaks again or mutes, so a decision taken on an
642
+ // earlier stretch of speech is dropped rather than answering over them
643
+ this.turnRound = 0;
644
+ this.onClose = (code, reason) => {
604
645
  this.releaseHeldTurn();
605
646
  if (!this.config) return;
606
- this.log("Connection closed");
647
+ this.log("Connection closed", {
648
+ code,
649
+ reason: reason?.toString() || void 0
650
+ });
607
651
  const duration = Math.round((Date.now() - this.startTime) / 1e3);
608
652
  const conversation = this.conversation;
609
653
  this.agent?.destroy();
@@ -745,6 +789,7 @@ var MicdropServer = class extends EventEmitter3 {
745
789
  this.processQueue();
746
790
  }
747
791
  cancel() {
792
+ this.cancelRound++;
748
793
  this.config?.classifier?.cancel();
749
794
  this.config?.tts?.cancel();
750
795
  this.agent?.cancel();
@@ -758,6 +803,8 @@ var MicdropServer = class extends EventEmitter3 {
758
803
  this.emit("UserAudio", chunk);
759
804
  }
760
805
  onMute() {
806
+ this.turnRound++;
807
+ this.releaseHeldTurn();
761
808
  this.userSpeechChunks = 0;
762
809
  this.currentUserStream?.end();
763
810
  this.currentUserStream = void 0;
@@ -766,6 +813,7 @@ var MicdropServer = class extends EventEmitter3 {
766
813
  }
767
814
  onStartSpeaking() {
768
815
  if (!this.config) return;
816
+ this.turnRound++;
769
817
  this.userSpeechChunks = 0;
770
818
  this.currentUserStream?.end();
771
819
  this.currentUserStream = new PassThrough2();
@@ -817,7 +865,9 @@ var MicdropServer = class extends EventEmitter3 {
817
865
  }
818
866
  /** Answers, unless the sentence sounds like it has more coming */
819
867
  async answerUserTurn() {
868
+ const round = this.turnRound;
820
869
  const complete = await (this.turnComplete ?? Promise.resolve(true));
870
+ if (round !== this.turnRound || !this.config) return;
821
871
  if (!complete) {
822
872
  this.log("Waiting for the rest of the sentence");
823
873
  this.socket?.send("SkipAnswer" /* SkipAnswer */);
@@ -893,6 +943,7 @@ var MicdropServer = class extends EventEmitter3 {
893
943
  `${"Message" /* Message */} ${JSON.stringify(message)}`
894
944
  );
895
945
  this.emit("Message", message);
946
+ if (message.role === "assistant") this.assistantMessages++;
896
947
  if (message.role === "user" && this.config?.realtime) this.classifyTurn();
897
948
  }
898
949
  sendPartialAnswer(content) {
@@ -939,12 +990,18 @@ var MicdropServer = class extends EventEmitter3 {
939
990
  return;
940
991
  }
941
992
  this.lastMessageSpeeched = lastMessage;
993
+ const round = this.cancelRound;
942
994
  try {
995
+ const sentBefore = this.assistantMessages;
943
996
  const stream = this.config.agent.answer();
944
997
  if (await hasContent(stream)) {
945
998
  await this._speak(
946
- this.config.partialMessages ? this.forwardPartialAnswer(stream) : stream
999
+ this.config.partialMessages ? this.forwardPartialAnswer(stream, sentBefore) : stream
947
1000
  );
1001
+ } else if (round !== this.cancelRound) {
1002
+ if (this.lastMessageSpeeched === lastMessage) {
1003
+ this.lastMessageSpeeched = void 0;
1004
+ }
948
1005
  } else {
949
1006
  this.socket?.send("SkipAnswer" /* SkipAnswer */);
950
1007
  }
@@ -953,10 +1010,19 @@ var MicdropServer = class extends EventEmitter3 {
953
1010
  throw error;
954
1011
  }
955
1012
  }
956
- /** Sends the answer to the client as it is written, ahead of the voice */
957
- forwardPartialAnswer(stream) {
1013
+ /**
1014
+ * Sends the answer to the client as it is written, ahead of the voice.
1015
+ *
1016
+ * The stream reaches here a moment after the agent wrote it, so the agent
1017
+ * can record the complete message first, which the client then shows. A
1018
+ * partial answer arriving after it would show the answer twice, so the
1019
+ * forwarding stops once the message is sent.
1020
+ */
1021
+ forwardPartialAnswer(stream, sentBefore) {
958
1022
  let content = "";
959
- const send = (text) => this.sendPartialAnswer(text);
1023
+ const send = (text) => {
1024
+ if (this.assistantMessages === sentBefore) this.sendPartialAnswer(text);
1025
+ };
960
1026
  const forward = new Transform({
961
1027
  transform(chunk, _encoding, callback) {
962
1028
  content += chunk.toString();
@@ -1134,6 +1200,7 @@ var Realtime = class extends Agent {
1134
1200
  });
1135
1201
  }
1136
1202
  cancel() {
1203
+ super.cancel();
1137
1204
  this.queue = [];
1138
1205
  const answer = this.currentAnswer;
1139
1206
  if (!answer) return;
@@ -1461,19 +1528,281 @@ var SharedInstances = class {
1461
1528
  this.entries.set(id, loading);
1462
1529
  return loading;
1463
1530
  }
1531
+ /** The instance for this key, loaded or being loaded, without loading it. */
1532
+ get(key) {
1533
+ return this.entries.get(typeof key === "string" ? key : JSON.stringify(key));
1534
+ }
1464
1535
  /** Whether an instance is loaded, or being loaded, for this key. */
1465
1536
  has(key) {
1466
1537
  return this.entries.has(typeof key === "string" ? key : JSON.stringify(key));
1467
1538
  }
1539
+ /**
1540
+ * Forgets the instance for this key, so the next call loads a new one. For
1541
+ * an instance that broke, a crashed worker for instance. Releasing what it
1542
+ * holds is left to the caller.
1543
+ */
1544
+ delete(key) {
1545
+ this.entries.delete(typeof key === "string" ? key : JSON.stringify(key));
1546
+ }
1468
1547
  /** Drops every instance, so the next call loads again. */
1469
1548
  clear() {
1470
1549
  this.entries.clear();
1471
1550
  }
1472
1551
  };
1473
1552
 
1474
- // src/stt/STT.ts
1553
+ // src/socket/ReconnectingSocket.ts
1475
1554
  import { EventEmitter as EventEmitter5 } from "eventemitter3";
1476
- var STT = class extends EventEmitter5 {
1555
+ import WebSocket from "ws";
1556
+ var DEFAULT_CONNECTION_TIMEOUT = 5e3;
1557
+ var DEFAULT_RETRY_DELAY = 1e3;
1558
+ var DEFAULT_MAX_RETRY = 3;
1559
+ var ReconnectingSocket = class extends EventEmitter5 {
1560
+ constructor(options) {
1561
+ super();
1562
+ this.options = options;
1563
+ this._status = "idle";
1564
+ this.everOpened = false;
1565
+ this.retryCount = 0;
1566
+ // Numbers the attempts, so one overtaken while it awaited its address stops
1567
+ this.attemptId = 0;
1568
+ this.waiters = [];
1569
+ }
1570
+ get status() {
1571
+ return this._status;
1572
+ }
1573
+ /** Whether a message sent now goes out */
1574
+ get isOpen() {
1575
+ return this._status === "open";
1576
+ }
1577
+ /**
1578
+ * Opens the connection. Called again after `failed`, it starts over with a
1579
+ * full retry budget, and does nothing in any other status.
1580
+ */
1581
+ connect() {
1582
+ if (this._status !== "idle" && this._status !== "failed") return;
1583
+ this.retryCount = 0;
1584
+ this.attempt();
1585
+ }
1586
+ /**
1587
+ * Waits for the connection to be open.
1588
+ * @returns True once open, false if it failed or was destroyed meanwhile
1589
+ */
1590
+ ready() {
1591
+ if (this._status === "open") return Promise.resolve(true);
1592
+ if (this._status === "destroyed" || this._status === "failed") {
1593
+ return Promise.resolve(false);
1594
+ }
1595
+ return new Promise((resolve) => this.waiters.push(resolve));
1596
+ }
1597
+ /**
1598
+ * Sends a message if the connection is open
1599
+ * @returns False when it did not go out
1600
+ */
1601
+ send(data) {
1602
+ if (this._status !== "open" || !this.socket) return false;
1603
+ this.socket.send(data);
1604
+ return true;
1605
+ }
1606
+ /**
1607
+ * Drops the connection and opens a new one right away, for a provider that
1608
+ * can only stop what it is doing by closing. It costs no retry, and the new
1609
+ * connection is not a reconnection.
1610
+ */
1611
+ restart() {
1612
+ if (this._status === "destroyed") return;
1613
+ this.log("Restarting the connection");
1614
+ this.clearRetryTimer();
1615
+ this.drop();
1616
+ this.everOpened = false;
1617
+ this.retryCount = 0;
1618
+ this.attempt();
1619
+ }
1620
+ /** Closes the connection for good */
1621
+ destroy() {
1622
+ if (this._status === "destroyed") return;
1623
+ this.clearRetryTimer();
1624
+ this.drop();
1625
+ this.setStatus("destroyed");
1626
+ this.settleWaiters(false);
1627
+ this.removeAllListeners();
1628
+ }
1629
+ async attempt() {
1630
+ const id = ++this.attemptId;
1631
+ const isOvertaken = () => id !== this.attemptId || this._status !== "connecting";
1632
+ this.setStatus("connecting");
1633
+ let url;
1634
+ try {
1635
+ const { url: getUrl } = this.options;
1636
+ url = typeof getUrl === "string" ? getUrl : await getUrl();
1637
+ } catch (error) {
1638
+ if (isOvertaken()) return;
1639
+ this.onAttemptFailed(error);
1640
+ return;
1641
+ }
1642
+ if (isOvertaken()) return;
1643
+ const socket = new WebSocket(url, this.options.clientOptions);
1644
+ this.socket = socket;
1645
+ let settled = false;
1646
+ const fail = (error) => {
1647
+ if (settled || this.socket !== socket) return;
1648
+ settled = true;
1649
+ clearTimeout(timeout);
1650
+ this.drop();
1651
+ this.onAttemptFailed(error);
1652
+ };
1653
+ const timeout = setTimeout(
1654
+ () => fail(new Error("Connection timeout")),
1655
+ this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT
1656
+ );
1657
+ socket.on("error", (error) => {
1658
+ this.log("WebSocket error:", error.message);
1659
+ fail(error);
1660
+ });
1661
+ const { handshake } = this.options;
1662
+ const open = () => {
1663
+ settled = true;
1664
+ clearTimeout(timeout);
1665
+ this.onOpen();
1666
+ };
1667
+ socket.on("open", () => {
1668
+ if (this.socket !== socket) return;
1669
+ try {
1670
+ handshake?.start?.(socket);
1671
+ } catch (error) {
1672
+ fail(error);
1673
+ return;
1674
+ }
1675
+ if (!handshake?.read) open();
1676
+ });
1677
+ socket.on("message", (data, isBinary) => {
1678
+ if (this.socket !== socket) return;
1679
+ if (this._status === "open") {
1680
+ this.prove();
1681
+ this.emit("Message", data, isBinary);
1682
+ return;
1683
+ }
1684
+ if (settled || !handshake?.read) return;
1685
+ try {
1686
+ if (handshake.read(data, isBinary)) open();
1687
+ } catch (error) {
1688
+ fail(error);
1689
+ }
1690
+ });
1691
+ socket.on("close", (code, reasonBuffer) => {
1692
+ const reason = reasonBuffer.toString();
1693
+ if (!settled) {
1694
+ fail(new Error(`Connection closed (${code}) ${reason}`));
1695
+ return;
1696
+ }
1697
+ if (this.socket !== socket) return;
1698
+ this.log("Connection closed", { code, reason });
1699
+ this.drop();
1700
+ this.setStatus("reconnecting");
1701
+ this.emit("Close", code, reason);
1702
+ if (this._status !== "reconnecting") return;
1703
+ this.scheduleRetry(new Error(`Connection closed (${code}) ${reason}`));
1704
+ });
1705
+ }
1706
+ onOpen() {
1707
+ this.log("Connection opened");
1708
+ const reconnected = this.everOpened;
1709
+ this.everOpened = true;
1710
+ if (this.options.handshake?.read) {
1711
+ this.prove();
1712
+ } else {
1713
+ this.proofTimer = setTimeout(
1714
+ () => this.prove(),
1715
+ this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT
1716
+ );
1717
+ }
1718
+ this.startKeepAlive();
1719
+ this.setStatus("open");
1720
+ this.emit("Open", { reconnected });
1721
+ this.settleWaiters(true);
1722
+ }
1723
+ /** The connection works, the retry budget is whole again */
1724
+ prove() {
1725
+ this.clearProofTimer();
1726
+ this.retryCount = 0;
1727
+ }
1728
+ clearProofTimer() {
1729
+ if (!this.proofTimer) return;
1730
+ clearTimeout(this.proofTimer);
1731
+ this.proofTimer = void 0;
1732
+ }
1733
+ onAttemptFailed(error) {
1734
+ this.log("Connection failed:", error.message);
1735
+ this.scheduleRetry(error);
1736
+ }
1737
+ scheduleRetry(error) {
1738
+ if (this._status === "destroyed") return;
1739
+ this.retryCount++;
1740
+ if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY)) {
1741
+ this.log("Max retries reached, giving up");
1742
+ this.setStatus("failed");
1743
+ this.settleWaiters(false);
1744
+ this.emit("Failed", error);
1745
+ return;
1746
+ }
1747
+ this.setStatus("reconnecting");
1748
+ this.log(`Reconnecting (attempt ${this.retryCount})`);
1749
+ this.clearRetryTimer();
1750
+ this.retryTimer = setTimeout(() => {
1751
+ this.retryTimer = void 0;
1752
+ if (this._status === "reconnecting") this.attempt();
1753
+ }, this.options.retryDelay ?? DEFAULT_RETRY_DELAY);
1754
+ }
1755
+ /** Lets go of the current socket, which will never be heard from again */
1756
+ drop() {
1757
+ this.stopKeepAlive();
1758
+ this.clearProofTimer();
1759
+ const socket = this.socket;
1760
+ this.socket = void 0;
1761
+ if (!socket) return;
1762
+ socket.removeAllListeners();
1763
+ socket.on("error", () => {
1764
+ });
1765
+ if (socket.readyState === WebSocket.OPEN || socket.readyState === WebSocket.CONNECTING) {
1766
+ socket.close(1e3);
1767
+ }
1768
+ }
1769
+ startKeepAlive() {
1770
+ const keepAlive = this.options.keepAlive;
1771
+ if (!keepAlive) return;
1772
+ this.stopKeepAlive();
1773
+ this.keepAliveTimer = setInterval(() => {
1774
+ const { message } = keepAlive;
1775
+ this.send(typeof message === "string" ? message : message());
1776
+ }, keepAlive.interval);
1777
+ }
1778
+ stopKeepAlive() {
1779
+ if (!this.keepAliveTimer) return;
1780
+ clearInterval(this.keepAliveTimer);
1781
+ this.keepAliveTimer = void 0;
1782
+ }
1783
+ clearRetryTimer() {
1784
+ if (!this.retryTimer) return;
1785
+ clearTimeout(this.retryTimer);
1786
+ this.retryTimer = void 0;
1787
+ }
1788
+ settleWaiters(open) {
1789
+ const waiters = this.waiters;
1790
+ this.waiters = [];
1791
+ waiters.forEach((resolve) => resolve(open));
1792
+ }
1793
+ setStatus(status) {
1794
+ if (this._status === status) return;
1795
+ this._status = status;
1796
+ this.emit("StatusChange", status);
1797
+ }
1798
+ log(...message) {
1799
+ this.options.log?.(...message);
1800
+ }
1801
+ };
1802
+
1803
+ // src/stt/STT.ts
1804
+ import { EventEmitter as EventEmitter6 } from "eventemitter3";
1805
+ var STT = class extends EventEmitter6 {
1477
1806
  log(...message) {
1478
1807
  this.logger?.log(...message);
1479
1808
  }
@@ -1499,24 +1828,31 @@ var MockSTT = class extends STT {
1499
1828
  // src/stt/FallbackSTT.ts
1500
1829
  import { PassThrough as PassThrough3 } from "stream";
1501
1830
  var FallbackSTT = class extends STT {
1502
- // Start at -1 because we need to increment it before using it
1503
1831
  constructor(options) {
1504
1832
  super();
1505
1833
  this.options = options;
1506
1834
  this.stt = null;
1507
1835
  this.sttIndex = -1;
1836
+ // Start at -1 because we need to increment it before using it
1837
+ // In the order spoken, each STT giving one transcript per utterance in turn
1838
+ this.pending = [];
1508
1839
  this.onTranscript = (transcript) => {
1840
+ this.pending.shift();
1509
1841
  this.emit("Transcript", transcript);
1510
1842
  };
1511
- this.onFailed = (chunks) => {
1843
+ /**
1844
+ * Hands every utterance still waiting to the next STT, one stream each so
1845
+ * each gets its own transcript. One still being spoken keeps going there.
1846
+ */
1847
+ this.onFailed = () => {
1512
1848
  this.log("STT failed, trying next STT");
1513
1849
  this.startNextSTT();
1514
- if (chunks.length > 0) {
1515
- this.log("Sending audio chunks again");
1516
- const stream = new PassThrough3();
1517
- this.stt?.transcribe(stream);
1518
- chunks.forEach((chunk) => stream.write(chunk));
1519
- stream.end();
1850
+ if (this.pending.length > 0) this.log("Sending audio chunks again");
1851
+ for (const utterance of this.pending) {
1852
+ utterance.stream = new PassThrough3();
1853
+ this.stt?.transcribe(utterance.stream);
1854
+ utterance.chunks.forEach((chunk) => utterance.stream.write(chunk));
1855
+ if (utterance.ended) utterance.stream.end();
1520
1856
  }
1521
1857
  };
1522
1858
  if (this.options.factories.length === 0) {
@@ -1525,13 +1861,28 @@ var FallbackSTT = class extends STT {
1525
1861
  this.startNextSTT();
1526
1862
  }
1527
1863
  transcribe(audioStream) {
1528
- this.stt?.transcribe(audioStream);
1864
+ const utterance = {
1865
+ chunks: [],
1866
+ ended: false,
1867
+ stream: new PassThrough3()
1868
+ };
1869
+ this.pending.push(utterance);
1870
+ audioStream.on("data", (chunk) => {
1871
+ utterance.chunks.push(chunk);
1872
+ utterance.stream.write(chunk);
1873
+ });
1874
+ audioStream.on("end", () => {
1875
+ utterance.ended = true;
1876
+ utterance.stream.end();
1877
+ });
1878
+ this.stt?.transcribe(utterance.stream);
1529
1879
  }
1530
1880
  destroy() {
1531
1881
  super.destroy();
1532
1882
  this.stt?.destroy();
1533
1883
  this.stt = null;
1534
1884
  this.sttIndex = -1;
1885
+ this.pending = [];
1535
1886
  }
1536
1887
  startNextSTT() {
1537
1888
  this.sttIndex++;
@@ -1554,8 +1905,8 @@ var FallbackSTT = class extends STT {
1554
1905
  import { PassThrough as PassThrough4 } from "stream";
1555
1906
 
1556
1907
  // src/tts/TTS.ts
1557
- import { EventEmitter as EventEmitter6 } from "eventemitter3";
1558
- var TTS = class extends EventEmitter6 {
1908
+ import { EventEmitter as EventEmitter7 } from "eventemitter3";
1909
+ var TTS = class extends EventEmitter7 {
1559
1910
  log(...message) {
1560
1911
  this.logger?.log(...message);
1561
1912
  }
@@ -1567,7 +1918,6 @@ var TTS = class extends EventEmitter6 {
1567
1918
 
1568
1919
  // src/tts/FallbackTTS.ts
1569
1920
  var FallbackTTS = class extends TTS {
1570
- // Start at -1 because we need to increment it before using it
1571
1921
  constructor(options) {
1572
1922
  super();
1573
1923
  this.options = options;
@@ -1576,16 +1926,22 @@ var FallbackTTS = class extends TTS {
1576
1926
  this.onAudio = (audio) => {
1577
1927
  this.emit("Audio", audio);
1578
1928
  };
1929
+ /**
1930
+ * Hands the unspoken text to the next TTS. An utterance whose text is still
1931
+ * being written goes on there, so its end is spoken too.
1932
+ */
1579
1933
  this.onFailed = (chunks) => {
1580
1934
  this.log("TTS failed, trying next TTS");
1581
1935
  this.startNextTTS();
1582
- if (chunks.length > 0) {
1583
- this.log("Sending text chunks again");
1584
- const stream = new PassThrough4();
1585
- this.tts?.speak(stream);
1586
- chunks.forEach((chunk) => stream.write(chunk));
1587
- stream.end();
1588
- }
1936
+ const utterance = this.current;
1937
+ const continues = !!utterance && !utterance.ended;
1938
+ if (chunks.length === 0 && !continues) return;
1939
+ this.log("Sending text chunks again");
1940
+ const stream = new PassThrough4();
1941
+ this.tts?.speak(stream);
1942
+ chunks.forEach((chunk) => stream.write(chunk));
1943
+ if (continues) utterance.stream = stream;
1944
+ else stream.end();
1589
1945
  };
1590
1946
  if (this.options.factories.length === 0) {
1591
1947
  throw new Error("FallbackTTS: No factories provided");
@@ -1593,9 +1949,20 @@ var FallbackTTS = class extends TTS {
1593
1949
  this.startNextTTS();
1594
1950
  }
1595
1951
  speak(textStream) {
1596
- this.tts?.speak(textStream);
1952
+ const utterance = {
1953
+ ended: false,
1954
+ stream: new PassThrough4()
1955
+ };
1956
+ this.current = utterance;
1957
+ textStream.on("data", (chunk) => utterance.stream.write(chunk));
1958
+ textStream.on("end", () => {
1959
+ utterance.ended = true;
1960
+ utterance.stream.end();
1961
+ });
1962
+ this.tts?.speak(utterance.stream);
1597
1963
  }
1598
1964
  cancel() {
1965
+ this.current = void 0;
1599
1966
  this.tts?.cancel();
1600
1967
  }
1601
1968
  destroy() {
@@ -1623,26 +1990,28 @@ var FallbackTTS = class extends TTS {
1623
1990
 
1624
1991
  // src/tts/MockTTS.ts
1625
1992
  import * as fs from "fs";
1626
- import { PassThrough as PassThrough5 } from "stream";
1627
1993
  var MockTTS = class extends TTS {
1628
1994
  constructor(audioFilePaths) {
1629
1995
  super();
1630
1996
  this.audioFilePaths = audioFilePaths;
1997
+ // Bumped by every answer and every cancel, so the chunks of an answer that
1998
+ // is no longer wanted stop there
1999
+ this.generation = 0;
1631
2000
  }
1632
2001
  speak(textStream) {
1633
- const audioStream = new PassThrough5();
2002
+ const generation = ++this.generation;
1634
2003
  textStream.once("data", async () => {
1635
2004
  for (const filePath of this.audioFilePaths) {
1636
2005
  await new Promise((resolve) => setTimeout(resolve, 200));
1637
- const audioBuffer = fs.readFileSync(filePath);
1638
- this.log(`Loaded chunk (${audioBuffer.length} bytes)`);
1639
- audioStream.write(audioBuffer);
2006
+ if (generation !== this.generation) return;
2007
+ const audio = fs.readFileSync(filePath);
2008
+ this.log(`Loaded chunk (${audio.length} bytes)`);
2009
+ this.emit("Audio", audio);
1640
2010
  }
1641
- audioStream.end();
1642
2011
  });
1643
- return audioStream;
1644
2012
  }
1645
2013
  cancel() {
2014
+ this.generation++;
1646
2015
  }
1647
2016
  };
1648
2017
 
@@ -1721,6 +2090,9 @@ var SentenceTTS = class extends TTS {
1721
2090
  this.counter++;
1722
2091
  counter = this.counter;
1723
2092
  this.splitter.reset();
2093
+ this.queue = [];
2094
+ this.controller?.abort();
2095
+ this.controller = void 0;
1724
2096
  return true;
1725
2097
  };
1726
2098
  textStream.on("data", (chunk) => {
@@ -1822,6 +2194,7 @@ export {
1822
2194
  MockTTS,
1823
2195
  Pcm16Resampler,
1824
2196
  Realtime,
2197
+ ReconnectingSocket,
1825
2198
  STT,
1826
2199
  SentenceSplitter,
1827
2200
  SentenceTTS,