2023-12-19 00:48:02 +01:00
|
|
|
const RepoLoader = require("./RepoLoader");
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
const { default: slugify } = require("slugify");
|
|
|
|
const { v4 } = require("uuid");
|
2024-07-23 21:23:51 +02:00
|
|
|
const { writeToServerDocuments } = require("../../../files");
|
|
|
|
const { tokenizeString } = require("../../../tokenizer");
|
2023-12-19 00:48:02 +01:00
|
|
|
|
2024-06-21 22:38:50 +02:00
|
|
|
/**
|
2024-07-23 21:23:51 +02:00
|
|
|
* Load in a Gitlab Repo recursively or just the top level if no PAT is provided
|
2024-06-21 22:38:50 +02:00
|
|
|
* @param {object} args - forwarded request body params
|
|
|
|
* @param {import("../../../middleware/setDataSigner").ResponseWithSigner} response - Express response object with encryptionWorker
|
|
|
|
* @returns
|
|
|
|
*/
|
2024-07-23 21:23:51 +02:00
|
|
|
async function loadGitlabRepo(args, response) {
|
2023-12-19 00:48:02 +01:00
|
|
|
const repo = new RepoLoader(args);
|
|
|
|
await repo.init();
|
|
|
|
|
|
|
|
if (!repo.ready)
|
|
|
|
return {
|
|
|
|
success: false,
|
2024-07-23 21:23:51 +02:00
|
|
|
reason: "Could not prepare Gitlab repo for loading! Check URL",
|
2023-12-19 00:48:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
console.log(
|
2024-07-23 21:23:51 +02:00
|
|
|
`-- Working GitLab ${repo.author}/${repo.project}:${repo.branch} --`
|
2023-12-19 00:48:02 +01:00
|
|
|
);
|
|
|
|
const docs = await repo.recursiveLoader();
|
|
|
|
if (!docs.length) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
reason: "No files were found for those settings.",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-07-23 21:23:51 +02:00
|
|
|
console.log(`[GitLab Loader]: Found ${docs.length} source files. Saving...`);
|
2023-12-19 00:48:02 +01:00
|
|
|
const outFolder = slugify(
|
|
|
|
`${repo.author}-${repo.project}-${repo.branch}-${v4().slice(0, 4)}`
|
|
|
|
).toLowerCase();
|
2024-05-02 23:03:10 +02:00
|
|
|
|
|
|
|
const outFolderPath =
|
|
|
|
process.env.NODE_ENV === "development"
|
|
|
|
? path.resolve(
|
|
|
|
__dirname,
|
2024-07-23 21:23:51 +02:00
|
|
|
`../../../../../server/storage/documents/${outFolder}`
|
2024-05-02 23:03:10 +02:00
|
|
|
)
|
|
|
|
: path.resolve(process.env.STORAGE_DIR, `documents/${outFolder}`);
|
|
|
|
|
|
|
|
if (!fs.existsSync(outFolderPath))
|
|
|
|
fs.mkdirSync(outFolderPath, { recursive: true });
|
2023-12-19 00:48:02 +01:00
|
|
|
|
|
|
|
for (const doc of docs) {
|
|
|
|
if (!doc.pageContent) continue;
|
|
|
|
const data = {
|
|
|
|
id: v4(),
|
2024-07-23 21:23:51 +02:00
|
|
|
url: "gitlab://" + doc.metadata.source,
|
2023-12-19 00:48:02 +01:00
|
|
|
title: doc.metadata.source,
|
|
|
|
docAuthor: repo.author,
|
|
|
|
description: "No description found.",
|
2024-02-13 23:11:57 +01:00
|
|
|
docSource: doc.metadata.source,
|
2024-06-21 22:38:50 +02:00
|
|
|
chunkSource: generateChunkSource(
|
|
|
|
repo,
|
|
|
|
doc,
|
|
|
|
response.locals.encryptionWorker
|
|
|
|
),
|
2023-12-19 00:48:02 +01:00
|
|
|
published: new Date().toLocaleString(),
|
|
|
|
wordCount: doc.pageContent.split(" ").length,
|
|
|
|
pageContent: doc.pageContent,
|
|
|
|
token_count_estimate: tokenizeString(doc.pageContent).length,
|
|
|
|
};
|
|
|
|
console.log(
|
2024-07-23 21:23:51 +02:00
|
|
|
`[GitLab Loader]: Saving ${doc.metadata.source} to ${outFolder}`
|
2023-12-19 00:48:02 +01:00
|
|
|
);
|
|
|
|
writeToServerDocuments(
|
|
|
|
data,
|
|
|
|
`${slugify(doc.metadata.source)}-${data.id}`,
|
|
|
|
outFolderPath
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
reason: null,
|
|
|
|
data: {
|
|
|
|
author: repo.author,
|
|
|
|
repo: repo.project,
|
2024-07-23 21:23:51 +02:00
|
|
|
projectId: repo.projectId,
|
2023-12-19 00:48:02 +01:00
|
|
|
branch: repo.branch,
|
|
|
|
files: docs.length,
|
|
|
|
destination: outFolder,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-07-23 21:23:51 +02:00
|
|
|
async function fetchGitlabFile({
|
2024-06-21 22:38:50 +02:00
|
|
|
repoUrl,
|
|
|
|
branch,
|
|
|
|
accessToken = null,
|
|
|
|
sourceFilePath,
|
|
|
|
}) {
|
|
|
|
const repo = new RepoLoader({
|
|
|
|
repo: repoUrl,
|
|
|
|
branch,
|
|
|
|
accessToken,
|
|
|
|
});
|
|
|
|
await repo.init();
|
|
|
|
|
|
|
|
if (!repo.ready)
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
content: null,
|
2024-07-23 21:23:51 +02:00
|
|
|
reason: "Could not prepare GitLab repo for loading! Check URL or PAT.",
|
2024-06-21 22:38:50 +02:00
|
|
|
};
|
|
|
|
console.log(
|
2024-07-23 21:23:51 +02:00
|
|
|
`-- Working GitLab ${repo.author}/${repo.project}:${repo.branch} file:${sourceFilePath} --`
|
2024-06-21 22:38:50 +02:00
|
|
|
);
|
|
|
|
const fileContent = await repo.fetchSingleFile(sourceFilePath);
|
|
|
|
if (!fileContent) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
reason: "Target file returned a null content response.",
|
|
|
|
content: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
reason: null,
|
|
|
|
content: fileContent,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateChunkSource(repo, doc, encryptionWorker) {
|
|
|
|
const payload = {
|
2024-07-23 21:23:51 +02:00
|
|
|
projectId: decodeURIComponent(repo.projectId),
|
2024-06-21 22:38:50 +02:00
|
|
|
branch: repo.branch,
|
|
|
|
path: doc.metadata.source,
|
|
|
|
pat: !!repo.accessToken ? repo.accessToken : null,
|
|
|
|
};
|
2024-07-23 21:23:51 +02:00
|
|
|
return `gitlab://${repo.repo}?payload=${encryptionWorker.encrypt(
|
2024-06-21 22:38:50 +02:00
|
|
|
JSON.stringify(payload)
|
|
|
|
)}`;
|
|
|
|
}
|
|
|
|
|
2024-07-23 21:23:51 +02:00
|
|
|
module.exports = { loadGitlabRepo, fetchGitlabFile };
|