@fonoster/autopilot 0.4.15

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.
Files changed (44) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cerebro/cerebro.d.ts +26 -0
  3. package/dist/cerebro/cerebro.js +155 -0
  4. package/dist/cerebro/effects.d.ts +11 -0
  5. package/dist/cerebro/effects.js +91 -0
  6. package/dist/cerebro/helper.d.ts +5 -0
  7. package/dist/cerebro/helper.js +43 -0
  8. package/dist/cerebro/index.d.ts +2 -0
  9. package/dist/cerebro/index.js +36 -0
  10. package/dist/cerebro/types.d.ts +40 -0
  11. package/dist/cerebro/types.js +9 -0
  12. package/dist/events/emitter.d.ts +7 -0
  13. package/dist/events/emitter.js +13 -0
  14. package/dist/events/server.d.ts +12 -0
  15. package/dist/events/server.js +67 -0
  16. package/dist/events/types.d.ts +20 -0
  17. package/dist/events/types.js +29 -0
  18. package/dist/file-retention/cron.d.ts +2 -0
  19. package/dist/file-retention/cron.js +23 -0
  20. package/dist/file-retention/index.d.ts +1 -0
  21. package/dist/file-retention/index.js +35 -0
  22. package/dist/file-retention/task.d.ts +6 -0
  23. package/dist/file-retention/task.js +56 -0
  24. package/dist/index.d.ts +1 -0
  25. package/dist/index.js +29 -0
  26. package/dist/intents/df_utils.d.ts +3 -0
  27. package/dist/intents/df_utils.js +39 -0
  28. package/dist/intents/dialogflow_cx.d.ts +15 -0
  29. package/dist/intents/dialogflow_cx.js +87 -0
  30. package/dist/intents/dialogflow_es.d.ts +15 -0
  31. package/dist/intents/dialogflow_es.js +172 -0
  32. package/dist/intents/engines.d.ts +3 -0
  33. package/dist/intents/engines.js +32 -0
  34. package/dist/intents/types.d.ts +26 -0
  35. package/dist/intents/types.js +2 -0
  36. package/dist/pilot.d.ts +3 -0
  37. package/dist/pilot.js +131 -0
  38. package/dist/types.d.ts +36 -0
  39. package/dist/types.js +2 -0
  40. package/dist/util.d.ts +6 -0
  41. package/dist/util.js +26 -0
  42. package/dist/voice.d.ts +3 -0
  43. package/dist/voice.js +154 -0
  44. package/package.json +41 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Fonoster Inc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,26 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { EffectsManager } from "./effects";
4
+ import { IntentsEngine } from "../intents/types";
5
+ import { SGatherStream, VoiceRequest, VoiceResponse } from "@fonoster/voice";
6
+ import { CerebroConfig, CerebroStatus } from "./types";
7
+ import Events from "events";
8
+ export declare class Cerebro {
9
+ voiceResponse: VoiceResponse;
10
+ cerebroEvents: Events;
11
+ voiceRequest: VoiceRequest;
12
+ status: CerebroStatus;
13
+ activationTimeout: number;
14
+ activeTimer: NodeJS.Timer;
15
+ intentsEngine: IntentsEngine;
16
+ stream: SGatherStream;
17
+ config: CerebroConfig;
18
+ lastIntent: any;
19
+ effects: EffectsManager;
20
+ constructor(config: CerebroConfig);
21
+ wake(): Promise<void>;
22
+ sleep(): Promise<void>;
23
+ startActiveTimer(): void;
24
+ resetActiveTimer(): void;
25
+ stopPlayback(): Promise<void>;
26
+ }
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Cerebro = void 0;
7
+ /* eslint-disable require-jsdoc */
8
+ /*
9
+ * Copyright (C) 2023 by Fonoster Inc (https://fonoster.com)
10
+ * http://github.com/fonoster/rox
11
+ *
12
+ * This file is part of Rox AI
13
+ *
14
+ * Licensed under the MIT License (the "License");
15
+ * you may not use this file except in compliance with
16
+ * the License. You may obtain a copy of the License at
17
+ *
18
+ * https://opensource.org/licenses/MIT
19
+ *
20
+ * Unless required by applicable law or agreed to in writing, software
21
+ * distributed under the License is distributed on an "AS IS" BASIS,
22
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23
+ * See the License for the specific language governing permissions and
24
+ * limitations under the License.
25
+ */
26
+ const effects_1 = require("./effects");
27
+ const types_1 = require("./types");
28
+ const util_1 = require("../util");
29
+ const types_2 = require("../events/types");
30
+ const logger_1 = require("@fonoster/logger");
31
+ const events_1 = __importDefault(require("events"));
32
+ const logger = (0, logger_1.getLogger)({ service: "autopilot", filePath: __filename });
33
+ class Cerebro {
34
+ voiceResponse;
35
+ cerebroEvents;
36
+ voiceRequest;
37
+ status;
38
+ activationTimeout;
39
+ activeTimer;
40
+ intentsEngine;
41
+ stream;
42
+ config;
43
+ lastIntent;
44
+ effects;
45
+ constructor(config) {
46
+ this.voiceResponse = config.voiceResponse;
47
+ this.voiceRequest = config.voiceRequest;
48
+ this.cerebroEvents = new events_1.default();
49
+ this.status = types_1.CerebroStatus.SLEEP;
50
+ this.activationTimeout = config.activationTimeout || 15000;
51
+ this.intentsEngine = config.intentsEngine;
52
+ this.effects = new effects_1.EffectsManager({
53
+ playbackId: config.voiceConfig.playbackId,
54
+ eventsClient: config.eventsClient,
55
+ voice: config.voiceResponse,
56
+ voiceConfig: config.voiceConfig,
57
+ activationIntentId: config.activationIntentId,
58
+ transfer: config.transfer
59
+ });
60
+ this.config = config;
61
+ }
62
+ // Subscribe to events
63
+ async wake() {
64
+ this.status = types_1.CerebroStatus.AWAKE_PASSIVE;
65
+ this.voiceResponse.on("error", (error) => {
66
+ this.cerebroEvents.emit("error", error);
67
+ (0, logger_1.ulogger)({
68
+ accessKeyId: this.voiceRequest.accessKeyId,
69
+ eventType: logger_1.ULogType.APP,
70
+ level: "error",
71
+ message: error.message
72
+ });
73
+ });
74
+ const speechConfig = { source: "speech,dtmf" };
75
+ if (this.config.alternativeLanguageCode) {
76
+ speechConfig.model = "command_and_search";
77
+ speechConfig.alternativeLanguageCodes = [
78
+ this.config.alternativeLanguageCode
79
+ ];
80
+ }
81
+ this.stream = await this.voiceResponse.sgather(speechConfig);
82
+ this.stream.on("transcript", async (data) => {
83
+ if (data.isFinal) {
84
+ const intent = await this.intentsEngine.findIntent(data.transcript, {
85
+ telephony: {
86
+ caller_id: this.voiceRequest.callerNumber
87
+ }
88
+ });
89
+ logger.verbose("cerebro received new transcription from user", {
90
+ text: data.transcript,
91
+ ref: intent.ref,
92
+ confidence: intent.confidence
93
+ });
94
+ await this.effects.invokeEffects(intent, this.status, async () => {
95
+ await this.stopPlayback();
96
+ if (this.config.activationIntentId === intent.ref) {
97
+ (0, util_1.sendClientEvent)(this.config.eventsClient, {
98
+ eventName: types_2.CLIENT_EVENTS.RECOGNIZING
99
+ });
100
+ if (this.status === types_1.CerebroStatus.AWAKE_ACTIVE) {
101
+ this.resetActiveTimer();
102
+ }
103
+ else {
104
+ this.startActiveTimer();
105
+ }
106
+ }
107
+ });
108
+ // Need to save this to avoid duplicate intents
109
+ this.lastIntent = intent;
110
+ }
111
+ });
112
+ }
113
+ // Unsubscribe from events
114
+ async sleep() {
115
+ logger.verbose("cerebro timeout and is going to sleep");
116
+ await this.voiceResponse.closeMediaPipe();
117
+ this.stream.close();
118
+ this.status = types_1.CerebroStatus.SLEEP;
119
+ }
120
+ startActiveTimer() {
121
+ this.status = types_1.CerebroStatus.AWAKE_ACTIVE;
122
+ this.activeTimer = setTimeout(() => {
123
+ this.status = types_1.CerebroStatus.AWAKE_PASSIVE;
124
+ (0, util_1.sendClientEvent)(this.config.eventsClient, {
125
+ eventName: types_2.CLIENT_EVENTS.RECOGNIZING_FINISHED
126
+ });
127
+ logger.verbose("cerebro changed awake status", { status: this.status });
128
+ }, this.activationTimeout);
129
+ logger.verbose("cerebro changed awake status", { status: this.status });
130
+ }
131
+ resetActiveTimer() {
132
+ logger.verbose("cerebro is reseting awake status");
133
+ clearTimeout(this.activeTimer);
134
+ this.startActiveTimer();
135
+ }
136
+ async stopPlayback() {
137
+ const { playbackId } = this.config.voiceConfig;
138
+ if (playbackId) {
139
+ try {
140
+ const playbackControl = this.voiceResponse.playback(playbackId);
141
+ logger.verbose("cerebro is stoping playback", { playbackId });
142
+ await playbackControl.stop();
143
+ }
144
+ catch (e) {
145
+ (0, logger_1.ulogger)({
146
+ accessKeyId: this.voiceRequest.accessKeyId,
147
+ eventType: logger_1.ULogType.APP,
148
+ level: "error",
149
+ message: e.message
150
+ });
151
+ }
152
+ }
153
+ }
154
+ }
155
+ exports.Cerebro = Cerebro;
@@ -0,0 +1,11 @@
1
+ import { VoiceResponse } from "@fonoster/voice";
2
+ import { Intent } from "../intents/types";
3
+ import { EffectsManagerConfig, CerebroStatus, Effect } from "./types";
4
+ export declare class EffectsManager {
5
+ voice: VoiceResponse;
6
+ config: EffectsManagerConfig;
7
+ constructor(config: EffectsManagerConfig);
8
+ invokeEffects(intent: Intent, status: CerebroStatus, activateCallback: Function): Promise<void>;
9
+ run(effect: Effect): Promise<void>;
10
+ transferEffect(voice: VoiceResponse, effect: Effect): Promise<void>;
11
+ }
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EffectsManager = void 0;
4
+ const nanoid_1 = require("nanoid");
5
+ const helper_1 = require("./helper");
6
+ const types_1 = require("./types");
7
+ const util_1 = require("../util");
8
+ const types_2 = require("../events/types");
9
+ const logger_1 = require("@fonoster/logger");
10
+ const logger = (0, logger_1.getLogger)({ service: "autopilot", filePath: __filename });
11
+ class EffectsManager {
12
+ voice;
13
+ config;
14
+ constructor(config) {
15
+ this.voice = config.voice;
16
+ this.config = config;
17
+ }
18
+ async invokeEffects(intent, status, activateCallback) {
19
+ activateCallback();
20
+ if (this.config.activationIntentId === intent.ref) {
21
+ logger.verbose("fired activation intent");
22
+ return;
23
+ }
24
+ else if (this.config.activationIntentId &&
25
+ status != types_1.CerebroStatus.AWAKE_ACTIVE) {
26
+ logger.verbose("received an intent but cerebro is not awake");
27
+ // If we have activation intent cerebro needs and active status
28
+ // before we can have any effects
29
+ return;
30
+ }
31
+ for (const e of intent.effects) {
32
+ logger.verbose("effects running effect", { type: e.type });
33
+ await this.run(e);
34
+ }
35
+ }
36
+ async run(effect) {
37
+ switch (effect.type) {
38
+ case "say":
39
+ await this.voice.say(effect.parameters["response"], this.config.voiceConfig);
40
+ break;
41
+ case "hangup":
42
+ await this.voice.hangup();
43
+ (0, util_1.sendClientEvent)(this.config.eventsClient, {
44
+ eventName: types_2.CLIENT_EVENTS.HANGUP
45
+ });
46
+ break;
47
+ case "transfer":
48
+ // TODO: Add record effect
49
+ await this.transferEffect(this.voice, effect);
50
+ break;
51
+ case "send_data":
52
+ // Only send if client support events
53
+ (0, util_1.sendClientEvent)(this.config.eventsClient, {
54
+ eventName: types_2.CLIENT_EVENTS.RECOGNIZING_FINISHED
55
+ });
56
+ (0, util_1.sendClientEvent)(this.config.eventsClient, {
57
+ eventName: types_2.CLIENT_EVENTS.INTENT,
58
+ intent: effect.parameters
59
+ });
60
+ break;
61
+ default:
62
+ throw new Error(`effects received unknown effect ${effect.type}`);
63
+ }
64
+ }
65
+ async transferEffect(voice, effect) {
66
+ await this.voice.closeMediaPipe();
67
+ const stream = await this.voice.dial(effect.parameters["destination"]);
68
+ const playbackId = (0, nanoid_1.nanoid)();
69
+ const control = this.voice.playback(playbackId);
70
+ let stay = true;
71
+ const moveForward = async () => {
72
+ stay = false;
73
+ await control.stop();
74
+ };
75
+ stream.on("answer", () => {
76
+ moveForward();
77
+ });
78
+ stream.on("busy", async () => {
79
+ await moveForward();
80
+ await (0, helper_1.playBusyAndHangup)(this.voice, playbackId, this.config);
81
+ });
82
+ stream.on("noanswer", async () => {
83
+ await moveForward();
84
+ await (0, helper_1.playNoAnswerAndHangup)(this.voice, playbackId, this.config);
85
+ });
86
+ while (stay) {
87
+ await (0, helper_1.playTransfering)(this.voice, playbackId, this.config);
88
+ }
89
+ }
90
+ }
91
+ exports.EffectsManager = EffectsManager;
@@ -0,0 +1,5 @@
1
+ import { VoiceResponse } from "@fonoster/voice";
2
+ import { EffectsManagerConfig } from "./types";
3
+ export declare const playTransfering: (voice: VoiceResponse, playbackId: string, config: EffectsManagerConfig) => Promise<void>;
4
+ export declare const playBusyAndHangup: (voice: VoiceResponse, playbackId: string, config: EffectsManagerConfig) => Promise<void>;
5
+ export declare const playNoAnswerAndHangup: (voice: VoiceResponse, playbackId: string, config: EffectsManagerConfig) => Promise<void>;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.playNoAnswerAndHangup = exports.playBusyAndHangup = exports.playTransfering = void 0;
4
+ const playOrSay = async (param) => {
5
+ if (param.media) {
6
+ await param.voice.play("sound:" + param.media, {
7
+ playbackId: param.playbackId
8
+ });
9
+ }
10
+ if (param.message) {
11
+ if (param.voiceConfig) {
12
+ param.voiceConfig.playbackId = param.playbackId;
13
+ }
14
+ else {
15
+ param.voiceConfig = { playbackId: param.playbackId };
16
+ }
17
+ await param.voice.say(param.message, param.voiceConfig);
18
+ }
19
+ };
20
+ const playTransfering = async (voice, playbackId, config) => await playOrSay({
21
+ voice,
22
+ voiceConfig: config.voiceConfig,
23
+ playbackId,
24
+ media: config.transfer?.media,
25
+ message: config.transfer?.message
26
+ });
27
+ exports.playTransfering = playTransfering;
28
+ const playBusyAndHangup = async (voice, playbackId, config) => await playOrSay({
29
+ voice,
30
+ voiceConfig: config.voiceConfig,
31
+ playbackId,
32
+ media: config.transfer?.mediaBusy,
33
+ message: config.transfer?.messageBusy
34
+ });
35
+ exports.playBusyAndHangup = playBusyAndHangup;
36
+ const playNoAnswerAndHangup = async (voice, playbackId, config) => await playOrSay({
37
+ voice,
38
+ voiceConfig: config.voiceConfig,
39
+ playbackId,
40
+ media: config.transfer?.mediaNoAnswer,
41
+ message: config.transfer?.messageNoAnswer
42
+ });
43
+ exports.playNoAnswerAndHangup = playNoAnswerAndHangup;
@@ -0,0 +1,2 @@
1
+ export * from "./cerebro";
2
+ export * from "./effects";
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ /*
18
+ * Copyright (C) 2023 by Fonoster Inc (https://fonoster.com)
19
+ * http://github.com/fonoster/rox
20
+ *
21
+ * This file is part of Rox AI
22
+ *
23
+ * Licensed under the MIT License (the "License");
24
+ * you may not use this file except in compliance with
25
+ * the License. You may obtain a copy of the License at
26
+ *
27
+ * https://opensource.org/licenses/MIT
28
+ *
29
+ * Unless required by applicable law or agreed to in writing, software
30
+ * distributed under the License is distributed on an "AS IS" BASIS,
31
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32
+ * See the License for the specific language governing permissions and
33
+ * limitations under the License.
34
+ */
35
+ __exportStar(require("./cerebro"), exports);
36
+ __exportStar(require("./effects"), exports);
@@ -0,0 +1,40 @@
1
+ import { VoiceResponse } from "@fonoster/voice";
2
+ import { VoiceRequest } from "@fonoster/voice/dist/types";
3
+ import { EventsClient } from "../events/emitter";
4
+ import { IntentsEngine } from "../intents/types";
5
+ export declare enum CerebroStatus {
6
+ SLEEP = 0,
7
+ AWAKE_ACTIVE = 1,
8
+ AWAKE_PASSIVE = 2
9
+ }
10
+ export interface CerebroConfig {
11
+ voiceRequest: VoiceRequest;
12
+ voiceResponse: VoiceResponse;
13
+ activationTimeout?: number;
14
+ activationIntentId?: string;
15
+ intentsEngine: IntentsEngine;
16
+ voiceConfig: Record<string, string>;
17
+ eventsClient: EventsClient | null;
18
+ transfer?: Transfer;
19
+ alternativeLanguageCode?: string;
20
+ }
21
+ export interface Transfer {
22
+ media?: string;
23
+ mediaNoAnswer?: string;
24
+ mediaBusy?: string;
25
+ message?: string;
26
+ messageNoAnswer?: string;
27
+ messageBusy?: string;
28
+ }
29
+ export interface EffectsManagerConfig {
30
+ eventsClient: EventsClient | null;
31
+ voice: VoiceResponse;
32
+ voiceConfig: Record<string, unknown>;
33
+ activationIntentId?: string;
34
+ playbackId: string;
35
+ transfer?: Transfer;
36
+ }
37
+ export interface Effect {
38
+ type: "say" | "hangup" | "send_data" | "transfer";
39
+ parameters: Record<string, unknown>;
40
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CerebroStatus = void 0;
4
+ var CerebroStatus;
5
+ (function (CerebroStatus) {
6
+ CerebroStatus[CerebroStatus["SLEEP"] = 0] = "SLEEP";
7
+ CerebroStatus[CerebroStatus["AWAKE_ACTIVE"] = 1] = "AWAKE_ACTIVE";
8
+ CerebroStatus[CerebroStatus["AWAKE_PASSIVE"] = 2] = "AWAKE_PASSIVE";
9
+ })(CerebroStatus || (exports.CerebroStatus = CerebroStatus = {}));
@@ -0,0 +1,7 @@
1
+ import { EventEmitter, ClientEvent } from "./types";
2
+ import WebSocket = require("ws");
3
+ export declare class EventsClient implements EventEmitter {
4
+ ws: WebSocket;
5
+ constructor(ws: WebSocket);
6
+ send(event: ClientEvent): void;
7
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventsClient = void 0;
4
+ class EventsClient {
5
+ ws;
6
+ constructor(ws) {
7
+ this.ws = ws;
8
+ }
9
+ send(event) {
10
+ this.ws.send(JSON.stringify(event));
11
+ }
12
+ }
13
+ exports.EventsClient = EventsClient;
@@ -0,0 +1,12 @@
1
+ import { EventsClient } from "./emitter";
2
+ import WebSocket = require("ws");
3
+ export declare class EventsServer {
4
+ clientConnections: Map<string, WebSocket>;
5
+ wss: WebSocket.Server;
6
+ port: number;
7
+ constructor(clientConnections: Map<string, WebSocket>, port?: number);
8
+ start(): void;
9
+ getConnection(clientId: string): EventsClient | null;
10
+ removeConnection(clientId: string): void;
11
+ }
12
+ export declare const eventsServer: EventsServer;
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.eventsServer = exports.EventsServer = void 0;
4
+ /* eslint-disable require-jsdoc */
5
+ /*
6
+ * Copyright (C) 2023 by Fonoster Inc (https://fonoster.com)
7
+ * http://github.com/fonoster/rox
8
+ *
9
+ * This file is part of Rox AI
10
+ *
11
+ * Licensed under the MIT License (the "License");
12
+ * you may not use this file except in compliance with
13
+ * the License. You may obtain a copy of the License at
14
+ *
15
+ * https://opensource.org/licenses/MIT
16
+ *
17
+ * Unless required by applicable law or agreed to in writing, software
18
+ * distributed under the License is distributed on an "AS IS" BASIS,
19
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
+ * See the License for the specific language governing permissions and
21
+ * limitations under the License.
22
+ */
23
+ const emitter_1 = require("./emitter");
24
+ const logger_1 = require("@fonoster/logger");
25
+ const WebSocket = require("ws");
26
+ const logger = (0, logger_1.getLogger)({ service: "autopilot", filePath: __filename });
27
+ // Events server
28
+ class EventsServer {
29
+ clientConnections;
30
+ wss;
31
+ port;
32
+ constructor(clientConnections, port = 3001) {
33
+ this.port = port;
34
+ this.wss = new WebSocket.Server({ port });
35
+ this.clientConnections = clientConnections;
36
+ }
37
+ start() {
38
+ this.wss.on("connection", (ws) => {
39
+ logger.verbose("received a new client connection");
40
+ ws.on("message", (data) => {
41
+ // Once we receive the first and only message from client we
42
+ // save the client in the clientConnections map
43
+ const clientId = JSON.parse(data.toString()).clientId;
44
+ this.clientConnections.set(clientId, ws);
45
+ logger.verbose("added clientId to list of connections", { clientId });
46
+ });
47
+ ws.send(JSON.stringify({
48
+ name: "connected",
49
+ payload: {}
50
+ }));
51
+ });
52
+ logger.verbose("starting events server", { port: this.port });
53
+ }
54
+ getConnection(clientId) {
55
+ const connection = this.clientConnections.get(clientId);
56
+ if (!connection) {
57
+ return null;
58
+ }
59
+ return new emitter_1.EventsClient(connection);
60
+ }
61
+ removeConnection(clientId) {
62
+ this.clientConnections.delete(clientId);
63
+ }
64
+ }
65
+ exports.EventsServer = EventsServer;
66
+ // Starting events server
67
+ exports.eventsServer = new EventsServer(new Map());
@@ -0,0 +1,20 @@
1
+ export declare enum CLIENT_EVENTS {
2
+ RECOGNIZING = "RECOGNIZING",
3
+ ANSWERED = "ANSWERED",
4
+ RECOGNIZING_FINISHED = "RECOGNIZING_FINISHED",
5
+ INTENT = "INTENT",
6
+ HANGUP = "HANGUP"
7
+ }
8
+ export interface EventEmitter {
9
+ send(payload?: ClientEvent): void;
10
+ }
11
+ export interface Intent {
12
+ icon?: string;
13
+ title: string;
14
+ description: string;
15
+ transcript: string;
16
+ }
17
+ export interface ClientEvent {
18
+ eventName: CLIENT_EVENTS;
19
+ intent?: Intent;
20
+ }
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CLIENT_EVENTS = void 0;
4
+ /*
5
+ * Copyright (C) 2023 by Fonoster Inc (https://fonoster.com)
6
+ * http://github.com/fonoster/rox
7
+ *
8
+ * This file is part of Rox AI
9
+ *
10
+ * Licensed under the MIT License (the "License");
11
+ * you may not use this file except in compliance with
12
+ * the License. You may obtain a copy of the License at
13
+ *
14
+ * https://opensource.org/licenses/MIT
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+ var CLIENT_EVENTS;
23
+ (function (CLIENT_EVENTS) {
24
+ CLIENT_EVENTS["RECOGNIZING"] = "RECOGNIZING";
25
+ CLIENT_EVENTS["ANSWERED"] = "ANSWERED";
26
+ CLIENT_EVENTS["RECOGNIZING_FINISHED"] = "RECOGNIZING_FINISHED";
27
+ CLIENT_EVENTS["INTENT"] = "INTENT";
28
+ CLIENT_EVENTS["HANGUP"] = "HANGUP";
29
+ })(CLIENT_EVENTS || (exports.CLIENT_EVENTS = CLIENT_EVENTS = {}));
@@ -0,0 +1,2 @@
1
+ import { ServerConfig } from "../types";
2
+ export declare const startFileRetentionPolicy: (config: ServerConfig) => void;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.startFileRetentionPolicy = void 0;
7
+ const task_1 = require("./task");
8
+ const logger_1 = require("@fonoster/logger");
9
+ const node_cron_1 = __importDefault(require("node-cron"));
10
+ const logger = (0, logger_1.getLogger)({ service: "autopilot", filePath: __filename });
11
+ const startFileRetentionPolicy = (config) => {
12
+ if (config.fileRetentionPolicyEnabled) {
13
+ logger.info("file retention policy enabled");
14
+ node_cron_1.default.schedule(config.fileRetentionPolicyCronExpression, () => (0, task_1.runFileRetentionPolicy)({
15
+ filesDirectory: config.fileRetentionPolicyDirectory,
16
+ maxFileAge: config.fileRetentionPolicyMaxAge,
17
+ fileExtension: config.fileRetentionPolicyExtension
18
+ }));
19
+ return;
20
+ }
21
+ logger.info("file retention policy is disabled, all files will be kept forever in the server");
22
+ };
23
+ exports.startFileRetentionPolicy = startFileRetentionPolicy;
@@ -0,0 +1 @@
1
+ export * from "./cron";