fix(preview): agents see your browser page, and keep working while you're in another thread #18584
Closedt3dotgg(GitHub profile, opens in a new tab)wants to merge 6 commits into
main fromt3/preview-late-desktop-attachopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:S
juliusmarminge added labelsmacroscope-review
- apps/server/src/preview/ServerBrowser.ts line 2430Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2408,7 +2410,14 @@24082410 // The page came back, for example after its DevTools closed. Reconnect now24092411 // rather than on the next viewer or agent, or the tab's URL and title stop24102412 // reaching every client in the meantime.2413+ // A tab that went headless before the desktop mounted it (its thread was in2414+ // the background) moves onto the desktop's page. Otherwise the user and the2415+ // agent see two different pages under one tab.24112416 yield* desktopChannel.attached.pipe(24122417 Stream.runForEach((key) =>24132418 Effect.gen(function* () {2414- if (tabs.has(tabKey(key.threadId, key.tabId))) return;2419+ const pending = pendingTabs.get(tabKey(key.threadId, key.tabId));2420+ if (pending) yield* Effect.promise(() => pending.catch(constVoid));2421+ const existing = tabs.get(tabKey(key.threadId, key.tabId));2422+ if (existing?.desktop) return;2423+ if (existing) dropTab(existing, false, true);
- apps/server/src/preview/ServerBrowser.ts line 695Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -683,11 +683,13 @@683683 /** Whether the preview session for a tab still exists. */684684 const sessionOpen = (tab: ServerTab) => !closedSessions.has(tabKey(tab.threadId, tab.tabId));685685686- const dropTab = (tab: ServerTab, closeSession: boolean) => {686+ /** `handOff` keeps the session for a new page, so viewers reconnect to it. */687+ const dropTab = (tab: ServerTab, closeSession: boolean, handOff = false) => {687688 const key = tabKey(tab.threadId, tab.tabId);688689 if (tabs.get(key) !== tab) return;689690 tabs.delete(key);690691 tab.closing = true;691692 clearAbortedNavigation(tab);692693 // A desktop page outlives the connection unless its session closed with it.693- const end = tab.desktop && !closeSession && sessionOpen(tab) ? "reconnect" : "gone";694+ const end =695+ (tab.desktop || handOff) && !closeSession && sessionOpen(tab) ? "reconnect" : "gone";apps/server/src/preview/ServerBrowser.ts line 2430Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -2408,7 +2410,14 @@24082410 // The page came back, for example after its DevTools closed. Reconnect now24092411 // rather than on the next viewer or agent, or the tab's URL and title stop24102412 // reaching every client in the meantime.2413+ // A tab that went headless before the desktop mounted it (its thread was in2414+ // the background) moves onto the desktop's page. Otherwise the user and the2415+ // agent see two different pages under one tab.24112416 yield* desktopChannel.attached.pipe(24122417 Stream.runForEach((key) =>24132418 Effect.gen(function* () {2414- if (tabs.has(tabKey(key.threadId, key.tabId))) return;2419+ const pending = pendingTabs.get(tabKey(key.threadId, key.tabId));2420+ if (pending) yield* Effect.promise(() => pending.catch(constVoid));2421+ const existing = tabs.get(tabKey(key.threadId, key.tabId));2422+ if (existing?.desktop) return;2423+ if (existing) dropTab(existing, false, true);
t3dotgg(GitHub profile, opens in a new tab) changed the title
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:S
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -2411,9 +2421,19 @@2421+ // the background) moves onto the desktop's page. Otherwise the user and the2422+ // agent see two different pages under one tab.24112423 yield* desktopChannel.attached.pipe(24122424 Stream.runForEach((key) =>24132425 Effect.gen(function* () {2414- if (tabs.has(tabKey(key.threadId, key.tabId))) return;2426+ const pending = pendingTabs.get(tabKey(key.threadId, key.tabId));2427+ if (pending) yield* Effect.promise(() => pending.catch(constVoid));2428+ const existing = tabs.get(tabKey(key.threadId, key.tabId));2429+ if (existing?.desktop) return;2430+ if (existing) dropTab(existing, false, true);24152431 const { sessions } = yield* manager.list({ threadId: ThreadId.make(key.threadId) });24162432 const snapshot = sessions.find(24172433 (session) => session.tabId === key.tabId && session.runtime === "server",24182434 );2419- if (snapshot) yield* Effect.promise(() => ensureTab(snapshot).catch(constVoid));2435+ const tab = snapshot2436+ ? yield* Effect.promise(() => ensureTab(snapshot).catch(() => null))2437+ : null;2438+ if (!existing) return;2439+ if (tab) tab.downloads.unshift(...existing.downloads);
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed