fix(files): honor Unicode byte-order marks
This commit is contained in:
1 parent
602efc3bc3
commit
394b4f2cc3
1 file changed
+24
-20
@@ -348,28 +348,32 @@ class ReadFileTool(_FsTool):
|
|||||||
fp, offset=offset, limit=limit, content_hash=content_hash,
|
fp, offset=offset, limit=limit, content_hash=content_hash,
|
||||||
):
|
):
|
||||||
return f"[File unchanged since last read: {path}]"
|
return f"[File unchanged since last read: {path}]"
|
||||||
try:
|
from nanobot.utils.document import _decode_bom_text
|
||||||
text_content = raw.decode("utf-8")
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
# Match the former eager extractor for known text formats while
|
|
||||||
# keeping arbitrary binary files on the guarded error path.
|
|
||||||
from nanobot.utils.document import _is_text_extension
|
|
||||||
|
|
||||||
if _is_text_extension(fp.suffix.lower()):
|
text_content = _decode_bom_text(raw)
|
||||||
text_content = raw.decode("latin-1")
|
if text_content is None:
|
||||||
else:
|
try:
|
||||||
mime = detect_image_mime(raw) or mimetypes.guess_type(path)[0]
|
text_content = raw.decode("utf-8")
|
||||||
if mime and mime.startswith("image/"):
|
except UnicodeDecodeError:
|
||||||
return build_image_content_blocks(
|
# Match the former eager extractor for known text formats while
|
||||||
raw,
|
# keeping arbitrary binary files on the guarded error path.
|
||||||
mime,
|
from nanobot.utils.document import _is_text_extension
|
||||||
str(fp),
|
|
||||||
f"(Image file: {path})",
|
if _is_text_extension(fp.suffix.lower()):
|
||||||
|
text_content = raw.decode("latin-1")
|
||||||
|
else:
|
||||||
|
mime = detect_image_mime(raw) or mimetypes.guess_type(path)[0]
|
||||||
|
if mime and mime.startswith("image/"):
|
||||||
|
return build_image_content_blocks(
|
||||||
|
raw,
|
||||||
|
mime,
|
||||||
|
str(fp),
|
||||||
|
f"(Image file: {path})",
|
||||||
|
)
|
||||||
|
return ToolResult.error(
|
||||||
|
f"Error: Cannot read binary file {path} (MIME: {mime or 'unknown'}). "
|
||||||
|
"Only supported text files and images can be read."
|
||||||
)
|
)
|
||||||
return ToolResult.error(
|
|
||||||
f"Error: Cannot read binary file {path} (MIME: {mime or 'unknown'}). "
|
|
||||||
"Only supported text files and images can be read."
|
|
||||||
)
|
|
||||||
|
|
||||||
# Normalize CRLF -> LF before line-splitting. Primarily a Windows
|
# Normalize CRLF -> LF before line-splitting. Primarily a Windows
|
||||||
# concern (git checkouts with autocrlf, editors saving CRLF) but
|
# concern (git checkouts with autocrlf, editors saving CRLF) but
|
||||||
|
|||||||
Reference in new issue
Block a user