mongodb-rag-ingest
Version:
MongoDB Ingest CLI for the MongoDB Chatbot Framework.
97 lines • 4.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.doDeleteCommand = exports.doUpdatePagesCommand = exports.doInitPagesCommand = void 0;
const mongodb_rag_core_1 = require("mongodb-rag-core");
const withConfig_1 = require("../withConfig");
const commandModule = {
command: "pages <action>",
describe: "Manage pages data from sources",
builder(args) {
return args
.command({
command: "init",
describe: "Initialize page store",
builder: (updateArgs) => (0, withConfig_1.withConfigOptions)(updateArgs),
handler: (updateArgs) => (0, withConfig_1.withConfig)(exports.doInitPagesCommand, updateArgs),
})
.command({
command: "update",
describe: "Update pages data from sources",
builder: (updateArgs) => (0, withConfig_1.withConfigOptions)(updateArgs).option("source", {
string: true,
description: "A source name to load. If unspecified, loads all sources.",
}),
handler: (updateArgs) => (0, withConfig_1.withConfig)(exports.doUpdatePagesCommand, updateArgs),
})
.command({
command: "delete [permanent]",
describe: "Delete pages data from database",
builder: (deleteArgs) => (0, withConfig_1.withConfigOptions)(deleteArgs)
.option("source", {
string: true,
description: "A source name to delete. If unspecified, deletes all sources.",
})
.option("permanent", {
boolean: true,
description: "If true, permanently deletes the pages. If false or unspecified, marks the pages as deleted without removing them from the collection.",
}),
handler: (deleteArgs) => (0, withConfig_1.withConfig)(exports.doDeleteCommand, deleteArgs),
})
.demandCommand(1, "Specify an action for 'pages' command");
},
handler: (_args) => {
mongodb_rag_core_1.logger.error('Specify an action for "pages" command');
},
};
exports.default = commandModule;
const doInitPagesCommand = async ({ pageStore }) => {
if (!pageStore) {
throw new Error(`Failed to initialize page store.`);
}
await pageStore.init?.();
};
exports.doInitPagesCommand = doInitPagesCommand;
const doUpdatePagesCommand = async ({ pageStore, dataSources, concurrencyOptions }, { source }) => {
const requestedSources = new Set(Array.isArray(source) ? source : [source]);
const sources = source === undefined
? dataSources
: dataSources.filter(({ name }) => requestedSources.has(name));
if (sources.length === 0) {
throw new Error(`Request at least one valid source. Available sources:\n${dataSources
.map(({ name }) => `- ${name}`)
.join("\n")}`);
}
mongodb_rag_core_1.logger.info(`Loaded sources:\n${sources.map(({ name }) => `- ${name}`).join("\n")}`);
await (0, mongodb_rag_core_1.updatePages)({
sources,
pageStore,
concurrencyOptions: concurrencyOptions?.pages,
});
};
exports.doUpdatePagesCommand = doUpdatePagesCommand;
const doDeleteCommand = async ({ pageStore, dataSources }, { source, permanent }) => {
if (source === undefined) {
mongodb_rag_core_1.logger.info(`All sources to be ${permanent ? "permanently deleted" : "marked for deletion"}`);
await pageStore.deletePages({ permanent: permanent });
return;
}
const sourcesToDelete = new Set(Array.isArray(source) ? source : [source]);
const validSources = dataSources
.filter(({ name }) => sourcesToDelete.has(name))
.map(({ name }) => name);
const invalidSources = Array.from(sourcesToDelete).filter((source) => !validSources.includes(source));
if (invalidSources.length) {
throw new Error(`Delete failed because the following invalid sources were requested to be deleted:\n${invalidSources
.map((source) => `- ${source}`)
.join("\n")}\nRemove invalid sources from the list and try again.\nAvailable sources:\n${dataSources
.map(({ name }) => `- ${name}`)
.join("\n")}`);
}
mongodb_rag_core_1.logger.info(`Sources to be ${permanent ? "permanently deleted" : "marked for deletion"}:\n${validSources.map((source) => `- ${source}`).join("\n")}`);
await pageStore.deletePages({
dataSources: validSources,
permanent: permanent,
});
};
exports.doDeleteCommand = doDeleteCommand;
//# sourceMappingURL=pages.js.map