fix(files): reject oversized reads before loading
This commit is contained in:
committed by
chengyongru
parent
d5658dbc91
commit
cdb2df4982
@@ -237,6 +237,7 @@ class ReadFileTool(_FsTool):
|
||||
_scopes = {"core", "subagent", "memory"}
|
||||
|
||||
_MAX_CHARS = 128_000
|
||||
_MAX_FILE_SIZE_BYTES = 100 * 1024 * 1024
|
||||
_DEFAULT_LIMIT = 2000
|
||||
_MAX_PDF_PAGES = 20
|
||||
|
||||
@@ -290,6 +291,15 @@ class ReadFileTool(_FsTool):
|
||||
if not fp.is_file():
|
||||
return ToolResult.error(f"Error: Not a file: {path}")
|
||||
|
||||
file_size = fp.stat().st_size
|
||||
if file_size > self._MAX_FILE_SIZE_BYTES:
|
||||
size_mib = file_size / (1024 * 1024)
|
||||
max_mib = self._MAX_FILE_SIZE_BYTES // (1024 * 1024)
|
||||
return ToolResult.error(
|
||||
f"Error: File too large to read ({size_mib:.1f} MiB). "
|
||||
f"Maximum is {max_mib} MiB."
|
||||
)
|
||||
|
||||
# PDF support
|
||||
if fp.suffix.lower() == ".pdf":
|
||||
return self._read_pdf(fp, pages)
|
||||
|
||||
Reference in New Issue
Block a user