@fonoster/autopilot 0.7.20 → 0.7.21

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.
@@ -2,6 +2,7 @@ import { AutopilotParams } from "./types";
2
2
  declare class Autopilot {
3
3
  private params;
4
4
  private actor;
5
+ private vadWorker;
5
6
  constructor(params: AutopilotParams);
6
7
  start(): void;
7
8
  stop(): void;
package/dist/Autopilot.js CHANGED
@@ -1,4 +1,7 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.Autopilot = void 0;
4
7
  /*
@@ -19,6 +22,8 @@ exports.Autopilot = void 0;
19
22
  * See the License for the specific language governing permissions and
20
23
  * limitations under the License.
21
24
  */
25
+ const path_1 = __importDefault(require("path"));
26
+ const worker_threads_1 = require("worker_threads");
22
27
  const logger_1 = require("@fonoster/logger");
23
28
  const xstate_1 = require("xstate");
24
29
  const machine_1 = require("./machine/machine");
@@ -27,6 +32,10 @@ class Autopilot {
27
32
  constructor(params) {
28
33
  this.params = params;
29
34
  const { voice, languageModel, conversationSettings } = this.params;
35
+ const vadWorkerPath = path_1.default.resolve(__dirname, "../dist", "./vadWorker");
36
+ this.vadWorker = new worker_threads_1.Worker(vadWorkerPath, {
37
+ workerData: conversationSettings.vad
38
+ });
30
39
  this.actor = (0, xstate_1.createActor)(machine_1.machine, {
31
40
  input: {
32
41
  conversationSettings,
@@ -42,30 +51,36 @@ class Autopilot {
42
51
  });
43
52
  this.setupVoiceStream();
44
53
  this.setupSpeechGathering();
54
+ this.vadWorker.on("error", (err) => {
55
+ logger.error("vad worker error", err);
56
+ });
57
+ this.vadWorker.on("exit", (code) => {
58
+ if (code !== 0) {
59
+ logger.error("vad worker stopped with exit code", { code });
60
+ }
61
+ });
45
62
  }
46
63
  stop() {
47
64
  logger.verbose("stopping autopilot");
48
65
  this.actor.stop();
66
+ this.vadWorker.terminate();
49
67
  }
50
68
  async setupVoiceStream() {
51
- const { voice, vad } = this.params;
69
+ const { voice } = this.params;
52
70
  const stream = await voice.stream();
53
- stream.onData(this.handleVoicePayload(vad));
71
+ stream.onData(this.handleVoicePayload.bind(this));
72
+ this.vadWorker.on("message", (event) => {
73
+ logger.verbose("received speech event from vad", { event });
74
+ this.actor.send({ type: event });
75
+ });
54
76
  }
55
- handleVoicePayload(vad) {
56
- return (chunk) => {
57
- try {
58
- vad.processChunk(chunk, (event) => {
59
- if (["SPEECH_START", "SPEECH_END"].includes(event)) {
60
- logger.verbose("received speech event", { event });
61
- this.actor.send({ type: event });
62
- }
63
- });
64
- }
65
- catch (err) {
66
- logger.error("an error occurred while processing vad", err);
67
- }
68
- };
77
+ handleVoicePayload(chunk) {
78
+ try {
79
+ this.vadWorker.postMessage(chunk);
80
+ }
81
+ catch (err) {
82
+ logger.error("an error occurred while processing vad", err);
83
+ }
69
84
  }
70
85
  async setupSpeechGathering() {
71
86
  const { voice } = this.params;
@@ -60,7 +60,6 @@ async function handleVoiceRequest(req, res) {
60
60
  const assistantConfig = (0, loadAssistantConfig_1.loadAssistantConfig)();
61
61
  const knowledgeBase = await (0, loadKnowledgeBase_1.loadKnowledgeBase)();
62
62
  const voice = new _1.VoiceImpl(sessionRef, res);
63
- const vad = new _1.SileroVad(assistantConfig.conversationSettings.vad);
64
63
  const languageModel = (0, createLanguageModel_1.createLanguageModel)({
65
64
  voice,
66
65
  assistantConfig,
@@ -74,7 +73,6 @@ async function handleVoiceRequest(req, res) {
74
73
  const autopilot = new _1.default({
75
74
  conversationSettings: assistantConfig.conversationSettings,
76
75
  voice,
77
- vad,
78
76
  languageModel
79
77
  });
80
78
  autopilot.start();
package/dist/types.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { ConversationSettings } from "./assistants";
2
2
  import { LanguageModel } from "./models";
3
- import { Vad } from "./vad";
4
3
  import { Voice } from "./voice";
5
4
  declare enum LANGUAGE_MODEL_PROVIDER {
6
5
  OPENAI = "openai",
@@ -9,7 +8,6 @@ declare enum LANGUAGE_MODEL_PROVIDER {
9
8
  }
10
9
  type AutopilotParams = {
11
10
  voice: Voice;
12
- vad: Vad;
13
11
  conversationSettings: ConversationSettings;
14
12
  languageModel: LanguageModel;
15
13
  };
@@ -1,5 +1,6 @@
1
+ type VadEvent = "SPEECH_START" | "SPEECH_END";
1
2
  type Vad = {
2
- processChunk: (chunk: Uint8Array, callback: (event: "SPEECH_START" | "SPEECH_END") => void) => void;
3
+ processChunk: (chunk: Uint8Array, callback: (event: VadEvent) => void) => void;
3
4
  };
4
5
  type SpeechProbabilities = {
5
6
  notSpeech: number;
@@ -16,4 +17,4 @@ type ONNXRuntimeAPI = {
16
17
  new (type: "float32", data: Float32Array, dims: [1, number]): unknown;
17
18
  };
18
19
  };
19
- export { ONNXRuntimeAPI, SpeechProbabilities, Vad };
20
+ export { ONNXRuntimeAPI, SpeechProbabilities, Vad, VadEvent };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /*
4
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
5
+ * http://github.com/fonoster/fonoster
6
+ *
7
+ * This file is part of Fonoster
8
+ *
9
+ * Licensed under the MIT License (the "License");
10
+ * you may not use this file except in compliance with
11
+ * the License. You may obtain a copy of the License at
12
+ *
13
+ * https://opensource.org/licenses/MIT
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+ const worker_threads_1 = require("worker_threads");
22
+ const SileroVad_1 = require("./vad/SileroVad");
23
+ const vad = new SileroVad_1.SileroVad(worker_threads_1.workerData);
24
+ worker_threads_1.parentPort?.on("message", (chunk) => {
25
+ vad.processChunk(chunk, (voiceActivity) => {
26
+ worker_threads_1.parentPort?.postMessage(voiceActivity);
27
+ });
28
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fonoster/autopilot",
3
- "version": "0.7.20",
3
+ "version": "0.7.21",
4
4
  "description": "Voice AI for the Fonoster platform",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/fonoster#readme",
@@ -56,5 +56,5 @@
56
56
  "devDependencies": {
57
57
  "typescript": "^5.5.4"
58
58
  },
59
- "gitHead": "bde17656cad787fd22127beaa5d8617e38be2eb6"
59
+ "gitHead": "0de74ab45f5fe25b0f096ad02bab2be00be53d89"
60
60
  }