Enhances code documentation and structure for plugin #3
No files matched your search
@@ -4,7 +4,7 @@
|
|||||||
import { Editor, MarkdownView, Notice } from 'obsidian';
|
import { Editor, MarkdownView, Notice } from 'obsidian';
|
||||||
import { CleanupService } from '../services/cleanupService';
|
import { CleanupService } from '../services/cleanupService';
|
||||||
import { FreewritingCleanupSettings, ANTHROPIC_LIMITS } from '../types';
|
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
|
* Command handler for freewriting cleanup operations
|
||||||
@@ -72,14 +72,25 @@ export class CleanupCommand {
|
|||||||
// Move to end of current line and insert new content
|
// Move to end of current line and insert new content
|
||||||
editor.setCursor(insertPosition);
|
editor.setCursor(insertPosition);
|
||||||
editor.replaceRange(formattedResult, 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) {
|
} catch (error) {
|
||||||
// Handle specific error types using instanceof checks
|
// Handle specific error types using instanceof checks
|
||||||
if (error instanceof ApiKeyError) {
|
if (error instanceof ApiKeyError) {
|
||||||
new Notice('API key error. Please check your settings.');
|
new Notice('API key error. Please check your settings.');
|
||||||
} else if (error instanceof TextTooLongError) {
|
} else if (error instanceof TextTooLongError) {
|
||||||
new Notice(error.message);
|
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) {
|
} else if (error instanceof ServiceUnavailableError) {
|
||||||
new Notice(error.message);
|
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) {
|
} else if (error instanceof Error) {
|
||||||
new Notice(`Cleanup failed: ${error.message}`);
|
new Notice(`Cleanup failed: ${error.message}`);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -74,6 +74,8 @@ export class CleanupService {
|
|||||||
settings.customCommentaryPrompt
|
settings.customCommentaryPrompt
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const duration = Date.now() - startTime;
|
||||||
|
|
||||||
const result: CleanupResult = {
|
const result: CleanupResult = {
|
||||||
originalText: text,
|
originalText: text,
|
||||||
cleanedText,
|
cleanedText,
|
||||||
@@ -83,12 +85,10 @@ export class CleanupService {
|
|||||||
tokensUsed: {
|
tokensUsed: {
|
||||||
input: usage?.input_tokens ?? 0,
|
input: usage?.input_tokens ?? 0,
|
||||||
output: usage?.output_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;
|
return result;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Let the command layer handle user feedback to avoid duplicate notices
|
// Let the command layer handle user feedback to avoid duplicate notices
|
||||||
@@ -193,6 +193,12 @@ export class CleanupService {
|
|||||||
errors.push('Cleanup prompt is required');
|
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 {
|
return {
|
||||||
isValid: errors.length === 0,
|
isValid: errors.length === 0,
|
||||||
errors
|
errors
|
||||||
@@ -215,14 +221,17 @@ export class CleanupService {
|
|||||||
* Estimates token count from character count
|
* Estimates token count from character count
|
||||||
*
|
*
|
||||||
* Uses rough approximation of 1 token per 4 characters for English text.
|
* 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
|
* @param text - Text to estimate tokens for
|
||||||
* @returns Estimated token count
|
* @returns Estimated token count with safety margin
|
||||||
*/
|
*/
|
||||||
static estimateTokenCount(text: string): number {
|
static estimateTokenCount(text: string): number {
|
||||||
// Rough estimation: 1 token ≈ 4 characters for English text
|
// 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user