Files
Momento/memento-note/lib/ai/services/title-suggestion.service.ts
Sepehr Ramezani 1c659ce42f fix: comprehensive security, consistency, and dead code cleanup
Security:
- Add auth + file type/size validation to upload API
- Add admin auth to /api/admin/ endpoints
- Add SSRF protection to scrape action
- Whitelist fields in PUT /api/notes/[id] to prevent mass assignment
- Protect /lab, /agents, /chat, /canvas, /notebooks routes in middleware

AI provider fixes:
- Add deepseek/openrouter to factory ProviderType (was silently falling back to ollama)
- Fix title-suggestion.service.ts to use factory instead of hardcoded OpenAI
- Fix getAIProvider→getChatProvider in memory-echo, notebook-summary, agent-executor
- Fix getAIProvider→getTagsProvider in notebook-suggestion, title-suggestions, transform-markdown

Functional bugs:
- Fix ALLOW_REGISTRATION AND→OR logic
- Fix note-editor.tsx passing stale props to useAutoTagging instead of local state
- Fix stale Note.embedding type (migrated to NoteEmbedding table)
- Remove hardcoded SQLite path from prisma.ts

Frontend:
- Add AbortController to useAutoTagging and useTitleSuggestions hooks
- Add error rollback to optimistic UI in note-inline-editor
- Remove stale closure over notebookId/language in useAutoTagging

Cleanup:
- Rename docker-compose from keepnotes→memento
- Remove unused unstable_cache import from config.ts
- Remove dead useUndoRedo hook
- Fix TagSuggestion type (add isNewLabel, reasoning)
- Remove dead AIConfig/AIProviderType types
- Fix ghost-tags unused isEmpty var and as any cast
- Fix note-editor titleSuggestions typed as any[]

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-04-21 22:22:02 +02:00

175 lines
5.3 KiB
TypeScript

/**
* Title Suggestion Service
* Generates intelligent title suggestions based on note content
*/
import { generateText } from 'ai'
import { LanguageDetectionService } from './language-detection.service'
import { getTagsProvider } from '../factory'
import { getSystemConfig } from '@/lib/config'
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 config = await getSystemConfig()
const provider = getTagsProvider(config)
const model = provider.getModel()
// 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()