UNPKG

ask-cli-x

Version:

Alexa Skills Kit (ASK) Command Line Interfaces

116 lines (115 loc) 6.85 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatDialogAct = exports.explainAst = void 0; const acdl_1 = require("@alexa/acdl"); const cli_error_1 = __importDefault(require("../../../exceptions/cli-error")); const ensureShortName = (fullyQualifiedName) => { return fullyQualifiedName === null || fullyQualifiedName === void 0 ? void 0 : fullyQualifiedName.slice(fullyQualifiedName.lastIndexOf(".") + 1); }; const getName = (named) => { var _a; return (_a = named === null || named === void 0 ? void 0 : named.name) === null || _a === void 0 ? void 0 : _a.name; }; /** * Check to see if some argument is an array and the elements are likely of type argument. * Makes the assumption that all elements are of the same type as the first element. */ const isArrayOfArguments = (args) => { return !!args && Array.isArray(args) && args.length > 0 && args[0] instanceof acdl_1.Argument; }; const argumentArrayOfOrUndefined = (args) => { return isArrayOfArguments(args) ? args : undefined; }; const parseNer = (utterance) => { let pairs = []; while (utterance.indexOf("{") !== -1) { const ner = utterance.substring(utterance.indexOf("{") + 1, utterance.indexOf("}")); const [slot, value] = ner.split("|"); pairs.push([slot, value]); utterance = utterance.substring(utterance.indexOf("}") + 1); } return pairs; }; const formatNerPairs = (pairs) => { return pairs.map(([key, value]) => `${key} as ${value}`).join(", "); }; /** * TODO: Rewrite this... */ const explainAst = (ast) => { var _a, _b; const astName = getName(ast); if ((0, acdl_1.isTypeDeclaration)(ast)) { const typePropMsg = (_a = ast.properties) === null || _a === void 0 ? void 0 : _a.map((tp) => ` property "${getName(tp)}" as type "${getName(tp.type)}"`).join(","); return `Declare type "${astName}", with` + (typePropMsg !== null && typePropMsg !== void 0 ? typePropMsg : ` ${typePropMsg}`); } else if ((0, acdl_1.isNameDeclaration)(ast)) { return `Declare variable "${astName}". ${ast.expression ? (0, exports.explainAst)(ast.expression) : ""}`; } else if ((0, acdl_1.isCall)(ast)) { if (astName === null || astName === void 0 ? void 0 : astName.includes("received")) { return formatReceivedCall(ast); } else if (astName === null || astName === void 0 ? void 0 : astName.includes("response")) { return formatResponseCall(ast); } else { const argumentsMsg = (_b = argumentArrayOfOrUndefined(ast.arguments)) === null || _b === void 0 ? void 0 : _b.map((arg) => ` ${getName(arg)}'s value as "${getName(arg.value)}"`).join(","); return `Alexa calls "${astName}" with` + (argumentsMsg !== null && argumentsMsg !== void 0 ? argumentsMsg : ` ${argumentsMsg}`); } } else { throw new cli_error_1.default(`Unexpected AST to explain: ${ast}`); } }; exports.explainAst = explainAst; const formatReceivedCall = (ast) => { var _a; const returnTypeMsg = ast.genericArguments && ast.genericArguments[0] ? ` Return type "${getName(ast.genericArguments[0])}". ` : ""; const [first = undefined, second = undefined] = (_a = argumentArrayOfOrUndefined(ast.arguments)) !== null && _a !== void 0 ? _a : []; const secondArgumentNerCall = (second === null || second === void 0 ? void 0 : second.value) && (0, acdl_1.isCall)(second.value) ? second.value : undefined; const secondArgumentNer = typeof (secondArgumentNerCall === null || secondArgumentNerCall === void 0 ? void 0 : secondArgumentNerCall.arguments) === "string" ? secondArgumentNerCall.arguments : undefined; const firstArgumentActName = ensureShortName(getName(first === null || first === void 0 ? void 0 : first.value)); const nerPairs = secondArgumentNer ? parseNer(secondArgumentNer) : undefined; const nerMsg = nerPairs && nerPairs.length > 0 ? ` with ${formatNerPairs(nerPairs)}` : ""; return `User request act is ${firstArgumentActName}${nerMsg}.${returnTypeMsg}`; }; const formatResponseCall = (ast) => { var _a, _b; const responseArgument = (_a = argumentArrayOfOrUndefined(ast.arguments)) === null || _a === void 0 ? void 0 : _a.find((arg) => getName(arg) === "response"); const responseReference = (responseArgument === null || responseArgument === void 0 ? void 0 : responseArgument.value) && "name" in responseArgument.value ? responseArgument.value : undefined; const actArgument = (_b = argumentArrayOfOrUndefined(ast.arguments)) === null || _b === void 0 ? void 0 : _b.find((arg) => getName(arg) === "act"); const act = (actArgument === null || actArgument === void 0 ? void 0 : actArgument.value) && (0, acdl_1.isCall)(actArgument.value) ? actArgument.value : undefined; const responseMsg = responseReference ? ` with ${getName(responseReference)}` : ""; const actMsg = act && (0, exports.formatDialogAct)(act); return `Alexa responds${responseMsg} to ${actMsg}`; }; const formatDialogAct = (dialogAct) => { var _a, _b, _c, _d, _e, _f; const dialogActName = ensureShortName(getName(dialogAct)); switch (dialogActName) { case "RequestArgs": case "ConfirmArgs": const argsMsg = (_b = (_a = argumentArrayOfOrUndefined(dialogAct.arguments)) === null || _a === void 0 ? void 0 : _a.map((arg) => getName(arg === null || arg === void 0 ? void 0 : arg.value)).join(" ")) !== null && _b !== void 0 ? _b : ""; return `${ensureShortName(getName(dialogAct))} with arguments ${argsMsg}`; case "Notify": const actionNameArg = getName((_d = (_c = argumentArrayOfOrUndefined(dialogAct.arguments)) === null || _c === void 0 ? void 0 : _c.find((x) => getName(x) === "actionName")) === null || _d === void 0 ? void 0 : _d.value); const successArg = (_f = (_e = argumentArrayOfOrUndefined(dialogAct.arguments)) === null || _e === void 0 ? void 0 : _e.find((x) => getName(x) === "success")) === null || _f === void 0 ? void 0 : _f.value; const success = (0, acdl_1.isCall)(successArg) && typeof successArg.arguments === "boolean" && successArg.arguments; return `Notify that the call to API "${actionNameArg}" is ${success ? "succeeded" : "failed"}`; case "Offer": return `call the API`; case "Bye": return `exit the skill`; case "ReqMore": return `request the next user input`; case "OutOfDomain": return `let the user know their request is not supported by the skill`; default: return `${dialogActName}`; } }; exports.formatDialogAct = formatDialogAct;