UNPKG

@cognigy/rest-api-client

Version:

Cognigy REST-Client

239 lines 8.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SWITCH = void 0; /* Custom modules */ const createNodeDescriptor_1 = require("../../../createNodeDescriptor"); exports.SWITCH = (0, createNodeDescriptor_1.createNodeDescriptor)({ type: "switch", defaultLabel: "Lookup", summary: "UI__NODE_EDITOR__SWITCH__SUMMARY", appearance: { color: "#FFC237", textColor: "white", contrastTextColor: "black", variant: "hexagon", showIcon: false, }, constraints: { collapsable: true, placement: { children: { whitelist: ["case", "default"], }, }, }, dependencies: { children: ["default", "case", "case"], }, fields: [ { key: "switch", type: "switchNode", label: "UI__NODE_EDITOR__SWITCH__SWITCH__LABEL", params: { required: true, options: [ { label: "UI__NODE_EDITOR__SWITCH__SWITCH__INTENT__LABEL", value: "intent", }, { label: "UI__NODE_EDITOR__SWITCH__SWITCH__TEXT__LABEL", value: "text", }, { label: "UI__NODE_EDITOR__SWITCH__SWITCH__STATE__LABEL", value: "state", }, { label: "UI__NODE_EDITOR__SWITCH__SWITCH__TYPE__LABEL", value: "type", }, { label: "UI__NODE_EDITOR__SWITCH__SWITCH__MODE__LABEL", value: "mode", }, { label: "UI__NODE_EDITOR__SWITCH__SWITCH__HANDOVER_STATUS__LABEL", value: "handoverStatus", }, { label: "UI__NODE_EDITOR__SWITCH__SWITCH__CALL_EVENT_STATUS__LABEL", value: "callEventStatus", }, { label: "UI__NODE_EDITOR__SWITCH__SWITCH__COGNIGY_SCRIPT__LABEL", value: "cognigyScript", }, ], }, defaultValue: { type: "intent", operator: "ci.intent" }, }, { key: "intentLevel", type: "select", label: "UI__NODE_EDITOR__SWITCH__INTENT_LEVEL__LABEL", description: "UI__NODE_EDITOR__SWITCH__INTENT_LEVEL__DESCRIPTION", condition: { /** The key of the Field whose value should be matched */ key: "switch.type", /** The expected value(s) that make this condition match */ value: "intent", }, params: { required: false, options: [ { label: "UI__NODE_EDITOR__SWITCH__INTENT_LEVEL__INPUT_INTENT__DEFAULT", value: "input.intent", }, { label: "UI__NODE_EDITOR__SWITCH__INTENT_LEVEL__INPUT_INTENT__LEVEL_1", value: "input.intentLevel.level1", groupName: "intentLevel" }, { label: "UI__NODE_EDITOR__SWITCH__INTENT_LEVEL__INPUT_INTENT__LEVEL_2", value: "input.intentLevel.level2", indentation: 1, groupName: "intentLevel" }, { label: "UI__NODE_EDITOR__SWITCH__INTENT_LEVEL__INPUT_INTENT__LEVEL_3", value: "input.intentLevel.level3", indentation: 2, groupName: "intentLevel" }, ], }, defaultValue: "input.intent", }, { key: "useStrict", label: "UI__NODE_EDITOR__SWITCH__USE_STRICT__LABEL", type: "toggle", description: "UI__NODE_EDITOR__SWITCH__USE_STRICT__DESCRIPTION" } ], sections: [ { key: "advanced", label: "UI__NODE_EDITOR__SWITCH__ADVANCED__LABEL", defaultCollapsed: true, fields: [ "useStrict" ] }, ], form: [ { type: "field", key: "switch" }, { type: "field", key: "intentLevel" }, { type: "section", key: "advanced" } ], preview: { key: "switch.operator", type: "custom", }, tags: ["basic", "logic", "switch", "branch"], function: async ({ cognigy, config, childConfigs: children }) => { var _a; const { api } = cognigy; const { switch: { type }, useStrict, intentLevel, } = config; let { switch: { operator }, } = config; switch (type) { case "intent": operator = intentLevel || "input.intent"; break; case "state": operator = "input.state"; break; case "type": operator = "input.type"; break; case "mode": operator = "input.mode"; break; case "text": operator = "input.text"; break; case "handoverStatus": operator = "input.handover.status"; break; case "callEventStatus": operator = "input.data.event"; break; case "cognigyScript": default: operator = operator; break; } let parsedOperator; if ( /** * Check if {{CognigyScript}} or token in the operator was already parsed in executeNode. * If that is the case, we don't parse again. * Otherwise parseCognigyScriptText(`{{${operator}}}`) would log an error. */ config.switch.operator !== config.switch.originalOperator && (config.switch.originalOperator.startsWith("[[snippet-") || (config.switch.originalOperator.startsWith("{{") && config.switch.originalOperator.endsWith("}}")))) { parsedOperator = operator; } else { /** * Try to parse the direct CognigyScript * in the operator field. In case the user * has used {{}} markers in the operator field * by mistake, then CS has already been parsed, * causing the parser return empty string. */ parsedOperator = (_a = (await api.parseCognigyScriptText(`{{${operator}}}`))) !== null && _a !== void 0 ? _a : operator; if (parsedOperator === "") { parsedOperator = operator; } } let matchedCase = undefined; if (children) { for (const child of children) { if (child.type !== "case") { continue; } const check = async () => { var _a, _b; if (useStrict) { return (await api.parseCognigyScriptText(`${(_a = child.config.case.value) === null || _a === void 0 ? void 0 : _a.trim()}`)) === parsedOperator; } else { /** * We cast the case * and the operator to strings to avoid issues * where e.g. the case contains a string but the * operator is a number. We do not support switching on * objects */ const parsedCognigyScriptText = `${await api.parseCognigyScriptText(`${(_b = child.config.case.value) === null || _b === void 0 ? void 0 : _b.trim()}`)}`; const parsedOperatorString = `${parsedOperator}`; return parsedCognigyScriptText === parsedOperatorString; } }; if (await check()) { matchedCase = child; break; } } } if (matchedCase) { api.setNextNode(matchedCase.id); return; } const defaultCase = children === null || children === void 0 ? void 0 : children.find((child) => child.type === "default"); if (defaultCase) { api.setNextNode(defaultCase.id); return; } }, }); //# sourceMappingURL=switch.js.map