Adds dynamic Claude model fetching and management #1
@@ -2,7 +2,7 @@
|
|||||||
// ABOUTME: Coordinates between services, commands, and Obsidian's plugin lifecycle
|
// ABOUTME: Coordinates between services, commands, and Obsidian's plugin lifecycle
|
||||||
|
|
||||||
import { Editor, MarkdownView, Notice, Plugin } from 'obsidian';
|
import { Editor, MarkdownView, Notice, Plugin } from 'obsidian';
|
||||||
import { FreewritingPromptsSettings, FreewritingPromptsData } from './types';
|
import { FreewritingPromptsSettings, FreewritingPromptsData, ModelCache } from './types';
|
||||||
import { DEFAULT_SETTINGS, FreewritingPromptsSettingTab } from './settings';
|
import { DEFAULT_SETTINGS, FreewritingPromptsSettingTab } from './settings';
|
||||||
import { PromptGeneratorService } from './services/promptGenerator';
|
import { PromptGeneratorService } from './services/promptGenerator';
|
||||||
import { ModelService } from './services/modelService';
|
import { ModelService } from './services/modelService';
|
||||||
@@ -50,11 +50,25 @@ export default class FreewritingPromptsPlugin extends Plugin {
|
|||||||
*/
|
*/
|
||||||
async onload() {
|
async onload() {
|
||||||
// Load data once and use it for both settings and model cache
|
// Load data once and use it for both settings and model cache
|
||||||
const data = await this.loadData() as FreewritingPromptsData | null;
|
// Guard against corrupted persisted data to prevent plugin load failure
|
||||||
|
let modelCache: ModelCache | undefined;
|
||||||
// Destructure to prevent modelCache from being merged into settings (type drift)
|
try {
|
||||||
const { modelCache, ...savedSettings } = data ?? {};
|
const data = await this.loadData() as FreewritingPromptsData | null;
|
||||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, savedSettings);
|
if (data) {
|
||||||
|
// Destructure to prevent modelCache from being merged into settings (type drift)
|
||||||
|
const { modelCache: cache, ...savedSettings } = data;
|
||||||
|
modelCache = cache;
|
||||||
|
this.settings = Object.assign({}, DEFAULT_SETTINGS, savedSettings);
|
||||||
|
} else {
|
||||||
|
// No data loaded, use defaults
|
||||||
|
this.settings = { ...DEFAULT_SETTINGS };
|
||||||
|
modelCache = undefined;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to load settings; using defaults.', error);
|
||||||
|
this.settings = { ...DEFAULT_SETTINGS };
|
||||||
|
modelCache = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
// Track initial API key to detect changes on save
|
// Track initial API key to detect changes on save
|
||||||
this.lastApiKey = this.settings.apiKey;
|
this.lastApiKey = this.settings.apiKey;
|
||||||
|
|||||||
Reference in new issue
Block a user