Agent Zero AI framework
Add contextual file browser surface actions
Alessandro1c9b5c8b2167
diff --git a/plugins/_desktop/helpers/desktop_session.py b/plugins/_desktop/helpers/desktop_session.pyindex 0b8960916..c2a7e270b 100644--- a/plugins/_desktop/helpers/desktop_session.py+++ b/plugins/_desktop/helpers/desktop_session.py@@ -23,7 +23,7 @@ from plugins._desktop.helpers import desktop_statefrom plugins._office.helpers import document_store, libreoffice-OFFICIAL_EXTENSIONS = {"odt", "ods", "odp", "docx", "xlsx", "pptx"}+OFFICIAL_EXTENSIONS = {"odt", "ods", "odp", "docx", "xlsx", "pptx", "txt"}PLUGIN_NAME = "_desktop"SYSTEM_SESSION_ID = "agent-zero-desktop"SYSTEM_FILE_ID = "system-desktop"diff --git a/plugins/_desktop/webui/desktop-store.js b/plugins/_desktop/webui/desktop-store.jsindex a8acd6a53..e106f5f12 100644--- a/plugins/_desktop/webui/desktop-store.js+++ b/plugins/_desktop/webui/desktop-store.js@@ -42,7 +42,7 @@ function extensionOf(path = "") {}function isOfficialExtension(extension = "") {- return ["odt", "ods", "odp", "docx", "xlsx", "pptx"].includes(String(extension || "").toLowerCase());+ return ["odt", "ods", "odp", "docx", "xlsx", "pptx", "txt"].includes(String(extension || "").toLowerCase());}function parentPath(path = "") {@@ -579,7 +579,7 @@ const model = {},async openPath(path) {- await this.openSession({ path: String(path || "") });+ return await this.openSession({ path: String(path || "") });},async openSession(payload = {}) {@@ -1047,7 +1047,7 @@ const model = {isBinaryOffice(tab = this.session) {const ext = String(tab?.extension || tab?.document?.extension || "").toLowerCase();- return ["odt", "ods", "odp", "docx", "xlsx", "pptx"].includes(ext);+ return ["odt", "ods", "odp", "docx", "xlsx", "pptx", "txt"].includes(ext);},hasOfficialOffice(tab = this.session) {@@ -2321,6 +2321,7 @@ const model = {if (ext === "odt" || ext === "docx") return "description";if (ext === "ods" || ext === "xlsx") return "table_chart";if (ext === "odp" || ext === "pptx") return "co_present";+ if (ext === "txt") return "notes";return "draft";},diff --git a/plugins/_editor/webui/editor-store.js b/plugins/_editor/webui/editor-store.jsindex 4418bce71..653ff7108 100644--- a/plugins/_editor/webui/editor-store.js+++ b/plugins/_editor/webui/editor-store.js@@ -874,7 +874,7 @@ const model = {},async openPath(path) {- await this.openSession({ path: String(path || "") });+ return await this.openSession({ path: String(path || "") });},async openSession(payload = {}) {diff --git a/plugins/_office/helpers/document_store.py b/plugins/_office/helpers/document_store.pyindex 1d6c472d0..906f92cbb 100644--- a/plugins/_office/helpers/document_store.py+++ b/plugins/_office/helpers/document_store.py@@ -23,7 +23,8 @@ from plugins._office.helpers import pptx_writerPLUGIN_NAME = "_office"OPEN_DOCUMENT_EXTENSIONS = {"odt", "ods", "odp"}OOXML_EXTENSIONS = {"docx", "xlsx", "pptx"}-SUPPORTED_EXTENSIONS = {"md", *OPEN_DOCUMENT_EXTENSIONS, *OOXML_EXTENSIONS}+DESKTOP_TEXT_EXTENSIONS = {"txt"}+SUPPORTED_EXTENSIONS = {"md", *OPEN_DOCUMENT_EXTENSIONS, *OOXML_EXTENSIONS, *DESKTOP_TEXT_EXTENSIONS}DEFAULT_TTL_SECONDS = 8 * 60 * 60MAX_SAVE_BYTES = 512 * 1024 * 1024ODF_OFFICE_NS = "urn:oasis:names:tc:opendocument:xmlns:office:1.0"@@ -636,6 +637,8 @@ def template_bytes(kind: str, ext: str, title: str, content: str) -> bytes:ext = normalize_extension(ext or "md")if ext == "md":return _markdown(title, content).encode("utf-8")+ if ext == "txt":+ return (str(content or "") or str(title or "")).encode("utf-8")if ext == "odt":return odt_bytes(title, content)if ext == "ods":diff --git a/tests/test_office_document_store.py b/tests/test_office_document_store.pyindex b30e74c78..b7d704b63 100644--- a/tests/test_office_document_store.py+++ b/tests/test_office_document_store.py@@ -83,6 +83,17 @@ def test_document_store_create_defaults_to_markdown(office_state):assert Path(doc["path"]).read_text(encoding="utf-8").startswith("# Research Note")+def test_text_files_register_as_desktop_documents(office_state):+ path = office_state.workdir / "plain-note.txt"+ path.write_text("Plain text belongs on the Desktop surface.\n", encoding="utf-8")++ doc = document_store.register_document(path)++ assert doc["extension"] == "txt"+ assert "txt" in document_store.DESKTOP_TEXT_EXTENSIONS+ assert "txt" in desktop_session.OFFICIAL_EXTENSIONS++@pytest.mark.parametrize(("kind", "title", "fmt", "expected_name"),[diff --git a/webui/components/modals/file-browser/file-browser-store.js b/webui/components/modals/file-browser/file-browser-store.jsindex 7924fb1bd..56967e1dd 100644--- a/webui/components/modals/file-browser/file-browser-store.js+++ b/webui/components/modals/file-browser/file-browser-store.js@@ -2,6 +2,47 @@ import { createStore } from "/js/AlpineStore.js";import { fetchApi } from "/js/api.js";import { formatDateTime } from "/js/time-utils.js";import { store as fileEditorStore } from "/components/modals/file-editor/file-editor-store.js";+import { openLatest as openLatestSurface } from "/js/surfaces.js";++const MARKDOWN_EXTENSIONS = new Set(["md", "markdown", "mdown"]);+const DESKTOP_EXTENSIONS = new Set(["odt", "ods", "odp", "docx", "xlsx", "pptx", "txt"]);+const BROWSER_EXTENSIONS = new Set([+ "html",+ "htm",+ "xhtml",+ "svg",+ "xml",+ "pdf",+ "png",+ "jpg",+ "jpeg",+ "gif",+ "webp",+ "bmp",+ "ico",+]);++const SURFACE_ACTIONS = {+ editor: {+ label: "Open in Editor",+ icon: "article",+ title: "Open Markdown in Editor",+ },+ desktop: {+ label: "Open in Desktop",+ icon: "desktop_windows",+ title: "Open document in Desktop",+ },+ browser: {+ label: "Open in Browser",+ icon: "language",+ title: "Open web-viewable file in Browser",+ },+};++function delay(ms) {+ return new Promise((resolve) => globalThis.setTimeout(resolve, ms));+}// Model migrated from legacy file_browser.js (lift-and-shift)const model = {@@ -214,6 +255,57 @@ const model = {return path.startsWith("/") ? path : `/${path}`;},+ fileExtension(file = {}) {+ const name = String(file?.name || file?.path || "").split(/[?#]/, 1)[0].toLowerCase();+ const index = name.lastIndexOf(".");+ return index >= 0 ? name.slice(index + 1) : "";+ },++ fileSurfaceTarget(file = {}) {+ if (!file || file.is_dir) return "";+ const ext = this.fileExtension(file);+ if (MARKDOWN_EXTENSIONS.has(ext)) return "editor";+ if (BROWSER_EXTENSIONS.has(ext)) return "browser";+ if (DESKTOP_EXTENSIONS.has(ext)) return "desktop";+ return "";+ },++ canOpenInSurface(file = {}) {+ return Boolean(this.fileSurfaceTarget(file));+ },++ surfaceAction(file = {}) {+ const target = this.fileSurfaceTarget(file);+ return target ? SURFACE_ACTIONS[target] : null;+ },++ surfaceActionLabel(file = {}) {+ return this.surfaceAction(file)?.label || "Open";+ },++ surfaceActionIcon(file = {}) {+ return this.surfaceAction(file)?.icon || "open_in_new";+ },++ surfaceActionTitle(file = {}) {+ return this.surfaceAction(file)?.title || "Open file";+ },++ fileUrl(file = {}) {+ const path = this.normalizePath(String(file?.path || ""));+ const encodedPath = path+ .split("/")+ .map((part) => encodeURIComponent(part))+ .join("/");+ return `file://${encodedPath}`;+ },++ storeHasPath(surfaceStore = {}, path = "") {+ const normalizedPath = this.normalizePath(path);+ const activePath = surfaceStore?.session?.path || surfaceStore?.session?.document?.path || "";+ return this.normalizePath(activePath) === normalizedPath;+ },+buildChildPath(name) {const base = this.normalizePath(this.browser.currentPath || "");const trimmedBase = base.replace(/\/$/, "");@@ -720,6 +812,58 @@ const model = {return store._handleFileUpload(event); // bind to model to ensure correct context},+ async openInSurface(file = {}) {+ const target = this.fileSurfaceTarget(file);+ const path = this.normalizePath(String(file?.path || ""));+ if (!target || !path) return;++ this.closeDropdown();++ try {+ if (target === "browser") {+ const url = this.fileUrl(file);+ const { store: browserStore } = await import("/plugins/_browser/webui/browser-store.js");+ await openLatestSurface("browser", { url, source: "file-browser" });++ let opened = false;+ for (let attempt = 0; attempt < 40 && !opened; attempt += 1) {+ opened = await browserStore.openUrlIntent(url, { source: "file-browser" });+ if (!opened) await delay(75);+ }+ if (!opened) {+ throw new Error("Browser surface is unavailable.");+ }+ } else {+ await openLatestSurface(target, { path, source: "file-browser" });+ if (target === "editor") {+ const { store: editorStore } = await import("/plugins/_editor/webui/editor-store.js");+ if (!this.storeHasPath(editorStore, path)) {+ const session = await editorStore.openPath(path);+ if (!session || session.ok === false) {+ throw new Error(editorStore.error || "Markdown could not be opened.");+ }+ }+ }+ if (target === "desktop") {+ const { store: desktopStore } = await import("/plugins/_desktop/webui/desktop-store.js");+ if (!this.storeHasPath(desktopStore, path)) {+ const session = await desktopStore.openPath(path);+ if (!session || session.ok === false) {+ throw new Error(desktopStore.error || "Document could not be opened.");+ }+ }+ }+ }++ await window.closeModal?.("modals/file-browser/file-browser.html");+ } catch (error) {+ window.toastFrontendError?.(+ error?.message || "Could not open file",+ "File Browser"+ );+ }+ },+async _handleFileUpload(event) {try {const files = event.target.files;diff --git a/webui/components/modals/file-browser/file-browser.html b/webui/components/modals/file-browser/file-browser.htmlindex 271438b1c..7e090dc8e 100644--- a/webui/components/modals/file-browser/file-browser.html+++ b/webui/components/modals/file-browser/file-browser.html@@ -199,7 +199,18 @@<buttontype="button"class="dropdown-item"- x-show="!file.is_dir && file.size <= 1048576"+ x-show="$store.fileBrowser.canOpenInSurface(file)"+ @click="$store.fileBrowser.openInSurface(file)"+ :title="$store.fileBrowser.surfaceActionTitle(file)"+ >+ <span class="material-symbols-outlined" x-text="$store.fileBrowser.surfaceActionIcon(file)"></span>+ <span x-text="$store.fileBrowser.surfaceActionLabel(file)"></span>+ </button>++ <button+ type="button"+ class="dropdown-item"+ x-show="!file.is_dir && file.size <= 1048576 && !$store.fileBrowser.canOpenInSurface(file)"@click="$store.fileBrowser.openFileEditor(file)"><span class="material-symbols-outlined">file_open</span>diff --git a/webui/components/tooltips/tooltip-store.js b/webui/components/tooltips/tooltip-store.jsindex 2ecee7c00..b5ec67738 100644--- a/webui/components/tooltips/tooltip-store.js+++ b/webui/components/tooltips/tooltip-store.js@@ -47,6 +47,16 @@ function initBootstrapTooltips(root = document) {tooltipTargets.forEach((element) => ensureBootstrapTooltip(element));}+function disposeBootstrapTooltip(element) {+ const instance = globalThis.bootstrap?.Tooltip?.getInstance(element);+ if (!instance) return;+ try {+ instance.dispose();+ } catch {+ // Bootstrap 5 can throw while disposing an already-torn-down tooltip node.+ }+}+function observeBootstrapTooltips() {if (!globalThis.bootstrap?.Tooltip) return;@@ -75,10 +85,7 @@ function observeBootstrapTooltips() {);tooltipElements.forEach((el) => {if (el.isConnected) return;- const instance = globalThis.bootstrap?.Tooltip?.getInstance(el);- if (instance) {- instance.dispose();- }+ disposeBootstrapTooltip(el);});});