| 1 | from __future__ import annotations |
| 2 | |
| 3 | from typing import Any |
| 4 | |
| 5 | from plugins._editor.helpers import markdown_sessions |
| 6 | |
| 7 | |
| 8 | def build_context(context_id: str = "", max_items: int = 20) -> str: |
| 9 | files = markdown_sessions.get_manager().list_open(context_id=context_id, limit=max_items) |
| 10 | if not files: |
| 11 | return "" |
| 12 | |
| 13 | lines = [ |
| 14 | "These Markdown or plain text files are open in the Editor for the active Agent Zero context. Content is omitted; use `text_editor` with action `read` before content-sensitive edits.", |
| 15 | ] |
| 16 | for item in files: |
| 17 | lines.append(format_open_file_line(item)) |
| 18 | lines.append( |
| 19 | "For these paths, `text_editor` is the canonical saved-edit tool. Use `open_in_canvas: true` only when the user asks to open the Editor UI; open Editor sessions refresh automatically after saved text edits." |
| 20 | ) |
| 21 | return "\n".join(lines) |
| 22 | |
| 23 | |
| 24 | def format_open_file_line(item: dict[str, Any]) -> str: |
| 25 | active = "active, " if item.get("active") else "" |
| 26 | dirty = "dirty, " if item.get("dirty") else "saved, " |
| 27 | external = "external changes pending, " if item.get("external_modified") else "" |
| 28 | return ( |
| 29 | f"- {item.get('title', 'Untitled')} " |
| 30 | f"(.{item.get('extension', 'md')}, {active}{dirty}{external}" |
| 31 | f"file_id={item.get('file_id', '')}, path={item.get('path', '')}, " |
| 32 | f"version={item.get('version', '')}, size={item.get('size', 0)} bytes, " |
| 33 | f"last_modified={item.get('last_modified', '')}, open_sessions={item.get('open_sessions', 1)})" |
| 34 | ) |