From b712a0a9cb394353c09410711d04bc6064a2cc6a Mon Sep 17 00:00:00 2001 From: pewdiepie-archdaemon Date: Mon, 29 Jun 2026 09:18:26 +0000 Subject: [PATCH] Reuse open email drafts for agent replies --- static/js/document.js | 1 + static/js/emailInbox.js | 14 ++++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/static/js/document.js b/static/js/document.js index 759761514..c2745c6b6 100644 --- a/static/js/document.js +++ b/static/js/document.js @@ -10408,6 +10408,7 @@ const documentModule = { newDocument, loadDocument, injectFreshDoc, + replaceEmailReplyBody, ensurePaneMounted: _ensureDocPaneMounted, loadSessionDocs, ensureDocPanel, diff --git a/static/js/emailInbox.js b/static/js/emailInbox.js index 35eefff61..7f0e24773 100644 --- a/static/js/emailInbox.js +++ b/static/js/emailInbox.js @@ -914,12 +914,10 @@ async function _openEmail(em, itemEl, preloadedData = null, mode = 'reply', note } if (_docModule) { - // Only reuse an existing doc tab if the user really just wants to "view" - // the email again. For reply/reply-all/forward/ai-reply, always create - // a fresh draft — otherwise a previously-emptied doc (sent reply, AI - // reply that came back blank, etc.) keeps coming back instead of a - // proper pre-filled reply. - const reuseExisting = (mode === 'view' || mode === 'open'); + // Agent-provided reply text should land in the email draft the user + // already has open. Otherwise mobile users see the source email while the + // agent silently creates a second draft elsewhere. + const reuseExisting = (mode === 'view' || mode === 'open' || (!!aiSuggestedBody && mode !== 'forward')); const existingDocId = (reuseExisting && _docModule.findEmailDocId) ? _docModule.findEmailDocId(em.uid, _currentFolder) : null; @@ -927,6 +925,10 @@ async function _openEmail(em, itemEl, preloadedData = null, mode = 'reply', note if (!_docModule.isPanelOpen()) _docModule.openPanel(); await new Promise(r => requestAnimationFrame(() => requestAnimationFrame(r))); await _docModule.loadDocument(existingDocId); + if (aiSuggestedBody && typeof _docModule.replaceEmailReplyBody === 'function') { + await _docModule.replaceEmailReplyBody(existingDocId, aiSuggestedBody); + _bringEmailReplyDraftToFrontOnMobile(); + } } else { // If the user already has a chat session open, reuse it instead of // spawning a new one. They asked for this explicitly — opening reply