@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.mjs
CHANGED
|
@@ -335,6 +335,47 @@ var storage = {
|
|
|
335
335
|
}
|
|
336
336
|
};
|
|
337
337
|
|
|
338
|
+
// ../server/src/types.ts
|
|
339
|
+
var MicdropClientCommands = /* @__PURE__ */ ((MicdropClientCommands2) => {
|
|
340
|
+
MicdropClientCommands2["StartSpeaking"] = "StartSpeaking";
|
|
341
|
+
MicdropClientCommands2["StopSpeaking"] = "StopSpeaking";
|
|
342
|
+
MicdropClientCommands2["Mute"] = "Mute";
|
|
343
|
+
return MicdropClientCommands2;
|
|
344
|
+
})(MicdropClientCommands || {});
|
|
345
|
+
var MicdropServerCommands = /* @__PURE__ */ ((MicdropServerCommands2) => {
|
|
346
|
+
MicdropServerCommands2["Message"] = "Message";
|
|
347
|
+
MicdropServerCommands2["PartialAssistantMessage"] = "PartialAssistantMessage";
|
|
348
|
+
MicdropServerCommands2["CancelLastUserMessage"] = "CancelLastUserMessage";
|
|
349
|
+
MicdropServerCommands2["SkipAnswer"] = "SkipAnswer";
|
|
350
|
+
MicdropServerCommands2["EndCall"] = "EndCall";
|
|
351
|
+
MicdropServerCommands2["ToolCall"] = "ToolCall";
|
|
352
|
+
MicdropServerCommands2["Classification"] = "Classification";
|
|
353
|
+
MicdropServerCommands2["SampleRate"] = "SampleRate";
|
|
354
|
+
return MicdropServerCommands2;
|
|
355
|
+
})(MicdropServerCommands || {});
|
|
356
|
+
var DEFAULT_SAMPLE_RATE = 16e3;
|
|
357
|
+
function currentTurn(conversation) {
|
|
358
|
+
let start = conversation.length;
|
|
359
|
+
while (start > 0) {
|
|
360
|
+
const item = conversation[start - 1];
|
|
361
|
+
if (item.role !== "user" || item.metadata?.classification) break;
|
|
362
|
+
start--;
|
|
363
|
+
}
|
|
364
|
+
const messages = conversation.slice(start);
|
|
365
|
+
return {
|
|
366
|
+
transcript: messages.map((message) => message.content).join(" "),
|
|
367
|
+
messages,
|
|
368
|
+
before: conversation.slice(0, start)
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
function getTurnClassification(conversation) {
|
|
372
|
+
for (let index = conversation.length - 1; index >= 0; index--) {
|
|
373
|
+
const item = conversation[index];
|
|
374
|
+
if (item.role === "user") return item.metadata?.classification;
|
|
375
|
+
}
|
|
376
|
+
return void 0;
|
|
377
|
+
}
|
|
378
|
+
|
|
338
379
|
// src/audio/Speaker.ts
|
|
339
380
|
var SpeakerController = class extends EventEmitter2 {
|
|
340
381
|
constructor() {
|
|
@@ -399,7 +440,7 @@ var SpeakerController = class extends EventEmitter2 {
|
|
|
399
440
|
* @param pcm - Mono samples as 16 bits integers, or the bytes holding them
|
|
400
441
|
* @param sampleRate - Sample rate of `pcm`, in Hz
|
|
401
442
|
*/
|
|
402
|
-
playAudio(pcm, sampleRate =
|
|
443
|
+
playAudio(pcm, sampleRate = DEFAULT_SAMPLE_RATE) {
|
|
403
444
|
try {
|
|
404
445
|
const samples = pcm instanceof Int16Array ? pcm : new Int16Array(pcm);
|
|
405
446
|
this.getDriver().play(samples, sampleRate);
|
|
@@ -609,6 +650,9 @@ var SileroVAD = class extends VAD {
|
|
|
609
650
|
// Bumped whenever the state is reset, so a window scored before that is
|
|
610
651
|
// dropped rather than reopening a turn
|
|
611
652
|
this.round = 0;
|
|
653
|
+
// Bumped by every start and stop, so a start overtaken while its model
|
|
654
|
+
// loaded lets go of it
|
|
655
|
+
this.startId = 0;
|
|
612
656
|
this.onFrames = (frames, sampleRate) => {
|
|
613
657
|
if (this._isPaused || !this.model) return;
|
|
614
658
|
if (sampleRate !== this.bufferRate) {
|
|
@@ -644,19 +688,33 @@ var SileroVAD = class extends VAD {
|
|
|
644
688
|
"No Silero model. Add `import '@micdrop/web/silero'` in a browser or `import '@micdrop/react-native/silero'` on a phone, or call setSileroModelLoader()."
|
|
645
689
|
);
|
|
646
690
|
}
|
|
647
|
-
this.
|
|
691
|
+
this.mic = mic;
|
|
692
|
+
const startId = ++this.startId;
|
|
693
|
+
let model;
|
|
694
|
+
try {
|
|
695
|
+
model = await loadModel();
|
|
696
|
+
} catch (error) {
|
|
697
|
+
if (startId === this.startId) this.mic = void 0;
|
|
698
|
+
throw error;
|
|
699
|
+
}
|
|
700
|
+
if (startId !== this.startId) {
|
|
701
|
+
await model.release();
|
|
702
|
+
return;
|
|
703
|
+
}
|
|
704
|
+
this.model = model;
|
|
648
705
|
this.resetState();
|
|
649
706
|
this.setStatus(0 /* Silence */);
|
|
650
|
-
this.mic = mic;
|
|
651
707
|
mic.on("Frames", this.onFrames);
|
|
652
708
|
}
|
|
653
709
|
async stop() {
|
|
710
|
+
this.startId++;
|
|
654
711
|
this.mic?.off("Frames", this.onFrames);
|
|
655
712
|
this.mic = void 0;
|
|
656
713
|
this.closeOpenTurn();
|
|
657
714
|
this.resetState();
|
|
658
|
-
|
|
715
|
+
const model = this.model;
|
|
659
716
|
this.model = void 0;
|
|
717
|
+
await model?.release();
|
|
660
718
|
}
|
|
661
719
|
async pause() {
|
|
662
720
|
if (this._isPaused) return;
|
|
@@ -976,6 +1034,9 @@ var MicRecorder = class extends EventEmitter4 {
|
|
|
976
1034
|
isStarted: false,
|
|
977
1035
|
isSpeaking: false
|
|
978
1036
|
});
|
|
1037
|
+
if (this.turnOpen) {
|
|
1038
|
+
this.endTurn();
|
|
1039
|
+
}
|
|
979
1040
|
this.cancelPendingClose();
|
|
980
1041
|
try {
|
|
981
1042
|
this.mic?.off("Frames", this.onFrames);
|
|
@@ -1466,6 +1527,92 @@ var SpeakerDriver = class extends EventEmitter6 {
|
|
|
1466
1527
|
}
|
|
1467
1528
|
};
|
|
1468
1529
|
|
|
1530
|
+
// src/audio/vad/lazySession.ts
|
|
1531
|
+
var LazySession = class {
|
|
1532
|
+
/**
|
|
1533
|
+
* @param name - What the session runs, to name it in errors
|
|
1534
|
+
* @param create - Creates the session, called once unless it fails
|
|
1535
|
+
*/
|
|
1536
|
+
constructor(name, create) {
|
|
1537
|
+
this.name = name;
|
|
1538
|
+
this.create = create;
|
|
1539
|
+
this.state = { status: "unloaded" };
|
|
1540
|
+
this.runs = /* @__PURE__ */ new Set();
|
|
1541
|
+
}
|
|
1542
|
+
get status() {
|
|
1543
|
+
return this.state.status;
|
|
1544
|
+
}
|
|
1545
|
+
/** The session, loaded on the first call */
|
|
1546
|
+
get() {
|
|
1547
|
+
const state = this.state;
|
|
1548
|
+
if (state.status === "loaded") return Promise.resolve(state.session);
|
|
1549
|
+
if (state.status === "loading") return state.promise;
|
|
1550
|
+
if (state.status === "released") return Promise.reject(this.releasedError());
|
|
1551
|
+
const promise = this.create().then(
|
|
1552
|
+
async (session) => {
|
|
1553
|
+
if (this.isLoading(promise)) {
|
|
1554
|
+
this.state = { status: "loaded", session };
|
|
1555
|
+
return session;
|
|
1556
|
+
}
|
|
1557
|
+
await session.release();
|
|
1558
|
+
throw this.releasedError();
|
|
1559
|
+
},
|
|
1560
|
+
(error) => {
|
|
1561
|
+
if (this.isLoading(promise)) this.state = { status: "unloaded" };
|
|
1562
|
+
throw error;
|
|
1563
|
+
}
|
|
1564
|
+
);
|
|
1565
|
+
this.state = { status: "loading", promise };
|
|
1566
|
+
return promise;
|
|
1567
|
+
}
|
|
1568
|
+
/**
|
|
1569
|
+
* Runs something on the session, which a release waits for
|
|
1570
|
+
* @param run - Uses the session, and only until its promise settles
|
|
1571
|
+
*/
|
|
1572
|
+
async use(run) {
|
|
1573
|
+
const session = await this.get();
|
|
1574
|
+
if (this.state.status !== "loaded" || this.state.session !== session) {
|
|
1575
|
+
throw this.releasedError();
|
|
1576
|
+
}
|
|
1577
|
+
const pending = run(session);
|
|
1578
|
+
this.runs.add(pending);
|
|
1579
|
+
try {
|
|
1580
|
+
return await pending;
|
|
1581
|
+
} finally {
|
|
1582
|
+
this.runs.delete(pending);
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
/** Releases the session, for good */
|
|
1586
|
+
async release() {
|
|
1587
|
+
const state = this.state;
|
|
1588
|
+
this.state = { status: "released" };
|
|
1589
|
+
if (state.status !== "loaded") return;
|
|
1590
|
+
await Promise.allSettled(this.runs);
|
|
1591
|
+
await state.session.release();
|
|
1592
|
+
}
|
|
1593
|
+
isLoading(promise) {
|
|
1594
|
+
return this.state.status === "loading" && this.state.promise === promise;
|
|
1595
|
+
}
|
|
1596
|
+
releasedError() {
|
|
1597
|
+
return new Error(`The ${this.name} model was released`);
|
|
1598
|
+
}
|
|
1599
|
+
};
|
|
1600
|
+
function createDownloadCache(download) {
|
|
1601
|
+
const downloads = /* @__PURE__ */ new Map();
|
|
1602
|
+
return (url) => {
|
|
1603
|
+
let pending = downloads.get(url);
|
|
1604
|
+
if (!pending) {
|
|
1605
|
+
const started = download(url);
|
|
1606
|
+
downloads.set(url, started);
|
|
1607
|
+
started.catch(() => {
|
|
1608
|
+
if (downloads.get(url) === started) downloads.delete(url);
|
|
1609
|
+
});
|
|
1610
|
+
pending = started;
|
|
1611
|
+
}
|
|
1612
|
+
return pending;
|
|
1613
|
+
};
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1469
1616
|
// src/audio/index.ts
|
|
1470
1617
|
var globalScope = globalThis;
|
|
1471
1618
|
if (!globalScope.micdropMic) {
|
|
@@ -1480,45 +1627,6 @@ var Speaker = globalScope.micdropSpeaker;
|
|
|
1480
1627
|
// src/client/MicdropClient.ts
|
|
1481
1628
|
import { EventEmitter as EventEmitter7 } from "eventemitter3";
|
|
1482
1629
|
|
|
1483
|
-
// ../server/src/types.ts
|
|
1484
|
-
var MicdropClientCommands = /* @__PURE__ */ ((MicdropClientCommands2) => {
|
|
1485
|
-
MicdropClientCommands2["StartSpeaking"] = "StartSpeaking";
|
|
1486
|
-
MicdropClientCommands2["StopSpeaking"] = "StopSpeaking";
|
|
1487
|
-
MicdropClientCommands2["Mute"] = "Mute";
|
|
1488
|
-
return MicdropClientCommands2;
|
|
1489
|
-
})(MicdropClientCommands || {});
|
|
1490
|
-
var MicdropServerCommands = /* @__PURE__ */ ((MicdropServerCommands2) => {
|
|
1491
|
-
MicdropServerCommands2["Message"] = "Message";
|
|
1492
|
-
MicdropServerCommands2["PartialAssistantMessage"] = "PartialAssistantMessage";
|
|
1493
|
-
MicdropServerCommands2["CancelLastUserMessage"] = "CancelLastUserMessage";
|
|
1494
|
-
MicdropServerCommands2["SkipAnswer"] = "SkipAnswer";
|
|
1495
|
-
MicdropServerCommands2["EndCall"] = "EndCall";
|
|
1496
|
-
MicdropServerCommands2["ToolCall"] = "ToolCall";
|
|
1497
|
-
MicdropServerCommands2["Classification"] = "Classification";
|
|
1498
|
-
return MicdropServerCommands2;
|
|
1499
|
-
})(MicdropServerCommands || {});
|
|
1500
|
-
function currentTurn(conversation) {
|
|
1501
|
-
let start = conversation.length;
|
|
1502
|
-
while (start > 0) {
|
|
1503
|
-
const item = conversation[start - 1];
|
|
1504
|
-
if (item.role !== "user" || item.metadata?.classification) break;
|
|
1505
|
-
start--;
|
|
1506
|
-
}
|
|
1507
|
-
const messages = conversation.slice(start);
|
|
1508
|
-
return {
|
|
1509
|
-
transcript: messages.map((message) => message.content).join(" "),
|
|
1510
|
-
messages,
|
|
1511
|
-
before: conversation.slice(0, start)
|
|
1512
|
-
};
|
|
1513
|
-
}
|
|
1514
|
-
function getTurnClassification(conversation) {
|
|
1515
|
-
for (let index = conversation.length - 1; index >= 0; index--) {
|
|
1516
|
-
const item = conversation[index];
|
|
1517
|
-
if (item.role === "user") return item.metadata?.classification;
|
|
1518
|
-
}
|
|
1519
|
-
return void 0;
|
|
1520
|
-
}
|
|
1521
|
-
|
|
1522
1630
|
// src/client/MicdropClientError.ts
|
|
1523
1631
|
var MicdropClientErrorCode = /* @__PURE__ */ ((MicdropClientErrorCode2) => {
|
|
1524
1632
|
MicdropClientErrorCode2["MissingUrl"] = "MissingUrl";
|
|
@@ -1625,15 +1733,17 @@ var MicdropClient = class extends EventEmitter7 {
|
|
|
1625
1733
|
this.startTime = 0;
|
|
1626
1734
|
this.lastNotifiedState = this.state;
|
|
1627
1735
|
this._isProcessing = false;
|
|
1736
|
+
// Rate of the assistant voice, announced by the server whenever it changes
|
|
1737
|
+
this.assistantSampleRate = DEFAULT_SAMPLE_RATE;
|
|
1628
1738
|
this._isMuted = false;
|
|
1629
1739
|
this._isPaused = false;
|
|
1630
|
-
|
|
1631
|
-
//
|
|
1632
|
-
|
|
1633
|
-
this._isStartingCall = false;
|
|
1740
|
+
// The call as start() and the connection leave it. The public status also
|
|
1741
|
+
// weighs the microphone, which a device change restarts during a call.
|
|
1742
|
+
this.callStatus = "stopped";
|
|
1634
1743
|
this.reconnectAttempt = 0;
|
|
1635
1744
|
this.echoGuard = new EchoGuard();
|
|
1636
1745
|
this.start = async (options) => {
|
|
1746
|
+
this.cancelPendingStop();
|
|
1637
1747
|
this.error = void 0;
|
|
1638
1748
|
this.options = { ...this.options, ...options };
|
|
1639
1749
|
this.startTime = Date.now();
|
|
@@ -1642,34 +1752,36 @@ var MicdropClient = class extends EventEmitter7 {
|
|
|
1642
1752
|
this._isProcessing = true;
|
|
1643
1753
|
this._isMuted = false;
|
|
1644
1754
|
this._isPaused = false;
|
|
1645
|
-
this.
|
|
1646
|
-
this._isStartingCall = true;
|
|
1755
|
+
this.callStatus = "starting";
|
|
1647
1756
|
this.reconnectAttempt = 0;
|
|
1648
|
-
|
|
1649
|
-
clearTimeout(this.reconnectTimer);
|
|
1650
|
-
this.reconnectTimer = void 0;
|
|
1651
|
-
}
|
|
1757
|
+
this.clearReconnectTimer();
|
|
1652
1758
|
this.notifyStateChange();
|
|
1653
|
-
|
|
1654
|
-
|
|
1759
|
+
try {
|
|
1760
|
+
if (!this.micRecorder || options?.vad) {
|
|
1761
|
+
await this.startMic({ vad: options?.vad });
|
|
1762
|
+
}
|
|
1763
|
+
if (this.callStatus !== "starting") return;
|
|
1764
|
+
this.openWS();
|
|
1765
|
+
} catch (error) {
|
|
1766
|
+
if (this.callStatus !== "stopped") {
|
|
1767
|
+
this.setError(toClientError(error));
|
|
1768
|
+
await this.stop();
|
|
1769
|
+
}
|
|
1770
|
+
throw error;
|
|
1655
1771
|
}
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
this._isStartingCall = false;
|
|
1772
|
+
if (this.isWSStarted) {
|
|
1773
|
+
this.callStatus = "started";
|
|
1659
1774
|
this.notifyStateChange();
|
|
1660
1775
|
}
|
|
1661
1776
|
};
|
|
1662
1777
|
this.stop = async () => {
|
|
1778
|
+
this.cancelPendingStop();
|
|
1663
1779
|
this.partialAssistantMessage = "";
|
|
1664
|
-
this.
|
|
1780
|
+
this.callStatus = "stopped";
|
|
1665
1781
|
this._isProcessing = false;
|
|
1666
1782
|
this._isMuted = false;
|
|
1667
1783
|
this._isPaused = false;
|
|
1668
|
-
this.
|
|
1669
|
-
if (this.reconnectTimer) {
|
|
1670
|
-
clearTimeout(this.reconnectTimer);
|
|
1671
|
-
this.reconnectTimer = void 0;
|
|
1672
|
-
}
|
|
1784
|
+
this.clearReconnectTimer();
|
|
1673
1785
|
try {
|
|
1674
1786
|
this.stopWS();
|
|
1675
1787
|
} catch (error) {
|
|
@@ -1684,6 +1796,35 @@ var MicdropClient = class extends EventEmitter7 {
|
|
|
1684
1796
|
console.error("[MicdropClient] Error stopping microphone", error);
|
|
1685
1797
|
}
|
|
1686
1798
|
};
|
|
1799
|
+
/**
|
|
1800
|
+
* Stops the call once the assistant finished its answer, the one it is
|
|
1801
|
+
* still preparing included, such as the goodbye that follows an `EndCall`.
|
|
1802
|
+
* Stops right away when no answer is under way.
|
|
1803
|
+
*
|
|
1804
|
+
* A `start()` or a `stop()` meanwhile cancels the wait, so it never acts on
|
|
1805
|
+
* the next call.
|
|
1806
|
+
* @returns Resolves once the call is stopped, or the wait cancelled
|
|
1807
|
+
*/
|
|
1808
|
+
this.stopAfterAnswer = async () => {
|
|
1809
|
+
if (!this.isAnswering) return this.stop();
|
|
1810
|
+
this.cancelPendingStop();
|
|
1811
|
+
return new Promise((resolve) => {
|
|
1812
|
+
const detach = () => {
|
|
1813
|
+
this.off("StateChange", onStateChange);
|
|
1814
|
+
this.pendingStop = void 0;
|
|
1815
|
+
};
|
|
1816
|
+
const onStateChange = () => {
|
|
1817
|
+
if (this.isAnswering) return;
|
|
1818
|
+
detach();
|
|
1819
|
+
this.stop().then(resolve);
|
|
1820
|
+
};
|
|
1821
|
+
this.on("StateChange", onStateChange);
|
|
1822
|
+
this.pendingStop = () => {
|
|
1823
|
+
detach();
|
|
1824
|
+
resolve();
|
|
1825
|
+
};
|
|
1826
|
+
});
|
|
1827
|
+
};
|
|
1687
1828
|
this.mute = () => {
|
|
1688
1829
|
this.vad?.pause();
|
|
1689
1830
|
this._isMuted = true;
|
|
@@ -1760,7 +1901,9 @@ var MicdropClient = class extends EventEmitter7 {
|
|
|
1760
1901
|
});
|
|
1761
1902
|
}
|
|
1762
1903
|
const deviceId = options.deviceId ?? storage.getItem("micdrop.micDevice" /* MicDevice */) ?? void 0;
|
|
1904
|
+
const recorder = this.micRecorder;
|
|
1763
1905
|
await Mic.start(deviceId);
|
|
1906
|
+
if (this.micRecorder !== recorder || !Mic.isStarted) return;
|
|
1764
1907
|
if (Mic.deviceId) {
|
|
1765
1908
|
storage.setItem("micdrop.micDevice" /* MicDevice */, Mic.deviceId);
|
|
1766
1909
|
} else {
|
|
@@ -1828,8 +1971,7 @@ var MicdropClient = class extends EventEmitter7 {
|
|
|
1828
1971
|
clearTimeout(this.connectionTimer);
|
|
1829
1972
|
this.connectionTimer = void 0;
|
|
1830
1973
|
}
|
|
1831
|
-
this.
|
|
1832
|
-
this._isStartingCall = false;
|
|
1974
|
+
this.callStatus = "started";
|
|
1833
1975
|
this.reconnectAttempt = 0;
|
|
1834
1976
|
this.error = void 0;
|
|
1835
1977
|
this.notifyStateChange();
|
|
@@ -1845,7 +1987,7 @@ var MicdropClient = class extends EventEmitter7 {
|
|
|
1845
1987
|
if (data instanceof ArrayBuffer) {
|
|
1846
1988
|
this.log("Received audio", data.byteLength);
|
|
1847
1989
|
if (this.isPaused || this.isUserSpeaking) return;
|
|
1848
|
-
Speaker.playAudio(new Int16Array(data));
|
|
1990
|
+
Speaker.playAudio(new Int16Array(data), this.assistantSampleRate);
|
|
1849
1991
|
this._isProcessing = false;
|
|
1850
1992
|
this.notifyStateChange();
|
|
1851
1993
|
return;
|
|
@@ -1855,6 +1997,17 @@ var MicdropClient = class extends EventEmitter7 {
|
|
|
1855
1997
|
return;
|
|
1856
1998
|
}
|
|
1857
1999
|
this.log("Received message:", data);
|
|
2000
|
+
if (data.startsWith("SampleRate" /* SampleRate */)) {
|
|
2001
|
+
const sampleRate = Number(
|
|
2002
|
+
data.substring("SampleRate" /* SampleRate */.length + 1)
|
|
2003
|
+
);
|
|
2004
|
+
if (Number.isFinite(sampleRate) && sampleRate > 0) {
|
|
2005
|
+
this.assistantSampleRate = sampleRate;
|
|
2006
|
+
} else {
|
|
2007
|
+
console.error("[MicdropClient] Invalid sample rate:", data);
|
|
2008
|
+
}
|
|
2009
|
+
return;
|
|
2010
|
+
}
|
|
1858
2011
|
if (data.startsWith("Message" /* Message */)) {
|
|
1859
2012
|
try {
|
|
1860
2013
|
const message = JSON.parse(
|
|
@@ -1914,7 +2067,7 @@ var MicdropClient = class extends EventEmitter7 {
|
|
|
1914
2067
|
const config = { ...DEFAULT_RECONNECT_OPTIONS, ...this.options.reconnect };
|
|
1915
2068
|
const canReconnect = error && isRecoverableError(error) && config.maxAttempts > 0;
|
|
1916
2069
|
if (canReconnect) {
|
|
1917
|
-
this.
|
|
2070
|
+
this.callStatus = "reconnecting";
|
|
1918
2071
|
this.stopWS();
|
|
1919
2072
|
this.scheduleReconnect(config);
|
|
1920
2073
|
} else {
|
|
@@ -1979,14 +2132,30 @@ var MicdropClient = class extends EventEmitter7 {
|
|
|
1979
2132
|
get vad() {
|
|
1980
2133
|
return this.micRecorder?.vad;
|
|
1981
2134
|
}
|
|
2135
|
+
get status() {
|
|
2136
|
+
const mic = this.micRecorder?.state;
|
|
2137
|
+
if (this.callStatus === "reconnecting") return "reconnecting";
|
|
2138
|
+
if (this.callStatus === "started" && mic?.isStarted) return "started";
|
|
2139
|
+
if (this.callStatus !== "stopped" || mic?.isStarting) return "starting";
|
|
2140
|
+
return "stopped";
|
|
2141
|
+
}
|
|
2142
|
+
get activity() {
|
|
2143
|
+
if (this.isUserSpeaking) return "userSpeaking";
|
|
2144
|
+
if (this.isAssistantSpeaking) return "assistantSpeaking";
|
|
2145
|
+
if (this.isProcessing) return "processing";
|
|
2146
|
+
if (this.isListening) return "listening";
|
|
2147
|
+
return "idle";
|
|
2148
|
+
}
|
|
2149
|
+
/** The call is on, reconnecting included */
|
|
1982
2150
|
get isStarted() {
|
|
1983
|
-
|
|
2151
|
+
const status = this.status;
|
|
2152
|
+
return status === "started" || status === "reconnecting";
|
|
1984
2153
|
}
|
|
1985
2154
|
get isStarting() {
|
|
1986
|
-
return
|
|
2155
|
+
return this.status === "starting";
|
|
1987
2156
|
}
|
|
1988
2157
|
get isReconnecting() {
|
|
1989
|
-
return this.
|
|
2158
|
+
return this.status === "reconnecting";
|
|
1990
2159
|
}
|
|
1991
2160
|
get isMuted() {
|
|
1992
2161
|
return this._isMuted;
|
|
@@ -2039,6 +2208,8 @@ var MicdropClient = class extends EventEmitter7 {
|
|
|
2039
2208
|
}
|
|
2040
2209
|
get state() {
|
|
2041
2210
|
return {
|
|
2211
|
+
status: this.status,
|
|
2212
|
+
activity: this.activity,
|
|
2042
2213
|
isStarting: this.isStarting,
|
|
2043
2214
|
isStarted: this.isStarted,
|
|
2044
2215
|
isReconnecting: this.isReconnecting,
|
|
@@ -2059,62 +2230,70 @@ var MicdropClient = class extends EventEmitter7 {
|
|
|
2059
2230
|
speakerDevices: this.speakerDevices
|
|
2060
2231
|
};
|
|
2061
2232
|
}
|
|
2233
|
+
/** The assistant prepares or speaks an answer */
|
|
2234
|
+
get isAnswering() {
|
|
2235
|
+
const { activity } = this;
|
|
2236
|
+
return activity === "processing" || activity === "assistantSpeaking";
|
|
2237
|
+
}
|
|
2238
|
+
cancelPendingStop() {
|
|
2239
|
+
this.pendingStop?.();
|
|
2240
|
+
}
|
|
2062
2241
|
async stopMic() {
|
|
2063
2242
|
this.micRecorder?.stop();
|
|
2064
2243
|
this.micRecorder = void 0;
|
|
2065
2244
|
await Mic.stop();
|
|
2066
2245
|
this.notifyStateChange();
|
|
2067
2246
|
}
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
throw new MicdropClientError("MissingUrl" /* MissingUrl */);
|
|
2079
|
-
}
|
|
2080
|
-
this.ws = new WebSocket(this.options.url);
|
|
2081
|
-
this.ws.binaryType = "arraybuffer";
|
|
2082
|
-
this.notifyStateChange();
|
|
2083
|
-
this.ws.onopen = this.onWSOpen;
|
|
2084
|
-
this.ws.onmessage = this.onWSMessage;
|
|
2085
|
-
this.ws.onclose = this.onWSClose;
|
|
2086
|
-
this.ws.onerror = this.onWSError;
|
|
2087
|
-
this.connectionTimer = setTimeout(() => {
|
|
2088
|
-
this.connectionTimer = void 0;
|
|
2089
|
-
if (this.ws?.readyState === 0) {
|
|
2090
|
-
this.log("WebSocket connection timeout");
|
|
2091
|
-
this.ws.close();
|
|
2092
|
-
}
|
|
2093
|
-
}, this.options.reconnect?.connectionTimeout ?? DEFAULT_RECONNECT_OPTIONS.connectionTimeout);
|
|
2094
|
-
} catch (error) {
|
|
2095
|
-
this.setError(
|
|
2096
|
-
error instanceof MicdropClientError ? error : new MicdropClientError(
|
|
2097
|
-
"Connection" /* Connection */,
|
|
2098
|
-
error?.message
|
|
2099
|
-
)
|
|
2100
|
-
);
|
|
2101
|
-
await this.stop();
|
|
2102
|
-
throw error;
|
|
2247
|
+
/**
|
|
2248
|
+
* Opens the connection, unless one is already open or opening.
|
|
2249
|
+
*
|
|
2250
|
+
* The socket answers through handlers bound to it, so one left behind by a
|
|
2251
|
+
* stop or a reconnection never acts on the call that followed.
|
|
2252
|
+
*/
|
|
2253
|
+
openWS() {
|
|
2254
|
+
if (this.ws) {
|
|
2255
|
+
this.log("WebSocket is already started");
|
|
2256
|
+
return;
|
|
2103
2257
|
}
|
|
2258
|
+
if (!this.isMicStarted) {
|
|
2259
|
+
throw new MicdropClientError("Mic" /* Mic */);
|
|
2260
|
+
}
|
|
2261
|
+
if (!this.options.url) {
|
|
2262
|
+
throw new MicdropClientError("MissingUrl" /* MissingUrl */);
|
|
2263
|
+
}
|
|
2264
|
+
const ws = new WebSocket(this.options.url);
|
|
2265
|
+
ws.binaryType = "arraybuffer";
|
|
2266
|
+
this.ws = ws;
|
|
2267
|
+
this.assistantSampleRate = DEFAULT_SAMPLE_RATE;
|
|
2268
|
+
this.notifyStateChange();
|
|
2269
|
+
const isCurrent = () => this.ws === ws;
|
|
2270
|
+
ws.onopen = () => isCurrent() && this.onWSOpen();
|
|
2271
|
+
ws.onmessage = (event) => isCurrent() && this.onWSMessage(event);
|
|
2272
|
+
ws.onclose = (event) => isCurrent() && this.onWSClose(event);
|
|
2273
|
+
ws.onerror = () => isCurrent() && this.onWSError();
|
|
2274
|
+
this.connectionTimer = setTimeout(() => {
|
|
2275
|
+
this.connectionTimer = void 0;
|
|
2276
|
+
if (isCurrent() && ws.readyState === 0) {
|
|
2277
|
+
this.log("WebSocket connection timeout");
|
|
2278
|
+
ws.close();
|
|
2279
|
+
}
|
|
2280
|
+
}, this.options.reconnect?.connectionTimeout ?? DEFAULT_RECONNECT_OPTIONS.connectionTimeout);
|
|
2104
2281
|
}
|
|
2105
2282
|
stopWS() {
|
|
2106
2283
|
if (this.connectionTimer) {
|
|
2107
2284
|
clearTimeout(this.connectionTimer);
|
|
2108
2285
|
this.connectionTimer = void 0;
|
|
2109
2286
|
}
|
|
2110
|
-
|
|
2111
|
-
if (
|
|
2112
|
-
this.ws.close();
|
|
2113
|
-
}
|
|
2287
|
+
const ws = this.ws;
|
|
2288
|
+
if (!ws) return;
|
|
2114
2289
|
this.ws = void 0;
|
|
2290
|
+
if (ws.readyState === 1 || ws.readyState === 0) {
|
|
2291
|
+
ws.close();
|
|
2292
|
+
}
|
|
2115
2293
|
this.notifyStateChange();
|
|
2116
2294
|
}
|
|
2117
2295
|
scheduleReconnect(config) {
|
|
2296
|
+
if (this.callStatus !== "reconnecting") return;
|
|
2118
2297
|
if (this.reconnectAttempt >= config.maxAttempts) {
|
|
2119
2298
|
this.stop();
|
|
2120
2299
|
this.setError(new MicdropClientError("Connection" /* Connection */));
|
|
@@ -2127,17 +2306,23 @@ var MicdropClient = class extends EventEmitter7 {
|
|
|
2127
2306
|
if (!this.isPaused && !this.isMuted) {
|
|
2128
2307
|
this.vad?.pause?.();
|
|
2129
2308
|
}
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2309
|
+
this.clearReconnectTimer();
|
|
2310
|
+
this.reconnectTimer = setTimeout(() => {
|
|
2311
|
+
this.reconnectTimer = void 0;
|
|
2312
|
+
if (this.callStatus !== "reconnecting") return;
|
|
2134
2313
|
try {
|
|
2135
|
-
|
|
2136
|
-
} catch {
|
|
2137
|
-
this.
|
|
2314
|
+
this.openWS();
|
|
2315
|
+
} catch (error) {
|
|
2316
|
+
this.setError(toClientError(error));
|
|
2317
|
+
this.stop();
|
|
2138
2318
|
}
|
|
2139
2319
|
}, config.delayMs);
|
|
2140
2320
|
}
|
|
2321
|
+
clearReconnectTimer() {
|
|
2322
|
+
if (!this.reconnectTimer) return;
|
|
2323
|
+
clearTimeout(this.reconnectTimer);
|
|
2324
|
+
this.reconnectTimer = void 0;
|
|
2325
|
+
}
|
|
2141
2326
|
// The same as when the speaker starts on a call without interruptions
|
|
2142
2327
|
onInterruptionsGivenUp() {
|
|
2143
2328
|
this.log("Interruptions disabled until the end of the call");
|
|
@@ -2182,6 +2367,13 @@ var MicdropClient = class extends EventEmitter7 {
|
|
|
2182
2367
|
console.log(`[MicdropClient ${Date.now() - this.startTime}]`, ...message);
|
|
2183
2368
|
}
|
|
2184
2369
|
};
|
|
2370
|
+
function toClientError(error) {
|
|
2371
|
+
if (error instanceof MicdropClientError) return error;
|
|
2372
|
+
return new MicdropClientError(
|
|
2373
|
+
"Connection" /* Connection */,
|
|
2374
|
+
error?.message
|
|
2375
|
+
);
|
|
2376
|
+
}
|
|
2185
2377
|
|
|
2186
2378
|
// src/Micdrop.ts
|
|
2187
2379
|
var globalScope2 = globalThis;
|
|
@@ -2191,8 +2383,10 @@ if (!globalScope2.micdropClient) {
|
|
|
2191
2383
|
var Micdrop = globalScope2.micdropClient;
|
|
2192
2384
|
export {
|
|
2193
2385
|
CONFIRM_MARGIN,
|
|
2386
|
+
DEFAULT_SAMPLE_RATE,
|
|
2194
2387
|
DEFAULT_TURN_MAX_WAIT,
|
|
2195
2388
|
ECHO_TAIL,
|
|
2389
|
+
LazySession,
|
|
2196
2390
|
Mic,
|
|
2197
2391
|
MicController,
|
|
2198
2392
|
MicDriver,
|
|
@@ -2218,6 +2412,7 @@ export {
|
|
|
2218
2412
|
VolumeMeter,
|
|
2219
2413
|
VolumeVAD,
|
|
2220
2414
|
concatFloat32,
|
|
2415
|
+
createDownloadCache,
|
|
2221
2416
|
currentTurn,
|
|
2222
2417
|
equalVADConfig,
|
|
2223
2418
|
floatToPcm16,
|