refactor(files): share document extraction and stream cleanup
This commit is contained in:
1 parent
c11c1a376f
commit
63209dab1d
4 files changed
+63
-86
No files matched your search
@@ -466,7 +466,7 @@ class ReadFileTool(_FsTool):
|
||||
return result
|
||||
|
||||
def _read_pdf(self, source: ReadableFile, pages: str | None) -> str:
|
||||
from nanobot.utils.document import PdfPageRangeError, PdfSafetyError
|
||||
from nanobot.utils.document import PdfPageRangeError
|
||||
|
||||
try:
|
||||
extraction = source.pdf_pages(
|
||||
@@ -476,8 +476,6 @@ class ReadFileTool(_FsTool):
|
||||
)
|
||||
except PdfPageRangeError as e:
|
||||
return ToolResult.error(f"Error: Invalid page range '{pages}': {e!s}.")
|
||||
except PdfSafetyError as e:
|
||||
return ToolResult.error(f"Error reading PDF: {e}")
|
||||
except Exception as e:
|
||||
return ToolResult.error(f"Error reading PDF: {e}")
|
||||
|
||||
@@ -508,12 +506,11 @@ class ReadFileTool(_FsTool):
|
||||
fp = file.path
|
||||
offset = max(1, offset)
|
||||
requested_limit = limit or self._DEFAULT_LIMIT
|
||||
source_iterator = None
|
||||
source = None
|
||||
try:
|
||||
source = file.document_lines()
|
||||
if source is None:
|
||||
return ToolResult.error(f"Error: Unsupported file format: {fp.suffix}")
|
||||
source_iterator = source.lines
|
||||
numbered: list[str] = []
|
||||
output_chars = 0
|
||||
total_seen = 0
|
||||
@@ -521,7 +518,7 @@ class ReadFileTool(_FsTool):
|
||||
has_more = False
|
||||
line_was_clipped = False
|
||||
|
||||
for line in source_iterator:
|
||||
for line in source.lines:
|
||||
total_seen = line.extracted_line
|
||||
if line.extracted_line < offset:
|
||||
continue
|
||||
@@ -574,9 +571,8 @@ class ReadFileTool(_FsTool):
|
||||
except Exception as e:
|
||||
return ToolResult.error(f"Error reading {fp.suffix.upper()} file: {e!s}")
|
||||
finally:
|
||||
close = getattr(source_iterator, "close", None)
|
||||
if close is not None:
|
||||
close()
|
||||
if source is not None:
|
||||
source.close()
|
||||
|
||||
|
||||
@tool_parameters(
|
||||
|
||||
@@ -783,7 +783,7 @@ class GrepTool(_SearchTool):
|
||||
with suppress(ValueError):
|
||||
display_path = file_path.relative_to(workspace).as_posix()
|
||||
|
||||
source_iterator: Iterator[LocatedDocumentLine] | None = None
|
||||
source: DocumentLineSource | None = None
|
||||
is_document = file_path.suffix.lower() in _DOCUMENT_EXTENSIONS
|
||||
file_matches = 0
|
||||
try:
|
||||
@@ -811,12 +811,11 @@ class GrepTool(_SearchTool):
|
||||
if source is None:
|
||||
skipped_binary += 1
|
||||
continue
|
||||
source_iterator = source.lines
|
||||
if source.continuation and len(document_continuations) < 10:
|
||||
document_continuations.append(
|
||||
f"({display_path}: continue PDF search with {source.continuation})"
|
||||
)
|
||||
source_lines = checked_lines(source_iterator)
|
||||
source_lines = checked_lines(source.lines)
|
||||
|
||||
if output_mode == "content":
|
||||
content_page.scan(display_path, source_lines, regex, context_before, context_after)
|
||||
@@ -845,9 +844,8 @@ class GrepTool(_SearchTool):
|
||||
document_error = f"{display_path}: {e!s}"
|
||||
continue
|
||||
finally:
|
||||
close = getattr(source_iterator, "close", None)
|
||||
if close is not None:
|
||||
close()
|
||||
if source is not None:
|
||||
source.close()
|
||||
# A document can fail after yielding matches. Keep the count
|
||||
# of those lines even though the failed file is not ranked.
|
||||
if file_matches and output_mode == "count":
|
||||
|
||||
Reference in new issue
Block a user