From dc22fa8f86610be42fb1829a4f0150c8c3ce68be Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 8 Nov 2026 00:36:43 +0900 Subject: [PATCH] refac --- src/lib/apis/terminal/browser.ts | 14 +++++-- .../components/chat/ChatTabs/Browser.svelte | 23 +++++++++-- .../chat/ChatTabs/ChromeBrowser.svelte | 38 ++++++++++++++++--- 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/src/lib/apis/terminal/browser.ts b/src/lib/apis/terminal/browser.ts index 7492b4ad41..fcf8627970 100644 --- a/src/lib/apis/terminal/browser.ts +++ b/src/lib/apis/terminal/browser.ts @@ -20,16 +20,24 @@ export const getBrowserSession = ( `/browser/sessions/${encodeURIComponent(id)}` ); -export const createBrowserSession = ( +export const createBrowserSession = async ( connection: TerminalConnection, chatId: string | null, url = '' -) => - terminalRequest(connection, chatId, '/browser/sessions', { +) => { + const session = await terminalRequest(connection, chatId, '/browser/sessions', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ url }) }); + if (!session.chrome_available || session.mode === 'chrome') return session; + try { + return await updateBrowserSession(connection, chatId, session.id, { mode: 'chrome' }); + } catch { + // Reset the server too: Chrome startup may have failed after changing the mode. + return updateBrowserSession(connection, chatId, session.id, { mode: 'proxy' }); + } +}; export const updateBrowserSession = ( connection: TerminalConnection, diff --git a/src/lib/components/chat/ChatTabs/Browser.svelte b/src/lib/components/chat/ChatTabs/Browser.svelte index 8d906b6d90..baf80dc1f2 100644 --- a/src/lib/components/chat/ChatTabs/Browser.svelte +++ b/src/lib/components/chat/ChatTabs/Browser.svelte @@ -66,8 +66,8 @@ () => {} ); }; - const switchMode = async (mode: 'proxy' | 'chrome', remember = true) => { - if (!session || !sessionConnection || switching || session.mode === mode) return; + const switchMode = async (mode: 'proxy' | 'chrome', force = false) => { + if (!session || !sessionConnection || switching || (!force && session.mode === mode)) return; switching = true; try { session = await updateBrowserSession(sessionConnection, sessionChatId, session.id, { @@ -82,13 +82,27 @@ frameUrl = browserFrameUrl(browserViewUrl(sessionConnection, session), currentUrl); } error = ''; - if (remember) localStorage.setItem('browserMode', mode); + return true; } catch (reason) { error = reason instanceof Error ? reason.message : String(reason); + if (mode === 'chrome') { + switching = false; + await fallbackToProxy(error); + } } finally { switching = false; } }; + const fallbackToProxy = async (message: string) => { + if (!session || !sessionConnection || switching) return; + // The server may already be in Chrome mode even if its startup request failed. + const switched = await switchMode('proxy', true); + connectionStatus = ''; + if (switched) { + loading = false; + error = `${$i18n.t('Chrome failed. Switched to Proxy.')} ${message}`; + } + }; const frameCommand = (command: string, url?: string) => iframe?.contentWindow?.postMessage({ type: 'terminal-browser-command', command, url }, '*'); const reload = () => @@ -340,7 +354,7 @@ >
- {#each session.chrome_available ? ['proxy', 'chrome'] : ['proxy'] as mode} + {#each session.chrome_available ? ['chrome', 'proxy'] : ['proxy'] as mode}