From eb2439b90ca09d1c6c0aaf3492b4fc3643d16650 Mon Sep 17 00:00:00 2001 From: onlyfeng Date: Thu, 20 Feb 2025 11:02:47 +0800 Subject: [PATCH] fix(KnowledgeBase): handle JSON parse failure by falling back to text file processing to avoid error display affecting normal progress indication --- src/main/loader/index.ts | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/loader/index.ts b/src/main/loader/index.ts index f23c58b1..2c6afa74 100644 --- a/src/main/loader/index.ts +++ b/src/main/loader/index.ts @@ -123,13 +123,22 @@ export async function addFileLoader( // JSON类型 if (['.json'].includes(file.ext)) { - const jsonObject = JSON.parse(fileContent) - const loaderReturn = await ragApplication.addLoader(new JsonLoader({ object: jsonObject })) - return { - entriesAdded: loaderReturn.entriesAdded, - uniqueId: loaderReturn.uniqueId, - uniqueIds: [loaderReturn.uniqueId], - loaderType: loaderReturn.loaderType + let jsonObject = {} + let jsonParsed = true + try { + jsonObject = JSON.parse(fileContent) + } catch (error) { + jsonParsed = false + Logger.warn('[KnowledgeBase] failed parsing json file, failling back to text processing:', file.path, error) + } + if (jsonParsed) { + const loaderReturn = await ragApplication.addLoader(new JsonLoader({ object: jsonObject })) + return { + entriesAdded: loaderReturn.entriesAdded, + uniqueId: loaderReturn.uniqueId, + uniqueIds: [loaderReturn.uniqueId], + loaderType: loaderReturn.loaderType + } } }