You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.0 KiB

11 months ago
  1. const fs = require("fs");
  2. const path = require("path");
  3. const documentsPath =
  4. process.env.NODE_ENV === "development"
  5. ? path.resolve(__dirname, `../../storage/documents`)
  6. : path.resolve(process.env.STORAGE_DIR, `documents`);
  7. class DocumentManager {
  8. constructor({ workspace = null, maxTokens = null }) {
  9. this.workspace = workspace;
  10. this.maxTokens = maxTokens || Number.POSITIVE_INFINITY;
  11. this.documentStoragePath = documentsPath;
  12. }
  13. log(text, ...args) {
  14. console.log(`\x1b[36m[DocumentManager]\x1b[0m ${text}`, ...args);
  15. }
  16. async pinnedDocuments() {
  17. if (!this.workspace) return [];
  18. const { Document } = require("../../models/documents");
  19. return await Document.where({
  20. workspaceId: Number(this.workspace.id),
  21. pinned: true,
  22. });
  23. }
  24. async pinnedDocs() {
  25. if (!this.workspace) return [];
  26. const docPaths = (await this.pinnedDocuments()).map((doc) => doc.docpath);
  27. if (docPaths.length === 0) return [];
  28. let tokens = 0;
  29. const pinnedDocs = [];
  30. for await (const docPath of docPaths) {
  31. try {
  32. const filePath = path.resolve(this.documentStoragePath, docPath);
  33. const data = JSON.parse(
  34. fs.readFileSync(filePath, { encoding: "utf-8" })
  35. );
  36. if (
  37. !data.hasOwnProperty("pageContent") ||
  38. !data.hasOwnProperty("token_count_estimate")
  39. ) {
  40. this.log(
  41. `Skipping document - Could not find page content or token_count_estimate in pinned source.`
  42. );
  43. continue;
  44. }
  45. if (tokens >= this.maxTokens) {
  46. this.log(
  47. `Skipping document - Token limit of ${this.maxTokens} has already been exceeded by pinned documents.`
  48. );
  49. continue;
  50. }
  51. pinnedDocs.push(data);
  52. tokens += data.token_count_estimate || 0;
  53. } catch {}
  54. }
  55. this.log(
  56. `Found ${pinnedDocs.length} pinned sources - prepending to content with ~${tokens} tokens of content.`
  57. );
  58. return pinnedDocs;
  59. }
  60. }
  61. module.exports.DocumentManager = DocumentManager;