ask-cli-x
Version:
Alexa Skills Kit (ASK) Command Line Interfaces
167 lines (166 loc) • 7.28 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DialogController = void 0;
const chalk_1 = __importDefault(require("chalk"));
const fs = __importStar(require("fs-extra"));
const stringUtils = __importStar(require("../../utils/string-utils"));
const messenger_1 = __importDefault(require("../../view/messenger"));
const responseParser = __importStar(require("./simulation-response-parser"));
const skill_simulation_controller_1 = require("../skill-simulation-controller");
const RECORD_FORMAT = 'Please use the format: ".record <fileName>" or ".record <fileName> --append-quit"';
class DialogController extends skill_simulation_controller_1.SkillSimulationController {
/**
* Constructor for DialogModeController.
* @param {Object} configuration | config object includes information such as skillId, locale, profile, stage.
*/
constructor(configuration) {
super(configuration);
this.utteranceCache = [];
this.newSession = configuration.newSession === false ? configuration.newSession : true;
}
/**
* Evaluate individual utterance input by the User/replay_file.
* @param {String} input Utterance by the user sent to Alexa.
* @param {Object} replView Dialog command's repl view.
* @param {Function} replCallback
*/
async evaluateUtterance(input, replView) {
var _a;
replView.startProgressSpinner("Sending simulation request to Alexa...");
try {
const startResponse = await this.startSkillSimulation(input.trim());
replView.updateProgressSpinner("Waiting for the simulation response...");
const simulationId = (_a = startResponse === null || startResponse === void 0 ? void 0 : startResponse.body) === null || _a === void 0 ? void 0 : _a.id;
const response = await this.getSkillSimulationResult(simulationId);
this.newSession = false;
replView.terminateProgressSpinner();
if (responseParser.shouldEndSession(response.body)) {
messenger_1.default.getInstance().info("Session ended");
this.clearSession();
}
const captions = responseParser.getCaption(response.body);
captions.forEach((caption) => {
messenger_1.default.getInstance().info(chalk_1.default.yellow.bold("Alexa > ") + caption);
});
}
catch (startErr) {
replView.terminateProgressSpinner();
messenger_1.default.getInstance().error(startErr.message ? startErr.message : startErr);
throw startErr;
}
}
/**
* Registers special commands with the REPL server.
* @param {Object} dialogReplView dialog command's repl view.
* @param {Function} callback
*/
async setupSpecialCommands(dialogReplView) {
return new Promise((resolve, reject) => {
dialogReplView.registerRecordCommand((recordArgs) => {
const recordArgsList = recordArgs.trim().split(" ");
if (!stringUtils.isNonBlankString(recordArgs) || recordArgsList.length > 2) {
messenger_1.default.getInstance().warn(`Incorrect format. ${RECORD_FORMAT}`);
return reject();
}
const { filePath, shouldAppendQuit } = this._validateRecordCommandInput(recordArgsList);
const utteranceCacheCopy = [...this.utteranceCache];
if (shouldAppendQuit) {
utteranceCacheCopy.push(".quit");
}
if (filePath) {
try {
this.createReplayFile(filePath, utteranceCacheCopy);
messenger_1.default.getInstance().info(`Created replay file at ${filePath}` + `${shouldAppendQuit ? ' (appended ".quit" to list of utterances).' : ""}`);
}
catch (replayFileCreationError) {
return reject(replayFileCreationError);
}
}
resolve();
});
const self = this;
dialogReplView.registerQuitCommand(() => {
self.skillIOInstance.save();
});
});
}
/**
* Validate record command arguments.
* @param {Array} recordArgsList
* @param {String} recordCommandFormat
*/
_validateRecordCommandInput(recordArgsList) {
const filePath = recordArgsList[0];
const appendQuitArgument = recordArgsList[1];
let shouldAppendQuit = false;
if (stringUtils.isNonBlankString(appendQuitArgument)) {
if (appendQuitArgument !== "--append-quit") {
messenger_1.default.getInstance().warn(`Unable to validate arguments: "${appendQuitArgument}". ${RECORD_FORMAT}`);
return {};
}
shouldAppendQuit = true;
}
return {
filePath,
shouldAppendQuit,
};
}
/**
* Start skill simulation by calling SMAPI POST skill simulation endpoint.
* @param {String} utterance text utterance to simulate against.
*/
async startSkillSimulation(utterance) {
const response = await super.startSkillSimulation(utterance, this.newSession);
this.utteranceCache.push(utterance);
return response;
}
/**
* Clears dialog session by resetting to a new session and clearing caches.
*/
clearSession() {
this.newSession = true;
this.utteranceCache = [];
}
/**
* Function to create replay file.
* @param {String} filename name of file to save replay JSON.
*/
createReplayFile(filename, utterances) {
if (stringUtils.isNonBlankString(filename)) {
const content = {
skillId: this.skillId,
locale: this.locale,
type: "text",
userInput: utterances,
};
fs.outputJSONSync(filename, content);
}
}
}
exports.DialogController = DialogController;