Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setting to exclude copilot index in obsidian sync #908

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/VectorStoreManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ class VectorStoreManager {

constructor(app: App) {
this.app = app;

this.dbPath = this.getDbPath();
this.embeddingsManager = EmbeddingsManager.getInstance();

// Initialize the database asynchronously
Expand Down Expand Up @@ -89,8 +87,21 @@ class VectorStoreManager {
}
}

private getDbPath(): string {
return `${this.app.vault.configDir}/copilot-index-${this.getVaultIdentifier()}.json`;
private async getDbPath(): Promise<string> {
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) {
Expand Down Expand Up @@ -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))) {
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ export const DEFAULT_SETTINGS: CopilotSettings = {
qaInclusions: "",
chatNoteContextPath: "",
chatNoteContextTags: [],
enableIndexSync: true,
debug: false,
enableEncryption: false,
maxSourceChunks: 3,
Expand Down
10 changes: 8 additions & 2 deletions src/settings/components/QASettings.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -8,7 +9,6 @@ import {
TextAreaComponent,
ToggleComponent,
} from "./SettingBlocks";
import { updateSetting, useSettingsValue } from "@/settings/model";

const QASettings: React.FC = () => {
const settings = useSettingsValue();
Expand All @@ -23,7 +23,7 @@ const QASettings: React.FC = () => {
};

return (
<div>
<div className="copilot-settings-tab">
<h1>QA Settings</h1>
<p>
QA mode relies on a <em>local</em> vector index.
Expand Down Expand Up @@ -123,6 +123,12 @@ const QASettings: React.FC = () => {
value={settings.qaInclusions}
onChange={(value) => updateSetting("qaInclusions", value)}
/>
<ToggleComponent
name="Enable Obsidian Sync for Copilot index"
description="If enabled, the index will be stored in the .obsidian folder and synced with Obsidian Sync by default. If disabled, it will be stored in .copilot-index folder at vault root."
value={settings.enableIndexSync}
onChange={(value) => updateSetting("enableIndexSync", value)}
/>
<ToggleComponent
name="Disable index loading on mobile"
description="When enabled, Copilot index won't be loaded on mobile devices to save resources. Only chat mode will be available. Any existing index from desktop sync will be preserved. Uncheck to enable QA modes on mobile."
Expand Down
1 change: 1 addition & 0 deletions src/settings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface CopilotSettings {
indexVaultToVectorStore: string;
chatNoteContextPath: string;
chatNoteContextTags: string[];
enableIndexSync: boolean;
debug: boolean;
enableEncryption: boolean;
maxSourceChunks: number;
Expand Down
Loading