UNPKG

clwoz-models

Version:
398 lines 18.4 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ var slateSerializer_1 = require("./slateSerializer"); var ActionTypes; (function (ActionTypes) { ActionTypes["TEXT"] = "TEXT"; ActionTypes["API_LOCAL"] = "API_LOCAL"; ActionTypes["CARD"] = "CARD"; ActionTypes["END_SESSION"] = "END_SESSION"; ActionTypes["SET_ENTITY"] = "SET_ENTITY"; ActionTypes["DISPATCH"] = "DISPATCH"; ActionTypes["CHANGE_MODEL"] = "CHANGE_MODEL"; })(ActionTypes = exports.ActionTypes || (exports.ActionTypes = {})); var ConditionType; (function (ConditionType) { ConditionType["EQUAL"] = "EQUAL"; ConditionType["NOT_EQUAL"] = "NOT_EQUAL"; ConditionType["GREATER_THAN"] = "GREATER_THAN"; ConditionType["GREATER_THAN_OR_EQUAL"] = "GREATER_THAN_OR_EQUAL"; ConditionType["LESS_THAN"] = "LESS_THAN"; ConditionType["LESS_THAN_OR_EQUAL"] = "LESS_THAN_OR_EQUAL"; ConditionType["STRING_EQUAL"] = "STRING_EQUAL"; })(ConditionType = exports.ConditionType || (exports.ConditionType = {})); var ComparisonType; (function (ComparisonType) { ComparisonType["NUMBER_OF_VALUES"] = "NUMBER_OF_VALUES"; ComparisonType["NUMERIC_VALUE"] = "NUMERIC_VALUE"; ComparisonType["STRING"] = "STRING"; })(ComparisonType = exports.ComparisonType || (exports.ComparisonType = {})); // Need dummy actionId for stub action exports.CL_STUB_IMPORT_ACTION_ID = '51cd7df5-e504-451d-b629-0932e604689c'; var ActionBase = /** @class */ (function () { function ActionBase(action) { this.requiredEntities = []; this.negativeEntities = []; this.requiredConditions = []; this.negativeConditions = []; this.actionId = action.actionId; this.actionType = action.actionType; this.createdDateTime = action.createdDateTime; this.payload = action.payload; this.isTerminal = action.isTerminal; this.isEntryNode = action.isEntryNode; this.repromptActionId = action.repromptActionId; this.requiredEntitiesFromPayload = action.requiredEntitiesFromPayload; this.requiredEntities = action.requiredEntities; this.negativeEntities = action.negativeEntities; this.requiredConditions = action.requiredConditions; this.negativeConditions = action.negativeConditions; this.suggestedEntity = action.suggestedEntity; this.version = action.version; this.packageCreationId = action.packageCreationId; this.packageDeletionId = action.packageDeletionId; this.entityId = action.entityId; this.enumValueId = action.enumValueId; this.clientData = action.clientData; } // TODO: Refactor away from generic GetPayload for different action types // They all return strings but the strings are very different (Text is the substituted values, but other actions dont) // This causes issue of having to pass in entityValueMap even when it's not required, but making it optional ruins // safety for those places which should require it. // TODO: Remove ScoredAction since it doesn't have payload ActionBase.GetPayload = function (action, entityValues) { if (this.isPVAContent(action)) { var pvaAction = new PVAAction(action); return pvaAction.renderValue(entityValues); } switch (action.actionType) { case ActionTypes.TEXT: { /** * For backwards compatibility check if payload is of new TextPayload type * Ideally we would implement schema refactor: * 1. Make payloads discriminated unions (E.g. After checking the action.type, flow control knows the type of the payload property) * This removes the need for the GetPayload function and GetArguments which are brittle coding patterns. */ try { var textPayload = JSON.parse(action.payload); return slateSerializer_1.default.serialize(textPayload.json, entityValues); } catch (e) { var error = e; throw new Error("Error when attempting to parse text action payload. This might be an old action which was saved as a string. Please create a new action. " + error.message); } } case ActionTypes.END_SESSION: { var textPayload = JSON.parse(action.payload); return slateSerializer_1.default.serialize(textPayload.json, entityValues); } case ActionTypes.CARD: { // For API or CARD the payload field of the outer payload is the name of API or the filename of the card template without extension var cardPayload = JSON.parse(action.payload); return cardPayload.payload; } case ActionTypes.API_LOCAL: { var actionPayload = JSON.parse(action.payload); return actionPayload.payload; } case ActionTypes.DISPATCH: { // TODO: Another reason to schema refactor... var actionPayload = JSON.parse(action.payload); return ActionTypes.DISPATCH + ": " + actionPayload.modelName; } case ActionTypes.CHANGE_MODEL: { var actionPayload = JSON.parse(action.payload); return ActionTypes.CHANGE_MODEL + ": " + actionPayload.modelName; } default: return action.payload; } }; // Return true if action is a placeholder ActionBase.isPlaceholderAPI = function (action) { if (!action) { return false; } if (action.payload && JSON.parse(action.payload).isPlaceholder) { return true; } return false; }; // Return true if action should be rendered as PVA content ActionBase.isPVAContent = function (action) { if (!action) { return false; } if (action.payload && JSON.parse(action.payload).isPVAContent) { return true; } return false; }; // Create dummy placeholder API action ActionBase.createPlaceholderAPIAction = function (placeholderName, isTerminal) { return new ActionBase({ actionId: null, payload: JSON.stringify({ payload: placeholderName, logicArguments: [], renderArguments: [], isPlaceholder: true }), createdDateTime: new Date().toJSON(), isTerminal: isTerminal, isEntryNode: false, requiredEntitiesFromPayload: [], requiredEntities: [], negativeEntities: [], requiredConditions: [], negativeConditions: [], suggestedEntity: undefined, version: 0, packageCreationId: 0, packageDeletionId: 0, actionType: ActionTypes.API_LOCAL, entityId: undefined, enumValueId: undefined }); }; /** Return arguments for an action */ ActionBase.GetActionArguments = function (action) { if (ActionTypes.CARD === action.actionType) { var cardPayload = JSON.parse(action.payload); return cardPayload.arguments.map(function (aa) { return new ActionArgument(aa); }); } else if (action.actionType === ActionTypes.API_LOCAL) { var actionPayload = JSON.parse(action.payload); return __spreadArrays(actionPayload.logicArguments, actionPayload.renderArguments).map(function (aa) { return new ActionArgument(aa); }); } return []; }; return ActionBase; }()); exports.ActionBase = ActionBase; var ActionArgument = /** @class */ (function () { function ActionArgument(actionArgument) { this.parameter = actionArgument.parameter; this.value = actionArgument.value.json; } ActionArgument.prototype.renderValue = function (entityValues, serializerOptions) { if (serializerOptions === void 0) { serializerOptions = {}; } return slateSerializer_1.default.serialize(this.value, entityValues, serializerOptions); }; return ActionArgument; }()); exports.ActionArgument = ActionArgument; var TextAction = /** @class */ (function (_super) { __extends(TextAction, _super); function TextAction(action) { var _this = _super.call(this, action) || this; if (action.actionType !== ActionTypes.TEXT) { throw new Error("You attempted to create text action from action of type: " + action.actionType); } _this.value = JSON.parse(_this.payload).json; return _this; } TextAction.prototype.renderValue = function (entityValues, serializerOptions) { if (serializerOptions === void 0) { serializerOptions = {}; } return slateSerializer_1.default.serialize(this.value, entityValues, serializerOptions); }; return TextAction; }(ActionBase)); exports.TextAction = TextAction; var ApiAction = /** @class */ (function (_super) { __extends(ApiAction, _super); function ApiAction(action) { var _this = _super.call(this, action) || this; if (action.actionType !== ActionTypes.API_LOCAL) { throw new Error("You attempted to create api action from action of type: " + action.actionType); } var actionPayload = JSON.parse(_this.payload); _this.name = actionPayload.payload; _this.logicArguments = actionPayload.logicArguments ? actionPayload.logicArguments.map(function (aa) { return new ActionArgument(aa); }) : []; _this.renderArguments = actionPayload.renderArguments ? actionPayload.renderArguments.map(function (aa) { return new ActionArgument(aa); }) : []; _this.isPlaceholder = actionPayload.isPlaceholder; _this.isCallbackUnassigned = actionPayload.isCallbackUnassigned; return _this; } ApiAction.prototype.renderLogicArguments = function (entityValues, serializerOptions) { if (serializerOptions === void 0) { serializerOptions = {}; } return this.renderArgs(this.logicArguments, entityValues, serializerOptions); }; ApiAction.prototype.renderRenderArguments = function (entityValues, serializerOptions) { if (serializerOptions === void 0) { serializerOptions = {}; } return this.renderArgs(this.renderArguments, entityValues, serializerOptions); }; ApiAction.prototype.renderArgs = function (args, entityValues, serializerOptions) { if (serializerOptions === void 0) { serializerOptions = {}; } return args.map(function (aa) { var value = null; try { value = slateSerializer_1.default.serialize(aa.value, entityValues, serializerOptions); } catch (error) { // Just return null if argument doesn't have a value } return __assign(__assign({}, aa), { value: value }); }); }; return ApiAction; }(ActionBase)); exports.ApiAction = ApiAction; var CardAction = /** @class */ (function (_super) { __extends(CardAction, _super); function CardAction(action) { var _this = _super.call(this, action) || this; if (action.actionType !== ActionTypes.CARD) { throw new Error("You attempted to create card action from action of type: " + action.actionType); } var payload = JSON.parse(_this.payload); _this.templateName = payload.payload; _this.arguments = payload.arguments.map(function (aa) { return new ActionArgument(aa); }); return _this; } CardAction.prototype.renderArguments = function (entityValues, serializerOptions) { if (serializerOptions === void 0) { serializerOptions = {}; } return this.arguments.map(function (aa) { var value = null; try { value = slateSerializer_1.default.serialize(aa.value, entityValues, serializerOptions); } catch (error) { // Just return null if argument doesn't have a value } return __assign(__assign({}, aa), { value: value }); }); }; return CardAction; }(ActionBase)); exports.CardAction = CardAction; var SessionAction = /** @class */ (function (_super) { __extends(SessionAction, _super); function SessionAction(action) { var _this = _super.call(this, action) || this; if (action.actionType !== ActionTypes.END_SESSION) { throw new Error("You attempted to create session action from action of type: " + action.actionType); } _this.value = JSON.parse(_this.payload).json; return _this; } SessionAction.prototype.renderValue = function (entityValues, serializerOptions) { if (serializerOptions === void 0) { serializerOptions = {}; } return slateSerializer_1.default.serialize(this.value, entityValues, serializerOptions); }; return SessionAction; }(ActionBase)); exports.SessionAction = SessionAction; var PVAAction = /** @class */ (function (_super) { __extends(PVAAction, _super); function PVAAction(action) { var _this = _super.call(this, action) || this; var payload = JSON.parse(_this.payload); if (!payload.isPVAContent) { throw new Error("You attempted to create PVA action for non-PVA content"); } _this.value = payload.value; return _this; } PVAAction.prototype.renderValue = function (entityValues) { var output = this.value; entityValues.forEach(function (value, key) { output = output.replace("{" + key + "}", value); }); return output; }; return PVAAction; }(ActionBase)); exports.PVAAction = PVAAction; var SetEntityAction = /** @class */ (function (_super) { __extends(SetEntityAction, _super); function SetEntityAction(action) { var _this = _super.call(this, action) || this; if (action.actionType !== ActionTypes.SET_ENTITY) { throw new Error("You attempted to create set entity action from action of type: " + action.actionType); } // TODO: Server already has actual entityId and enumValueId values, should not need to use payload like this // but some things like scored action only have the payload var jsonPayload = JSON.parse(_this.payload); _this.entityId = jsonPayload.entityId; _this.enumValueId = jsonPayload.enumValueId; return _this; } return SetEntityAction; }(ActionBase)); exports.SetEntityAction = SetEntityAction; var ModelAction = /** @class */ (function (_super) { __extends(ModelAction, _super); function ModelAction(action) { var _this = _super.call(this, action) || this; if (action.actionType !== ActionTypes.DISPATCH && action.actionType !== ActionTypes.CHANGE_MODEL) { throw new Error("You attempted to create Model action from action of type: " + action.actionType); } // TODO: Server already has actual modelId and modelName values, should not need to use payload like this // but some things like scored action only have the payload var jsonPayload = JSON.parse(_this.payload); _this.modelId = jsonPayload.modelId; _this.modelName = jsonPayload.modelName; return _this; } return ModelAction; }(ActionBase)); exports.ModelAction = ModelAction; var DispatchAction = /** @class */ (function (_super) { __extends(DispatchAction, _super); function DispatchAction(action) { var _this = this; if (action.actionType !== ActionTypes.DISPATCH) { throw new Error("You attempted to create Dispatch action from action of type: " + action.actionType); } _this = _super.call(this, action) || this; return _this; } return DispatchAction; }(ModelAction)); exports.DispatchAction = DispatchAction; var ChangeModelAction = /** @class */ (function (_super) { __extends(ChangeModelAction, _super); function ChangeModelAction(action) { var _this = this; if (action.actionType !== ActionTypes.CHANGE_MODEL) { throw new Error("You attempted to create ChangeModel action from action of type: " + action.actionType); } _this = _super.call(this, action) || this; return _this; } return ChangeModelAction; }(ModelAction)); exports.ChangeModelAction = ChangeModelAction; //# sourceMappingURL=Action.js.map