Skip to content

The agent that grows with you

feat(desktop): let a composer draft move between windows

Brooklyn Nicholson8560dc6b9777
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts
index b5bf87dc466..c32db0f6c22 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts
@@ -3,7 +3,14 @@ import { type RefObject, useCallback, useEffect, useLayoutEffect, useRef, useSta
import { SLASH_COMMAND_RE } from '@/lib/chat-runtime'
import { sanitizeComposerInput } from '@/lib/composer-input-sanitize'
-import { type ComposerAttachment, stashSessionDraft, takeSessionDraft } from '@/store/composer'
+import {
+ type ComposerAttachment,
+ type ComposerDraftSyncMode,
+ onComposerDraftSyncRequest,
+ reloadPersistedDrafts,
+ stashSessionDraft,
+ takeSessionDraft
+} from '@/store/composer'
import { isBrowsingHistory } from '@/store/composer-input-history'
import {
@@ -391,6 +398,38 @@ export function useComposerDraft({
}
}, [activeQueueSessionKey]) // eslint-disable-line react-hooks/exhaustive-deps
+ // The HUD handoff's two verbs. Entering HUD mode flushes this editor's text
+ // into the shared stash so the HUD's composer boots with it; leaving repaints
+ // from the stash so whatever the HUD typed (or sent, clearing it) is what the
+ // app window shows. The per-session swap effect above can't cover either one:
+ // the session scope doesn't change, so it never re-consults the stash.
+ const syncDraft = (mode: ComposerDraftSyncMode) => {
+ if (mode === 'flush') {
+ window.clearTimeout(draftPersistTimerRef.current)
+ pendingDraftPersistRef.current = null
+ stashAt(draftScopeRef.current, syncDraftFromEditor())
+
+ return
+ }
+
+ reloadPersistedDrafts()
+ const stashed = takeSessionDraft(draftScopeRef.current)
+ loadIntoComposer(stashed.text, stashed.attachments)
+ }
+
+ const syncDraftRef = useRef(syncDraft)
+ syncDraftRef.current = syncDraft
+
+ useEffect(
+ () =>
+ onComposerDraftSyncRequest(({ mode, target: requested }) => {
+ if (requested === target) {
+ syncDraftRef.current(mode)
+ }
+ }),
+ [target]
+ )
+
// pagehide is load-bearing: React skips effect cleanups on reload, so Cmd+R
// inside the debounce/rAF window would drop trailing keystrokes without this.
// eslint-disable-next-line no-restricted-syntax -- legitimate non-atom ref write (see eslint rule comment)
diff --git a/apps/desktop/src/store/composer.ts b/apps/desktop/src/store/composer.ts
index 7f32348cd36..6783db9637a 100644
--- a/apps/desktop/src/store/composer.ts
+++ b/apps/desktop/src/store/composer.ts
@@ -151,6 +151,84 @@ function loadPersistedDraftTexts(): [string, SessionDraft][] {
const draftsBySession = new Map<string, SessionDraft>(loadPersistedDraftTexts())
+/**
+ * Re-read the persisted drafts written by ANOTHER window into this one's map.
+ *
+ * Drafts are per-renderer state backed by shared localStorage, and the map
+ * above is read exactly once at module load. Two windows on the same session
+ * (HUD mode ⇄ the app window) therefore diverge the moment either one types:
+ * whichever window mounted first keeps its stale copy forever, so text typed
+ * in the HUD is simply gone when you return to the app.
+ *
+ * Merge, don't clobber — the local map may hold attachments (never persisted)
+ * that the incoming text-only snapshot can't know about.
+ */
+export function reloadPersistedDrafts(): void {
+ const incoming = new Map(loadPersistedDraftTexts())
+
+ for (const [key, draft] of incoming) {
+ const local = draftsBySession.get(key)
+ draftsBySession.set(key, local?.attachments.length ? { ...local, text: draft.text } : draft)
+ }
+
+ // A key that vanished from storage was cleared (sent) in the other window.
+ for (const key of [...draftsBySession.keys()]) {
+ if (!incoming.has(key)) {
+ draftsBySession.delete(key)
+ }
+ }
+}
+
+// localStorage `storage` events fire across Electron BrowserWindows of the
+// same origin, so the other window's write is the sync signal.
+if (typeof window !== 'undefined') {
+ window.addEventListener('storage', event => {
+ if (event.key === SESSION_DRAFTS_STORAGE_KEY) {
+ reloadPersistedDrafts()
+ }
+ })
+}
+
+/**
+ * Push a composer's live text into the shared stash (`flush`), or repaint it
+ * from the stash (`reload`).
+ *
+ * Both halves of the HUD handoff need this. The stash is the only draft state
+ * two windows share, but a mounted composer only consults it when its session
+ * scope changes — so entering HUD mode has to flush the app window's in-editor
+ * text down to the stash before the HUD boots and reads it, and leaving has to
+ * repaint the app's editor from whatever the HUD left behind (usually empty,
+ * because the HUD sent it).
+ *
+ * Dispatched synchronously, unlike the focus bus: the flush must complete
+ * before the HUD window is created.
+ */
+const DRAFT_SYNC_EVENT = 'hermes:composer-draft-sync'
+
+export type ComposerDraftSyncMode = 'flush' | 'reload'
+
+interface ComposerDraftSyncDetail {
+ mode: ComposerDraftSyncMode
+ target: string
+}
+
+export function requestComposerDraftSync(mode: ComposerDraftSyncMode, target = 'main'): void {
+ if (typeof window !== 'undefined') {
+ window.dispatchEvent(new CustomEvent<ComposerDraftSyncDetail>(DRAFT_SYNC_EVENT, { detail: { mode, target } }))
+ }
+}
+
+export function onComposerDraftSyncRequest(handler: (detail: ComposerDraftSyncDetail) => void): () => void {
+ if (typeof window === 'undefined') {
+ return () => undefined
+ }
+
+ const listener = (event: Event) => handler((event as CustomEvent<ComposerDraftSyncDetail>).detail)
+ window.addEventListener(DRAFT_SYNC_EVENT, listener)
+
+ return () => window.removeEventListener(DRAFT_SYNC_EVENT, listener)
+}
+
function persistDraftTexts() {
try {
const entries = [...draftsBySession]