UNPKG

@cognigy/rest-api-client

Version:

Cognigy REST-Client

213 lines 7.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.THINK_V2 = void 0; /* Custom modules */ const createNodeDescriptor_1 = require("../../createNodeDescriptor"); const logic_1 = require("../logic"); const logFullConfigToDebugMode_1 = require("../../../helper/logFullConfigToDebugMode"); const errors_1 = require("../../../errors"); /** * Node name: 'think' * * Purpose: * Thinks a message into the Flow */ exports.THINK_V2 = (0, createNodeDescriptor_1.createNodeDescriptor)({ type: "thinkV2", defaultLabel: "Think", summary: "UI__NODE_EDITOR__THINK_V2__SUMMARY", appearance: { showIcon: false }, fields: [ { key: "thinkType", type: "select", label: "UI__NODE_EDITOR__THINK_V2__THINK_TYPE__LABEL", description: "UI__NODE_EDITOR__THINK_V2__THINK_TYPE__DESCRIPTION", params: { options: [ { label: "UI__NODE_EDITOR__THINK_V2__THINK_TYPE__DEFAULT__LABEL", value: "default", }, { label: "UI__NODE_EDITOR__THINK_V2__THINK_TYPE__INTENT__LABEL", value: "intent", }, ], }, defaultValue: "default" }, { key: "text", type: "cognigyText", label: "UI__NODE_EDITOR__THINK_V2__TEXT__LABEL", description: "UI__NODE_EDITOR__THINK_V2__TEXT__DESCRIPTION", condition: { key: "thinkType", value: "intent", negate: true } }, { key: "data", type: "json", label: "UI__NODE_EDITOR__THINK_V2__DATA__LABEL", defaultValue: "{}", condition: { key: "thinkType", value: "intent", negate: true } }, { key: "intent", type: "cognigyText", label: "UI__NODE_EDITOR__THINK_V2__INTENT__LABEL", description: "UI__NODE_EDITOR__THINK_V2__INTENT__DESCRIPTION", condition: { key: "thinkType", value: "intent" } }, { key: "logErrorToSystem", label: "UI__NODE_EDITOR__THINK_V2__LOG_ERROR_TO_SYSTEM__LABEL", description: "UI__NODE_EDITOR__THINK_V2__LOG_ERROR_TO_SYSTEM__DESCRIPTION", type: "toggle", defaultValue: false, }, { key: "errorHandling", type: "select", label: "UI__NODE_EDITOR__THINK_V2__HANDLE_ERROR__LABEL", description: "UI__NODE_EDITOR__THINK_V2__HANDLE_ERROR__DESCRIPTION", defaultValue: "continue", params: { options: [ { label: "UI__NODE_EDITOR__THINK_V2__HANDLE_ERROR__OPTIONS__STOP__LABEL", value: "stop" }, { label: "UI__NODE_EDITOR__THINK_V2__HANDLE_ERROR__OPTIONS__CONTINUE__LABEL", value: "continue" }, { label: "UI__NODE_EDITOR__THINK_V2__HANDLE_ERROR__ERROR__OPTIONS__GOTO__LABEL", value: "goto" }, ] } }, { key: "errorMessage", label: "UI__NODE_EDITOR__THINK_V2__ERROR_MESSAGE__LABEL", type: "cognigyText", description: "UI__NODE_EDITOR__THINK_V2__ERROR_MESSAGE__DESCRIPTION", defaultValue: "", condition: { key: "errorHandling", value: "continue" } }, { key: "errorHandlingGotoTarget", type: "flowNode", label: "UI__NODE_EDITOR__THINK_V2__ERROR__GOTO_NODE__LABEL", condition: { key: "errorHandling", value: "goto" } }, ], sections: [ { key: "advanced", label: "UI__NODE_EDITOR__THINK_V2__ADVANCED__LABEL", defaultCollapsed: true, fields: [ "thinkType", "data" ] }, { key: "errors", label: "UI__NODE_EDITOR__THINK_V2__SECTIONS__ERRORS__LABEL", defaultCollapsed: true, fields: [ "logErrorToSystem", "errorHandling", "errorMessage", "errorHandlingGotoTarget", ] } ], form: [ { type: "field", key: "text" }, { type: "field", key: "intent" }, { type: "section", key: "advanced" }, { type: "section", key: "errors" }, ], preview: { key: "text", type: "text", }, tags: ["basic", "logic", "recursion", "inject"], function: async ({ cognigy, config, nodeId }) => { const { text, data, intent, thinkType, errorHandling = "continue", errorHandlingGotoTarget, errorMessage, logErrorToSystem, } = config; const { api, input } = cognigy; const { traceId } = input; (0, logFullConfigToDebugMode_1.logFullConfigToDebugMode)(cognigy, config); if ((thinkType === "default" && typeof text !== "string") || (thinkType === "intent" && typeof intent !== "string")) { const errorDetails = { message: "Think input should be string", originalInput: JSON.stringify(text || intent) }; if (logErrorToSystem) { api.log("error", JSON.stringify(errorDetails)); } api.logDebugError(errorDetails, "UI__DEBUG_MODE__THINK_NODE__ERROR"); // handle error depending on the settings if (errorHandling === "continue") { // output the provided error message if (errorMessage) { await api.output(errorMessage, null); } } else if (errorHandling === "goto") { if (!errorHandlingGotoTarget) { throw new Error("GoTo Target is required"); } const gotoParams = { cognigy, childConfigs: [], nodeId, config: { flowNode: { flow: errorHandlingGotoTarget.flow, node: errorHandlingGotoTarget.node, }, injectedText: undefined, injectedData: undefined, executionMode: "continue", absorbContext: false } }; await logic_1.GO_TO.function(gotoParams); } else { throw new errors_1.InternalServerError(errorDetails.message, { traceId }); } } else if (thinkType === "intent") { api.thinkV2(`cIntent:${intent}`, null); } else if (text || data) { api.thinkV2(text, data); } } }); //# sourceMappingURL=thinkV2.js.map