UNPKG

@cognigy/rest-api-client

Version:

Cognigy REST-Client

551 lines 21.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NEXT_ACTION_ASSIST = void 0; /* Custom modules */ const createNodeDescriptor_1 = require("../../createNodeDescriptor"); const constants_1 = require("./constants/constants"); const nextActionWidgetTemplate_1 = require("./htmlTemplates/nextActionWidgetTemplate"); const constants_2 = require("./constants/constants"); const configValidator_helper_1 = require("./helpers/knowledgeSearch/configValidator.helper"); const followUpDetection_helper_1 = require("./helpers/knowledgeSearch/followUpDetection.helper"); const errorHandler_helper_1 = require("./helpers/knowledgeSearch/errorHandler.helper"); const knowledgeSearch_helper_1 = require("./helpers/knowledgeSearch/knowledgeSearch.helper"); const answerExtraction_helper_1 = require("./helpers/knowledgeSearch/answerExtraction.helper"); const getFontSizeFieldOptions_1 = require("./helpers/getFontSizeFieldOptions"); /** * Node name: "nextActionAssist" * * Purpose: * The Next Action Assist widget will suggest agent responses and provide more information to draft messages. */ exports.NEXT_ACTION_ASSIST = (0, createNodeDescriptor_1.createNodeDescriptor)({ type: "nextActionAssist", defaultLabel: "Copilot: Next Action Tile", summary: "UI__NODE_EDITOR__NEXT_ACTION_ASSIST__SUMMARY", appearance: { showIcon: false, }, tags: ["agentAssist"], fields: [ { key: "tileId", label: "UI__NODE_EDITOR__NEXT_ACTION_ASSIST__ID__LABEL", type: "cognigyText", description: "UI__NODE_EDITOR__NEXT_ACTION_ASSIST_TILE_ID__DESCRIPTION", defaultValue: "next-action", params: { required: true, }, }, { key: "text", label: "UI__NODE_EDITOR__NEXT_ACTION_ASSIST__TEXT__LABEL", type: "cognigyText", description: "UI__NODE_EDITOR__NEXT_ACTION_ASSIST__TEXT__DESCRIPTION", defaultValue: "", condition: { key: "type", value: "text", }, }, { key: "type", label: "UI__NODE_EDITOR__NEXT_ACTION_ASSIST__TYPE__LABEL", type: "select", defaultValue: "text", params: { options: [ { label: "UI__NODE_EDITOR__NEXT_ACTION_ASSIST__TYPE__OPTIONS__TEXT__LABEL", value: "text", }, { label: "UI__NODE_EDITOR__NEXT_ACTION_ASSIST__TYPE__OPTIONS_KNOWLEDGE_ASSIST__LABEL", value: "knowledgeAssist", }, ], }, }, { key: "knowledgeStoreId", type: "knowledgeStoreSelect", label: "UI__NODE_EDITOR__NEXT_ACTION_ASSIST__TYPE__KNOWLEDGE_STORE_ID__LABEL", description: "UI__NODE_EDITOR__NEXT_ACTION_ASSIST__TYPE__KNOWLEDGE_STORE_ID__DESCRIPTION", params: { required: true, }, condition: { key: "type", value: "knowledgeAssist", }, }, { key: "followUpDetection", type: "select", label: "UI__NODE_EDITOR__SEARCH_EXTRACT_OUTPUT__FOLLOW_UP_DETECTION__LABEL", description: "UI__NODE_EDITOR__SEARCH_EXTRACT_OUTPUT__FOLLOW_UP_DETECTION__DESCRIPTION", defaultValue: "transcript", params: { options: [ { label: "UI__NODE_EDITOR__SEARCH_EXTRACT_OUTPUT__FOLLOW_UP_DETECTION__OPTIONS__NONE__LABEL", value: "none", }, { label: "UI__NODE_EDITOR__SEARCH_EXTRACT_OUTPUT__FOLLOW_UP_DETECTION__OPTIONS__TRANSCRIPT__LABEL", value: "transcript", }, ], }, }, { key: "followUpDetectionSteps", type: "slider", label: "UI__NODE_EDITOR__SEARCH_EXTRACT_OUTPUT__FOLLOW_UP_DETECTION_STEPS__LABEL", description: "UI__NODE_EDITOR__SEARCH_EXTRACT_OUTPUT__FOLLOW_UP_DETECTION_STEPS__DESCRIPTION", defaultValue: 2, params: { min: 1, max: 6, step: 1, }, condition: { key: "followUpDetection", value: "transcript", }, }, { key: "topK", type: "slider", label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__TOP_K__LABEL", description: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__TOP_K__DESCRIPTION", defaultValue: 5, params: { required: true, min: constants_1.TOP_K_MIN_VALUE, max: constants_1.TOP_K_MAX_VALUE, }, }, { key: "searchStoreLocation", type: "select", label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__STORE_LOCATION__LABEL", params: { options: [ { label: "default (input.knowledgeSearch)", value: "default", }, { label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__STORE_LOCATION__INPUT__LABEL", value: "input", }, { label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__STORE_LOCATION__CONTEXT__LABEL", value: "context", }, ], required: true, }, defaultValue: "default", }, { key: "searchStoreLocationInputKey", type: "cognigyText", label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__INPUT_KEY__LABEL", description: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__INPUT_KEY__DESCRIPTION", defaultValue: "knowledgeSearch", condition: { key: "searchStoreLocation", value: "input", }, }, { key: "searchStoreLocationContextKey", type: "cognigyText", label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__CONTEXT_KEY__LABEL", description: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__CONTEXT_KEY__DESCRIPTION", defaultValue: "knowledgeSearch", condition: { key: "searchStoreLocation", value: "context", }, }, { key: "prompt", label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__PROMPT__LABEL", type: "cognigyLLMText", description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__PROMPT__DESCRIPTION", params: { required: true, multiline: true, rows: 5, }, defaultValue: constants_2.DEFAULT_PROMPT, }, { key: "temperature", label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__TEMPERATURE__LABEL", type: "slider", description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__TEMPERATURE__DESCRIPTION", defaultValue: 0.7, params: { min: 0, max: 1, step: 0.1, }, }, { key: "maxTokens", label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__MAX_TOKENS__LABEL", type: "slider", description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__MAX_TOKENS__DESCRIPTION", defaultValue: 1000, params: { min: 100, max: 4000, step: 1, }, }, { key: "frequencyPenalty", label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__FREQUENCY_PENALTY__LABEL", type: "slider", description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__FREQUENCY_PENALTY__DESCRIPTION", defaultValue: 0, params: { min: -2, max: 2, step: 0.1, }, }, { key: "presencePenalty", label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__PRESENCE_PENALTY__LABEL", type: "slider", description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__PRESENCE_PENALTY__DESCRIPTION", defaultValue: 0, params: { min: -2, max: 2, step: 0.1, }, }, { key: "useStop", label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__USE_STOP__LABEL", type: "toggle", description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__USE_STOP__DESCRIPTION", defaultValue: false, }, { key: "stop", label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STOP__LABEL", type: "textArray", description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STOP__DESCRIPTION", condition: { key: "useStop", value: true, }, }, { key: "timeout", label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__TIMEOUT__LABEL", defaultValue: 5000, type: "number", description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__TIMEOUT__DESCRIPTION", }, { key: "outputFallback", type: "cognigyText", label: "UI__NODE_EDITOR__SEARCH_EXTRACT_OUTPUT__FIELDS__OUTPUT_FALLBACK__LABEL", description: "UI__NODE_EDITOR__SEARCH_EXTRACT_OUTPUT__FIELDS__OUTPUT_FALLBACK__DESCRIPTION", defaultValue: constants_1.OUTPUT_FALLBACK_DEFAULT, }, { key: "errorHandling", type: "select", label: "UI__NODE_EDITOR__SEARCH_EXTRACT_OUTPUT__FIELDS__HANDLE_SERVICE_ERROR__LABEL", description: "UI__NODE_EDITOR__SEARCH_EXTRACT_OUTPUT__FIELDS__HANDLE_SERVICE_ERROR__DESCRIPTION", defaultValue: "stop", params: { options: [ { label: "UI__NODE_EDITOR__SEARCH_EXTRACT_OUTPUT__FIELDS__HANDLE_SERVICE_ERROR__OPTIONS__STOP__LABEL", value: "stop", }, { label: "UI__NODE_EDITOR__SEARCH_EXTRACT_OUTPUT__FIELDS__HANDLE_SERVICE_ERROR__OPTIONS__CONTINUE__LABEL", value: "continue", }, { label: "UI__NODE_EDITOR__SEARCH_EXTRACT_OUTPUT__FIELDS__HANDLE_SERVICE_ERROR__OPTIONS__GOTO__LABEL", value: "goto", }, ], }, }, { key: "timeoutMessage", label: "UI__NODE_EDITOR__SEARCH_EXTRACT_OUTPUT__FIELDS__ERROR_MESSAGE__LABEL", type: "cognigyText", description: "UI__NODE_EDITOR__SEARCH_EXTRACT_OUTPUT__FIELDS__ERROR_MESSAGE__DESCRIPTION", defaultValue: constants_1.TIMEOUT_MESSAGE_DEFAULT, condition: { key: "errorHandling", value: "continue", }, }, { key: "errorHandlingGotoTarget", type: "flowNode", label: "UI__NODE_EDITOR__SEARCH_EXTRACT_OUTPUT__FIELDS__ERROR__GOTO_NODE__LABEL", condition: { key: "errorHandling", value: "goto", }, }, { key: "enableCopyToClipboard", label: "UI__NODE_EDITOR__TRANSCRIPT_ASSIST__ENABLE_COPY_TO_CLIPBOARD__LABEL", type: "toggle", defaultValue: false, }, { key: "fontSize", type: "select", label: "UI__NODE_EDITOR__COPILOT__TILE_UI_PREFERENCES__FONT_SIZE", description: "UI__NODE_EDITOR__COPILOT__TILE_UI_PREFERENCES__FONT_SIZE__INFO", defaultValue: "text-sm", params: { options: getFontSizeFieldOptions_1.getFontSizeFieldOptions, }, }, { key: "actionFontSize", type: "select", label: "UI__NODE_EDITOR__COPILOT__TILE_UI_PREFERENCES__NEXT_ASSIST__ACTION__FONT_SIZE", description: "UI__NODE_EDITOR__COPILOT__TILE_UI_PREFERENCES__NEXT_ASSIST__ACTION__FONT_SIZE__DESCRIPTION", defaultValue: "text-sm", params: { options: getFontSizeFieldOptions_1.getFontSizeFieldOptions, }, }, ], sections: [ { key: "searchSettings", label: "Search Settings", defaultCollapsed: true, fields: [ "followUpDetection", "followUpDetectionSteps", "topK", "searchStoreLocation", "searchStoreWarning", "searchStoreLocationInputKey", "searchStoreLocationContextKey", ], condition: { key: "type", value: "knowledgeAssist", }, }, { key: "extractSettings", label: "Extract Settings", defaultCollapsed: true, fields: [ "prompt", "model", "temperature", "topP", "maxTokens", "presencePenalty", "frequencyPenalty", "useStop", "stop", "timeout", ], condition: { or: [ { key: "mode", value: "seo", }, { key: "mode", value: "se", }, ], }, }, { key: "outputSettings", label: "Output Settings", defaultCollapsed: true, fields: [ "outputMode", "streamDescription", "streamStopTokens", "outputFallback", ], condition: { key: "mode", value: "seo", }, }, { key: "uiPreferences", label: "UI__NODE_EDITOR__COPILOT__TILE_UI_PREFERENCES__TITLE", defaultCollapsed: true, fields: ["fontSize", "actionFontSize", "enableCopyToClipboard"], }, { key: "errorHandling", label: "UI__NODE_EDITOR__SEARCH_EXTRACT_OUTPUT__SECTIONS__ERROR_HANDLING__LABEL", defaultCollapsed: true, fields: ["errorHandling", "timeoutMessage", "errorHandlingGotoTarget"], condition: { key: "type", value: "knowledgeAssist", }, }, ], form: [ { type: "field", key: "tileId" }, { type: "field", key: "text" }, { type: "field", key: "type" }, { type: "field", key: "knowledgeStoreId" }, { type: "section", key: "searchSettings", }, { type: "section", key: "extractSettings" }, { type: "section", key: "outputSettings" }, { type: "section", key: "errorHandling" }, { type: "section", key: "uiPreferences" }, ].filter((element) => !!element), function: async (params) => { const { cognigy, config, nodeId } = params; const { api, input } = cognigy; if (!api || !api.log) { throw new Error("Unable to retrieve executionApi"); } const { searchStoreLocation = "default", searchStoreLocationContextKey = "knowledgeSearch", searchStoreLocationInputKey = "knowledgeSearch", temperature = 0.7, maxTokens = 1000, topP, presencePenalty = 0, frequencyPenalty = 0, useStop = false, stop = "", contextKey, inputKey = "knowledgeSearch", timeout = 5000, timeoutMessage = constants_1.TIMEOUT_MESSAGE_DEFAULT, outputFallback = constants_1.OUTPUT_FALLBACK_DEFAULT, outputMode, errorHandling = "stop", errorHandlingGotoTarget = "", streamStopTokens, followUpDetection = "transcript", followUpDetectionSteps = 2, type, prompt = constants_2.DEFAULT_PROMPT, } = config; const { traceId } = input; let nextActionText = config.text; let lastCustomerInput = input.text; if (!lastCustomerInput) { api.logDebugError("No input text found.", "Copilot: Next Action Tile Error"); return; } // timeout message name not changed because of legacy compatibility const errorMessage = timeoutMessage; try { if (type === "knowledgeAssist") { (0, configValidator_helper_1.validateKnowledgeSearchConfig)({ knowledgeStoreId: config.knowledgeStoreId, topK: config.topK, searchStoreLocation, searchStoreLocationContextKey, searchStoreLocationInputKey, }); const errorHandler = (0, errorHandler_helper_1.handleServiceError)({ api, cognigy, nodeId, traceId, searchStoreLocation, searchStoreLocationContextKey, searchStoreLocationInputKey, errorHandling, errorMessage, errorHandlingGotoTarget, }); // If is there is transcript to give context to the follow-up detection, use it to rewrite the input if (followUpDetection === "transcript") { const promptResponse = await (0, followUpDetection_helper_1.detectFollowUp)({ value: lastCustomerInput, followUpDetectionSteps, cognigy, }); if (promptResponse) { // Override the lastCustomerInput with the detected follow-up which is the rephrased version of the lastCustomerInput lastCustomerInput = promptResponse; } } const knowledgeSearchResponseData = await (0, knowledgeSearch_helper_1.performKnowledgeSearch)({ api, config: { language: input.language, traceId, topK: config.topK, searchStoreLocation, searchStoreLocationContextKey, searchStoreLocationInputKey, knowledgeStoreId: config.knowledgeStoreId, }, query: lastCustomerInput, input, errorHandler, }); const answer = await (0, answerExtraction_helper_1.extractAnswer)({ api, config: { contextKey, frequencyPenalty, inputKey, maxTokens, outputMode, presencePenalty, searchStoreLocation, stop, streamStopTokens, temperature, timeout, topP, useStop, prompt, }, knowledgeSearchResponseData, userInput: lastCustomerInput, input, errorHandler, }); nextActionText = answer || outputFallback; } else { if (!nextActionText) { api.logDebugError("Please provide a text in the text field.", "Copilot: Next Action Tile Error"); return; } } const data = { html: (0, nextActionWidgetTemplate_1.default)({ text: nextActionText, postMessageUrl: process.env.AGENT_ASSIST_WORKSPACE_FRONTEND_URL_WITH_PROTOCOL || "", enableCopyToClipboard: config.enableCopyToClipboard, fontSize: config.fontSize, actionFontSize: config.actionFontSize, }), }; cognigy.api.sendTileUpdateToAgentAssistWorkspace(Object.assign(Object.assign({}, params), { tile: { id: config.tileId, type: "html", data, } })); } catch (error) { if (error instanceof SyntaxError) { api.logDebugError("Please provide a valid JSON in the JSON Data field.", "Copilot: Next Action Tile Error"); } else { api.logDebugError(error, "Copilot: Next Action Tile Error"); } } return; }, }); //# sourceMappingURL=nextActionAssist.js.map