From adbe6045cf91bb973348c77ffaa7234ec3e2ea87 Mon Sep 17 00:00:00 2001 From: Andrew Beal Date: Fri, 3 Oct 2025 22:14:30 +0100 Subject: [PATCH] Implement conversation reset functionality and file deletion in ConversationFileSystemService --- AIClasses/Gemini/Gemini.ts | 1 - Components/ChatWindow.svelte | 6 ++++++ Components/TopBar.svelte | 11 ++++++++--- Services/ConversationFileSystemService.ts | 14 ++++++++++++++ Services/FileSystemService.ts | 16 ++++++++++++++++ Stores/conversationStore.ts | 13 +++++++++++++ 6 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 Stores/conversationStore.ts diff --git a/AIClasses/Gemini/Gemini.ts b/AIClasses/Gemini/Gemini.ts index 95ff7c7..fa20c6c 100644 --- a/AIClasses/Gemini/Gemini.ts +++ b/AIClasses/Gemini/Gemini.ts @@ -6,7 +6,6 @@ import type { GeminiActionDefinitions } from "Actioner/Gemini/GeminiActionDefini import { create_file } from "Actioner/Actions"; import type { IAIClass } from "AIClasses/IAIClass"; import type { IPrompt } from "AIClasses/IPrompt"; -import type { Part } from "@google/genai"; import { StreamingService, type StreamChunk } from "Services/StreamingService"; export class Gemini implements IAIClass { diff --git a/Components/ChatWindow.svelte b/Components/ChatWindow.svelte index 9d600dd..a427424 100644 --- a/Components/ChatWindow.svelte +++ b/Components/ChatWindow.svelte @@ -8,6 +8,7 @@ import { tick } from "svelte"; import { ConversationFileSystemService } from "Services/ConversationFileSystemService"; import { setIcon } from "obsidian"; + import { conversationStore } from "../Stores/conversationStore"; let ai: IAIClass = Resolve(Services.IAIClass); let actioner: IActioner = Resolve(Services.IActioner); @@ -139,6 +140,11 @@ $: if (submitButton) { setIcon(submitButton, 'send-horizontal'); } + + $: if ($conversationStore.shouldReset) { + messages = []; + conversationStore.clearResetFlag(); + }
diff --git a/Components/TopBar.svelte b/Components/TopBar.svelte index b0f78d2..674370d 100644 --- a/Components/TopBar.svelte +++ b/Components/TopBar.svelte @@ -3,17 +3,22 @@ import { Services } from '../Services/Services'; import type AIAgentPlugin from '../main'; import { setIcon, type WorkspaceLeaf } from 'obsidian'; + import { ConversationFileSystemService } from '../Services/ConversationFileSystemService'; + import { conversationStore } from '../Stores/conversationStore'; export let leaf: WorkspaceLeaf; const plugin = Resolve(Services.AIAgentPlugin); + const conversationService = Resolve(Services.ConversationFileSystemService); function startNewConversation() { - + conversationService.resetCurrentConversation(); + conversationStore.reset(); } - function deleteCurrentConversation() { - + async function deleteCurrentConversation() { + await conversationService.deleteCurrentConversation(); + conversationStore.reset(); } function openConversationHistory() { diff --git a/Services/ConversationFileSystemService.ts b/Services/ConversationFileSystemService.ts index cbff989..126a9e7 100644 --- a/Services/ConversationFileSystemService.ts +++ b/Services/ConversationFileSystemService.ts @@ -47,4 +47,18 @@ export class ConversationFileSystemService { this.currentConversationPath = null; } + public async deleteCurrentConversation(): Promise { + if (!this.currentConversationPath) { + return false; + } + + const deleted = await this.fileSystemService.deleteFile(this.currentConversationPath); + + if (deleted) { + this.resetCurrentConversation(); + } + + return deleted; + } + } diff --git a/Services/FileSystemService.ts b/Services/FileSystemService.ts index 42d1fec..d210e1b 100644 --- a/Services/FileSystemService.ts +++ b/Services/FileSystemService.ts @@ -42,6 +42,22 @@ export class FileSystemService { } } + public async deleteFile(filePath: string): Promise { + try { + const file: TAbstractFile | null = this.vault.getAbstractFileByPath(filePath); + + if (file && file instanceof TFile) { + await this.vault.delete(file); + return true; + } + + return false; + } catch (error) { + console.error("Error deleting file:", error); + return false; + } + } + private async createDirectories(vault: Vault, filePath: string) { const dirPath: string = filePath.substring(0, filePath.lastIndexOf('/')); diff --git a/Stores/conversationStore.ts b/Stores/conversationStore.ts new file mode 100644 index 0000000..d3897a0 --- /dev/null +++ b/Stores/conversationStore.ts @@ -0,0 +1,13 @@ +import { writable } from 'svelte/store'; + +function createConversationStore() { + const { subscribe, set, update } = writable({ shouldReset: false }); + + return { + subscribe, + reset: () => set({ shouldReset: true }), + clearResetFlag: () => set({ shouldReset: false }) + }; +} + +export const conversationStore = createConversationStore();