From 907dcf33d6fc3f976f44188fafb2e0b2c2c62c18 Mon Sep 17 00:00:00 2001 From: sepehr Date: Thu, 30 Apr 2026 21:09:13 +0200 Subject: [PATCH] fix: increase reformulate word limit to 2000 + i18n error messages - Raise MAX_WORDS from 500 to 2000 for text reformulation - Error messages now use i18n keys (ai.wordCountMin/Max) instead of hardcoded English strings - Client translates server errors using the user's UI language Co-Authored-By: Claude Opus 4.7 --- memento-note/app/api/ai/reformulate/route.ts | 6 +++++- memento-note/components/contextual-ai-chat.tsx | 8 +++++++- .../lib/ai/services/paragraph-refactor.service.ts | 12 ++++++++---- memento-note/locales/en.json | 2 ++ memento-note/locales/fr.json | 2 ++ 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/memento-note/app/api/ai/reformulate/route.ts b/memento-note/app/api/ai/reformulate/route.ts index e5e3c84..3924fa0 100644 --- a/memento-note/app/api/ai/reformulate/route.ts +++ b/memento-note/app/api/ai/reformulate/route.ts @@ -42,7 +42,11 @@ export async function POST(request: NextRequest) { // Validate word count const validation = paragraphRefactorService.validateWordCount(text) if (!validation.valid) { - return NextResponse.json({ error: validation.error }, { status: 400 }) + return NextResponse.json({ + error: validation.error, + errorKey: validation.errorKey, + params: validation.params, + }, { status: 400 }) } // Use the ParagraphRefactorService diff --git a/memento-note/components/contextual-ai-chat.tsx b/memento-note/components/contextual-ai-chat.tsx index 5cf6303..5461aa8 100644 --- a/memento-note/components/contextual-ai-chat.tsx +++ b/memento-note/components/contextual-ai-chat.tsx @@ -205,7 +205,13 @@ export function ContextualAIChat({ body: JSON.stringify(action.body(noteContent, undefined, language)), }) const data = await res.json() - if (!res.ok) throw new Error(data.error || t('ai.genericError')) + if (!res.ok) { + // Use i18n key if provided by the server + if (data.errorKey) { + throw new Error(t(data.errorKey, data.params || {})) + } + throw new Error(data.error || t('ai.genericError')) + } const result = data[action.resultKey] || '' setActionPreview({ label: t(action.i18nKey), text: result }) } catch (e: any) { diff --git a/memento-note/lib/ai/services/paragraph-refactor.service.ts b/memento-note/lib/ai/services/paragraph-refactor.service.ts index 727bc0a..867b537 100644 --- a/memento-note/lib/ai/services/paragraph-refactor.service.ts +++ b/memento-note/lib/ai/services/paragraph-refactor.service.ts @@ -56,7 +56,7 @@ export const REFACTOR_OPTIONS: RefactorOption[] = [ export class ParagraphRefactorService { private languageDetection: LanguageDetectionService private readonly MIN_WORDS = 10 - private readonly MAX_WORDS = 500 + private readonly MAX_WORDS = 2000 constructor() { this.languageDetection = new LanguageDetectionService() @@ -288,20 +288,24 @@ CRITICAL: Respond ONLY with the refactored text in ${language}. No explanations, /** * Validate that text is within acceptable word count range */ - validateWordCount(content: string): { valid: boolean; error?: string } { + validateWordCount(content: string): { valid: boolean; error?: string; errorKey?: string; params?: Record } { const wordCount = content.split(/\s+/).length if (wordCount < this.MIN_WORDS) { return { valid: false, - error: `Please select at least ${this.MIN_WORDS} words to reformulate (currently ${wordCount} words)` + errorKey: 'ai.wordCountMin', + params: { min: this.MIN_WORDS, current: wordCount }, + error: `min:${this.MIN_WORDS}:current:${wordCount}` } } if (wordCount > this.MAX_WORDS) { return { valid: false, - error: `Please select at most ${this.MAX_WORDS} words to reformulate (currently ${wordCount} words)` + errorKey: 'ai.wordCountMax', + params: { max: this.MAX_WORDS, current: wordCount }, + error: `max:${this.MAX_WORDS}:current:${wordCount}` } } diff --git a/memento-note/locales/en.json b/memento-note/locales/en.json index 265ee6a..ce00981 100644 --- a/memento-note/locales/en.json +++ b/memento-note/locales/en.json @@ -376,6 +376,8 @@ "undoAI": "Undo AI transformation", "undoApplied": "Original text restored", "minWordsError": "Note must contain at least 5 words to use AI actions.", + "wordCountMin": "Please select at least {min} words to reformulate (currently {current} words)", + "wordCountMax": "Please select at most {max} words to reformulate (currently {current} words)", "genericError": "AI error", "actionError": "Error during AI action", "appliedToNote": "Applied to note", diff --git a/memento-note/locales/fr.json b/memento-note/locales/fr.json index 8fa1177..f27b83b 100644 --- a/memento-note/locales/fr.json +++ b/memento-note/locales/fr.json @@ -376,6 +376,8 @@ "undoAI": "Annuler la transformation IA", "undoApplied": "Texte original restauré", "minWordsError": "La note doit contenir au moins 5 mots pour utiliser les actions IA.", + "wordCountMin": "Veuillez sélectionner au moins {min} mots pour reformuler ({current} mots sélectionnés)", + "wordCountMax": "Veuillez sélectionner au maximum {max} mots pour reformuler ({current} mots sélectionnés)", "genericError": "Erreur IA", "actionError": "Erreur lors de l'action IA", "appliedToNote": "Appliqué à la note",