mirror of
https://github.com/agent0ai/agent-zero.git
synced 2026-12-04 09:28:56 +00:00
615 lines
21 KiB
Python
615 lines
21 KiB
Python
import hashlib
|
|
from pathlib import Path
|
|
import tempfile
|
|
|
|
from helpers import destination_files
|
|
from helpers.tool_policy import ensure_retired_host_tool_allowed
|
|
|
|
from helpers.tool import Tool, Response
|
|
from helpers.extension import call_extensions_async
|
|
from helpers import files, plugins
|
|
from plugins._text_editor.helpers.file_ops import (
|
|
FileInfo,
|
|
read_file,
|
|
write_file,
|
|
validate_edits,
|
|
apply_patch,
|
|
apply_context_patch_file,
|
|
apply_exact_replace_file,
|
|
file_info,
|
|
)
|
|
from plugins._text_editor.helpers.log import create_editor_log
|
|
from plugins._text_editor.helpers.patch_request import parse_patch_request
|
|
from plugins._text_editor.helpers.patch_state import (
|
|
LOCAL_FRESHNESS_KEY,
|
|
apply_patch_post_state,
|
|
check_patch_freshness,
|
|
mark_file_state_stale,
|
|
record_file_state,
|
|
)
|
|
|
|
# Key used in agent.data to store file state for patch validation
|
|
# Value: {path: {"mtime": float, "total_lines": int}}
|
|
_MTIME_KEY = LOCAL_FRESHNESS_KEY
|
|
|
|
|
|
|
|
class TextEditor(Tool):
|
|
|
|
def get_log_object(self):
|
|
return create_editor_log(self, remote=self.args.get("destination") == "host")
|
|
|
|
async def execute(self, **kwargs):
|
|
action = _current_action(self, kwargs)
|
|
destination = str(kwargs.pop("destination", self.args.get("destination", "local")) or "local").strip()
|
|
if destination == "host":
|
|
from plugins._a0_connector.helpers.host_text_editor import HostTextEditor
|
|
|
|
ensure_retired_host_tool_allowed(self.agent, "text_editor_remote", action=action)
|
|
remote = HostTextEditor(self.agent, {**self.args, **kwargs, "action": action})
|
|
return await remote.execute()
|
|
if destination and destination != "local":
|
|
kwargs.pop("action", None)
|
|
return await self._sandbox_edit(destination, action, **kwargs)
|
|
if action == "read":
|
|
return await self._read(**kwargs)
|
|
elif action == "write":
|
|
return await self._write(**kwargs)
|
|
elif action == "patch":
|
|
return await self._patch(**kwargs)
|
|
return Response(
|
|
message=(
|
|
f"unknown action '{action or self.method or ''}'. "
|
|
"Supported actions: read, write, patch."
|
|
),
|
|
break_loop=False,
|
|
)
|
|
|
|
async def _sandbox_edit(self, destination, action, path="", **kwargs):
|
|
limit = 10 * 1024 * 1024
|
|
try:
|
|
if not isinstance(path, str) or not path or action not in {"read", "write", "patch"}:
|
|
raise ValueError("Provide a path and a read, write, or patch action")
|
|
target = await destination_files.resolve(self.agent, destination)
|
|
key = f"{target.target}:{path}"
|
|
revisions = self.agent.data.setdefault("_text_editor_sandbox_revisions", {})
|
|
if action == "write":
|
|
content = kwargs.get("content")
|
|
if content is None:
|
|
content = ""
|
|
if not isinstance(content, str):
|
|
raise ValueError("content must be a string")
|
|
revision = await destination_files.write(
|
|
self.agent, destination, path, content.encode("utf-8"), limit=limit,
|
|
)
|
|
revisions[key] = revision
|
|
return Response(message=f"{path} written in {destination}", break_loop=False)
|
|
|
|
content = await destination_files.read(self.agent, destination, path, limit=limit)
|
|
if b"\x00" in content[:8192]:
|
|
raise ValueError("File appears binary")
|
|
revision = hashlib.sha256(content).hexdigest()
|
|
with tempfile.TemporaryDirectory(prefix="a0-text-editor-") as directory:
|
|
staged = Path(directory, "content")
|
|
staged.write_bytes(content)
|
|
if action == "read":
|
|
cfg = _get_config(self.agent)
|
|
result = read_file(
|
|
str(staged), line_from=int(kwargs.get("line_from", 1)),
|
|
line_to=int(kwargs["line_to"]) if kwargs.get("line_to") is not None else None,
|
|
max_line_tokens=cfg["max_line_tokens"],
|
|
default_line_count=cfg["default_line_count"],
|
|
max_total_read_tokens=cfg["max_total_read_tokens"],
|
|
)
|
|
if result["error"]:
|
|
raise ValueError(result["error"])
|
|
revisions[key] = revision
|
|
visible = {name: result[name] for name in ("content", "warnings")}
|
|
await call_extensions_async("text_editor_read_after", agent=self.agent, data=visible)
|
|
message = self.agent.read_prompt(
|
|
"fw.text_editor.read_ok.md", path=path, total_lines=str(result["total_lines"]), **visible,
|
|
)
|
|
return Response(message=message, break_loop=False)
|
|
|
|
patch, error = parse_patch_request(
|
|
kwargs.get("edits"), kwargs.get("patch_text"), kwargs.get("old_text"), kwargs.get("new_text"),
|
|
)
|
|
if error:
|
|
raise ValueError(error)
|
|
if patch.mode == "replace":
|
|
apply_exact_replace_file(str(staged), patch.old_text, patch.new_text)
|
|
elif patch.mode == "patch_text":
|
|
apply_context_patch_file(str(staged), patch.patch_text)
|
|
else:
|
|
if key not in revisions:
|
|
return Response(self.agent.read_prompt("fw.text_editor.patch_need_read.md", path=path), False)
|
|
if revisions[key] != revision:
|
|
return Response(self.agent.read_prompt("fw.text_editor.patch_stale_read.md", path=path), False)
|
|
edits, error = validate_edits(patch.edits)
|
|
if error:
|
|
raise ValueError(error)
|
|
apply_patch(str(staged), edits)
|
|
new_revision = await destination_files.write(
|
|
self.agent, destination, path, staged.read_bytes(), limit=limit, expected=revision,
|
|
)
|
|
if patch.mode == "edits":
|
|
revisions[key] = new_revision
|
|
else:
|
|
revisions.pop(key, None)
|
|
return Response(message=f"{path} patched in {destination}", break_loop=False)
|
|
except (OSError, ValueError, RuntimeError, TypeError) as exc:
|
|
return self._error(action, path, str(exc))
|
|
|
|
# ------------------------------------------------------------------
|
|
# READ
|
|
# ------------------------------------------------------------------
|
|
async def _read(self, path: str = "", **kwargs) -> Response:
|
|
if not path:
|
|
return self._error("read", path, "path is required")
|
|
path = files.fix_dev_path(str(Path(path).expanduser().absolute()))
|
|
|
|
cfg = _get_config(self.agent)
|
|
line_from = int(kwargs.get("line_from", 1))
|
|
raw_to = kwargs.get("line_to")
|
|
line_to = int(raw_to) if raw_to is not None else None
|
|
|
|
result = read_file(
|
|
path,
|
|
line_from=line_from,
|
|
line_to=line_to,
|
|
max_line_tokens=cfg["max_line_tokens"],
|
|
default_line_count=cfg["default_line_count"],
|
|
max_total_read_tokens=cfg["max_total_read_tokens"],
|
|
)
|
|
|
|
if result["error"]:
|
|
return self._error("read", path, result["error"])
|
|
|
|
info = file_info(path)
|
|
record_file_state(
|
|
self.agent,
|
|
info,
|
|
key=_MTIME_KEY,
|
|
total_lines=result["total_lines"],
|
|
)
|
|
|
|
# Extension point
|
|
ext_data = {
|
|
"content": result["content"],
|
|
"warnings": result["warnings"],
|
|
}
|
|
await call_extensions_async(
|
|
"text_editor_read_after", agent=self.agent, data=ext_data
|
|
)
|
|
|
|
msg = self.agent.read_prompt(
|
|
"fw.text_editor.read_ok.md",
|
|
path=info["expanded"],
|
|
total_lines=str(result["total_lines"]),
|
|
warnings=ext_data["warnings"],
|
|
content=ext_data["content"],
|
|
)
|
|
return Response(message=msg, break_loop=False)
|
|
|
|
# ------------------------------------------------------------------
|
|
# WRITE
|
|
# ------------------------------------------------------------------
|
|
async def _write(
|
|
self, path: str = "", content: str | None = "", **kwargs
|
|
) -> Response:
|
|
if not path:
|
|
return self._error("write", path, "path is required")
|
|
path = files.fix_dev_path(str(Path(path).expanduser().absolute()))
|
|
|
|
# Extension point
|
|
ext_data = {"path": path, "content": content}
|
|
await call_extensions_async(
|
|
"text_editor_write_before", agent=self.agent, data=ext_data
|
|
)
|
|
|
|
result = write_file(ext_data["path"], ext_data["content"])
|
|
|
|
if result["error"]:
|
|
return self._error("write", path, result["error"])
|
|
|
|
# Extension point
|
|
await call_extensions_async(
|
|
"text_editor_write_after", agent=self.agent,
|
|
data={"path": path, "total_lines": result["total_lines"]},
|
|
)
|
|
|
|
info = file_info(path)
|
|
record_file_state(
|
|
self.agent,
|
|
info,
|
|
key=_MTIME_KEY,
|
|
total_lines=result["total_lines"],
|
|
)
|
|
|
|
cfg = _get_config(self.agent)
|
|
read_result = read_file(
|
|
info["expanded"],
|
|
line_from=1,
|
|
line_to=result["total_lines"],
|
|
max_line_tokens=cfg["max_line_tokens"],
|
|
max_total_read_tokens=cfg["max_total_read_tokens"],
|
|
)
|
|
|
|
msg = self.agent.read_prompt(
|
|
"fw.text_editor.write_ok.md",
|
|
path=info["expanded"],
|
|
total_lines=str(result["total_lines"]),
|
|
content=read_result["content"],
|
|
)
|
|
return Response(
|
|
message=msg,
|
|
break_loop=False,
|
|
additional=_result_additional("write", info, kwargs, agent=self.agent),
|
|
)
|
|
|
|
# ------------------------------------------------------------------
|
|
# PATCH
|
|
# ------------------------------------------------------------------
|
|
async def _patch(
|
|
self, path: str = "", edits=None, patch_text=None, old_text=None, new_text=None, **kwargs
|
|
) -> Response:
|
|
if not path:
|
|
return self._error("patch", path, "path is required")
|
|
path = files.fix_dev_path(str(Path(path).expanduser().absolute()))
|
|
patch_request, err = parse_patch_request(
|
|
edits,
|
|
patch_text,
|
|
old_text,
|
|
new_text,
|
|
missing_error="",
|
|
)
|
|
if err:
|
|
return self._error("patch", path, err)
|
|
|
|
info = file_info(path)
|
|
if not info["is_file"]:
|
|
return self._error("patch", path, "file not found")
|
|
|
|
expanded = info["expanded"]
|
|
|
|
if patch_request and patch_request.mode == "patch_text":
|
|
return await self._patch_context(
|
|
path, expanded, patch_request.patch_text, kwargs
|
|
)
|
|
if patch_request and patch_request.mode == "replace":
|
|
return await self._patch_replace(
|
|
path, expanded, patch_request.old_text, patch_request.new_text, kwargs
|
|
)
|
|
|
|
return await self._patch_edits(
|
|
path,
|
|
expanded,
|
|
info,
|
|
patch_request.edits if patch_request else edits,
|
|
kwargs,
|
|
)
|
|
|
|
async def _patch_edits(
|
|
self, path: str, expanded: str, info: FileInfo, edits, options: dict | None = None
|
|
) -> Response:
|
|
freshness_code = check_patch_freshness(self.agent, info, key=_MTIME_KEY)
|
|
if freshness_code:
|
|
return self._error(
|
|
"patch",
|
|
path,
|
|
_freshness_error_message(self.agent, info, freshness_code),
|
|
)
|
|
|
|
parsed, err = validate_edits(edits)
|
|
if err:
|
|
return self._error("patch", path, err)
|
|
|
|
# Extension point
|
|
ext_data = {"path": expanded, "edits": parsed}
|
|
await call_extensions_async(
|
|
"text_editor_patch_before", agent=self.agent, data=ext_data
|
|
)
|
|
|
|
try:
|
|
total_lines = apply_patch(ext_data["path"], ext_data["edits"])
|
|
except Exception as exc:
|
|
return self._error("patch", path, str(exc))
|
|
|
|
# Extension point
|
|
await call_extensions_async(
|
|
"text_editor_patch_after", agent=self.agent,
|
|
data={"path": expanded, "total_lines": total_lines},
|
|
)
|
|
|
|
# Refresh file info after patch for updated mtime
|
|
post_info = file_info(expanded)
|
|
apply_patch_post_state(
|
|
self.agent,
|
|
post_info,
|
|
ext_data["edits"],
|
|
key=_MTIME_KEY,
|
|
total_lines=total_lines,
|
|
)
|
|
|
|
patch_content = await _read_patch_region(
|
|
expanded, ext_data["edits"], total_lines, _get_config(self.agent)
|
|
)
|
|
|
|
msg = self.agent.read_prompt(
|
|
"fw.text_editor.patch_ok.md",
|
|
path=expanded,
|
|
edit_count=str(len(edits or [])),
|
|
total_lines=str(total_lines),
|
|
content=patch_content,
|
|
)
|
|
return Response(
|
|
message=msg,
|
|
break_loop=False,
|
|
additional=_result_additional("patch", post_info, options, agent=self.agent),
|
|
)
|
|
|
|
async def _patch_replace(
|
|
self, path: str, expanded: str, old_text: str, new_text: str, options: dict | None = None
|
|
) -> Response:
|
|
# Extension point
|
|
ext_data = {
|
|
"path": expanded,
|
|
"old_text": old_text,
|
|
"new_text": new_text,
|
|
"edits": [],
|
|
"mode": "replace",
|
|
}
|
|
await call_extensions_async(
|
|
"text_editor_patch_before", agent=self.agent, data=ext_data
|
|
)
|
|
|
|
try:
|
|
result = apply_exact_replace_file(
|
|
ext_data["path"],
|
|
ext_data["old_text"],
|
|
ext_data["new_text"],
|
|
)
|
|
except Exception as exc:
|
|
return self._error("patch", path, str(exc))
|
|
|
|
total_lines = result["total_lines"]
|
|
|
|
await call_extensions_async(
|
|
"text_editor_patch_after", agent=self.agent,
|
|
data={
|
|
"path": ext_data["path"],
|
|
"total_lines": total_lines,
|
|
"replacement_count": result["replacement_count"],
|
|
"mode": "replace",
|
|
},
|
|
)
|
|
|
|
post_info = file_info(ext_data["path"])
|
|
mark_file_state_stale(self.agent, post_info, key=_MTIME_KEY)
|
|
|
|
patch_content = await _read_exact_replace_region(
|
|
ext_data["path"], result, _get_config(self.agent)
|
|
)
|
|
|
|
msg = self.agent.read_prompt(
|
|
"fw.text_editor.patch_ok.md",
|
|
path=ext_data["path"],
|
|
edit_count=str(result["replacement_count"]),
|
|
total_lines=str(total_lines),
|
|
content=patch_content,
|
|
)
|
|
return Response(
|
|
message=msg,
|
|
break_loop=False,
|
|
additional=_result_additional("patch", post_info, options, agent=self.agent),
|
|
)
|
|
|
|
async def _patch_context(
|
|
self, path: str, expanded: str, patch_text, options: dict | None = None
|
|
) -> Response:
|
|
patch_text = str(patch_text)
|
|
if not patch_text.strip():
|
|
return self._error("patch", path, "patch_text must not be empty")
|
|
|
|
# Extension point
|
|
ext_data = {
|
|
"path": expanded,
|
|
"patch_text": patch_text,
|
|
"edits": [],
|
|
"mode": "patch_text",
|
|
}
|
|
await call_extensions_async(
|
|
"text_editor_patch_before", agent=self.agent, data=ext_data
|
|
)
|
|
|
|
try:
|
|
result = apply_context_patch_file(
|
|
ext_data["path"],
|
|
ext_data["patch_text"],
|
|
)
|
|
except Exception as exc:
|
|
return self._error("patch", path, str(exc))
|
|
|
|
total_lines = result["total_lines"]
|
|
|
|
# Extension point
|
|
await call_extensions_async(
|
|
"text_editor_patch_after", agent=self.agent,
|
|
data={
|
|
"path": ext_data["path"],
|
|
"total_lines": total_lines,
|
|
"hunk_count": result["hunk_count"],
|
|
"mode": "patch_text",
|
|
},
|
|
)
|
|
|
|
post_info = file_info(ext_data["path"])
|
|
mark_file_state_stale(self.agent, post_info, key=_MTIME_KEY)
|
|
|
|
patch_content = await _read_context_patch_region(
|
|
ext_data["path"], result, _get_config(self.agent)
|
|
)
|
|
|
|
msg = self.agent.read_prompt(
|
|
"fw.text_editor.patch_ok.md",
|
|
path=ext_data["path"],
|
|
edit_count=str(result["hunk_count"]),
|
|
total_lines=str(total_lines),
|
|
content=patch_content,
|
|
)
|
|
return Response(
|
|
message=msg,
|
|
break_loop=False,
|
|
additional=_result_additional("patch", post_info, options, agent=self.agent),
|
|
)
|
|
|
|
# ------------------------------------------------------------------
|
|
# Shared error helper
|
|
# ------------------------------------------------------------------
|
|
def _error(self, action: str, path: str, error: str) -> Response:
|
|
msg = self.agent.read_prompt(
|
|
f"fw.text_editor.{action}_error.md", path=path, error=error
|
|
)
|
|
return Response(message=msg, break_loop=False)
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
# Standalone helpers
|
|
# ------------------------------------------------------------------
|
|
|
|
async def _read_patch_region(
|
|
path: str, edits: list[dict], total_lines: int, cfg: dict
|
|
) -> str:
|
|
if not edits:
|
|
return ""
|
|
|
|
min_from = min(e["from"] for e in edits)
|
|
added = sum(
|
|
e["content"].count("\n")
|
|
+ (1 if e["content"] and not e["content"].endswith("\n") else 0)
|
|
for e in edits if e.get("content")
|
|
)
|
|
removed = sum(
|
|
max(e["to"] - e["from"] + 1, 0)
|
|
for e in edits if not e.get("insert")
|
|
)
|
|
max_to = max(e["to"] for e in edits)
|
|
end_line = max_to + added - removed + 3
|
|
|
|
result = read_file(
|
|
path,
|
|
line_from=max(min_from - 1, 1),
|
|
line_to=min(end_line, total_lines),
|
|
max_line_tokens=cfg["max_line_tokens"],
|
|
max_total_read_tokens=cfg["max_total_read_tokens"],
|
|
)
|
|
return result["content"]
|
|
|
|
|
|
async def _read_context_patch_region(
|
|
path: str, result: dict, cfg: dict
|
|
) -> str:
|
|
total_lines = int(result["total_lines"])
|
|
if total_lines <= 0:
|
|
return ""
|
|
|
|
line_from = min(max(int(result["line_from"]), 1), total_lines)
|
|
line_to = min(max(int(result["line_to"]), line_from) + 3, total_lines)
|
|
|
|
read_result = read_file(
|
|
path,
|
|
line_from=max(line_from - 1, 1),
|
|
line_to=line_to,
|
|
max_line_tokens=cfg["max_line_tokens"],
|
|
max_total_read_tokens=cfg["max_total_read_tokens"],
|
|
)
|
|
return read_result["content"]
|
|
|
|
|
|
async def _read_exact_replace_region(
|
|
path: str, result: dict, cfg: dict
|
|
) -> str:
|
|
total_lines = int(result["total_lines"])
|
|
if total_lines <= 0:
|
|
return ""
|
|
|
|
line_from = min(max(int(result["line_from"]), 1), total_lines)
|
|
line_to = min(max(int(result["line_to"]), line_from) + 3, total_lines)
|
|
|
|
read_result = read_file(
|
|
path,
|
|
line_from=max(line_from - 1, 1),
|
|
line_to=line_to,
|
|
max_line_tokens=cfg["max_line_tokens"],
|
|
max_total_read_tokens=cfg["max_total_read_tokens"],
|
|
)
|
|
return read_result["content"]
|
|
|
|
|
|
def _freshness_error_message(agent, info: FileInfo, code: str) -> str:
|
|
prompt = (
|
|
"fw.text_editor.patch_stale_read.md"
|
|
if code == "patch_stale_read"
|
|
else "fw.text_editor.patch_need_read.md"
|
|
)
|
|
return agent.read_prompt(prompt, path=info["expanded"])
|
|
|
|
|
|
def _result_additional(action: str, info: FileInfo, options: dict | None = None, agent=None) -> dict:
|
|
path = str(info.get("expanded") or "")
|
|
extension = Path(path).suffix.lower().lstrip(".")
|
|
options = options or {}
|
|
open_in_canvas = _truthy(
|
|
options.get("open_in_canvas")
|
|
or options.get("open_canvas")
|
|
or options.get("open_document")
|
|
)
|
|
additional = {
|
|
"_tool_name": "text_editor",
|
|
"action": action,
|
|
"path": path,
|
|
"format": extension,
|
|
"extension": extension,
|
|
"open_in_canvas": open_in_canvas,
|
|
}
|
|
context_id = str(getattr(getattr(agent, "context", None), "id", "") or "").strip()
|
|
if context_id:
|
|
additional["context_id"] = context_id
|
|
additional["ctxid"] = context_id
|
|
return additional
|
|
|
|
|
|
def _truthy(value) -> bool:
|
|
if isinstance(value, bool):
|
|
return value
|
|
if value is None:
|
|
return False
|
|
if isinstance(value, (int, float)):
|
|
return value != 0
|
|
return str(value).strip().lower() in {"1", "true", "yes", "y", "on"}
|
|
|
|
# ------------------------------------------------------------------
|
|
# Config
|
|
# ------------------------------------------------------------------
|
|
|
|
def _get_config(agent) -> dict:
|
|
config = plugins.get_plugin_config("_text_editor", agent=agent) or {}
|
|
return {
|
|
"max_line_tokens": int(config.get("max_line_tokens", 500)),
|
|
"default_line_count": int(config.get("default_line_count", 100)),
|
|
"max_total_read_tokens": int(config.get("max_total_read_tokens", 4000)),
|
|
}
|
|
|
|
|
|
def _current_action(tool: TextEditor, kwargs: dict) -> str:
|
|
return (
|
|
str(
|
|
kwargs.get("action")
|
|
or tool.args.get("action")
|
|
or ""
|
|
)
|
|
.strip()
|
|
.lower()
|
|
.replace("-", "_")
|
|
)
|