Agent Zero AI framework
Harden Office canvas sync and PPTX output
Alessandro3466160e4ce5
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 af8ce63ee..44dbda416 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,3 +1,122 @@-export default async function autoOpenDocumentResults(_context) {- return;+const SYNC_WINDOW_MS = 10 * 60 * 1000;+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);+ if (getToolName(payload) !== "document_artifact") continue;+ if (!shouldSyncOpenOfficeCanvas(args, payload)) continue;++ const document = payload.document && typeof payload.document === "object" ? payload.document : {};+ const path = payload.path || document.path || "";+ const fileId = payload.file_id || document.file_id || "";+ if (!path && !fileId) continue;++ const key = [+ args?.id || "",+ payload.action || "",+ fileId || "",+ path || "",+ payload.version || document.version || "",+ ].join(":");+ if (syncedDocumentResults.has(key)) continue;+ syncedDocumentResults.add(key);++ globalThis.setTimeout(async () => {+ if (!isOfficeCanvasAlreadyOpen()) return;+ const office = globalThis.Alpine?.store?.("office");+ if (!office || isDirtySameDocument(office, { path, file_id: fileId })) return;+ await office.openSession?.({+ path,+ file_id: fileId,+ source: "tool-result-sync",+ });+ }, 0);+ }+}++function getDocumentPayload(args = {}) {+ const contentPayload = parseMaybeJson(args.content);+ const kvpsPayload = args.kvps && typeof args.kvps === "object"+ ? args.kvps+ : parseMaybeJson(args.kvps);+ return {+ ...pickPayloadFields(args),+ ...(contentPayload || {}),+ ...(kvpsPayload || {}),+ };+}++function pickPayloadFields(args = {}) {+ const payload = {};+ for (const key of [+ "_tool_name",+ "tool_name",+ "action",+ "file_id",+ "path",+ "version",+ "last_modified",+ ]) {+ if (args[key] != null && args[key] !== "") payload[key] = args[key];+ }+ return payload;+}++function getToolName(payload = {}) {+ return String(payload._tool_name || payload.tool_name || "").trim();+}++function shouldSyncOpenOfficeCanvas(args = {}, payload = {}) {+ if (!isFresh(args.timestamp, payload.last_modified || payload.document?.last_modified)) return false;+ const action = String(payload.action || "").trim().toLowerCase().replace("-", "_");+ return ["create", "open", "edit", "restore_version"].includes(action);+}++function isOfficeCanvasAlreadyOpen() {+ const canvas = globalThis.Alpine?.store?.("rightCanvas");+ return Boolean(canvas?.isOpen && canvas?.activeSurfaceId === "office");+}++function isDirtySameDocument(office, document = {}) {+ if (!office?.dirty || !office?.session) return false;+ const path = String(document.path || "");+ const fileId = String(document.file_id || "");+ return Boolean(+ (fileId && office.session.file_id === fileId)+ || (path && office.session.path === path),+ );+}++function isFresh(...timestamps) {+ const now = Date.now();+ for (const value of timestamps) {+ const time = parseTimestamp(value);+ if (time && now - time < SYNC_WINDOW_MS) return true;+ }+ return false;+}++function parseTimestamp(value) {+ if (!value) return 0;+ if (typeof value === "number") return value > 1e12 ? value : value * 1000;+ const parsed = Date.parse(String(value));+ return Number.isFinite(parsed) ? parsed : 0;+}++function parseMaybeJson(value) {+ if (!value) return null;+ if (typeof value === "object") return value;+ if (typeof value !== "string") return null;+ const trimmed = value.trim();+ if (!trimmed.startsWith("{")) return null;+ try {+ const parsed = JSON.parse(trimmed);+ return parsed && typeof parsed === "object" ? parsed : null;+ } catch {+ return null;+ }}diff --git a/plugins/_office/helpers/artifact_editor.py b/plugins/_office/helpers/artifact_editor.pyindex 81f29c20c..5c5fad456 100644--- a/plugins/_office/helpers/artifact_editor.py+++ b/plugins/_office/helpers/artifact_editor.py@@ -10,7 +10,7 @@ from typing import Anyfrom xml.sax.saxutils import escapeimport xml.etree.ElementTree as ET-from plugins._office.helpers import document_store+from plugins._office.helpers import document_store, pptx_writerW_NS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"@@ -1205,25 +1205,7 @@ def _slide_from_mapping(value: dict[str, Any]) -> dict[str, Any]:def _pptx_from_slides(slides: list[dict[str, Any]]) -> bytes:- if not slides:- slides = [{"title": "Presentation", "bullets": []}]-- files: dict[str, str | bytes] = {- "[Content_Types].xml": _pptx_content_types(len(slides)),- "_rels/.rels": (- '<?xml version="1.0" encoding="UTF-8"?>'- f'<Relationships xmlns="{REL_NS}">'- '<Relationship Id="rId1" '- 'Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" '- 'Target="ppt/presentation.xml"/>'- "</Relationships>"- ),- "ppt/_rels/presentation.xml.rels": _pptx_presentation_rels(len(slides)),- "ppt/presentation.xml": _pptx_presentation_xml(len(slides)),- }- for index, slide in enumerate(slides, start=1):- files[f"ppt/slides/slide{index}.xml"] = _pptx_slide_xml(slide)- return _zip_map(files)+ return pptx_writer.pptx_from_slides(slides)def _pptx_content_types(count: int) -> str:diff --git a/plugins/_office/helpers/document_store.py b/plugins/_office/helpers/document_store.pyindex f85d54b07..761e7c767 100644--- a/plugins/_office/helpers/document_store.py+++ b/plugins/_office/helpers/document_store.py@@ -17,6 +17,7 @@ from typing import Anyfrom xml.sax.saxutils import escapefrom helpers import files+from plugins._office.helpers import pptx_writerPLUGIN_NAME = "_office"@@ -829,11 +830,4 @@ def _column_name(index: int) -> str:def _pptx(title: str, content: str) -> bytes:- subtitle = content.splitlines()[0] if content.splitlines() else ""- return _zip_bytes({- "[Content_Types].xml": """<?xml version="1.0" encoding="UTF-8"?><Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/ppt/presentation.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml"/><Override PartName="/ppt/slides/slide1.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.slide+xml"/></Types>""",- "_rels/.rels": """<?xml version="1.0" encoding="UTF-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="ppt/presentation.xml"/></Relationships>""",- "ppt/_rels/presentation.xml.rels": """<?xml version="1.0" encoding="UTF-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="slides/slide1.xml"/></Relationships>""",- "ppt/presentation.xml": """<?xml version="1.0" encoding="UTF-8"?><p:presentation xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><p:sldIdLst><p:sldId id="256" r:id="rId1"/></p:sldIdLst><p:sldSz cx="9144000" cy="5143500"/></p:presentation>""",- "ppt/slides/slide1.xml": f"""<?xml version="1.0" encoding="UTF-8"?><p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main"><p:cSld><p:spTree><p:nvGrpSpPr><p:cNvPr id="1" name=""/><p:cNvGrpSpPr/><p:nvPr/></p:nvGrpSpPr><p:grpSpPr/><p:sp><p:nvSpPr><p:cNvPr id="2" name="Title"/><p:cNvSpPr/><p:nvPr/></p:nvSpPr><p:txBody><a:bodyPr/><a:lstStyle/><a:p><a:r><a:t>{escape(title)}</a:t></a:r></a:p><a:p><a:r><a:t>{escape(subtitle)}</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>""",- })+ return pptx_writer.pptx_from_text(title, content)diff --git a/plugins/_office/helpers/pptx_writer.py b/plugins/_office/helpers/pptx_writer.pynew file mode 100644index 000000000..dab98111d--- /dev/null+++ b/plugins/_office/helpers/pptx_writer.py@@ -0,0 +1,225 @@+from __future__ import annotations++import io+import json+import re+import zipfile+from typing import Any+from xml.sax.saxutils import escape+++A_NS = "http://schemas.openxmlformats.org/drawingml/2006/main"+P_NS = "http://schemas.openxmlformats.org/presentationml/2006/main"+R_NS = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"+CT_NS = "http://schemas.openxmlformats.org/package/2006/content-types"+REL_NS = "http://schemas.openxmlformats.org/package/2006/relationships"+++def pptx_from_text(title: str, content: str) -> bytes:+ return pptx_from_slides(slides_from_text(title, content))+++def slides_from_text(title: str, content: str) -> list[dict[str, Any]]:+ normalized = normalize_slides(content)+ document_title = str(title or "Presentation").strip() or "Presentation"+ if not normalized:+ return [{"title": document_title, "bullets": []}]++ text = str(content or "")+ if len(normalized) == 1 and "---" not in text:+ slide = normalized[0]+ slide_title = str(slide.get("title") or "").strip()+ if slide_title and slide_title.casefold() != document_title.casefold():+ return [{"title": document_title, "bullets": [slide_title, *slide.get("bullets", [])]}]+ return normalized+++def normalize_slides(value: Any) -> list[dict[str, Any]]:+ if value is None:+ return []+ if isinstance(value, str):+ stripped = value.strip()+ if not stripped:+ return []+ if stripped.startswith("[") or stripped.startswith("{"):+ return normalize_slides(json.loads(stripped))+ chunks = re.split(r"(?m)^\s*---+\s*$", stripped)+ result = []+ for chunk in chunks:+ lines = [_clean_slide_line(line) for line in chunk.splitlines() if line.strip()]+ lines = [line for line in lines if line]+ if lines:+ result.append({"title": lines[0], "bullets": lines[1:]})+ return result+ if isinstance(value, dict):+ return [_slide_from_mapping(value)]+ if isinstance(value, list):+ result = []+ for item in value:+ if isinstance(item, dict):+ result.append(_slide_from_mapping(item))+ elif isinstance(item, str):+ result.extend(normalize_slides(item))+ elif isinstance(item, (list, tuple)):+ lines = [_clean_slide_line(part) for part in item if str(part).strip()]+ if lines:+ result.append({"title": lines[0], "bullets": lines[1:]})+ else:+ result.append({"title": str(item), "bullets": []})+ return result+ return [{"title": str(value), "bullets": []}]+++def pptx_from_slides(slides: list[dict[str, Any]]) -> bytes:+ normalized = normalize_slides(slides)+ if not normalized:+ normalized = [{"title": "Presentation", "bullets": []}]+ try:+ return _pptx_from_slides_with_python_pptx(normalized)+ except Exception:+ return _pptx_from_slides_ooxml(normalized)+++def _slide_from_mapping(value: dict[str, Any]) -> dict[str, Any]:+ title = _clean_slide_line(value.get("title") or value.get("heading") or "Slide")+ bullets = value.get("bullets")+ if bullets is None:+ body = value.get("body") or value.get("content") or ""+ bullets = [_clean_slide_line(line) for line in str(body).splitlines() if line.strip()]+ elif isinstance(bullets, str):+ bullets = [_clean_slide_line(line) for line in bullets.splitlines() if line.strip()]+ else:+ bullets = [_clean_slide_line(item) for item in bullets]+ return {"title": title or "Slide", "bullets": [bullet for bullet in bullets if bullet]}+++def _clean_slide_line(value: Any) -> str:+ line = str(value or "").strip()+ line = re.sub(r"^\s{0,3}#{1,6}\s+", "", line)+ line = re.sub(r"^\s*(?:[-*•]|\d+[.)])\s+", "", line)+ return line.strip()+++def _pptx_from_slides_with_python_pptx(slides: list[dict[str, Any]]) -> bytes:+ from pptx import Presentation # type: ignore+ from pptx.util import Inches # type: ignore++ presentation = Presentation()+ for slide_spec in slides:+ layout = presentation.slide_layouts[1] if len(presentation.slide_layouts) > 1 else presentation.slide_layouts[0]+ slide = presentation.slides.add_slide(layout)+ title = str(slide_spec.get("title") or "Slide")+ bullets = [str(item) for item in slide_spec.get("bullets") or []]++ if slide.shapes.title:+ slide.shapes.title.text = title+ else:+ title_box = slide.shapes.add_textbox(Inches(0.6), Inches(0.35), Inches(8.8), Inches(0.8))+ title_box.text_frame.text = title++ body_shape = slide.placeholders[1] if len(slide.placeholders) > 1 else None+ if body_shape is None:+ body_shape = slide.shapes.add_textbox(Inches(0.85), Inches(1.45), Inches(8.35), Inches(4.55))++ text_frame = body_shape.text_frame+ text_frame.clear()+ if not bullets:+ text_frame.text = ""+ continue++ for index, bullet in enumerate(bullets):+ paragraph = text_frame.paragraphs[0] if index == 0 else text_frame.add_paragraph()+ paragraph.text = bullet+ paragraph.level = 0++ buffer = io.BytesIO()+ presentation.save(buffer)+ return buffer.getvalue()+++def _pptx_from_slides_ooxml(slides: list[dict[str, Any]]) -> bytes:+ files: dict[str, str | bytes] = {+ "[Content_Types].xml": _pptx_content_types(len(slides)),+ "_rels/.rels": (+ '<?xml version="1.0" encoding="UTF-8"?>'+ f'<Relationships xmlns="{REL_NS}">'+ '<Relationship Id="rId1" '+ 'Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" '+ 'Target="ppt/presentation.xml"/>'+ "</Relationships>"+ ),+ "ppt/_rels/presentation.xml.rels": _pptx_presentation_rels(len(slides)),+ "ppt/presentation.xml": _pptx_presentation_xml(len(slides)),+ }+ for index, slide in enumerate(slides, start=1):+ files[f"ppt/slides/slide{index}.xml"] = _pptx_slide_xml(slide)+ return _zip_map(files)+++def _pptx_content_types(count: int) -> str:+ overrides = [+ '<Override PartName="/ppt/presentation.xml" '+ 'ContentType="application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml"/>'+ ]+ for index in range(1, count + 1):+ overrides.append(+ f'<Override PartName="/ppt/slides/slide{index}.xml" '+ 'ContentType="application/vnd.openxmlformats-officedocument.presentationml.slide+xml"/>'+ )+ return (+ '<?xml version="1.0" encoding="UTF-8"?>'+ f'<Types xmlns="{CT_NS}">'+ '<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>'+ '<Default Extension="xml" ContentType="application/xml"/>'+ + "".join(overrides)+ + "</Types>"+ )+++def _pptx_presentation_rels(count: int) -> str:+ rels = []+ for index in range(1, count + 1):+ rels.append(+ f'<Relationship Id="rId{index}" '+ 'Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" '+ f'Target="slides/slide{index}.xml"/>'+ )+ return '<?xml version="1.0" encoding="UTF-8"?>' + f'<Relationships xmlns="{REL_NS}">' + "".join(rels) + "</Relationships>"+++def _pptx_presentation_xml(count: int) -> str:+ slide_ids = "".join(f'<p:sldId id="{255 + index}" r:id="rId{index}"/>' for index in range(1, count + 1))+ return (+ '<?xml version="1.0" encoding="UTF-8"?>'+ f'<p:presentation xmlns:p="{P_NS}" xmlns:r="{R_NS}">'+ f"<p:sldIdLst>{slide_ids}</p:sldIdLst>"+ '<p:sldSz cx="9144000" cy="5143500"/>'+ "</p:presentation>"+ )+++def _pptx_slide_xml(slide: dict[str, Any]) -> str:+ title = str(slide.get("title") or "Slide")+ bullets = [str(item) for item in slide.get("bullets") or []]+ paragraphs = [title, *bullets]+ text = "".join(f"<a:p><a:r><a:t>{escape(item)}</a:t></a:r></a:p>" for item in paragraphs)+ return (+ '<?xml version="1.0" encoding="UTF-8"?>'+ f'<p:sld xmlns:a="{A_NS}" xmlns:p="{P_NS}">'+ "<p:cSld><p:spTree>"+ '<p:nvGrpSpPr><p:cNvPr id="1" name=""/><p:cNvGrpSpPr/><p:nvPr/></p:nvGrpSpPr>'+ "<p:grpSpPr/>"+ '<p:sp><p:nvSpPr><p:cNvPr id="2" name="Content"/><p:cNvSpPr/><p:nvPr/></p:nvSpPr>'+ f"<p:txBody><a:bodyPr/><a:lstStyle/>{text}</p:txBody>"+ "</p:sp>"+ "</p:spTree></p:cSld>"+ "</p:sld>"+ )+++def _zip_map(files_map: dict[str, str | bytes]) -> bytes:+ buffer = io.BytesIO()+ with zipfile.ZipFile(buffer, "w", compression=zipfile.ZIP_DEFLATED) as archive:+ for name, value in files_map.items():+ archive.writestr(name, value.encode("utf-8") if isinstance(value, str) else value)+ return buffer.getvalue()diff --git a/tests/test_office_canvas_setup.py b/tests/test_office_canvas_setup.pyindex 9acdbe849..e692e366b 100644--- a/tests/test_office_canvas_setup.py+++ b/tests/test_office_canvas_setup.py@@ -321,7 +321,11 @@ def test_right_canvas_requires_explicit_open_and_is_absent_on_mobile():assert "body.right-canvas-mobile-mode .right-canvas" in canvas_cssassert "display: none !important" in canvas_cssassert "autoOpenOfficeCanvas" not in handler- assert "requestAnimationFrame" not in after_loop+ assert "isOfficeCanvasAlreadyOpen" in after_loop+ assert 'canvas?.isOpen && canvas?.activeSurfaceId === "office"' in after_loop+ assert "office.openSession?.(" in after_loop+ assert 'source: "tool-result-sync"' in after_loop+ assert 'rightCanvas.open' not in after_loopdef test_office_skills_preserve_markdown_first_and_opt_in_desktop_policy():diff --git a/tests/test_office_document_store.py b/tests/test_office_document_store.pyindex 0e7784b26..7aea3a49a 100644--- a/tests/test_office_document_store.py+++ b/tests/test_office_document_store.py@@ -112,7 +112,21 @@ def test_xlsx_and_pptx_creation_and_direct_edits_still_work(office_state):assert rows[1][1] == 12500assert rows[2][0] == "Research"- deck = document_store.create_document("presentation", "Roadmap", "pptx", "Initial")+ deck = document_store.create_document(+ "presentation",+ "Roadmap",+ "pptx",+ "Roadmap\nLaunch sequence\n\n---\n\nNext\nPolish rollout",+ )+ created_deck_read = artifact_editor.read_artifact(deck)+ with zipfile.ZipFile(deck["path"]) as archive:+ created_slide_names = [name for name in archive.namelist() if name.startswith("ppt/slides/slide") and name.endswith(".xml")]++ assert created_deck_read["slide_count"] == 2+ assert created_deck_read["slides"][0]["title"] == "Roadmap"+ assert created_deck_read["slides"][1]["title"] == "Next"+ assert len(created_slide_names) == 2+updated_deck, deck_payload = artifact_editor.edit_artifact(deck,operation="set_slides",