Agent Zero AI framework
Polish Browser settings and viewport handling
Alessandroc32e32828745
diff --git a/plugins/_browser/api/extensions.py b/plugins/_browser/api/extensions.pyindex df85a28a0..9ed257346 100644--- a/plugins/_browser/api/extensions.py+++ b/plugins/_browser/api/extensions.py@@ -3,6 +3,8 @@ from types import SimpleNamespacefrom helpers import pluginsfrom helpers.api import ApiHandler, Requestfrom plugins._browser.helpers.config import (+ AUTOFOCUS_ACTIVE_PAGE_KEY,+ DEFAULT_HOMEPAGE_KEY,MODEL_PRESET_KEY,PLUGIN_NAME,get_browser_config,@@ -81,6 +83,8 @@ class Extensions(ApiHandler):"ok": True,"root": str(get_extensions_root()),"extensions": list_browser_extensions(),+ DEFAULT_HOMEPAGE_KEY: config[DEFAULT_HOMEPAGE_KEY],+ AUTOFOCUS_ACTIVE_PAGE_KEY: config[AUTOFOCUS_ACTIVE_PAGE_KEY],MODEL_PRESET_KEY: config[MODEL_PRESET_KEY],"main_model_summary": get_browser_main_model_summary(agent=agent),"model_preset_options": get_browser_model_preset_options(agent=agent, settings=config),diff --git a/plugins/_browser/api/ws_browser.py b/plugins/_browser/api/ws_browser.pyindex 8925da215..b1df9146b 100644--- a/plugins/_browser/api/ws_browser.py+++ b/plugins/_browser/api/ws_browser.py@@ -61,7 +61,7 @@ class WsBrowser(WsHandler):listing = await runtime.call("list")browsers = listing.get("browsers") or []if not browsers:- opened = await runtime.call("open", "about:blank")+ opened = await runtime.call("open", "")listing = await runtime.call("list")browsers = listing.get("browsers") or []if opened.get("id"):@@ -114,7 +114,7 @@ class WsBrowser(WsHandler):try:if command == "open":- result = await runtime.call("open", data.get("url") or "about:blank")+ result = await runtime.call("open", data.get("url") or "")elif command == "navigate":result = await runtime.call("navigate", browser_id, data.get("url") or "")elif command == "back":diff --git a/plugins/_browser/default_config.yaml b/plugins/_browser/default_config.yamlindex 928f8cba0..b40a95e65 100644--- a/plugins/_browser/default_config.yaml+++ b/plugins/_browser/default_config.yaml@@ -2,6 +2,12 @@# Paths must be readable from the Agent Zero runtime itself.extension_paths: []+# Page opened by new Browser sessions when no URL is provided.+default_homepage: "about:blank"++# Focus Browser canvas pages automatically when agent Browser tool results arrive.+autofocus_active_page: true+# Optional _model_config preset used by Browser-owned model helpers.# Empty uses the effective Main Model.model_preset: ""diff --git a/plugins/_browser/extensions/webui/get_tool_message_handler/browser-tool-handler.js b/plugins/_browser/extensions/webui/get_tool_message_handler/browser-tool-handler.jsindex 52e089b94..78c608945 100644--- a/plugins/_browser/extensions/webui/get_tool_message_handler/browser-tool-handler.js+++ b/plugins/_browser/extensions/webui/get_tool_message_handler/browser-tool-handler.js@@ -34,6 +34,19 @@ async function openBrowserCanvas(payload = {}) {await window.openModal?.(BROWSER_MODAL);}+async function browserAllowsToolAutofocus() {+ try {+ const browser = globalThis.Alpine?.store?.("browserPage")+ || (await import("/plugins/_browser/webui/browser-store.js")).store;+ if (browser?.allowsToolAutofocus) {+ return await browser.allowsToolAutofocus();+ }+ } catch (error) {+ console.warn("Browser autofocus setting could not be checked", error);+ }+ return true;+}+function parseBrowserResult(content) {if (!content || typeof content !== "string") return {};try {@@ -78,7 +91,8 @@ function autoOpenBrowserCanvas(args, result) {if (autoOpenedBrowsers.has(key) || sessionStorage.getItem(persistedKey)) return;autoOpenedBrowsers.add(key);sessionStorage.setItem(persistedKey, "1");- requestAnimationFrame(() => {+ requestAnimationFrame(async () => {+ if (!(await browserAllowsToolAutofocus())) return;void openBrowserCanvas({ browserId, source: "tool" });});}diff --git a/plugins/_browser/extensions/webui/set_messages_after_loop/auto-open-browser-results.js b/plugins/_browser/extensions/webui/set_messages_after_loop/auto-open-browser-results.jsindex b0fbf7bdd..58f18e875 100644--- a/plugins/_browser/extensions/webui/set_messages_after_loop/auto-open-browser-results.js+++ b/plugins/_browser/extensions/webui/set_messages_after_loop/auto-open-browser-results.js@@ -21,7 +21,8 @@ export default async function autoOpenBrowserResults(context) {const persistedKey = `a0.browser.autoOpened.${key}`;if (hasOpened(key, persistedKey)) continue;- requestAnimationFrame(() => {+ requestAnimationFrame(async () => {+ if (!(await browserAllowsToolAutofocus())) return;void openBrowserCanvas({ browserId, source: "tool-result" });});}@@ -147,3 +148,16 @@ async function openBrowserCanvas(payload = {}) {}await window.openModal?.(BROWSER_MODAL);}++async function browserAllowsToolAutofocus() {+ try {+ const browser = globalThis.Alpine?.store?.("browserPage")+ || (await import("/plugins/_browser/webui/browser-store.js")).store;+ if (browser?.allowsToolAutofocus) {+ return await browser.allowsToolAutofocus();+ }+ } catch (error) {+ console.warn("Browser autofocus setting could not be checked", error);+ }+ return true;+}diff --git a/plugins/_browser/helpers/config.py b/plugins/_browser/helpers/config.pyindex 648cffc02..85c3f6035 100644--- a/plugins/_browser/helpers/config.py+++ b/plugins/_browser/helpers/config.py@@ -9,6 +9,8 @@ if TYPE_CHECKING:PLUGIN_NAME = "_browser"MODEL_PRESET_KEY = "model_preset"+DEFAULT_HOMEPAGE_KEY = "default_homepage"+AUTOFOCUS_ACTIVE_PAGE_KEY = "autofocus_active_page"BASE_BROWSER_ARGS = ["--no-sandbox","--disable-dev-shm-usage",@@ -42,6 +44,26 @@ def _normalize_model_preset(value: Any) -> str:return str(value or "").strip()+def _normalize_default_homepage(value: Any) -> str:+ homepage = str(value or "").strip()+ return homepage or "about:blank"+++def _normalize_bool(value: Any, default: bool = True) -> bool:+ if value is None:+ return default+ if isinstance(value, bool):+ return value+ if isinstance(value, (int, float)):+ return bool(value)+ normalized = str(value).strip().lower()+ if normalized in {"1", "true", "yes", "on", "enabled"}:+ return True+ if normalized in {"0", "false", "no", "off", "disabled"}:+ return False+ return default++def _model_config_summary(config: dict[str, Any] | None) -> str:if not isinstance(config, dict):return ""@@ -55,6 +77,13 @@ def normalize_browser_config(settings: dict[str, Any] | None) -> dict[str, Any]:extension_paths = _normalize_extension_paths(raw.get("extension_paths", []))return {"extension_paths": extension_paths,+ DEFAULT_HOMEPAGE_KEY: _normalize_default_homepage(+ raw.get(DEFAULT_HOMEPAGE_KEY, raw.get("starting_page", "about:blank"))+ ),+ AUTOFOCUS_ACTIVE_PAGE_KEY: _normalize_bool(+ raw.get(AUTOFOCUS_ACTIVE_PAGE_KEY, True),+ default=True,+ ),MODEL_PRESET_KEY: _normalize_model_preset(raw.get(MODEL_PRESET_KEY, "")),}diff --git a/plugins/_browser/helpers/extension_manager.py b/plugins/_browser/helpers/extension_manager.pyindex b1d615163..1f3372a6d 100644--- a/plugins/_browser/helpers/extension_manager.py+++ b/plugins/_browser/helpers/extension_manager.py@@ -20,6 +20,7 @@ EXTENSIONS_ROOT_DIR = ("usr", "plugins", PLUGIN_NAME, "extensions")EXTENSION_ID_RE = re.compile(r"^[a-p]{32}$")WEB_STORE_ID_RE = re.compile(r"(?<![a-p])([a-p]{32})(?![a-p])")CHROME_VERSION_RE = re.compile(r"(\d+(?:\.\d+){0,3})")+CHROME_I18N_MESSAGE_RE = re.compile(r"__MSG_([[email protected]]+)__")DEFAULT_CHROME_PRODVERSION = "140.0.0.0"CHROME_VERSION_COMMANDS = (("google-chrome", "--version"),@@ -103,7 +104,7 @@ def install_chrome_web_store_extension(source: str) -> dict[str, Any]:return {"ok": True,"id": extension_id,- "name": manifest.get("name") or extension_id,+ "name": _manifest_label(target, manifest, "name") or extension_id,"version": manifest.get("version") or "","path": str(target),"extension_paths": config["extension_paths"],@@ -260,8 +261,15 @@ def _enable_extension_path(extension_path: Path) -> dict[str, Any]:def _extension_entry(extension_dir: Path, enabled_paths: set[str]) -> dict[str, Any]:manifest = _read_manifest(extension_dir)extension_path = str(extension_dir)+ name = (+ _manifest_label(extension_dir, manifest, "name")+ or _manifest_label(extension_dir, manifest, "short_name")+ or extension_dir.name+ )return {- "name": manifest.get("name") or extension_dir.name,+ "name": name,+ "raw_name": manifest.get("name") or "",+ "description": _manifest_label(extension_dir, manifest, "description"),"version": manifest.get("version") or "","path": extension_path,"enabled": extension_path in enabled_paths,@@ -274,3 +282,81 @@ def _read_manifest(extension_path: Path) -> dict[str, Any]:return json.loads(manifest_path.read_text(encoding="utf-8"))except Exception:return {}+++def _manifest_label(extension_dir: Path, manifest: dict[str, Any], key: str) -> str:+ value = str(manifest.get(key) or "").strip()+ if not value:+ return ""++ messages = _load_locale_messages(extension_dir, str(manifest.get("default_locale") or ""))+ if not messages:+ return "" if CHROME_I18N_MESSAGE_RE.fullmatch(value) else value++ def replace_message(match: re.Match[str]) -> str:+ message_key = match.group(1)+ message = _resolve_locale_message(messages, message_key)+ return message if message is not None else match.group(0)++ resolved = CHROME_I18N_MESSAGE_RE.sub(replace_message, value).strip()+ if CHROME_I18N_MESSAGE_RE.fullmatch(resolved):+ return ""+ return resolved+++def _load_locale_messages(extension_dir: Path, default_locale: str) -> dict[str, Any]:+ locale_root = extension_dir / "_locales"+ if not locale_root.is_dir():+ return {}++ preferred_locales = [+ default_locale,+ default_locale.split("_", 1)[0] if default_locale else "",+ "en_US",+ "en",+ ]+ for locale in [item for item in preferred_locales if item]:+ messages = _read_locale_file(locale_root / locale / "messages.json")+ if messages:+ return messages++ for messages_path in sorted(locale_root.glob("*/messages.json")):+ messages = _read_locale_file(messages_path)+ if messages:+ return messages+ return {}+++def _read_locale_file(messages_path: Path) -> dict[str, Any]:+ if not messages_path.is_file():+ return {}+ try:+ data = json.loads(messages_path.read_text(encoding="utf-8"))+ except Exception:+ return {}+ return data if isinstance(data, dict) else {}+++def _resolve_locale_message(messages: dict[str, Any], key: str) -> str | None:+ entry = messages.get(key)+ if not isinstance(entry, dict):+ return None+ message = str(entry.get("message") or "")+ if not message:+ return None++ placeholders = entry.get("placeholders")+ if isinstance(placeholders, dict):+ for name, placeholder in placeholders.items():+ if not isinstance(placeholder, dict):+ continue+ content = str(placeholder.get("content") or "")+ if not content:+ continue+ message = re.sub(+ rf"\${re.escape(str(name))}\$",+ content,+ message,+ flags=re.IGNORECASE,+ )+ return messagediff --git a/plugins/_browser/helpers/runtime.py b/plugins/_browser/helpers/runtime.pyindex a9c156a6e..036d1c715 100644--- a/plugins/_browser/helpers/runtime.py+++ b/plugins/_browser/helpers/runtime.py@@ -20,7 +20,11 @@ from helpers import filesfrom helpers.defer import DeferredTaskfrom helpers.print_style import PrintStyle-from plugins._browser.helpers.config import build_browser_launch_config, get_browser_config+from plugins._browser.helpers.config import (+ DEFAULT_HOMEPAGE_KEY,+ build_browser_launch_config,+ get_browser_config,+)from plugins._browser.helpers.playwright import configure_playwright_env, ensure_playwright_binary@@ -31,6 +35,7 @@ DEFAULT_VIEWPORT = {"width": 1024, "height": 768}CHROME_SINGLETON_FILES = ("SingletonLock", "SingletonCookie", "SingletonSocket")SCREENCAST_MAX_WIDTH = 4096SCREENCAST_MAX_HEIGHT = 4096+VIEWPORT_SIZE_TOLERANCE = 4_SPECIAL_SCHEME_RE = re.compile(r"^(?:about|blob|data|file|mailto|tel):", re.I)_URL_SCHEME_RE = re.compile(r"^[a-z][a-z\d+\-.]*://", re.I)@@ -460,17 +465,24 @@ class _BrowserRuntimeCore:except OSError as exc:PrintStyle.warning(f"Could not force-stop orphaned Chromium process {pid}: {exc}")- async def open(self, url: str = "about:blank") -> dict[str, Any]:+ async def open(self, url: str = "") -> dict[str, Any]:await self.ensure_started()page = await self.context.new_page()browser_page = self._register_page(page)self.last_interacted_browser_id = browser_page.id- if url and url != "about:blank":- await self._goto(page, normalize_url(url))+ target_url = self._initial_url(url)+ if target_url and target_url != "about:blank":+ await self._goto(page, normalize_url(target_url))else:await self._settle(page)return {"id": browser_page.id, "state": await self._state(browser_page.id)}+ def _initial_url(self, url: str = "") -> str:+ raw_url = str(url or "").strip()+ if raw_url:+ return raw_url+ return str(get_browser_config().get(DEFAULT_HOMEPAGE_KEY) or "about:blank").strip() or "about:blank"+async def list(self) -> dict[str, Any]:await self.ensure_started()return {@@ -691,8 +703,10 @@ class _BrowserRuntimeCore:}current_viewport = page.viewport_size or {}changed = (- int(current_viewport.get("width") or 0) != viewport["width"]- or int(current_viewport.get("height") or 0) != viewport["height"]+ abs(int(current_viewport.get("width") or 0) - viewport["width"])+ > VIEWPORT_SIZE_TOLERANCE+ or abs(int(current_viewport.get("height") or 0) - viewport["height"])+ > VIEWPORT_SIZE_TOLERANCE)if changed:await page.set_viewport_size(viewport)diff --git a/plugins/_browser/tools/browser.py b/plugins/_browser/tools/browser.pyindex 8d5a969c4..8ea6001e2 100644--- a/plugins/_browser/tools/browser.py+++ b/plugins/_browser/tools/browser.py@@ -25,7 +25,7 @@ class Browser(Tool):try:if action == "open":- result = await runtime.call("open", url or "about:blank")+ result = await runtime.call("open", url or "")elif action == "list":result = await runtime.call("list")elif action == "state":diff --git a/plugins/_browser/webui/browser-config-store.js b/plugins/_browser/webui/browser-config-store.jsindex d177377d7..62c8685d5 100644--- a/plugins/_browser/webui/browser-config-store.js+++ b/plugins/_browser/webui/browser-config-store.js@@ -21,11 +21,23 @@ function normalizePathList(value) {function ensureConfig(config) {if (!config || typeof config !== "object") return null;config.extension_paths = normalizePathList(config.extension_paths);+ config.default_homepage = String(config.default_homepage || "about:blank").trim() || "about:blank";+ config.autofocus_active_page = normalizeBoolean(config.autofocus_active_page, true);config.model_preset = String(config.model_preset || "").trim();delete config.model;return config;}+function normalizeBoolean(value, fallback = true) {+ if (value === undefined || value === null || value === "") return fallback;+ if (typeof value === "boolean") return value;+ if (typeof value === "number") return Boolean(value);+ const normalized = String(value).trim().toLowerCase();+ if (["1", "true", "yes", "on", "enabled"].includes(normalized)) return true;+ if (["0", "false", "no", "off", "disabled"].includes(normalized)) return false;+ return fallback;+}+export const store = createStore("browserConfig", {config: null,extensionsList: [],@@ -50,6 +62,16 @@ export const store = createStore("browserConfig", {this.config = safeConfig;},+ setAutofocusActivePage(enabled) {+ const safeConfig = ensureConfig(this.config);+ if (!safeConfig) return;+ safeConfig.autofocus_active_page = Boolean(enabled);+ },++ autofocusLabel() {+ return this.config?.autofocus_active_page === false ? "Off" : "On";+ },+hasPaths() {return this.pathCount() > 0;},diff --git a/plugins/_browser/webui/browser-panel.html b/plugins/_browser/webui/browser-panel.htmlindex 7697f7e2f..e25808403 100644--- a/plugins/_browser/webui/browser-panel.html+++ b/plugins/_browser/webui/browser-panel.html@@ -43,11 +43,11 @@<div class="browser-session-controls"><div class="browser-extension-menu" @click.outside="$store.browserPage.closeExtensionsMenu()"@keydown.escape.window="$store.browserPage.closeExtensionsMenu()">- <button type="button" class="btn btn-icon-action browser-extensions" title="Browser extensions"- aria-label="Browser extensions" @click.stop="$store.browserPage.toggleExtensionsMenu()"+ <button type="button" class="btn btn-icon-action browser-extensions" title="Browser settings"+ aria-label="Browser settings" @click.stop="$store.browserPage.toggleExtensionsMenu()":aria-expanded="$store.browserPage.extensionMenuOpen.toString()":class="{ 'is-active': $store.browserPage.status?.extensions?.active }">- <span class="material-symbols-outlined">extension</span>+ <span class="material-symbols-outlined">tune</span></button><div class="browser-extension-dropdown" x-show="$store.browserPage.extensionMenuOpen" x-transitionstyle="display: none;">@@ -160,14 +160,8 @@</form></div>- <div class="browser-error" x-show="$store.browserPage.error" x-text="$store.browserPage.error"></div>-<div class="browser-stage" tabindex="0" @click="$el.focus()"@wheel.prevent="$store.browserPage.sendWheel($event)">- <div class="browser-status" x-show="$store.browserPage.isBusy()">- <span class="material-symbols-outlined spinning">progress_activity</span>- <span x-text="$store.browserPage.loadingMessage()">Connecting browser...</span>- </div><template x-if="$store.browserPage.frameSrc"><img class="browser-frame" :src="$store.browserPage.frameSrc"@click="$store.browserPage.sendMouse('click', $event)"@@ -176,11 +170,24 @@<template x-if="!$store.browserPage.frameSrc && !$store.browserPage.isBusy()"><div class="browser-empty"><span class="material-symbols-outlined">captive_portal</span>- <button class="btn btn-field" @click="$store.browserPage.command('open', { url: 'about:blank' })">Open+ <button class="btn btn-field" @click="$store.browserPage.command('open')">OpenBrowser</button></div></template></div>++ <div class="browser-bottom-status"+ :class="{ 'is-active': $store.browserPage.isBusy() || $store.browserPage.error, 'has-error': $store.browserPage.error && !$store.browserPage.isBusy() }">+ <template x-if="$store.browserPage.isBusy()">+ <div class="browser-status" :title="$store.browserPage.loadingMessage()">+ <span class="material-symbols-outlined spinning">progress_activity</span>+ <span x-text="$store.browserPage.loadingMessage()">Loading</span>+ </div>+ </template>+ <template x-if="!$store.browserPage.isBusy() && $store.browserPage.error">+ <div class="browser-error" x-text="$store.browserPage.error" :title="$store.browserPage.error"></div>+ </template>+ </div></div></template></div>@@ -799,18 +806,17 @@.browser-stage {flex: 1 1 auto;- display: flex;- flex-direction: column;min-height: 0;overflow: hidden;position: relative;- background: #fff;+ contain: size layout paint;outline: none;}.browser-frame {- flex: 1 1 auto;display: block;+ position: absolute;+ inset: 0;width: 100%;height: 100%;min-width: 0;@@ -821,8 +827,6 @@background: #fff;}- .browser-status,- .browser-error,.browser-empty {display: flex;align-items: center;@@ -831,26 +835,48 @@font-size: 0.88rem;}- .browser-status,- .browser-error {- padding: 0 12px;- }+ .browser-bottom-status {+ display: flex;+ align-items: center;+ flex: 0 0 24px;+ min-height: 24px;+ max-height: 24px;+ min-width: 0;+ padding: 0 10px;+ overflow: hidden;+ border-top: 1px solid color-mix(in srgb, var(--color-border) 58%, transparent);+ background: color-mix(in srgb, var(--color-background) 95%, #000 5%);+ color: color-mix(in srgb, var(--color-text) 74%, transparent);+ font-size: 0.76rem;+ line-height: 1;+ }- .browser-status {- position: absolute;- top: 10px;- left: 10px;- z-index: 5;- width: max-content;- max-width: calc(100% - 20px);- min-height: 32px;- padding: 7px 10px;- border: 1px solid color-mix(in srgb, var(--color-border) 70%, transparent);- border-radius: 7px;- background: color-mix(in srgb, var(--color-background) 92%, transparent);- box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);- pointer-events: none;- }+ .browser-bottom-status.has-error {+ color: #fca5a5;+ background: color-mix(in srgb, #7f1d1d 22%, var(--color-background));+ }++ .browser-status,+ .browser-error {+ display: inline-flex;+ align-items: center;+ gap: 6px;+ min-width: 0;+ max-width: 100%;+ overflow: hidden;+ white-space: nowrap;+ }++ .browser-status span:not(.material-symbols-outlined),+ .browser-error {+ min-width: 0;+ overflow: hidden;+ text-overflow: ellipsis;+ }++ .browser-status .material-symbols-outlined {+ font-size: 15px;+ }.browser-modal .spinning,.browser-panel .spinning {@@ -865,10 +891,6 @@}}- .browser-error {- color: #9f1239;- }-.browser-empty {display: grid;flex: 1 1 auto;@@ -879,7 +901,6 @@text-align: center;padding: 24px;color: var(--color-text);- background: var(--color-background);}@container (max-width: 460px) {diff --git a/plugins/_browser/webui/browser-store.js b/plugins/_browser/webui/browser-store.jsindex dce7ef08f..57252f179 100644--- a/plugins/_browser/webui/browser-store.js+++ b/plugins/_browser/webui/browser-store.js@@ -10,6 +10,9 @@ websocket.addHandlers(["ws_webui"]);const EXTENSIONS_ROOT = "/a0/usr/plugins/_browser/extensions";const BROWSER_SUBSCRIBE_TIMEOUT_MS = 60000;const BROWSER_FIRST_INSTALL_TIMEOUT_MS = 300000;+const BROWSER_CONFIG_REFRESH_MS = 15000;+const VIEWPORT_SYNC_DEBOUNCE_MS = 220;+const VIEWPORT_SYNC_SIZE_TOLERANCE = 4;function makeViewerToken() {return globalThis.crypto?.randomUUID?.()@@ -30,6 +33,23 @@ function firstOk(response) {return {};}+function normalizeBool(value, fallback = true) {+ if (value === undefined || value === null || value === "") return fallback;+ if (typeof value === "boolean") return value;+ if (typeof value === "number") return Boolean(value);+ const normalized = String(value).trim().toLowerCase();+ if (["1", "true", "yes", "on", "enabled"].includes(normalized)) return true;+ if (["0", "false", "no", "off", "disabled"].includes(normalized)) return false;+ return fallback;+}++function nextAnimationFrame() {+ return new Promise((resolve) => {+ const schedule = globalThis.requestAnimationFrame || ((callback) => globalThis.setTimeout(callback, 16));+ schedule(() => resolve());+ });+}+const model = {loading: true,error: "",@@ -55,7 +75,10 @@ const model = {_stageResizeObserver: null,_viewportSyncTimer: null,_lastViewportKey: "",- _mode: "canvas",+ _lastViewport: null,+ _mode: "",+ _surfaceMounted: false,+ _surfaceSwitching: false,_connectSequence: 0,_viewerToken: "",extensionMenuOpen: false,@@ -72,6 +95,10 @@ const model = {mainModelSummary: "",modelPresetSaving: false,browserInstallExpected: false,+ defaultHomepage: "about:blank",+ autofocusActivePage: true,+ _configLoadedAt: 0,+ _configRefreshPromise: null,async refreshStatus() {this.status = await callJsonApi("/plugins/_browser/status", {});@@ -99,11 +126,48 @@ const model = {applyExtensionPayload(response = {}) {this.extensionsRoot = response.root || EXTENSIONS_ROOT;this.extensionsList = Array.isArray(response.extensions) ? response.extensions : [];+ this.defaultHomepage = String(response.default_homepage || "about:blank").trim() || "about:blank";+ this.autofocusActivePage = normalizeBool(response.autofocus_active_page, true);this.modelPreset = String(response.model_preset || "");this.mainModelSummary = String(response.main_model_summary || "");this.modelPresetOptions = Array.isArray(response.model_preset_options)? response.model_preset_options: [];+ this._configLoadedAt = Date.now();+ },++ async ensureBrowserConfigLoaded(force = false) {+ if (!force && this._configLoadedAt && Date.now() - this._configLoadedAt < BROWSER_CONFIG_REFRESH_MS) {+ return;+ }+ if (this._configRefreshPromise) {+ await this._configRefreshPromise;+ return;+ }+ this._configRefreshPromise = (async () => {+ const response = await callJsonApi("/plugins/_browser/extensions", {+ action: "list",+ context_id: this.contextId || this.resolveContextId(),+ });+ if (!response?.ok) {+ throw new Error(response?.error || "Could not load browser settings.");+ }+ this.applyExtensionPayload(response);+ })();+ try {+ await this._configRefreshPromise;+ } finally {+ this._configRefreshPromise = null;+ }+ },++ async allowsToolAutofocus() {+ try {+ await this.ensureBrowserConfigLoaded();+ } catch (error) {+ console.warn("Browser autofocus setting could not be loaded", error);+ }+ return this.autofocusActivePage !== false;},toggleExtensionsMenu() {@@ -300,8 +364,9 @@ const model = {this.loading = true;this.error = "";const requestedBrowserId = this.normalizeBrowserId(options.browserId ?? options.browser_id);- this._mode = options?.mode === "modal" ? "modal" : "canvas";- if (this._mode === "modal") {+ const nextMode = options?.mode === "modal" ? "modal" : "canvas";+ this.prepareSurfaceOpen(nextMode, requestedBrowserId);+ if (nextMode === "modal") {this.setupFloatingModal(element);} else {this.setupCanvasSurface(element);@@ -309,7 +374,10 @@ const model = {this.contextId = this.resolveContextId();try {await this.refreshStatus();- await this.connectViewer({ browserId: requestedBrowserId });+ const viewport = await this.waitForSurfaceViewport();+ this.resetRenderedFrameIfViewportChanged(viewport, requestedBrowserId);+ await this.connectViewer({ browserId: requestedBrowserId, initialViewport: viewport });+ await this.syncViewportAfterSurfaceOpen();} catch (error) {this.error = error instanceof Error ? error.message : String(error);} finally {@@ -317,11 +385,80 @@ const model = {}},+ prepareSurfaceOpen(nextMode, requestedBrowserId = null) {+ const previousMode = this._mode;+ const modeChanged = this._surfaceMounted && previousMode && previousMode !== nextMode;+ const targetBrowserId = requestedBrowserId || this.activeBrowserId || this.firstBrowserId();+ this._mode = nextMode;+ this._surfaceMounted = true;+ this._lastViewportKey = "";+ if (!modeChanged && (this.frameSrc || !targetBrowserId)) return;++ this.resetRenderedFrame();+ this.resetViewportTracking();+ this._surfaceSwitching = Boolean(targetBrowserId);+ this.switchingBrowserId = targetBrowserId;+ },++ resetViewportTracking() {+ this._lastViewportKey = "";+ this._lastViewport = null;+ },++ resetRenderedFrame() {+ this.cancelFrameRender();+ this.frameSrc = "";+ this._lastFrameAt = 0;+ },++ resetRenderedFrameIfViewportChanged(viewport = null, requestedBrowserId = null) {+ if (!viewport || !this.frameSrc || !this._lastViewport) return;+ const targetBrowserId = requestedBrowserId || this.activeBrowserId || this.firstBrowserId();+ if (!this.sameBrowserId(this._lastViewport.browserId, targetBrowserId)) return;+ const changed = Math.abs(this._lastViewport.width - viewport.width) > VIEWPORT_SYNC_SIZE_TOLERANCE+ || Math.abs(this._lastViewport.height - viewport.height) > VIEWPORT_SYNC_SIZE_TOLERANCE;+ if (!changed) return;++ this.resetRenderedFrame();+ this.resetViewportTracking();+ this._surfaceSwitching = true;+ this.switchingBrowserId = targetBrowserId;+ },++ async waitForSurfaceViewport() {+ let lastKey = "";+ let stableCount = 0;+ for (let index = 0; index < 24; index += 1) {+ await nextAnimationFrame();+ const viewport = this.currentViewportSize();+ if (!viewport) continue;+ const key = `${viewport.width}x${viewport.height}`;+ if (key === lastKey) {+ stableCount += 1;+ if (stableCount >= 2) return viewport;+ } else {+ stableCount = 0;+ lastKey = key;+ }+ }+ return this.currentViewportSize();+ },++ async syncViewportAfterSurfaceOpen() {+ if (!this.connected || !this.activeBrowserId) return;+ await this.waitForSurfaceViewport();+ await this.syncViewport(true);+ if (this._mode !== "canvas") return;+ globalThis.setTimeout?.(() => this.queueViewportSync(true), 240);+ globalThis.setTimeout?.(() => this.queueViewportSync(true), 420);+ },+async connectViewer(options = {}) {if (!this.contextId) {this.connected = false;this.error = "No active chat context is selected.";this.switchingBrowserId = null;+ this._surfaceSwitching = false;return;}const requestedBrowserId = this.normalizeBrowserId(options.browserId ?? this.activeBrowserId);@@ -334,7 +471,7 @@ const model = {if (sequence !== this._connectSequence || viewerToken !== this._viewerToken) {return;}- const initialViewport = this.currentViewportSize();+ const initialViewport = options.initialViewport || this.currentViewportSize();let response;try {response = await websocket.request(@@ -355,6 +492,7 @@ const model = {} catch (error) {if (sequence === this._connectSequence && viewerToken === this._viewerToken) {this.switchingBrowserId = null;+ this._surfaceSwitching = false;throw error;}return;@@ -393,6 +531,7 @@ const model = {if (this.sameBrowserId(this.switchingBrowserId, incomingBrowserId || this.activeBrowserId)) {this.switchingBrowserId = null;}+ this._surfaceSwitching = false;} else {this.cancelFrameRender();if (!data.state) {@@ -573,7 +712,7 @@ const model = {},async openNewBrowser() {- await this.command("open", { url: "about:blank" });+ await this.command("open");},isActiveBrowser(browser) {@@ -647,6 +786,7 @@ const model = {if (this.sameBrowserId(this.switchingBrowserId, snapshotId || this.activeBrowserId)) {this.switchingBrowserId = null;}+ this._surfaceSwitching = false;},isSwitchingBrowser() {@@ -654,7 +794,7 @@ const model = {},isBusy() {- return Boolean(this.loading || this.commandInFlight || this.isSwitchingBrowser());+ return Boolean(this.loading || this.commandInFlight || this._surfaceSwitching || this.isSwitchingBrowser());},setActiveBrowserId(id) {@@ -664,6 +804,7 @@ const model = {this.activeBrowserId = exists ? numeric : null;if (this.activeBrowserId !== previous) {this._lastViewportKey = "";+ this._lastViewport = null;}},@@ -673,17 +814,47 @@ const model = {const rect = target.getBoundingClientRect();const naturalWidth = target.naturalWidth || rect.width;const naturalHeight = target.naturalHeight || rect.height;+ let contentLeft = rect.left;+ let contentTop = rect.top;+ let contentWidth = rect.width;+ let contentHeight = rect.height;++ const objectFit = globalThis.getComputedStyle?.(target)?.objectFit || "";+ if (+ target.matches?.(".browser-frame")+ && ["contain", "scale-down"].includes(objectFit)+ && naturalWidth > 0+ && naturalHeight > 0+ && rect.width > 0+ && rect.height > 0+ ) {+ const naturalRatio = naturalWidth / naturalHeight;+ const rectRatio = rect.width / rect.height;+ if (naturalRatio > rectRatio) {+ contentWidth = rect.width;+ contentHeight = rect.width / naturalRatio;+ contentTop = rect.top + (rect.height - contentHeight) / 2;+ } else {+ contentHeight = rect.height;+ contentWidth = rect.height * naturalRatio;+ contentLeft = rect.left + (rect.width - contentWidth) / 2;+ }+ }++ const relativeX = (event.clientX - contentLeft) / Math.max(1, contentWidth);+ const relativeY = (event.clientY - contentTop) / Math.max(1, contentHeight);return {- x: ((event.clientX - rect.left) / Math.max(1, rect.width)) * naturalWidth,- y: ((event.clientY - rect.top) / Math.max(1, rect.height)) * naturalHeight,+ x: Math.max(0, Math.min(naturalWidth, relativeX * naturalWidth)),+ y: Math.max(0, Math.min(naturalHeight, relativeY * naturalHeight)),};},currentViewportSize() {const stage = this._stageElement;if (!stage) return null;- const width = Math.floor(stage.clientWidth || 0);- const height = Math.floor(stage.clientHeight || 0);+ const rect = stage.getBoundingClientRect?.();+ const width = Math.round(rect?.width || stage.clientWidth || 0);+ const height = Math.round(rect?.height || stage.clientHeight || 0);if (width < 80 || height < 80) return null;return {width: Math.max(320, width),@@ -698,7 +869,7 @@ const model = {this._viewportSyncTimer = globalThis.setTimeout(() => {this._viewportSyncTimer = null;void this.syncViewport(force);- }, force ? 0 : 80);+ }, force ? 0 : VIEWPORT_SYNC_DEBOUNCE_MS);},async syncViewport(force = false) {@@ -706,7 +877,16 @@ const model = {const viewport = this.currentViewportSize();if (!viewport) return;const key = `${this.activeBrowserId}:${viewport.width}x${viewport.height}`;- if (this._lastViewportKey === key) return;+ if (+ this._lastViewportKey === key+ || (+ !force+ && this._lastViewport+ && this.sameBrowserId(this._lastViewport.browserId, this.activeBrowserId)+ && Math.abs(this._lastViewport.width - viewport.width) <= VIEWPORT_SYNC_SIZE_TOLERANCE+ && Math.abs(this._lastViewport.height - viewport.height) <= VIEWPORT_SYNC_SIZE_TOLERANCE+ )+ ) return;try {await websocket.emit("browser_viewer_input", {context_id: this.contextId,@@ -717,8 +897,14 @@ const model = {height: viewport.height,});this._lastViewportKey = key;+ this._lastViewport = {+ browserId: this.activeBrowserId,+ width: viewport.width,+ height: viewport.height,+ };} catch (error) {this._lastViewportKey = "";+ this._lastViewport = null;console.warn("Browser viewport sync failed", error);}},@@ -793,6 +979,8 @@ const model = {this._connectSequence += 1;this._viewerToken = "";this.switchingBrowserId = null;+ this._surfaceMounted = false;+ this._surfaceSwitching = false;this.commandInFlight = false;if (this.contextId) {try {@@ -803,7 +991,7 @@ const model = {this._stateOff?.();this._frameOff = null;this._stateOff = null;- this.cancelFrameRender();+ this.resetRenderedFrame();this._floatingCleanup?.();this._floatingCleanup = null;this._stageResizeObserver?.disconnect?.();@@ -813,7 +1001,7 @@ const model = {globalThis.clearTimeout(this._viewportSyncTimer);this._viewportSyncTimer = null;}- this._lastViewportKey = "";+ this.resetViewportTracking();this.extensionMenuOpen = false;this.extensionActionLoading = false;this.extensionsListLoading = false;diff --git a/plugins/_browser/webui/config.html b/plugins/_browser/webui/config.htmlindex 1d195b83d..d28203b95 100644--- a/plugins/_browser/webui/config.html+++ b/plugins/_browser/webui/config.html@@ -15,6 +15,41 @@x-effect="$store.browserConfig.bindConfig(config)"x-destroy="$store.browserConfig.cleanup()">+ <div class="browser-config-card">+ <div class="section-title">Browsing</div>+ <div class="section-description">+ Set how new Browser sessions start and whether agent activity should pull focus.+ </div>++ <label class="browser-config-field">+ <span class="browser-config-field-label">Starting page</span>+ <input+ type="text"+ x-model="$store.browserConfig.config.default_homepage"+ placeholder="about:blank or https://example.com"+ autocomplete="off"+ />+ </label>++ <label class="browser-config-switch-row">+ <span class="browser-config-switch-copy">+ <span class="browser-config-field-label">Autofocus active page</span>+ <span class="browser-config-field-help">Focus pages opened or changed by Browser tool results.</span>+ </span>+ <span class="browser-config-toggle-with-label">+ <span class="browser-config-toggle-label" x-text="$store.browserConfig.autofocusLabel()"></span>+ <span class="browser-config-toggle">+ <input+ type="checkbox"+ :checked="$store.browserConfig.config.autofocus_active_page !== false"+ @change="$store.browserConfig.setAutofocusActivePage($event.target.checked)"+ />+ <span class="browser-config-switch"></span>+ </span>+ </span>+ </label>+ </div>+<div class="browser-config-card"><div class="section-title">Extensions</div><div class="section-description">@@ -86,6 +121,65 @@border-radius: 8px;}+ .browser-config-field {+ display: flex;+ flex-direction: column;+ gap: 6px;+ }++ .browser-config-field-label {+ color: var(--color-text);+ font-size: 0.84rem;+ font-weight: 650;+ }++ .browser-config-field-help {+ color: var(--color-text-secondary);+ font-size: 0.78rem;+ line-height: 1.35;+ }++ .browser-config-field input[type="text"] {+ width: 100%;+ min-height: 36px;+ padding: 7px 10px;+ border: 1px solid color-mix(in srgb, var(--color-border) 74%, transparent);+ border-radius: 8px;+ background: var(--color-input);+ color: var(--color-text);+ font: inherit;+ }++ .browser-config-switch-row {+ display: grid;+ grid-template-columns: minmax(0, 1fr) auto;+ align-items: center;+ gap: 16px;+ min-height: 46px;+ padding-top: 2px;+ }++ .browser-config-switch-copy {+ display: flex;+ min-width: 0;+ flex-direction: column;+ gap: 3px;+ }++ .browser-config-toggle-with-label {+ display: inline-flex;+ align-items: center;+ gap: 8px;+ }++ .browser-config-toggle-label {+ min-width: 24px;+ color: var(--color-text-secondary);+ font-size: 0.78rem;+ font-weight: 650;+ text-align: right;+ }+.browser-config-extension-list {display: flex;flex-direction: column;@@ -166,6 +260,7 @@height: 100%;border-radius: 999px;background: color-mix(in srgb, var(--color-border) 78%, transparent);+ pointer-events: none;transition: background-color 0.18s cubic-bezier(0.4, 0, 0.2, 1);}