From 2d30dfe7ffcd05ef572b2dd25af4335f951787b6 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sat, 11 Jul 2026 17:45:16 -0500 Subject: [PATCH] refac --- cptr/frontend/src/lib/apis/files.ts | 22 + .../src/lib/components/Admin/Web.svelte | 1083 +++++++++-------- .../src/lib/components/ChromeBrowser.svelte | 15 +- .../src/lib/components/FileBrowser.svelte | 236 +++- .../src/lib/components/FileEditor.svelte | 40 +- .../lib/components/Settings/Appearance.svelte | 5 +- cptr/frontend/src/lib/i18n/locales/de.json | 1 + cptr/frontend/src/lib/i18n/locales/en.json | 6 + cptr/frontend/src/lib/i18n/locales/es.json | 1 + cptr/frontend/src/lib/i18n/locales/fr.json | 1 + cptr/frontend/src/lib/i18n/locales/ja.json | 1 + cptr/frontend/src/lib/i18n/locales/ko.json | 1 + cptr/frontend/src/lib/i18n/locales/pt-BR.json | 1 + cptr/frontend/src/lib/i18n/locales/ru.json | 1 + cptr/frontend/src/lib/i18n/locales/zh-CN.json | 1 + cptr/frontend/src/lib/i18n/locales/zh-TW.json | 1 + cptr/frontend/src/lib/stores.ts | 43 +- cptr/frontend/src/routes/+page.svelte | 15 +- cptr/frontend/static/browser-encoder.js | 1 - cptr/routers/workspace.py | 221 +++- cptr/utils/browser/viewer.py | 26 +- 21 files changed, 1127 insertions(+), 595 deletions(-) diff --git a/cptr/frontend/src/lib/apis/files.ts b/cptr/frontend/src/lib/apis/files.ts index 525bf0d..0162f66 100644 --- a/cptr/frontend/src/lib/apis/files.ts +++ b/cptr/frontend/src/lib/apis/files.ts @@ -40,3 +40,25 @@ export const searchFiles = (query: string, path: string) => fetchJSON( `/api/workspace/files/search?query=${encodeURIComponent(query)}&path=${encodeURIComponent(path)}` ); + +export type ContentMatch = { line: number; column: number; text: string }; +export type FileMatch = { + path: string; + relative_path: string; + name: string; + type: 'file' | 'directory'; + name_match: boolean; + content_matches: ContentMatch[]; +}; +export type FileMatches = { results: FileMatch[]; truncated: boolean }; + +export const getFileMatches = ( + query: string, + path: string, + showHidden: boolean, + signal?: AbortSignal +) => + fetchJSON( + `/api/workspace/files/matches?query=${encodeURIComponent(query)}&path=${encodeURIComponent(path)}&show_hidden=${showHidden}`, + { signal } + ); diff --git a/cptr/frontend/src/lib/components/Admin/Web.svelte b/cptr/frontend/src/lib/components/Admin/Web.svelte index d24e263..b9cded2 100644 --- a/cptr/frontend/src/lib/components/Admin/Web.svelte +++ b/cptr/frontend/src/lib/components/Admin/Web.svelte @@ -220,565 +220,130 @@ } -
-

{$t('admin.web')}

- +
{#if loading}
{:else} - -

{$t('admin.webSearch')}

+
+

{$t('admin.web')}

-
- -

- {webEnabled ? $t('admin.webEnabledHint') : $t('admin.webDisabledHint')} -

+ +

{$t('admin.webSearch')}

- {#if webEnabled} -
- {$t('admin.webSearchProvider')} - -
-

- {#if searchProvider === 'auto'} - {$t('admin.webAutoHint')} - {:else if searchProvider === 'duckduckgo'} - {$t('admin.webDuckDuckGoNote')} - {/if} -

- - {#if searchProvider === 'exa'} -
- - -

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

-
- {:else if searchProvider === 'tavily'} -
- - -

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

-
- {:else if searchProvider === 'brave'} -
- - -

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

-
- {:else if searchProvider === 'firecrawl'} -
- - -

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

-
-
- - -

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

-
- {:else if searchProvider === 'searxng'} -
- - -

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

-
- {:else if searchProvider === 'perplexity'} -
- - -

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

-
-
- - -

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

-
- {:else if searchProvider === 'chat_completions'} -
- - -
-
- - -
-
- - -
-

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

- {/if} - {/if} -
- - -

{$t('admin.browser')}

- -
-

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

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

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

-
- -
- - {#if browserTabDefaultMode === 'chrome'} -
-
- {$t('admin.chromeProfile')} -

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

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

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

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

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

-
- -
- -
- {#each ['low', 'balanced', 'crisp'] as name} -
- {name} -
- - -
-
- {/each} -
- {$t('admin.browserQualityMaxBitrate')} - -
-
-
- {:else} -
- -
- - -
- {#if testResult} -

- {testResult.message} -

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

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

-
- -
-
- - {#if personalStatus?.status === 'playing'} - {$t('admin.personalChromeConnected')} - {#if personalStatus.session_count} - · {personalStatus.session_count} {$t('admin.personalChromeTabs')} - {/if} - {:else if personalStatus?.status === 'lost'} - {$t('admin.personalChromeLost')} - {:else} - {$t('admin.personalChromeDisconnected')} - {/if} - - -
-
- {/if} - {/if} -
-
- -
-

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

- {$t('admin.browserHint')} + {webEnabled ? $t('admin.webEnabledHint') : $t('admin.webDisabledHint')}

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

- {#if browserProvider === 'local'} - {$t('admin.browserLocalHint')} - {:else if browserProvider === 'firecrawl'} - {$t('admin.browserFirecrawlHint')} - {:else} - {$t('admin.browserBrowserUseHint')} + {#if searchProvider === 'auto'} + {$t('admin.webAutoHint')} + {:else if searchProvider === 'duckduckgo'} + {$t('admin.webDuckDuckGoNote')} {/if}

- {#if browserProvider === 'local'} - - + {#if searchProvider === 'exa'}
- -
- - -
- {#if testResult} -

- {testResult.message} -

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

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

+
+ {:else if searchProvider === 'tavily'} +
+ + +

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

+
+ {:else if searchProvider === 'brave'} +
+ + +

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

+
+ {:else if searchProvider === 'firecrawl'} +
+ + +

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

- {$t('admin.webFirecrawlBaseUrl')} @@ -786,43 +351,483 @@ {$t('admin.browserFirecrawlBaseUrlHint')}

- {:else if browserProvider === 'browser_use'} + {:else if searchProvider === 'searxng'}
- {$t('admin.webSearxngBaseUrl')} -
-
- - +

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

+
+ {:else if searchProvider === 'perplexity'} +
+ + +

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

+
+
+ + +

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

+
+ {:else if searchProvider === 'chat_completions'} +
+ +
+
+ + +
+
+ + +
+

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

{/if} {/if}
+ + +

{$t('admin.browser')}

+ +
+

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

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

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

+
+ +
+ + {#if browserTabDefaultMode === 'chrome'} +
+
+ {$t('admin.chromeProfile')} +

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

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

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

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

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

+
+ +
+ +
+ {#each ['low', 'balanced', 'crisp'] as name} +
+ {name} +
+ + +
+
+ {/each} +
+ {$t('admin.browserQualityMaxBitrate')} + +
+
+
+ {:else} +
+ +
+ + +
+ {#if testResult} +

+ {testResult.message} +

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

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

+
+ +
+
+ + {#if personalStatus?.status === 'playing'} + {$t('admin.personalChromeConnected')} + {#if personalStatus.session_count} + · {personalStatus.session_count} {$t('admin.personalChromeTabs')} + {/if} + {:else if personalStatus?.status === 'lost'} + {$t('admin.personalChromeLost')} + {:else} + {$t('admin.personalChromeDisconnected')} + {/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} +
+
-
+
+ {#if saving}{$t('settings.saving')}{:else}{$t('settings.save')}{/if} +
{/if}
diff --git a/cptr/frontend/src/lib/components/ChromeBrowser.svelte b/cptr/frontend/src/lib/components/ChromeBrowser.svelte index 9b089d6..b8b57a8 100644 --- a/cptr/frontend/src/lib/components/ChromeBrowser.svelte +++ b/cptr/frontend/src/lib/components/ChromeBrowser.svelte @@ -59,7 +59,7 @@ let viewportTimer: ReturnType | undefined; let reconnectAttempt = 0; let disposed = false; - let viewport = { width: 1280, height: 720 }; + let viewport = { width: 0, height: 0 }; let pendingPointer: Record | undefined; let pointerFrame = 0; let hasFrame = false; @@ -77,6 +77,7 @@ let suppressText = ''; let keepKeyboardFocus = false; let audioClockOrigin: number | undefined; + let reapplyViewportAfterConfig = true; const pressedKeys = new Map>(); const touchPoints = new Map>(); const macClient = /Mac|iPhone|iPad/.test(navigator.userAgent); @@ -140,6 +141,7 @@ if (disposed) return; ready = false; lastViewport = ''; + reapplyViewportAfterConfig = true; awaitingFrame = true; onstatus(hasFrame ? 'playing' : 'reconnecting'); const scheme = location.protocol === 'https:' ? 'wss' : 'ws'; @@ -250,6 +252,10 @@ } }); decoder.configure({ codec: config.codec, optimizeForLatency: true }); + if (managed && reapplyViewportAfterConfig) { + reapplyViewportAfterConfig = false; + void sendViewport(true); + } } function configureAudio(config: { codec: string; sampleRate: number; numberOfChannels: number }) { @@ -325,10 +331,10 @@ } } - async function sendViewport() { + async function sendViewport(force = false) { const device = deviceProfile || (await deviceProfilePromise); const key = JSON.stringify([viewport, quality, device, deviceMode, mobileViewport]); - if (key === lastViewport) return; + if (!force && key === lastViewport) return; lastViewport = key; send({ type: 'viewport', @@ -336,7 +342,8 @@ quality, device, device_mode: deviceMode, - mobile_viewport: mobileViewport + mobile_viewport: mobileViewport, + force }); } diff --git a/cptr/frontend/src/lib/components/FileBrowser.svelte b/cptr/frontend/src/lib/components/FileBrowser.svelte index 04277e7..0262c47 100644 --- a/cptr/frontend/src/lib/components/FileBrowser.svelte +++ b/cptr/frontend/src/lib/components/FileBrowser.svelte @@ -15,7 +15,10 @@ deleteFiles, moveFile, uploadFiles as apiUpload, - createEntry + createEntry, + getFileMatches, + type FileMatch, + type ContentMatch } from '$lib/apis/files'; import { fileIconName } from '$lib/utils/fileIcon'; import Icon from './Icon.svelte'; @@ -44,6 +47,14 @@ let dragExpandTimer: ReturnType | null = null; let error = $state(null); let searchQuery = $state(''); + let matchResults = $state(null); + let matchLoading = $state(false); + let matchError = $state(null); + let matchesTruncated = $state(false); + let matchTimer: ReturnType | null = null; + let matchController: AbortController | null = null; + let matchRequestId = 0; + let searchTargetRequestId = 0; let dragOverDir = $state(null); let draggedItem = $state(null); let showNewInput = $state<'file' | 'folder' | null>(null); @@ -80,6 +91,8 @@ let cwd = $derived($activeWorkspace?.fileBrowserCwd ?? $activeWorkspace?.path ?? '/'); let workspacePath = $derived($activeWorkspace?.path ?? '/'); + let searchText = $derived(searchQuery.trim()); + let isSearching = $derived(Boolean(searchText)); let gitStatus = $derived(gitStatusStore.status); let gitRoot = $derived(workspacePath.replace(/\/$/, '')); @@ -283,6 +296,7 @@ const isNavigation = _prevCwd !== ''; _prevCwd = cwd; if (isNavigation) { + searchQuery = ''; // Navigated to a new directory: reset tree state expandedDirs = new Set(); dirContents = new Map(); @@ -300,6 +314,50 @@ } }); + $effect(() => { + const query = searchText; + const hidden = showHidden; + if (matchTimer) clearTimeout(matchTimer); + matchTimer = null; + matchController?.abort(); + matchController = null; + matchError = null; + const requestId = ++matchRequestId; + + if (!query) { + matchResults = null; + matchLoading = false; + matchesTruncated = false; + return; + } + + showNewInput = null; + clearSelection(); + contextMenu = null; + directoryMenu = null; + addMenuOpen = false; + sortMenuOpen = false; + matchLoading = true; + matchTimer = setTimeout(async () => { + const controller = new AbortController(); + matchController = controller; + try { + const data = await getFileMatches(query, cwd, hidden, controller.signal); + if (requestId === matchRequestId) { + matchResults = data.results; + matchesTruncated = data.truncated; + } + } catch (e: any) { + if (e.name !== 'AbortError' && requestId === matchRequestId) { + matchError = e.message || $t('files.failedToSearch'); + matchResults = []; + } + } finally { + if (requestId === matchRequestId) matchLoading = false; + } + }, 200); + }); + // Keep the websocket watch path in sync (separate effect so it doesn't // re-trigger directory fetches via fsTick feedback loops) $effect(() => { @@ -464,9 +522,46 @@ } function navigateTo(path: string) { + searchQuery = ''; setFileBrowserCwd(path); } + function highlightParts(text: string): { text: string; match: boolean }[] { + const query = searchText.toLowerCase(); + if (!query) return [{ text, match: false }]; + const parts: { text: string; match: boolean }[] = []; + const lower = text.toLowerCase(); + let start = 0; + let index = lower.indexOf(query, start); + while (index !== -1) { + if (index > start) parts.push({ text: text.slice(start, index), match: false }); + parts.push({ text: text.slice(index, index + query.length), match: true }); + start = index + query.length; + index = lower.indexOf(query, start); + } + if (start < text.length) parts.push({ text: text.slice(start), match: false }); + return parts; + } + + function openFileMatch(match: FileMatch) { + if (match.type === 'directory') { + navigateTo(match.path); + return; + } + openFileTab(match.path); + } + + function openContentMatch(match: FileMatch, contentMatch: ContentMatch) { + openFileTab(match.path, undefined, { + searchTarget: { + line: contentMatch.line, + column: contentMatch.column, + length: searchText.length, + requestId: ++searchTargetRequestId + } + }); + } + // fileIconName imported from $lib/utils/fileIcon function formatSize(bytes: number | null): string { @@ -955,9 +1050,9 @@
!isSearching && onDropzoneOver(e)} + ondragleave={(e) => !isSearching && onDropzoneLeave(e)} + ondrop={(e) => !isSearching && onDropzoneDrop(e)} >
- - + {#if !isSearching} + + - - + + + {/if}
@@ -1051,7 +1148,7 @@ {#if searchQuery} @@ -1064,9 +1161,12 @@ -
+
!isSearching && onDirectoryContextMenu(e)} + > - {#if showNewInput} + {#if showNewInput && !isSearching}
fetchDirectory(cwd)}>{$t('files.retry')}
+ {:else if isSearching} + {#if matchLoading} +
+ +
+ {:else if matchError} +
+

{matchError}

+
+ {:else if !matchResults?.length} +
+

{$t('files.noMatches')}

+
+ {:else} + {#each matchResults as match (match.path)} +
+ + {#each match.content_matches as contentMatch (contentMatch.line)} + + {/each} +
+ {/each} + {#if matchesTruncated} +

+ {$t('files.searchTruncated')} +

+ {/if} + {/if} {:else if visibleEntries.length === 0 && !showNewInput}

@@ -1268,7 +1442,7 @@

- {#if dropzoneActive} + {#if dropzoneActive && !isSearching}
@@ -1277,7 +1451,7 @@ {/if} - {#if selectedPaths.size > 0} + {#if selectedPaths.size > 0 && !isSearching}
diff --git a/cptr/frontend/src/lib/components/FileEditor.svelte b/cptr/frontend/src/lib/components/FileEditor.svelte index 87935da..da5a68e 100644 --- a/cptr/frontend/src/lib/components/FileEditor.svelte +++ b/cptr/frontend/src/lib/components/FileEditor.svelte @@ -1,6 +1,13 @@