From 53e61abd3b47e44a4306c78a352bff0789a74dbb Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Fri, 24 Oct 2025 13:44:45 +0200 Subject: [PATCH] Optimize settings and API key management Improves settings loading and saving by preventing model cache merging and tracking API key changes Prevents type drift in settings by destructuring saved data Adds tracking of last API key to avoid unnecessary prompt generator updates Ensures only meaningful settings are merged with defaults Reduces redundant operations and improves plugin performance --- main.ts | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/main.ts b/main.ts index 3e1775e..aef3700 100644 --- a/main.ts +++ b/main.ts @@ -37,6 +37,8 @@ export default class FreewritingPromptsPlugin extends Plugin { timedCommand: TimedPromptsCommand; /** Command handler for note prompt insertion */ noteCommand: NotePromptsCommand; + /** Tracks last saved API key to avoid unnecessary cache clears */ + private lastApiKey?: string; /** * Called when the plugin loads. @@ -49,7 +51,13 @@ export default class FreewritingPromptsPlugin extends Plugin { async onload() { // Load data once and use it for both settings and model cache const data = await this.loadData() as FreewritingPromptsData | null; - this.settings = Object.assign({}, DEFAULT_SETTINGS, data); + + // Destructure to prevent modelCache from being merged into settings (type drift) + const { modelCache, ...savedSettings } = data ?? {}; + this.settings = Object.assign({}, DEFAULT_SETTINGS, savedSettings); + + // Track initial API key to detect changes on save + this.lastApiKey = this.settings.apiKey; // Initialize services this.promptGenerator = new PromptGeneratorService(this.settings); @@ -58,8 +66,8 @@ export default class FreewritingPromptsPlugin extends Plugin { this.noteCommand = new NotePromptsCommand(this.promptGenerator); // Load model cache - if (data?.modelCache) { - this.modelService.loadCache(data.modelCache); + if (modelCache) { + this.modelService.loadCache(modelCache); } // Register commands @@ -93,16 +101,6 @@ export default class FreewritingPromptsPlugin extends Plugin { // MARK: - Settings Management - /** - * Loads plugin settings from disk. - * - * Merges saved settings with defaults to ensure any new settings added - * in plugin updates have sensible default values. - */ - async loadSettings() { - this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); - } - /** * Saves plugin settings to disk. * @@ -118,9 +116,11 @@ export default class FreewritingPromptsPlugin extends Plugin { }; await this.saveData(data); - // Update the prompt generator with new settings - if (this.promptGenerator) { + // Update the prompt generator only if API key actually changed + // This avoids clearing the prompt cache unnecessarily on every save + if (this.promptGenerator && this.lastApiKey !== this.settings.apiKey) { this.promptGenerator.updateApiKey(this.settings.apiKey); + this.lastApiKey = this.settings.apiKey; } }