Files
freewriting-cleanup/main.ts
T
Jarvis 7b23ed6615 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>
2025-10-23 10:25:22 +02:00

84 lines
2.7 KiB
TypeScript

// ABOUTME: Main plugin file for Freewriting Cleanup - cleans up freewriting text using AI
// ABOUTME: Coordinates between services, commands, and Obsidian's plugin lifecycle
import { Editor, MarkdownView, Plugin } from 'obsidian';
import { FreewritingCleanupSettings, FreewritingCleanupData } from './types';
import { DEFAULT_SETTINGS, FreewritingCleanupSettingTab } from './settings';
import { CleanupService } from './services/cleanupService';
import { ModelService } from './services/modelService';
import { CleanupCommand } from './commands/cleanupCommand';
export default class FreewritingCleanupPlugin extends Plugin {
settings: FreewritingCleanupSettings;
cleanupService: CleanupService;
modelService: ModelService;
cleanupCommand: CleanupCommand;
async onload() {
await this.loadSettings();
// Initialize services
this.cleanupService = new CleanupService(this.settings.apiKey);
this.modelService = new ModelService(this.cleanupService.anthropicClient);
this.cleanupCommand = new CleanupCommand(this.cleanupService);
// Load model cache
const data = await this.loadData() as FreewritingCleanupData | null;
if (data?.modelCache) {
this.modelService.loadCache(data.modelCache);
}
// Register command
this.registerCommand();
// Add settings tab
this.addSettingTab(new FreewritingCleanupSettingTab(this.app, this));
}
onunload() {
// Cleanup if needed
}
// MARK: - Settings Management
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
// Save settings with model cache
const data: FreewritingCleanupData = {
...this.settings,
modelCache: this.modelService?.getCache() || undefined
};
await this.saveData(data);
// Update the cleanup service with new settings
if (this.cleanupService) {
this.cleanupService.updateApiKey(this.settings.apiKey);
}
}
// MARK: - Command Registration
private registerCommand() {
this.addCommand({
id: 'cleanup-text',
name: 'Clean up text',
editorCallback: async (editor: Editor, view: MarkdownView) => {
await this.executeCleanupCommand(editor, view);
}
});
}
// MARK: - Command Implementation
private async executeCleanupCommand(editor: Editor, view: MarkdownView): Promise<void> {
try {
await this.cleanupCommand.execute(editor, view, this.settings);
} catch (error) {
console.error('Error executing cleanup command:', error);
}
}
}