@cognigy/rest-api-client
Version:
Cognigy REST-Client
490 lines • 19.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.KNOWLEDGE_ASSIST = void 0;
/* Custom modules */
const createNodeDescriptor_1 = require("../../createNodeDescriptor");
/*Template*/
const knowledgeAssistTemplate_1 = require("./htmlTemplates/knowledgeAssistTemplate");
const constants_1 = 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: 'knowledgeAssist'
*
* Purpose:
* Searches internal knowledge database for answers based on the user's input and sends it back to the AAW.
*/
exports.KNOWLEDGE_ASSIST = (0, createNodeDescriptor_1.createNodeDescriptor)({
type: "knowledgeAssist",
defaultLabel: "Copilot: Knowledge Tile",
summary: "UI__NODE_EDITOR__KNOWLEDGE_ASSIST__SUMMARY",
appearance: {
showIcon: false,
},
fields: [
{
key: "tileId",
label: "UI__NODE_EDITOR__KNOWLEDGE_ASSIST__ID__LABEL",
type: "cognigyText",
description: "UI__NODE_EDITOR__KNOWLEDGE_ASSIST_TILE_ID__DESCRIPTION",
defaultValue: "knowledge-assist",
params: {
required: true,
},
},
{
key: "knowledgeStoreId",
type: "knowledgeStoreSelect",
label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__KNOWLEDGE_STORE_ID__LABEL",
description: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__KNOWLEDGE_STORE_ID__DESCRIPTION",
params: {
required: true,
},
},
{
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_1.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,
},
},
],
sections: [
{
key: "searchSettings",
label: "Search Settings",
defaultCollapsed: true,
fields: [
"followUpDetection",
"followUpDetectionSteps",
"topK",
"searchStoreLocation",
"searchStoreWarning",
"searchStoreLocationInputKey",
"searchStoreLocationContextKey",
],
},
{
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", "enableCopyToClipboard"],
},
{
key: "errorHandling",
label: "UI__NODE_EDITOR__SEARCH_EXTRACT_OUTPUT__SECTIONS__ERROR_HANDLING__LABEL",
defaultCollapsed: true,
fields: ["errorHandling", "timeoutMessage", "errorHandlingGotoTarget"],
},
],
form: [
{ type: "field", key: "tileId" },
{ type: "field", key: "knowledgeStoreId" },
{ type: "section", key: "searchSettings" },
{ type: "section", key: "extractSettings" },
{ type: "section", key: "outputSettings" },
{ type: "section", key: "uiPreferences" },
{ type: "section", key: "errorHandling" },
],
preview: {
key: "text",
type: "text",
},
tags: ["agentAssist", "knowledgeSearch"],
// In order to prevent re-rendering on new end user messages, the flow node needs to be configured like specified here
// https://docs.cognigy.com/ai/nodes/ai-copilot/knowledge-tile/#configuration
function: async (knowledgeSearchParams) => {
var _a, _b, _c, _d;
const { cognigy, config, nodeId } = knowledgeSearchParams;
const { input, api } = cognigy;
if (!api || !api.log) {
throw new Error("Unable to retrieve executionApi");
}
const { searchStoreLocation, searchStoreLocationContextKey, searchStoreLocationInputKey, temperature, maxTokens, topP, presencePenalty, frequencyPenalty, useStop, stop, contextKey, inputKey = "knowledgeSearch", timeout, timeoutMessage, outputMode, outputFallback, errorHandling, errorHandlingGotoTarget, streamStopTokens, followUpDetection, prompt, followUpDetectionSteps, } = config;
// timeout message name not changed because of legacy compatibility
const errorMessage = timeoutMessage;
const { traceId } = input;
let actualSearchString = ((_d = (_c = (_b = (_a = input === null || input === void 0 ? void 0 : input.data) === null || _a === void 0 ? void 0 : _a._cognigy) === null || _b === void 0 ? void 0 : _b._agentAssist) === null || _c === void 0 ? void 0 : _c.payload) === null || _d === void 0 ? void 0 : _d.searchString) || "Hi";
(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,
});
try {
// 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: actualSearchString,
followUpDetectionSteps,
cognigy,
});
if (promptResponse) {
// Override the lastCustomerInput with the detected follow-up which is the rephrased version of the lastCustomerInput
actualSearchString = 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: actualSearchString,
input,
errorHandler,
});
const promptResponse = await (0, answerExtraction_helper_1.extractAnswer)({
api,
config: {
contextKey,
frequencyPenalty,
inputKey,
maxTokens,
outputMode,
presencePenalty,
searchStoreLocation,
stop,
streamStopTokens,
temperature,
timeout,
topP,
useStop,
prompt,
},
knowledgeSearchResponseData,
userInput: actualSearchString,
input,
errorHandler,
});
const data = {
html: (0, knowledgeAssistTemplate_1.knowledgeAssistTemplate)({
data: knowledgeSearchResponseData,
promptResponse: promptResponse || outputFallback,
tileId: config.tileId,
searchString: input.execution > 2 ? actualSearchString : "",
postMessageUrl: process.env.AGENT_ASSIST_WORKSPACE_FRONTEND_URL_WITH_PROTOCOL || "",
enableCopyToClipboard: config.enableCopyToClipboard,
fontSize: config.fontSize,
}),
};
await cognigy.api.sendTileUpdateToAgentAssistWorkspace(Object.assign(Object.assign({}, knowledgeSearchParams), { tile: {
id: config.tileId,
type: "html",
data,
} }));
}
catch (error) {
if ((0, errorHandler_helper_1.isInternalServerError)(error) || error instanceof Error) {
await errorHandler(error);
}
else {
api.log("error", `received error of unknown format: ${error}`);
}
api.logDebugError(error, "Copilot: Knowledge Tile Error");
return;
}
},
});
//# sourceMappingURL=knowledgeAssist.js.map