90 lines
3.2 KiB
TypeScript
90 lines
3.2 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, format } = 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 outputFormat = format === 'html' ? 'HTML (with proper tags like <h2>, <p>, <ul>, <li>)' : 'Markdown (with ##, -, **, etc.)'
|
|
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.
|
|
FORMAT RULE: Respond in ${outputFormat}.
|
|
|
|
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 and the requested FORMAT RULE
|
|
- 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.
|
|
FORMAT RULE: Respond in ${outputFormat}.
|
|
|
|
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 with the requested FORMAT RULE
|
|
- 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 }
|
|
)
|
|
}
|
|
}
|