UNPKG

@cognigy/rest-api-client

Version:

Cognigy REST-Client

152 lines 5.93 kB
import { __awaiter } from "tslib"; /* Custom modules */ import { createNodeDescriptor } from "../../createNodeDescriptor"; import { InternalServerError } from "../../../errors"; const TOP_K_MIN_VALUE = 1; const TOP_K_MAX_VALUE = 10; /** * Node name: 'knowledgeSearch' * * Purpose: * Searches internal knowledge database for answers based on the user's input. */ export const KNOWLEDGE_SEARCH = createNodeDescriptor({ type: "knowledgeSearch", defaultLabel: "Knowledge Search", summary: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__SUMMARY", appearance: { showIcon: false }, constraints: { placement: {}, creatable: false, }, fields: [ { key: "topK", type: "number", label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__TOP_K__LABEL", description: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__TOP_K__DESCRIPTION", defaultValue: 3, params: { required: true, min: TOP_K_MIN_VALUE, max: TOP_K_MAX_VALUE } }, { key: "knowledgeStoreIds", type: "knowledgeStore", label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__KNOWLEDGE_STORE_IDS__LABEL", description: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__KNOWLEDGE_STORE_IDS__DESCRIPTION", }, { key: "storeLocation", type: "select", label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__STORE_LOCATION__LABEL", params: { options: [ { 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: "input" }, { key: "inputKey", type: "cognigyText", label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__INPUT_KEY__LABEL", description: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__INPUT_KEY__DESCRIPTION", defaultValue: "knowledgeSearch", condition: { key: "storeLocation", value: "input" } }, { key: "contextKey", type: "cognigyText", label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__CONTEXT_KEY__LABEL", description: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__CONTEXT_KEY__DESCRIPTION", defaultValue: "knowledgeSearch", condition: { key: "storeLocation", value: "context" } } ], sections: [ { key: "storage", label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__STORAGE__LABEL", defaultCollapsed: true, fields: ["storeLocation", "inputKey", "contextKey"] } ], form: [ { type: "field", key: "topK" }, { type: "field", key: "knowledgeStoreIds" }, { type: "section", key: "storage" } ], preview: { key: "text", type: "text" }, tags: ["knowledgeSearch"], function: (knowledgeSearchParams) => __awaiter(void 0, void 0, void 0, function* () { var _a; const { cognigy, config } = knowledgeSearchParams; const { input, api } = cognigy; const { topK, knowledgeStoreIds, storeLocation, contextKey, inputKey } = config; const { language, text, traceId } = input; if (!topK || topK < TOP_K_MIN_VALUE || topK > TOP_K_MAX_VALUE) { throw new Error(`Unable to perform knowledge search. topK value of "${topK}" is invalid. Must be between or equal to ${TOP_K_MIN_VALUE} and ${TOP_K_MAX_VALUE}.`); } if (storeLocation === "context" && !contextKey) { throw new Error("Unable to perform knowledge search. Context Key is missing."); } if (storeLocation === "input" && !inputKey) { throw new Error("Unable to perform knowledge search. Input Key is missing."); } const data = { language: language, query: text, topK: topK, traceId: traceId, disableSensitiveLogging: false }; // Add knowledgeStoreIds to data if (knowledgeStoreIds === null || knowledgeStoreIds === void 0 ? void 0 : knowledgeStoreIds.trim().length) { data.knowledgeStoreIds = (_a = knowledgeStoreIds === null || knowledgeStoreIds === void 0 ? void 0 : knowledgeStoreIds.split(",")) === null || _a === void 0 ? void 0 : _a.map(id => id === null || id === void 0 ? void 0 : id.trim()); } // Perform knowledge search const response = yield api.knowledgeSearch(data); // Handle possible errors if (response.status != "success") { const errorMessage = response.error ? response.error : "empty"; throw new InternalServerError(`Error while performing knowledge search. Remote returned error: ${errorMessage}`); } if (!response.data) { throw new InternalServerError("Error while performing knowledge search. Remote returned empty response."); } // Save result switch (storeLocation) { case "context": api.addToContext(contextKey, response.data, "simple"); break; case "input": input[inputKey] = response.data; break; default: throw new InternalServerError(`Error while performing knowledge search. Store location was invalid. Value was: ${storeLocation}`, { traceId: data.traceId }); } }) }); //# sourceMappingURL=knowledgeSearch.js.map