fix: chat memory lost between messages + per-note history
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 1m11s
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 1m11s
Chat (AIChat floating widget): conversationId was never captured from the API response, so every message created a new conversation with no context. Now creates the conversation upfront before streaming (same pattern as ChatContainer) so the ID persists across messages. Note history: was stored globally in UserAISettings, so enabling history on one note enabled it for ALL notes. Now each Note has its own historyEnabled boolean field. The "Enable history" action only affects the specific note. A migration adds the column with default false. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -436,14 +436,12 @@ export async function commitNoteHistory(noteId: string) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) throw new Error('Unauthorized')
|
||||
|
||||
const enabled = await isNoteHistoryEnabledForUser(session.user.id)
|
||||
if (!enabled) throw new Error('History is disabled')
|
||||
|
||||
const note = await prisma.note.findFirst({
|
||||
where: { id: noteId, userId: session.user.id },
|
||||
select: { id: true },
|
||||
select: { id: true, historyEnabled: true },
|
||||
})
|
||||
if (!note) throw new Error('Note not found')
|
||||
if (!note.historyEnabled) throw new Error('History is disabled for this note')
|
||||
|
||||
await createNoteHistorySnapshot({
|
||||
noteId: note.id,
|
||||
@@ -466,6 +464,22 @@ export async function deleteNoteHistoryEntry(noteId: string, historyEntryId: str
|
||||
})
|
||||
}
|
||||
|
||||
export async function enableNoteHistory(noteId: string) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) throw new Error('Unauthorized')
|
||||
|
||||
const note = await prisma.note.findFirst({
|
||||
where: { id: noteId, userId: session.user.id },
|
||||
select: { id: true },
|
||||
})
|
||||
if (!note) throw new Error('Note not found')
|
||||
|
||||
await prisma.note.update({
|
||||
where: { id: noteId },
|
||||
data: { historyEnabled: true },
|
||||
})
|
||||
}
|
||||
|
||||
// Search notes - DB-side filtering (fast) with optional semantic search
|
||||
// Supports contextual search within notebook (IA5)
|
||||
export async function searchNotes(query: string, useSemantic: boolean = false, notebookId?: string) {
|
||||
@@ -641,14 +655,9 @@ export async function createNote(data: {
|
||||
}
|
||||
|
||||
try {
|
||||
const historyEnabled = await isNoteHistoryEnabledForUser(session.user.id)
|
||||
if (historyEnabled) {
|
||||
await createNoteHistorySnapshot({
|
||||
noteId: note.id,
|
||||
userId: session.user.id,
|
||||
reason: 'create',
|
||||
})
|
||||
}
|
||||
// New notes start with historyEnabled=false (schema default),
|
||||
// so no initial snapshot is needed here.
|
||||
// History is enabled per-note via enableNoteHistory() action.
|
||||
} catch (snapshotError) {
|
||||
console.error('[HISTORY] Failed to create initial snapshot:', snapshotError)
|
||||
}
|
||||
@@ -783,7 +792,7 @@ export async function updateNote(id: string, data: {
|
||||
try {
|
||||
const oldNote = await prisma.note.findUnique({
|
||||
where: { id, userId: session.user.id },
|
||||
select: { labels: true, notebookId: true, reminder: true, content: true, title: true }
|
||||
select: { labels: true, notebookId: true, reminder: true, content: true, title: true, historyEnabled: true }
|
||||
})
|
||||
const oldLabels: string[] = oldNote?.labels ? JSON.parse(oldNote.labels) : []
|
||||
const oldNotebookId = oldNote?.notebookId
|
||||
@@ -854,7 +863,7 @@ export async function updateNote(id: string, data: {
|
||||
}
|
||||
|
||||
try {
|
||||
const historyEnabled = await isNoteHistoryEnabledForUser(session.user.id)
|
||||
const historyEnabled = oldNote?.historyEnabled === true
|
||||
if (historyEnabled && shouldCaptureHistorySnapshot(data as Record<string, unknown>)) {
|
||||
const mode = await getNoteHistoryMode(session.user.id)
|
||||
if (mode === 'manual') {
|
||||
|
||||
Reference in New Issue
Block a user