Agent Zero AI framework
Keep Office canvas documents live after edits
Alessandro9950aa4f59de
diff --git a/plugins/_office/api/office_session.py b/plugins/_office/api/office_session.pyindex 2ec186b9c..69691c24d 100644--- a/plugins/_office/api/office_session.py+++ b/plugins/_office/api/office_session.py@@ -76,7 +76,7 @@ class OfficeSession(ApiHandler):origin=self._origin(request),)if str(doc.get("extension") or "").lower() in libreoffice_desktop.OFFICIAL_EXTENSIONS:- desktop = libreoffice_desktop.get_manager().open(doc)+ desktop = libreoffice_desktop.get_manager().open(doc, refresh=input.get("refresh") is True)if not desktop.get("available"):document_store.close_session(session_id=store_session["session_id"])return {diff --git a/plugins/_office/extensions/webui/right_canvas_register_surfaces/register-office.js b/plugins/_office/extensions/webui/right_canvas_register_surfaces/register-office.jsindex c6196dfc0..f82a364ad 100644--- a/plugins/_office/extensions/webui/right_canvas_register_surfaces/register-office.js+++ b/plugins/_office/extensions/webui/right_canvas_register_surfaces/register-office.js@@ -2,7 +2,7 @@ import { store as officeStore } from "/plugins/_office/webui/office-store.js";void officeStore;-function waitForElement(selector, timeoutMs = 3000) {+function waitForElement(selector, timeoutMs = 10000) {const found = document.querySelector(selector);if (found) return Promise.resolve(found);return new Promise((resolve) => {@@ -42,6 +42,7 @@ export default async function registerOfficeSurface(canvas) {},async open(payload = {}) {const panel = await waitForElement('[data-surface-id="office"] .office-panel');+ if (!panel) throw new Error("Office canvas panel did not mount.");const office = globalThis.Alpine?.store?.("office");await office?.onMount?.(panel, { mode: "canvas" });await office?.onOpen?.(payload);diff --git a/plugins/_office/extensions/webui/set_messages_after_loop/auto-open-document-results.js b/plugins/_office/extensions/webui/set_messages_after_loop/auto-open-document-results.jsindex 44dbda416..755073992 100644--- a/plugins/_office/extensions/webui/set_messages_after_loop/auto-open-document-results.js+++ b/plugins/_office/extensions/webui/set_messages_after_loop/auto-open-document-results.js@@ -1,9 +1,9 @@const SYNC_WINDOW_MS = 10 * 60 * 1000;+const DESKTOP_OFFICE_FORMATS = new Set(["odt", "ods", "odp", "docx", "xlsx", "pptx"]);const syncedDocumentResults = new Set();export default async function syncDocumentResultsIntoOpenCanvas(context) {if (!context?.results?.length || context.historyEmpty) return;- if (!isOfficeCanvasAlreadyOpen()) return;for (const { args } of context.results) {const payload = getDocumentPayload(args);@@ -25,13 +25,19 @@ export default async function syncDocumentResultsIntoOpenCanvas(context) {if (syncedDocumentResults.has(key)) continue;syncedDocumentResults.add(key);+ if (!isOfficeCanvasOrModalOpen() && shouldColdOpenOfficeCanvas(payload, document)) {+ await openOfficeCanvasFromResult({ path, file_id: fileId });+ continue;+ }+globalThis.setTimeout(async () => {- if (!isOfficeCanvasAlreadyOpen()) return;+ if (!isOfficeCanvasOrModalOpen()) return;const office = globalThis.Alpine?.store?.("office");if (!office || isDirtySameDocument(office, { path, file_id: fileId })) return;await office.openSession?.({path,file_id: fileId,+ refresh: true,source: "tool-result-sync",});}, 0);@@ -56,7 +62,9 @@ function pickPayloadFields(args = {}) {"_tool_name","tool_name","action",+ "canvas_surface","file_id",+ "format","path","version","last_modified",@@ -76,11 +84,55 @@ function shouldSyncOpenOfficeCanvas(args = {}, payload = {}) {return ["create", "open", "edit", "restore_version"].includes(action);}+function shouldColdOpenOfficeCanvas(payload = {}, document = {}) {+ const action = String(payload.action || "").trim().toLowerCase().replace("-", "_");+ if (!["create", "open"].includes(action)) return false;+ return DESKTOP_OFFICE_FORMATS.has(documentFormat(payload, document));+}++async function openOfficeCanvasFromResult(document = {}) {+ const canvas = globalThis.Alpine?.store?.("rightCanvas")+ || (await import("/components/canvas/right-canvas-store.js")).store;+ await canvas?.open?.("office", {+ path: document.path || "",+ file_id: document.file_id || "",+ refresh: true,+ source: "tool-result-sync",+ });+}++function documentFormat(payload = {}, document = {}) {+ return String(+ payload.format+ || payload.extension+ || document.extension+ || extensionOf(payload.path || document.path || ""),+ ).trim().toLowerCase().replace(/^\./, "");+}++function extensionOf(path = "") {+ const name = String(path || "").split("?")[0].split("#")[0].split("/").filter(Boolean).pop() || "";+ const index = name.lastIndexOf(".");+ return index >= 0 ? name.slice(index + 1) : "";+}+function isOfficeCanvasAlreadyOpen() {const canvas = globalThis.Alpine?.store?.("rightCanvas");return Boolean(canvas?.isOpen && canvas?.activeSurfaceId === "office");}+function isOfficeCanvasOrModalOpen() {+ return Boolean(isOfficeCanvasAlreadyOpen() || isOfficeModalAlreadyOpen());+}++function isOfficeModalAlreadyOpen() {+ return Boolean(+ globalThis.isModalOpen?.("/plugins/_office/webui/main.html")+ || globalThis.isModalOpen?.("plugins/_office/webui/main.html")+ || globalThis.document?.querySelector?.(".office-modal .office-panel, .modal .office-panel"),+ );+}+function isDirtySameDocument(office, document = {}) {if (!office?.dirty || !office?.session) return false;const path = String(document.path || "");diff --git a/plugins/_office/helpers/artifact_editor.py b/plugins/_office/helpers/artifact_editor.pyindex e32aeaf65..bbec68e1e 100644--- a/plugins/_office/helpers/artifact_editor.py+++ b/plugins/_office/helpers/artifact_editor.py@@ -139,7 +139,13 @@ def _refresh_open_editor_sessions(file_id: str) -> None:markdown_sessions.get_manager().refresh_document(file_id)except Exception:# Direct artifact edits should never fail just because no canvas is open.- return+ pass+ try:+ from plugins._office.helpers import libreoffice_desktop++ libreoffice_desktop.get_manager().refresh_document(file_id)+ except Exception:+ passdef normalize_operation(diff --git a/plugins/_office/helpers/libreoffice_desktop.py b/plugins/_office/helpers/libreoffice_desktop.pyindex 1b5e1e0ab..757e6e9fb 100644--- a/plugins/_office/helpers/libreoffice_desktop.py+++ b/plugins/_office/helpers/libreoffice_desktop.py@@ -5,6 +5,7 @@ import fcntlimport hashlibimport jsonimport os+import reimport shutilimport socketimport subprocess@@ -88,10 +89,12 @@ class DesktopSession:width: int = DEFAULT_SCREEN_WIDTHheight: int = DEFAULT_SCREEN_HEIGHTprocesses: dict[str, subprocess.Popen[Any]] = field(default_factory=dict)+ process_ids: dict[str, int] = field(default_factory=dict)+ owns_processes: bool = Truestarted_at: float = field(default_factory=time.time)def alive(self) -> bool:- return _running(self.processes.get("xpra"))+ return _running(self.processes.get("xpra")) or _pid_is_running(self.process_ids.get("xpra", 0))def public(self, doc: dict[str, Any] | None = None) -> dict[str, Any]:title = str(doc.get("basename") or "") if doc else self.title@@ -134,7 +137,7 @@ class LibreOfficeDesktopManager:"status": status,}- def open(self, doc: dict[str, Any]) -> dict[str, Any]:+ def open(self, doc: dict[str, Any], *, refresh: bool = False) -> dict[str, Any]:ext = str(doc.get("extension") or "").lower()if ext not in OFFICIAL_EXTENSIONS:return {"available": False, "reason": f".{ext} does not use the LibreOffice desktop surface."}@@ -150,13 +153,59 @@ class LibreOfficeDesktopManager:"error": str(exc),"status": status,}- self._open_document_locked(session, doc)+ refreshed = False+ try:+ if refresh and session.file_id == str(doc.get("file_id") or ""):+ refreshed = self._reload_document_locked(session, doc)+ else:+ self._open_document_locked(session, doc)+ except Exception as exc:+ return {+ "available": False,+ "error": str(exc),+ "status": collect_desktop_status(),+ }session.file_id = str(doc["file_id"])session.extension = extsession.path = str(doc["path"])session.title = str(doc["basename"])self._write_manifest(session)- return session.public(doc)+ public = session.public(doc)+ public["refreshed"] = refreshed+ return public++ def refresh_document(self, file_id: str) -> dict[str, Any]:+ normalized = str(file_id or "").strip()+ if not normalized:+ return {"ok": True, "refreshed": False}+ try:+ doc = document_store.get_document(normalized)+ except Exception:+ return {"ok": False, "refreshed": False, "error": "Document not found."}++ ext = str(doc.get("extension") or "").lower()+ if ext not in OFFICIAL_EXTENSIONS:+ return {"ok": True, "refreshed": False}++ with self._lock:+ self._reap_dead_locked()+ session = self._find_by_file_id_locked(normalized)+ if not session:+ existing = self._load_system_desktop_from_manifest_locked()+ if existing:+ self._sessions[existing.session_id] = existing+ self._register_virtual_desktop(existing)+ if existing.file_id == normalized:+ session = existing+ if not session:+ return {"ok": True, "refreshed": False}+ refreshed = self._reload_document_locked(session, doc)+ session.file_id = str(doc["file_id"])+ session.extension = ext+ session.path = str(doc["path"])+ session.title = str(doc["basename"])+ self._write_manifest(session)+ return {"ok": True, "refreshed": refreshed, "desktop": session.public(doc)}def save(self, session_id: str, file_id: str = "") -> dict[str, Any]:session = self.require(session_id)@@ -268,7 +317,7 @@ class LibreOfficeDesktopManager:if self._sessions.get(SYSTEM_SESSION_ID) is session:self._sessions.pop(SYSTEM_SESSION_ID, None)virtual_desktop.unregister_session(session.token)- self._terminate_session(session)+ self._terminate_session(session, include_rehydrated=True)self._remove_manifest(session.session_id)_clear_shutdown_request(session)return {@@ -322,7 +371,7 @@ class LibreOfficeDesktopManager:with self._lock:self._sessions.pop(session.session_id, None)virtual_desktop.unregister_session(session.token)- self._terminate_session(session)+ self._terminate_session(session, include_rehydrated=True)self._remove_manifest(session.session_id)return {"ok": True, "closed": 1, "session_id": session.session_id, "save": save_result}@@ -389,8 +438,9 @@ class LibreOfficeDesktopManager:self._sessions.clear()for session in sessions:virtual_desktop.unregister_session(session.token)- self._terminate_session(session)- self._remove_manifest(session.session_id)+ if session.owns_processes:+ self._terminate_session(session)+ self._remove_manifest(session.session_id)def _document_for_save(self, session: DesktopSession, file_id: str = "") -> dict[str, Any] | None:normalized = str(file_id or "").strip()@@ -424,6 +474,14 @@ class LibreOfficeDesktopManager:self._refresh_xfce_desktop(existing)return existing+ existing = self._load_system_desktop_from_manifest_locked()+ if existing:+ self._sessions[existing.session_id] = existing+ self._register_virtual_desktop(existing)+ self._prepare_desktop_url_bridge(existing)+ self._refresh_xfce_desktop(existing)+ return existing+status = collect_desktop_status()if not status["healthy"]:raise RuntimeError(status["message"])@@ -454,6 +512,54 @@ class LibreOfficeDesktopManager:self._write_manifest(session)return session+ def _load_system_desktop_from_manifest_locked(self) -> DesktopSession | None:+ manifest = SESSION_DIR / f"{SYSTEM_SESSION_ID}.json"+ if not manifest.exists():+ return None+ try:+ payload = json.loads(manifest.read_text(encoding="utf-8"))+ display = int(payload.get("display") or 0)+ xpra_port = int(payload.get("xpra_port") or 0)+ process_ids = {+ str(name): pid+ for name, value in dict(payload.get("pids") or {}).items()+ if (pid := _coerce_pid(value))+ }+ if not display or not xpra_port:+ return None+ if not _pid_is_running(process_ids.get("xpra", 0)):+ return None+ if not _port_is_accepting("127.0.0.1", xpra_port):+ return None++ path = str(payload.get("path") or document_store.document_binary_home())+ file_id = str(payload.get("file_id") or SYSTEM_FILE_ID)+ extension = str(payload.get("extension") or "").lower()+ if not extension:+ extension = "desktop" if file_id == SYSTEM_FILE_ID else Path(path).suffix.lower().lstrip(".")+ title = str(payload.get("title") or "")+ if not title:+ title = SYSTEM_TITLE if file_id == SYSTEM_FILE_ID else Path(path).name or SYSTEM_TITLE+ return DesktopSession(+ session_id=SYSTEM_SESSION_ID,+ file_id=file_id,+ extension=extension,+ path=path,+ title=title,+ display=display,+ xpra_port=xpra_port,+ token=SYSTEM_SESSION_ID,+ url=_xpra_url(SYSTEM_SESSION_ID),+ profile_dir=Path(payload.get("profile_dir") or PROFILE_DIR / SYSTEM_SESSION_ID),+ width=int(payload.get("width") or DEFAULT_SCREEN_WIDTH),+ height=int(payload.get("height") or DEFAULT_SCREEN_HEIGHT),+ process_ids=process_ids,+ owns_processes=False,+ started_at=float(payload.get("started_at") or time.time()),+ )+ except Exception:+ return None+def _spawn_desktop_locked(self, session: DesktopSession) -> None:STATE_DIR.mkdir(parents=True, exist_ok=True)SESSION_DIR.mkdir(parents=True, exist_ok=True)@@ -540,6 +646,261 @@ class LibreOfficeDesktopManager:env=self._display_env(session),)self._fit_office_window(session, process=session.processes[process_key])+ window_id = self._wait_for_office_window_locked(+ session,+ title=str(doc.get("basename") or ""),+ process=session.processes[process_key],+ )+ if not window_id:+ raise RuntimeError(+ f"LibreOffice did not show {doc.get('basename') or 'the document'} "+ f"on desktop :{session.display}.",+ )+ self._fit_office_window_id_locked(+ session,+ window_id,+ env=self._display_env(session),+ keys=("Escape",),+ )++ def _reload_document_locked(self, session: DesktopSession, doc: dict[str, Any]) -> bool:+ if self._close_document_window_locked(session, doc):+ self._open_document_locked(session, doc)+ return True+ if self._send_reload_shortcut_locked(session, doc):+ return True+ self._open_document_locked(session, doc)+ return False++ def _close_document_window_locked(self, session: DesktopSession, doc: dict[str, Any]) -> bool:+ xdotool = shutil.which("xdotool")+ if not xdotool:+ return False+ title = str(doc.get("basename") or Path(str(doc.get("path") or "")).name or "").strip()+ if not title:+ return False+ window_id = self._office_window_id_locked(session, title=title, fallback=False)+ if not window_id:+ return False+ env = self._display_env(session)+ self._fit_office_window_id_locked(session, window_id, env=env, keys=("Escape",))+ for command in (+ [xdotool, "key", "--clearmodifiers", "alt+F4"],+ [xdotool, "windowclose", window_id],+ ):+ try:+ subprocess.run(+ command,+ check=False,+ stdout=subprocess.DEVNULL,+ stderr=subprocess.DEVNULL,+ timeout=3,+ env=env,+ )+ except (OSError, subprocess.TimeoutExpired):+ continue+ if self._wait_for_window_closed_locked(session, window_id):+ return True+ self._dismiss_blocking_dialogs(session)+ return self._wait_for_window_closed_locked(session, window_id, timeout_seconds=1.5)++ def _send_reload_shortcut_locked(self, session: DesktopSession, doc: dict[str, Any]) -> bool:+ xdotool = shutil.which("xdotool")+ if not xdotool:+ return False+ window_id = self._office_window_id_locked(+ session,+ title=str(doc.get("basename") or ""),+ )+ if not window_id:+ return False+ env = self._display_env(session)+ self._fit_office_window_id_locked(session, window_id, env=env, keys=("Escape",))+ try:+ result = subprocess.run(+ [xdotool, "key", "--clearmodifiers", "ctrl+shift+r"],+ check=False,+ capture_output=True,+ text=True,+ timeout=4,+ env=env,+ )+ except (OSError, subprocess.TimeoutExpired):+ return False+ time.sleep(0.8)+ self._dismiss_blocking_dialogs(session)+ self._fit_office_window_id_locked(session, window_id, env=env, keys=("Escape",))+ return result.returncode == 0++ def _office_window_id_locked(+ self,+ session: DesktopSession,+ *,+ title: str = "",+ fallback: bool = True,+ ) -> str:+ xdotool = shutil.which("xdotool")+ if not xdotool:+ return ""+ env = self._display_env(session)+ title = str(title or "").strip()+ searches: list[list[str]] = []+ if title:+ escaped_title = re.escape(title)+ searches.append([+ xdotool,+ "search",+ "--onlyvisible",+ "--name",+ escaped_title,+ ])+ for window_class in (+ "libreoffice",+ "libreoffice-writer",+ "libreoffice-calc",+ "libreoffice-impress",+ ):+ searches.append([+ xdotool,+ "search",+ "--onlyvisible",+ "--class",+ window_class,+ "--name",+ escaped_title,+ ])+ if fallback:+ for window_class in (+ "libreoffice",+ "libreoffice-writer",+ "libreoffice-calc",+ "libreoffice-impress",+ ):+ searches.append([xdotool, "search", "--onlyvisible", "--class", window_class])+ searches.append([xdotool, "search", "--onlyvisible", "--name", "LibreOffice"])+ for command in searches:+ try:+ result = subprocess.run(+ command,+ check=False,+ capture_output=True,+ text=True,+ timeout=2,+ env=env,+ )+ except (OSError, subprocess.TimeoutExpired):+ continue+ window_ids = [+ line.strip()+ for line in result.stdout.splitlines()+ if line.strip()+ ]+ if window_ids:+ return window_ids[-1]+ return ""++ def _wait_for_office_window_locked(+ self,+ session: DesktopSession,+ *,+ title: str = "",+ process: subprocess.Popen[Any] | None = None,+ timeout_seconds: float = 20.0,+ ) -> str:+ deadline = time.time() + timeout_seconds+ last_fallback = ""+ title = str(title or "").strip()+ while time.time() < deadline:+ window_id = self._office_window_id_locked(session, title=title, fallback=False)+ if window_id:+ return window_id+ last_fallback = self._office_window_id_locked(session, fallback=True) or last_fallback+ if last_fallback and not title:+ return last_fallback+ if process and process.poll() is not None and last_fallback:+ return last_fallback+ time.sleep(0.25)+ return self._office_window_id_locked(session, title=title, fallback=True) or last_fallback++ def _wait_for_window_closed_locked(+ self,+ session: DesktopSession,+ window_id: str,+ *,+ timeout_seconds: float = 6.0,+ ) -> bool:+ deadline = time.time() + timeout_seconds+ while time.time() < deadline:+ if not self._window_exists_locked(session, window_id):+ return True+ time.sleep(0.2)+ return not self._window_exists_locked(session, window_id)++ def _window_exists_locked(self, session: DesktopSession, window_id: str) -> bool:+ xdotool = shutil.which("xdotool")+ if not xdotool or not window_id:+ return False+ try:+ result = subprocess.run(+ [xdotool, "getwindowname", str(window_id)],+ check=False,+ stdout=subprocess.DEVNULL,+ stderr=subprocess.DEVNULL,+ timeout=2,+ env=self._display_env(session),+ )+ except (OSError, subprocess.TimeoutExpired):+ return False+ return result.returncode == 0++ def _fit_office_window_id_locked(+ self,+ session: DesktopSession,+ window_id: str,+ *,+ env: dict[str, str],+ keys: tuple[str, ...] = (),+ ) -> None:+ xdotool = shutil.which("xdotool")+ if not xdotool or not window_id:+ return+ for command in (+ [xdotool, "windowactivate", window_id],+ [+ xdotool,+ "windowmove",+ window_id,+ "0",+ "0",+ "windowsize",+ window_id,+ str(session.width),+ str(session.height),+ ],+ ):+ try:+ subprocess.run(+ command,+ check=False,+ stdout=subprocess.DEVNULL,+ stderr=subprocess.DEVNULL,+ timeout=2,+ env=env,+ )+ except (OSError, subprocess.TimeoutExpired):+ continue+ for key in keys:+ try:+ subprocess.run(+ [xdotool, "key", "--clearmodifiers", key],+ check=False,+ stdout=subprocess.DEVNULL,+ stderr=subprocess.DEVNULL,+ timeout=2,+ env=env,+ )+ except (OSError, subprocess.TimeoutExpired):+ continuedef _prepare_profile(self, session: DesktopSession) -> None:user_dir = session.profile_dir / "user"@@ -1137,29 +1498,45 @@ fidef _write_manifest(self, session: DesktopSession) -> None:SESSION_DIR.mkdir(parents=True, exist_ok=True)+ pids = dict(session.process_ids)+ pids.update({name: process.pid for name, process in session.processes.items()})payload = {"session_id": session.session_id,"file_id": session.file_id,+ "extension": session.extension,"path": session.path,+ "title": session.title,"display": session.display,"xpra_port": session.xpra_port,"profile_dir": str(session.profile_dir),+ "width": session.width,+ "height": session.height,+ "started_at": session.started_at,"owner_pid": os.getpid(),- "pids": {name: process.pid for name, process in session.processes.items()},+ "pids": pids,}(SESSION_DIR / f"{session.session_id}.json").write_text(json.dumps(payload), encoding="utf-8")def _remove_manifest(self, session_id: str) -> None:(SESSION_DIR / f"{session_id}.json").unlink(missing_ok=True)- def _terminate_session(self, session: DesktopSession) -> None:+ def _terminate_session(self, session: DesktopSession, *, include_rehydrated: bool = False) -> None:process_names = [name for name in session.processes if name.startswith("soffice")]process_names.extend(["xfce", "xpra", "xvfb"])+ terminated_pids: set[int] = set()for name in process_names:process = session.processes.get(name)if not process:continue+ if process.pid:+ terminated_pids.add(process.pid)_terminate_process(process)+ if session.owns_processes or include_rehydrated:+ for name, pid in session.process_ids.items():+ if pid in terminated_pids:+ continue+ if name.startswith("soffice") or name in {"xfce", "xpra", "xvfb"}:+ _kill_pid(pid)self._remove_stale_lock_file(session)def _remove_stale_lock_file(self, session: DesktopSession, *, path: str | Path | None = None) -> None:@@ -1924,6 +2301,14 @@ def _port_is_free(port: int) -> bool:return False+def _port_is_accepting(host: str, port: int) -> bool:+ try:+ with socket.create_connection((host, port), timeout=0.2):+ return True+ except OSError:+ return False++def _terminate_process(process: subprocess.Popen[Any]) -> None:if process.poll() is not None:return@@ -1961,6 +2346,8 @@ def _coerce_pid(value: Any) -> int:def _pid_is_running(pid: int) -> bool:+ if pid <= 0:+ return Falsetry:os.kill(pid, 0)return Truediff --git a/plugins/_office/webui/office-store.js b/plugins/_office/webui/office-store.jsindex a570e0565..1beb1777f 100644--- a/plugins/_office/webui/office-store.js+++ b/plugins/_office/webui/office-store.js@@ -268,6 +268,8 @@ const model = {await this.openSession({path: payload.path || "",file_id: payload.file_id || "",+ refresh: payload.refresh === true,+ source: payload.source || "",});} else if (this._desktopIntentionalShutdown) {this.session = null;@@ -2261,15 +2263,18 @@ const model = {},tabTitle(tab = {}) {+ tab = tab || {};return tab.title || tab.document?.basename || basename(tab.path);},tabLabel(tab = {}) {+ tab = tab || {};const title = this.tabTitle(tab);return tab.dirty ? `${title} unsaved` : title;},tabIcon(tab = {}) {+ tab = tab || {};const ext = String(tab.extension || tab.document?.extension || "").toLowerCase();if (this.isDesktopSession(tab)) return "desktop_windows";if (ext === "md") return "article";