diff --git a/commands/cleanupCommand.ts b/commands/cleanupCommand.ts index d066f16..09120cc 100644 --- a/commands/cleanupCommand.ts +++ b/commands/cleanupCommand.ts @@ -4,7 +4,7 @@ import { Editor, MarkdownView, Notice } from 'obsidian'; import { CleanupService } from '../services/cleanupService'; import { FreewritingCleanupSettings, ANTHROPIC_LIMITS } from '../types'; -import { ApiKeyError, TextTooLongError, ServiceUnavailableError } from '../errors'; +import { ApiKeyError, TextTooLongError, ServiceUnavailableError, ApiError, InvalidResponseError, NetworkError } from '../errors'; /** * Command handler for freewriting cleanup operations @@ -72,14 +72,25 @@ export class CleanupCommand { // Move to end of current line and insert new content editor.setCursor(insertPosition); editor.replaceRange(formattedResult, insertPosition); + + // Show success notice with duration (loadingNotice will be hidden in finally) + new Notice(`Text cleanup completed in ${(result.duration / 1000).toFixed(1)}s`); } catch (error) { // Handle specific error types using instanceof checks if (error instanceof ApiKeyError) { new Notice('API key error. Please check your settings.'); } else if (error instanceof TextTooLongError) { new Notice(error.message); + } else if (error instanceof ApiError && (error.status === 401 || error.status === 403)) { + new Notice('API key was rejected by the server. Please verify your key.'); + } else if (error instanceof ApiError && error.status === 429) { + new Notice('Rate limited by API. Please wait and try again.'); } else if (error instanceof ServiceUnavailableError) { new Notice(error.message); + } else if (error instanceof InvalidResponseError) { + new Notice('Received an unexpected response from Claude. Please try again.'); + } else if (error instanceof NetworkError) { + new Notice('Network error. Check your connection and try again.'); } else if (error instanceof Error) { new Notice(`Cleanup failed: ${error.message}`); } else { diff --git a/services/cleanupService.ts b/services/cleanupService.ts index 68fdf08..ca6601a 100644 --- a/services/cleanupService.ts +++ b/services/cleanupService.ts @@ -74,6 +74,8 @@ export class CleanupService { settings.customCommentaryPrompt ); + const duration = Date.now() - startTime; + const result: CleanupResult = { originalText: text, cleanedText, @@ -83,12 +85,10 @@ export class CleanupService { tokensUsed: { input: usage?.input_tokens ?? 0, output: usage?.output_tokens ?? 0 - } + }, + duration }; - const duration = Date.now() - startTime; - new Notice(`Text cleanup completed in ${(duration / 1000).toFixed(1)}s`); - return result; } catch (error) { // Let the command layer handle user feedback to avoid duplicate notices @@ -193,6 +193,12 @@ export class CleanupService { errors.push('Cleanup prompt is required'); } + if (settings.enableCommentary && + settings.commentaryStyle === 'custom' && + (!settings.customCommentaryPrompt || settings.customCommentaryPrompt.trim().length === 0)) { + errors.push('Custom commentary prompt is required when commentary style is "custom"'); + } + return { isValid: errors.length === 0, errors @@ -215,14 +221,17 @@ export class CleanupService { * Estimates token count from character count * * Uses rough approximation of 1 token per 4 characters for English text. - * This is used for user feedback before making API requests. + * Adds a 10% safety margin to account for estimation inaccuracy and reduce + * the chance of near-limit failures. * * @param text - Text to estimate tokens for - * @returns Estimated token count + * @returns Estimated token count with safety margin */ static estimateTokenCount(text: string): number { // Rough estimation: 1 token ≈ 4 characters for English text - return Math.ceil(text.length / 4); + const baseEstimate = Math.ceil(text.length / 4); + // Add 10% safety margin to reduce near-limit failures + return Math.ceil(baseEstimate * 1.1); } /** diff --git a/types.ts b/types.ts index 0d784cb..746bc59 100644 --- a/types.ts +++ b/types.ts @@ -128,6 +128,8 @@ export interface CleanupResult { /** Output tokens */ output: number; }; + /** Duration of the cleanup operation in milliseconds */ + duration: number; } /**