From 0cc95076a7ebde749dc9119d6e649013fdf1f136 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Fri, 10 Jul 2026 18:52:16 -0500 Subject: [PATCH] refac --- cptr/frontend/src/lib/apis/browser.ts | 14 +- .../src/lib/components/Admin/Web.svelte | 461 +++++++++++------- .../src/lib/components/BrowserPreview.svelte | 25 +- .../src/lib/components/ChromeBrowser.svelte | 10 + cptr/frontend/src/lib/i18n/locales/de.json | 17 +- cptr/frontend/src/lib/i18n/locales/en.json | 17 +- cptr/frontend/src/lib/i18n/locales/es.json | 17 +- cptr/frontend/src/lib/i18n/locales/fr.json | 17 +- cptr/frontend/src/lib/i18n/locales/ja.json | 17 +- cptr/frontend/src/lib/i18n/locales/ko.json | 17 +- cptr/frontend/src/lib/i18n/locales/pt-BR.json | 17 +- cptr/frontend/src/lib/i18n/locales/ru.json | 17 +- cptr/frontend/src/lib/i18n/locales/zh-CN.json | 17 +- cptr/frontend/src/lib/i18n/locales/zh-TW.json | 17 +- cptr/routers/browser.py | 62 ++- cptr/utils/browser/viewer.py | 138 ++++-- 16 files changed, 617 insertions(+), 263 deletions(-) diff --git a/cptr/frontend/src/lib/apis/browser.ts b/cptr/frontend/src/lib/apis/browser.ts index 5c8e821..3d67698 100644 --- a/cptr/frontend/src/lib/apis/browser.ts +++ b/cptr/frontend/src/lib/apis/browser.ts @@ -12,8 +12,6 @@ export interface BrowserAvailability { proxy: { available: true }; chrome: { available: boolean; - browser_name: string | null; - experimental: true; reason: string | null; }; } @@ -61,3 +59,15 @@ export const browserFrameUrl = (sessionId: string, rawUrl: string) => { }; export const browserBlankUrl = (sessionId: string) => `/api/browser/sessions/${sessionId}/blank`; + +export const clearManagedChromeProfile = () => + fetchJSON<{ status: string; closed_session_ids: string[] }>('/api/browser/profile', { + method: 'DELETE' + }); + +export const testBrowserCdp = (url: string) => + fetchJSON<{ browser: string }>('/api/browser/cdp', { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ url }) + }); diff --git a/cptr/frontend/src/lib/components/Admin/Web.svelte b/cptr/frontend/src/lib/components/Admin/Web.svelte index 1bea6a3..cb418af 100644 --- a/cptr/frontend/src/lib/components/Admin/Web.svelte +++ b/cptr/frontend/src/lib/components/Admin/Web.svelte @@ -2,7 +2,7 @@ import { toast } from 'svelte-sonner'; import { onMount } from 'svelte'; import { getAdminConfig, updateConfig } from '$lib/apis/admin'; - import { getBrowserAvailability } from '$lib/apis/browser'; + import { clearManagedChromeProfile, testBrowserCdp } from '$lib/apis/browser'; import { t } from '$lib/i18n'; import ToggleSwitch from '$lib/components/common/ToggleSwitch.svelte'; import Spinner from '$lib/components/common/Spinner.svelte'; @@ -30,8 +30,7 @@ // ── Browser ─────────────────────────────────────────── let browserEnabled = $state(false); let browserTabDefaultMode = $state<'proxy' | 'chrome'>('proxy'); - let chromeTabAvailable = $state(false); - let chromeTabReason = $state(''); + let browserTabChromeSource = $state<'managed' | 'personal'>('managed'); let browserProvider = $state<'local' | 'firecrawl' | 'browser_use'>('local'); let cdpUrl = $state('http://localhost:9222'); let autoLaunch = $state(true); @@ -64,6 +63,8 @@ // Browser browserEnabled = config['browser.enabled'] === true || config['browser.enabled'] === 'true'; browserTabDefaultMode = config['browser.tab_default_mode'] === 'chrome' ? 'chrome' : 'proxy'; + browserTabChromeSource = + config['browser.tab_chrome_source'] === 'personal' ? 'personal' : 'managed'; browserProvider = (config['browser.provider'] as typeof browserProvider) || 'local'; cdpUrl = (config['browser.cdp_url'] as string) || 'http://localhost:9222'; autoLaunch = @@ -75,10 +76,6 @@ browserUseApiKey = (config['browser.browser_use_api_key'] as string) || ''; browserUseBaseUrl = (config['browser.browser_use_base_url'] as string) || 'https://api.browser-use.com'; - void getBrowserAvailability().then((availability) => { - chromeTabAvailable = availability.chrome.available; - chromeTabReason = availability.chrome.reason || ''; - }); } catch { toast.error($t('admin.failedToLoadConfig')); } @@ -104,6 +101,7 @@ 'web.chat_completions_model': ccModel, 'browser.enabled': browserEnabled, 'browser.tab_default_mode': browserTabDefaultMode, + 'browser.tab_chrome_source': browserTabChromeSource, 'browser.provider': browserProvider, 'browser.cdp_url': cdpUrl, 'browser.auto_launch': autoLaunch, @@ -122,19 +120,27 @@ } } + async function clearManagedProfile() { + if (!confirm($t('admin.managedChromeClearConfirm'))) return; + try { + await clearManagedChromeProfile(); + toast.success($t('admin.managedChromeCleared')); + } catch (error) { + toast.error(error instanceof Error ? error.message : $t('admin.failedToSave')); + } + } + async function testConnection() { testing = true; testResult = null; try { - const resp = await fetch(`${cdpUrl}/json/version`); - if (resp.ok) { - const data = await resp.json(); - testResult = { ok: true, message: data.Browser || 'Connected' }; - } else { - testResult = { ok: false, message: `HTTP ${resp.status}` }; - } - } catch { - testResult = { ok: false, message: 'Could not connect' }; + const result = await testBrowserCdp(cdpUrl); + testResult = { ok: true, message: result.browser }; + } catch (error) { + testResult = { + ok: false, + message: error instanceof Error ? error.message : 'Could not connect' + }; } finally { testing = false; } @@ -365,184 +371,269 @@

{$t('admin.browser')}

-
-
-
- {$t('admin.browserTabDefault')} -

- {$t('admin.browserTabDefaultHint')} -

-
- -
- - -

- {$t('admin.browserHint')} +

+

+ {$t('admin.browserTabs')}

- - {#if browserEnabled} +
- {$t('admin.browserProvider')} - -
-

- {#if browserProvider === 'local'} - {$t('admin.browserLocalHint')} - {:else if browserProvider === 'firecrawl'} - {$t('admin.browserFirecrawlHint')} - {:else} - {$t('admin.browserBrowserUseHint')} - {/if} -

- - {#if browserProvider === 'local'} - -
- {$t('admin.browserTabDefault')} -
- - -
- {#if testResult} -

- {testResult.message} -

- {/if} -
- -
- -
- - {$t('admin.browserMinutes')} -
-
- {:else if browserProvider === 'firecrawl'} -
- - -
-
- - -

- {$t('admin.browserFirecrawlBaseUrlHint')} +

+ {$t('admin.browserTabDefaultHint')}

- {:else if browserProvider === 'browser_use'} -
- + + + +
+ + {#if browserTabDefaultMode === 'chrome'} +
+
+ {$t('admin.chromeProfile')} +

+ {browserTabChromeSource === 'personal' + ? $t('admin.personalChromeHint') + : $t('admin.managedChromeHint')} +

+
+ -
-
- - + + +
+ + {#if browserTabChromeSource === 'managed'} +
+
+ {$t('admin.managedChromeData')} +

+ {$t('admin.managedChromeDataHint')} +

+
+ +
+ {:else} +
+ +
+ + +
+ {#if testResult} +

+ {testResult.message} +

+ {/if} + {$t('admin.personalChromeEnableDebugging')} ↗ +
+ {/if} {/if} - {/if} +
+
+ +
+

+ {$t('admin.agentBrowserTools')} +

+
+ +

+ {$t('admin.browserHint')} +

+ + {#if browserEnabled} +
+ {$t('admin.browserProvider')} + +
+

+ {#if browserProvider === 'local'} + {$t('admin.browserLocalHint')} + {:else if browserProvider === 'firecrawl'} + {$t('admin.browserFirecrawlHint')} + {:else} + {$t('admin.browserBrowserUseHint')} + {/if} +

+ + {#if browserProvider === 'local'} + + +
+ +
+ + +
+ {#if testResult} +

+ {testResult.message} +

+ {/if} +
+ +
+ +
+ + {$t('admin.browserMinutes')} +
+
+ {:else if browserProvider === 'firecrawl'} +
+ + +
+
+ + +

+ {$t('admin.browserFirecrawlBaseUrlHint')} +

+
+ {:else if browserProvider === 'browser_use'} +
+ + +
+
+ + +
+ {/if} + {/if} +
diff --git a/cptr/frontend/src/lib/components/BrowserPreview.svelte b/cptr/frontend/src/lib/components/BrowserPreview.svelte index ea65080..9034dec 100644 --- a/cptr/frontend/src/lib/components/BrowserPreview.svelte +++ b/cptr/frontend/src/lib/components/BrowserPreview.svelte @@ -195,7 +195,6 @@ >
- {#if modeError}
{modeError}
{/if}
{#if error}
@@ -226,13 +225,22 @@ /> {#if chromeStatus === 'connecting'}
- {$t('common.loading')} + {$t('browser.starting')}
{/if} - {#if chromeStatus === 'view_only'}
{$t('browser.viewOnly')}
{/if} + {#if chromeStatus === 'lost'} +
+ {$t('browser.connectionLost')} + {#if modeError}{modeError}{/if} +
+ {:else if chromeStatus === 'view_only'} +
{$t('browser.viewOnly')}
+ {/if} {:else}