Keep/keep-notes/lib/ai/services/title-suggestion.service.ts
sepehr 7fb486c9a4 feat: Complete internationalization and code cleanup
## Translation Files
- Add 11 new language files (es, de, pt, ru, zh, ja, ko, ar, hi, nl, pl)
- Add 100+ missing translation keys across all 15 languages
- New sections: notebook, pagination, ai.batchOrganization, ai.autoLabels
- Update nav section with workspace, quickAccess, myLibrary keys

## Component Updates
- Update 15+ components to use translation keys instead of hardcoded text
- Components: notebook dialogs, sidebar, header, note-input, ghost-tags, etc.
- Replace 80+ hardcoded English/French strings with t() calls
- Ensure consistent UI across all supported languages

## Code Quality
- Remove 77+ console.log statements from codebase
- Clean up API routes, components, hooks, and services
- Keep only essential error handling (no debugging logs)

## UI/UX Improvements
- Update Keep logo to yellow post-it style (from-yellow-400 to-amber-500)
- Change selection colors to #FEF3C6 (notebooks) and #EFB162 (nav items)
- Make "+" button permanently visible in notebooks section
- Fix grammar and syntax errors in multiple components

## Bug Fixes
- Fix JSON syntax errors in it.json, nl.json, pl.json, zh.json
- Fix syntax errors in notebook-suggestion-toast.tsx
- Fix syntax errors in use-auto-tagging.ts
- Fix syntax errors in paragraph-refactor.service.ts
- Fix duplicate "fusion" section in nl.json

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

Ou une version plus courte si vous préférez :

feat(i18n): Add 15 languages, remove logs, update UI components

- Create 11 new translation files (es, de, pt, ru, zh, ja, ko, ar, hi, nl, pl)
- Add 100+ translation keys: notebook, pagination, AI features
- Update 15+ components to use translations (80+ strings)
- Remove 77+ console.log statements from codebase
- Fix JSON syntax errors in 4 translation files
- Fix component syntax errors (toast, hooks, services)
- Update logo to yellow post-it style
- Change selection colors (#FEF3C6, #EFB162)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-11 22:26:13 +01:00

183 lines
5.4 KiB
TypeScript

/**
* Title Suggestion Service
* Generates intelligent title suggestions based on note content
*/
import { createOpenAI } from '@ai-sdk/openai'
import { generateText } from 'ai'
import { LanguageDetectionService } from './language-detection.service'
// Helper to get AI model for text generation
function getTextGenerationModel() {
const apiKey = process.env.OPENAI_API_KEY
if (!apiKey) {
throw new Error('OPENAI_API_KEY not configured for title generation')
}
const openai = createOpenAI({ apiKey })
return openai('gpt-4o-mini')
}
export interface TitleSuggestion {
title: string
confidence: number // 0-100
reasoning?: string // Why this title was suggested
}
export class TitleSuggestionService {
private languageDetection: LanguageDetectionService
constructor() {
this.languageDetection = new LanguageDetectionService()
}
/**
* Generate 3 title suggestions for a note
* Uses interface language (from user settings) for prompts
*/
async generateSuggestions(noteContent: string): Promise<TitleSuggestion[]> {
// Detect language of the note content
const { language: contentLanguage } = await this.languageDetection.detectLanguage(noteContent)
try {
const model = getTextGenerationModel()
// System prompt - explains what to do
const systemPrompt = `You are an expert title generator for a note-taking application.
Your task is to generate 3 distinct, engaging titles that capture the essence of the user's note.
Requirements:
- Generate EXACTLY 3 titles
- Each title should be 3-8 words
- Titles should be concise but descriptive
- Each title should have a different style:
1. Direct/Summary style - What the note is about
2. Question style - Posing a question the note addresses
3. Creative/Metaphorical style - Using imagery or analogy
- Return titles in the SAME LANGUAGE as the user's note
- Be helpful and avoid generic titles like "My Note" or "Untitled"
Output Format (JSON):
{
"suggestions": [
{ "title": "...", "confidence": 85, "reasoning": "..." },
{ "title": "...", "confidence": 80, "reasoning": "..." },
{ "title": "...", "confidence": 75, "reasoning": "..." }
]
}`
// User prompt with language context
const userPrompt = `Generate 3 title suggestions for this note:
${noteContent}
Note language detected: ${contentLanguage}
Respond with titles in ${contentLanguage} (same language as the note).`
const { text } = await generateText({
model,
system: systemPrompt,
prompt: userPrompt,
temperature: 0.7
})
// Parse JSON response
const response = JSON.parse(text)
if (!response.suggestions || !Array.isArray(response.suggestions)) {
throw new Error('Invalid response format')
}
// Validate and limit to exactly 3 suggestions
const suggestions = response.suggestions
.slice(0, 3)
.filter((s: any) => s.title && typeof s.title === 'string')
.map((s: any) => ({
title: s.title.trim(),
confidence: Math.min(100, Math.max(0, s.confidence || 75)),
reasoning: s.reasoning || ''
}))
// Ensure we always return exactly 3 suggestions
while (suggestions.length < 3) {
suggestions.push({
title: this.generateFallbackTitle(noteContent, contentLanguage),
confidence: 60,
reasoning: 'Generated fallback title'
})
}
return suggestions
} catch (error) {
console.error('Error generating title suggestions:', error)
// Fallback to simple extraction
return this.generateFallbackSuggestions(noteContent, contentLanguage)
}
}
/**
* Generate fallback title from first few meaningful words
*/
private generateFallbackTitle(content: string, language: string): string {
const words = content.split(/\s+/).filter(w => w.length > 3)
if (words.length === 0) {
return language === 'fr' ? 'Note sans titre' : 'Untitled Note'
}
// Take first 3-5 meaningful words
const titleWords = words.slice(0, Math.min(5, words.length))
return titleWords.join(' ').charAt(0).toUpperCase() + titleWords.join(' ').slice(1)
}
/**
* Generate fallback suggestions when AI fails
*/
private generateFallbackSuggestions(content: string, language: string): TitleSuggestion[] {
const baseTitle = this.generateFallbackTitle(content, language)
return [
{
title: baseTitle,
confidence: 70,
reasoning: 'Extracted from note content'
},
{
title: language === 'fr'
? `Réflexions sur ${baseTitle.toLowerCase()}`
: `Thoughts on ${baseTitle.toLowerCase()}`,
confidence: 65,
reasoning: 'Contextual variation'
},
{
title: language === 'fr'
? `${baseTitle}: Points clés`
: `${baseTitle}: Key Points`,
confidence: 60,
reasoning: 'Summary style'
}
]
}
/**
* Save selected title to note metadata
*/
async recordFeedback(
noteId: string,
selectedTitle: string,
allSuggestions: TitleSuggestion[]
): Promise<void> {
// This will be implemented in Phase 3 when we add feedback collection
// For now, we just log it
// TODO: In Phase 3, save to AiFeedback table for:
// - Improving future suggestions
// - Building user preference model
// - Computing confidence scores
}
}
// Singleton instance
export const titleSuggestionService = new TitleSuggestionService()