From 5d5500fbb32193e0d3d391eaef6255b19c0ff718 Mon Sep 17 00:00:00 2001 From: pewdiepie-archdaemon Date: Mon, 29 Jun 2026 03:10:42 +0000 Subject: [PATCH] Keep open editor drafts in chat context --- routes/chat_routes.py | 9 +++++++++ static/js/chat.js | 11 +++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/routes/chat_routes.py b/routes/chat_routes.py index 1ddf15334..35605fcf3 100644 --- a/routes/chat_routes.py +++ b/routes/chat_routes.py @@ -729,6 +729,15 @@ def setup_chat_routes( logger.info(f"[doc-inject] found by ID: title={active_doc.title!r}, lang={active_doc.language!r}, is_active={active_doc.is_active}, content_len={len(active_doc.current_content or '')}") else: logger.warning(f"[doc-inject] NOT FOUND by ID {active_doc_id}") + if not active_doc: + _email_doc_q = _doc_db.query(DBDocument).filter( + DBDocument.session_id == session, + DBDocument.is_active == True, + DBDocument.language == "email", + ) + active_doc = _owner_session_filter(_email_doc_q, ctx.user).order_by(DBDocument.updated_at.desc()).first() + if active_doc: + logger.info(f"[doc-inject] found email draft by session fallback: title={active_doc.title!r}") if not active_doc: _session_doc_q = _doc_db.query(DBDocument).filter( DBDocument.session_id == session, diff --git a/static/js/chat.js b/static/js/chat.js index 5c3e98ec1..75d5ca014 100644 --- a/static/js/chat.js +++ b/static/js/chat.js @@ -780,7 +780,10 @@ import { wireArrowUpRecall, getLastUserMessageFromChatHistory } from './composer } // Auto-save document editor content before sending so the AI sees latest text - if (documentModule && documentModule.isPanelOpen() && documentModule.getCurrentDocId()) { + const activeDocIdForSend = documentModule && typeof documentModule.getCurrentDocId === 'function' + ? documentModule.getCurrentDocId() + : null; + if (documentModule && activeDocIdForSend) { try { await documentModule.saveDocument(); } catch(e) { console.warn('doc auto-save failed', e); } } @@ -812,9 +815,9 @@ import { wireArrowUpRecall, getLastUserMessageFromChatHistory } from './composer fd.append('session', streamSessionId); if (ids.length) fd.append('attachments', JSON.stringify(ids)); // Auto-save & send active doc ID so the backend sees latest content - if (documentModule && documentModule.isPanelOpen() && documentModule.getCurrentDocId()) { + if (documentModule && activeDocIdForSend) { try { await documentModule.saveDocument({ silent: true }); } catch (_e) { /* best-effort */ } - fd.append('active_doc_id', documentModule.getCurrentDocId()); + fd.append('active_doc_id', activeDocIdForSend); } // Active email context — when an email reader is open, pass its // uid/folder/account so "reply", "summarize", "what does this say" @@ -836,7 +839,7 @@ import { wireArrowUpRecall, getLastUserMessageFromChatHistory } from './composer const isIncognito = !!(incognitoChk && incognitoChk.checked); // Auto-escalate to agent mode when a document is open — the user expects // the AI to see the document and have tools to edit it - if (!isIncognito && !isAgentMode && documentModule && documentModule.isPanelOpen() && documentModule.getCurrentDocId()) { + if (!isIncognito && !isAgentMode && documentModule && activeDocIdForSend) { isAgentMode = true; } if (isIncognito) isAgentMode = false;