feat: IA Note — rename panel, add Resource tab + chat hover-actions
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 44s

- 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)
This commit is contained in:
2026-05-02 21:06:25 +02:00
parent 547c5ffecb
commit 99d0583871
4 changed files with 552 additions and 27 deletions

View File

@@ -97,3 +97,107 @@ export async function fetchLinkMetadata(url: string): Promise<LinkMetadata | nul
return null;
}
}
/**
* Scrape full readable text content from a URL.
* Removes nav, header, footer, scripts, and ads — keeps main content only.
* Returns markdown-structured plain text (preserves paragraph/heading structure).
*/
export async function scrapePageText(url: string): Promise<{ text: string; title: string } | null> {
try {
let targetUrl = url
if (!url.startsWith('http://') && !url.startsWith('https://')) {
targetUrl = 'https://' + url
}
// SSRF protection
const parsed = new URL(targetUrl)
const hostname = parsed.hostname.toLowerCase()
const blockedHosts = ['localhost', '127.0.0.1', '0.0.0.0', '::1', '169.254.169.254']
if (blockedHosts.includes(hostname)) return null
if (hostname.startsWith('10.') || hostname.startsWith('172.') || hostname.startsWith('192.168.') || hostname.startsWith('fc') || hostname.startsWith('fd')) return null
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return null
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), 15000)
let response: Response
try {
response = await fetch(targetUrl, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'fr,en;q=0.8',
},
signal: controller.signal,
redirect: 'follow',
})
} catch (fetchError: any) {
clearTimeout(timeoutId)
console.error(`[ScrapeText] Fetch error for ${url}:`, fetchError.message)
return null
}
clearTimeout(timeoutId)
if (!response.ok) return null
const contentType = response.headers.get('content-type') || ''
if (!contentType.includes('text/html') && !contentType.includes('application/xhtml')) {
// Plain text or other — return raw text
const text = await response.text()
return { text: text.slice(0, 50000), title: url }
}
const html = await response.text()
const $ = cheerio.load(html)
// Extract title
const getMeta = (prop: string) =>
$(`meta[property="${prop}"]`).attr('content') ||
$(`meta[name="${prop}"]`).attr('content')
const title = getMeta('og:title') || $('title').text()?.trim() || url
// Remove noise elements
$('script, style, noscript, nav, header, footer, aside, iframe, img, svg, figure, form, button, input, select, textarea, [role="navigation"], [role="banner"], [role="complementary"], .ads, .advertisement, .cookie-banner, .popup, .modal').remove()
// Try to find main content container
const mainSelectors = ['main', 'article', '[role="main"]', '.content', '.post-content', '.article-body', '.entry-content', '#content', '#main']
let mainEl = null
for (const sel of mainSelectors) {
if ($(sel).length) { mainEl = $(sel).first(); break }
}
const container = mainEl || $('body')
// Extract text preserving paragraph/heading structure as markdown
const lines: string[] = []
container.find('h1, h2, h3, h4, h5, h6, p, li, blockquote, pre, td, th').each((_, el) => {
const tag = (el as any).tagName?.toLowerCase()
const text = $(el).text().trim()
if (!text || text.length < 3) return
if (['h1', 'h2', 'h3'].includes(tag)) {
lines.push(`\n## ${text}`)
} else if (['h4', 'h5', 'h6'].includes(tag)) {
lines.push(`\n### ${text}`)
} else if (tag === 'li') {
lines.push(`- ${text}`)
} else if (tag === 'blockquote') {
lines.push(`> ${text}`)
} else if (tag === 'pre') {
lines.push(`\`\`\`\n${text}\n\`\`\``)
} else {
lines.push(text)
}
})
const text = lines.join('\n').replace(/\n{3,}/g, '\n\n').trim()
// Limit to ~50k characters to avoid token overflows
return { text: text.slice(0, 50000), title: title.trim() }
} catch (error) {
console.error(`[ScrapeText] Error for ${url}:`, error)
return null
}
}

View File

@@ -0,0 +1,86 @@
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 }
)
}
}