@cognigy/rest-api-client
Version:
Cognigy REST-Client
292 lines • 12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FUZZY_SEARCH = void 0;
const createNodeDescriptor_1 = require("../../createNodeDescriptor");
const objectSizeValidator_1 = require("../../../helper/objectSizeValidator");
const getFuzzySearchMaxObjectSizeInBytes = () => {
return parseInt(process.env.FUZZYSEARCH_MAX_OBJECT_SIZE) || 524288;
};
exports.FUZZY_SEARCH = (0, createNodeDescriptor_1.createNodeDescriptor)({
type: "fuzzySearch",
defaultLabel: "Fuzzy Search",
summary: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__SUMMARY",
preview: {
key: "searchPattern",
type: "text"
},
tags: ["ai", "nlu", "fuzzy", "search"],
fields: [
{
key: "searchPattern",
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__SEARCH_PATTERN__LABEL",
description: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__SEARCH_PATTERN__DESCRIPTION",
type: "cognigyText",
params: {
required: true
}
},
{
key: "items",
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__ITEMS__LABEL",
description: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__ITEMS__DESCRIPTION",
type: "json",
params: {
required: true
},
defaultValue: `[
"apple",
"orange",
"banana",
"pear"
]`
},
{
key: "isCaseSensitive",
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__IN_CASE_SENSITIVE__LABEL",
defaultValue: false,
type: "toggle",
description: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__IN_CASE_SENSITIVE__DESCRIPTION"
},
{
key: "includeScore",
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__INCLUDE_STORE__LABEL",
defaultValue: true,
type: "toggle",
description: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__INCLUDE_STORE__DESCRIPTION"
},
{
key: "includeMatches",
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__INCLUDE_MATCHES__LABEL",
defaultValue: false,
type: "toggle",
description: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__INCLUDE_MATCHES__DESCRIPTION"
},
{
key: "minMatchCharLength",
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__MIN_MATCH_CHAR_LENGTH__LABEL",
defaultValue: 1,
type: "number",
description: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__MIN_MATCH_CHAR_LENGTH__DESCRIPTION",
params: {
min: 1,
max: 100
}
},
{
key: "shouldSort",
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__SHOULD_SORT__LABEL",
defaultValue: true,
type: "toggle",
description: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__SHOULD_SORT__DESCRIPTION"
},
{
key: "findAllMatches",
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__FIND_ALL_MATCHES__LABEL",
defaultValue: true,
type: "toggle",
description: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__FIND_ALL_MATCHES__DESCRIPTION"
},
{
key: "location",
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__LOCATION__LABEL",
defaultValue: 0,
type: "number",
description: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__LOCATION__DESCRIPTION",
params: {
min: 0,
max: 100
}
},
{
key: "threshold",
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__THRESHOLD__LABEL",
defaultValue: 0.1,
type: "slider",
description: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__THRESHOLD__DESCRIPTION",
params: {
min: 0,
max: 1,
step: 0.1
}
},
{
key: "distance",
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__DISTANCE__LABEL",
defaultValue: 100,
type: "number",
description: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__DISTANCE__DESCRIPTION",
params: {
min: 0,
max: 1000
}
},
{
key: "ignoreLocation",
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__IGNORE_LOCATION__LABEL",
defaultValue: true,
type: "toggle",
description: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__IGNORE_LOCATION__DESCRIPTION"
},
{
key: "storeLocation",
type: "select",
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__STORE_LOCATION__LABEL",
params: {
options: [
{
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__STORE_LOCATION__OPTIONS__INPUT__LABEL",
value: "input"
},
{
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__STORE_LOCATION__OPTIONS__CONTEXT__LABEL",
value: "context"
}
],
required: true
},
defaultValue: "input"
},
{
key: "inputKey",
type: "cognigyText",
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__INPUT_KEY__LABEL",
defaultValue: "search",
condition: {
key: "storeLocation",
value: "input"
}
},
{
key: "contextKey",
type: "cognigyText",
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__FIELDS__CONTEXT_KEY__LABEL",
defaultValue: "search",
condition: {
key: "storeLocation",
value: "context"
}
}
],
sections: [
{
key: "storageOption",
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__SECTIONS__STORAGE_OPTION__LABEL",
defaultCollapsed: true,
fields: [
"storeLocation",
"inputKey",
"contextKey"
]
},
{
key: "basicOptions",
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__SECTIONS__BASIC_OPTIONS__LABEL",
defaultCollapsed: true,
fields: [
"isCaseSensitive",
"includeScore",
"includeMatches",
"minMatchCharLength",
"shouldSort",
"findAllMatches",
]
},
{
key: "fuzzyOptions",
label: "UI__NODE_EDITOR__NLU__FUZZY_SEARCH__SECTIONS__FUZZY_OPTIONS__LABEL",
defaultCollapsed: true,
fields: [
"location",
"threshold",
"distance",
"ignoreLocation",
]
}
],
form: [
{ type: "field", key: "searchPattern" },
{ type: "field", key: "items" },
{ type: "section", key: "basicOptions" },
{ type: "section", key: "fuzzyOptions" },
{ type: "section", key: "storageOption" }
],
function: async ({ cognigy, config }) => {
var _a, _b, _c, _d, _e, _f, _g;
const { api, input } = cognigy;
const { searchPattern, items, distance, findAllMatches, ignoreLocation, includeMatches, includeScore, isCaseSensitive, location, minMatchCharLength, shouldSort, threshold, storeLocation, inputKey, contextKey } = config;
if (!searchPattern)
throw new Error("No search pattern provided.");
if (!items)
throw new Error("No source data provided.");
try {
// reverse threshold as fuse requires it inverse (0 = perfect match, 1 = matches everything)
const newThreshold = 1 - threshold;
const options = {
distance,
findAllMatches,
ignoreLocation,
includeMatches,
includeScore,
isCaseSensitive,
location,
minMatchCharLength,
shouldSort,
threshold: newThreshold,
};
const validObjectSize = (0, objectSizeValidator_1.validateObjectSize)(items, getFuzzySearchMaxObjectSizeInBytes());
let result = null;
if (validObjectSize.valid) {
result = api.fuseSearch(items, options, searchPattern);
// reverse scores from fuse's 0 = perfect match to Cognigy's 1 = perfect match.
if (includeScore && result && Array.isArray(result) && result.length > 0) {
result.forEach((item) => {
if (item.score || item.score === 0) {
item.score = 1 - item.score;
}
});
}
if (result && result.length > 0 && ((_a = result === null || result === void 0 ? void 0 : result[0]) === null || _a === void 0 ? void 0 : _a.item) && (input.endpointType === "adminconsole" || api.getMetadata().isFollowSessionActive)) {
let debugMessage = `Found ${result.length} match${result.length > 1 ? 'es' : ''}.<br><br>`;
if (result.length > 3) {
debugMessage += `Top 3:<br>`;
}
if (result.length >= 1 && ((_b = result === null || result === void 0 ? void 0 : result[0]) === null || _b === void 0 ? void 0 : _b.item) && ((_c = result === null || result === void 0 ? void 0 : result[0]) === null || _c === void 0 ? void 0 : _c.score)) {
debugMessage += `1. ${result[0].item} (score: ${result[0].score})<br>`;
}
if (result.length >= 2 && ((_d = result === null || result === void 0 ? void 0 : result[1]) === null || _d === void 0 ? void 0 : _d.item) && ((_e = result === null || result === void 0 ? void 0 : result[1]) === null || _e === void 0 ? void 0 : _e.score)) {
debugMessage += `2. ${result[1].item} (score: ${result[1].score})<br>`;
}
if (result.length >= 3 && ((_f = result === null || result === void 0 ? void 0 : result[2]) === null || _f === void 0 ? void 0 : _f.item) && ((_g = result === null || result === void 0 ? void 0 : result[2]) === null || _g === void 0 ? void 0 : _g.score)) {
debugMessage += `3. ${result[2].item} (score: ${result[2].score})`;
}
api.logDebugMessage(debugMessage, "Result Found");
}
else {
api.logDebugMessage("UI__DEBUG_MODE__FUZZY_SEARCH__NO_RESULTS", "No Results");
}
}
else {
result = {
"error": `Source data file size limit (${getFuzzySearchMaxObjectSizeInBytes()} bytes) exceeded`
};
api.logDebugError(`Source data file size limit (${getFuzzySearchMaxObjectSizeInBytes()} bytes) exceeded`, "Size Limit Exceeded");
}
if (storeLocation === "context") {
api.addToContext(contextKey, result, "simple");
}
else {
api.addToInput(inputKey, result);
}
}
catch (error) {
if (storeLocation === "context") {
api.addToContext(contextKey, error, "simple");
}
else {
api.addToInput(inputKey, error);
}
api.logDebugError(error.message, "Error");
}
}
});
//# sourceMappingURL=fuzzySearch.js.map