@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.d.mts +206 -9
- package/dist/index.d.ts +206 -9
- package/dist/index.js +421 -47
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +420 -47
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -53,6 +53,7 @@ __export(index_exports, {
|
|
|
53
53
|
MockTTS: () => MockTTS,
|
|
54
54
|
Pcm16Resampler: () => Pcm16Resampler,
|
|
55
55
|
Realtime: () => Realtime,
|
|
56
|
+
ReconnectingSocket: () => ReconnectingSocket,
|
|
56
57
|
STT: () => STT,
|
|
57
58
|
SentenceSplitter: () => SentenceSplitter,
|
|
58
59
|
SentenceTTS: () => SentenceTTS,
|
|
@@ -85,32 +86,65 @@ var Agent = class extends import_eventemitter3.EventEmitter {
|
|
|
85
86
|
constructor(options) {
|
|
86
87
|
super();
|
|
87
88
|
this.options = options;
|
|
89
|
+
/**
|
|
90
|
+
* @deprecated Read the signal given to `generateAnswer` instead. Still
|
|
91
|
+
* bumped by every answer and every cancel, for agents written before it.
|
|
92
|
+
*/
|
|
88
93
|
this.answerCount = 0;
|
|
89
|
-
this.answering = false;
|
|
90
94
|
this.conversation = [{ role: "system", content: options.systemPrompt }];
|
|
91
95
|
this.tools = this.getDefaultTools();
|
|
92
96
|
}
|
|
97
|
+
/** Whether an answer is being generated */
|
|
98
|
+
get isAnswering() {
|
|
99
|
+
return !!this.answerController;
|
|
100
|
+
}
|
|
101
|
+
/** @deprecated Read `isAnswering` instead */
|
|
102
|
+
get answering() {
|
|
103
|
+
return this.isAnswering;
|
|
104
|
+
}
|
|
105
|
+
/** @deprecated The answer is over once its stream ends or it is cancelled */
|
|
106
|
+
set answering(_value) {
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Stops the answer being generated, by aborting its signal.
|
|
110
|
+
*
|
|
111
|
+
* An agent overriding it to stop more than the signal does calls
|
|
112
|
+
* `super.cancel()`.
|
|
113
|
+
*/
|
|
114
|
+
cancel() {
|
|
115
|
+
this.answerCount++;
|
|
116
|
+
if (!this.answerController) return;
|
|
117
|
+
this.log("Cancel");
|
|
118
|
+
this.answerController.abort();
|
|
119
|
+
this.answerController = void 0;
|
|
120
|
+
}
|
|
93
121
|
answer() {
|
|
94
122
|
this.log("Start answering");
|
|
95
|
-
|
|
123
|
+
this.answerCount++;
|
|
124
|
+
this.answerController?.abort();
|
|
125
|
+
const controller = new AbortController();
|
|
126
|
+
this.answerController = controller;
|
|
127
|
+
const { signal } = controller;
|
|
96
128
|
const stream = new import_stream.PassThrough();
|
|
97
|
-
this.answering = true;
|
|
98
129
|
Promise.resolve().then(
|
|
99
130
|
() => this.options.onBeforeAnswer?.bind(this)(stream)
|
|
100
131
|
).then((result) => {
|
|
132
|
+
if (signal.aborted) return;
|
|
101
133
|
if (typeof result === "string") {
|
|
102
134
|
stream.write(result);
|
|
103
135
|
this.addAssistantMessage(result);
|
|
104
136
|
return;
|
|
105
137
|
}
|
|
106
138
|
if (result) return;
|
|
107
|
-
return this.generateAnswer(stream);
|
|
139
|
+
return this.generateAnswer(stream, signal);
|
|
140
|
+
}).catch((error) => {
|
|
141
|
+
if (!signal.aborted) this.log("Error answering:", error);
|
|
108
142
|
}).finally(() => {
|
|
109
143
|
if (stream.writable) {
|
|
110
144
|
stream.end();
|
|
111
145
|
}
|
|
112
|
-
if (
|
|
113
|
-
this.
|
|
146
|
+
if (this.answerController === controller) {
|
|
147
|
+
this.answerController = void 0;
|
|
114
148
|
}
|
|
115
149
|
});
|
|
116
150
|
return stream;
|
|
@@ -336,10 +370,10 @@ var FallbackAgent = class extends Agent {
|
|
|
336
370
|
extract(message) {
|
|
337
371
|
return this.agent ? this.agent.extract(message) : super.extract(message);
|
|
338
372
|
}
|
|
339
|
-
async generateAnswer(stream) {
|
|
373
|
+
async generateAnswer(stream, signal) {
|
|
340
374
|
for (let attempt = 0; attempt < this.fallbackOptions.factories.length; attempt++) {
|
|
341
375
|
const agent = this.agent;
|
|
342
|
-
if (!agent) return;
|
|
376
|
+
if (!agent || signal.aborted) return;
|
|
343
377
|
let failed = false;
|
|
344
378
|
const onFailed = () => {
|
|
345
379
|
failed = true;
|
|
@@ -350,7 +384,7 @@ var FallbackAgent = class extends Agent {
|
|
|
350
384
|
} finally {
|
|
351
385
|
agent.off("Failed", onFailed);
|
|
352
386
|
}
|
|
353
|
-
if (!failed) return;
|
|
387
|
+
if (!failed || signal.aborted) return;
|
|
354
388
|
this.log("Agent failed, trying next agent");
|
|
355
389
|
this.startNextAgent();
|
|
356
390
|
}
|
|
@@ -358,6 +392,7 @@ var FallbackAgent = class extends Agent {
|
|
|
358
392
|
this.emit("Failed");
|
|
359
393
|
}
|
|
360
394
|
cancel() {
|
|
395
|
+
super.cancel();
|
|
361
396
|
this.agent?.cancel();
|
|
362
397
|
}
|
|
363
398
|
destroy() {
|
|
@@ -420,10 +455,8 @@ var MockAgent = class extends Agent {
|
|
|
420
455
|
}
|
|
421
456
|
async generateAnswer(stream) {
|
|
422
457
|
const message = `Assistant Message ${this.i++}`;
|
|
423
|
-
this.addAssistantMessage(message);
|
|
424
458
|
stream.write(message);
|
|
425
|
-
|
|
426
|
-
cancel() {
|
|
459
|
+
this.addAssistantMessage(message);
|
|
427
460
|
}
|
|
428
461
|
};
|
|
429
462
|
|
|
@@ -663,6 +696,12 @@ var MicdropServer = class extends import_eventemitter33.EventEmitter {
|
|
|
663
696
|
this.socket = null;
|
|
664
697
|
this.config = null;
|
|
665
698
|
this.startTime = Date.now();
|
|
699
|
+
// Bumped by every cancel, so an answer cancelled before its first word can
|
|
700
|
+
// tell, and leave its message to the answer that replaces it
|
|
701
|
+
this.cancelRound = 0;
|
|
702
|
+
// Counts the assistant messages sent, so the partial answer of one already
|
|
703
|
+
// settled is not sent after it
|
|
704
|
+
this.assistantMessages = 0;
|
|
666
705
|
// Holds the conversation when no agent does, and keeps it readable once the
|
|
667
706
|
// call is over and the config is gone
|
|
668
707
|
this.ownConversation = [];
|
|
@@ -670,10 +709,16 @@ var MicdropServer = class extends import_eventemitter33.EventEmitter {
|
|
|
670
709
|
this.operationQueue = [];
|
|
671
710
|
this.isProcessingQueue = false;
|
|
672
711
|
this.userSpeechChunks = 0;
|
|
673
|
-
|
|
712
|
+
// Bumped whenever the user speaks again or mutes, so a decision taken on an
|
|
713
|
+
// earlier stretch of speech is dropped rather than answering over them
|
|
714
|
+
this.turnRound = 0;
|
|
715
|
+
this.onClose = (code, reason) => {
|
|
674
716
|
this.releaseHeldTurn();
|
|
675
717
|
if (!this.config) return;
|
|
676
|
-
this.log("Connection closed"
|
|
718
|
+
this.log("Connection closed", {
|
|
719
|
+
code,
|
|
720
|
+
reason: reason?.toString() || void 0
|
|
721
|
+
});
|
|
677
722
|
const duration = Math.round((Date.now() - this.startTime) / 1e3);
|
|
678
723
|
const conversation = this.conversation;
|
|
679
724
|
this.agent?.destroy();
|
|
@@ -815,6 +860,7 @@ var MicdropServer = class extends import_eventemitter33.EventEmitter {
|
|
|
815
860
|
this.processQueue();
|
|
816
861
|
}
|
|
817
862
|
cancel() {
|
|
863
|
+
this.cancelRound++;
|
|
818
864
|
this.config?.classifier?.cancel();
|
|
819
865
|
this.config?.tts?.cancel();
|
|
820
866
|
this.agent?.cancel();
|
|
@@ -828,6 +874,8 @@ var MicdropServer = class extends import_eventemitter33.EventEmitter {
|
|
|
828
874
|
this.emit("UserAudio", chunk);
|
|
829
875
|
}
|
|
830
876
|
onMute() {
|
|
877
|
+
this.turnRound++;
|
|
878
|
+
this.releaseHeldTurn();
|
|
831
879
|
this.userSpeechChunks = 0;
|
|
832
880
|
this.currentUserStream?.end();
|
|
833
881
|
this.currentUserStream = void 0;
|
|
@@ -836,6 +884,7 @@ var MicdropServer = class extends import_eventemitter33.EventEmitter {
|
|
|
836
884
|
}
|
|
837
885
|
onStartSpeaking() {
|
|
838
886
|
if (!this.config) return;
|
|
887
|
+
this.turnRound++;
|
|
839
888
|
this.userSpeechChunks = 0;
|
|
840
889
|
this.currentUserStream?.end();
|
|
841
890
|
this.currentUserStream = new import_stream2.PassThrough();
|
|
@@ -887,7 +936,9 @@ var MicdropServer = class extends import_eventemitter33.EventEmitter {
|
|
|
887
936
|
}
|
|
888
937
|
/** Answers, unless the sentence sounds like it has more coming */
|
|
889
938
|
async answerUserTurn() {
|
|
939
|
+
const round = this.turnRound;
|
|
890
940
|
const complete = await (this.turnComplete ?? Promise.resolve(true));
|
|
941
|
+
if (round !== this.turnRound || !this.config) return;
|
|
891
942
|
if (!complete) {
|
|
892
943
|
this.log("Waiting for the rest of the sentence");
|
|
893
944
|
this.socket?.send("SkipAnswer" /* SkipAnswer */);
|
|
@@ -963,6 +1014,7 @@ var MicdropServer = class extends import_eventemitter33.EventEmitter {
|
|
|
963
1014
|
`${"Message" /* Message */} ${JSON.stringify(message)}`
|
|
964
1015
|
);
|
|
965
1016
|
this.emit("Message", message);
|
|
1017
|
+
if (message.role === "assistant") this.assistantMessages++;
|
|
966
1018
|
if (message.role === "user" && this.config?.realtime) this.classifyTurn();
|
|
967
1019
|
}
|
|
968
1020
|
sendPartialAnswer(content) {
|
|
@@ -1009,12 +1061,18 @@ var MicdropServer = class extends import_eventemitter33.EventEmitter {
|
|
|
1009
1061
|
return;
|
|
1010
1062
|
}
|
|
1011
1063
|
this.lastMessageSpeeched = lastMessage;
|
|
1064
|
+
const round = this.cancelRound;
|
|
1012
1065
|
try {
|
|
1066
|
+
const sentBefore = this.assistantMessages;
|
|
1013
1067
|
const stream = this.config.agent.answer();
|
|
1014
1068
|
if (await hasContent(stream)) {
|
|
1015
1069
|
await this._speak(
|
|
1016
|
-
this.config.partialMessages ? this.forwardPartialAnswer(stream) : stream
|
|
1070
|
+
this.config.partialMessages ? this.forwardPartialAnswer(stream, sentBefore) : stream
|
|
1017
1071
|
);
|
|
1072
|
+
} else if (round !== this.cancelRound) {
|
|
1073
|
+
if (this.lastMessageSpeeched === lastMessage) {
|
|
1074
|
+
this.lastMessageSpeeched = void 0;
|
|
1075
|
+
}
|
|
1018
1076
|
} else {
|
|
1019
1077
|
this.socket?.send("SkipAnswer" /* SkipAnswer */);
|
|
1020
1078
|
}
|
|
@@ -1023,10 +1081,19 @@ var MicdropServer = class extends import_eventemitter33.EventEmitter {
|
|
|
1023
1081
|
throw error;
|
|
1024
1082
|
}
|
|
1025
1083
|
}
|
|
1026
|
-
/**
|
|
1027
|
-
|
|
1084
|
+
/**
|
|
1085
|
+
* Sends the answer to the client as it is written, ahead of the voice.
|
|
1086
|
+
*
|
|
1087
|
+
* The stream reaches here a moment after the agent wrote it, so the agent
|
|
1088
|
+
* can record the complete message first, which the client then shows. A
|
|
1089
|
+
* partial answer arriving after it would show the answer twice, so the
|
|
1090
|
+
* forwarding stops once the message is sent.
|
|
1091
|
+
*/
|
|
1092
|
+
forwardPartialAnswer(stream, sentBefore) {
|
|
1028
1093
|
let content = "";
|
|
1029
|
-
const send = (text) =>
|
|
1094
|
+
const send = (text) => {
|
|
1095
|
+
if (this.assistantMessages === sentBefore) this.sendPartialAnswer(text);
|
|
1096
|
+
};
|
|
1030
1097
|
const forward = new import_stream2.Transform({
|
|
1031
1098
|
transform(chunk, _encoding, callback) {
|
|
1032
1099
|
content += chunk.toString();
|
|
@@ -1204,6 +1271,7 @@ var Realtime = class extends Agent {
|
|
|
1204
1271
|
});
|
|
1205
1272
|
}
|
|
1206
1273
|
cancel() {
|
|
1274
|
+
super.cancel();
|
|
1207
1275
|
this.queue = [];
|
|
1208
1276
|
const answer = this.currentAnswer;
|
|
1209
1277
|
if (!answer) return;
|
|
@@ -1531,19 +1599,281 @@ var SharedInstances = class {
|
|
|
1531
1599
|
this.entries.set(id, loading);
|
|
1532
1600
|
return loading;
|
|
1533
1601
|
}
|
|
1602
|
+
/** The instance for this key, loaded or being loaded, without loading it. */
|
|
1603
|
+
get(key) {
|
|
1604
|
+
return this.entries.get(typeof key === "string" ? key : JSON.stringify(key));
|
|
1605
|
+
}
|
|
1534
1606
|
/** Whether an instance is loaded, or being loaded, for this key. */
|
|
1535
1607
|
has(key) {
|
|
1536
1608
|
return this.entries.has(typeof key === "string" ? key : JSON.stringify(key));
|
|
1537
1609
|
}
|
|
1610
|
+
/**
|
|
1611
|
+
* Forgets the instance for this key, so the next call loads a new one. For
|
|
1612
|
+
* an instance that broke, a crashed worker for instance. Releasing what it
|
|
1613
|
+
* holds is left to the caller.
|
|
1614
|
+
*/
|
|
1615
|
+
delete(key) {
|
|
1616
|
+
this.entries.delete(typeof key === "string" ? key : JSON.stringify(key));
|
|
1617
|
+
}
|
|
1538
1618
|
/** Drops every instance, so the next call loads again. */
|
|
1539
1619
|
clear() {
|
|
1540
1620
|
this.entries.clear();
|
|
1541
1621
|
}
|
|
1542
1622
|
};
|
|
1543
1623
|
|
|
1544
|
-
// src/
|
|
1624
|
+
// src/socket/ReconnectingSocket.ts
|
|
1545
1625
|
var import_eventemitter35 = require("eventemitter3");
|
|
1546
|
-
var
|
|
1626
|
+
var import_ws = __toESM(require("ws"));
|
|
1627
|
+
var DEFAULT_CONNECTION_TIMEOUT = 5e3;
|
|
1628
|
+
var DEFAULT_RETRY_DELAY = 1e3;
|
|
1629
|
+
var DEFAULT_MAX_RETRY = 3;
|
|
1630
|
+
var ReconnectingSocket = class extends import_eventemitter35.EventEmitter {
|
|
1631
|
+
constructor(options) {
|
|
1632
|
+
super();
|
|
1633
|
+
this.options = options;
|
|
1634
|
+
this._status = "idle";
|
|
1635
|
+
this.everOpened = false;
|
|
1636
|
+
this.retryCount = 0;
|
|
1637
|
+
// Numbers the attempts, so one overtaken while it awaited its address stops
|
|
1638
|
+
this.attemptId = 0;
|
|
1639
|
+
this.waiters = [];
|
|
1640
|
+
}
|
|
1641
|
+
get status() {
|
|
1642
|
+
return this._status;
|
|
1643
|
+
}
|
|
1644
|
+
/** Whether a message sent now goes out */
|
|
1645
|
+
get isOpen() {
|
|
1646
|
+
return this._status === "open";
|
|
1647
|
+
}
|
|
1648
|
+
/**
|
|
1649
|
+
* Opens the connection. Called again after `failed`, it starts over with a
|
|
1650
|
+
* full retry budget, and does nothing in any other status.
|
|
1651
|
+
*/
|
|
1652
|
+
connect() {
|
|
1653
|
+
if (this._status !== "idle" && this._status !== "failed") return;
|
|
1654
|
+
this.retryCount = 0;
|
|
1655
|
+
this.attempt();
|
|
1656
|
+
}
|
|
1657
|
+
/**
|
|
1658
|
+
* Waits for the connection to be open.
|
|
1659
|
+
* @returns True once open, false if it failed or was destroyed meanwhile
|
|
1660
|
+
*/
|
|
1661
|
+
ready() {
|
|
1662
|
+
if (this._status === "open") return Promise.resolve(true);
|
|
1663
|
+
if (this._status === "destroyed" || this._status === "failed") {
|
|
1664
|
+
return Promise.resolve(false);
|
|
1665
|
+
}
|
|
1666
|
+
return new Promise((resolve) => this.waiters.push(resolve));
|
|
1667
|
+
}
|
|
1668
|
+
/**
|
|
1669
|
+
* Sends a message if the connection is open
|
|
1670
|
+
* @returns False when it did not go out
|
|
1671
|
+
*/
|
|
1672
|
+
send(data) {
|
|
1673
|
+
if (this._status !== "open" || !this.socket) return false;
|
|
1674
|
+
this.socket.send(data);
|
|
1675
|
+
return true;
|
|
1676
|
+
}
|
|
1677
|
+
/**
|
|
1678
|
+
* Drops the connection and opens a new one right away, for a provider that
|
|
1679
|
+
* can only stop what it is doing by closing. It costs no retry, and the new
|
|
1680
|
+
* connection is not a reconnection.
|
|
1681
|
+
*/
|
|
1682
|
+
restart() {
|
|
1683
|
+
if (this._status === "destroyed") return;
|
|
1684
|
+
this.log("Restarting the connection");
|
|
1685
|
+
this.clearRetryTimer();
|
|
1686
|
+
this.drop();
|
|
1687
|
+
this.everOpened = false;
|
|
1688
|
+
this.retryCount = 0;
|
|
1689
|
+
this.attempt();
|
|
1690
|
+
}
|
|
1691
|
+
/** Closes the connection for good */
|
|
1692
|
+
destroy() {
|
|
1693
|
+
if (this._status === "destroyed") return;
|
|
1694
|
+
this.clearRetryTimer();
|
|
1695
|
+
this.drop();
|
|
1696
|
+
this.setStatus("destroyed");
|
|
1697
|
+
this.settleWaiters(false);
|
|
1698
|
+
this.removeAllListeners();
|
|
1699
|
+
}
|
|
1700
|
+
async attempt() {
|
|
1701
|
+
const id = ++this.attemptId;
|
|
1702
|
+
const isOvertaken = () => id !== this.attemptId || this._status !== "connecting";
|
|
1703
|
+
this.setStatus("connecting");
|
|
1704
|
+
let url;
|
|
1705
|
+
try {
|
|
1706
|
+
const { url: getUrl } = this.options;
|
|
1707
|
+
url = typeof getUrl === "string" ? getUrl : await getUrl();
|
|
1708
|
+
} catch (error) {
|
|
1709
|
+
if (isOvertaken()) return;
|
|
1710
|
+
this.onAttemptFailed(error);
|
|
1711
|
+
return;
|
|
1712
|
+
}
|
|
1713
|
+
if (isOvertaken()) return;
|
|
1714
|
+
const socket = new import_ws.default(url, this.options.clientOptions);
|
|
1715
|
+
this.socket = socket;
|
|
1716
|
+
let settled = false;
|
|
1717
|
+
const fail = (error) => {
|
|
1718
|
+
if (settled || this.socket !== socket) return;
|
|
1719
|
+
settled = true;
|
|
1720
|
+
clearTimeout(timeout);
|
|
1721
|
+
this.drop();
|
|
1722
|
+
this.onAttemptFailed(error);
|
|
1723
|
+
};
|
|
1724
|
+
const timeout = setTimeout(
|
|
1725
|
+
() => fail(new Error("Connection timeout")),
|
|
1726
|
+
this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT
|
|
1727
|
+
);
|
|
1728
|
+
socket.on("error", (error) => {
|
|
1729
|
+
this.log("WebSocket error:", error.message);
|
|
1730
|
+
fail(error);
|
|
1731
|
+
});
|
|
1732
|
+
const { handshake } = this.options;
|
|
1733
|
+
const open = () => {
|
|
1734
|
+
settled = true;
|
|
1735
|
+
clearTimeout(timeout);
|
|
1736
|
+
this.onOpen();
|
|
1737
|
+
};
|
|
1738
|
+
socket.on("open", () => {
|
|
1739
|
+
if (this.socket !== socket) return;
|
|
1740
|
+
try {
|
|
1741
|
+
handshake?.start?.(socket);
|
|
1742
|
+
} catch (error) {
|
|
1743
|
+
fail(error);
|
|
1744
|
+
return;
|
|
1745
|
+
}
|
|
1746
|
+
if (!handshake?.read) open();
|
|
1747
|
+
});
|
|
1748
|
+
socket.on("message", (data, isBinary) => {
|
|
1749
|
+
if (this.socket !== socket) return;
|
|
1750
|
+
if (this._status === "open") {
|
|
1751
|
+
this.prove();
|
|
1752
|
+
this.emit("Message", data, isBinary);
|
|
1753
|
+
return;
|
|
1754
|
+
}
|
|
1755
|
+
if (settled || !handshake?.read) return;
|
|
1756
|
+
try {
|
|
1757
|
+
if (handshake.read(data, isBinary)) open();
|
|
1758
|
+
} catch (error) {
|
|
1759
|
+
fail(error);
|
|
1760
|
+
}
|
|
1761
|
+
});
|
|
1762
|
+
socket.on("close", (code, reasonBuffer) => {
|
|
1763
|
+
const reason = reasonBuffer.toString();
|
|
1764
|
+
if (!settled) {
|
|
1765
|
+
fail(new Error(`Connection closed (${code}) ${reason}`));
|
|
1766
|
+
return;
|
|
1767
|
+
}
|
|
1768
|
+
if (this.socket !== socket) return;
|
|
1769
|
+
this.log("Connection closed", { code, reason });
|
|
1770
|
+
this.drop();
|
|
1771
|
+
this.setStatus("reconnecting");
|
|
1772
|
+
this.emit("Close", code, reason);
|
|
1773
|
+
if (this._status !== "reconnecting") return;
|
|
1774
|
+
this.scheduleRetry(new Error(`Connection closed (${code}) ${reason}`));
|
|
1775
|
+
});
|
|
1776
|
+
}
|
|
1777
|
+
onOpen() {
|
|
1778
|
+
this.log("Connection opened");
|
|
1779
|
+
const reconnected = this.everOpened;
|
|
1780
|
+
this.everOpened = true;
|
|
1781
|
+
if (this.options.handshake?.read) {
|
|
1782
|
+
this.prove();
|
|
1783
|
+
} else {
|
|
1784
|
+
this.proofTimer = setTimeout(
|
|
1785
|
+
() => this.prove(),
|
|
1786
|
+
this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT
|
|
1787
|
+
);
|
|
1788
|
+
}
|
|
1789
|
+
this.startKeepAlive();
|
|
1790
|
+
this.setStatus("open");
|
|
1791
|
+
this.emit("Open", { reconnected });
|
|
1792
|
+
this.settleWaiters(true);
|
|
1793
|
+
}
|
|
1794
|
+
/** The connection works, the retry budget is whole again */
|
|
1795
|
+
prove() {
|
|
1796
|
+
this.clearProofTimer();
|
|
1797
|
+
this.retryCount = 0;
|
|
1798
|
+
}
|
|
1799
|
+
clearProofTimer() {
|
|
1800
|
+
if (!this.proofTimer) return;
|
|
1801
|
+
clearTimeout(this.proofTimer);
|
|
1802
|
+
this.proofTimer = void 0;
|
|
1803
|
+
}
|
|
1804
|
+
onAttemptFailed(error) {
|
|
1805
|
+
this.log("Connection failed:", error.message);
|
|
1806
|
+
this.scheduleRetry(error);
|
|
1807
|
+
}
|
|
1808
|
+
scheduleRetry(error) {
|
|
1809
|
+
if (this._status === "destroyed") return;
|
|
1810
|
+
this.retryCount++;
|
|
1811
|
+
if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY)) {
|
|
1812
|
+
this.log("Max retries reached, giving up");
|
|
1813
|
+
this.setStatus("failed");
|
|
1814
|
+
this.settleWaiters(false);
|
|
1815
|
+
this.emit("Failed", error);
|
|
1816
|
+
return;
|
|
1817
|
+
}
|
|
1818
|
+
this.setStatus("reconnecting");
|
|
1819
|
+
this.log(`Reconnecting (attempt ${this.retryCount})`);
|
|
1820
|
+
this.clearRetryTimer();
|
|
1821
|
+
this.retryTimer = setTimeout(() => {
|
|
1822
|
+
this.retryTimer = void 0;
|
|
1823
|
+
if (this._status === "reconnecting") this.attempt();
|
|
1824
|
+
}, this.options.retryDelay ?? DEFAULT_RETRY_DELAY);
|
|
1825
|
+
}
|
|
1826
|
+
/** Lets go of the current socket, which will never be heard from again */
|
|
1827
|
+
drop() {
|
|
1828
|
+
this.stopKeepAlive();
|
|
1829
|
+
this.clearProofTimer();
|
|
1830
|
+
const socket = this.socket;
|
|
1831
|
+
this.socket = void 0;
|
|
1832
|
+
if (!socket) return;
|
|
1833
|
+
socket.removeAllListeners();
|
|
1834
|
+
socket.on("error", () => {
|
|
1835
|
+
});
|
|
1836
|
+
if (socket.readyState === import_ws.default.OPEN || socket.readyState === import_ws.default.CONNECTING) {
|
|
1837
|
+
socket.close(1e3);
|
|
1838
|
+
}
|
|
1839
|
+
}
|
|
1840
|
+
startKeepAlive() {
|
|
1841
|
+
const keepAlive = this.options.keepAlive;
|
|
1842
|
+
if (!keepAlive) return;
|
|
1843
|
+
this.stopKeepAlive();
|
|
1844
|
+
this.keepAliveTimer = setInterval(() => {
|
|
1845
|
+
const { message } = keepAlive;
|
|
1846
|
+
this.send(typeof message === "string" ? message : message());
|
|
1847
|
+
}, keepAlive.interval);
|
|
1848
|
+
}
|
|
1849
|
+
stopKeepAlive() {
|
|
1850
|
+
if (!this.keepAliveTimer) return;
|
|
1851
|
+
clearInterval(this.keepAliveTimer);
|
|
1852
|
+
this.keepAliveTimer = void 0;
|
|
1853
|
+
}
|
|
1854
|
+
clearRetryTimer() {
|
|
1855
|
+
if (!this.retryTimer) return;
|
|
1856
|
+
clearTimeout(this.retryTimer);
|
|
1857
|
+
this.retryTimer = void 0;
|
|
1858
|
+
}
|
|
1859
|
+
settleWaiters(open) {
|
|
1860
|
+
const waiters = this.waiters;
|
|
1861
|
+
this.waiters = [];
|
|
1862
|
+
waiters.forEach((resolve) => resolve(open));
|
|
1863
|
+
}
|
|
1864
|
+
setStatus(status) {
|
|
1865
|
+
if (this._status === status) return;
|
|
1866
|
+
this._status = status;
|
|
1867
|
+
this.emit("StatusChange", status);
|
|
1868
|
+
}
|
|
1869
|
+
log(...message) {
|
|
1870
|
+
this.options.log?.(...message);
|
|
1871
|
+
}
|
|
1872
|
+
};
|
|
1873
|
+
|
|
1874
|
+
// src/stt/STT.ts
|
|
1875
|
+
var import_eventemitter36 = require("eventemitter3");
|
|
1876
|
+
var STT = class extends import_eventemitter36.EventEmitter {
|
|
1547
1877
|
log(...message) {
|
|
1548
1878
|
this.logger?.log(...message);
|
|
1549
1879
|
}
|
|
@@ -1569,24 +1899,31 @@ var MockSTT = class extends STT {
|
|
|
1569
1899
|
// src/stt/FallbackSTT.ts
|
|
1570
1900
|
var import_stream3 = require("stream");
|
|
1571
1901
|
var FallbackSTT = class extends STT {
|
|
1572
|
-
// Start at -1 because we need to increment it before using it
|
|
1573
1902
|
constructor(options) {
|
|
1574
1903
|
super();
|
|
1575
1904
|
this.options = options;
|
|
1576
1905
|
this.stt = null;
|
|
1577
1906
|
this.sttIndex = -1;
|
|
1907
|
+
// Start at -1 because we need to increment it before using it
|
|
1908
|
+
// In the order spoken, each STT giving one transcript per utterance in turn
|
|
1909
|
+
this.pending = [];
|
|
1578
1910
|
this.onTranscript = (transcript) => {
|
|
1911
|
+
this.pending.shift();
|
|
1579
1912
|
this.emit("Transcript", transcript);
|
|
1580
1913
|
};
|
|
1581
|
-
|
|
1914
|
+
/**
|
|
1915
|
+
* Hands every utterance still waiting to the next STT, one stream each so
|
|
1916
|
+
* each gets its own transcript. One still being spoken keeps going there.
|
|
1917
|
+
*/
|
|
1918
|
+
this.onFailed = () => {
|
|
1582
1919
|
this.log("STT failed, trying next STT");
|
|
1583
1920
|
this.startNextSTT();
|
|
1584
|
-
if (
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
this.stt?.transcribe(stream);
|
|
1588
|
-
chunks.forEach((chunk) => stream.write(chunk));
|
|
1589
|
-
stream.end();
|
|
1921
|
+
if (this.pending.length > 0) this.log("Sending audio chunks again");
|
|
1922
|
+
for (const utterance of this.pending) {
|
|
1923
|
+
utterance.stream = new import_stream3.PassThrough();
|
|
1924
|
+
this.stt?.transcribe(utterance.stream);
|
|
1925
|
+
utterance.chunks.forEach((chunk) => utterance.stream.write(chunk));
|
|
1926
|
+
if (utterance.ended) utterance.stream.end();
|
|
1590
1927
|
}
|
|
1591
1928
|
};
|
|
1592
1929
|
if (this.options.factories.length === 0) {
|
|
@@ -1595,13 +1932,28 @@ var FallbackSTT = class extends STT {
|
|
|
1595
1932
|
this.startNextSTT();
|
|
1596
1933
|
}
|
|
1597
1934
|
transcribe(audioStream) {
|
|
1598
|
-
|
|
1935
|
+
const utterance = {
|
|
1936
|
+
chunks: [],
|
|
1937
|
+
ended: false,
|
|
1938
|
+
stream: new import_stream3.PassThrough()
|
|
1939
|
+
};
|
|
1940
|
+
this.pending.push(utterance);
|
|
1941
|
+
audioStream.on("data", (chunk) => {
|
|
1942
|
+
utterance.chunks.push(chunk);
|
|
1943
|
+
utterance.stream.write(chunk);
|
|
1944
|
+
});
|
|
1945
|
+
audioStream.on("end", () => {
|
|
1946
|
+
utterance.ended = true;
|
|
1947
|
+
utterance.stream.end();
|
|
1948
|
+
});
|
|
1949
|
+
this.stt?.transcribe(utterance.stream);
|
|
1599
1950
|
}
|
|
1600
1951
|
destroy() {
|
|
1601
1952
|
super.destroy();
|
|
1602
1953
|
this.stt?.destroy();
|
|
1603
1954
|
this.stt = null;
|
|
1604
1955
|
this.sttIndex = -1;
|
|
1956
|
+
this.pending = [];
|
|
1605
1957
|
}
|
|
1606
1958
|
startNextSTT() {
|
|
1607
1959
|
this.sttIndex++;
|
|
@@ -1624,8 +1976,8 @@ var FallbackSTT = class extends STT {
|
|
|
1624
1976
|
var import_stream4 = require("stream");
|
|
1625
1977
|
|
|
1626
1978
|
// src/tts/TTS.ts
|
|
1627
|
-
var
|
|
1628
|
-
var TTS = class extends
|
|
1979
|
+
var import_eventemitter37 = require("eventemitter3");
|
|
1980
|
+
var TTS = class extends import_eventemitter37.EventEmitter {
|
|
1629
1981
|
log(...message) {
|
|
1630
1982
|
this.logger?.log(...message);
|
|
1631
1983
|
}
|
|
@@ -1637,7 +1989,6 @@ var TTS = class extends import_eventemitter36.EventEmitter {
|
|
|
1637
1989
|
|
|
1638
1990
|
// src/tts/FallbackTTS.ts
|
|
1639
1991
|
var FallbackTTS = class extends TTS {
|
|
1640
|
-
// Start at -1 because we need to increment it before using it
|
|
1641
1992
|
constructor(options) {
|
|
1642
1993
|
super();
|
|
1643
1994
|
this.options = options;
|
|
@@ -1646,16 +1997,22 @@ var FallbackTTS = class extends TTS {
|
|
|
1646
1997
|
this.onAudio = (audio) => {
|
|
1647
1998
|
this.emit("Audio", audio);
|
|
1648
1999
|
};
|
|
2000
|
+
/**
|
|
2001
|
+
* Hands the unspoken text to the next TTS. An utterance whose text is still
|
|
2002
|
+
* being written goes on there, so its end is spoken too.
|
|
2003
|
+
*/
|
|
1649
2004
|
this.onFailed = (chunks) => {
|
|
1650
2005
|
this.log("TTS failed, trying next TTS");
|
|
1651
2006
|
this.startNextTTS();
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
2007
|
+
const utterance = this.current;
|
|
2008
|
+
const continues = !!utterance && !utterance.ended;
|
|
2009
|
+
if (chunks.length === 0 && !continues) return;
|
|
2010
|
+
this.log("Sending text chunks again");
|
|
2011
|
+
const stream = new import_stream4.PassThrough();
|
|
2012
|
+
this.tts?.speak(stream);
|
|
2013
|
+
chunks.forEach((chunk) => stream.write(chunk));
|
|
2014
|
+
if (continues) utterance.stream = stream;
|
|
2015
|
+
else stream.end();
|
|
1659
2016
|
};
|
|
1660
2017
|
if (this.options.factories.length === 0) {
|
|
1661
2018
|
throw new Error("FallbackTTS: No factories provided");
|
|
@@ -1663,9 +2020,20 @@ var FallbackTTS = class extends TTS {
|
|
|
1663
2020
|
this.startNextTTS();
|
|
1664
2021
|
}
|
|
1665
2022
|
speak(textStream) {
|
|
1666
|
-
|
|
2023
|
+
const utterance = {
|
|
2024
|
+
ended: false,
|
|
2025
|
+
stream: new import_stream4.PassThrough()
|
|
2026
|
+
};
|
|
2027
|
+
this.current = utterance;
|
|
2028
|
+
textStream.on("data", (chunk) => utterance.stream.write(chunk));
|
|
2029
|
+
textStream.on("end", () => {
|
|
2030
|
+
utterance.ended = true;
|
|
2031
|
+
utterance.stream.end();
|
|
2032
|
+
});
|
|
2033
|
+
this.tts?.speak(utterance.stream);
|
|
1667
2034
|
}
|
|
1668
2035
|
cancel() {
|
|
2036
|
+
this.current = void 0;
|
|
1669
2037
|
this.tts?.cancel();
|
|
1670
2038
|
}
|
|
1671
2039
|
destroy() {
|
|
@@ -1693,26 +2061,28 @@ var FallbackTTS = class extends TTS {
|
|
|
1693
2061
|
|
|
1694
2062
|
// src/tts/MockTTS.ts
|
|
1695
2063
|
var fs = __toESM(require("fs"));
|
|
1696
|
-
var import_stream5 = require("stream");
|
|
1697
2064
|
var MockTTS = class extends TTS {
|
|
1698
2065
|
constructor(audioFilePaths) {
|
|
1699
2066
|
super();
|
|
1700
2067
|
this.audioFilePaths = audioFilePaths;
|
|
2068
|
+
// Bumped by every answer and every cancel, so the chunks of an answer that
|
|
2069
|
+
// is no longer wanted stop there
|
|
2070
|
+
this.generation = 0;
|
|
1701
2071
|
}
|
|
1702
2072
|
speak(textStream) {
|
|
1703
|
-
const
|
|
2073
|
+
const generation = ++this.generation;
|
|
1704
2074
|
textStream.once("data", async () => {
|
|
1705
2075
|
for (const filePath of this.audioFilePaths) {
|
|
1706
2076
|
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
2077
|
+
if (generation !== this.generation) return;
|
|
2078
|
+
const audio = fs.readFileSync(filePath);
|
|
2079
|
+
this.log(`Loaded chunk (${audio.length} bytes)`);
|
|
2080
|
+
this.emit("Audio", audio);
|
|
1710
2081
|
}
|
|
1711
|
-
audioStream.end();
|
|
1712
2082
|
});
|
|
1713
|
-
return audioStream;
|
|
1714
2083
|
}
|
|
1715
2084
|
cancel() {
|
|
2085
|
+
this.generation++;
|
|
1716
2086
|
}
|
|
1717
2087
|
};
|
|
1718
2088
|
|
|
@@ -1791,6 +2161,9 @@ var SentenceTTS = class extends TTS {
|
|
|
1791
2161
|
this.counter++;
|
|
1792
2162
|
counter = this.counter;
|
|
1793
2163
|
this.splitter.reset();
|
|
2164
|
+
this.queue = [];
|
|
2165
|
+
this.controller?.abort();
|
|
2166
|
+
this.controller = void 0;
|
|
1794
2167
|
return true;
|
|
1795
2168
|
};
|
|
1796
2169
|
textStream.on("data", (chunk) => {
|
|
@@ -1893,6 +2266,7 @@ async function waitForParams(socket, validate) {
|
|
|
1893
2266
|
MockTTS,
|
|
1894
2267
|
Pcm16Resampler,
|
|
1895
2268
|
Realtime,
|
|
2269
|
+
ReconnectingSocket,
|
|
1896
2270
|
STT,
|
|
1897
2271
|
SentenceSplitter,
|
|
1898
2272
|
SentenceTTS,
|