UNPKG

@cognigy/rest-api-client

Version:

Cognigy REST-Client

177 lines 9.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CXONE_KNOWLEDGEHUB = exports.CXONE_KNOWLEDGEHUB_NODE_TYPE = void 0; /* Node modules */ const crypto = require("crypto"); /* Custom modules */ const createNodeDescriptor_1 = require("../../../createNodeDescriptor"); const logic_1 = require("../../logic"); const cxOneKnowledgeHubForm_1 = require("../fields/cxOneKnowledgeHubForm"); const cxOneKnowledgeHubUtils_1 = require("../libs/cxOneKnowledgeHubUtils"); const errors_1 = require("../../../../errors"); exports.CXONE_KNOWLEDGEHUB_NODE_TYPE = "cxOneKnowledgeHub"; exports.CXONE_KNOWLEDGEHUB = (0, createNodeDescriptor_1.createNodeDescriptor)({ type: exports.CXONE_KNOWLEDGEHUB_NODE_TYPE, defaultLabel: "UI__NODE_EDITOR__CXONE_KNOWLEDGEHUB__LABEL", summary: "UI__NODE_EDITOR__CXONE_KNOWLEDGEHUB__DESCRIPTION", behavior: { entrypoint: true }, fields: cxOneKnowledgeHubForm_1.fields, sections: cxOneKnowledgeHubForm_1.sections, form: cxOneKnowledgeHubForm_1.form, appearance: { color: "#3694FD", }, preview: { key: "query", type: "text" }, tags: ["nice", "cxone"], function: async ({ cognigy, config, nodeId }) => { var _a, _b, _c, _d, _e, _f, _g, _h; const { api, input } = cognigy; const { query, queryPersona, queryLanguage, filters, promptEditorProfileId, answersMaxWords, linksMax, knowledgeHubId, awsBedrockKbId, maxKernels, prompt, modelArn, storeLocation, inputKey, contextKey, logErrorToSystem, errorHandling = "continue", errorHandlingGotoTarget, errorMessage, debugLogRequestBody, debugLogRequestLatency, cxoneTenantId, cxoneAgentUid } = config; const { traceId, sessionId } = input; try { // Validate that both prompt and model ARN are provided if either is used. const isPromptTemplateUsed = ((prompt && prompt.trim().length > 0) || (modelArn && modelArn.trim().length > 0)); if (isPromptTemplateUsed && (!prompt || !modelArn)) { throw new errors_1.InvalidArgumentError("Both Prompt and Model ARN are required when using a custom prompt template."); } // Construct request body. const requestBody = { timestamp: new Date().toISOString(), meta: { tenantId: cxoneTenantId, contactNumber: (0, cxOneKnowledgeHubUtils_1.uuidToSafeInt64String)(sessionId), agentUid: cxoneAgentUid, provider: "Cognigy", }, query: { uid: crypto.randomUUID(), text: query, persona: queryPersona || undefined, language: queryLanguage || undefined, }, kbFiltering: { filters }, config: { kbAnswers: { promptEditorProfileId: promptEditorProfileId || undefined, answers: { enabled: true, maxWords: answersMaxWords !== null && answersMaxWords !== void 0 ? answersMaxWords : 100 }, // It was agreed to keep images disabled for now. images: { enabled: false, maxImages: 5 }, links: { enabled: true, maxLinks: linksMax !== null && linksMax !== void 0 ? linksMax : 5 } }, // It was agreed to keep process steps disabled for now. kbProcessSteps: { enabled: false, maxSteps: 5, maxTitleWords: 8, maxStepWords: 10 }, awsBedrock: { knowledgeHubId, awsBedrockKbId, maxKernels: maxKernels !== null && maxKernels !== void 0 ? maxKernels : 5, shareable: false, promptTemplate: isPromptTemplateUsed ? { textPromptTemplate: prompt, modelArn: modelArn } : undefined } } }; // Start measuring node latency. const startTime = debugLogRequestLatency ? Date.now() : 0; // Execute the request against the CXone Knowledgehub API. // A result means successful communication with the API, not necessarily that something was found. // An error is always thrown and handled by the catch block. const result = await api.executeCxOneKnowledgehubApiRequest(requestBody, nodeId); // We log the request body after the actual request to include any modifications made by the API function. if (debugLogRequestBody) { // Create a filtered copy of requestBody for logging, excluding sensitive fields const filteredRequestBody = Object.assign(Object.assign({}, requestBody), { kbFiltering: Object.assign(Object.assign({}, requestBody.kbFiltering), { conversationContextRefId: undefined }), config: Object.assign(Object.assign({}, requestBody.config), { awsBedrock: Object.assign(Object.assign({}, requestBody.config.awsBedrock), { awsBedrockKbId: undefined }) }) }); const debugMessage = `UI__DEBUG_MODE__CXONE_KNOWLEDGEHUB__REQUEST_BODY__LABEL: ${JSON.stringify(filteredRequestBody, null, 2)}`; (_a = api.logDebugMessage) === null || _a === void 0 ? void 0 : _a.call(api, debugMessage, "UI__DEBUG_MODE__CXONE_KNOWLEDGEHUB__REQUEST_BODY__HEADER"); } if (debugLogRequestLatency) { const llmLatencyMs = Date.now() - startTime; const debugMessage = `UI__DEBUG_MODE__CXONE_KNOWLEDGEHUB__REQEUEST_LATENCY__LABEL: ${llmLatencyMs}ms`; (_b = api.logDebugMessage) === null || _b === void 0 ? void 0 : _b.call(api, debugMessage, "UI__DEBUG_MODE__CXONE_KNOWLEDGEHUB__REQEUEST_LATENCY__HEADER"); } // Save result to input or context based on configuration. switch (storeLocation) { case "context": (_c = api.addToContext) === null || _c === void 0 ? void 0 : _c.call(api, contextKey, result, "simple"); break; default: api.addToInput(inputKey, result); } } catch (error) { const errorDetails = { name: error === null || error === void 0 ? void 0 : error.name, code: (error === null || error === void 0 ? void 0 : error.code) || (error === null || error === void 0 ? void 0 : error.httpStatusCode), message: error === null || error === void 0 ? void 0 : error.message, }; // Debug log to console. (_d = api.logDebugError) === null || _d === void 0 ? void 0 : _d.call(api, errorDetails, "UI__DEBUG_MODE__CXONE_KNOWLEDGEHUB__ERROR"); // Log to system if configured. if (logErrorToSystem) { (_e = api.log) === null || _e === void 0 ? void 0 : _e.call(api, "error", JSON.stringify(errorDetails)); } // Store error details either in input or context. switch (storeLocation) { case "context": (_f = api.addToContext) === null || _f === void 0 ? void 0 : _f.call(api, contextKey, errorDetails, "simple"); break; default: api.addToInput(inputKey, errorDetails); } // Handle the error based on the configured error handling approach. if (errorHandling === "continue") { if (errorMessage) { await ((_g = api.output) === null || _g === void 0 ? void 0 : _g.call(api, errorMessage, null)); } } else if (errorHandling === "goto") { if (!errorHandlingGotoTarget) { throw new errors_1.InvalidArgumentError("GoTo Target is required"); } const gotoParams = { cognigy, childConfigs: [], nodeId, config: { flowNode: { flow: errorHandlingGotoTarget.flow, node: errorHandlingGotoTarget.node, }, injectedText: input.text, injectedData: input.data, executionMode: "continue", absorbContext: false } }; await ((_h = logic_1.GO_TO.function) === null || _h === void 0 ? void 0 : _h.call(logic_1.GO_TO, gotoParams)); } else { throw new errors_1.InternalServerError(error === null || error === void 0 ? void 0 : error.message, { traceId }); } } } }); //# sourceMappingURL=cxOneKnowledgeHub.js.map