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 - FreewritingCleanupData 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:
6 files changed
+294
-22
No files matched your search
+79
-7
@@ -1,9 +1,10 @@
|
||||
// ABOUTME: Settings interface and SettingTab implementation for the Freewriting Cleanup 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 FreewritingCleanupPlugin from './main';
|
||||
import { FreewritingCleanupSettings, ANTHROPIC_MODELS, AnthropicModel, COMMENTARY_STYLES, CommentaryStyle, COMMENTARY_PRESETS } from './types';
|
||||
import { FreewritingCleanupSettings, COMMENTARY_STYLES, CommentaryStyle, COMMENTARY_PRESETS } from './types';
|
||||
import { ModelOption } from './services/modelService';
|
||||
|
||||
export const DEFAULT_SETTINGS: FreewritingCleanupSettings = {
|
||||
apiKey: '',
|
||||
@@ -16,18 +17,23 @@ export const DEFAULT_SETTINGS: FreewritingCleanupSettings = {
|
||||
|
||||
export class FreewritingCleanupSettingTab extends PluginSettingTab {
|
||||
plugin: FreewritingCleanupPlugin;
|
||||
private modelDropdown: DropdownComponent | null = null;
|
||||
private availableModels: ModelOption[] = [];
|
||||
|
||||
constructor(app: App, plugin: FreewritingCleanupPlugin) {
|
||||
super(app, plugin);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
display(): void {
|
||||
async display(): Promise<void> {
|
||||
const { containerEl } = this;
|
||||
containerEl.empty();
|
||||
|
||||
containerEl.createEl('h2', { text: 'Freewriting Cleanup Settings' });
|
||||
|
||||
// Load models asynchronously
|
||||
await this.loadModels();
|
||||
|
||||
// MARK: - API Configuration
|
||||
|
||||
containerEl.createEl('h3', { text: 'API Configuration' });
|
||||
@@ -42,6 +48,11 @@ export class FreewritingCleanupSettingTab extends PluginSettingTab {
|
||||
this.plugin.settings.apiKey = value;
|
||||
await this.plugin.saveSettings();
|
||||
this.plugin.cleanupService.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
|
||||
@@ -61,13 +72,12 @@ export class FreewritingCleanupSettingTab extends PluginSettingTab {
|
||||
.setName('Claude Model')
|
||||
.setDesc('Which Claude model to use for text cleanup')
|
||||
.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();
|
||||
});
|
||||
});
|
||||
@@ -191,6 +201,68 @@ export class FreewritingCleanupSettingTab 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
|
||||
}
|
||||
}
|
||||
|
||||
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: - Private Methods
|
||||
|
||||
private async testApiKey(button: ButtonComponent): Promise<void> {
|
||||
|
||||
Reference in new issue
Block a user