feat: add dynamic model fetching from Anthropic API

Replace hardcoded model list with dynamic API-based model fetching
that queries the Anthropic models endpoint. Implements 24-hour caching
with automatic fallback to hardcoded models if API is unavailable.

Changes:
- Add ModelService to fetch and cache available models from API
- Implement 24-hour TTL cache persisted in plugin data
- Update AnthropicClient with fetchModels() method
- Modify settings UI to load models asynchronously with loading states
- Auto-refresh models when API key is entered or changed
- Display model names using display_name field from API
- Preserve user's selected model across updates and restarts
- Show notice when falling back to hardcoded model list
- Change model type from strict enum to string for dynamic support

Technical details:
- New ModelInfo and ModelsListResponse interfaces for API response
- ModelCache interface with timestamp for TTL validation
- FreewritingPromptsData extends settings to include cache
- Settings dropdown disabled during model fetch/refresh
- Fallback to ANTHROPIC_MODELS constant on API failure

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Jarvis committed 2025-10-23 10:17:30 +02:00
1 parent a79dcec18f
commit 20530142e4
6 files changed
+287 -19

No files matched your search

+16 -2
View File
@@ -2,15 +2,17 @@
// ABOUTME: Coordinates between services, commands, and Obsidian's plugin lifecycle
import { Editor, MarkdownView, Notice, Plugin } from 'obsidian';
import { FreewritingPromptsSettings } from './types';
import { FreewritingPromptsSettings, FreewritingPromptsData } from './types';
import { DEFAULT_SETTINGS, FreewritingPromptsSettingTab } from './settings';
import { PromptGeneratorService } from './services/promptGenerator';
import { ModelService } from './services/modelService';
import { TimedPromptsCommand } from './commands/timedPrompts';
import { NotePromptsCommand } from './commands/notePrompts';
export default class FreewritingPromptsPlugin extends Plugin {
settings: FreewritingPromptsSettings;
promptGenerator: PromptGeneratorService;
modelService: ModelService;
timedCommand: TimedPromptsCommand;
noteCommand: NotePromptsCommand;
@@ -19,9 +21,16 @@ export default class FreewritingPromptsPlugin extends Plugin {
// Initialize services
this.promptGenerator = new PromptGeneratorService(this.settings);
this.modelService = new ModelService(this.promptGenerator.anthropicClient);
this.timedCommand = new TimedPromptsCommand(this.promptGenerator);
this.noteCommand = new NotePromptsCommand(this.promptGenerator);
// Load model cache
const data = await this.loadData() as FreewritingPromptsData | null;
if (data?.modelCache) {
this.modelService.loadCache(data.modelCache);
}
// Register commands
this.registerCommands();
@@ -48,7 +57,12 @@ export default class FreewritingPromptsPlugin extends Plugin {
}
async saveSettings() {
await this.saveData(this.settings);
// Save settings with model cache
const data: FreewritingPromptsData = {
...this.settings,
modelCache: this.modelService?.getCache() || undefined
};
await this.saveData(data);
// Update the prompt generator with new settings
if (this.promptGenerator) {