UNPKG

mongodb-rag-ingest

Version:

MongoDB Ingest CLI for the MongoDB Chatbot Framework.

98 lines 3.72 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.withConfigOptions = exports.withConfig = exports.loadConfig = void 0; const path_1 = __importDefault(require("path")); const mongodb_rag_core_1 = require("mongodb-rag-core"); const loadConfig = async ({ config: configPathIn, }) => { const path = path_1.default.resolve(configPathIn === undefined ? "ingest.config.cjs" : configPathIn); const maybePartialConfig = (await import(path)).default; const partialConfig = (maybePartialConfig.default ?? maybePartialConfig); const missingProperties = []; const config = { ...partialConfig, dataSources: checkRequiredProperty(partialConfig, "dataSources", missingProperties), embeddedContentStore: checkRequiredProperty(partialConfig, "embeddedContentStore", missingProperties), embedder: checkRequiredProperty(partialConfig, "embedder", missingProperties), ingestMetaStore: checkRequiredProperty(partialConfig, "ingestMetaStore", missingProperties), pageStore: checkRequiredProperty(partialConfig, "pageStore", missingProperties), }; if (missingProperties.length !== 0) { throw new Error(`Config is missing the following properties: ${missingProperties.join(", ")}`); } return config; }; exports.loadConfig = loadConfig; const withConfig = async (action, args) => { const config = await (0, exports.loadConfig)(args); const [resolvedConfig, cleanup] = await resolveConfig(config); try { return await action(resolvedConfig, args); } finally { await Promise.all(cleanup.map(async (close) => { try { await close(); } catch (error) { mongodb_rag_core_1.logger.error(`Cleanup failed: ${error.message}`); } })); } }; exports.withConfig = withConfig; /** Apply config options to CLI command. */ const withConfigOptions = (args) => { return args.option("config", { string: true, description: "Path to config JS file.", }); }; exports.withConfigOptions = withConfigOptions; /** Resolve any promises in the config object. */ const resolveConfig = async (config) => { const cleanup = []; try { return [ Object.fromEntries(await Promise.all(Object.entries(config).map(async ([k, v]) => { const resolved = await resolve(v); const closeable = resolved; if (closeable?.close !== undefined) { // Save cleanup so that any constructed instances can be cleaned up // if subsequent construction fails cleanup.push(async () => { closeable.close && (await closeable.close()); }); } return [k, resolved]; }))), cleanup, ]; } catch (error) { await Promise.all(cleanup.map((close) => close())); throw error; } }; const resolve = async (v) => typeof v === "function" ? v() : v; /** Asserts that the given property is defined in the given object and returns that value as a definitely not undefined type. */ function checkRequiredProperty(object, k, missingProperties) { const value = object[k]; if (value === undefined) { missingProperties.push(k.toString()); // Hack: this is an invalid value. The caller MUST check the errors return undefined; } return value; } //# sourceMappingURL=withConfig.js.map