From f67252b5e82985cd09a11c4feb4683112cb3fb3f Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Wed, 15 Jul 2026 23:27:58 -0400 Subject: [PATCH] refac --- backend/open_webui/routers/notes.py | 44 ++++++++++++++++------ src/lib/components/chat/Chat.svelte | 37 +++++++++--------- src/lib/components/notes/NoteEditor.svelte | 3 +- 3 files changed, 52 insertions(+), 32 deletions(-) diff --git a/backend/open_webui/routers/notes.py b/backend/open_webui/routers/notes.py index cb6a08dff7..b4555b2149 100644 --- a/backend/open_webui/routers/notes.py +++ b/backend/open_webui/routers/notes.py @@ -348,9 +348,14 @@ async def get_note_chat_by_id( changed = True system = ( - f'Note id: {note.id}. ' - 'Use view_note before reading or editing. ' - 'Use replace_note_content with content for whole-note updates or operations for selected/range updates.' + f'CONTEXT: +Current note id: {note.id} +' + 'This chat is attached to the current note. +' + 'For edit requests like make this concise, rewrite, enhance, shorten, or update: call view_note then replace_note_content. +' + 'Do not say an edit is done unless replace_note_content succeeds.' ) if params.get('system') != system: params['system'] = system @@ -378,9 +383,14 @@ async def get_note_chat_by_id( 'models': [''], 'params': { 'system': ( - f'Note id: {note.id}. ' - 'Use view_note before reading or editing. ' - 'Use replace_note_content with content for whole-note updates or operations for selected/range updates.' + f'CONTEXT: +Current note id: {note.id} +' + 'This chat is attached to the current note. +' + 'For edit requests like make this concise, rewrite, enhance, shorten, or update: call view_note then replace_note_content. +' + 'Do not say an edit is done unless replace_note_content succeeds.' ) }, 'history': {'messages': {}, 'currentId': None}, @@ -441,9 +451,14 @@ async def get_note_chats_by_id( changed = True system = ( - f'Note id: {note.id}. ' - 'Use view_note before reading or editing. ' - 'Use replace_note_content with content for whole-note updates or operations for selected/range updates.' + f'CONTEXT: +Current note id: {note.id} +' + 'This chat is attached to the current note. +' + 'For edit requests like make this concise, rewrite, enhance, shorten, or update: call view_note then replace_note_content. +' + 'Do not say an edit is done unless replace_note_content succeeds.' ) if params.get('system') != system: params['system'] = system @@ -503,9 +518,14 @@ async def create_note_chat_by_id( 'models': [''], 'params': { 'system': ( - f'Note id: {note.id}. ' - 'Use view_note before reading or editing. ' - 'Use replace_note_content with content for whole-note updates or operations for selected/range updates.' + f'CONTEXT: +Current note id: {note.id} +' + 'This chat is attached to the current note. +' + 'For edit requests like make this concise, rewrite, enhance, shorten, or update: call view_note then replace_note_content. +' + 'Do not say an edit is done unless replace_note_content succeeds.' ) }, 'history': {'messages': {}, 'currentId': None}, diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 9f80f77592..6a260953c3 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -368,9 +368,9 @@ let chatFiles = []; let files = []; let params = {}; - let appliedInitialFilesKey = ''; + let attachedFilesKey = ''; let loadedChatIdProp = ''; - let loadedEmbeddedDraftKey = ''; + let currentDraftKey = ''; const mergeFiles = (current, incoming) => { const seen = new Set(); @@ -387,11 +387,11 @@ const key = JSON.stringify( initialFiles.map((file) => `${file?.type ?? ''}:${file?.id ?? file?.url ?? file?.name ?? ''}`) ); - if (key === appliedInitialFilesKey) return; + if (key === attachedFilesKey) return; files = mergeFiles(files, initialFiles); chatFiles = mergeFiles(chatFiles, initialFiles); - appliedInitialFilesKey = key; + attachedFilesKey = key; }; const withSelectedText = (text: string) => embedded && selectedText?.trim() @@ -419,9 +419,9 @@ navigateHandler(); } - $: if (embedded && embeddedDraftKey && embeddedDraftKey !== loadedEmbeddedDraftKey) { + $: if (embedded && embeddedDraftKey && embeddedDraftKey !== currentDraftKey) { noteChatDebug('embedded draft requested', { embeddedDraftKey }); - loadedEmbeddedDraftKey = embeddedDraftKey; + currentDraftKey = embeddedDraftKey; initEmbeddedDraft(); } @@ -2708,13 +2708,13 @@ } history = history; - // Empty embedded drafts create their backing chat only when the first message is sent. - if (!_chatId) { - if (embedded && onCreateEmbeddedChat) { - const createdChat = await onCreateEmbeddedChat(); - if (!createdChat?.id) { - toast.error($i18n.t('Failed to create chat')); - return; + // Empty embedded drafts create their backing chat only when the first message is sent. + if (!_chatId) { + if (embedded && onCreateEmbeddedChat) { + const createdChat = await onCreateEmbeddedChat(); + if (!createdChat?.id) { + toast.error($i18n.t('Failed to create chat')); + return; } chat = createdChat; @@ -2723,11 +2723,12 @@ await chatId.set(_chatId); await chatTitle.set(createdChat?.chat?.title ?? createdChat?.title ?? $i18n.t('Chat')); - params = structuredClone(createdChat?.chat?.params ?? {}); - delete params.note_id; - chatFiles = mergeFiles(chatFiles, createdChat?.chat?.files ?? []); - applyInitialFiles(); - } else if ($temporaryChatEnabled) { + params = structuredClone(createdChat?.chat?.params ?? {}); + delete params.note_id; + chatFiles = mergeFiles(chatFiles, createdChat?.chat?.files ?? []); + applyInitialFiles(); + await onSelectEmbeddedChat?.(_chatId); + } else if ($temporaryChatEnabled) { _chatId = `local:${$socket?.id}`; await chatId.set(_chatId); } diff --git a/src/lib/components/notes/NoteEditor.svelte b/src/lib/components/notes/NoteEditor.svelte index 70128dcf3d..86c952d059 100644 --- a/src/lib/components/notes/NoteEditor.svelte +++ b/src/lib/components/notes/NoteEditor.svelte @@ -712,8 +712,6 @@ ${content} : null; if (chat?.id) { - noteChatId = chat.id; - noteChatDraftKey = ''; noteChats = chats ?? [chat, ...noteChats.filter((item) => item.id !== chat.id)]; showNoteChat = true; } @@ -1440,6 +1438,7 @@ ${content} onSelectEmbeddedChat={(chatId) => { if (!chatId || chatId === noteChatId) return; noteChatId = chatId; + noteChatDraftKey = ''; }} onDeleteEmbeddedChat={deleteNoteChat} onEmbeddedChatTitle={(chatId, title) => {