UNPKG

@cognigy/rest-api-client

Version:

Cognigy REST-Client

190 lines 8.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SAY = void 0; /* Custom modules */ const createNodeDescriptor_1 = require("../../createNodeDescriptor"); const getRephraseWithAIFields_1 = require("../../helpers/generativeAI/getRephraseWithAIFields"); const rephraseSentenceWithAi_1 = require("../../helpers/generativeAI/rephraseSentenceWithAi"); exports.SAY = (0, createNodeDescriptor_1.createNodeDescriptor)({ type: "say", defaultLabel: "Say", summary: "UI__NODE_EDITOR__MESSAGE__SAY__SUMMARY", appearance: { showIcon: false, }, fields: [ { key: "say", type: "say", label: "UI__NODE_EDITOR__MESSAGE__SAY__FIELDS__SAY__LABEL", defaultValue: { data: "", linear: false, loop: false, text: [], type: "text", _cognigy: {}, }, }, { key: "handoverOutput", type: "select", label: "UI__NODE_EDITOR__MESSAGE__SAY__FIELDS__HANDOVER_OUTPUT__LABEL", description: "UI__NODE_EDITOR__MESSAGE__SAY__FIELDS__HANDOVER_OUTPUT__DESCRIPTION", defaultValue: "userAndAgent", params: { options: [ { label: "UI__NODE_EDITOR__MESSAGE__SAY__FIELDS__HANDOVER_OUTPUT__OPTINOS__USER_AGENT__LABEL", value: "userAndAgent" }, { label: "UI__NODE_EDITOR__MESSAGE__SAY__FIELDS__HANDOVER_OUTPUT__OPTINOS__USER_ONLY__LABEL", value: "userOnly" }, { label: "UI__NODE_EDITOR__MESSAGE__SAY__FIELDS__HANDOVER_OUTPUT__OPTINOS__AGENT_ONLY__LABEL", value: "agentOnly" }, ], }, }, { key: "preventTranscript", type: "toggle", defaultValue: false, label: "UI__NODE_EDITOR__MESSAGE__SAY__FIELDS__PREVENT_TRANSCRIPT__LABEL", description: "UI__NODE_EDITOR__MESSAGE__SAY__FIELDS__PREVENT_TRANSCRIPT__DESCRIPTION", }, ...(0, getRephraseWithAIFields_1.getRephraseWithAIFields)(), ].filter(field => !!field), sections: [ (0, getRephraseWithAIFields_1.getRephraseWithAISection)(), { key: "handoverSettings", label: "UI__NODE_EDITOR__MESSAGE__SAY__SECTIONS__HANDOVER_SETTINGS__LABEL", defaultCollapsed: true, fields: [ "handoverOutput", ] }, { key: "advanced", label: "UI__NODE_EDITOR__MESSAGE__SAY__SECTIONS__ADVANCED__LABEL", defaultCollapsed: true, fields: [ "preventTranscript", ], }, ].filter(section => !!section), form: [ { type: "field", key: "say" }, { type: "section", key: "aiEnhancedOutput" }, { type: "section", key: "handoverSettings" }, { type: "section", key: "advanced" } ], preview: { key: "say", type: "sayNode", }, tags: ["basic", "message"], mocking: { defaultMockCode: `api.say("Hello from Mock Code!");` }, function: async ({ cognigy, config, nodeId, organisationId, nodeType }) => { var _a, _b, _c, _d, _e, _f, _g; const { api } = cognigy; const { text, loop, linear, type, _data } = config.say; /*Say nodes are always forwardable */ const settings = { liveAgentSettings: { forwardable: true, outputDestination: config.handoverOutput || "userAndAgent", }, }; /** * If the 'type' attribute is set to 'text' we essentially * have a classic say node with a 'text' and 'data' attribute. */ if (type === "text") { let outputText = ""; let filteredText; if (Array.isArray(text)) { filteredText = text.filter(t => t !== null && t !== undefined && t !== "").map(t => t === null || t === void 0 ? void 0 : t.toString()); } if (linear) { let nodeIndex = api.getSystemContext(`say:order-${nodeId}`); if (nodeIndex === undefined) { nodeIndex = 0; } else { nodeIndex++; } if (nodeIndex > filteredText.length - 1) { if (loop === true) { nodeIndex = 0; } else { nodeIndex--; } } api.setSystemContext(`say:order-${nodeId}`, nodeIndex); outputText = filteredText[nodeIndex]; } else { outputText = Array.isArray(text) ? filteredText[Math.floor(Math.random() * filteredText.length)] : text; } /** * Clean unused config part */ if ((_a = _data === null || _data === void 0 ? void 0 : _data._cognigy) === null || _a === void 0 ? void 0 : _a._default) { delete _data._cognigy._default; } let sayData = _data; if (config.preventTranscript) { sayData = sayData || {}; sayData._cognigy = sayData._cognigy || {}; sayData._cognigy._preventTranscript = true; } if (((_b = cognigy === null || cognigy === void 0 ? void 0 : cognigy.input) === null || _b === void 0 ? void 0 : _b.channel) === "voiceGateway2") { sayData = Object.assign(Object.assign({}, sayData), { nodeType }); } outputText = await (0, rephraseSentenceWithAi_1.rephraseSentenceWithAI)(outputText, config, api, organisationId); await api.say(outputText, sayData, settings); } else { const _cognigyDefault = (_c = _data === null || _data === void 0 ? void 0 : _data._cognigy) === null || _c === void 0 ? void 0 : _c._default; const typeKey = "_" + type; if (type === "quickReplies" || type === "buttons") { if (((_d = _cognigyDefault === null || _cognigyDefault === void 0 ? void 0 : _cognigyDefault[typeKey]) === null || _d === void 0 ? void 0 : _d.text) && (_cognigyDefault === null || _cognigyDefault === void 0 ? void 0 : _cognigyDefault[typeKey].fallbackText)) { const result = await (0, rephraseSentenceWithAi_1.rephraseMultipleSentencesWithAI)([_cognigyDefault[typeKey].text, _cognigyDefault[typeKey].fallbackText], config, api, organisationId); if (result.length === 2) { _cognigyDefault[typeKey].text = result[0]; _cognigyDefault[typeKey].fallbackText = result[1]; } } else if ((_e = _cognigyDefault === null || _cognigyDefault === void 0 ? void 0 : _cognigyDefault[typeKey]) === null || _e === void 0 ? void 0 : _e.text) { _cognigyDefault[typeKey].text = await (0, rephraseSentenceWithAi_1.rephraseSentenceWithAI)(_cognigyDefault[typeKey].text, config, api, organisationId); } else if ((_f = _cognigyDefault === null || _cognigyDefault === void 0 ? void 0 : _cognigyDefault[typeKey]) === null || _f === void 0 ? void 0 : _f.fallbackText) { _cognigyDefault[typeKey].fallbackText = await (0, rephraseSentenceWithAi_1.rephraseSentenceWithAI)(_cognigyDefault[typeKey].fallbackText, config, api, organisationId); } } else if ((_g = _cognigyDefault === null || _cognigyDefault === void 0 ? void 0 : _cognigyDefault[typeKey]) === null || _g === void 0 ? void 0 : _g.fallbackText) { _cognigyDefault[typeKey].fallbackText = await (0, rephraseSentenceWithAi_1.rephraseSentenceWithAI)(_cognigyDefault[typeKey].fallbackText, config, api, organisationId); } const data = config.say; if (config.preventTranscript) { data._cognigy = Object.assign(Object.assign({}, (data._cognigy || {})), { _preventTranscript: true }); } /** * This is a say node which is set to a type other than 'text' - * so we have one of the new rich-media types such as 'gallery' */ await api.say(null, data, settings); } }, }); //# sourceMappingURL=say.js.map