@micdrop/client 3.5.0 → 3.7.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 +1 -1
- package/dist/index.d.mts +118 -7
- package/dist/index.d.ts +118 -7
- package/dist/index.js +315 -117
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +312 -117
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -21,8 +21,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
CONFIRM_MARGIN: () => CONFIRM_MARGIN,
|
|
24
|
+
DEFAULT_SAMPLE_RATE: () => DEFAULT_SAMPLE_RATE,
|
|
24
25
|
DEFAULT_TURN_MAX_WAIT: () => DEFAULT_TURN_MAX_WAIT,
|
|
25
26
|
ECHO_TAIL: () => ECHO_TAIL,
|
|
27
|
+
LazySession: () => LazySession,
|
|
26
28
|
Mic: () => Mic,
|
|
27
29
|
MicController: () => MicController,
|
|
28
30
|
MicDriver: () => MicDriver,
|
|
@@ -48,6 +50,7 @@ __export(index_exports, {
|
|
|
48
50
|
VolumeMeter: () => VolumeMeter,
|
|
49
51
|
VolumeVAD: () => VolumeVAD,
|
|
50
52
|
concatFloat32: () => concatFloat32,
|
|
53
|
+
createDownloadCache: () => createDownloadCache,
|
|
51
54
|
currentTurn: () => currentTurn,
|
|
52
55
|
equalVADConfig: () => equalVADConfig,
|
|
53
56
|
floatToPcm16: () => floatToPcm16,
|
|
@@ -403,6 +406,47 @@ var storage = {
|
|
|
403
406
|
}
|
|
404
407
|
};
|
|
405
408
|
|
|
409
|
+
// ../server/src/types.ts
|
|
410
|
+
var MicdropClientCommands = /* @__PURE__ */ ((MicdropClientCommands2) => {
|
|
411
|
+
MicdropClientCommands2["StartSpeaking"] = "StartSpeaking";
|
|
412
|
+
MicdropClientCommands2["StopSpeaking"] = "StopSpeaking";
|
|
413
|
+
MicdropClientCommands2["Mute"] = "Mute";
|
|
414
|
+
return MicdropClientCommands2;
|
|
415
|
+
})(MicdropClientCommands || {});
|
|
416
|
+
var MicdropServerCommands = /* @__PURE__ */ ((MicdropServerCommands2) => {
|
|
417
|
+
MicdropServerCommands2["Message"] = "Message";
|
|
418
|
+
MicdropServerCommands2["PartialAssistantMessage"] = "PartialAssistantMessage";
|
|
419
|
+
MicdropServerCommands2["CancelLastUserMessage"] = "CancelLastUserMessage";
|
|
420
|
+
MicdropServerCommands2["SkipAnswer"] = "SkipAnswer";
|
|
421
|
+
MicdropServerCommands2["EndCall"] = "EndCall";
|
|
422
|
+
MicdropServerCommands2["ToolCall"] = "ToolCall";
|
|
423
|
+
MicdropServerCommands2["Classification"] = "Classification";
|
|
424
|
+
MicdropServerCommands2["SampleRate"] = "SampleRate";
|
|
425
|
+
return MicdropServerCommands2;
|
|
426
|
+
})(MicdropServerCommands || {});
|
|
427
|
+
var DEFAULT_SAMPLE_RATE = 16e3;
|
|
428
|
+
function currentTurn(conversation) {
|
|
429
|
+
let start = conversation.length;
|
|
430
|
+
while (start > 0) {
|
|
431
|
+
const item = conversation[start - 1];
|
|
432
|
+
if (item.role !== "user" || item.metadata?.classification) break;
|
|
433
|
+
start--;
|
|
434
|
+
}
|
|
435
|
+
const messages = conversation.slice(start);
|
|
436
|
+
return {
|
|
437
|
+
transcript: messages.map((message) => message.content).join(" "),
|
|
438
|
+
messages,
|
|
439
|
+
before: conversation.slice(0, start)
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
function getTurnClassification(conversation) {
|
|
443
|
+
for (let index = conversation.length - 1; index >= 0; index--) {
|
|
444
|
+
const item = conversation[index];
|
|
445
|
+
if (item.role === "user") return item.metadata?.classification;
|
|
446
|
+
}
|
|
447
|
+
return void 0;
|
|
448
|
+
}
|
|
449
|
+
|
|
406
450
|
// src/audio/Speaker.ts
|
|
407
451
|
var SpeakerController = class extends import_eventemitter32.EventEmitter {
|
|
408
452
|
constructor() {
|
|
@@ -467,7 +511,7 @@ var SpeakerController = class extends import_eventemitter32.EventEmitter {
|
|
|
467
511
|
* @param pcm - Mono samples as 16 bits integers, or the bytes holding them
|
|
468
512
|
* @param sampleRate - Sample rate of `pcm`, in Hz
|
|
469
513
|
*/
|
|
470
|
-
playAudio(pcm, sampleRate =
|
|
514
|
+
playAudio(pcm, sampleRate = DEFAULT_SAMPLE_RATE) {
|
|
471
515
|
try {
|
|
472
516
|
const samples = pcm instanceof Int16Array ? pcm : new Int16Array(pcm);
|
|
473
517
|
this.getDriver().play(samples, sampleRate);
|
|
@@ -677,6 +721,9 @@ var SileroVAD = class extends VAD {
|
|
|
677
721
|
// Bumped whenever the state is reset, so a window scored before that is
|
|
678
722
|
// dropped rather than reopening a turn
|
|
679
723
|
this.round = 0;
|
|
724
|
+
// Bumped by every start and stop, so a start overtaken while its model
|
|
725
|
+
// loaded lets go of it
|
|
726
|
+
this.startId = 0;
|
|
680
727
|
this.onFrames = (frames, sampleRate) => {
|
|
681
728
|
if (this._isPaused || !this.model) return;
|
|
682
729
|
if (sampleRate !== this.bufferRate) {
|
|
@@ -712,19 +759,33 @@ var SileroVAD = class extends VAD {
|
|
|
712
759
|
"No Silero model. Add `import '@micdrop/web/silero'` in a browser or `import '@micdrop/react-native/silero'` on a phone, or call setSileroModelLoader()."
|
|
713
760
|
);
|
|
714
761
|
}
|
|
715
|
-
this.
|
|
762
|
+
this.mic = mic;
|
|
763
|
+
const startId = ++this.startId;
|
|
764
|
+
let model;
|
|
765
|
+
try {
|
|
766
|
+
model = await loadModel();
|
|
767
|
+
} catch (error) {
|
|
768
|
+
if (startId === this.startId) this.mic = void 0;
|
|
769
|
+
throw error;
|
|
770
|
+
}
|
|
771
|
+
if (startId !== this.startId) {
|
|
772
|
+
await model.release();
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
this.model = model;
|
|
716
776
|
this.resetState();
|
|
717
777
|
this.setStatus(0 /* Silence */);
|
|
718
|
-
this.mic = mic;
|
|
719
778
|
mic.on("Frames", this.onFrames);
|
|
720
779
|
}
|
|
721
780
|
async stop() {
|
|
781
|
+
this.startId++;
|
|
722
782
|
this.mic?.off("Frames", this.onFrames);
|
|
723
783
|
this.mic = void 0;
|
|
724
784
|
this.closeOpenTurn();
|
|
725
785
|
this.resetState();
|
|
726
|
-
|
|
786
|
+
const model = this.model;
|
|
727
787
|
this.model = void 0;
|
|
788
|
+
await model?.release();
|
|
728
789
|
}
|
|
729
790
|
async pause() {
|
|
730
791
|
if (this._isPaused) return;
|
|
@@ -1044,6 +1105,9 @@ var MicRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
1044
1105
|
isStarted: false,
|
|
1045
1106
|
isSpeaking: false
|
|
1046
1107
|
});
|
|
1108
|
+
if (this.turnOpen) {
|
|
1109
|
+
this.endTurn();
|
|
1110
|
+
}
|
|
1047
1111
|
this.cancelPendingClose();
|
|
1048
1112
|
try {
|
|
1049
1113
|
this.mic?.off("Frames", this.onFrames);
|
|
@@ -1534,6 +1598,92 @@ var SpeakerDriver = class extends import_eventemitter36.EventEmitter {
|
|
|
1534
1598
|
}
|
|
1535
1599
|
};
|
|
1536
1600
|
|
|
1601
|
+
// src/audio/vad/lazySession.ts
|
|
1602
|
+
var LazySession = class {
|
|
1603
|
+
/**
|
|
1604
|
+
* @param name - What the session runs, to name it in errors
|
|
1605
|
+
* @param create - Creates the session, called once unless it fails
|
|
1606
|
+
*/
|
|
1607
|
+
constructor(name, create) {
|
|
1608
|
+
this.name = name;
|
|
1609
|
+
this.create = create;
|
|
1610
|
+
this.state = { status: "unloaded" };
|
|
1611
|
+
this.runs = /* @__PURE__ */ new Set();
|
|
1612
|
+
}
|
|
1613
|
+
get status() {
|
|
1614
|
+
return this.state.status;
|
|
1615
|
+
}
|
|
1616
|
+
/** The session, loaded on the first call */
|
|
1617
|
+
get() {
|
|
1618
|
+
const state = this.state;
|
|
1619
|
+
if (state.status === "loaded") return Promise.resolve(state.session);
|
|
1620
|
+
if (state.status === "loading") return state.promise;
|
|
1621
|
+
if (state.status === "released") return Promise.reject(this.releasedError());
|
|
1622
|
+
const promise = this.create().then(
|
|
1623
|
+
async (session) => {
|
|
1624
|
+
if (this.isLoading(promise)) {
|
|
1625
|
+
this.state = { status: "loaded", session };
|
|
1626
|
+
return session;
|
|
1627
|
+
}
|
|
1628
|
+
await session.release();
|
|
1629
|
+
throw this.releasedError();
|
|
1630
|
+
},
|
|
1631
|
+
(error) => {
|
|
1632
|
+
if (this.isLoading(promise)) this.state = { status: "unloaded" };
|
|
1633
|
+
throw error;
|
|
1634
|
+
}
|
|
1635
|
+
);
|
|
1636
|
+
this.state = { status: "loading", promise };
|
|
1637
|
+
return promise;
|
|
1638
|
+
}
|
|
1639
|
+
/**
|
|
1640
|
+
* Runs something on the session, which a release waits for
|
|
1641
|
+
* @param run - Uses the session, and only until its promise settles
|
|
1642
|
+
*/
|
|
1643
|
+
async use(run) {
|
|
1644
|
+
const session = await this.get();
|
|
1645
|
+
if (this.state.status !== "loaded" || this.state.session !== session) {
|
|
1646
|
+
throw this.releasedError();
|
|
1647
|
+
}
|
|
1648
|
+
const pending = run(session);
|
|
1649
|
+
this.runs.add(pending);
|
|
1650
|
+
try {
|
|
1651
|
+
return await pending;
|
|
1652
|
+
} finally {
|
|
1653
|
+
this.runs.delete(pending);
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
/** Releases the session, for good */
|
|
1657
|
+
async release() {
|
|
1658
|
+
const state = this.state;
|
|
1659
|
+
this.state = { status: "released" };
|
|
1660
|
+
if (state.status !== "loaded") return;
|
|
1661
|
+
await Promise.allSettled(this.runs);
|
|
1662
|
+
await state.session.release();
|
|
1663
|
+
}
|
|
1664
|
+
isLoading(promise) {
|
|
1665
|
+
return this.state.status === "loading" && this.state.promise === promise;
|
|
1666
|
+
}
|
|
1667
|
+
releasedError() {
|
|
1668
|
+
return new Error(`The ${this.name} model was released`);
|
|
1669
|
+
}
|
|
1670
|
+
};
|
|
1671
|
+
function createDownloadCache(download) {
|
|
1672
|
+
const downloads = /* @__PURE__ */ new Map();
|
|
1673
|
+
return (url) => {
|
|
1674
|
+
let pending = downloads.get(url);
|
|
1675
|
+
if (!pending) {
|
|
1676
|
+
const started = download(url);
|
|
1677
|
+
downloads.set(url, started);
|
|
1678
|
+
started.catch(() => {
|
|
1679
|
+
if (downloads.get(url) === started) downloads.delete(url);
|
|
1680
|
+
});
|
|
1681
|
+
pending = started;
|
|
1682
|
+
}
|
|
1683
|
+
return pending;
|
|
1684
|
+
};
|
|
1685
|
+
}
|
|
1686
|
+
|
|
1537
1687
|
// src/audio/index.ts
|
|
1538
1688
|
var globalScope = globalThis;
|
|
1539
1689
|
if (!globalScope.micdropMic) {
|
|
@@ -1548,45 +1698,6 @@ var Speaker = globalScope.micdropSpeaker;
|
|
|
1548
1698
|
// src/client/MicdropClient.ts
|
|
1549
1699
|
var import_eventemitter37 = require("eventemitter3");
|
|
1550
1700
|
|
|
1551
|
-
// ../server/src/types.ts
|
|
1552
|
-
var MicdropClientCommands = /* @__PURE__ */ ((MicdropClientCommands2) => {
|
|
1553
|
-
MicdropClientCommands2["StartSpeaking"] = "StartSpeaking";
|
|
1554
|
-
MicdropClientCommands2["StopSpeaking"] = "StopSpeaking";
|
|
1555
|
-
MicdropClientCommands2["Mute"] = "Mute";
|
|
1556
|
-
return MicdropClientCommands2;
|
|
1557
|
-
})(MicdropClientCommands || {});
|
|
1558
|
-
var MicdropServerCommands = /* @__PURE__ */ ((MicdropServerCommands2) => {
|
|
1559
|
-
MicdropServerCommands2["Message"] = "Message";
|
|
1560
|
-
MicdropServerCommands2["PartialAssistantMessage"] = "PartialAssistantMessage";
|
|
1561
|
-
MicdropServerCommands2["CancelLastUserMessage"] = "CancelLastUserMessage";
|
|
1562
|
-
MicdropServerCommands2["SkipAnswer"] = "SkipAnswer";
|
|
1563
|
-
MicdropServerCommands2["EndCall"] = "EndCall";
|
|
1564
|
-
MicdropServerCommands2["ToolCall"] = "ToolCall";
|
|
1565
|
-
MicdropServerCommands2["Classification"] = "Classification";
|
|
1566
|
-
return MicdropServerCommands2;
|
|
1567
|
-
})(MicdropServerCommands || {});
|
|
1568
|
-
function currentTurn(conversation) {
|
|
1569
|
-
let start = conversation.length;
|
|
1570
|
-
while (start > 0) {
|
|
1571
|
-
const item = conversation[start - 1];
|
|
1572
|
-
if (item.role !== "user" || item.metadata?.classification) break;
|
|
1573
|
-
start--;
|
|
1574
|
-
}
|
|
1575
|
-
const messages = conversation.slice(start);
|
|
1576
|
-
return {
|
|
1577
|
-
transcript: messages.map((message) => message.content).join(" "),
|
|
1578
|
-
messages,
|
|
1579
|
-
before: conversation.slice(0, start)
|
|
1580
|
-
};
|
|
1581
|
-
}
|
|
1582
|
-
function getTurnClassification(conversation) {
|
|
1583
|
-
for (let index = conversation.length - 1; index >= 0; index--) {
|
|
1584
|
-
const item = conversation[index];
|
|
1585
|
-
if (item.role === "user") return item.metadata?.classification;
|
|
1586
|
-
}
|
|
1587
|
-
return void 0;
|
|
1588
|
-
}
|
|
1589
|
-
|
|
1590
1701
|
// src/client/MicdropClientError.ts
|
|
1591
1702
|
var MicdropClientErrorCode = /* @__PURE__ */ ((MicdropClientErrorCode2) => {
|
|
1592
1703
|
MicdropClientErrorCode2["MissingUrl"] = "MissingUrl";
|
|
@@ -1693,15 +1804,17 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
1693
1804
|
this.startTime = 0;
|
|
1694
1805
|
this.lastNotifiedState = this.state;
|
|
1695
1806
|
this._isProcessing = false;
|
|
1807
|
+
// Rate of the assistant voice, announced by the server whenever it changes
|
|
1808
|
+
this.assistantSampleRate = DEFAULT_SAMPLE_RATE;
|
|
1696
1809
|
this._isMuted = false;
|
|
1697
1810
|
this._isPaused = false;
|
|
1698
|
-
|
|
1699
|
-
//
|
|
1700
|
-
|
|
1701
|
-
this._isStartingCall = false;
|
|
1811
|
+
// The call as start() and the connection leave it. The public status also
|
|
1812
|
+
// weighs the microphone, which a device change restarts during a call.
|
|
1813
|
+
this.callStatus = "stopped";
|
|
1702
1814
|
this.reconnectAttempt = 0;
|
|
1703
1815
|
this.echoGuard = new EchoGuard();
|
|
1704
1816
|
this.start = async (options) => {
|
|
1817
|
+
this.cancelPendingStop();
|
|
1705
1818
|
this.error = void 0;
|
|
1706
1819
|
this.options = { ...this.options, ...options };
|
|
1707
1820
|
this.startTime = Date.now();
|
|
@@ -1710,34 +1823,36 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
1710
1823
|
this._isProcessing = true;
|
|
1711
1824
|
this._isMuted = false;
|
|
1712
1825
|
this._isPaused = false;
|
|
1713
|
-
this.
|
|
1714
|
-
this._isStartingCall = true;
|
|
1826
|
+
this.callStatus = "starting";
|
|
1715
1827
|
this.reconnectAttempt = 0;
|
|
1716
|
-
|
|
1717
|
-
clearTimeout(this.reconnectTimer);
|
|
1718
|
-
this.reconnectTimer = void 0;
|
|
1719
|
-
}
|
|
1828
|
+
this.clearReconnectTimer();
|
|
1720
1829
|
this.notifyStateChange();
|
|
1721
|
-
|
|
1722
|
-
|
|
1830
|
+
try {
|
|
1831
|
+
if (!this.micRecorder || options?.vad) {
|
|
1832
|
+
await this.startMic({ vad: options?.vad });
|
|
1833
|
+
}
|
|
1834
|
+
if (this.callStatus !== "starting") return;
|
|
1835
|
+
this.openWS();
|
|
1836
|
+
} catch (error) {
|
|
1837
|
+
if (this.callStatus !== "stopped") {
|
|
1838
|
+
this.setError(toClientError(error));
|
|
1839
|
+
await this.stop();
|
|
1840
|
+
}
|
|
1841
|
+
throw error;
|
|
1723
1842
|
}
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
this._isStartingCall = false;
|
|
1843
|
+
if (this.isWSStarted) {
|
|
1844
|
+
this.callStatus = "started";
|
|
1727
1845
|
this.notifyStateChange();
|
|
1728
1846
|
}
|
|
1729
1847
|
};
|
|
1730
1848
|
this.stop = async () => {
|
|
1849
|
+
this.cancelPendingStop();
|
|
1731
1850
|
this.partialAssistantMessage = "";
|
|
1732
|
-
this.
|
|
1851
|
+
this.callStatus = "stopped";
|
|
1733
1852
|
this._isProcessing = false;
|
|
1734
1853
|
this._isMuted = false;
|
|
1735
1854
|
this._isPaused = false;
|
|
1736
|
-
this.
|
|
1737
|
-
if (this.reconnectTimer) {
|
|
1738
|
-
clearTimeout(this.reconnectTimer);
|
|
1739
|
-
this.reconnectTimer = void 0;
|
|
1740
|
-
}
|
|
1855
|
+
this.clearReconnectTimer();
|
|
1741
1856
|
try {
|
|
1742
1857
|
this.stopWS();
|
|
1743
1858
|
} catch (error) {
|
|
@@ -1752,6 +1867,35 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
1752
1867
|
console.error("[MicdropClient] Error stopping microphone", error);
|
|
1753
1868
|
}
|
|
1754
1869
|
};
|
|
1870
|
+
/**
|
|
1871
|
+
* Stops the call once the assistant finished its answer, the one it is
|
|
1872
|
+
* still preparing included, such as the goodbye that follows an `EndCall`.
|
|
1873
|
+
* Stops right away when no answer is under way.
|
|
1874
|
+
*
|
|
1875
|
+
* A `start()` or a `stop()` meanwhile cancels the wait, so it never acts on
|
|
1876
|
+
* the next call.
|
|
1877
|
+
* @returns Resolves once the call is stopped, or the wait cancelled
|
|
1878
|
+
*/
|
|
1879
|
+
this.stopAfterAnswer = async () => {
|
|
1880
|
+
if (!this.isAnswering) return this.stop();
|
|
1881
|
+
this.cancelPendingStop();
|
|
1882
|
+
return new Promise((resolve) => {
|
|
1883
|
+
const detach = () => {
|
|
1884
|
+
this.off("StateChange", onStateChange);
|
|
1885
|
+
this.pendingStop = void 0;
|
|
1886
|
+
};
|
|
1887
|
+
const onStateChange = () => {
|
|
1888
|
+
if (this.isAnswering) return;
|
|
1889
|
+
detach();
|
|
1890
|
+
this.stop().then(resolve);
|
|
1891
|
+
};
|
|
1892
|
+
this.on("StateChange", onStateChange);
|
|
1893
|
+
this.pendingStop = () => {
|
|
1894
|
+
detach();
|
|
1895
|
+
resolve();
|
|
1896
|
+
};
|
|
1897
|
+
});
|
|
1898
|
+
};
|
|
1755
1899
|
this.mute = () => {
|
|
1756
1900
|
this.vad?.pause();
|
|
1757
1901
|
this._isMuted = true;
|
|
@@ -1828,7 +1972,9 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
1828
1972
|
});
|
|
1829
1973
|
}
|
|
1830
1974
|
const deviceId = options.deviceId ?? storage.getItem("micdrop.micDevice" /* MicDevice */) ?? void 0;
|
|
1975
|
+
const recorder = this.micRecorder;
|
|
1831
1976
|
await Mic.start(deviceId);
|
|
1977
|
+
if (this.micRecorder !== recorder || !Mic.isStarted) return;
|
|
1832
1978
|
if (Mic.deviceId) {
|
|
1833
1979
|
storage.setItem("micdrop.micDevice" /* MicDevice */, Mic.deviceId);
|
|
1834
1980
|
} else {
|
|
@@ -1896,8 +2042,7 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
1896
2042
|
clearTimeout(this.connectionTimer);
|
|
1897
2043
|
this.connectionTimer = void 0;
|
|
1898
2044
|
}
|
|
1899
|
-
this.
|
|
1900
|
-
this._isStartingCall = false;
|
|
2045
|
+
this.callStatus = "started";
|
|
1901
2046
|
this.reconnectAttempt = 0;
|
|
1902
2047
|
this.error = void 0;
|
|
1903
2048
|
this.notifyStateChange();
|
|
@@ -1913,7 +2058,7 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
1913
2058
|
if (data instanceof ArrayBuffer) {
|
|
1914
2059
|
this.log("Received audio", data.byteLength);
|
|
1915
2060
|
if (this.isPaused || this.isUserSpeaking) return;
|
|
1916
|
-
Speaker.playAudio(new Int16Array(data));
|
|
2061
|
+
Speaker.playAudio(new Int16Array(data), this.assistantSampleRate);
|
|
1917
2062
|
this._isProcessing = false;
|
|
1918
2063
|
this.notifyStateChange();
|
|
1919
2064
|
return;
|
|
@@ -1923,6 +2068,17 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
1923
2068
|
return;
|
|
1924
2069
|
}
|
|
1925
2070
|
this.log("Received message:", data);
|
|
2071
|
+
if (data.startsWith("SampleRate" /* SampleRate */)) {
|
|
2072
|
+
const sampleRate = Number(
|
|
2073
|
+
data.substring("SampleRate" /* SampleRate */.length + 1)
|
|
2074
|
+
);
|
|
2075
|
+
if (Number.isFinite(sampleRate) && sampleRate > 0) {
|
|
2076
|
+
this.assistantSampleRate = sampleRate;
|
|
2077
|
+
} else {
|
|
2078
|
+
console.error("[MicdropClient] Invalid sample rate:", data);
|
|
2079
|
+
}
|
|
2080
|
+
return;
|
|
2081
|
+
}
|
|
1926
2082
|
if (data.startsWith("Message" /* Message */)) {
|
|
1927
2083
|
try {
|
|
1928
2084
|
const message = JSON.parse(
|
|
@@ -1982,7 +2138,7 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
1982
2138
|
const config = { ...DEFAULT_RECONNECT_OPTIONS, ...this.options.reconnect };
|
|
1983
2139
|
const canReconnect = error && isRecoverableError(error) && config.maxAttempts > 0;
|
|
1984
2140
|
if (canReconnect) {
|
|
1985
|
-
this.
|
|
2141
|
+
this.callStatus = "reconnecting";
|
|
1986
2142
|
this.stopWS();
|
|
1987
2143
|
this.scheduleReconnect(config);
|
|
1988
2144
|
} else {
|
|
@@ -2047,14 +2203,30 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
2047
2203
|
get vad() {
|
|
2048
2204
|
return this.micRecorder?.vad;
|
|
2049
2205
|
}
|
|
2206
|
+
get status() {
|
|
2207
|
+
const mic = this.micRecorder?.state;
|
|
2208
|
+
if (this.callStatus === "reconnecting") return "reconnecting";
|
|
2209
|
+
if (this.callStatus === "started" && mic?.isStarted) return "started";
|
|
2210
|
+
if (this.callStatus !== "stopped" || mic?.isStarting) return "starting";
|
|
2211
|
+
return "stopped";
|
|
2212
|
+
}
|
|
2213
|
+
get activity() {
|
|
2214
|
+
if (this.isUserSpeaking) return "userSpeaking";
|
|
2215
|
+
if (this.isAssistantSpeaking) return "assistantSpeaking";
|
|
2216
|
+
if (this.isProcessing) return "processing";
|
|
2217
|
+
if (this.isListening) return "listening";
|
|
2218
|
+
return "idle";
|
|
2219
|
+
}
|
|
2220
|
+
/** The call is on, reconnecting included */
|
|
2050
2221
|
get isStarted() {
|
|
2051
|
-
|
|
2222
|
+
const status = this.status;
|
|
2223
|
+
return status === "started" || status === "reconnecting";
|
|
2052
2224
|
}
|
|
2053
2225
|
get isStarting() {
|
|
2054
|
-
return
|
|
2226
|
+
return this.status === "starting";
|
|
2055
2227
|
}
|
|
2056
2228
|
get isReconnecting() {
|
|
2057
|
-
return this.
|
|
2229
|
+
return this.status === "reconnecting";
|
|
2058
2230
|
}
|
|
2059
2231
|
get isMuted() {
|
|
2060
2232
|
return this._isMuted;
|
|
@@ -2107,6 +2279,8 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
2107
2279
|
}
|
|
2108
2280
|
get state() {
|
|
2109
2281
|
return {
|
|
2282
|
+
status: this.status,
|
|
2283
|
+
activity: this.activity,
|
|
2110
2284
|
isStarting: this.isStarting,
|
|
2111
2285
|
isStarted: this.isStarted,
|
|
2112
2286
|
isReconnecting: this.isReconnecting,
|
|
@@ -2127,62 +2301,70 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
2127
2301
|
speakerDevices: this.speakerDevices
|
|
2128
2302
|
};
|
|
2129
2303
|
}
|
|
2304
|
+
/** The assistant prepares or speaks an answer */
|
|
2305
|
+
get isAnswering() {
|
|
2306
|
+
const { activity } = this;
|
|
2307
|
+
return activity === "processing" || activity === "assistantSpeaking";
|
|
2308
|
+
}
|
|
2309
|
+
cancelPendingStop() {
|
|
2310
|
+
this.pendingStop?.();
|
|
2311
|
+
}
|
|
2130
2312
|
async stopMic() {
|
|
2131
2313
|
this.micRecorder?.stop();
|
|
2132
2314
|
this.micRecorder = void 0;
|
|
2133
2315
|
await Mic.stop();
|
|
2134
2316
|
this.notifyStateChange();
|
|
2135
2317
|
}
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
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;
|
|
2318
|
+
/**
|
|
2319
|
+
* Opens the connection, unless one is already open or opening.
|
|
2320
|
+
*
|
|
2321
|
+
* The socket answers through handlers bound to it, so one left behind by a
|
|
2322
|
+
* stop or a reconnection never acts on the call that followed.
|
|
2323
|
+
*/
|
|
2324
|
+
openWS() {
|
|
2325
|
+
if (this.ws) {
|
|
2326
|
+
this.log("WebSocket is already started");
|
|
2327
|
+
return;
|
|
2171
2328
|
}
|
|
2329
|
+
if (!this.isMicStarted) {
|
|
2330
|
+
throw new MicdropClientError("Mic" /* Mic */);
|
|
2331
|
+
}
|
|
2332
|
+
if (!this.options.url) {
|
|
2333
|
+
throw new MicdropClientError("MissingUrl" /* MissingUrl */);
|
|
2334
|
+
}
|
|
2335
|
+
const ws = new WebSocket(this.options.url);
|
|
2336
|
+
ws.binaryType = "arraybuffer";
|
|
2337
|
+
this.ws = ws;
|
|
2338
|
+
this.assistantSampleRate = DEFAULT_SAMPLE_RATE;
|
|
2339
|
+
this.notifyStateChange();
|
|
2340
|
+
const isCurrent = () => this.ws === ws;
|
|
2341
|
+
ws.onopen = () => isCurrent() && this.onWSOpen();
|
|
2342
|
+
ws.onmessage = (event) => isCurrent() && this.onWSMessage(event);
|
|
2343
|
+
ws.onclose = (event) => isCurrent() && this.onWSClose(event);
|
|
2344
|
+
ws.onerror = () => isCurrent() && this.onWSError();
|
|
2345
|
+
this.connectionTimer = setTimeout(() => {
|
|
2346
|
+
this.connectionTimer = void 0;
|
|
2347
|
+
if (isCurrent() && ws.readyState === 0) {
|
|
2348
|
+
this.log("WebSocket connection timeout");
|
|
2349
|
+
ws.close();
|
|
2350
|
+
}
|
|
2351
|
+
}, this.options.reconnect?.connectionTimeout ?? DEFAULT_RECONNECT_OPTIONS.connectionTimeout);
|
|
2172
2352
|
}
|
|
2173
2353
|
stopWS() {
|
|
2174
2354
|
if (this.connectionTimer) {
|
|
2175
2355
|
clearTimeout(this.connectionTimer);
|
|
2176
2356
|
this.connectionTimer = void 0;
|
|
2177
2357
|
}
|
|
2178
|
-
|
|
2179
|
-
if (
|
|
2180
|
-
this.ws.close();
|
|
2181
|
-
}
|
|
2358
|
+
const ws = this.ws;
|
|
2359
|
+
if (!ws) return;
|
|
2182
2360
|
this.ws = void 0;
|
|
2361
|
+
if (ws.readyState === 1 || ws.readyState === 0) {
|
|
2362
|
+
ws.close();
|
|
2363
|
+
}
|
|
2183
2364
|
this.notifyStateChange();
|
|
2184
2365
|
}
|
|
2185
2366
|
scheduleReconnect(config) {
|
|
2367
|
+
if (this.callStatus !== "reconnecting") return;
|
|
2186
2368
|
if (this.reconnectAttempt >= config.maxAttempts) {
|
|
2187
2369
|
this.stop();
|
|
2188
2370
|
this.setError(new MicdropClientError("Connection" /* Connection */));
|
|
@@ -2195,17 +2377,23 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
2195
2377
|
if (!this.isPaused && !this.isMuted) {
|
|
2196
2378
|
this.vad?.pause?.();
|
|
2197
2379
|
}
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2380
|
+
this.clearReconnectTimer();
|
|
2381
|
+
this.reconnectTimer = setTimeout(() => {
|
|
2382
|
+
this.reconnectTimer = void 0;
|
|
2383
|
+
if (this.callStatus !== "reconnecting") return;
|
|
2202
2384
|
try {
|
|
2203
|
-
|
|
2204
|
-
} catch {
|
|
2205
|
-
this.
|
|
2385
|
+
this.openWS();
|
|
2386
|
+
} catch (error) {
|
|
2387
|
+
this.setError(toClientError(error));
|
|
2388
|
+
this.stop();
|
|
2206
2389
|
}
|
|
2207
2390
|
}, config.delayMs);
|
|
2208
2391
|
}
|
|
2392
|
+
clearReconnectTimer() {
|
|
2393
|
+
if (!this.reconnectTimer) return;
|
|
2394
|
+
clearTimeout(this.reconnectTimer);
|
|
2395
|
+
this.reconnectTimer = void 0;
|
|
2396
|
+
}
|
|
2209
2397
|
// The same as when the speaker starts on a call without interruptions
|
|
2210
2398
|
onInterruptionsGivenUp() {
|
|
2211
2399
|
this.log("Interruptions disabled until the end of the call");
|
|
@@ -2250,6 +2438,13 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
2250
2438
|
console.log(`[MicdropClient ${Date.now() - this.startTime}]`, ...message);
|
|
2251
2439
|
}
|
|
2252
2440
|
};
|
|
2441
|
+
function toClientError(error) {
|
|
2442
|
+
if (error instanceof MicdropClientError) return error;
|
|
2443
|
+
return new MicdropClientError(
|
|
2444
|
+
"Connection" /* Connection */,
|
|
2445
|
+
error?.message
|
|
2446
|
+
);
|
|
2447
|
+
}
|
|
2253
2448
|
|
|
2254
2449
|
// src/Micdrop.ts
|
|
2255
2450
|
var globalScope2 = globalThis;
|
|
@@ -2260,8 +2455,10 @@ var Micdrop = globalScope2.micdropClient;
|
|
|
2260
2455
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2261
2456
|
0 && (module.exports = {
|
|
2262
2457
|
CONFIRM_MARGIN,
|
|
2458
|
+
DEFAULT_SAMPLE_RATE,
|
|
2263
2459
|
DEFAULT_TURN_MAX_WAIT,
|
|
2264
2460
|
ECHO_TAIL,
|
|
2461
|
+
LazySession,
|
|
2265
2462
|
Mic,
|
|
2266
2463
|
MicController,
|
|
2267
2464
|
MicDriver,
|
|
@@ -2287,6 +2484,7 @@ var Micdrop = globalScope2.micdropClient;
|
|
|
2287
2484
|
VolumeMeter,
|
|
2288
2485
|
VolumeVAD,
|
|
2289
2486
|
concatFloat32,
|
|
2487
|
+
createDownloadCache,
|
|
2290
2488
|
currentTurn,
|
|
2291
2489
|
equalVADConfig,
|
|
2292
2490
|
floatToPcm16,
|