Files
Momento/memento-note/app/api/ai/enrich-from-resource/route.ts
sepehr 99d0583871
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 44s
feat: IA Note — rename panel, add Resource tab + chat hover-actions
- Renamed 'AI Copilot' / 'Assistant IA' → 'IA Note' everywhere in UI
- Added 3rd 'Ressource' tab in IA Note panel with:
  * Optional URL field that scrapes page text (via new scrapePageText action)
  * Textarea for paste (markdown, HTML, plain text)
  * 3 integration modes: Remplacer / Compléter (AI) / Fusionner (AI)
  * Dual-format preview: Rendu + Markdown brut before applying
- Added hover-actions on assistant chat messages:
  * Remplacer / Compléter / Fusionner appear on hover
  * Triggers same preview/apply flow via resource tab
- New API route: POST /api/ai/enrich-from-resource
  * Supports complete and merge modes with language-aware prompts
- Extended scrape.ts with scrapePageText() (full content extraction)
2026-05-02 21:06:25 +02:00

87 lines
3.0 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { auth } from '@/auth'
import { getSystemConfig } from '@/lib/config'
import { getTagsProvider } from '@/lib/ai/factory'
export async function POST(request: NextRequest) {
try {
const session = await auth()
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const { existingContent, resourceText, mode, language } = await request.json()
if (!resourceText || typeof resourceText !== 'string') {
return NextResponse.json({ error: 'resourceText is required' }, { status: 400 })
}
if (!mode || !['complete', 'merge'].includes(mode)) {
return NextResponse.json({ error: 'mode must be "complete" or "merge"' }, { status: 400 })
}
const lang = language || 'fr'
const config = await getSystemConfig()
const provider = getTagsProvider(config)
let prompt: string
if (mode === 'complete') {
// Add missing info from resource without rewriting the existing note
prompt = `You are an expert note editor. Your task is to enrich an existing note by adding relevant information from a provided resource, WITHOUT modifying or rewriting the existing content.
LANGUAGE RULE: Respond in ${lang}. Match the language of the existing note.
EXISTING NOTE:
---
${existingContent || '(empty note)'}
---
RESOURCE TO INTEGRATE:
---
${resourceText}
---
INSTRUCTIONS:
- Keep ALL existing note content exactly as-is at the top
- Append ONLY new, non-redundant information from the resource below the existing content
- Use a clear separator (e.g., "---" or a new section heading) between existing and new content
- Skip information already covered in the existing note
- Format the new content consistently with the existing note style
- Respond ONLY with the enriched note content, no explanations`
} else {
// Merge: intelligently rewrite integrating both sources
prompt = `You are an expert note writer. Your task is to intelligently merge an existing note with a resource into a single, coherent, well-structured document.
LANGUAGE RULE: Respond in ${lang}. Match the language of the existing note.
EXISTING NOTE:
---
${existingContent || '(empty note)'}
---
RESOURCE TO INTEGRATE:
---
${resourceText}
---
INSTRUCTIONS:
- Combine both sources into one cohesive, well-organized document
- Eliminate redundancy — include each piece of information only once
- Preserve the key ideas from both sources
- Maintain a logical structure with clear headings if appropriate
- Keep the tone and style consistent
- Respond ONLY with the merged content, no meta-commentary or explanations`
}
const enrichedContent = await provider.generateText(prompt)
return NextResponse.json({ enrichedContent: enrichedContent.trim() })
} catch (error: any) {
console.error('[enrich-from-resource] Error:', error)
return NextResponse.json(
{ error: error.message || 'Failed to enrich content' },
{ status: 500 }
)
}
}