voxa-cli
Version:
The Voxa CLI tools
213 lines • 8.64 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/*
* Copyright (c) 2018 Rain Agency <contact@rain.agency>
* Author: Rain Agency <contact@rain.agency>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* tslint:disable:no-empty no-submodule-imports */
const lodash_1 = __importDefault(require("lodash"));
const path_1 = __importDefault(require("path"));
const v5_1 = __importDefault(require("uuid/v5"));
const Schema_1 = require("./Schema");
const NAMESPACE = "alexa";
const AVAILABLE_LOCALES = [
"en-US",
"en-GB",
"en-CA",
"en-AU",
"en-IN",
"de-DE",
"ja-JP",
"es-ES",
"es-MX",
"es-US",
"fr-FR",
"fr-CA",
"it-IT",
"pt-BR"
];
class AlexaSchema extends Schema_1.Schema {
constructor(voxaSheets, interactionOptions) {
super(NAMESPACE, AVAILABLE_LOCALES, voxaSheets, interactionOptions);
this.environment = "staging";
}
validate() { }
build(locale, environment) {
this.buildLanguageModel(locale, environment);
this.buildPublishing(environment);
}
buildPublishing(environment) {
const manifest = this.mergeManifest(environment);
lodash_1.default.set(manifest, "manifestVersion", "1.0");
this.fileContent.push({
path: path_1.default.join(this.interactionOptions.rootPath, this.interactionOptions.speechPath, this.NAMESPACE, `${lodash_1.default.kebabCase(environment)}-manifest.json`),
content: { manifest }
});
}
contentLanguageModel(locale, environment) {
const invocation = lodash_1.default.find(this.invocations, { locale, environment });
const invocationName = lodash_1.default.get(invocation, "name", "Skill with no name");
const intents = this.getIntentsDefinition(locale, environment);
const types = this.getSlotsByIntentsDefinition(locale, environment).map(rawSlot => ({
name: rawSlot.name,
values: rawSlot.values.map(value => ({ name: value }))
}));
return {
interactionModel: {
languageModel: { invocationName, intents, types },
dialog: this.getDialogForLocaleAndEnvironment(locale, environment),
prompts: this.getPromptsForLocaleAndEnvironment(locale, environment)
}
};
}
buildLanguageModel(locale, environment) {
this.fileContent.push({
path: path_1.default.join(this.interactionOptions.rootPath, this.interactionOptions.speechPath, this.NAMESPACE, locale, `${lodash_1.default.kebabCase(environment)}-interaction.json`),
content: this.contentLanguageModel(locale, environment)
});
const canFulfillIntents = lodash_1.default.chain(this.intentsByPlatformAndEnvironments(locale, environment))
.filter("canFulfillIntent")
.map("name")
.value();
this.fileContent.push({
path: path_1.default.join(this.interactionOptions.rootPath, this.interactionOptions.contentPath, `${lodash_1.default.kebabCase(environment)}-canfulfill-intents.json`),
content: canFulfillIntents
});
}
getDialogForLocaleAndEnvironment(locale, environment) {
const dialog = {
intents: this.generateDialogModel(this.intentsByPlatformAndEnvironments(locale, environment)),
delegationStrategy: "SKILL_RESPONSE"
};
if (!dialog.intents.length) {
return;
}
return dialog;
}
getPromptsForLocaleAndEnvironment(locale, environment) {
const prompts = this.generatePrompts(this.intentsByPlatformAndEnvironments(locale, environment));
if (!prompts.length) {
return;
}
return prompts;
}
generateDialogModel(intents) {
return lodash_1.default(intents)
.filter(intentOrSlotRequireDialog)
.map((intent) => {
const prompts = {};
if (intent.confirmations.length > 0) {
prompts.confirmation = getPromptId("Confirmation", "Intent", intent.confirmations);
}
return {
name: intent.name,
delegationStrategy: intent.delegationStrategy,
confirmationRequired: intent.confirmationRequired,
slots: this.generateDialogSlotModel(intent.slotsDefinition),
prompts
};
})
.value();
}
generateDialogSlotModel(slots) {
return lodash_1.default(slots)
.map((slot) => {
const prompts = {};
if (slot.prompts.elicitation.length > 0) {
prompts.elicitation = getPromptId("Elicitation", "Slot", slot.prompts.elicitation);
}
if (slot.prompts.confirmation.length > 0) {
prompts.confirmation = getPromptId("Confirmation", "Slot", slot.prompts.confirmation);
}
return {
name: slot.name.replace("{", "").replace("}", ""),
type: slot.type,
elicitationRequired: slot.requiresElicitation,
confirmationRequired: slot.requiresConfirmation,
prompts
};
})
.value();
}
generatePrompts(intents) {
const intentPrompts = lodash_1.default(intents)
.filter(intentHasPrompts)
.map((intent) => getPromptsObject("Confirmation", "Intent", intent.confirmations))
.value();
const slotPrompts = this.generateSlotPrompts(intents);
return lodash_1.default.concat(intentPrompts, slotPrompts);
}
generateSlotPrompts(intents) {
return lodash_1.default(intents)
.map("slotsDefinition")
.flatten()
.filter(slotHasPrompts)
.map((slot) => {
const prompts = [];
if (slot.prompts.confirmation.length > 0) {
prompts.push(getPromptsObject("Confirmation", "Slot", slot.prompts.confirmation));
}
if (slot.prompts.elicitation.length > 0) {
prompts.push(getPromptsObject("Elicitation", "Slot", slot.prompts.elicitation));
}
return prompts;
})
.flatten()
.value();
}
}
exports.AlexaSchema = AlexaSchema;
function hashObj(obj) {
return v5_1.default(JSON.stringify(obj), v5_1.default.DNS);
}
function intentOrSlotRequireDialog(intent) {
if (intent.confirmationRequired || intent.delegationStrategy) {
return true;
}
const slotRequiresDialog = lodash_1.default(intent.slotsDefinition)
.map(slot => slot.requiresElicitation || slot.requiresConfirmation)
.some();
return slotRequiresDialog;
}
function slotHasPrompts(slot) {
return slot.prompts.confirmation.length > 0 || slot.prompts.elicitation.length > 0;
}
function intentHasPrompts(intent) {
return intent.confirmations.length > 0;
}
function getPromptsObject(dialogType, objectType, data) {
return {
id: getPromptId(dialogType, objectType, data),
variations: formatVariations(data)
};
}
function getPromptId(dialogType, objectType, data) {
return `${dialogType}.${objectType}.${hashObj(data)}`;
}
function formatVariations(variations) {
return lodash_1.default.map(variations, (variation) => ({
type: "PlainText",
value: variation
}));
}
//# sourceMappingURL=AlexaSchema.js.map