Files
freewriting-cleanup/services/cleanupService.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

167 lines
5.1 KiB
TypeScript

// ABOUTME: Service class for orchestrating freewriting text cleanup operations
// ABOUTME: Handles validation, API coordination, and result formatting
import { Notice } from 'obsidian';
import { AnthropicClient } from '../api/anthropicClient';
import { FreewritingCleanupSettings, CleanupResult, ANTHROPIC_LIMITS } from '../types';
export class CleanupService {
private client: AnthropicClient;
// Expose client for ModelService
get anthropicClient(): AnthropicClient {
return this.client;
}
constructor(apiKey: string) {
this.client = new AnthropicClient(apiKey);
}
// MARK: - Public Methods
async cleanupText(text: string, settings: FreewritingCleanupSettings): Promise<CleanupResult> {
// Validate input
if (!text || text.trim().length === 0) {
throw new Error('No text selected for cleanup');
}
if (!this.client.validateApiKey()) {
throw new Error('API key is not configured. Please check your plugin settings.');
}
// Check text length limits
if (text.length > ANTHROPIC_LIMITS.MAX_CHARACTERS) {
throw new Error(`Selected text is too long (${text.length} characters). Maximum allowed: ${ANTHROPIC_LIMITS.MAX_CHARACTERS} characters.`);
}
const startTime = Date.now();
try {
const { cleanedText, commentary } = await this.client.cleanupText(
text,
settings.model,
settings.cleanupPrompt,
settings.enableCommentary,
settings.commentaryStyle,
settings.customCommentaryPrompt
);
const result: CleanupResult = {
originalText: text,
cleanedText,
commentary,
timestamp: new Date(),
model: settings.model,
tokensUsed: {
input: 0, // We don't have access to this from the response
output: 0 // We don't have access to this from the response
}
};
const duration = Date.now() - startTime;
new Notice(`Text cleanup completed in ${(duration / 1000).toFixed(1)}s`);
return result;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
new Notice(`Text cleanup failed: ${errorMessage}`);
throw error;
}
}
async testConnection(settings: FreewritingCleanupSettings): Promise<{
success: boolean;
message: string;
details?: {
model: string;
responseTime: number;
inputTokens?: number;
outputTokens?: number;
};
}> {
if (!this.client.validateApiKey()) {
return {
success: false,
message: 'API key is not configured'
};
}
try {
const result = await this.client.testConnection(settings.model);
return result;
} catch (error) {
return {
success: false,
message: error instanceof Error ? error.message : 'Unknown error'
};
}
}
formatCleanupResult(result: CleanupResult): string {
let formatted = `\n\n---\n\nAI Cleanup:\n\n${result.cleanedText}`;
if (result.commentary) {
formatted += `\n\n---\n\nAI Commentary:\n\n${result.commentary}`;
}
return formatted;
}
// MARK: - Configuration Methods
updateApiKey(apiKey: string): void {
this.client.updateApiKey(apiKey);
}
validateSettings(settings: FreewritingCleanupSettings): {
isValid: boolean;
errors: string[];
} {
const errors: string[] = [];
if (!settings.apiKey || settings.apiKey.trim().length === 0) {
errors.push('API key is required');
}
if (!settings.model) {
errors.push('Model selection is required');
}
if (!settings.cleanupPrompt || settings.cleanupPrompt.trim().length === 0) {
errors.push('Cleanup prompt is required');
}
return {
isValid: errors.length === 0,
errors
};
}
// MARK: - Utility Methods
static getCharacterCount(text: string): number {
return text.length;
}
static estimateTokenCount(text: string): number {
// Rough estimation: 1 token ≈ 4 characters for English text
return Math.ceil(text.length / 4);
}
static isWithinLimits(text: string): {
withinCharacterLimit: boolean;
withinTokenLimit: boolean;
characterCount: number;
estimatedTokenCount: number;
} {
const characterCount = this.getCharacterCount(text);
const estimatedTokenCount = this.estimateTokenCount(text);
return {
withinCharacterLimit: characterCount <= ANTHROPIC_LIMITS.MAX_CHARACTERS,
withinTokenLimit: estimatedTokenCount <= ANTHROPIC_LIMITS.MAX_TOKENS,
characterCount,
estimatedTokenCount
};
}
}