main
py 56 lines 2.06 KB
Raw
1 from __future__ import annotations
2
3 from typing import Any
4
5 from plugins._desktop.helpers import desktop_state
6 from plugins._office.helpers import document_store
7
8 OFFICE_EXTENSIONS = document_store.OPEN_DOCUMENT_EXTENSIONS | document_store.OOXML_EXTENSIONS
9
10
11 def build_context(max_items: int = 6) -> str:
12 documents = [
13 doc
14 for doc in document_store.get_open_documents(limit=max(max_items * 4, 20))
15 if str(doc.get("extension") or "").lower() in OFFICE_EXTENSIONS
16 ][:max_items]
17 desktop_context = build_desktop_context()
18 if not documents:
19 return desktop_context
20
21 lines = [
22 "These Office artifacts have active document sessions. Content is omitted; use `office_artifact` with action `read` before content-sensitive edits.",
23 ]
24 for doc in documents:
25 lines.append(format_document_line(doc))
26 lines.append(
27 "Use `office_artifact` with action `edit` and file_id or path for saved Office edits; tool results refresh already-open document canvases automatically."
28 )
29 if desktop_context:
30 lines.extend(["", desktop_context])
31 return "\n".join(lines)
32
33
34 def format_document_line(doc: dict[str, Any]) -> str:
35 return (
36 f"- {doc.get('basename', 'Untitled')} "
37 f"(.{doc.get('extension', '')}, file_id={doc.get('file_id', '')}, "
38 f"path={document_store.display_path(doc.get('path', ''))}, version={document_store.item_version(doc)}, "
39 f"size={doc.get('size', 0)} bytes, last_modified={doc.get('last_modified', '')}, "
40 f"open_sessions={doc.get('open_sessions', 1)})"
41 )
42
43
44 def build_desktop_context() -> str:
45 if not desktop_state.session_manifest_exists():
46 return ""
47 try:
48 return desktop_state.compact_prompt_context(
49 desktop_state.collect_state(include_screenshot=False),
50 )
51 except Exception as exc:
52 return (
53 "[DESKTOP STATE]\n"
54 f"- unavailable={exc}\n"
55 "- next=Open the Desktop surface manually, then run plugins/_desktop/skills/linux-desktop/scripts/desktopctl.sh observe --json."
56 )