UNPKG

@cognigy/rest-api-client

Version:

Cognigy REST-Client

175 lines 9.18 kB
import { __awaiter } from "tslib"; /* Node modules */ import * as crypto from "crypto"; /* Custom modules */ import { createNodeDescriptor } from "../../../createNodeDescriptor"; import { GO_TO } from "../../logic"; import { fields, sections, form } from "../fields/cxOneKnowledgeHubForm"; import { uuidToSafeInt64String } from "../libs/cxOneKnowledgeHubUtils"; import { InternalServerError, InvalidArgumentError } from "../../../../errors"; export const CXONE_KNOWLEDGEHUB_NODE_TYPE = "cxOneKnowledgeHub"; export const CXONE_KNOWLEDGEHUB = createNodeDescriptor({ type: CXONE_KNOWLEDGEHUB_NODE_TYPE, defaultLabel: "UI__NODE_EDITOR__CXONE_KNOWLEDGEHUB__LABEL", summary: "UI__NODE_EDITOR__CXONE_KNOWLEDGEHUB__DESCRIPTION", behavior: { entrypoint: true }, fields, sections, form, appearance: { color: "#3694FD", }, preview: { key: "query", type: "text" }, tags: ["nice", "cxone"], function: ({ cognigy, config, nodeId }) => __awaiter(void 0, void 0, void 0, function* () { 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 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: 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 = yield 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) { yield ((_g = api.output) === null || _g === void 0 ? void 0 : _g.call(api, errorMessage, null)); } } else if (errorHandling === "goto") { if (!errorHandlingGotoTarget) { throw new 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 } }; yield ((_h = GO_TO.function) === null || _h === void 0 ? void 0 : _h.call(GO_TO, gotoParams)); } else { throw new InternalServerError(error === null || error === void 0 ? void 0 : error.message, { traceId }); } } }) }); //# sourceMappingURL=cxOneKnowledgeHub.js.map