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:
Jarvis committed 2025-10-23 10:25:22 +02:00
1 parent b119923300
commit 7b23ed6615
6 files changed
+294 -22

No files matched your search

+12 -7
View File
@@ -6,10 +6,15 @@ import { AnthropicClient } from '../api/anthropicClient';
import { FreewritingCleanupSettings, CleanupResult, ANTHROPIC_LIMITS } from '../types';
export class CleanupService {
private anthropicClient: AnthropicClient;
private client: AnthropicClient;
// Expose client for ModelService
get anthropicClient(): AnthropicClient {
return this.client;
}
constructor(apiKey: string) {
this.anthropicClient = new AnthropicClient(apiKey);
this.client = new AnthropicClient(apiKey);
}
// MARK: - Public Methods
@@ -20,7 +25,7 @@ export class CleanupService {
throw new Error('No text selected for cleanup');
}
if (!this.anthropicClient.validateApiKey()) {
if (!this.client.validateApiKey()) {
throw new Error('API key is not configured. Please check your plugin settings.');
}
@@ -32,7 +37,7 @@ export class CleanupService {
const startTime = Date.now();
try {
const { cleanedText, commentary } = await this.anthropicClient.cleanupText(
const { cleanedText, commentary } = await this.client.cleanupText(
text,
settings.model,
settings.cleanupPrompt,
@@ -74,7 +79,7 @@ export class CleanupService {
outputTokens?: number;
};
}> {
if (!this.anthropicClient.validateApiKey()) {
if (!this.client.validateApiKey()) {
return {
success: false,
message: 'API key is not configured'
@@ -82,7 +87,7 @@ export class CleanupService {
}
try {
const result = await this.anthropicClient.testConnection(settings.model);
const result = await this.client.testConnection(settings.model);
return result;
} catch (error) {
return {
@@ -105,7 +110,7 @@ export class CleanupService {
// MARK: - Configuration Methods
updateApiKey(apiKey: string): void {
this.anthropicClient.updateApiKey(apiKey);
this.client.updateApiKey(apiKey);
}
validateSettings(settings: FreewritingCleanupSettings): {