@cognigy/rest-api-client
Version:
Cognigy REST-Client
86 lines • 2.71 kB
JavaScript
import { __awaiter } from "tslib";
/* Custom modules */
import { createNodeDescriptor } from "../../createNodeDescriptor";
/**
* Node name: 'addToContext'
*
* Purpose:
* Adds a new value to the Cognigy context object.
*/
export const ADD_TO_CONTEXT = createNodeDescriptor({
type: "addToContext",
defaultLabel: "Add To Context",
summary: "UI__NODE_EDITOR__ADD_TO_CONTEXT__SUMMARY",
appearance: {
showIcon: false
},
fields: [
{
key: "key",
type: "cognigyText",
label: "UI__NODE_EDITOR__ADD_TO_CONTEXT__FIELDS__KEY__LABEL",
params: {
required: true
}
},
{
key: "value",
type: "cognigyText",
label: "UI__NODE_EDITOR__ADD_TO_CONTEXT__FIELDS__VALUE__LABEL",
params: {
required: true
}
},
{
key: "mode",
type: "select",
label: "UI__NODE_EDITOR__ADD_TO_CONTEXT__FIELDS__MODE__LABEL",
defaultValue: "simple",
description: "UI__NODE_EDITOR__ADD_TO_CONTEXT__FIELDS__MODE__DESCRIPTION",
params: {
required: true,
options: [
{
label: "UI__NODE_EDITOR__ADD_TO_CONTEXT__FIELDS__MODE__OPTIONS__SIMPLE__LABEL",
value: "simple",
},
{
label: "UI__NODE_EDITOR__ADD_TO_CONTEXT__FIELDS__MODE__OPTIONS__ARRAY__LABEL",
value: "array",
}
]
}
}
],
preview: {
key: "key",
type: "text",
},
tags: ["basic", "data", "store"],
function: ({ cognigy, config }) => __awaiter(void 0, void 0, void 0, function* () {
const { api } = cognigy;
const { key, mode } = config;
if (!key) {
throw new Error("Unable to add value to context. No context key was defined.");
}
if (!mode || !["simple", "array"].includes(mode)) {
throw new Error("Unable to add value to context. The 'mode' is missing or neither 'simple' nor 'array'.");
}
let value;
try {
value = JSON.parse(config.value);
}
catch (e) {
value = config.value;
}
api.addToContext(key, value, mode);
let stringValue = value;
try {
stringValue = JSON.stringify(value, undefined, 2);
}
catch (err) {
}
api.logDebugMessage(`context.${key} = ${stringValue}`);
})
});
//# sourceMappingURL=addToContext.js.map