From 391cb33ce5abda75f80366e1b97985274553ff2a Mon Sep 17 00:00:00 2001 From: ashwin271 Date: Fri, 10 Jan 2025 23:27:26 +0530 Subject: [PATCH] Implement requirement checks for Ollama server and model availability in VectorSearchPlugin --- main.ts | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/main.ts b/main.ts index 83f5138..a69b10c 100644 --- a/main.ts +++ b/main.ts @@ -35,13 +35,19 @@ export default class VectorSearchPlugin extends Plugin { vectorStore: Map = new Map(); async onload() { + // Version check if (this.compareVersions(this.app.version, MINIMUM_OBSIDIAN_VERSION) < 0) { new Notice(`Vector Search requires Obsidian ${MINIMUM_OBSIDIAN_VERSION} or higher`); return; } await this.loadSettings(); - await this.checkOllamaConnection(); + + // Check Ollama and model availability before enabling plugin features + const isReady = await this.checkRequirements(); + if (!isReady) { + return; // Don't load plugin features if requirements aren't met + } // Add a ribbon icon for rebuilding the vector index this.addRibbonIcon('refresh-cw', 'Rebuild Vector Index', async () => { @@ -88,6 +94,54 @@ export default class VectorSearchPlugin extends Plugin { return 0; } + private async checkRequirements(): Promise { + try { + // Check if Ollama is running + const ollamaResponse = await fetch(`${this.settings.ollamaURL}/api/version`, { + method: 'GET' + }); + + if (!ollamaResponse.ok) { + new Notice('Could not connect to Ollama server. Please ensure Ollama is installed and running.'); + console.error('[Vector Search] Ollama connection failed'); + return false; + } + + // Check if the model is available + const modelResponse = await fetch(`${this.settings.ollamaURL}/api/tags`, { + method: 'GET' + }); + + if (!modelResponse.ok) { + new Notice('Could not check available models. Please verify Ollama installation.'); + return false; + } + + const models = await modelResponse.json(); + const hasModel = models.models?.some((model: any) => + model.name === this.settings.modelName + ); + + if (!hasModel) { + new Notice(`Required model '${this.settings.modelName}' not found. Please run: ollama pull ${this.settings.modelName}`); + console.error('[Vector Search] Required model not installed'); + return false; + } + + return true; + + } catch (error) { + new Notice(` + Vector Search Plugin Requirements Not Met: + 1. Install Ollama from ollama.ai + 2. Start Ollama service + 3. Run: ollama pull ${this.settings.modelName} + `); + console.error('[Vector Search] Requirements check failed:', error); + return false; + } + } + private async checkOllamaConnection(): Promise { try { const response = await fetch(`${this.settings.ollamaURL}/api/embeddings`, {