From 394b4f2cc3f28fc41ea7f4dd27d09362b52ef45e Mon Sep 17 00:00:00 2001 From: Zihan Xu <2024010904024@std.uestc.edu.cn> Date: Tue, 22 Sep 2026 21:44:19 +0800 Subject: [PATCH] fix(files): honor Unicode byte-order marks --- nanobot/agent/tools/filesystem.py | 44 +++++++++++++++++-------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/nanobot/agent/tools/filesystem.py b/nanobot/agent/tools/filesystem.py index 7a2ae142f..6f680bb05 100644 --- a/nanobot/agent/tools/filesystem.py +++ b/nanobot/agent/tools/filesystem.py @@ -348,28 +348,32 @@ class ReadFileTool(_FsTool): fp, offset=offset, limit=limit, content_hash=content_hash, ): return f"[File unchanged since last read: {path}]" - try: - 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 + from nanobot.utils.document import _decode_bom_text - 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})", + text_content = _decode_bom_text(raw) + if text_content is None: + try: + 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 = 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 # concern (git checkouts with autocrlf, editors saving CRLF) but