@jss-rule-engine/workflow
Version:
140 lines • 9.44 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ragItemsIndexingHandler = void 0;
var core_1 = require("@jss-rule-engine/core");
var indexSitecoreItemsToRagDb_1 = require("../db/indexSitecoreItemsToRagDb");
function ragItemsIndexingHandler(_a) {
var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
var itemId = _a.itemId, registerRuleEngine = _a.registerRuleEngine;
return __awaiter(this, void 0, void 0, function () {
var sitecoreEdgeUrl, sitecoreApiKey, itemProvider, configItemId, ruleEngine, ragIndexItems, _i, _o, ragIndex, ragIndexData, fields, indexingRule, indexedFieldIdsRaw, enabled, source, indexedFieldIds, error_1;
return __generator(this, function (_p) {
switch (_p.label) {
case 0:
_p.trys.push([0, 7, , 8]);
sitecoreEdgeUrl = process.env.EDGE_QL_ENDPOINT || '';
sitecoreApiKey = process.env.SITECORE_API_KEY || '';
itemProvider = new core_1.GraphQLItemProvider({
graphEndpoint: sitecoreEdgeUrl,
apiKey: sitecoreApiKey,
});
configItemId = process.env.RAG_CONFIG_ITEMID;
if (!configItemId) {
console.warn("RAG_CONFIG_ITEMID is not configured. Skipping RAG indexing.");
return [2 /*return*/, {
success: false,
errorMessage: "RAG_CONFIG_ITEMID is not configured. Skipping RAG indexing.",
errorCode: 400
}];
}
ruleEngine = new core_1.JssRuleEngine();
if (registerRuleEngine) {
registerRuleEngine(ruleEngine);
}
return [4 /*yield*/, itemProvider.getItemDescendantsInfoById(configItemId)];
case 1:
ragIndexItems = _p.sent();
if (!ragIndexItems || !((_b = ragIndexItems === null || ragIndexItems === void 0 ? void 0 : ragIndexItems.item) === null || _b === void 0 ? void 0 : _b.children) || ((_d = (_c = ragIndexItems === null || ragIndexItems === void 0 ? void 0 : ragIndexItems.item) === null || _c === void 0 ? void 0 : _c.children) === null || _d === void 0 ? void 0 : _d.total) === 0) {
console.warn("No RAG Index items found in config. Skipping RAG indexing.");
return [2 /*return*/, {
success: false,
errorMessage: "No RAG Index items found in config. Skipping RAG indexing.",
errorCode: 400
}];
}
_i = 0, _o = (_f = (_e = ragIndexItems === null || ragIndexItems === void 0 ? void 0 : ragIndexItems.item) === null || _e === void 0 ? void 0 : _e.children) === null || _f === void 0 ? void 0 : _f.results;
_p.label = 2;
case 2:
if (!(_i < _o.length)) return [3 /*break*/, 6];
ragIndex = _o[_i];
return [4 /*yield*/, itemProvider.getItemById(ragIndex.id)];
case 3:
ragIndexData = _p.sent();
console.log('RAG Index data - ', ragIndexData);
if (!ragIndexData || !((_g = ragIndexData === null || ragIndexData === void 0 ? void 0 : ragIndexData.item) === null || _g === void 0 ? void 0 : _g.fields)) {
console.warn("RAG Index item ".concat(ragIndex.id, " has no fields. Skipping."));
return [3 /*break*/, 5];
}
fields = (_h = ragIndexData === null || ragIndexData === void 0 ? void 0 : ragIndexData.item) === null || _h === void 0 ? void 0 : _h.fields;
console.log("Index fields - ", fields);
indexingRule = (_j = fields === null || fields === void 0 ? void 0 : fields.find(function (x) { return x.name == "IndexingRule"; })) === null || _j === void 0 ? void 0 : _j.value;
indexedFieldIdsRaw = ((_k = fields === null || fields === void 0 ? void 0 : fields.find(function (x) { return x.name == "IndexedFieldIDs"; })) === null || _k === void 0 ? void 0 : _k.value) || '';
enabled = ((_l = fields === null || fields === void 0 ? void 0 : fields.find(function (x) { return x.name == "Enabled"; })) === null || _l === void 0 ? void 0 : _l.value) == "1";
source = (_m = fields === null || fields === void 0 ? void 0 : fields.find(function (x) { return x.name == "Source"; })) === null || _m === void 0 ? void 0 : _m.value;
console.log("Processing RAG Index: ".concat(ragIndex.id));
console.log("Indexing rule:", indexingRule, indexedFieldIdsRaw, enabled, source);
indexedFieldIds = indexedFieldIdsRaw
.split(',')
.map(function (id) { return id.trim(); })
.filter(function (id) { return !!id; });
console.log("Indexed field IDs: ".concat(indexedFieldIds.join(', ')));
// Index the target item using this RAG Index configuration
return [4 /*yield*/, (0, indexSitecoreItemsToRagDb_1.indexSitecoreItem)({
itemId: itemId,
itemProvider: itemProvider,
indexId: ragIndex.id,
indexSource: source,
indexingRule: indexingRule,
indexedFieldIds: indexedFieldIds,
ruleEngine: ruleEngine
})];
case 4:
// Index the target item using this RAG Index configuration
_p.sent();
_p.label = 5;
case 5:
_i++;
return [3 /*break*/, 2];
case 6: return [2 /*return*/, {
success: true
}];
case 7:
error_1 = _p.sent();
return [2 /*return*/, {
success: false,
errorCode: 500,
errorMessage: error_1 === null || error_1 === void 0 ? void 0 : error_1.toString()
}];
case 8: return [2 /*return*/];
}
});
});
}
exports.ragItemsIndexingHandler = ragItemsIndexingHandler;
//# sourceMappingURL=ragItemsIndexingHandler.js.map