Centralise la réserve via ai-quota, corrige admin unavailable (-1), brancher les routes sans quota et le host-pays brainstorm, avec usage-meter élargi, noms de clusters, MCP et ajustements dashboard/insights. Co-authored-by: Cursor <cursoragent@cursor.com>
99 lines
3.6 KiB
TypeScript
99 lines
3.6 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { auth } from '@/auth'
|
|
import { getSystemConfig } from '@/lib/config'
|
|
import { getTagsProvider } from '@/lib/ai/factory'
|
|
import { hasUserAiConsent } from '@/lib/consent/server-consent'
|
|
import { withAiQuota, handleQuotaHttpError } from '@/lib/ai-quota'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const session = await auth()
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
if (!(await hasUserAiConsent())) {
|
|
return NextResponse.json({ error: 'ai_consent_required' }, { status: 403 })
|
|
}
|
|
|
|
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') {
|
|
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 {
|
|
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 withAiQuota(
|
|
session.user.id,
|
|
'reformulate',
|
|
() => provider.generateText(prompt),
|
|
{ lane: 'chat' },
|
|
)
|
|
|
|
return NextResponse.json({ enrichedContent: enrichedContent.trim() })
|
|
} catch (error: unknown) {
|
|
const quotaResp = handleQuotaHttpError(error)
|
|
if (quotaResp) return quotaResp
|
|
console.error('[enrich-from-resource] Error:', error)
|
|
const message = error instanceof Error ? error.message : 'Failed to enrich content'
|
|
return NextResponse.json({ error: message }, { status: 500 })
|
|
}
|
|
}
|