From d3bceb02ac28a255970ef01f0c7563f64ea43f84 Mon Sep 17 00:00:00 2001 From: Logan Yang Date: Sat, 7 Dec 2024 20:42:11 -0800 Subject: [PATCH] Add setting to exclude copilot index in obsidian sync --- src/VectorStoreManager.ts | 28 +++++++++++++++++++++----- src/constants.ts | 1 + src/settings/components/QASettings.tsx | 10 +++++++-- src/settings/model.ts | 1 + 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/VectorStoreManager.ts b/src/VectorStoreManager.ts index 83a0413e..255cfcda 100644 --- a/src/VectorStoreManager.ts +++ b/src/VectorStoreManager.ts @@ -35,8 +35,6 @@ class VectorStoreManager { constructor(app: App) { this.app = app; - - this.dbPath = this.getDbPath(); this.embeddingsManager = EmbeddingsManager.getInstance(); // Initialize the database asynchronously @@ -89,8 +87,21 @@ class VectorStoreManager { } } - private getDbPath(): string { - return `${this.app.vault.configDir}/copilot-index-${this.getVaultIdentifier()}.json`; + private async getDbPath(): Promise { + if (getSettings().enableIndexSync) { + return `${this.app.vault.configDir}/copilot-index-${this.getVaultIdentifier()}.json`; + } + + // Get vault root path (parent of .obsidian directory) + const vaultRoot = this.app.vault.getRoot().path; + const indexDir = `${vaultRoot}/.copilot-index`; + + // Create .copilot-index directory if it doesn't exist + if (!(await this.app.vault.adapter.exists(indexDir))) { + await this.app.vault.adapter.mkdir(indexDir); + } + + return `${indexDir}/copilot-index-${this.getVaultIdentifier()}.json`; } private createDynamicSchema(vectorLength: number) { @@ -118,7 +129,7 @@ class VectorStoreManager { return; } - this.dbPath = this.getDbPath(); + this.dbPath = await this.getDbPath(); // Ensure the config directory exists const configDir = this.app.vault.configDir; if (!(await this.app.vault.adapter.exists(configDir))) { @@ -244,6 +255,13 @@ class VectorStoreManager { ...rawData, }; + // Ensure the directory exists before saving + const dbDir = this.dbPath.substring(0, this.dbPath.lastIndexOf("/")); + + if (!(await this.app.vault.adapter.exists(dbDir))) { + await this.app.vault.adapter.mkdir(dbDir); + } + // Use requestIdleCallback if available, otherwise use setTimeout const saveOperation = async () => { try { diff --git a/src/constants.ts b/src/constants.ts index e8ab2ab4..15682e4a 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -251,6 +251,7 @@ export const DEFAULT_SETTINGS: CopilotSettings = { qaInclusions: "", chatNoteContextPath: "", chatNoteContextTags: [], + enableIndexSync: true, debug: false, enableEncryption: false, maxSourceChunks: 3, diff --git a/src/settings/components/QASettings.tsx b/src/settings/components/QASettings.tsx index 756ac682..086efe0d 100644 --- a/src/settings/components/QASettings.tsx +++ b/src/settings/components/QASettings.tsx @@ -1,5 +1,6 @@ import { CustomModel } from "@/aiParams"; import { EmbeddingModelProviders, VAULT_VECTOR_STORE_STRATEGIES } from "@/constants"; +import { updateSetting, useSettingsValue } from "@/settings/model"; import React from "react"; import { DropdownComponent, @@ -8,7 +9,6 @@ import { TextAreaComponent, ToggleComponent, } from "./SettingBlocks"; -import { updateSetting, useSettingsValue } from "@/settings/model"; const QASettings: React.FC = () => { const settings = useSettingsValue(); @@ -23,7 +23,7 @@ const QASettings: React.FC = () => { }; return ( -
+

QA Settings

QA mode relies on a local vector index. @@ -123,6 +123,12 @@ const QASettings: React.FC = () => { value={settings.qaInclusions} onChange={(value) => updateSetting("qaInclusions", value)} /> + updateSetting("enableIndexSync", value)} + />