Skip to content

Agent Zero AI framework

Persist Browser and Desktop modal modes

Alessandro2e0069751358
diff --git a/tests/test_browser_agent_regressions.py b/tests/test_browser_agent_regressions.py
index e4c339468..fba109ead 100644
--- a/tests/test_browser_agent_regressions.py
+++ b/tests/test_browser_agent_regressions.py
@@ -662,6 +662,36 @@ def test_browser_entry_points_prefer_canvas_and_modal_dock_handoff():
assert "Alpine.store" not in js
+def test_browser_and_desktop_surface_buttons_remember_latest_window_mode():
+ canvas_store = (PROJECT_ROOT / "webui" / "components" / "canvas" / "right-canvas-store.js").read_text(
+ encoding="utf-8"
+ )
+ canvas_html = (PROJECT_ROOT / "webui" / "components" / "canvas" / "right-canvas.html").read_text(
+ encoding="utf-8"
+ )
+ modals_js = (PROJECT_ROOT / "webui" / "js" / "modals.js").read_text(encoding="utf-8")
+
+ assert "surfaceModes: {}" in canvas_store
+ assert "recordSurfaceMode(surfaceId" in canvas_store
+ assert "latestSurfaceMode(surfaceId)" in canvas_store
+ assert "async openLatest(surfaceId" in canvas_store
+ assert "async openModalSurface(surfaceId" in canvas_store
+ assert "this.recordSurfaceMode(targetId, SURFACE_MODE_CANVAS" in canvas_store
+ assert "this.recordSurfaceMode(targetId, SURFACE_MODE_MODAL)" in canvas_store
+ assert "surfaceModes: this.surfaceModes" in canvas_store
+ assert "normalizeSurfaceMode(mode)" in canvas_store
+
+ assert '@click="$store.rightCanvas.openLatest(surface.id)"' in canvas_html
+ assert '@click="$store.rightCanvas.open(surface.id)"' in canvas_html
+
+ assert 'rightCanvasStore.recordSurfaceMode?.(metadata.surfaceId, "modal")' in modals_js
+ assert "modalRequiresExplicitClose" in modals_js
+ assert '"plugins/_browser/webui/main.html"' in modals_js
+ assert '"plugins/_office/webui/main.html"' in modals_js
+ assert "&& !modalRequiresExplicitClose(newModal)" in modals_js
+ assert "if (modalRequiresExplicitClose(modalStack[modalStack.length - 1])) return;" in modals_js
+
+
def test_browser_tool_does_not_auto_open_canvas_policy_is_documented():
prompt = (
PROJECT_ROOT / "plugins" / "_browser" / "prompts" / "agent.system.tool.browser.md"
diff --git a/webui/components/canvas/right-canvas-store.js b/webui/components/canvas/right-canvas-store.js
index f6fb5d5e2..4d155d1c6 100644
--- a/webui/components/canvas/right-canvas-store.js
+++ b/webui/components/canvas/right-canvas-store.js
@@ -7,6 +7,8 @@ const MIN_WIDTH = 420;
const MAX_WIDTH = 900;
const DESKTOP_BREAKPOINT = 1200;
const MOBILE_BREAKPOINT = 768;
+const SURFACE_MODE_CANVAS = "canvas";
+const SURFACE_MODE_MODAL = "modal";
function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
@@ -16,9 +18,14 @@ function viewportWidth() {
return Math.max(document.documentElement.clientWidth || 0, globalThis.innerWidth || 0);
}
+function normalizeSurfaceMode(mode = "") {
+ return mode === SURFACE_MODE_MODAL ? SURFACE_MODE_MODAL : SURFACE_MODE_CANVAS;
+}
+
const model = {
surfaces: [],
activeSurfaceId: "",
+ surfaceModes: {},
isOpen: false,
width: DEFAULT_WIDTH,
isOverlayMode: false,
@@ -75,6 +82,9 @@ const model = {
} else {
this.surfaces.push(normalized);
}
+ if (!this.surfaceModes[normalized.id]) {
+ this.surfaceModes[normalized.id] = SURFACE_MODE_CANVAS;
+ }
this.surfaces.sort((a, b) => (a.order ?? 100) - (b.order ?? 100));
if (!this._registering) {
this.ensureActiveSurface();
@@ -116,6 +126,7 @@ const model = {
this.activeSurfaceId = targetId;
this.isOpen = true;
+ this.recordSurfaceMode(targetId, SURFACE_MODE_CANVAS, { persist: false });
this._lastPayloadBySurface[targetId] = payload || {};
this.persist();
this.applyLayoutState();
@@ -128,6 +139,30 @@ const model = {
return true;
},
+ recordSurfaceMode(surfaceId, mode = SURFACE_MODE_CANVAS, options = {}) {
+ const targetId = String(surfaceId || "").trim();
+ if (!targetId) return;
+ this.surfaceModes = {
+ ...this.surfaceModes,
+ [targetId]: normalizeSurfaceMode(mode),
+ };
+ if (options.persist !== false) this.persist();
+ },
+
+ latestSurfaceMode(surfaceId) {
+ const targetId = String(surfaceId || "").trim();
+ return normalizeSurfaceMode(this.surfaceModes[targetId]);
+ },
+
+ async openLatest(surfaceId = "", payload = {}) {
+ const targetId = surfaceId || this.activeSurfaceId || this.panelSurfaces[0]?.id || "";
+ if (!targetId) return false;
+ if (this.latestSurfaceMode(targetId) === SURFACE_MODE_MODAL) {
+ return await this.openModalSurface(targetId, payload);
+ }
+ return await this.open(targetId, payload);
+ },
+
async close() {
const surface = this.currentSurface();
this.isOpen = false;
@@ -194,6 +229,8 @@ const model = {
const surface = this.getSurface(targetId);
const modalPath = payload.modalPath || surface?.modalPath || "";
if (!surface || !modalPath) return false;
+ const openModal = globalThis.ensureModalOpen || globalThis.openModal;
+ if (!openModal) return false;
if (this.activeSurfaceId === targetId) {
this.isOpen = false;
this.persist();
@@ -204,13 +241,41 @@ const model = {
console.error(`Canvas surface ${targetId} failed to close while undocking`, error);
}
}
- const modalPromise = globalThis.ensureModalOpen?.(modalPath);
+ this.recordSurfaceMode(targetId, SURFACE_MODE_MODAL);
+ const modalPromise = openModal(modalPath);
if (modalPromise?.catch) {
modalPromise.catch((error) => console.error(`Canvas surface ${targetId} failed to undock`, error));
}
return true;
},
+ async openModalSurface(surfaceId = "", payload = {}) {
+ const targetId = surfaceId || this.activeSurfaceId;
+ const surface = this.getSurface(targetId);
+ const modalPath = payload.modalPath || surface?.modalPath || "";
+ if (!surface || !modalPath) return false;
+ const openModal = globalThis.ensureModalOpen || globalThis.openModal;
+ if (!openModal) return false;
+
+ if (this.isOpen && this.activeSurfaceId === targetId) {
+ this.isOpen = false;
+ this.persist();
+ this.applyLayoutState();
+ try {
+ await surface.close?.(this._lastPayloadBySurface[targetId] || {});
+ } catch (error) {
+ console.error(`Canvas surface ${targetId} failed to close before modal open`, error);
+ }
+ }
+
+ this.recordSurfaceMode(targetId, SURFACE_MODE_MODAL);
+ const modalPromise = openModal(modalPath);
+ if (modalPromise?.catch) {
+ modalPromise.catch((error) => console.error(`Canvas surface ${targetId} failed to open as modal`, error));
+ }
+ return true;
+ },
+
async undockActiveSurface() {
return await this.undockSurface(this.activeSurfaceId);
},
@@ -301,6 +366,7 @@ const model = {
JSON.stringify({
isOpen: this.isOpen,
activeSurfaceId: this.activeSurfaceId,
+ surfaceModes: this.surfaceModes,
width: this.width,
}),
);
@@ -315,6 +381,12 @@ const model = {
const saved = JSON.parse(localStorage.getItem(STORAGE_KEY) || "{}");
this.isOpen = false;
this.activeSurfaceId = String(saved.activeSurfaceId || "");
+ this.surfaceModes = Object.fromEntries(
+ Object.entries(saved.surfaceModes || {}).map(([surfaceId, mode]) => [
+ surfaceId,
+ normalizeSurfaceMode(mode),
+ ]),
+ );
if (saved.width) this.width = Number(saved.width);
} catch (error) {
console.warn("Could not restore right canvas state", error);
diff --git a/webui/components/canvas/right-canvas.html b/webui/components/canvas/right-canvas.html
index 3cba11a98..c9ece5644 100644
--- a/webui/components/canvas/right-canvas.html
+++ b/webui/components/canvas/right-canvas.html
@@ -46,7 +46,7 @@
:class="{ 'is-active': $store.rightCanvas.isSurfaceActive(surface.id) && $store.rightCanvas.isOpen }"
:title="surface.title"
:aria-label="surface.title"
- @click="$store.rightCanvas.open(surface.id)"
+ @click="$store.rightCanvas.openLatest(surface.id)"
>
<template x-if="surface.image">
<img class="right-canvas-surface-image" :src="surface.image" alt="" aria-hidden="true">
diff --git a/webui/js/modals.js b/webui/js/modals.js
index a7a2b5b36..a449bd533 100644
--- a/webui/js/modals.js
+++ b/webui/js/modals.js
@@ -5,6 +5,22 @@ import { store as rightCanvasStore } from "/components/canvas/right-canvas-store
// Modal functionality
const modalStack = [];
+const EXPLICIT_CLOSE_MODAL_PATHS = new Set([
+ "plugins/_browser/webui/main.html",
+ "plugins/_office/webui/main.html",
+]);
+
+function normalizeModalPath(modalPath = "") {
+ return String(modalPath || "").replace(/^\/+/, "");
+}
+
+function modalRequiresExplicitClose(modalOrElement) {
+ const element = modalOrElement?.element || modalOrElement;
+ const path = normalizeModalPath(modalOrElement?.path || element?.path || "");
+ return EXPLICIT_CLOSE_MODAL_PATHS.has(path)
+ || element?.classList?.contains("modal-explicit-close")
+ || element?.querySelector?.(".modal-inner")?.classList?.contains("modal-explicit-close");
+}
function findModalIndexByPath(modalPath) {
return modalStack.findIndex((modal) => modal.path === modalPath);
@@ -115,7 +131,11 @@ function createModalElement(path) {
mouseDownTarget = event.target;
});
newModal.addEventListener("mouseup", (event) => {
- if (event.target === newModal && mouseDownTarget === newModal) {
+ if (
+ event.target === newModal
+ && mouseDownTarget === newModal
+ && !modalRequiresExplicitClose(newModal)
+ ) {
closeModal();
}
mouseDownTarget = null;
@@ -187,6 +207,8 @@ function configureModalDockButton(modal, doc) {
return;
}
+ rightCanvasStore.recordSurfaceMode?.(metadata.surfaceId, "modal");
+
const button = document.createElement("button");
button.type = "button";
button.className = "modal-dock-button";
@@ -469,6 +491,7 @@ document.addEventListener("click", async (e) => {
// Close modal on escape key (closes only the top modal)
document.addEventListener("keydown", (e) => {
if (e.key === "Escape" && modalStack.length > 0) {
+ if (modalRequiresExplicitClose(modalStack[modalStack.length - 1])) return;
closeModal();
}
});