Agent Zero AI framework
text_editor: simplify RFC implementation
linuztx1491d72db493
diff --git a/plugins/text_editor/helpers/file_ops.py b/plugins/text_editor/helpers/file_ops.pyindex aa002c865..43114773e 100644--- a/plugins/text_editor/helpers/file_ops.py+++ b/plugins/text_editor/helpers/file_ops.py@@ -1,55 +1,78 @@"""-Async file operations for the text_editor plugin. Routes I/O to the-Docker container via rfc_text_editor._impl functions and applies-token budgeting host-side.+Pure file operations for the text_editor plugin.++No agent/tool dependencies — only stdlib + tokens helper."""-from dataclasses import dataclass+import os+import shutil+import tempfile+from typing import TypedDict++from python.helpers import tokens++_BINARY_PEEK = 8192-from python.helpers import tokens, runtime-from plugins.text_editor.helpers.rfc_text_editor import (- is_binary_impl as _is_binary_impl,- read_file_raw_impl as _read_file_raw_impl,- write_file_impl as _write_file_impl,- apply_patch_impl as _apply_patch_impl,- file_info_impl as _file_info_impl,-)# ------------------------------------------------------------------# Binary detection# -------------------------------------------------------------------async def is_binary(path: str) -> bool:+def is_binary(path: str) -> bool:"""Detect binary file by checking for null bytes."""- return await runtime.call_development_function(_is_binary_impl, path)+ try:+ with open(path, "rb") as f:+ chunk = f.read(_BINARY_PEEK)+ return b"\x00" in chunk+ except OSError:+ return False# ------------------------------------------------------------------# File metadata# -------------------------------------------------------------------async def file_info(path: str) -> dict:- """- Get file metadata from the container.-- Returns dict with: exists, is_file, realpath, expanded, mtime.- """- return await runtime.call_development_function(_file_info_impl, path)+class FileInfo(TypedDict):+ exists: bool+ is_file: bool+ realpath: str+ expanded: str+ mtime: float | None+++def file_info(path: str) -> FileInfo:+ """Return file metadata for mtime tracking and path resolution."""+ path = os.path.expanduser(path)+ rp = os.path.realpath(path)+ exists = os.path.exists(path)+ is_file = os.path.isfile(path)+ mtime = None+ if exists:+ try:+ mtime = os.path.getmtime(path)+ except OSError:+ pass+ return FileInfo(+ exists=exists,+ is_file=is_file,+ realpath=rp,+ expanded=path,+ mtime=mtime,+ )# ------------------------------------------------------------------# Read# -------------------------------------------------------------------@dataclass-class ReadResult:- content: str = ""- total_lines: int = 0- warnings: str = ""- error: str = ""+class ReadResult(TypedDict):+ content: str+ total_lines: int+ warnings: str+ error: str-async def read_file(+def read_file(path: str,line_from: int = 1,line_to: int | None = None,@@ -64,15 +87,30 @@ async def read_file(line_from and line_to are both inclusive.None line_to defaults to line_from + default_line_count - 1."""- # I/O happens in the container via RFC; token budgeting stays host-side.- raw = await runtime.call_development_function(_read_file_raw_impl, path)+ path = os.path.expanduser(path)++ if not os.path.isfile(path):+ return ReadResult(+ content="", total_lines=0, warnings="",+ error="file not found",+ )- if raw["error"]:- return ReadResult(error=raw["error"])+ if is_binary(path):+ return ReadResult(+ content="", total_lines=0, warnings="",+ error="file appears binary, use terminal instead",+ )- all_lines = raw["lines"]- total_lines = raw["total_lines"]+ try:+ with open(path, "r", encoding="utf-8", errors="replace") as f:+ all_lines = f.readlines()+ except OSError as exc:+ return ReadResult(+ content="", total_lines=0, warnings="",+ error=str(exc),+ )+ total_lines = len(all_lines)line_from = max(line_from, 1)if line_to is None:line_to = line_from + default_line_count - 1@@ -129,6 +167,7 @@ async def read_file(content="\n".join(output_lines),total_lines=total_lines,warnings=warn_str,+ error="",)@@ -136,33 +175,37 @@ async def read_file(# Write# -------------------------------------------------------------------@dataclass-class WriteResult:- total_lines: int = 0- error: str = ""+class WriteResult(TypedDict):+ total_lines: int+ error: str-async def write_file(path: str, content: str | None) -> WriteResult:+def write_file(path: str, content: str | None) -> WriteResult:"""Create or overwrite a file."""if content is None:content = ""- result = await runtime.call_development_function(- _write_file_impl, path, content+ path = os.path.expanduser(path)+ try:+ os.makedirs(os.path.dirname(path) or ".", exist_ok=True)+ with open(path, "w", encoding="utf-8") as f:+ f.write(content)+ except OSError as exc:+ return WriteResult(total_lines=0, error=str(exc))++ total = content.count("\n") + (+ 1 if content and not content.endswith("\n") else 0)- if result["error"]:- return WriteResult(error=result["error"])- return WriteResult(total_lines=result["total_lines"])+ return WriteResult(total_lines=total, error="")# ------------------------------------------------------------------# Patch# -------------------------------------------------------------------@dataclass-class PatchResult:- total_lines: int = 0- edit_count: int = 0- error: str = ""+class PatchResult(TypedDict):+ total_lines: int+ edit_count: int+ error: strdef validate_edits(edits: list | None) -> tuple[list[dict], str]:@@ -218,7 +261,7 @@ def validate_edits(edits: list | None) -> tuple[list[dict], str]:return parsed, ""-async def apply_patch(path: str, edits: list[dict]) -> int:+def apply_patch(path: str, edits: list[dict]) -> int:"""Apply sorted, validated edits by streaming to a temp file.@@ -226,29 +269,94 @@ async def apply_patch(path: str, edits: list[dict]) -> int:Inserts have 'insert': True.Returns total line count after patching."""- result = await runtime.call_development_function(- _apply_patch_impl, path, edits- )- if result["error"]:- raise Exception(result["error"])- return result["total_lines"]-+ # Ensure content always ends with newline to prevent line merging+ for e in edits:+ if e["content"] and not e["content"].endswith("\n"):+ e["content"] += "\n"-async def patch_file(path: str, edits: list | None) -> PatchResult:+ dir_name = os.path.dirname(path) or "."+ fd, tmp_path = tempfile.mkstemp(dir=dir_name, suffix=".tmp")+ try:+ with (+ open(path, "r", encoding="utf-8", errors="replace") as src,+ os.fdopen(fd, "w", encoding="utf-8") as dst,+ ):+ edit_idx = 0+ line_no = 1 # 1-based+ total_written = 0++ for raw_line in src:+ # Process all inserts targeting this line first+ while (+ edit_idx < len(edits)+ and edits[edit_idx]["insert"]+ and edits[edit_idx]["from"] == line_no+ ):+ edit = edits[edit_idx]+ if edit["content"]:+ dst.write(edit["content"])+ total_written += _count_content_lines(edit["content"])+ edit_idx += 1++ # Check if current line falls in a replace/delete range+ if edit_idx < len(edits) and not edits[edit_idx]["insert"]:+ edit = edits[edit_idx]+ if edit["from"] <= line_no <= edit["to"]:+ # Write replacement content once at range start+ if line_no == edit["from"] and edit["content"]:+ dst.write(edit["content"])+ total_written += _count_content_lines(+ edit["content"]+ )+ # Skip original line; advance edit at range end+ if line_no == edit["to"]:+ edit_idx += 1+ line_no += 1+ continue++ dst.write(raw_line)+ total_written += 1+ line_no += 1++ # Remaining edits past end of file+ while edit_idx < len(edits):+ edit = edits[edit_idx]+ if edit["content"]:+ dst.write(edit["content"])+ total_written += _count_content_lines(edit["content"])+ edit_idx += 1++ shutil.move(tmp_path, path)+ return total_written+ except Exception:+ if os.path.exists(tmp_path):+ os.unlink(tmp_path)+ raise+++def patch_file(path: str, edits: list | None) -> PatchResult:"""Validate and apply edits to a file."""- info = await runtime.call_development_function(- _file_info_impl, path- )- if not info["is_file"]:- return PatchResult(error="file not found")+ path = os.path.expanduser(path)+ if not os.path.isfile(path):+ return PatchResult(total_lines=0, edit_count=0, error="file not found")parsed, err = validate_edits(edits)if err:- return PatchResult(error=err)+ return PatchResult(total_lines=0, edit_count=0, error=err)try:- total = await apply_patch(info["expanded"], parsed)+ total = apply_patch(path, parsed)except Exception as exc:- return PatchResult(error=str(exc))+ return PatchResult(total_lines=0, edit_count=0, error=str(exc))- return PatchResult(total_lines=total, edit_count=len(parsed))+ return PatchResult(total_lines=total, edit_count=len(parsed), error="")+++# ------------------------------------------------------------------+# Internal+# ------------------------------------------------------------------++def _count_content_lines(content: str) -> int:+ return content.count("\n") + (+ 1 if content and not content.endswith("\n") else 0+ )diff --git a/plugins/text_editor/helpers/rfc_text_editor.py b/plugins/text_editor/helpers/rfc_text_editor.pydeleted file mode 100644index f3e15099b..000000000--- a/plugins/text_editor/helpers/rfc_text_editor.py+++ /dev/null@@ -1,206 +0,0 @@-"""-Filesystem operations for the text_editor plugin, executed on the-Docker container via RFC. Stdlib only — args and return values-must be JSON-serializable.-"""--import os-import shutil-import tempfile--_BINARY_PEEK = 8192---# -------------------------------------------------------------------# Internal-# --------------------------------------------------------------------def _count_content_lines(content: str) -> int:- return content.count("\n") + (- 1 if content and not content.endswith("\n") else 0- )---# -------------------------------------------------------------------# Binary detection-# --------------------------------------------------------------------def is_binary_impl(path: str) -> bool:- """Check for null bytes in the first 8 KiB."""- try:- with open(path, "rb") as f:- chunk = f.read(_BINARY_PEEK)- return b"\x00" in chunk- except OSError:- return False---# -------------------------------------------------------------------# Read-# --------------------------------------------------------------------def read_file_raw_impl(path: str) -> dict:- """- Read a text file and return raw lines plus metadata.-- Returns dict with:- - lines: list[str] (raw lines including newlines)- - total_lines: int- - error: str (empty on success)- """- path = os.path.expanduser(path)-- if not os.path.isfile(path):- return {"lines": [], "total_lines": 0, "error": "file not found"}-- # Binary check inline to avoid extra RFC round-trip- try:- with open(path, "rb") as f:- chunk = f.read(_BINARY_PEEK)- if b"\x00" in chunk:- return {"lines": [], "total_lines": 0,- "error": "file appears binary, use terminal instead"}- except OSError:- pass-- try:- with open(path, "r", encoding="utf-8", errors="replace") as f:- all_lines = f.readlines()- except OSError as exc:- return {"lines": [], "total_lines": 0, "error": str(exc)}-- return {"lines": all_lines, "total_lines": len(all_lines), "error": ""}---# -------------------------------------------------------------------# Write-# --------------------------------------------------------------------def write_file_impl(path: str, content: str) -> dict:- """- Write content to a file, creating parent dirs as needed.-- Returns dict with:- - total_lines: int- - error: str (empty on success)- """- path = os.path.expanduser(path)- try:- os.makedirs(os.path.dirname(path) or ".", exist_ok=True)- with open(path, "w", encoding="utf-8") as f:- f.write(content)- except OSError as exc:- return {"total_lines": 0, "error": str(exc)}-- total = content.count("\n") + (- 1 if content and not content.endswith("\n") else 0- )- return {"total_lines": total, "error": ""}---# -------------------------------------------------------------------# Patch-# --------------------------------------------------------------------def apply_patch_impl(path: str, edits: list) -> dict:- """- Apply sorted, validated edits to a file.-- Returns dict with:- - total_lines: int- - error: str (empty on success)- """- path = os.path.expanduser(path)-- # Ensure content always ends with newline to prevent line merging- for e in edits:- if e["content"] and not e["content"].endswith("\n"):- e["content"] += "\n"-- dir_name = os.path.dirname(path) or "."- fd, tmp_path = tempfile.mkstemp(dir=dir_name, suffix=".tmp")- try:- with (- open(path, "r", encoding="utf-8", errors="replace") as src,- os.fdopen(fd, "w", encoding="utf-8") as dst,- ):- edit_idx = 0- line_no = 1- total_written = 0-- for raw_line in src:- while (- edit_idx < len(edits)- and edits[edit_idx]["insert"]- and edits[edit_idx]["from"] == line_no- ):- edit = edits[edit_idx]- if edit["content"]:- dst.write(edit["content"])- total_written += _count_content_lines(edit["content"])- edit_idx += 1-- if edit_idx < len(edits) and not edits[edit_idx]["insert"]:- edit = edits[edit_idx]- if edit["from"] <= line_no <= edit["to"]:- if line_no == edit["from"] and edit["content"]:- dst.write(edit["content"])- total_written += _count_content_lines(- edit["content"]- )- if line_no == edit["to"]:- edit_idx += 1- line_no += 1- continue-- dst.write(raw_line)- total_written += 1- line_no += 1-- while edit_idx < len(edits):- edit = edits[edit_idx]- if edit["content"]:- dst.write(edit["content"])- total_written += _count_content_lines(edit["content"])- edit_idx += 1-- shutil.move(tmp_path, path)- return {"total_lines": total_written, "error": ""}- except Exception as exc:- if os.path.exists(tmp_path):- os.unlink(tmp_path)- return {"total_lines": 0, "error": str(exc)}---# -------------------------------------------------------------------# File info-# --------------------------------------------------------------------def file_info_impl(path: str) -> dict:- """- Return file metadata needed by the host for mtime tracking.-- Returns dict with:- - exists: bool- - is_file: bool- - realpath: str- - expanded: str (expanduser result)- - mtime: float | None- """- path = os.path.expanduser(path)- rp = os.path.realpath(path)- exists = os.path.exists(path)- is_file = os.path.isfile(path)- mtime = None- if exists:- try:- mtime = os.path.getmtime(path)- except OSError:- pass- return {- "exists": exists,- "is_file": is_file,- "realpath": rp,- "expanded": path,- "mtime": mtime,- }diff --git a/plugins/text_editor/tools/text_editor.py b/plugins/text_editor/tools/text_editor.pyindex 978568639..edbb627df 100644--- a/plugins/text_editor/tools/text_editor.py+++ b/plugins/text_editor/tools/text_editor.py@@ -1,7 +1,8 @@from python.helpers.tool import Tool, Responsefrom python.helpers.extension import call_extensions-from python.helpers import plugins+from python.helpers import plugins, runtimefrom plugins.text_editor.helpers.file_ops import (+ FileInfo,read_file,write_file,validate_edits,@@ -41,7 +42,8 @@ class TextEditor(Tool):raw_to = kwargs.get("line_to")line_to = int(raw_to) if raw_to is not None else None- result = await read_file(+ result = await runtime.call_development_function(+ read_file,path,line_from=line_from,line_to=line_to,@@ -50,14 +52,17 @@ class TextEditor(Tool):max_total_read_tokens=cfg["max_total_read_tokens"],)- if result.error:- return self._error("read", path, result.error)+ if result["error"]:+ return self._error("read", path, result["error"])- info = await file_info(path)- await _record_mtime(self.agent, info, result.total_lines)+ info = await runtime.call_development_function(file_info, path)+ _record_mtime(self.agent, info, result["total_lines"])# Extension point- ext_data = {"content": result.content, "warnings": result.warnings}+ ext_data = {+ "content": result["content"],+ "warnings": result["warnings"],+ }await call_extensions("text_editor_read_after", agent=self.agent, data=ext_data)@@ -65,7 +70,7 @@ class TextEditor(Tool):msg = self.agent.read_prompt("fw.text_editor.read_ok.md",path=info["expanded"],- total_lines=str(result.total_lines),+ total_lines=str(result["total_lines"]),warnings=ext_data["warnings"],content=ext_data["content"],)@@ -74,7 +79,9 @@ class TextEditor(Tool):# ------------------------------------------------------------------# WRITE# ------------------------------------------------------------------- async def _write(self, path: str = "", content: str | None = "", **kwargs) -> Response:+ async def _write(+ self, path: str = "", content: str | None = "", **kwargs+ ) -> Response:if not path:return self._error("write", path, "path is required")@@ -84,25 +91,28 @@ class TextEditor(Tool):"text_editor_write_before", agent=self.agent, data=ext_data)- result = await write_file(ext_data["path"], ext_data["content"])+ result = await runtime.call_development_function(+ write_file, ext_data["path"], ext_data["content"]+ )- if result.error:- return self._error("write", path, result.error)+ if result["error"]:+ return self._error("write", path, result["error"])# Extension pointawait call_extensions("text_editor_write_after", agent=self.agent,- data={"path": path, "total_lines": result.total_lines},+ data={"path": path, "total_lines": result["total_lines"]},)- info = await file_info(path)- await _record_mtime(self.agent, info, result.total_lines)+ info = await runtime.call_development_function(file_info, path)+ _record_mtime(self.agent, info, result["total_lines"])cfg = _get_config(self.agent)- read_result = await read_file(+ read_result = await runtime.call_development_function(+ read_file,info["expanded"],line_from=1,- line_to=result.total_lines,+ line_to=result["total_lines"],max_line_tokens=cfg["max_line_tokens"],max_total_read_tokens=cfg["max_total_read_tokens"],)@@ -110,8 +120,8 @@ class TextEditor(Tool):msg = self.agent.read_prompt("fw.text_editor.write_ok.md",path=info["expanded"],- total_lines=str(result.total_lines),- content=read_result.content,+ total_lines=str(result["total_lines"]),+ content=read_result["content"],)return Response(message=msg, break_loop=False)@@ -122,7 +132,7 @@ class TextEditor(Tool):if not path:return self._error("patch", path, "path is required")- info = await file_info(path)+ info = await runtime.call_development_function(file_info, path)if not info["is_file"]:return self._error("patch", path, "file not found")@@ -143,7 +153,9 @@ class TextEditor(Tool):)try:- total_lines = await apply_patch(ext_data["path"], ext_data["edits"])+ total_lines = await runtime.call_development_function(+ apply_patch, ext_data["path"], ext_data["edits"]+ )except Exception as exc:return self._error("patch", path, str(exc))@@ -154,8 +166,12 @@ class TextEditor(Tool):)# Refresh file info after patch for updated mtime- post_info = await file_info(expanded)- _apply_patch_post(self.agent, post_info, total_lines, ext_data["edits"])+ post_info = await runtime.call_development_function(+ file_info, expanded+ )+ _apply_patch_post(+ self.agent, post_info, total_lines, ext_data["edits"]+ )patch_content = await _read_patch_region(expanded, ext_data["edits"], total_lines, _get_config(self.agent)@@ -203,18 +219,18 @@ async def _read_patch_region(max_to = max(e["to"] for e in edits)end_line = max_to + added - removed + 3- result = await read_file(+ result = await runtime.call_development_function(+ 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+ return result["content"]-async def _record_mtime(agent, info: dict, total_lines: int):- """Record mtime using file_info dict from the container."""+def _record_mtime(agent, info: FileInfo, total_lines: int):mtimes = agent.data.setdefault(_MTIME_KEY, {})if info["mtime"] is not None:mtimes[info["realpath"]] = {@@ -223,12 +239,6 @@ async def _record_mtime(agent, info: dict, total_lines: int):}-def _clear_mtime(agent, info: dict):- mtimes = agent.data.get(_MTIME_KEY)- if mtimes is not None:- mtimes.pop(info["realpath"], None)--def _count_content_lines(content: str) -> int:return content.count("\n") + (1 if content and not content.endswith("\n") else 0@@ -246,7 +256,9 @@ def _all_edits_in_place(edits: list[dict]) -> bool:return True-def _apply_patch_post(agent, info: dict, new_total: int, edits: list[dict]):+def _apply_patch_post(+ agent, info: FileInfo, new_total: int, edits: list[dict]+):mtimes = agent.data.setdefault(_MTIME_KEY, {})real = info["realpath"]@@ -272,7 +284,7 @@ def _apply_patch_post(agent, info: dict, new_total: int, edits: list[dict]):mtimes[real] = {"mtime": 0, "total_lines": 0}-def _check_mtime(agent, info: dict) -> str:+def _check_mtime(agent, info: FileInfo) -> str:mtimes = agent.data.get(_MTIME_KEY, {})real = info["realpath"]if real not in mtimes: