feat(notes): liens internes, onglet Réseau, living blocks et consentement IA
Some checks failed
CI / Lint, Test & Build (push) Failing after 1m19s
CI / Deploy production (on server) (push) Has been skipped

Rend les liens entre notes visibles et persistants (sync NoteLink au save, auto-save, graphe réseau rafraîchi), ajoute living blocks, Memory Echo, recherche globale, consentement IA explicite et consolide les prototypes design en architectural-grid.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Antigravity
2026-05-24 14:27:29 +00:00
parent 077e665dfc
commit e2672cd2c2
323 changed files with 20670 additions and 42431 deletions

View File

@@ -0,0 +1,88 @@
import { NextRequest, NextResponse } from 'next/server'
import prisma from '@/lib/prisma'
import { auth } from '@/auth'
/**
* GET /api/notes/[id]/live-block-refs
* Notes that embed a living block sourced from this note.
*/
export async function GET(
_req: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const session = await auth()
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const { id: sourceNoteId } = await params
const sourceNote = await prisma.note.findFirst({
where: { id: sourceNoteId, userId: session.user.id },
select: { id: true },
})
if (!sourceNote) {
return NextResponse.json({ error: 'Note not found' }, { status: 404 })
}
const refs = await prisma.liveBlockRef.findMany({
where: { sourceNoteId },
orderBy: { createdAt: 'desc' },
select: {
id: true,
blockId: true,
targetNoteId: true,
createdAt: true,
targetNote: {
select: {
id: true,
title: true,
notebookId: true,
},
},
},
})
const notebookIds = [...new Set(
refs.map(r => r.targetNote.notebookId).filter(Boolean) as string[]
)]
const notebooks = notebookIds.length > 0
? await prisma.notebook.findMany({
where: { id: { in: notebookIds }, userId: session.user.id },
select: { id: true, name: true },
})
: []
const notebookMap = Object.fromEntries(notebooks.map(nb => [nb.id, nb.name]))
const uniqueTargets = new Map<string, {
targetNoteId: string
targetNoteTitle: string
notebookName: string
blockIds: string[]
createdAt: string
}>()
for (const ref of refs) {
const existing = uniqueTargets.get(ref.targetNoteId)
if (existing) {
if (!existing.blockIds.includes(ref.blockId)) {
existing.blockIds.push(ref.blockId)
}
continue
}
uniqueTargets.set(ref.targetNoteId, {
targetNoteId: ref.targetNoteId,
targetNoteTitle: ref.targetNote.title || '',
notebookName: ref.targetNote.notebookId
? (notebookMap[ref.targetNote.notebookId] || '')
: '',
blockIds: [ref.blockId],
createdAt: ref.createdAt.toISOString(),
})
}
return NextResponse.json({
refs: Array.from(uniqueTargets.values()),
total: uniqueTargets.size,
})
}

View File

@@ -0,0 +1,184 @@
import { NextRequest, NextResponse } from 'next/server'
import prisma from '@/lib/prisma'
import { auth } from '@/auth'
import { memoryEchoService } from '@/lib/ai/services/memory-echo.service'
import { hasUserAiConsent } from '@/lib/consent/server-consent'
import { SEMANTIC_SIMILARITY_FLOOR, SEMANTIC_SIMILARITY_FLOOR_DEMO } from '@/lib/ai/semantic-proximity'
import { extractNoteLinkTargets } from '@/lib/notes/sync-note-links'
function excerpt(text: string, max = 120): string {
const plain = text.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim()
if (plain.length <= max) return plain
return `${plain.slice(0, max).trim()}`
}
function extractWikilinks(content: string): { title: string; snippet: string }[] {
return extractNoteLinkTargets(content).map(t => ({
title: t.title,
snippet: t.snippet,
}))
}
export async function GET(
_request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const session = await auth()
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const { id } = await params
const note = await prisma.note.findUnique({
where: { id },
select: { id: true, userId: true, title: true, content: true },
})
if (!note || note.userId !== session.user.id) {
return NextResponse.json({ error: 'Not found' }, { status: 404 })
}
let backlinks: Awaited<ReturnType<typeof prisma.noteLink.findMany>> = []
let outbound: Awaited<ReturnType<typeof prisma.noteLink.findMany>> = []
try {
;[backlinks, outbound] = await Promise.all([
prisma.noteLink.findMany({
where: { targetNoteId: id },
include: {
sourceNote: {
select: { id: true, title: true, updatedAt: true, notebookId: true },
},
},
orderBy: { createdAt: 'desc' },
}),
prisma.noteLink.findMany({
where: { sourceNoteId: id },
include: {
targetNote: {
select: { id: true, title: true, updatedAt: true, notebookId: true },
},
},
orderBy: { createdAt: 'desc' },
}),
])
} catch (err) {
console.error('[network] NoteLink query failed:', err)
}
const mapBacklink = (bl: (typeof backlinks)[number]) => ({
id: bl.id,
note: bl.sourceNote,
contextSnippet: bl.contextSnippet,
createdAt: bl.createdAt,
})
const mapOutbound = (ol: (typeof outbound)[number]) => ({
id: ol.id,
note: ol.targetNote,
contextSnippet: ol.contextSnippet,
createdAt: ol.createdAt,
})
const backlinkRows = backlinks.map(mapBacklink)
const outboundRows = outbound.map(mapOutbound)
const linkedTitles = new Set(
outboundRows.map(link => (link.note?.title || '').toLowerCase()).filter(Boolean)
)
const wikilinks = extractWikilinks(note.content || '')
const unlinkedMentions = wikilinks
.filter(w => !linkedTitles.has(w.title.toLowerCase()))
.map(w => ({
title: w.title,
snippet: w.snippet,
}))
const liveBlockRefs = await prisma.liveBlockRef.findMany({
where: { sourceNoteId: id },
orderBy: { createdAt: 'desc' },
select: {
id: true,
blockId: true,
targetNoteId: true,
targetNote: {
select: { id: true, title: true, notebookId: true },
},
},
})
const embedHosts = new Map<string, {
note: { id: string; title: string | null; notebookId: string | null }
blockIds: string[]
}>()
for (const ref of liveBlockRefs) {
const existing = embedHosts.get(ref.targetNoteId)
if (existing) {
if (!existing.blockIds.includes(ref.blockId)) existing.blockIds.push(ref.blockId)
continue
}
embedHosts.set(ref.targetNoteId, {
note: ref.targetNote,
blockIds: [ref.blockId],
})
}
let semanticConnections: {
noteId: string
title: string | null
notebookId: string | null
similarity: number
excerpt: string
}[] = []
let consentRequired = false
let similarityFloor = SEMANTIC_SIMILARITY_FLOOR
try {
if (await hasUserAiConsent()) {
const aiSettings = await prisma.userAISettings.findUnique({
where: { userId: session.user.id },
select: { demoMode: true },
})
similarityFloor = aiSettings?.demoMode ? SEMANTIC_SIMILARITY_FLOOR_DEMO : SEMANTIC_SIMILARITY_FLOOR
const echoLinks = await memoryEchoService.getConnectionsForNote(id, session.user.id)
const otherIds = echoLinks.map(conn => (conn.note1.id === id ? conn.note2.id : conn.note1.id))
const notebookRows = otherIds.length > 0
? await prisma.note.findMany({
where: { id: { in: otherIds }, userId: session.user.id },
select: { id: true, notebookId: true },
})
: []
const notebookByNote = Object.fromEntries(notebookRows.map(n => [n.id, n.notebookId]))
semanticConnections = echoLinks.map(conn => {
const isNote1Target = conn.note1.id === id
const other = isNote1Target ? conn.note2 : conn.note1
return {
noteId: other.id,
title: other.title,
notebookId: notebookByNote[other.id] ?? null,
similarity: conn.similarityScore,
excerpt: excerpt(other.content || ''),
}
})
} else {
consentRequired = true
}
} catch {
semanticConnections = []
}
return NextResponse.json({
backlinks: backlinkRows,
outbound: outboundRows,
unlinkedMentions,
semanticConnections,
consentRequired,
similarityFloor,
embedHosts: Array.from(embedHosts.values()).map(entry => ({
note: entry.note,
blockIds: entry.blockIds,
})),
})
}

View File

@@ -9,6 +9,7 @@ import {
shouldCaptureHistorySnapshot,
shouldCreateAutoSnapshot,
} from '@/lib/note-history'
import { syncNoteLinksForNote } from '@/lib/notes/sync-note-links'
// GET /api/notes/[id] - Get a single note
export async function GET(
@@ -167,9 +168,11 @@ export async function PUT(
console.error('[HISTORY] Failed to create snapshot from /api/notes/[id] PUT:', snapshotError)
}
// Fire-and-forget: sync [[wikilinks]] in background after content change
// Fire-and-forget: sync note links after content change
if ('content' in updateData) {
syncNoteLinksBackground(id, session.user.id, note.content).catch(() => {})
syncNoteLinksForNote(id, session.user.id, note.content).catch(err => {
console.error('[NoteLink] sync failed after PUT:', err)
})
}
return NextResponse.json({
@@ -185,56 +188,6 @@ export async function PUT(
}
}
/** Background job: parse [[wikilinks]] and sync NoteLink table */
async function syncNoteLinksBackground(noteId: string, userId: string, content: string) {
const WIKILINK_RE = /\[\[([^\]|#]+?)(?:[|#][^\]]+)?\]\]/g
const plain = content.replace(/<[^>]+>/g, ' ')
const wikilinks: { title: string; snippet: string }[] = []
const seen = new Set<string>()
let match: RegExpExecArray | null
WIKILINK_RE.lastIndex = 0
while ((match = WIKILINK_RE.exec(plain)) !== null) {
const title = match[1].trim()
if (!title || seen.has(title.toLowerCase())) continue
seen.add(title.toLowerCase())
const start = Math.max(0, match.index - 50)
const end = Math.min(plain.length, match.index + match[0].length + 50)
const snippet = plain.slice(start, end).replace(/\s+/g, ' ').trim()
wikilinks.push({ title, snippet })
}
const upsertedIds: string[] = []
for (const { title, snippet } of wikilinks) {
let targetNote = await prisma.note.findFirst({
where: { userId, title: { equals: title, mode: 'insensitive' }, trashedAt: null },
select: { id: true },
})
if (!targetNote) {
targetNote = await prisma.note.create({
data: { title, content: '', userId, type: 'richtext', color: 'default', isMarkdown: true, order: 0 },
select: { id: true },
})
}
if (targetNote.id === noteId) continue
await (prisma as any).noteLink.upsert({
where: { sourceNoteId_targetNoteId: { sourceNoteId: noteId, targetNoteId: targetNote.id } },
update: { contextSnippet: snippet.slice(0, 200) },
create: { id: crypto.randomUUID(), sourceNoteId: noteId, targetNoteId: targetNote.id, contextSnippet: snippet.slice(0, 200) },
})
upsertedIds.push(targetNote.id)
}
if (upsertedIds.length > 0) {
await (prisma as any).noteLink.deleteMany({
where: { sourceNoteId: noteId, targetNoteId: { notIn: upsertedIds } },
})
} else {
await (prisma as any).noteLink.deleteMany({ where: { sourceNoteId: noteId } })
}
}
// DELETE /api/notes/[id] - Delete a note
export async function DELETE(
request: NextRequest,

View File

@@ -1,42 +1,11 @@
import { NextRequest, NextResponse } from 'next/server'
import prisma from '@/lib/prisma'
import { auth } from '@/auth'
import { syncNoteLinksForNote } from '@/lib/notes/sync-note-links'
const WIKILINK_RE = /\[\[([^\]|#]+?)(?:[|#][^\]]+)?\]\]/g
/**
* Extract [[wikilink]] targets from markdown/html content.
* Returns deduplicated list of linked note titles.
*/
function extractWikilinks(content: string): { title: string; snippet: string }[] {
// Strip HTML tags
const plain = content.replace(/<[^>]+>/g, ' ')
const results: { title: string; snippet: string }[] = []
const seen = new Set<string>()
let match: RegExpExecArray | null
WIKILINK_RE.lastIndex = 0
while ((match = WIKILINK_RE.exec(plain)) !== null) {
const title = match[1].trim()
if (!title || seen.has(title.toLowerCase())) continue
seen.add(title.toLowerCase())
// Extract snippet: 50 chars before + after the match
const start = Math.max(0, match.index - 50)
const end = Math.min(plain.length, match.index + match[0].length + 50)
const snippet = plain.slice(start, end).replace(/\s+/g, ' ').trim()
results.push({ title, snippet })
}
return results
}
// POST /api/notes/[id]/sync-links
// Parse [[wikilinks]] in note content and sync the NoteLink table.
// Called automatically after note save.
// POST /api/notes/[id]/sync-links — resynchronise les liens internes depuis le contenu
export async function POST(
request: NextRequest,
_request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const session = await auth()
@@ -56,75 +25,6 @@ export async function POST(
return NextResponse.json({ error: 'Not found' }, { status: 404 })
}
const wikilinks = extractWikilinks(note.content)
// For each wikilink, find or create the target note
const upsertedLinks: string[] = []
for (const { title, snippet } of wikilinks) {
// Find target note by title (case-insensitive) belonging to same user
let targetNote = await prisma.note.findFirst({
where: {
userId,
title: { equals: title, mode: 'insensitive' },
trashedAt: null,
},
select: { id: true },
})
if (!targetNote) {
// Create a stub note so the link resolves
targetNote = await prisma.note.create({
data: {
title,
content: '',
userId,
type: 'richtext',
color: 'default',
isMarkdown: true,
order: 0,
},
select: { id: true },
})
}
// Skip self-links
if (targetNote.id === id) continue
// Upsert the NoteLink
await (prisma as any).noteLink.upsert({
where: {
sourceNoteId_targetNoteId: {
sourceNoteId: id,
targetNoteId: targetNote.id,
},
},
update: { contextSnippet: snippet.slice(0, 200) },
create: {
id: crypto.randomUUID(),
sourceNoteId: id,
targetNoteId: targetNote.id,
contextSnippet: snippet.slice(0, 200),
},
})
upsertedLinks.push(targetNote.id)
}
// Delete obsolete links (links that existed before but wikilink was removed)
if (upsertedLinks.length > 0) {
await (prisma as any).noteLink.deleteMany({
where: {
sourceNoteId: id,
targetNoteId: { notIn: upsertedLinks },
},
})
} else {
// No wikilinks at all — remove all outgoing links from this note
await (prisma as any).noteLink.deleteMany({
where: { sourceNoteId: id },
})
}
return NextResponse.json({ synced: upsertedLinks.length })
const synced = await syncNoteLinksForNote(id, userId, note.content || '')
return NextResponse.json({ synced })
}