Skip to content

Commit 753e1e9

Browse files
Filter malformed post-processed docs from file picker (Mintplex-Labs#3853)
Filter malformed post-processed files from UI
1 parent de9d364 commit 753e1e9

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

server/utils/files/index.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ async function viewLocalFiles() {
6161
);
6262
filenames[cachefilename] = subfile;
6363
}
64-
const results = await Promise.all(filePromises).then((results) =>
65-
results.filter((i) => !!i)
66-
);
64+
const results = await Promise.all(filePromises)
65+
.then((results) => results.filter((i) => !!i)) // Remove null results
66+
.then((results) => results.filter((i) => hasRequiredMetadata(i))); // Remove invalid file structures
6767
subdocs.items.push(...results);
6868

6969
// Grab the pinned workspaces and watched documents for this folder's documents
@@ -433,6 +433,31 @@ async function fileToPickerData({
433433
};
434434
}
435435

436+
const REQUIRED_FILE_OBJECT_FIELDS = [
437+
"name",
438+
"type",
439+
"url",
440+
"title",
441+
"docAuthor",
442+
"description",
443+
"docSource",
444+
"chunkSource",
445+
"published",
446+
"wordCount",
447+
"token_count_estimate",
448+
];
449+
450+
/**
451+
* Checks if a given metadata object has all the required fields
452+
* @param {{name: string, type: string, url: string, title: string, docAuthor: string, description: string, docSource: string, chunkSource: string, published: string, wordCount: number, token_count_estimate: number}} metadata - The metadata object to check (fileToPickerData)
453+
* @returns {boolean} - Returns true if the metadata object has all the required fields, false otherwise
454+
*/
455+
function hasRequiredMetadata(metadata = {}) {
456+
return REQUIRED_FILE_OBJECT_FIELDS.every((field) =>
457+
metadata.hasOwnProperty(field)
458+
);
459+
}
460+
436461
module.exports = {
437462
findDocumentInDocuments,
438463
cachedVectorInformation,

0 commit comments

Comments
 (0)