UNPKG

ask-cli-x

Version:

Alexa Skills Kit (ASK) Command Line Interfaces

159 lines (158 loc) 6.87 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.alexaSay = exports.createView = void 0; const chalk_1 = __importDefault(require("chalk")); const inquirer_1 = __importDefault(require("inquirer")); const inputs_1 = require("../model/dialog/inputs"); const UserSayTitle = chalk_1.default.yellowBright("User >"); const AlexaSayTitle = chalk_1.default.bold.cyan("Alexa > "); const createView = (messenger, verbose) => { const requestUserUtteranceOrCommand = async () => { const userInput = (await inquirer_1.default.prompt([ { type: "input", name: "utterance", message: UserSayTitle, prefix: "", }, ])).utterance; return utteranceOrCommand(userInput); }; const commandOrElse = (input, els) => { if (input.startsWith(".")) { return { command: (0, inputs_1.inputToCommand)(input) }; } return els; }; const utteranceOrCommand = (input) => { return commandOrElse(input, { utterance: input }); }; const correctionOrCommand = (input) => { return commandOrElse(input, { correction: input }); }; const outputInfo = (info) => messenger.info(info); const outputError = (error) => messenger.error(error); const outputWarning = (warning) => messenger.warn(warning); const outputAcdl = (acdlCell) => { outputInfo(chalk_1.default.grey(` [acdl] ${acdlCell.acdl}`)); if (verbose) { outputInfo(chalk_1.default.grey(` [info] ${acdlCell.explanation}`)); } }; const outputAlexaResponse = (response) => outputInfo(alexaSay(response)); const outputPredictedAcdl = (predictedAcdl) => { predictedAcdl.map((acdl, i) => `${i === 0 ? " Prediction: " : " "}${acdl}`).forEach(outputInfo); }; const outputResponsesAndAcdlLinesGroups = (acdlResponseGroups) => { acdlResponseGroups.forEach(({ responseLine, acdlLines }) => { responseLine && outputAlexaResponse(responseLine); acdlLines && acdlLines.forEach(outputAcdl); }); }; const requestUserAcdlResponseAcceptance = async (acdlResponseGroups) => { acdlResponseGroups.before.forEach(outputAcdl); outputResponsesAndAcdlLinesGroups(acdlResponseGroups.groups); return (await inquirer_1.default.prompt([ { type: "confirm", name: "judge", default: true, message: "Do you accept this response [Y/n]: ", prefix: chalk_1.default.green(" ?"), }, ])).judge; }; const outputAlexaResponses = async (responses) => { responses.forEach((response) => outputAlexaResponse(response)); }; const requestSaveOnEgress = async () => { return (await inquirer_1.default.prompt([ { type: "confirm", name: "judge", default: true, message: "Delegate changes from Alexa Conversation to Skill. Do you want to save the interaction so far [Y/n]: ", prefix: chalk_1.default.green(" ?"), }, ])).judge; }; const outputCorrectionModeHeader = () => outputInfo("Enter correction mode: "); const outputDisabledCorrectionMessage = () => outputInfo(chalk_1.default.grey(` For the delegation into the conversations, service response is defined in custom endpoint and correction is not allowed.`)); const outputViewSeparator = () => outputInfo(" ----------------------------------"); const requestUserAcceptsPrediction = async () => (await inquirer_1.default.prompt([ { type: "confirm", name: "judge", default: true, message: "Do you accept this prediction [Y/n]: ", prefix: chalk_1.default.green(" ?"), }, ])).judge; const requestUserCorrectionAcdl = async () => { const typeCorrection = (await inquirer_1.default.prompt([ { type: "input", name: "correction1", message: "Correction for the type declaration (press Enter if no change): ", prefix: chalk_1.default.yellow(" >"), }, ])).correction1; const contentCorrection = (await inquirer_1.default.prompt([ { type: "input", name: "correction2", message: "Correction for the prediction (use .endTurn to exit correction mode): ", prefix: chalk_1.default.yellow(" >"), }, ])).correction2; return { type: typeCorrection ? { correction: typeCorrection.trim() } : undefined, content: correctionOrCommand(contentCorrection.trim()) }; }; return { requestUserUtteranceOrCommand, outputCommandResult: (result) => messenger.info(chalk_1.default.grey(` ${result}`)), outputError, outputWarning, outputEmpty: () => outputInfo(""), outputAlexaResponses, requestSaveOnEgress, outputCorrectionModeHeader, requestUserAcdlResponseAcceptance, outputPredictedAcdl, outputDisabledCorrectionMessage, outputViewSeparator, requestUserAcceptsPrediction, requestUserCorrectionAcdl, outputHeader: () => outputInfo(_formHeader()), outputFooter: () => outputInfo("Goodbye!"), outputSpecialCommandResult: (result) => outputInfo(chalk_1.default.grey(` ${result}`)), }; }; exports.createView = createView; const _formHeader = () => { const headerStr = `Welcome to ASK Dialog In evaluate mode, type your utterance text onto the console and hit enter Alexa will then evaluate your input and give a response! Use ".save" to save list of utterances to a file. Use ".vars" to check what variables have been created Use ".endTurn" to explicitly end the current turn You can exit the interactive mode by entering ".quit" or "ctrl + c".`; const terminalWidth = process.stdout.columns; const halfWidth = Math.floor(terminalWidth / 2); const bar = "=".repeat(terminalWidth); const formattedLines = headerStr.split("\n").map((line) => { const paddedLine = ` ${line.trim()} `; const offset = halfWidth - Math.floor(paddedLine.length / 2); if (offset < 0) { return `===${paddedLine}===`; } return bar.slice(0, offset) + paddedLine + bar.slice(offset + paddedLine.length); }); return `\n${formattedLines.join("\n")}\n`; }; function alexaSay(message) { return ` ${AlexaSayTitle}${chalk_1.default.bold(message)}`; } exports.alexaSay = alexaSay;