UNPKG

@cognigy/rest-api-client

Version:

Cognigy REST-Client

549 lines 21.3 kB
import { __awaiter } from "tslib"; /* Custom modules */ import { createNodeDescriptor } from "../../createNodeDescriptor"; import { OUTPUT_FALLBACK_DEFAULT, TIMEOUT_MESSAGE_DEFAULT, TOP_K_MAX_VALUE, TOP_K_MIN_VALUE, } from "./constants/constants"; import nextActionWidgetTemplate from "./htmlTemplates/nextActionWidgetTemplate"; import { DEFAULT_PROMPT } from "./constants/constants"; import { validateKnowledgeSearchConfig } from "./helpers/knowledgeSearch/configValidator.helper"; import { detectFollowUp } from "./helpers/knowledgeSearch/followUpDetection.helper"; import { handleServiceError } from "./helpers/knowledgeSearch/errorHandler.helper"; import { performKnowledgeSearch } from "./helpers/knowledgeSearch/knowledgeSearch.helper"; import { extractAnswer } from "./helpers/knowledgeSearch/answerExtraction.helper"; import { getFontSizeFieldOptions } from "./helpers/getFontSizeFieldOptions"; /** * Node name: "nextActionAssist" * * Purpose: * The Next Action Assist widget will suggest agent responses and provide more information to draft messages. */ export const NEXT_ACTION_ASSIST = 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: TOP_K_MIN_VALUE, max: 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: 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: 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: 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, }, }, { 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, }, }, ], 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: (params) => __awaiter(void 0, void 0, void 0, function* () { 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 = TIMEOUT_MESSAGE_DEFAULT, outputFallback = OUTPUT_FALLBACK_DEFAULT, outputMode, errorHandling = "stop", errorHandlingGotoTarget = "", streamStopTokens, followUpDetection = "transcript", followUpDetectionSteps = 2, type, prompt = 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") { validateKnowledgeSearchConfig({ knowledgeStoreId: config.knowledgeStoreId, topK: config.topK, searchStoreLocation, searchStoreLocationContextKey, searchStoreLocationInputKey, }); const errorHandler = 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 = yield 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 = yield performKnowledgeSearch({ api, config: { language: input.language, traceId, topK: config.topK, searchStoreLocation, searchStoreLocationContextKey, searchStoreLocationInputKey, knowledgeStoreId: config.knowledgeStoreId, }, query: lastCustomerInput, input, errorHandler, }); const answer = yield 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: nextActionWidgetTemplate({ 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