@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.
- package/LICENSE +21 -0
- package/dist/cerebro/cerebro.d.ts +26 -0
- package/dist/cerebro/cerebro.js +155 -0
- package/dist/cerebro/effects.d.ts +11 -0
- package/dist/cerebro/effects.js +91 -0
- package/dist/cerebro/helper.d.ts +5 -0
- package/dist/cerebro/helper.js +43 -0
- package/dist/cerebro/index.d.ts +2 -0
- package/dist/cerebro/index.js +36 -0
- package/dist/cerebro/types.d.ts +40 -0
- package/dist/cerebro/types.js +9 -0
- package/dist/events/emitter.d.ts +7 -0
- package/dist/events/emitter.js +13 -0
- package/dist/events/server.d.ts +12 -0
- package/dist/events/server.js +67 -0
- package/dist/events/types.d.ts +20 -0
- package/dist/events/types.js +29 -0
- package/dist/file-retention/cron.d.ts +2 -0
- package/dist/file-retention/cron.js +23 -0
- package/dist/file-retention/index.d.ts +1 -0
- package/dist/file-retention/index.js +35 -0
- package/dist/file-retention/task.d.ts +6 -0
- package/dist/file-retention/task.js +56 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +29 -0
- package/dist/intents/df_utils.d.ts +3 -0
- package/dist/intents/df_utils.js +39 -0
- package/dist/intents/dialogflow_cx.d.ts +15 -0
- package/dist/intents/dialogflow_cx.js +87 -0
- package/dist/intents/dialogflow_es.d.ts +15 -0
- package/dist/intents/dialogflow_es.js +172 -0
- package/dist/intents/engines.d.ts +3 -0
- package/dist/intents/engines.js +32 -0
- package/dist/intents/types.d.ts +26 -0
- package/dist/intents/types.js +2 -0
- package/dist/pilot.d.ts +3 -0
- package/dist/pilot.js +131 -0
- package/dist/types.d.ts +36 -0
- package/dist/types.js +2 -0
- package/dist/util.d.ts +6 -0
- package/dist/util.js +26 -0
- package/dist/voice.d.ts +3 -0
- package/dist/voice.js +154 -0
- package/package.json +41 -0
|
@@ -0,0 +1,35 @@
|
|
|
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
|
|
20
|
+
*
|
|
21
|
+
* This file is part of nodejs-voiceapp
|
|
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("./cron"), exports);
|
|
@@ -0,0 +1,56 @@
|
|
|
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.runFileRetentionPolicy = void 0;
|
|
7
|
+
/* eslint-disable no-loops/no-loops */
|
|
8
|
+
/*
|
|
9
|
+
* Copyright (C) 2023 by Fonoster Inc (https://fonoster.com)
|
|
10
|
+
* http://github.com/fonoster
|
|
11
|
+
*
|
|
12
|
+
* This file is part of nodejs-voiceapp
|
|
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 date_fns_1 = require("date-fns");
|
|
27
|
+
const logger_1 = require("@fonoster/logger");
|
|
28
|
+
const fs_1 = __importDefault(require("fs"));
|
|
29
|
+
const path_1 = __importDefault(require("path"));
|
|
30
|
+
const logger = (0, logger_1.getLogger)({ service: "autopilot", filePath: __filename });
|
|
31
|
+
const runFileRetentionPolicy = (config) => {
|
|
32
|
+
logger.verbose("running file retention policy in directory: " + config.filesDirectory);
|
|
33
|
+
fs_1.default.readdir(config.filesDirectory, (err, files) => {
|
|
34
|
+
if (err)
|
|
35
|
+
throw err;
|
|
36
|
+
const ttsFiles = files.filter((file) => path_1.default.extname(file) === config.fileExtension);
|
|
37
|
+
logger.verbose("found " + ttsFiles.length + " files to be deleted");
|
|
38
|
+
for (const file of ttsFiles) {
|
|
39
|
+
const filePath = path_1.default.join(config.filesDirectory, file);
|
|
40
|
+
fs_1.default.stat(filePath, (err, stats) => {
|
|
41
|
+
if (err)
|
|
42
|
+
throw err;
|
|
43
|
+
const diff = (0, date_fns_1.differenceInHours)(new Date(), stats.atime);
|
|
44
|
+
if (diff > config.maxFileAge) {
|
|
45
|
+
logger.verbose("file " + file + " was last accessed " + diff + " hours ago");
|
|
46
|
+
logger.verbose("deleting file " + file);
|
|
47
|
+
fs_1.default.unlink(filePath, (err) => {
|
|
48
|
+
if (err)
|
|
49
|
+
throw err;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
exports.runFileRetentionPolicy = runFileRetentionPolicy;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
/*
|
|
7
|
+
* Copyright (C) 2023 by Fonoster Inc (https://fonoster.com)
|
|
8
|
+
* http://github.com/fonoster/fonoster
|
|
9
|
+
*
|
|
10
|
+
* This file is part of Fonoster
|
|
11
|
+
*
|
|
12
|
+
* Licensed under the MIT License (the "License");
|
|
13
|
+
* you may not use this file except in compliance with
|
|
14
|
+
* the License. You may obtain a copy of the License at
|
|
15
|
+
*
|
|
16
|
+
* https://opensource.org/licenses/MIT
|
|
17
|
+
*
|
|
18
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
19
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
20
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
21
|
+
* See the License for the specific language governing permissions and
|
|
22
|
+
* limitations under the License.
|
|
23
|
+
*/
|
|
24
|
+
const pilot_1 = __importDefault(require("./pilot"));
|
|
25
|
+
(0, pilot_1.default)({
|
|
26
|
+
defaultLanguageCode: "en-US",
|
|
27
|
+
eventsServerEnabled: false,
|
|
28
|
+
port: 6445
|
|
29
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transformPayloadToEffect = exports.getRamdomValue = void 0;
|
|
4
|
+
function deserializePayload(object) {
|
|
5
|
+
const outputMessage = Array.isArray(object) ? [] : {};
|
|
6
|
+
Object.entries(object).forEach(([key, value]) => {
|
|
7
|
+
if (value.kind == "structValue") {
|
|
8
|
+
outputMessage[key] = deserializePayload(value.structValue.fields);
|
|
9
|
+
}
|
|
10
|
+
else if (value.kind == "listValue") {
|
|
11
|
+
outputMessage[key] = deserializePayload(value.listValue.values);
|
|
12
|
+
}
|
|
13
|
+
else if (value.kind == "stringValue") {
|
|
14
|
+
outputMessage[key] = value.stringValue;
|
|
15
|
+
}
|
|
16
|
+
else if (value.kind == "boolValue") {
|
|
17
|
+
outputMessage[key] = value.boolValue;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
outputMessage[key] = value;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
return outputMessage;
|
|
24
|
+
}
|
|
25
|
+
function getRamdomValue(values) {
|
|
26
|
+
return values[Math.floor(Math.random() * values.length)];
|
|
27
|
+
}
|
|
28
|
+
exports.getRamdomValue = getRamdomValue;
|
|
29
|
+
function transformPayloadToEffect(payload) {
|
|
30
|
+
const o = deserializePayload(payload.fields);
|
|
31
|
+
const parameters = o.effect === "say"
|
|
32
|
+
? { response: getRamdomValue(o.parameters.responses) }
|
|
33
|
+
: o.parameters;
|
|
34
|
+
return {
|
|
35
|
+
type: o.effect,
|
|
36
|
+
parameters
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
exports.transformPayloadToEffect = transformPayloadToEffect;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { DialogFlowCXConfig, IntentsEngine, Intent } from "./types";
|
|
2
|
+
import { SessionsClient } from "@google-cloud/dialogflow-cx";
|
|
3
|
+
export default class DialogFlowCX implements IntentsEngine {
|
|
4
|
+
sessionClient: SessionsClient;
|
|
5
|
+
sessionPath: any;
|
|
6
|
+
config: DialogFlowCXConfig;
|
|
7
|
+
projectId: string;
|
|
8
|
+
location: string;
|
|
9
|
+
agent: string;
|
|
10
|
+
sessionId: string;
|
|
11
|
+
constructor(config: DialogFlowCXConfig);
|
|
12
|
+
setProjectId(id: string): void;
|
|
13
|
+
findIntent(txt: string): Promise<Intent>;
|
|
14
|
+
private getEffects;
|
|
15
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
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
|
+
const df_utils_1 = require("./df_utils");
|
|
7
|
+
const logger_1 = require("@fonoster/logger");
|
|
8
|
+
const dialogflow_cx_1 = __importDefault(require("@google-cloud/dialogflow-cx"));
|
|
9
|
+
const uuid = require("uuid");
|
|
10
|
+
const logger = (0, logger_1.getLogger)({ service: "autopilot", filePath: __filename });
|
|
11
|
+
class DialogFlowCX {
|
|
12
|
+
sessionClient;
|
|
13
|
+
sessionPath;
|
|
14
|
+
config;
|
|
15
|
+
projectId;
|
|
16
|
+
location;
|
|
17
|
+
agent;
|
|
18
|
+
sessionId;
|
|
19
|
+
constructor(config) {
|
|
20
|
+
const sessionId = uuid.v4();
|
|
21
|
+
this.projectId = config.projectId;
|
|
22
|
+
this.location = config.location;
|
|
23
|
+
this.config = config;
|
|
24
|
+
this.sessionId = sessionId;
|
|
25
|
+
this.agent = config.agent;
|
|
26
|
+
// Create a new session
|
|
27
|
+
this.sessionClient = new dialogflow_cx_1.default.SessionsClient({
|
|
28
|
+
apiEndpoint: `${config.location}-dialogflow.googleapis.com`,
|
|
29
|
+
credentials: config.credentials
|
|
30
|
+
});
|
|
31
|
+
logger.verbose("created new dialogflow/cx session", {
|
|
32
|
+
projectId: this.projectId,
|
|
33
|
+
sessionId: this.sessionId
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
setProjectId(id) {
|
|
37
|
+
this.projectId = id;
|
|
38
|
+
}
|
|
39
|
+
async findIntent(txt) {
|
|
40
|
+
const sessionPath = this.sessionClient.projectLocationAgentSessionPath(this.projectId, this.location, this.agent, this.sessionId);
|
|
41
|
+
const request = {
|
|
42
|
+
session: sessionPath,
|
|
43
|
+
queryInput: {
|
|
44
|
+
text: {
|
|
45
|
+
text: txt
|
|
46
|
+
},
|
|
47
|
+
languageCode: this.config.languageCode
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const responses = await this.sessionClient.detectIntent(request);
|
|
51
|
+
logger.silly("got speech from api", { text: JSON.stringify(responses[0]) });
|
|
52
|
+
if (!responses ||
|
|
53
|
+
!responses[0].queryResult ||
|
|
54
|
+
!responses[0].queryResult.responseMessages) {
|
|
55
|
+
throw new Error("got unexpect null intent");
|
|
56
|
+
}
|
|
57
|
+
const effects = this.getEffects(responses[0].queryResult.responseMessages);
|
|
58
|
+
const ref = responses[0].queryResult.intent
|
|
59
|
+
? responses[0].queryResult.intent.displayName || "unknown"
|
|
60
|
+
: "unknown";
|
|
61
|
+
return {
|
|
62
|
+
ref,
|
|
63
|
+
effects,
|
|
64
|
+
confidence: responses[0].queryResult.intentDetectionConfidence || 0,
|
|
65
|
+
allRequiredParamsPresent: responses[0].queryResult.text ? true : false
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
getEffects(responseMessages) {
|
|
69
|
+
const effects = [];
|
|
70
|
+
for (const r of responseMessages) {
|
|
71
|
+
if (r.message === "text") {
|
|
72
|
+
effects.push({
|
|
73
|
+
type: "say",
|
|
74
|
+
parameters: {
|
|
75
|
+
response: r.text.text[0]
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
else if (r.payload) {
|
|
81
|
+
effects.push((0, df_utils_1.transformPayloadToEffect)(r.payload));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return effects;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
exports.default = DialogFlowCX;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as dialogflow from "@google-cloud/dialogflow";
|
|
2
|
+
import { IntentsEngine, Intent, DialogFlowESConfig } from "./types";
|
|
3
|
+
export default class DialogFlow implements IntentsEngine {
|
|
4
|
+
sessionClient: dialogflow.v2beta1.SessionsClient;
|
|
5
|
+
sessionPath: any;
|
|
6
|
+
config: DialogFlowESConfig;
|
|
7
|
+
sessionId: string;
|
|
8
|
+
projectId: string;
|
|
9
|
+
constructor(config: DialogFlowESConfig);
|
|
10
|
+
setProjectId(projectId: string): void;
|
|
11
|
+
findIntentWithEvent(name: string, payload?: Record<string, unknown>): Promise<Intent>;
|
|
12
|
+
findIntent(txt: string, payload?: Record<string, unknown>): Promise<Intent>;
|
|
13
|
+
private detect;
|
|
14
|
+
private getEffects;
|
|
15
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
/* eslint-disable no-loops/no-loops */
|
|
27
|
+
/* eslint-disable require-jsdoc */
|
|
28
|
+
/*
|
|
29
|
+
* Copyright (C) 2023 by Fonoster Inc (https://fonoster.com)
|
|
30
|
+
* http://github.com/fonoster/rox
|
|
31
|
+
*
|
|
32
|
+
* This file is part of Rox AI
|
|
33
|
+
*
|
|
34
|
+
* Licensed under the MIT License (the "License");
|
|
35
|
+
* you may not use this file except in compliance with
|
|
36
|
+
* the License. You may obtain a copy of the License at
|
|
37
|
+
*
|
|
38
|
+
* https://opensource.org/licenses/MIT
|
|
39
|
+
*
|
|
40
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
41
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
42
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
43
|
+
* See the License for the specific language governing permissions and
|
|
44
|
+
* limitations under the License.
|
|
45
|
+
*/
|
|
46
|
+
const dialogflow = __importStar(require("@google-cloud/dialogflow"));
|
|
47
|
+
const df_utils_1 = require("./df_utils");
|
|
48
|
+
const pb_util_1 = require("pb-util");
|
|
49
|
+
const logger_1 = require("@fonoster/logger");
|
|
50
|
+
const uuid = require("uuid");
|
|
51
|
+
const logger = (0, logger_1.getLogger)({ service: "autopilot", filePath: __filename });
|
|
52
|
+
class DialogFlow {
|
|
53
|
+
sessionClient;
|
|
54
|
+
sessionPath;
|
|
55
|
+
config;
|
|
56
|
+
sessionId;
|
|
57
|
+
projectId;
|
|
58
|
+
constructor(config) {
|
|
59
|
+
this.sessionId = uuid.v4();
|
|
60
|
+
this.config = config;
|
|
61
|
+
this.projectId = config.projectId;
|
|
62
|
+
// Create a new session
|
|
63
|
+
this.sessionClient = new dialogflow.v2beta1.SessionsClient({
|
|
64
|
+
credentials: config.credentials
|
|
65
|
+
});
|
|
66
|
+
logger.verbose("created new dialogflow/es session", {
|
|
67
|
+
projectId: this.projectId,
|
|
68
|
+
sessionId: this.sessionId
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
setProjectId(projectId) {
|
|
72
|
+
this.projectId = projectId;
|
|
73
|
+
}
|
|
74
|
+
async findIntentWithEvent(name, payload) {
|
|
75
|
+
const request = {
|
|
76
|
+
queryInput: {
|
|
77
|
+
event: {
|
|
78
|
+
name: name.toUpperCase(),
|
|
79
|
+
languageCode: this.config.languageCode
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
return this.detect(request, payload);
|
|
84
|
+
}
|
|
85
|
+
async findIntent(txt, payload) {
|
|
86
|
+
const request = {
|
|
87
|
+
queryParams: {},
|
|
88
|
+
queryInput: {
|
|
89
|
+
text: {
|
|
90
|
+
text: txt,
|
|
91
|
+
languageCode: this.config.languageCode
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
return this.detect(request, payload);
|
|
96
|
+
}
|
|
97
|
+
async detect(request, payload) {
|
|
98
|
+
const sessionPath = this.sessionClient.projectAgentSessionPath(this.projectId, this.sessionId);
|
|
99
|
+
request.session = sessionPath;
|
|
100
|
+
if (payload) {
|
|
101
|
+
request.queryParams = {
|
|
102
|
+
payload: pb_util_1.struct.encode(payload)
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
const responses = await this.sessionClient.detectIntent(request);
|
|
106
|
+
logger.silly("got speech from api", { text: JSON.stringify(responses[0]) });
|
|
107
|
+
if (!responses ||
|
|
108
|
+
!responses[0].queryResult ||
|
|
109
|
+
!responses[0].queryResult.intent) {
|
|
110
|
+
throw new Error("got unexpect null intent");
|
|
111
|
+
}
|
|
112
|
+
let effects = [];
|
|
113
|
+
if (responses[0].queryResult.fulfillmentMessages) {
|
|
114
|
+
const messages = responses[0].queryResult.fulfillmentMessages.filter((f) => f.platform === this.config.platform);
|
|
115
|
+
effects = this.getEffects(messages);
|
|
116
|
+
}
|
|
117
|
+
else if (responses[0].queryResult.fulfillmentText) {
|
|
118
|
+
effects = [
|
|
119
|
+
{
|
|
120
|
+
type: "say",
|
|
121
|
+
parameters: {
|
|
122
|
+
response: responses[0].queryResult.fulfillmentText
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
];
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
ref: responses[0].queryResult.intent.displayName || "unknown",
|
|
129
|
+
effects,
|
|
130
|
+
confidence: responses[0].queryResult.intentDetectionConfidence || 0,
|
|
131
|
+
allRequiredParamsPresent: responses[0].queryResult
|
|
132
|
+
.allRequiredParamsPresent
|
|
133
|
+
? true
|
|
134
|
+
: false
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
getEffects(fulfillmentMessages) {
|
|
138
|
+
const effects = [];
|
|
139
|
+
for (const f of fulfillmentMessages) {
|
|
140
|
+
if (f.payload) {
|
|
141
|
+
effects.push((0, df_utils_1.transformPayloadToEffect)(f.payload));
|
|
142
|
+
}
|
|
143
|
+
else if (f.telephonySynthesizeSpeech) {
|
|
144
|
+
effects.push({
|
|
145
|
+
type: "say",
|
|
146
|
+
parameters: {
|
|
147
|
+
response: f.telephonySynthesizeSpeech.text ||
|
|
148
|
+
f.telephonySynthesizeSpeech.ssml
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
else if (f.telephonyTransferCall) {
|
|
153
|
+
effects.push({
|
|
154
|
+
type: "transfer",
|
|
155
|
+
parameters: {
|
|
156
|
+
destination: f.telephonyTransferCall.phoneNumber
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
else if (f.text) {
|
|
161
|
+
effects.push({
|
|
162
|
+
type: "say",
|
|
163
|
+
parameters: {
|
|
164
|
+
response: f.text.text[0]
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return effects;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
exports.default = DialogFlow;
|
|
@@ -0,0 +1,32 @@
|
|
|
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.getIntentsEngine = void 0;
|
|
7
|
+
const dialogflow_cx_1 = __importDefault(require("./dialogflow_cx"));
|
|
8
|
+
const dialogflow_es_1 = __importDefault(require("./dialogflow_es"));
|
|
9
|
+
function getIntentsEngine(app) {
|
|
10
|
+
return function getEngine(credentials) {
|
|
11
|
+
const platform = app.intentsEngineConfig.emulateTelephonyPlatform
|
|
12
|
+
? "TELEPHONY"
|
|
13
|
+
: "PLATFORM_UNSPECIFIED";
|
|
14
|
+
if ("location" in app.intentsEngineConfig) {
|
|
15
|
+
return new dialogflow_cx_1.default({
|
|
16
|
+
credentials,
|
|
17
|
+
projectId: app.intentsEngineConfig.projectId,
|
|
18
|
+
agent: app.intentsEngineConfig.agent,
|
|
19
|
+
location: app.intentsEngineConfig.location,
|
|
20
|
+
platform,
|
|
21
|
+
languageCode: "en-US"
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return new dialogflow_es_1.default({
|
|
25
|
+
credentials,
|
|
26
|
+
projectId: app.intentsEngineConfig.projectId,
|
|
27
|
+
platform,
|
|
28
|
+
languageCode: "en-US"
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
exports.getIntentsEngine = getIntentsEngine;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Effect } from "../cerebro/types";
|
|
2
|
+
export interface Intent {
|
|
3
|
+
ref: string;
|
|
4
|
+
effects: Effect[];
|
|
5
|
+
confidence: number;
|
|
6
|
+
allRequiredParamsPresent: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface IntentsEngine {
|
|
9
|
+
setProjectId: (id: string) => void;
|
|
10
|
+
findIntent: (text: string, payload?: Record<string, unknown>) => Promise<Intent>;
|
|
11
|
+
findIntentWithEvent?: (name: string, payload?: Record<string, unknown>) => Promise<Intent>;
|
|
12
|
+
}
|
|
13
|
+
export interface DialogFlowESConfig {
|
|
14
|
+
projectId: string;
|
|
15
|
+
languageCode: string;
|
|
16
|
+
platform: string;
|
|
17
|
+
credentials: Record<string, string>;
|
|
18
|
+
}
|
|
19
|
+
export interface DialogFlowCXConfig {
|
|
20
|
+
projectId: string;
|
|
21
|
+
languageCode: string;
|
|
22
|
+
location: string;
|
|
23
|
+
agent: string;
|
|
24
|
+
platform: string;
|
|
25
|
+
credentials: Record<string, string>;
|
|
26
|
+
}
|
package/dist/pilot.d.ts
ADDED
package/dist/pilot.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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 types_1 = require("./events/types");
|
|
27
|
+
const voice_1 = require("@fonoster/voice");
|
|
28
|
+
const cerebro_1 = require("./cerebro");
|
|
29
|
+
const server_1 = require("./events/server");
|
|
30
|
+
const nanoid_1 = require("nanoid");
|
|
31
|
+
const engines_1 = require("./intents/engines");
|
|
32
|
+
const util_1 = require("./util");
|
|
33
|
+
const logger_1 = require("@fonoster/logger");
|
|
34
|
+
const googletts_1 = __importDefault(require("@fonoster/googletts"));
|
|
35
|
+
const apps_1 = __importDefault(require("@fonoster/apps"));
|
|
36
|
+
const secrets_1 = __importDefault(require("@fonoster/secrets"));
|
|
37
|
+
const googleasr_1 = __importDefault(require("@fonoster/googleasr"));
|
|
38
|
+
const logger = (0, logger_1.getLogger)({ service: "autopilot", filePath: __filename });
|
|
39
|
+
function pilot(config) {
|
|
40
|
+
logger.info("starting autopilot");
|
|
41
|
+
const voiceServer = new voice_1.VoiceServer({
|
|
42
|
+
port: config.port
|
|
43
|
+
});
|
|
44
|
+
if (config.eventsServerEnabled)
|
|
45
|
+
server_1.eventsServer.start();
|
|
46
|
+
logger.verbose("events server enabled = " + config.eventsServerEnabled);
|
|
47
|
+
voiceServer.listen(async (voiceRequest, voiceResponse) => {
|
|
48
|
+
logger.verbose(`new request [sessionId: ${voiceRequest.sessionId}]`, {
|
|
49
|
+
voiceRequest
|
|
50
|
+
});
|
|
51
|
+
try {
|
|
52
|
+
if (!voiceRequest.appRef)
|
|
53
|
+
throw new Error("invalid voice request: missing appRef");
|
|
54
|
+
// If set, we overwrite the configuration with the values obtain from the webhook
|
|
55
|
+
const serviceCredentials = {
|
|
56
|
+
accessKeyId: voiceRequest.accessKeyId,
|
|
57
|
+
accessKeySecret: voiceRequest.sessionToken
|
|
58
|
+
};
|
|
59
|
+
const apps = new apps_1.default(serviceCredentials);
|
|
60
|
+
const secrets = new secrets_1.default(serviceCredentials);
|
|
61
|
+
const app = await apps.getApp(voiceRequest.appRef);
|
|
62
|
+
logger.verbose(`requested app [ref: ${app.ref}]`, { app });
|
|
63
|
+
const ieSecret = await secrets.getSecret(app.intentsEngineConfig.secretName);
|
|
64
|
+
const intentsEngine = (0, engines_1.getIntentsEngine)(app)(JSON.parse(ieSecret.secret));
|
|
65
|
+
intentsEngine?.setProjectId(app.intentsEngineConfig.projectId);
|
|
66
|
+
const voiceConfig = {
|
|
67
|
+
name: app.speechConfig.voice,
|
|
68
|
+
playbackId: (0, nanoid_1.nanoid)()
|
|
69
|
+
};
|
|
70
|
+
const speechSecret = await secrets.getSecret(app.speechConfig.secretName);
|
|
71
|
+
const speechCredentials = {
|
|
72
|
+
private_key: JSON.parse(speechSecret.secret).private_key,
|
|
73
|
+
client_email: JSON.parse(speechSecret.secret).client_email
|
|
74
|
+
};
|
|
75
|
+
voiceResponse.use(new googletts_1.default({
|
|
76
|
+
credentials: speechCredentials,
|
|
77
|
+
languageCode: config.defaultLanguageCode,
|
|
78
|
+
path: config.fileRetentionPolicyDirectory
|
|
79
|
+
}));
|
|
80
|
+
voiceResponse.use(new googleasr_1.default({
|
|
81
|
+
credentials: speechCredentials,
|
|
82
|
+
languageCode: config.defaultLanguageCode
|
|
83
|
+
}));
|
|
84
|
+
await voiceResponse.answer();
|
|
85
|
+
const eventsClient = app.enableEvents && config.eventsServerEnabled
|
|
86
|
+
? server_1.eventsServer.getConnection(voiceRequest.callerNumber)
|
|
87
|
+
: null;
|
|
88
|
+
(0, util_1.sendClientEvent)(eventsClient, {
|
|
89
|
+
eventName: types_1.CLIENT_EVENTS.ANSWERED
|
|
90
|
+
});
|
|
91
|
+
if (app.initialDtmf)
|
|
92
|
+
await voiceResponse.dtmf({ dtmf: app.initialDtmf });
|
|
93
|
+
if (app.intentsEngineConfig.welcomeIntentId &&
|
|
94
|
+
intentsEngine.findIntentWithEvent) {
|
|
95
|
+
const response = await intentsEngine.findIntentWithEvent(app.intentsEngineConfig.welcomeIntentId, {
|
|
96
|
+
telephony: {
|
|
97
|
+
caller_id: voiceRequest.callerNumber
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
if (response.effects.length > 0) {
|
|
101
|
+
await voiceResponse.say(response.effects[0].parameters["response"], voiceConfig);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
logger.warn(`no effects found for welcome intent: trigger '${app.intentsEngineConfig.welcomeIntentId}'`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const cerebro = new cerebro_1.Cerebro({
|
|
108
|
+
voiceRequest,
|
|
109
|
+
voiceResponse,
|
|
110
|
+
eventsClient,
|
|
111
|
+
voiceConfig,
|
|
112
|
+
intentsEngine,
|
|
113
|
+
activationIntentId: app.activationIntentId,
|
|
114
|
+
activationTimeout: app.activationTimeout,
|
|
115
|
+
transfer: app.transferConfig,
|
|
116
|
+
alternativeLanguageCode: app.speechConfig.languageCode
|
|
117
|
+
});
|
|
118
|
+
// Open for bussiness
|
|
119
|
+
await cerebro.wake();
|
|
120
|
+
}
|
|
121
|
+
catch (e) {
|
|
122
|
+
(0, logger_1.ulogger)({
|
|
123
|
+
accessKeyId: voiceRequest.accessKeyId,
|
|
124
|
+
eventType: logger_1.ULogType.APP,
|
|
125
|
+
level: "error",
|
|
126
|
+
message: e.message
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
exports.default = pilot;
|