@cognigy/rest-api-client
Version:
Cognigy REST-Client
381 lines • 16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OPTIONAL_QUESTION = void 0;
/* Custom modules */
const createNodeDescriptor_1 = require("../../../createNodeDescriptor");
const evaluateQuestionAnswer_1 = require("./utils/evaluateQuestionAnswer");
const say_1 = require("../say");
const BaseContext_1 = require("../../../../helper/BaseContext");
const getRephraseWithAIFields_1 = require("../../../helpers/generativeAI/getRephraseWithAIFields");
exports.OPTIONAL_QUESTION = (0, createNodeDescriptor_1.createNodeDescriptor)({
type: "optionalQuestion",
defaultLabel: "Optional Question",
summary: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__SUMMARY",
behavior: {
stopping: false,
entrypoint: true
},
appearance: {
showIcon: false
},
fields: [
{
key: "type",
type: "select",
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__TYPE__LABEL",
params: {
required: true,
options: [
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__TYPE__OPTIONS__YES_NO__LABEL",
value: "yesNo"
},
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__TYPE__OPTIONS__INTENT__LABEL",
value: "intent"
},
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__TYPE__OPTIONS__SLOT__LABEL",
value: "keyphrase",
},
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__TYPE__OPTIONS__DATE__LABEL",
value: "date",
},
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__TYPE__OPTIONS__NUMBER__LABEL",
value: "number",
},
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__TYPE__OPTIONS__TEMPERATURE__LABEL",
value: "temperature",
},
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__TYPE__OPTIONS__AGE__LABEL",
value: "age",
},
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__TYPE__OPTIONS__URL__LABEL",
value: "url"
},
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__TYPE__OPTIONS__DURATION__LABEL",
value: "duration",
},
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__TYPE__OPTIONS__EMAIL__LABEL",
value: "email",
},
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__TYPE__OPTIONS__MONEY__LABEL",
value: "money"
},
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__TYPE__OPTIONS__PERCENTAGE__LABEL",
value: "percentage"
},
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__TYPE__OPTIONS__REGEX__LABEL",
value: "regex",
},
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__TYPE__OPTIONS__XAPP__LABEL",
value: "app",
},
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__TYPE__OPTIONS__DATA__LABEL",
value: "data",
},
].filter(option => !!option),
},
defaultValue: "yesNo",
},
{
key: "keyphraseTag",
type: "cognigyText",
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__KEY_PHRASE_TAG__LABEL",
condition: {
key: "type",
value: "keyphrase"
}
},
{
key: "usePositiveOnly",
type: "toggle",
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__USE_POSITIVE_ONLY__LABEL",
condition: {
key: "type",
value: "keyphrase"
}
},
{
key: "regex",
type: "cognigyText",
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__REGEX__LABEL",
condition: {
key: "type",
value: "regex"
}
},
{
key: "say",
type: "say",
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__SAY__LABEL",
defaultValue: {
data: "{}",
linear: false,
loop: false,
type: "text",
text: ["Yes or no?"],
_cognigy: {},
}
},
{
key: "retentionTime",
type: "number",
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__RETENTION_TIME__LABEL",
defaultValue: 3
},
{
key: "storeDetailedResults",
type: "toggle",
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__STORE_DETAILS_RESULT__LABEL"
},
{
key: "storeResultInContext",
type: "toggle",
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__STORE_RESULT_IN_CONTEXT__LABEL"
},
{
key: "contextKey",
type: "text",
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__CONTEXT_KEY__LABEL",
condition: {
key: "storeResultInContext",
value: true
}
},
{
key: "executeChildrenOnly",
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__EXECUTE_CHILDREN_ONLY__LABEL",
type: "toggle",
defaultValue: true
},
{
key: "cognigyScript",
type: "select",
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__COGNIGY_SCRIPT__LABEL",
params: {
required: true,
options: [
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__COGNIGY_SCRIPT__OPTIONS__ANSWER_LABEL",
value: "answer"
},
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__COGNIGY_SCRIPT__OPTIONS__QUESTION_LABEL",
value: "question"
},
]
},
defaultValue: "answer"
},
{
key: "handoverOutput",
type: "select",
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__HANDOVER_OUTPUT__LABEL",
description: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__HANDOVER_OUTPUT__DESCRIPTION",
defaultValue: "userAndAgent",
params: {
options: [
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__HANDOVER_OUTPUT__OPTIONS__USER_AND_AGENT__LABEL",
value: "userAndAgent"
},
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__HANDOVER_OUTPUT__OPTIONS__USER_ONLY__LABEL",
value: "userOnly"
},
{
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__FIELDS__HANDOVER_OUTPUT__OPTIONS__AGENT_ONLY__LABEL",
value: "agentOnly"
},
],
},
},
...(0, getRephraseWithAIFields_1.getRephraseWithAIFields)(),
].filter(field => !!field),
sections: [
{
key: "context",
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__SECTIONS__CONTEXT__LABEL",
defaultCollapsed: true,
fields: [
"storeResultInContext",
"contextKey",
"skipIfResultInContext"
]
},
{
key: "advanced",
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__SECTIONS__ADVANCE__LABEL",
defaultCollapsed: true,
fields: [
"storeDetailedResults",
"executeChildrenOnly",
"retentionTime",
"cognigyScript"
]
},
{
key: "handoverSettings",
label: "UI__NODE_EDITOR__MESSAGE__QUESTION__OPTIONAL_QUESTION__SECTIONS__HANDOVER_SETTINGS__LABEL",
defaultCollapsed: true,
fields: [
"handoverOutput",
]
},
(0, getRephraseWithAIFields_1.getRephraseWithAISection)(),
].filter(section => !!section),
form: [
{ type: "field", key: "type" },
{ type: "field", key: "keyphraseTag" },
{ type: "field", key: "usePositiveOnly" },
{ type: "field", key: "regex" },
{ type: "field", key: "say" },
{ type: "section", key: "advanced" },
{ type: "section", key: "context" },
{ type: "section", key: "aiEnhancedOutput" },
{ type: "section", key: "handoverSettings" },
],
preview: {
key: "say",
type: "sayNode",
},
dependencies: {
children: ["onAnswer", "onQuestion"],
},
constraints: {
collapsable: true,
placement: {}
},
tags: ["basic", "message", "nlu"],
function: async ({ cognigy, config, childConfigs, nodeId, organisationId, }) => {
const { say, retentionTime = 3, type, executeChildrenOnly, cognigyScript, storeResultInContext, contextKey, handoverOutput, } = config;
const { regex: regexField } = config;
const { keyphraseTag, usePositiveOnly } = config;
const { api, input, context } = cognigy;
const { conditionalEntrypointWasExecuted } = input;
const onQuestionChild = childConfigs.find(child => child.type === "onQuestion");
// remember the current question in systemContext to later fill Input with it
let activeQuestion = api.getSystemContext('activeOptionalQuestion');
if (!activeQuestion || activeQuestion.nodeId !== nodeId) {
activeQuestion = {
nodeId,
type,
lastExecutedAt: input.execution,
forgetQuestionThreshold: retentionTime
};
api.setSystemContext('activeOptionalQuestion', activeQuestion);
}
else {
activeQuestion.lastExecutedAt = input.execution;
}
if (!conditionalEntrypointWasExecuted) {
let condition;
switch (type) {
case "date":
case "temperature":
case "age":
case "duration":
case "number":
case "money":
case "percentage":
case "url":
case "email":
condition = `!!input.slots['${type.toUpperCase()}']`;
break;
case "intent":
condition = `!!input.intent`;
break;
case "data":
condition = `!!input.data`;
break;
case "yesNo": {
condition = "input.type === 'pAnswer' || input.type === 'nAnswer'";
/* Make sure we do not trigger an intent on yes / no answer */
api.setSystemContext("reconfirmationInProgress", true);
break;
}
case "regex": {
condition = `input.text.match(${regexField}) && input.text.match(${regexField}).length > 0`;
break;
}
case "keyphrase": {
condition = (usePositiveOnly) ? `input.slots['${keyphraseTag}'] && !!input.slots['${keyphraseTag}'].find(k => k.neg === false)` : `!!input.slots['${keyphraseTag}']`;
break;
}
case "app": {
condition = `input.data?._cognigy?._app?.type === "submit"`;
}
}
const addConditionalEntrypointParams = {
entrypoint: nodeId,
retentionTime,
condition,
};
if (cognigyScript === "question") {
addConditionalEntrypointParams.cognigyScriptInput = input;
}
api.addConditionalEntrypoint(addConditionalEntrypointParams);
const rephraseWithAIParams = {
generativeAI_rephraseOutputMode: config.generativeAI_rephraseOutputMode,
generativeAI_temperature: config.generativeAI_temperature,
generativeAI_amountOfLastUserInputs: config.generativeAI_amountOfLastUserInputs,
generativeAI_customInputs: config.generativeAI_customInputs,
promptType: "question",
questionType: config.type,
question: undefined,
answer: undefined,
};
rephraseWithAIParams.promptType = "question";
rephraseWithAIParams.questionType = config.type;
await say_1.SAY.function({ cognigy, config: Object.assign({ handoverOutput, say }, rephraseWithAIParams), childConfigs: [], nodeId, organisationId });
if (onQuestionChild) {
api.setNextNode(onQuestionChild.id);
}
return;
}
const result = await (0, evaluateQuestionAnswer_1.evaluateQuestionAnswer)({ cognigy, config });
if (storeResultInContext && contextKey) {
// check whether contextKey is a deep query and set
// context storage location accordingly
let location = context;
let target = contextKey;
// this is a deep query
if (target.indexOf(".") > -1) {
const splits = target.split(".");
location = BaseContext_1.BaseContext.findLocationPathStateless(splits, true, location);
target = splits[splits.length - 1];
}
location[target] = result;
}
else {
input.result = result;
}
const child = childConfigs.find(child => child.type === "onAnswer");
if (!child) {
throw new Error("Unable to process answer to Question node since the correct child node couldn't be found");
}
if (executeChildrenOnly) {
api.setNextNode(null);
}
else {
api.resetCognigyScriptInput();
}
api.setNextNode(child.id);
api.deleteSystemContext('activeOptionalQuestion');
api.deleteSystemContext("reconfirmationInProgress");
}
});
//# sourceMappingURL=optionalQuestion.js.map