@micdrop/server 2.10.0 → 2.11.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/README.md +12 -12
- package/dist/index.d.mts +168 -24
- package/dist/index.d.ts +168 -24
- package/dist/index.js +250 -92
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +243 -92
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -38,6 +38,7 @@ __export(index_exports, {
|
|
|
38
38
|
AUTO_SEMANTIC_TURN_TOOL_NAME: () => AUTO_SEMANTIC_TURN_TOOL_NAME,
|
|
39
39
|
Agent: () => Agent,
|
|
40
40
|
Classifier: () => Classifier,
|
|
41
|
+
DEFAULT_SAMPLE_RATE: () => DEFAULT_SAMPLE_RATE,
|
|
41
42
|
FallbackAgent: () => FallbackAgent,
|
|
42
43
|
FallbackSTT: () => FallbackSTT,
|
|
43
44
|
FallbackTTS: () => FallbackTTS,
|
|
@@ -51,6 +52,7 @@ __export(index_exports, {
|
|
|
51
52
|
MockAgent: () => MockAgent,
|
|
52
53
|
MockSTT: () => MockSTT,
|
|
53
54
|
MockTTS: () => MockTTS,
|
|
55
|
+
Pcm16Aligner: () => Pcm16Aligner,
|
|
54
56
|
Pcm16Resampler: () => Pcm16Resampler,
|
|
55
57
|
Realtime: () => Realtime,
|
|
56
58
|
ReconnectingSocket: () => ReconnectingSocket,
|
|
@@ -59,12 +61,17 @@ __export(index_exports, {
|
|
|
59
61
|
SentenceTTS: () => SentenceTTS,
|
|
60
62
|
SharedInstances: () => SharedInstances,
|
|
61
63
|
TTS: () => TTS,
|
|
64
|
+
cachedList: () => cachedList,
|
|
65
|
+
cachedModels: () => cachedModels,
|
|
66
|
+
cachedVoices: () => cachedVoices,
|
|
62
67
|
currentTurn: () => currentTurn,
|
|
63
68
|
float32ToPcm16: () => float32ToPcm16,
|
|
64
69
|
getTurnClassification: () => getTurnClassification,
|
|
65
70
|
handleError: () => handleError,
|
|
66
71
|
pcm16ToFloat32: () => pcm16ToFloat32,
|
|
67
72
|
turnInput: () => turnInput,
|
|
73
|
+
voiceGender: () => voiceGender,
|
|
74
|
+
voicesForLanguage: () => voicesForLanguage,
|
|
68
75
|
waitForParams: () => waitForParams
|
|
69
76
|
});
|
|
70
77
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -267,45 +274,48 @@ var Agent = class extends import_eventemitter3.EventEmitter {
|
|
|
267
274
|
};
|
|
268
275
|
}
|
|
269
276
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
return
|
|
277
|
+
/**
|
|
278
|
+
* Writes the answer to the stream as it is generated, leaving out the JSON
|
|
279
|
+
* value to extract: the voice stops at its first `{`.
|
|
280
|
+
*
|
|
281
|
+
* The value is cut from the message once the answer is complete, by
|
|
282
|
+
* `extract()`. Only the speech has to be cut while streaming.
|
|
283
|
+
*/
|
|
284
|
+
answerWriter(stream) {
|
|
285
|
+
let extracting = false;
|
|
286
|
+
return (text) => {
|
|
287
|
+
if (extracting || !text) return;
|
|
288
|
+
if (this.options.extract) {
|
|
289
|
+
const start = text.indexOf("{");
|
|
290
|
+
if (start !== -1) {
|
|
291
|
+
extracting = true;
|
|
292
|
+
text = text.slice(0, start).trimEnd();
|
|
293
|
+
if (!text) return;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
stream.write(text);
|
|
297
|
+
};
|
|
280
298
|
}
|
|
299
|
+
/** Cuts the JSON value to extract from a complete answer, and parses it */
|
|
281
300
|
extract(message) {
|
|
282
|
-
const
|
|
301
|
+
const options = this.options.extract;
|
|
283
302
|
let metadata = void 0;
|
|
284
|
-
if (
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
}
|
|
299
|
-
} catch (error) {
|
|
300
|
-
console.error(
|
|
301
|
-
`[OpenaiAgent] Error parsing extracted value (${extractedText}):`,
|
|
302
|
-
error
|
|
303
|
-
);
|
|
304
|
-
}
|
|
305
|
-
message = message.slice(0, startTagIndex).trimEnd();
|
|
306
|
-
}
|
|
303
|
+
if (!options) return { message, metadata };
|
|
304
|
+
const start = message.indexOf("{");
|
|
305
|
+
if (start === -1) return { message, metadata };
|
|
306
|
+
const end = message.lastIndexOf("}");
|
|
307
|
+
const extractedText = message.slice(start, end === -1 ? void 0 : end + 1).trim();
|
|
308
|
+
try {
|
|
309
|
+
const value = JSON.parse(extractedText);
|
|
310
|
+
options.callback?.(value);
|
|
311
|
+
if (options.saveInMetadata) metadata = { extracted: value };
|
|
312
|
+
} catch (error) {
|
|
313
|
+
console.error(
|
|
314
|
+
`[Agent] Error parsing extracted value (${extractedText}):`,
|
|
315
|
+
error
|
|
316
|
+
);
|
|
307
317
|
}
|
|
308
|
-
return { message, metadata };
|
|
318
|
+
return { message: message.slice(0, start).trimEnd(), metadata };
|
|
309
319
|
}
|
|
310
320
|
/**
|
|
311
321
|
* Emits one of the events every agent has.
|
|
@@ -539,8 +549,10 @@ var MicdropServerCommands = /* @__PURE__ */ ((MicdropServerCommands2) => {
|
|
|
539
549
|
MicdropServerCommands2["EndCall"] = "EndCall";
|
|
540
550
|
MicdropServerCommands2["ToolCall"] = "ToolCall";
|
|
541
551
|
MicdropServerCommands2["Classification"] = "Classification";
|
|
552
|
+
MicdropServerCommands2["SampleRate"] = "SampleRate";
|
|
542
553
|
return MicdropServerCommands2;
|
|
543
554
|
})(MicdropServerCommands || {});
|
|
555
|
+
var DEFAULT_SAMPLE_RATE = 16e3;
|
|
544
556
|
function currentTurn(conversation) {
|
|
545
557
|
let start = conversation.length;
|
|
546
558
|
while (start > 0) {
|
|
@@ -598,6 +610,20 @@ function turnInput(conversation, history = 1) {
|
|
|
598
610
|
return { input, message: turn.messages[turn.messages.length - 1] };
|
|
599
611
|
}
|
|
600
612
|
|
|
613
|
+
// src/audio/Pcm16Aligner.ts
|
|
614
|
+
var Pcm16Aligner = class {
|
|
615
|
+
process(chunk) {
|
|
616
|
+
const buf = this.leftover ? Buffer.concat([this.leftover, chunk]) : chunk;
|
|
617
|
+
const length = buf.length - buf.length % 2;
|
|
618
|
+
this.leftover = length < buf.length ? buf.subarray(length) : void 0;
|
|
619
|
+
return buf.subarray(0, length);
|
|
620
|
+
}
|
|
621
|
+
/** Forgets the held byte, to start an independent stream */
|
|
622
|
+
reset() {
|
|
623
|
+
this.leftover = void 0;
|
|
624
|
+
}
|
|
625
|
+
};
|
|
626
|
+
|
|
601
627
|
// src/audio/Pcm16Resampler.ts
|
|
602
628
|
var Pcm16Resampler = class {
|
|
603
629
|
// Fractional position into the first sample of the buffer
|
|
@@ -684,10 +710,107 @@ function handleError(socket, error) {
|
|
|
684
710
|
socket.terminate();
|
|
685
711
|
}
|
|
686
712
|
|
|
713
|
+
// src/lists.ts
|
|
714
|
+
var import_crypto = require("crypto");
|
|
715
|
+
|
|
716
|
+
// src/SharedInstances.ts
|
|
717
|
+
var SharedInstances = class {
|
|
718
|
+
constructor(options = {}) {
|
|
719
|
+
this.options = options;
|
|
720
|
+
this.entries = /* @__PURE__ */ new Map();
|
|
721
|
+
// When each instance expires, for the ones that do
|
|
722
|
+
this.expiries = /* @__PURE__ */ new Map();
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* Returns the instance for this key, calling `create` the first time.
|
|
726
|
+
*
|
|
727
|
+
* The key can be any JSON-serializable value, usually an array of the
|
|
728
|
+
* options that define the instance. Concurrent callers get the same promise
|
|
729
|
+
* instead of loading the model twice.
|
|
730
|
+
*/
|
|
731
|
+
load(key, create) {
|
|
732
|
+
const id = typeof key === "string" ? key : JSON.stringify(key);
|
|
733
|
+
this.expire(id);
|
|
734
|
+
const existing = this.entries.get(id);
|
|
735
|
+
if (existing) return existing;
|
|
736
|
+
let loading;
|
|
737
|
+
try {
|
|
738
|
+
loading = create();
|
|
739
|
+
} catch (error) {
|
|
740
|
+
return Promise.reject(error);
|
|
741
|
+
}
|
|
742
|
+
loading.catch(() => {
|
|
743
|
+
if (this.entries.get(id) === loading) this.entries.delete(id);
|
|
744
|
+
});
|
|
745
|
+
this.entries.set(id, loading);
|
|
746
|
+
const { ttl } = this.options;
|
|
747
|
+
if (ttl !== void 0) {
|
|
748
|
+
loading.then(
|
|
749
|
+
() => {
|
|
750
|
+
if (this.entries.get(id) === loading) {
|
|
751
|
+
this.expiries.set(id, Date.now() + ttl);
|
|
752
|
+
}
|
|
753
|
+
},
|
|
754
|
+
() => {
|
|
755
|
+
}
|
|
756
|
+
);
|
|
757
|
+
}
|
|
758
|
+
return loading;
|
|
759
|
+
}
|
|
760
|
+
/** Forgets an instance whose time is up */
|
|
761
|
+
expire(id) {
|
|
762
|
+
const expiry = this.expiries.get(id);
|
|
763
|
+
if (expiry === void 0 || Date.now() < expiry) return;
|
|
764
|
+
this.entries.delete(id);
|
|
765
|
+
this.expiries.delete(id);
|
|
766
|
+
}
|
|
767
|
+
/** The instance for this key, loaded or being loaded, without loading it. */
|
|
768
|
+
get(key) {
|
|
769
|
+
const id = typeof key === "string" ? key : JSON.stringify(key);
|
|
770
|
+
this.expire(id);
|
|
771
|
+
return this.entries.get(id);
|
|
772
|
+
}
|
|
773
|
+
/** Whether an instance is loaded, or being loaded, for this key. */
|
|
774
|
+
has(key) {
|
|
775
|
+
const id = typeof key === "string" ? key : JSON.stringify(key);
|
|
776
|
+
this.expire(id);
|
|
777
|
+
return this.entries.has(id);
|
|
778
|
+
}
|
|
779
|
+
/**
|
|
780
|
+
* Forgets the instance for this key, so the next call loads a new one. For
|
|
781
|
+
* an instance that broke, a crashed worker for instance. Releasing what it
|
|
782
|
+
* holds is left to the caller.
|
|
783
|
+
*/
|
|
784
|
+
delete(key) {
|
|
785
|
+
const id = typeof key === "string" ? key : JSON.stringify(key);
|
|
786
|
+
this.entries.delete(id);
|
|
787
|
+
this.expiries.delete(id);
|
|
788
|
+
}
|
|
789
|
+
/** Drops every instance, so the next call loads again. */
|
|
790
|
+
clear() {
|
|
791
|
+
this.entries.clear();
|
|
792
|
+
this.expiries.clear();
|
|
793
|
+
}
|
|
794
|
+
};
|
|
795
|
+
|
|
796
|
+
// src/lists.ts
|
|
797
|
+
var LIST_TTL = 24 * 60 * 60 * 1e3;
|
|
798
|
+
var lists = new SharedInstances({ ttl: LIST_TTL });
|
|
799
|
+
function cachedList(key, apiKey, load, refresh = false) {
|
|
800
|
+
const account = (0, import_crypto.createHash)("sha256").update(apiKey).digest("hex");
|
|
801
|
+
const id = [...key, account];
|
|
802
|
+
if (refresh) lists.delete(id);
|
|
803
|
+
return lists.load(id, load);
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
// src/models.ts
|
|
807
|
+
function cachedModels(key, apiKey, load, refresh = false) {
|
|
808
|
+
return cachedList(["models", ...key], apiKey, load, refresh);
|
|
809
|
+
}
|
|
810
|
+
|
|
687
811
|
// src/MicdropServer.ts
|
|
688
812
|
var import_eventemitter33 = require("eventemitter3");
|
|
689
813
|
var import_stream2 = require("stream");
|
|
690
|
-
var USER_SAMPLE_RATE = 16e3;
|
|
691
814
|
var DEFAULT_TURN_MAX_WAIT = 4e3;
|
|
692
815
|
var DEFAULT_CLASSIFICATION_MAX_WAIT = 1e3;
|
|
693
816
|
var MicdropServer = class extends import_eventemitter33.EventEmitter {
|
|
@@ -702,6 +825,9 @@ var MicdropServer = class extends import_eventemitter33.EventEmitter {
|
|
|
702
825
|
// Counts the assistant messages sent, so the partial answer of one already
|
|
703
826
|
// settled is not sent after it
|
|
704
827
|
this.assistantMessages = 0;
|
|
828
|
+
// Rate the client plays the assistant voice at, which it takes as the
|
|
829
|
+
// default until told otherwise
|
|
830
|
+
this.assistantSampleRate = DEFAULT_SAMPLE_RATE;
|
|
705
831
|
// Holds the conversation when no agent does, and keeps it readable once the
|
|
706
832
|
// call is over and the config is gone
|
|
707
833
|
this.ownConversation = [];
|
|
@@ -768,9 +894,16 @@ var MicdropServer = class extends import_eventemitter33.EventEmitter {
|
|
|
768
894
|
};
|
|
769
895
|
this.onAssistantAudio = (audio) => {
|
|
770
896
|
if (!this.socket) return;
|
|
897
|
+
const source = this.config?.realtime ?? this.config?.tts;
|
|
898
|
+
const sampleRate = source?.sampleRate ?? DEFAULT_SAMPLE_RATE;
|
|
899
|
+
if (sampleRate !== this.assistantSampleRate) {
|
|
900
|
+
this.log(`Assistant voice now at ${sampleRate} Hz`);
|
|
901
|
+
this.assistantSampleRate = sampleRate;
|
|
902
|
+
this.socket.send(`${"SampleRate" /* SampleRate */} ${sampleRate}`);
|
|
903
|
+
}
|
|
771
904
|
this.log(`Send audio chunk (${audio.byteLength} bytes)`);
|
|
772
905
|
this.socket.send(audio);
|
|
773
|
-
this.emit("AssistantAudio", audio);
|
|
906
|
+
this.emit("AssistantAudio", audio, sampleRate);
|
|
774
907
|
};
|
|
775
908
|
this.onMessageAgent = (message) => {
|
|
776
909
|
this.sendMessage(message);
|
|
@@ -870,7 +1003,7 @@ var MicdropServer = class extends import_eventemitter33.EventEmitter {
|
|
|
870
1003
|
this.log(`Received chunk (${chunk.byteLength} bytes)`);
|
|
871
1004
|
this.currentUserStream?.write(chunk);
|
|
872
1005
|
this.userSpeechChunks++;
|
|
873
|
-
this.config?.turnDetector?.push(pcm16ToFloat32(chunk),
|
|
1006
|
+
this.config?.turnDetector?.push(pcm16ToFloat32(chunk), DEFAULT_SAMPLE_RATE);
|
|
874
1007
|
this.emit("UserAudio", chunk);
|
|
875
1008
|
}
|
|
876
1009
|
onMute() {
|
|
@@ -1333,7 +1466,14 @@ var Realtime = class extends Agent {
|
|
|
1333
1466
|
get isIdle() {
|
|
1334
1467
|
return this.turn === "none" && !this.currentAnswer;
|
|
1335
1468
|
}
|
|
1336
|
-
/**
|
|
1469
|
+
/**
|
|
1470
|
+
* Rate of the audio emitted, in Hz, the one the model speaks at. The server
|
|
1471
|
+
* passes it on to the client, which plays the audio as it comes.
|
|
1472
|
+
*/
|
|
1473
|
+
get sampleRate() {
|
|
1474
|
+
return DEFAULT_SAMPLE_RATE;
|
|
1475
|
+
}
|
|
1476
|
+
/** Voice of the answer, PCM16 at `sampleRate` */
|
|
1337
1477
|
emitAudio(chunk) {
|
|
1338
1478
|
const answer = this.activeAnswer();
|
|
1339
1479
|
if (!answer) return;
|
|
@@ -1460,6 +1600,8 @@ var MicdropRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
1460
1600
|
this.audioMessages = [];
|
|
1461
1601
|
this.currentUserChunks = [];
|
|
1462
1602
|
this.currentAssistantChunks = [];
|
|
1603
|
+
// Rate of the assistant message being recorded, the one of its first chunk
|
|
1604
|
+
this.currentAssistantRate = DEFAULT_SAMPLE_RATE;
|
|
1463
1605
|
this.lastUserMessageIndex = -1;
|
|
1464
1606
|
this.lastAssistantMessageIndex = -1;
|
|
1465
1607
|
this.onUserAudio = (chunk) => {
|
|
@@ -1474,7 +1616,7 @@ var MicdropRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
1474
1616
|
this.log("Recording user audio chunk");
|
|
1475
1617
|
this.currentUserChunks.push(chunk);
|
|
1476
1618
|
};
|
|
1477
|
-
this.onAssistantAudio = (chunk) => {
|
|
1619
|
+
this.onAssistantAudio = (chunk, sampleRate = DEFAULT_SAMPLE_RATE) => {
|
|
1478
1620
|
if (this.currentUserChunks.length > 0) {
|
|
1479
1621
|
if (this.lastUserMessageIndex >= 0) {
|
|
1480
1622
|
this.finalizeUserAudio();
|
|
@@ -1484,7 +1626,7 @@ var MicdropRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
1484
1626
|
}
|
|
1485
1627
|
}
|
|
1486
1628
|
this.log("Recording assistant audio chunk");
|
|
1487
|
-
this.currentAssistantChunks.push(chunk);
|
|
1629
|
+
this.currentAssistantChunks.push(this.toAssistantRate(chunk, sampleRate));
|
|
1488
1630
|
};
|
|
1489
1631
|
this.onMessage = (message) => {
|
|
1490
1632
|
const messageIndex = this.server.conversation.length - 1;
|
|
@@ -1515,6 +1657,21 @@ var MicdropRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
1515
1657
|
this.server.on("End", this.onEnd);
|
|
1516
1658
|
this.server.on("Message", this.onMessage);
|
|
1517
1659
|
}
|
|
1660
|
+
/** A message holds one rate, the one its first chunk came at */
|
|
1661
|
+
toAssistantRate(chunk, sampleRate) {
|
|
1662
|
+
if (this.currentAssistantChunks.length === 0) {
|
|
1663
|
+
this.currentAssistantRate = sampleRate;
|
|
1664
|
+
this.assistantResampler = void 0;
|
|
1665
|
+
}
|
|
1666
|
+
if (sampleRate === this.currentAssistantRate) return chunk;
|
|
1667
|
+
if (this.assistantResampler?.from !== sampleRate) {
|
|
1668
|
+
this.assistantResampler = {
|
|
1669
|
+
from: sampleRate,
|
|
1670
|
+
resampler: new Pcm16Resampler(sampleRate, this.currentAssistantRate)
|
|
1671
|
+
};
|
|
1672
|
+
}
|
|
1673
|
+
return this.assistantResampler.resampler.process(chunk);
|
|
1674
|
+
}
|
|
1518
1675
|
finalizeUserAudio() {
|
|
1519
1676
|
if (this.currentUserChunks.length === 0) return;
|
|
1520
1677
|
if (this.lastUserMessageIndex < 0) return;
|
|
@@ -1523,6 +1680,7 @@ var MicdropRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
1523
1680
|
const buffer = Buffer.concat(this.currentUserChunks);
|
|
1524
1681
|
const audioMessage = {
|
|
1525
1682
|
buffer,
|
|
1683
|
+
sampleRate: DEFAULT_SAMPLE_RATE,
|
|
1526
1684
|
messageIndex: this.lastUserMessageIndex,
|
|
1527
1685
|
message: "content" in message ? message.content : "",
|
|
1528
1686
|
role: "user"
|
|
@@ -1543,6 +1701,7 @@ var MicdropRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
1543
1701
|
const buffer = Buffer.concat(this.currentAssistantChunks);
|
|
1544
1702
|
const audioMessage = {
|
|
1545
1703
|
buffer,
|
|
1704
|
+
sampleRate: this.currentAssistantRate,
|
|
1546
1705
|
messageIndex: this.lastAssistantMessageIndex,
|
|
1547
1706
|
message: "content" in message ? message.content : "",
|
|
1548
1707
|
role: "assistant"
|
|
@@ -1553,6 +1712,7 @@ var MicdropRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
1553
1712
|
this.audioMessages.push(audioMessage);
|
|
1554
1713
|
this.emit("AudioMessage", audioMessage);
|
|
1555
1714
|
this.currentAssistantChunks = [];
|
|
1715
|
+
this.assistantResampler = void 0;
|
|
1556
1716
|
this.lastAssistantMessageIndex = -1;
|
|
1557
1717
|
}
|
|
1558
1718
|
getAudioMessages() {
|
|
@@ -1571,56 +1731,6 @@ var MicdropRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
1571
1731
|
}
|
|
1572
1732
|
};
|
|
1573
1733
|
|
|
1574
|
-
// src/SharedInstances.ts
|
|
1575
|
-
var SharedInstances = class {
|
|
1576
|
-
constructor() {
|
|
1577
|
-
this.entries = /* @__PURE__ */ new Map();
|
|
1578
|
-
}
|
|
1579
|
-
/**
|
|
1580
|
-
* Returns the instance for this key, calling `create` the first time.
|
|
1581
|
-
*
|
|
1582
|
-
* The key can be any JSON-serializable value, usually an array of the
|
|
1583
|
-
* options that define the instance. Concurrent callers get the same promise
|
|
1584
|
-
* instead of loading the model twice.
|
|
1585
|
-
*/
|
|
1586
|
-
load(key, create) {
|
|
1587
|
-
const id = typeof key === "string" ? key : JSON.stringify(key);
|
|
1588
|
-
const existing = this.entries.get(id);
|
|
1589
|
-
if (existing) return existing;
|
|
1590
|
-
let loading;
|
|
1591
|
-
try {
|
|
1592
|
-
loading = create();
|
|
1593
|
-
} catch (error) {
|
|
1594
|
-
return Promise.reject(error);
|
|
1595
|
-
}
|
|
1596
|
-
loading.catch(() => {
|
|
1597
|
-
if (this.entries.get(id) === loading) this.entries.delete(id);
|
|
1598
|
-
});
|
|
1599
|
-
this.entries.set(id, loading);
|
|
1600
|
-
return loading;
|
|
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
|
-
}
|
|
1606
|
-
/** Whether an instance is loaded, or being loaded, for this key. */
|
|
1607
|
-
has(key) {
|
|
1608
|
-
return this.entries.has(typeof key === "string" ? key : JSON.stringify(key));
|
|
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
|
-
}
|
|
1618
|
-
/** Drops every instance, so the next call loads again. */
|
|
1619
|
-
clear() {
|
|
1620
|
-
this.entries.clear();
|
|
1621
|
-
}
|
|
1622
|
-
};
|
|
1623
|
-
|
|
1624
1734
|
// src/socket/ReconnectingSocket.ts
|
|
1625
1735
|
var import_eventemitter35 = require("eventemitter3");
|
|
1626
1736
|
var import_ws = __toESM(require("ws"));
|
|
@@ -1978,6 +2088,16 @@ var import_stream4 = require("stream");
|
|
|
1978
2088
|
// src/tts/TTS.ts
|
|
1979
2089
|
var import_eventemitter37 = require("eventemitter3");
|
|
1980
2090
|
var TTS = class extends import_eventemitter37.EventEmitter {
|
|
2091
|
+
/**
|
|
2092
|
+
* Rate of the audio emitted, in Hz.
|
|
2093
|
+
*
|
|
2094
|
+
* The server reads it with each chunk and tells the client when it changes,
|
|
2095
|
+
* so the audio reaches the client at the rate the provider produced it. A
|
|
2096
|
+
* provider that offers several rates takes the one chosen in its options.
|
|
2097
|
+
*/
|
|
2098
|
+
get sampleRate() {
|
|
2099
|
+
return DEFAULT_SAMPLE_RATE;
|
|
2100
|
+
}
|
|
1981
2101
|
log(...message) {
|
|
1982
2102
|
this.logger?.log(...message);
|
|
1983
2103
|
}
|
|
@@ -2019,6 +2139,10 @@ var FallbackTTS = class extends TTS {
|
|
|
2019
2139
|
}
|
|
2020
2140
|
this.startNextTTS();
|
|
2021
2141
|
}
|
|
2142
|
+
/** Rate of the TTS in use, which changes when it falls back to the next */
|
|
2143
|
+
get sampleRate() {
|
|
2144
|
+
return this.tts?.sampleRate ?? super.sampleRate;
|
|
2145
|
+
}
|
|
2022
2146
|
speak(textStream) {
|
|
2023
2147
|
const utterance = {
|
|
2024
2148
|
ended: false,
|
|
@@ -2062,13 +2186,17 @@ var FallbackTTS = class extends TTS {
|
|
|
2062
2186
|
// src/tts/MockTTS.ts
|
|
2063
2187
|
var fs = __toESM(require("fs"));
|
|
2064
2188
|
var MockTTS = class extends TTS {
|
|
2065
|
-
constructor(audioFilePaths) {
|
|
2189
|
+
constructor(audioFilePaths, rate = DEFAULT_SAMPLE_RATE) {
|
|
2066
2190
|
super();
|
|
2067
2191
|
this.audioFilePaths = audioFilePaths;
|
|
2192
|
+
this.rate = rate;
|
|
2068
2193
|
// Bumped by every answer and every cancel, so the chunks of an answer that
|
|
2069
2194
|
// is no longer wanted stop there
|
|
2070
2195
|
this.generation = 0;
|
|
2071
2196
|
}
|
|
2197
|
+
get sampleRate() {
|
|
2198
|
+
return this.rate;
|
|
2199
|
+
}
|
|
2072
2200
|
speak(textStream) {
|
|
2073
2201
|
const generation = ++this.generation;
|
|
2074
2202
|
textStream.once("data", async () => {
|
|
@@ -2222,6 +2350,29 @@ var SentenceTTS = class extends TTS {
|
|
|
2222
2350
|
}
|
|
2223
2351
|
};
|
|
2224
2352
|
|
|
2353
|
+
// src/tts/voices.ts
|
|
2354
|
+
function voiceGender(value) {
|
|
2355
|
+
const word = value?.toLowerCase();
|
|
2356
|
+
if (!word) return void 0;
|
|
2357
|
+
if (word.startsWith("fem") || word === "woman" || word === "f")
|
|
2358
|
+
return "female";
|
|
2359
|
+
if (word.startsWith("masc") || word === "male" || word === "man" || word === "m") {
|
|
2360
|
+
return "male";
|
|
2361
|
+
}
|
|
2362
|
+
if (word.includes("neutral") || word === "androgynous") return "neutral";
|
|
2363
|
+
return void 0;
|
|
2364
|
+
}
|
|
2365
|
+
function cachedVoices(key, apiKey, load, refresh = false) {
|
|
2366
|
+
return cachedList(["voices", ...key], apiKey, load, refresh);
|
|
2367
|
+
}
|
|
2368
|
+
function voicesForLanguage(voices, language) {
|
|
2369
|
+
if (!language) return voices;
|
|
2370
|
+
const base = language.split("-")[0].toLowerCase();
|
|
2371
|
+
return voices.filter(
|
|
2372
|
+
(voice) => !voice.languages?.length || voice.languages.some((code) => code.split("-")[0].toLowerCase() === base)
|
|
2373
|
+
);
|
|
2374
|
+
}
|
|
2375
|
+
|
|
2225
2376
|
// src/waitForParams.ts
|
|
2226
2377
|
async function waitForParams(socket, validate) {
|
|
2227
2378
|
return new Promise((resolve, reject) => {
|
|
@@ -2251,6 +2402,7 @@ async function waitForParams(socket, validate) {
|
|
|
2251
2402
|
AUTO_SEMANTIC_TURN_TOOL_NAME,
|
|
2252
2403
|
Agent,
|
|
2253
2404
|
Classifier,
|
|
2405
|
+
DEFAULT_SAMPLE_RATE,
|
|
2254
2406
|
FallbackAgent,
|
|
2255
2407
|
FallbackSTT,
|
|
2256
2408
|
FallbackTTS,
|
|
@@ -2264,6 +2416,7 @@ async function waitForParams(socket, validate) {
|
|
|
2264
2416
|
MockAgent,
|
|
2265
2417
|
MockSTT,
|
|
2266
2418
|
MockTTS,
|
|
2419
|
+
Pcm16Aligner,
|
|
2267
2420
|
Pcm16Resampler,
|
|
2268
2421
|
Realtime,
|
|
2269
2422
|
ReconnectingSocket,
|
|
@@ -2272,12 +2425,17 @@ async function waitForParams(socket, validate) {
|
|
|
2272
2425
|
SentenceTTS,
|
|
2273
2426
|
SharedInstances,
|
|
2274
2427
|
TTS,
|
|
2428
|
+
cachedList,
|
|
2429
|
+
cachedModels,
|
|
2430
|
+
cachedVoices,
|
|
2275
2431
|
currentTurn,
|
|
2276
2432
|
float32ToPcm16,
|
|
2277
2433
|
getTurnClassification,
|
|
2278
2434
|
handleError,
|
|
2279
2435
|
pcm16ToFloat32,
|
|
2280
2436
|
turnInput,
|
|
2437
|
+
voiceGender,
|
|
2438
|
+
voicesForLanguage,
|
|
2281
2439
|
waitForParams
|
|
2282
2440
|
});
|
|
2283
2441
|
//# sourceMappingURL=index.js.map
|