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.
This commit is contained in:
Logan Yang 2025-10-29 18:48:45 -07:00 committed by GitHub
parent f62cb918da
commit 42b078075c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 6 additions and 24 deletions

View file

@ -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<ChatProps & { chatInput: ReturnType<typeof useChatI
const handleNewChat = useCallback(async () => {
clearRecordedPromptPayload();
await logFileManager.clear();
handleStopGenerating(ABORT_REASON.NEW_CHAT);
// Analyze chat messages for memory if enabled

View file

@ -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 {

View file

@ -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) {