From 0721a849000ee20191612cf4e4301d64fa245b52 Mon Sep 17 00:00:00 2001
From: Marcus Schiesser <mail@marcusschiesser.de>
Date: Tue, 4 Jun 2024 19:26:16 +0200
Subject: [PATCH] fix: ignore empty vector store (#861)

---
 .../src/storage/vectorStore/SimpleVectorStore.ts   | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/packages/core/src/storage/vectorStore/SimpleVectorStore.ts b/packages/core/src/storage/vectorStore/SimpleVectorStore.ts
index 35e801e46..9676806ab 100644
--- a/packages/core/src/storage/vectorStore/SimpleVectorStore.ts
+++ b/packages/core/src/storage/vectorStore/SimpleVectorStore.ts
@@ -151,13 +151,20 @@ export class SimpleVectorStore
 
   async persist(
     persistPath: string = path.join(DEFAULT_PERSIST_DIR, "vector_store.json"),
+  ): Promise<void> {
+    await SimpleVectorStore.persistData(persistPath, this.data);
+  }
+
+  protected static async persistData(
+    persistPath: string,
+    data: SimpleVectorStoreData,
   ): Promise<void> {
     const dirPath = path.dirname(persistPath);
     if (!(await exists(dirPath))) {
       await fs.mkdir(dirPath);
     }
 
-    await fs.writeFile(persistPath, JSON.stringify(this.data));
+    await fs.writeFile(persistPath, JSON.stringify(data));
   }
 
   static async fromPersistPath(
@@ -177,6 +184,11 @@ export class SimpleVectorStore
       console.error(
         `No valid data found at path: ${persistPath} starting new store.`,
       );
+      // persist empty data, to ignore this error in the future
+      await SimpleVectorStore.persistData(
+        persistPath,
+        new SimpleVectorStoreData(),
+      );
     }
 
     const data = new SimpleVectorStoreData();
-- 
GitLab