diff --git a/manifest.json b/manifest.json index 853bf78..ef54275 100644 --- a/manifest.json +++ b/manifest.json @@ -4,7 +4,7 @@ "description": "Interact with your privacy focused assistant, leveraging Ollama or OpenAI, making your second brain even smarter.", "author": "Leo310, nicobrauchtgit", "authorUrl": "https://github.com/nicobrauchtgit", - "version": "0.2.1", + "version": "0.2.2", "minAppVersion": "1.5.0", "isDesktopOnly": true } diff --git a/src/components/Chat/Input.svelte b/src/components/Chat/Input.svelte index 817bdda..576892a 100644 --- a/src/components/Chat/Input.svelte +++ b/src/components/Chat/Input.svelte @@ -1,7 +1,7 @@
-

Setup

diff --git a/src/components/Settings/Settings.svelte b/src/components/Settings/Settings.svelte index 2cf3265..0f729b6 100644 --- a/src/components/Settings/Settings.svelte +++ b/src/components/Settings/Settings.svelte @@ -1,7 +1,7 @@ @@ -279,10 +275,6 @@ {/if} {/if} - - - -
@@ -291,6 +283,11 @@ changeDocNum(parseInt(docNum))} /> + + + + $plugin.clearPluginData()} /> + diff --git a/src/controller/Messages.ts b/src/controller/Messages.ts index fc28f83..2714265 100644 --- a/src/controller/Messages.ts +++ b/src/controller/Messages.ts @@ -1,7 +1,7 @@ import { plugin as p, chatHistory as history, type ChatMessage, isEditing, chatInput, isEditingAssistantMessage } from '../store'; import { get } from 'svelte/store'; import { MarkdownRenderer, Notice, setIcon } from 'obsidian'; -import { runSecondBrainFromChat } from './runSecondBrain'; +import { canRunSecondBrain, runSecondBrain } from './runSecondBrain'; import { DEFAULT_SETTINGS } from '../main'; import { nanoid } from 'nanoid'; @@ -115,12 +115,13 @@ export const toClipboard = (messageText: string) => { }; export const redoGeneration = async (message: ChatMessage) => { + if (!canRunSecondBrain()) return; const plugin = get(p); const chatHistory = get(history); const targetIndex = chatHistory.indexOf(message) - 1; const userQuery = chatHistory[targetIndex]; history.set(chatHistory.slice(0, targetIndex)); - await runSecondBrainFromChat(plugin.data.isUsingRag, userQuery.content); + await runSecondBrain(plugin.data.isUsingRag, userQuery.content); }; export function editMessage(message: ChatMessage, textarea: HTMLTextAreaElement): string { diff --git a/src/controller/runSecondBrain.ts b/src/controller/runSecondBrain.ts index b2b1776..4fd637b 100644 --- a/src/controller/runSecondBrain.ts +++ b/src/controller/runSecondBrain.ts @@ -1,8 +1,18 @@ import { get } from 'svelte/store'; import { nanoid } from 'nanoid'; import { chatHistory as cH, isEditing, plugin as p, serializeChatHistory, papaState } from '../store'; +import { Notice } from 'obsidian'; -export async function runSecondBrainFromChat(isRAG: boolean, userQuery: string) { +export const canRunSecondBrain = () => { + if (get(papaState) === 'running') return new Notice('Please wait for the current query to finish', 4000) && false; + else if (get(papaState) === 'indexing' || get(papaState) === 'indexing-paused' || get(papaState) === 'loading') + return new Notice('Please wait for the indexing to finish', 4000) && false; + else if (get(papaState) === 'error') return new Notice('Please wait for the error to resolve', 4000) && false; + else if (get(papaState) !== 'idle') return new Notice('Please initialize your Smart Second Brain first', 4000) && false; + return true; +}; + +export async function runSecondBrain(isRAG: boolean, userQuery: string) { papaState.set('running'); const plugin = get(p); @@ -23,7 +33,12 @@ export async function runSecondBrainFromChat(isRAG: boolean, userQuery: string) for await (const response of responseStream) { cH.set([...chatHistory, { role: 'Assistant', content: response.content, id: nanoid() }]); - if (get(papaState) === 'running-stopped') break; + if (get(papaState) === 'running-stopped') { + if (response.status !== 'Generating') cH.set([...chatHistory, { role: 'Assistant', content: 'Stopped', id: nanoid() }]); + papaState.set('idle'); + plugin.chatView.save(); + return; // when used break it somehow returns the whole function + } } plugin.chatView.save(); papaState.set('idle'); diff --git a/src/main.ts b/src/main.ts index f7c3a7c..f61f926 100644 --- a/src/main.ts +++ b/src/main.ts @@ -108,6 +108,8 @@ export default class SecondBrainPlugin extends Plugin { async initPapa() { if (get(papaState) === 'running') return new Notice('Smart Second Brain is still running.', 4000); + else if (get(papaState) === 'indexing' || get(papaState) === 'indexing-paused' || get(papaState) === 'loading') + return new Notice('Please wait for the indexing to finish', 4000); else if (this.data.isIncognitoMode && !(await isOllamaRunning())) return new Notice('Please make sure Ollama is running before initializing Smart Second Brain.', 4000); else if (!this.data.isIncognitoMode && !(await isAPIKeyValid())) @@ -162,6 +164,7 @@ export default class SecondBrainPlugin extends Plugin { this.saveVectorStoreData(); if (get(papaIndexingProgress) === 100) { new Notice('Smart Second Brain initialized.', 2000); + papaIndexingProgress.set(0); papaState.set('idle'); } } @@ -320,6 +323,14 @@ export default class SecondBrainPlugin extends Plugin { await this.activateView(newChatFile); } + async clearPluginData() { + await this.saveData({}); + const files = (await this.app.vault.adapter.list(normalizePath(this.manifest.dir))).files; + for (const file of files) { + if (file.endsWith('vector-store.bin')) await this.app.vault.adapter.remove(file); + } + } + registerMonkeyPatches() { // eslint-disable-next-line @typescript-eslint/no-this-alias const self = this; diff --git a/src/store.ts b/src/store.ts index 3279b07..2116bf2 100644 --- a/src/store.ts +++ b/src/store.ts @@ -15,7 +15,7 @@ export const chatInput = writable(''); export const isOnboarded = writable(false); export const isChatInSidebar = writable(true); -type PapaState = 'idle' | 'loading' | 'indexing' | 'indexing-paused' | 'running' | 'running-stopped' | 'error' | 'uninitialized'; +export type PapaState = 'idle' | 'loading' | 'indexing' | 'indexing-paused' | 'running' | 'running-stopped' | 'error' | 'uninitialized' | 'mode-changed'; export const papaState = writable('uninitialized'); export const papaIndexingProgress = writable(0);