feat(browser): replace the preview proxy with a real browser panel and an agent web tool (#2883)

The preview panel worked by proxying a dev server through OpenChamber's own
origin and rewriting the HTML that came back. Anything the rewriter did not
anticipate broke, and pages that refuse to be embedded never loaded at all.
This deletes the proxy (-1604 lines and its tests) and merges the preview and
browser panels into one surface backed by a real Chromium view.

What the panel is now

- A `<webview>` in its own session partition: logins and cookies persist, hot
  reload works because nothing is rewritten, DevTools are one click away.
- Annotation: pick one element, drag a region, or draw freehand, write a note,
  and it reaches chat with a screenshot of the visible page with the marks on it.
- Toolbar: hard reload, page zoom, device sizes, a light/dark switch that
  applies to the page rather than the app, and cookie/cache clearing scoped to
  the panel alone.
- Several pages at once, each tab showing the page's own favicon, and an address
  bar that suggests pages already visited in this project.
- Dev servers are listed from what is actually listening on the machine, checked
  against what a project announced, so a server is offered no matter how it was
  started. One that is still starting is waited for instead of failing.

Remote dev servers

The desktop app binds a local port and pipes raw bytes to the OpenChamber host
over the existing authenticated connection, so the page keeps its own origin at
the root of its own host. The reachable set is exactly what discovery reports
and is re-checked per connection, so an authenticated client cannot dial
arbitrary local services on the host. Links and redirects to another loopback
port stay on the machine that served the page. A tunnel that cannot be opened is
reported; it is never replaced by the plain loopback URL, which would answer
from the user's own machine under a remote address.

Agent control

Browser actions are a separate `openchamber_web` tool: open, snapshot, click,
type, scroll, inspect computed styles, resize between mobile/tablet/desktop, and
capture a screenshot into `.openchamber/screenshots/` in the project. The
existing `openchamber` tool keeps sessions, worktrees and scheduled tasks. Each
has its own setting in the new Settings -> General -> OpenChamber Tools section,
and the plugin is not injected at all when both are off.

Capability belongs to the connected client, not to configuration: a client
declares on its event stream that it can drive a page, which only a Chromium
host does. Exactly one client performs each request — it claims the request
before acting, and the first claim wins — because deciding by whose result
arrives first would be too late for a click that already happened. No client
listening is answered immediately with an explanation rather than a timeout.

Runtime boundaries

Web tabs get a plain iframe that can display a page but not inspect one. The
VS Code extension no longer offers the surface at all, since nothing that makes
the panel worth having works there. Mobile is unaffected.

Native boundary

Camera, microphone, location and device-picker requests from panel pages are
denied — Electron grants them by default when no handler is set, and the panel
loads whatever address the user types. Page capture, appearance emulation and
storage clearing verify that their target belongs to the panel's own session
instead of trusting a web-contents id from the renderer.

Persisted state

Stored `preview` tabs migrate to `browser` (v13 -> v14). Context panel tab
limits are now per surface, so filling one surface no longer evicts another's
tabs. Address history is stored per project and per runtime.

Documentation

`preview.mdx` and `desktop-browser.mdx` rewritten across all locales, the agent
tool settings path corrected, new `DOCUMENTATION.md` for the browser-control
broker and the dev tunnel, and the `ui-api-decoupling` skill updated where it
still described the deleted proxy.
This commit is contained in:
Bohdan Triapitsyn
2026-08-13 22:44:13 +03:00
committed by GitHub
parent 50613bb170
commit a5aa32446d
151 changed files with 10431 additions and 5587 deletions
+134 -37
View File
@@ -11,10 +11,11 @@ import { getRuntimeKey } from '@/lib/runtime-switch';
import type { TerminalShell } from '@/lib/api/types';
import { useFilesViewTabsStore } from './useFilesViewTabsStore';
import { isWindowsArm64 } from '@/lib/platform';
import { isVSCodeRuntime } from '@/lib/desktop';
export type MainTab = 'chat' | 'plan' | 'git' | 'diff' | 'terminal' | 'files' | 'context' | 'diagram';
export type PendingDiffScope = 'working' | 'staged' | 'turn';
export type ContextPanelMode = 'diff' | 'walkthrough' | 'file' | 'context' | 'plan' | 'chat' | 'preview' | 'browser' | 'git' | 'pr' | 'notes' | 'terminal';
export type ContextPanelMode = 'diff' | 'walkthrough' | 'file' | 'context' | 'plan' | 'chat' | 'browser' | 'git' | 'pr' | 'notes' | 'terminal';
export type MermaidRenderingMode = 'svg' | 'ascii';
export type UserMessageRenderingMode = 'markdown' | 'plain';
export type ChatRenderMode = 'sorted' | 'live';
@@ -119,10 +120,13 @@ const isLegacyDefaultTemplates = (value: unknown): boolean => {
const CONTEXT_PANEL_DEFAULT_WIDTH = 380;
const CONTEXT_PANEL_MIN_WIDTH = 380;
const CONTEXT_PANEL_MAX_WIDTH = 1400;
/** Per surface, not per panel: see clampContextPanelTabs. */
const CONTEXT_PANEL_MAX_TABS = 12;
const CONTEXT_PANEL_MAX_LABEL_LENGTH = 120;
const LEFT_SIDEBAR_MIN_WIDTH = 280;
const activeMainTabByRuntime = new Map<string, MainTab>();
/** Separates browser tabs opened in the same millisecond. */
let browserTabSequence = 0;
const runtimeMemoryKey = (value?: string | null): string => {
const key = (value ?? getRuntimeKey()).trim();
@@ -196,7 +200,7 @@ const buildDefaultContextPanelTabDedupeKey = (mode: ContextPanelMode, targetPath
return targetPath || mode;
}
if (mode === 'preview') {
if (mode === 'browser') {
return targetPath || mode;
}
@@ -247,26 +251,43 @@ const createContextPanelTab = (descriptor: ContextPanelTabDescriptor): ContextPa
};
};
const clampContextPanelTabs = (tabs: ContextPanelTab[], maxTabs: number, activeTabId: string | null): ContextPanelTab[] => {
if (tabs.length <= maxTabs) {
return tabs;
/**
* Keeps each surface's tab count in hand.
*
* The limit is per mode because the strip is per mode: a user looking at diffs
* only ever sees diff tabs, so evicting one to make room for a browser tab
* takes away something they cannot see being taken. Modes compete for screen
* space separately, so they get separate budgets.
*/
const clampContextPanelTabs = (
tabs: ContextPanelTab[],
maxTabsPerMode: number,
activeTabId: string | null,
): ContextPanelTab[] => {
const counts = new Map<ContextPanelMode, number>();
for (const tab of tabs) counts.set(tab.mode, (counts.get(tab.mode) ?? 0) + 1);
const over = [...counts.entries()].filter(([, count]) => count > maxTabsPerMode);
if (over.length === 0) return tabs;
const removeSet = new Set<string>();
for (const [mode, count] of over) {
const modeTabs = tabs.filter((tab) => tab.mode === mode);
const removable = [...modeTabs]
.sort((a, b) => a.touchedAt - b.touchedAt)
.filter((tab) => tab.id !== activeTabId);
// Never drop the tab being opened or looked at; if that leaves the mode one
// over its budget, one extra tab beats losing the one in use.
for (const tab of removable.slice(0, count - maxTabsPerMode)) removeSet.add(tab.id);
}
const tabsByTouch = [...tabs].sort((a, b) => a.touchedAt - b.touchedAt);
const removable = tabsByTouch.filter((tab) => tab.id !== activeTabId);
const removeCount = tabs.length - maxTabs;
if (removeCount <= 0 || removable.length === 0) {
return tabs.slice(-maxTabs);
}
const removeSet = new Set(removable.slice(0, removeCount).map((tab) => tab.id));
return tabs.filter((tab) => !removeSet.has(tab.id));
return removeSet.size === 0 ? tabs : tabs.filter((tab) => !removeSet.has(tab.id));
};
const sanitizeContextPanelTabs = (tabs: unknown): ContextPanelTab[] => {
if (!Array.isArray(tabs)) {
return [];
}
const dropBrowserTabs = isVSCodeRuntime();
const result: ContextPanelTab[] = [];
const seen = new Set<string>();
@@ -288,7 +309,16 @@ const sanitizeContextPanelTabs = (tabs: unknown): ContextPanelTab[] => {
touchedAt?: unknown;
};
if (candidate.mode !== 'diff' && candidate.mode !== 'walkthrough' && candidate.mode !== 'file' && candidate.mode !== 'context' && candidate.mode !== 'plan' && candidate.mode !== 'chat' && candidate.mode !== 'preview' && candidate.mode !== 'browser' && candidate.mode !== 'git' && candidate.mode !== 'pr' && candidate.mode !== 'notes' && candidate.mode !== 'terminal') {
// Legacy 'preview' tabs are converted to 'browser' by the v14 migration;
// anything still carrying an unknown mode here is discarded rather than
// resurrected into a tab the panel cannot render.
if (candidate.mode !== 'diff' && candidate.mode !== 'walkthrough' && candidate.mode !== 'file' && candidate.mode !== 'context' && candidate.mode !== 'plan' && candidate.mode !== 'chat' && candidate.mode !== 'browser' && candidate.mode !== 'git' && candidate.mode !== 'pr' && candidate.mode !== 'notes' && candidate.mode !== 'terminal') {
continue;
}
// State is shared with the desktop and web surfaces, which do have a
// browser; inside VS Code such a tab would have no surface to belong to.
if (dropBrowserTabs && candidate.mode === 'browser') {
continue;
}
@@ -523,7 +553,7 @@ const sanitizeContextPanelByDirectory = (
if (candidate.widthByMode && typeof candidate.widthByMode === 'object') {
for (const [mode, value] of Object.entries(candidate.widthByMode as Record<string, unknown>)) {
if (
(mode === 'diff' || mode === 'file' || mode === 'context' || mode === 'plan' || mode === 'chat' || mode === 'preview' || mode === 'browser' || mode === 'git' || mode === 'pr' || mode === 'notes' || mode === 'terminal')
(mode === 'diff' || mode === 'file' || mode === 'context' || mode === 'plan' || mode === 'chat' || mode === 'browser' || mode === 'git' || mode === 'pr' || mode === 'notes' || mode === 'terminal')
&& typeof value === 'number'
&& Number.isFinite(value)
) {
@@ -718,6 +748,7 @@ interface UIStore {
persistChatDraft: boolean;
showOpenCodeUpdateNotifications: boolean;
agentControlToolEnabled: boolean;
agentWebToolEnabled: boolean;
inputSpellcheckEnabled: boolean;
wideChatLayoutEnabled: boolean;
codeBlockLineWrap: boolean;
@@ -758,6 +789,7 @@ interface UIStore {
openContextPlan: (directory: string) => void;
openContextPreview: (directory: string, url: string) => void;
openContextBrowser: (directory: string, url?: string) => void;
openNewContextBrowserTab: (directory: string) => void;
setContextPanelTabTargetPath: (directory: string, tabID: string, targetPath: string) => void;
setActiveContextPanelTab: (directory: string, tabID: string) => void;
reorderContextPanelTabs: (directory: string, activeTabID: string, overTabID: string) => void;
@@ -890,6 +922,7 @@ interface UIStore {
setPersistChatDraft: (value: boolean) => void;
setShowOpenCodeUpdateNotifications: (value: boolean) => void;
setAgentControlToolEnabled: (value: boolean) => void;
setAgentWebToolEnabled: (value: boolean) => void;
setInputSpellcheckEnabled: (value: boolean) => void;
setWideChatLayoutEnabled: (value: boolean) => void;
setCodeBlockLineWrap: (value: boolean) => void;
@@ -1048,6 +1081,7 @@ export const useUIStore = create<UIStore>()(
persistChatDraft: true,
showOpenCodeUpdateNotifications: !isWindowsArm64(),
agentControlToolEnabled: true,
agentWebToolEnabled: true,
inputSpellcheckEnabled: false,
wideChatLayoutEnabled: false,
codeBlockLineWrap: true,
@@ -1166,10 +1200,10 @@ export const useUIStore = create<UIStore>()(
return;
}
// Content-driven modes need a payload (a preview URL or session);
// the rail renders them disabled until content exists. 'file' opens
// an empty editor whose embedded tree picks the first file.
if (mode === 'preview' || mode === 'chat') {
// Content-driven modes need a payload (a session to split); the rail
// renders them disabled until content exists. 'file' opens an empty
// editor whose embedded tree picks the first file.
if (mode === 'chat') {
return;
}
@@ -1262,36 +1296,41 @@ export const useUIStore = create<UIStore>()(
openContextPreview: (directory, url) => {
const normalizedDirectory = normalizeDirectoryPath((directory || '').trim());
const normalizedUrl = (url || '').trim();
if (!normalizedDirectory || !normalizedUrl) {
if (!normalizedDirectory || !normalizedUrl || isVSCodeRuntime()) {
return;
}
let label: string | null = null;
try {
const parsed = new URL(normalizedUrl);
if (parsed.protocol === 'http:' || parsed.protocol === 'https:') {
label = parsed.host || parsed.hostname || 'Preview';
}
} catch {
// ignore invalid URL
}
// No stored label: a browser tab is named after wherever it has
// navigated to, which the panel derives from targetPath.
get().openContextPanelTab(normalizedDirectory, {
mode: 'preview',
mode: 'browser',
targetPath: normalizedUrl,
dedupeKey: normalizedUrl,
label,
label: null,
});
},
// Always a new tab, never the existing one: the whole point of asking
// for one is to keep what is already open.
openNewContextBrowserTab: (directory) => {
const normalizedDirectory = normalizeDirectoryPath((directory || '').trim());
if (!normalizedDirectory || isVSCodeRuntime()) return;
browserTabSequence += 1;
get().openContextPanelTab(normalizedDirectory, {
mode: 'browser',
targetPath: '',
dedupeKey: `browser:new:${Date.now()}-${browserTabSequence}`,
label: null,
});
},
openContextBrowser: (directory, url = '') => {
const normalizedDirectory = normalizeDirectoryPath((directory || '').trim());
if (!normalizedDirectory) return;
if (!normalizedDirectory || isVSCodeRuntime()) return;
const targetUrl = typeof url === 'string' && url.trim().length > 0 ? url.trim() : '';
get().openContextPanelTab(normalizedDirectory, {
mode: 'browser',
targetPath: targetUrl,
dedupeKey: 'desktop-browser',
label: 'Browser',
dedupeKey: targetUrl || 'browser',
label: null,
});
},
@@ -2264,6 +2303,9 @@ export const useUIStore = create<UIStore>()(
setAgentControlToolEnabled: (value) => {
set({ agentControlToolEnabled: value });
},
setAgentWebToolEnabled: (value) => {
set({ agentWebToolEnabled: value });
},
setInputSpellcheckEnabled: (value) => {
set({ inputSpellcheckEnabled: value });
},
@@ -2368,13 +2410,67 @@ export const useUIStore = create<UIStore>()(
{
name: 'ui-store',
storage: createDeferredSafeJSONStorage(),
version: 13,
version: 14,
migrate: (persistedState, version) => {
if (!persistedState || typeof persistedState !== 'object') {
return persistedState;
}
const state = persistedState as Record<string, unknown>;
// v13 -> v14: the separate 'preview' surface merged into 'browser'.
// Stored preview tabs keep their URL and become browser tabs; their
// id encodes the mode, so it is rebuilt rather than left dangling.
// Persisted widths recorded under 'preview' carry over only when the
// user has not already sized the browser surface.
if (version < 14) {
const byDirectory = state.contextPanelByDirectory;
if (byDirectory && typeof byDirectory === 'object') {
for (const directoryState of Object.values(byDirectory as Record<string, unknown>)) {
if (!directoryState || typeof directoryState !== 'object') continue;
const entry = directoryState as Record<string, unknown>;
const widths = entry.widthByMode;
if (widths && typeof widths === 'object') {
const widthRecord = widths as Record<string, unknown>;
if (widthRecord.preview !== undefined) {
if (widthRecord.browser === undefined) widthRecord.browser = widthRecord.preview;
delete widthRecord.preview;
}
}
if (!Array.isArray(entry.tabs)) continue;
const seenIds = new Set<string>();
const migrated: Array<Record<string, unknown>> = [];
for (const rawTab of entry.tabs as Array<unknown>) {
if (!rawTab || typeof rawTab !== 'object') continue;
const tab = rawTab as Record<string, unknown>;
if (tab.mode !== 'preview') {
if (typeof tab.id === 'string') seenIds.add(tab.id);
migrated.push(tab);
continue;
}
const targetPath = typeof tab.targetPath === 'string' ? tab.targetPath : '';
const dedupeKey = typeof tab.dedupeKey === 'string' && tab.dedupeKey.trim()
? tab.dedupeKey.trim()
: (targetPath || 'browser');
const id = dedupeKey === 'browser' ? 'browser' : `browser:${dedupeKey}`;
// A converted tab can collide with a browser tab on the same
// URL; keep the existing one rather than producing duplicates.
if (seenIds.has(id)) continue;
seenIds.add(id);
migrated.push({ ...tab, mode: 'browser', id, dedupeKey });
}
entry.tabs = migrated;
if (typeof entry.activeTabId === 'string' && entry.activeTabId.startsWith('preview')) {
const nextActive = migrated.find((tab) => typeof tab.id === 'string');
entry.activeTabId = nextActive && typeof nextActive.id === 'string' ? nextActive.id : null;
}
}
}
}
// v12 -> v13: promote FilesView localStorage autosave toggle into the store.
if (version < 13) {
if (typeof state.autoSaveEnabled !== 'boolean') {
@@ -2592,6 +2688,7 @@ export const useUIStore = create<UIStore>()(
persistChatDraft: state.persistChatDraft,
showOpenCodeUpdateNotifications: state.showOpenCodeUpdateNotifications,
agentControlToolEnabled: state.agentControlToolEnabled,
agentWebToolEnabled: state.agentWebToolEnabled,
inputSpellcheckEnabled: state.inputSpellcheckEnabled,
wideChatLayoutEnabled: state.wideChatLayoutEnabled,
codeBlockLineWrap: state.codeBlockLineWrap,