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

+80 -7
View File
@@ -1,9 +1,10 @@
// ABOUTME: Settings interface and SettingTab implementation for the Freewriting Prompts plugin
// ABOUTME: Handles user configuration including API key, model selection, and prompt customization
import { App, ButtonComponent, Notice, PluginSettingTab, Setting } from 'obsidian';
import { App, ButtonComponent, DropdownComponent, Notice, PluginSettingTab, Setting } from 'obsidian';
import FreewritingPromptsPlugin from './main';
import { FreewritingPromptsSettings, ANTHROPIC_MODELS, AnthropicModel } from './types';
import { FreewritingPromptsSettings } from './types';
import { ModelOption } from './services/modelService';
export const DEFAULT_SETTINGS: FreewritingPromptsSettings = {
apiKey: '',
@@ -18,18 +19,23 @@ export const DEFAULT_SETTINGS: FreewritingPromptsSettings = {
export class FreewritingPromptsSettingTab extends PluginSettingTab {
plugin: FreewritingPromptsPlugin;
private modelDropdown: DropdownComponent | null = null;
private availableModels: ModelOption[] = [];
constructor(app: App, plugin: FreewritingPromptsPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
async display(): Promise<void> {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Freewriting Prompts' });
// Load models asynchronously
await this.loadModels();
// MARK: - API Configuration
containerEl.createEl('h3', { text: 'API Configuration' });
@@ -44,6 +50,11 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab {
this.plugin.settings.apiKey = value;
await this.plugin.saveSettings();
this.plugin.promptGenerator.updateApiKey(value);
// Trigger model refresh when API key is entered
if (value.trim().length > 0) {
await this.refreshModels();
}
}))
.then(setting => {
// Make it a password field
@@ -63,13 +74,12 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab {
.setName('Claude Model')
.setDesc('Which Claude model to use for generating prompts')
.addDropdown(dropdown => {
ANTHROPIC_MODELS.forEach(model => {
dropdown.addOption(model, model);
});
this.modelDropdown = dropdown;
this.populateModelDropdown(dropdown);
dropdown
.setValue(this.plugin.settings.model)
.onChange(async (value) => {
this.plugin.settings.model = value as AnthropicModel;
this.plugin.settings.model = value;
await this.plugin.saveSettings();
});
});
@@ -183,6 +193,69 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab {
}));
}
// MARK: - Model Loading
private async loadModels(): Promise<void> {
try {
this.availableModels = await this.plugin.modelService.getAvailableModels();
} catch (error) {
console.error('Error loading models:', error);
// Error already shown by ModelService via Notice
// availableModels will be empty array, dropdown will be disabled
}
}
private populateModelDropdown(dropdown: DropdownComponent): void {
if (this.availableModels.length === 0) {
// No models available yet, disable dropdown
dropdown.addOption('', 'Loading models...');
dropdown.setDisabled(true);
return;
}
// Clear existing options
dropdown.selectEl.empty();
// Add all available models
this.availableModels.forEach(model => {
dropdown.addOption(model.id, model.displayName);
});
dropdown.setDisabled(false);
}
private async refreshModels(): Promise<void> {
try {
// Disable dropdown during refresh
if (this.modelDropdown) {
this.modelDropdown.setDisabled(true);
this.modelDropdown.selectEl.empty();
this.modelDropdown.addOption('', 'Refreshing models...');
}
// Fetch new models
this.availableModels = await this.plugin.modelService.refreshModels();
// Update dropdown
if (this.modelDropdown) {
this.populateModelDropdown(this.modelDropdown);
// Restore selected model if it still exists
const currentModel = this.plugin.settings.model;
const modelExists = this.availableModels.some(m => m.id === currentModel);
if (modelExists) {
this.modelDropdown.setValue(currentModel);
}
}
// Save updated cache
await this.plugin.saveSettings();
} catch (error) {
console.error('Error refreshing models:', error);
// Error already shown by ModelService via Notice
}
}
// MARK: - API Testing
private async testApiKey(button: ButtonComponent): Promise<void> {