From 42b078075c92f4b4310bcc142d2035237e9dbc4d Mon Sep 17 00:00:00 2001 From: Logan Yang Date: Wed, 29 Oct 2025 18:48:45 -0700 Subject: [PATCH] refactor: Simplify log file initialization and clear log on new chat (#1982) - Updated `ensureInitialized` method in `LogFileManager` to start with an empty buffer, removing unnecessary file reading logic. - Integrated log clearing functionality in `handleNewChat` to ensure a fresh start for each chat session. --- src/components/Chat.tsx | 2 ++ src/logFileManager.ts | 27 +++------------------------ src/main.ts | 1 + 3 files changed, 6 insertions(+), 24 deletions(-) diff --git a/src/components/Chat.tsx b/src/components/Chat.tsx index 3c132037..4c37be55 100644 --- a/src/components/Chat.tsx +++ b/src/components/Chat.tsx @@ -33,6 +33,7 @@ import { useChatFileDrop } from "@/hooks/useChatFileDrop"; import { getAIResponse } from "@/langchainStream"; import ChainManager from "@/LLMProviders/chainManager"; import { clearRecordedPromptPayload } from "@/LLMProviders/chainRunner/utils/promptPayloadRecorder"; +import { logFileManager } from "@/logFileManager"; import CopilotPlugin from "@/main"; import { useIsPlusUser } from "@/plusUtils"; import { updateSetting, useSettingsValue } from "@/settings/model"; @@ -574,6 +575,7 @@ const ChatInternal: React.FC { clearRecordedPromptPayload(); + await logFileManager.clear(); handleStopGenerating(ABORT_REASON.NEW_CHAT); // Analyze chat messages for memory if enabled diff --git a/src/logFileManager.ts b/src/logFileManager.ts index d91739e7..6d4aa663 100644 --- a/src/logFileManager.ts +++ b/src/logFileManager.ts @@ -30,32 +30,11 @@ class LogFileManager { return "copilot/copilot-log.md"; // under copilot/ } - /** Ensure buffer is loaded with up to last 500 lines from existing file. */ + /** Ensure the log manager is initialized. Always starts with an empty buffer. */ private async ensureInitialized() { if (this.initialized) return; - try { - if (!this.hasVault()) { - this.initialized = true; - return; - } - const path = this.getLogPath(); - const exists = await app.vault.adapter.exists(path); - if (exists) { - const content = await app.vault.adapter.read(path); - // Normalize line endings and split - const lines = content.split(/\r?\n/).filter((l) => l.length > 0); - if (lines.length > this.maxLines) { - this.buffer = lines.slice(lines.length - this.maxLines); - } else { - this.buffer = lines; - } - } - } catch { - // Ignore read errors; start fresh - this.buffer = []; - } finally { - this.initialized = true; - } + // Start with empty buffer - log file is only an export artifact + this.initialized = true; } private hasVault(): boolean { diff --git a/src/main.ts b/src/main.ts index 9c7921fb..76f77915 100644 --- a/src/main.ts +++ b/src/main.ts @@ -468,6 +468,7 @@ export default class CopilotPlugin extends Plugin { async handleNewChat() { clearRecordedPromptPayload(); + await logFileManager.clear(); // Analyze chat messages for memory if enabled if (getSettings().enableRecentConversations) {