@micdrop/server 2.8.0 → 2.9.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 +144 -3
- package/dist/index.d.ts +144 -3
- package/dist/index.js +209 -29
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +205 -29
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -27,8 +27,13 @@ var Agent = class extends EventEmitter {
|
|
|
27
27
|
this.answering = true;
|
|
28
28
|
Promise.resolve().then(
|
|
29
29
|
() => this.options.onBeforeAnswer?.bind(this)(stream)
|
|
30
|
-
).then((
|
|
31
|
-
if (
|
|
30
|
+
).then((result) => {
|
|
31
|
+
if (typeof result === "string") {
|
|
32
|
+
stream.write(result);
|
|
33
|
+
this.addAssistantMessage(result);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (result) return;
|
|
32
37
|
return this.generateAnswer(stream);
|
|
33
38
|
}).finally(() => {
|
|
34
39
|
if (stream.writable) {
|
|
@@ -352,6 +357,144 @@ var MockAgent = class extends Agent {
|
|
|
352
357
|
}
|
|
353
358
|
};
|
|
354
359
|
|
|
360
|
+
// src/classifier/Classifier.ts
|
|
361
|
+
import { EventEmitter as EventEmitter2 } from "eventemitter3";
|
|
362
|
+
var Classifier = class extends EventEmitter2 {
|
|
363
|
+
constructor() {
|
|
364
|
+
super(...arguments);
|
|
365
|
+
this.controllers = /* @__PURE__ */ new Set();
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Classifies an input, and emits the result as `Classification`
|
|
369
|
+
* @param input - What to classify: a text, or JSON with named fields
|
|
370
|
+
* @returns The classification, or undefined when cancelled or failed
|
|
371
|
+
*/
|
|
372
|
+
classify(input) {
|
|
373
|
+
const controller = new AbortController();
|
|
374
|
+
this.controllers.add(controller);
|
|
375
|
+
const pending = this.run(input, controller.signal).finally(() => {
|
|
376
|
+
this.controllers.delete(controller);
|
|
377
|
+
if (this._pending === pending) this._pending = void 0;
|
|
378
|
+
});
|
|
379
|
+
this._pending = pending;
|
|
380
|
+
return pending;
|
|
381
|
+
}
|
|
382
|
+
/** The last classification started, until it is done */
|
|
383
|
+
get pending() {
|
|
384
|
+
return this._pending;
|
|
385
|
+
}
|
|
386
|
+
/** Drops every classification in progress, which then emit nothing */
|
|
387
|
+
cancel() {
|
|
388
|
+
this.controllers.forEach((controller) => controller.abort());
|
|
389
|
+
this.controllers.clear();
|
|
390
|
+
this._pending = void 0;
|
|
391
|
+
}
|
|
392
|
+
log(...message) {
|
|
393
|
+
this.logger?.log(...message);
|
|
394
|
+
}
|
|
395
|
+
destroy() {
|
|
396
|
+
this.log("Destroyed");
|
|
397
|
+
this.cancel();
|
|
398
|
+
this.removeAllListeners();
|
|
399
|
+
}
|
|
400
|
+
async run(input, signal) {
|
|
401
|
+
const start = Date.now();
|
|
402
|
+
try {
|
|
403
|
+
const result = await this.evaluate(input, signal);
|
|
404
|
+
if (signal.aborted) return;
|
|
405
|
+
const classification = {
|
|
406
|
+
input,
|
|
407
|
+
result,
|
|
408
|
+
duration: Date.now() - start
|
|
409
|
+
};
|
|
410
|
+
this.log(
|
|
411
|
+
`Classified in ${classification.duration} ms:`,
|
|
412
|
+
JSON.stringify(input)
|
|
413
|
+
);
|
|
414
|
+
this.lastClassification = classification;
|
|
415
|
+
this.emit("Classification", classification);
|
|
416
|
+
return classification;
|
|
417
|
+
} catch (error) {
|
|
418
|
+
if (!signal.aborted) this.log("Classification failed:", error);
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
// src/types.ts
|
|
425
|
+
var MicdropClientCommands = /* @__PURE__ */ ((MicdropClientCommands2) => {
|
|
426
|
+
MicdropClientCommands2["StartSpeaking"] = "StartSpeaking";
|
|
427
|
+
MicdropClientCommands2["StopSpeaking"] = "StopSpeaking";
|
|
428
|
+
MicdropClientCommands2["Mute"] = "Mute";
|
|
429
|
+
return MicdropClientCommands2;
|
|
430
|
+
})(MicdropClientCommands || {});
|
|
431
|
+
var MicdropServerCommands = /* @__PURE__ */ ((MicdropServerCommands2) => {
|
|
432
|
+
MicdropServerCommands2["Message"] = "Message";
|
|
433
|
+
MicdropServerCommands2["PartialAssistantMessage"] = "PartialAssistantMessage";
|
|
434
|
+
MicdropServerCommands2["CancelLastUserMessage"] = "CancelLastUserMessage";
|
|
435
|
+
MicdropServerCommands2["SkipAnswer"] = "SkipAnswer";
|
|
436
|
+
MicdropServerCommands2["EndCall"] = "EndCall";
|
|
437
|
+
MicdropServerCommands2["ToolCall"] = "ToolCall";
|
|
438
|
+
MicdropServerCommands2["Classification"] = "Classification";
|
|
439
|
+
return MicdropServerCommands2;
|
|
440
|
+
})(MicdropServerCommands || {});
|
|
441
|
+
function currentTurn(conversation) {
|
|
442
|
+
let start = conversation.length;
|
|
443
|
+
while (start > 0) {
|
|
444
|
+
const item = conversation[start - 1];
|
|
445
|
+
if (item.role !== "user" || item.metadata?.classification) break;
|
|
446
|
+
start--;
|
|
447
|
+
}
|
|
448
|
+
const messages = conversation.slice(start);
|
|
449
|
+
return {
|
|
450
|
+
transcript: messages.map((message) => message.content).join(" "),
|
|
451
|
+
messages,
|
|
452
|
+
before: conversation.slice(0, start)
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
function getTurnClassification(conversation) {
|
|
456
|
+
for (let index = conversation.length - 1; index >= 0; index--) {
|
|
457
|
+
const item = conversation[index];
|
|
458
|
+
if (item.role === "user") return item.metadata?.classification;
|
|
459
|
+
}
|
|
460
|
+
return void 0;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// src/classifier/turnInput.ts
|
|
464
|
+
function turnInput(conversation, history = 1) {
|
|
465
|
+
const turn = currentTurn(conversation);
|
|
466
|
+
const input = { history: [], turn: turn.transcript };
|
|
467
|
+
let rest = turn.before;
|
|
468
|
+
for (let n = 0; n < history && rest.length; n++) {
|
|
469
|
+
let end = rest.length;
|
|
470
|
+
const answers = [];
|
|
471
|
+
while (end > 0 && rest[end - 1].role !== "user") {
|
|
472
|
+
const item = rest[end - 1];
|
|
473
|
+
if (item.role === "assistant" && item.content.trim()) {
|
|
474
|
+
answers.unshift(item.content);
|
|
475
|
+
}
|
|
476
|
+
end--;
|
|
477
|
+
}
|
|
478
|
+
let start = end;
|
|
479
|
+
while (start > 0) {
|
|
480
|
+
const item = rest[start - 1];
|
|
481
|
+
if (item.role !== "user") break;
|
|
482
|
+
if (start < end && item.metadata?.classification)
|
|
483
|
+
break;
|
|
484
|
+
start--;
|
|
485
|
+
}
|
|
486
|
+
if (answers.length) {
|
|
487
|
+
input.history.unshift({ role: "assistant", text: answers.join(" ") });
|
|
488
|
+
}
|
|
489
|
+
if (start < end) {
|
|
490
|
+
const text = rest.slice(start, end).map((item) => "content" in item ? item.content : "").join(" ");
|
|
491
|
+
input.history.unshift({ role: "user", text });
|
|
492
|
+
}
|
|
493
|
+
rest = rest.slice(0, start);
|
|
494
|
+
}
|
|
495
|
+
return { input, message: turn.messages[turn.messages.length - 1] };
|
|
496
|
+
}
|
|
497
|
+
|
|
355
498
|
// src/audio/Pcm16Resampler.ts
|
|
356
499
|
var Pcm16Resampler = class {
|
|
357
500
|
// Fractional position into the first sample of the buffer
|
|
@@ -439,30 +582,12 @@ function handleError(socket, error) {
|
|
|
439
582
|
}
|
|
440
583
|
|
|
441
584
|
// src/MicdropServer.ts
|
|
442
|
-
import { EventEmitter as
|
|
585
|
+
import { EventEmitter as EventEmitter3 } from "eventemitter3";
|
|
443
586
|
import { PassThrough as PassThrough2, Transform } from "stream";
|
|
444
|
-
|
|
445
|
-
// src/types.ts
|
|
446
|
-
var MicdropClientCommands = /* @__PURE__ */ ((MicdropClientCommands2) => {
|
|
447
|
-
MicdropClientCommands2["StartSpeaking"] = "StartSpeaking";
|
|
448
|
-
MicdropClientCommands2["StopSpeaking"] = "StopSpeaking";
|
|
449
|
-
MicdropClientCommands2["Mute"] = "Mute";
|
|
450
|
-
return MicdropClientCommands2;
|
|
451
|
-
})(MicdropClientCommands || {});
|
|
452
|
-
var MicdropServerCommands = /* @__PURE__ */ ((MicdropServerCommands2) => {
|
|
453
|
-
MicdropServerCommands2["Message"] = "Message";
|
|
454
|
-
MicdropServerCommands2["PartialAssistantMessage"] = "PartialAssistantMessage";
|
|
455
|
-
MicdropServerCommands2["CancelLastUserMessage"] = "CancelLastUserMessage";
|
|
456
|
-
MicdropServerCommands2["SkipAnswer"] = "SkipAnswer";
|
|
457
|
-
MicdropServerCommands2["EndCall"] = "EndCall";
|
|
458
|
-
MicdropServerCommands2["ToolCall"] = "ToolCall";
|
|
459
|
-
return MicdropServerCommands2;
|
|
460
|
-
})(MicdropServerCommands || {});
|
|
461
|
-
|
|
462
|
-
// src/MicdropServer.ts
|
|
463
587
|
var USER_SAMPLE_RATE = 16e3;
|
|
464
588
|
var DEFAULT_TURN_MAX_WAIT = 4e3;
|
|
465
|
-
var
|
|
589
|
+
var DEFAULT_CLASSIFICATION_MAX_WAIT = 1e3;
|
|
590
|
+
var MicdropServer = class extends EventEmitter3 {
|
|
466
591
|
constructor(socket, config) {
|
|
467
592
|
super();
|
|
468
593
|
this.socket = null;
|
|
@@ -484,6 +609,7 @@ var MicdropServer = class extends EventEmitter2 {
|
|
|
484
609
|
this.agent?.destroy();
|
|
485
610
|
this.config.stt?.destroy();
|
|
486
611
|
this.config.tts?.destroy();
|
|
612
|
+
this.config.classifier?.destroy();
|
|
487
613
|
this.emit("End", {
|
|
488
614
|
conversation,
|
|
489
615
|
duration
|
|
@@ -575,6 +701,14 @@ var MicdropServer = class extends EventEmitter2 {
|
|
|
575
701
|
)
|
|
576
702
|
);
|
|
577
703
|
}
|
|
704
|
+
if (config.classifierOptions?.sendToClient) {
|
|
705
|
+
config.classifier?.on(
|
|
706
|
+
"Classification",
|
|
707
|
+
(classification) => this.socket?.send(
|
|
708
|
+
`${"Classification" /* Classification */} ${JSON.stringify(classification)}`
|
|
709
|
+
)
|
|
710
|
+
);
|
|
711
|
+
}
|
|
578
712
|
queueMicrotask(() => this.sendFirstMessage());
|
|
579
713
|
socket.on("close", this.onClose);
|
|
580
714
|
socket.on("message", this.onMessage);
|
|
@@ -611,6 +745,7 @@ var MicdropServer = class extends EventEmitter2 {
|
|
|
611
745
|
this.processQueue();
|
|
612
746
|
}
|
|
613
747
|
cancel() {
|
|
748
|
+
this.config?.classifier?.cancel();
|
|
614
749
|
this.config?.tts?.cancel();
|
|
615
750
|
this.agent?.cancel();
|
|
616
751
|
this.operationQueue = [];
|
|
@@ -714,6 +849,7 @@ var MicdropServer = class extends EventEmitter2 {
|
|
|
714
849
|
if (realtime) {
|
|
715
850
|
realtime.endTurn();
|
|
716
851
|
} else {
|
|
852
|
+
this.classifyTurn();
|
|
717
853
|
this.answer();
|
|
718
854
|
}
|
|
719
855
|
}
|
|
@@ -757,6 +893,7 @@ var MicdropServer = class extends EventEmitter2 {
|
|
|
757
893
|
`${"Message" /* Message */} ${JSON.stringify(message)}`
|
|
758
894
|
);
|
|
759
895
|
this.emit("Message", message);
|
|
896
|
+
if (message.role === "user" && this.config?.realtime) this.classifyTurn();
|
|
760
897
|
}
|
|
761
898
|
sendPartialAnswer(content) {
|
|
762
899
|
if (!this.socket || content === "") return;
|
|
@@ -777,6 +914,9 @@ var MicdropServer = class extends EventEmitter2 {
|
|
|
777
914
|
}
|
|
778
915
|
}
|
|
779
916
|
answer() {
|
|
917
|
+
if (this.config?.classifierOptions?.waitBeforeAnswer) {
|
|
918
|
+
this.queueOperation(() => this.waitForClassification());
|
|
919
|
+
}
|
|
780
920
|
this.queueOperation(async () => {
|
|
781
921
|
await this._answer();
|
|
782
922
|
});
|
|
@@ -826,6 +966,38 @@ var MicdropServer = class extends EventEmitter2 {
|
|
|
826
966
|
});
|
|
827
967
|
return stream.pipe(forward);
|
|
828
968
|
}
|
|
969
|
+
/**
|
|
970
|
+
* Classifies the turn that just ended, with the one before, and keeps the
|
|
971
|
+
* result in the metadata of its last message
|
|
972
|
+
*/
|
|
973
|
+
classifyTurn() {
|
|
974
|
+
const classifier = this.config?.classifier;
|
|
975
|
+
if (!classifier) return;
|
|
976
|
+
const { input, message } = turnInput(
|
|
977
|
+
this.conversation,
|
|
978
|
+
this.config?.classifierOptions?.history
|
|
979
|
+
);
|
|
980
|
+
if (!message || input.turn.trim() === "") return;
|
|
981
|
+
classifier.classify(input).then((classification) => {
|
|
982
|
+
if (!classification) return;
|
|
983
|
+
message.metadata = { ...message.metadata, classification };
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
/** Waits for the classification in progress, never longer than its limit */
|
|
987
|
+
async waitForClassification() {
|
|
988
|
+
const pending = this.config?.classifier?.pending;
|
|
989
|
+
if (!pending) return;
|
|
990
|
+
const limit = this.config?.classifierOptions?.maxWait ?? DEFAULT_CLASSIFICATION_MAX_WAIT;
|
|
991
|
+
let timer;
|
|
992
|
+
const timeout = new Promise((resolve) => {
|
|
993
|
+
timer = setTimeout(() => {
|
|
994
|
+
this.log(`Classification took over ${limit} ms, answering without it`);
|
|
995
|
+
resolve();
|
|
996
|
+
}, limit);
|
|
997
|
+
});
|
|
998
|
+
await Promise.race([pending, timeout]);
|
|
999
|
+
clearTimeout(timer);
|
|
1000
|
+
}
|
|
829
1001
|
// Run text-to-speech and send to client
|
|
830
1002
|
speak(message) {
|
|
831
1003
|
this.queueOperation(async () => {
|
|
@@ -1142,8 +1314,8 @@ var Realtime = class extends Agent {
|
|
|
1142
1314
|
};
|
|
1143
1315
|
|
|
1144
1316
|
// src/recorder/MicdropRecorder.ts
|
|
1145
|
-
import { EventEmitter as
|
|
1146
|
-
var MicdropRecorder = class extends
|
|
1317
|
+
import { EventEmitter as EventEmitter4 } from "eventemitter3";
|
|
1318
|
+
var MicdropRecorder = class extends EventEmitter4 {
|
|
1147
1319
|
constructor(server) {
|
|
1148
1320
|
super();
|
|
1149
1321
|
this.server = server;
|
|
@@ -1300,8 +1472,8 @@ var SharedInstances = class {
|
|
|
1300
1472
|
};
|
|
1301
1473
|
|
|
1302
1474
|
// src/stt/STT.ts
|
|
1303
|
-
import { EventEmitter as
|
|
1304
|
-
var STT = class extends
|
|
1475
|
+
import { EventEmitter as EventEmitter5 } from "eventemitter3";
|
|
1476
|
+
var STT = class extends EventEmitter5 {
|
|
1305
1477
|
log(...message) {
|
|
1306
1478
|
this.logger?.log(...message);
|
|
1307
1479
|
}
|
|
@@ -1382,8 +1554,8 @@ var FallbackSTT = class extends STT {
|
|
|
1382
1554
|
import { PassThrough as PassThrough4 } from "stream";
|
|
1383
1555
|
|
|
1384
1556
|
// src/tts/TTS.ts
|
|
1385
|
-
import { EventEmitter as
|
|
1386
|
-
var TTS = class extends
|
|
1557
|
+
import { EventEmitter as EventEmitter6 } from "eventemitter3";
|
|
1558
|
+
var TTS = class extends EventEmitter6 {
|
|
1387
1559
|
log(...message) {
|
|
1388
1560
|
this.logger?.log(...message);
|
|
1389
1561
|
}
|
|
@@ -1634,6 +1806,7 @@ export {
|
|
|
1634
1806
|
AUTO_SEMANTIC_TURN_PROMPT,
|
|
1635
1807
|
AUTO_SEMANTIC_TURN_TOOL_NAME,
|
|
1636
1808
|
Agent,
|
|
1809
|
+
Classifier,
|
|
1637
1810
|
FallbackAgent,
|
|
1638
1811
|
FallbackSTT,
|
|
1639
1812
|
FallbackTTS,
|
|
@@ -1654,9 +1827,12 @@ export {
|
|
|
1654
1827
|
SentenceTTS,
|
|
1655
1828
|
SharedInstances,
|
|
1656
1829
|
TTS,
|
|
1830
|
+
currentTurn,
|
|
1657
1831
|
float32ToPcm16,
|
|
1832
|
+
getTurnClassification,
|
|
1658
1833
|
handleError,
|
|
1659
1834
|
pcm16ToFloat32,
|
|
1835
|
+
turnInput,
|
|
1660
1836
|
waitForParams
|
|
1661
1837
|
};
|
|
1662
1838
|
//# sourceMappingURL=index.mjs.map
|