Skip to content

Agent Zero AI framework

fix: update file operations for inclusive line range handling

linuztxd45552572c2a
diff --git a/plugins/text_editor/helpers/file_ops.py b/plugins/text_editor/helpers/file_ops.py
index c71b3221e..c45114bf6 100644
--- a/plugins/text_editor/helpers/file_ops.py
+++ b/plugins/text_editor/helpers/file_ops.py
@@ -7,7 +7,7 @@ No agent/tool dependencies — only stdlib + tokens helper.
import os
import shutil
import tempfile
-from dataclasses import dataclass, field
+from dataclasses import dataclass
from typing import TYPE_CHECKING
from python.helpers import plugins, tokens
@@ -60,12 +60,17 @@ class ReadResult:
def read_file(
path: str,
line_from: int = 0,
- line_to: int = 0,
+ line_to: int | None = None,
max_line_tokens: int = 500,
default_line_count: int = 100,
max_total_read_tokens: int = 4000,
) -> ReadResult:
- """Read a text file and return numbered lines with token budgeting."""
+ """
+ Read a text file and return numbered lines with token budgeting.
+
+ line_from and line_to are both inclusive. None line_to defaults to
+ line_from + default_line_count - 1.
+ """
path = os.path.expanduser(path)
if not os.path.isfile(path):
@@ -82,11 +87,12 @@ def read_file(
total_lines = len(all_lines)
line_from = max(line_from, 0)
- if not line_to:
- line_to = min(line_from + default_line_count, total_lines)
- line_to = min(line_to, total_lines)
+ if line_to is None:
+ line_to = line_from + default_line_count - 1
+ line_to = min(line_to, total_lines - 1)
- selected = all_lines[line_from:line_to]
+ # Slice is exclusive on the right, so +1
+ selected = all_lines[line_from:line_to + 1]
warn_parts: list[str] = []
cropped_lines: list[int] = []
@@ -146,8 +152,10 @@ class WriteResult:
error: str = ""
-def write_file(path: str, content: str) -> WriteResult:
+def write_file(path: str, content: str | None) -> WriteResult:
"""Create or overwrite a file."""
+ if content is None:
+ content = ""
path = os.path.expanduser(path)
try:
os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
@@ -173,7 +181,7 @@ class PatchResult:
error: str = ""
-def validate_edits(edits: list) -> tuple[list[dict], str]:
+def validate_edits(edits: list | None) -> tuple[list[dict], str]:
"""
Normalise and validate an edits array.
@@ -292,7 +300,7 @@ def apply_patch(path: str, edits: list[dict]) -> int:
raise
-def patch_file(path: str, edits: list) -> PatchResult:
+def patch_file(path: str, edits: list | None) -> PatchResult:
"""Validate and apply edits to a file."""
path = os.path.expanduser(path)
if not os.path.isfile(path):
@@ -318,4 +326,3 @@ def _count_content_lines(content: str) -> int:
return content.count("\n") + (
1 if content and not content.endswith("\n") else 0
)
-
\ No newline at end of file
diff --git a/plugins/text_editor/tools/text_editor.py b/plugins/text_editor/tools/text_editor.py
index 1f3707265..d54f52a7f 100644
--- a/plugins/text_editor/tools/text_editor.py
+++ b/plugins/text_editor/tools/text_editor.py
@@ -34,7 +34,8 @@ class TextEditor(Tool):
cfg = get_config(self.agent)
line_from = int(kwargs.get("line_from", 0))
- line_to = int(kwargs.get("line_to", 0))
+ raw_to = kwargs.get("line_to")
+ line_to = int(raw_to) if raw_to is not None else None
result = read_file(
path,
@@ -66,7 +67,7 @@ class TextEditor(Tool):
# ------------------------------------------------------------------
# WRITE
# ------------------------------------------------------------------
- async def _write(self, path: str = "", content: str = "", **kwargs) -> Response:
+ async def _write(self, path: str = "", content: str | None = "", **kwargs) -> Response:
if not path:
return self._error("write", path, "path is required")