From 8ae281dba3a44bd7c4f8aa246c2765f450fe4e7f Mon Sep 17 00:00:00 2001 From: AlexFucuson9 Date: Sun, 28 Jun 2026 19:09:50 +0700 Subject: [PATCH] fix(platforms,gateway,tools): add encoding='utf-8' to write_text() calls Path.write_text() without an explicit encoding uses the platform's default encoding. On Windows this is typically cp1252 or mbcs, which corrupts non-ASCII characters (emoji, CJK text, accented characters) in user-facing content. Fixed the most critical locations where user/agent text is written: - Platform adapters (discord, telegram, feishu): temp file for reply text - gateway/run.py: agent response temp file - gateway/delivery.py: message content output files - tools/skills_hub.py: skill cache with ensure_ascii=False (Unicode data) This follows the existing pattern used throughout the codebase where read_text() calls consistently specify encoding='utf-8'. --- gateway/delivery.py | 4 ++-- gateway/run.py | 2 +- plugins/platforms/discord/adapter.py | 2 +- plugins/platforms/feishu/adapter.py | 2 +- plugins/platforms/telegram/adapter.py | 2 +- tools/skills_hub.py | 6 +++--- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/gateway/delivery.py b/gateway/delivery.py index faec3ca45eb7..798910e9042a 100644 --- a/gateway/delivery.py +++ b/gateway/delivery.py @@ -278,7 +278,7 @@ def _deliver_local( lines.append("") lines.append(content) - output_path.write_text("\n".join(lines)) + output_path.write_text("\n".join(lines), encoding="utf-8") return { "path": str(output_path), @@ -291,7 +291,7 @@ def _save_full_output(self, content: str, job_id: str) -> Path: out_dir = get_hermes_home() / "cron" / "output" out_dir.mkdir(parents=True, exist_ok=True) path = out_dir / f"{job_id}_{timestamp}.txt" - path.write_text(content) + path.write_text(content, encoding="utf-8") return path def _filter_silence_narration_enabled(self) -> bool: diff --git a/gateway/run.py b/gateway/run.py index 429f1ce0b542..d78d90dee76a 100644 --- a/gateway/run.py +++ b/gateway/run.py @@ -8001,7 +8001,7 @@ async def _handle_message(self, event: MessageEvent) -> Optional[str]: prompt_path = _hermes_home / ".update_prompt.json" try: tmp = response_path.with_suffix(".tmp") - tmp.write_text(response_text) + tmp.write_text(response_text, encoding="utf-8") tmp.replace(response_path) prompt_path.unlink(missing_ok=True) except OSError as e: diff --git a/plugins/platforms/discord/adapter.py b/plugins/platforms/discord/adapter.py index f5c83aede45b..e86fb1c844f3 100644 --- a/plugins/platforms/discord/adapter.py +++ b/plugins/platforms/discord/adapter.py @@ -6206,7 +6206,7 @@ async def _respond( home = get_hermes_home() response_path = home / ".update_response" tmp = response_path.with_suffix(".tmp") - tmp.write_text(answer) + tmp.write_text(answer, encoding="utf-8") tmp.replace(response_path) logger.info( "Discord update prompt answered '%s' by %s", diff --git a/plugins/platforms/feishu/adapter.py b/plugins/platforms/feishu/adapter.py index 42bd404104a1..269ff08868d9 100644 --- a/plugins/platforms/feishu/adapter.py +++ b/plugins/platforms/feishu/adapter.py @@ -2100,7 +2100,7 @@ def _build_resolved_update_prompt_card(*, answer: str, user_name: str) -> Dict[s def _write_update_prompt_response(answer: str) -> None: response_path = get_hermes_home() / ".update_response" tmp_path = response_path.with_suffix(".tmp") - tmp_path.write_text(answer) + tmp_path.write_text(answer, encoding="utf-8") tmp_path.replace(response_path) async def send_voice( diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.py index 9e456bc67bdd..8b76eaeddb0c 100644 --- a/plugins/platforms/telegram/adapter.py +++ b/plugins/platforms/telegram/adapter.py @@ -4805,7 +4805,7 @@ async def _handle_callback_query( home = get_hermes_home() response_path = home / ".update_response" tmp = response_path.with_suffix(".tmp") - tmp.write_text(answer) + tmp.write_text(answer, encoding="utf-8") tmp.replace(response_path) logger.info("Telegram update prompt answered '%s' by user %s", answer, getattr(query.from_user, "id", "unknown")) diff --git a/tools/skills_hub.py b/tools/skills_hub.py index 76969fb8d8ce..ec74edde9e61 100644 --- a/tools/skills_hub.py +++ b/tools/skills_hub.py @@ -987,7 +987,7 @@ def _write_cache(self, key: str, data: list) -> None: INDEX_CACHE_DIR.mkdir(parents=True, exist_ok=True) cache_file = INDEX_CACHE_DIR / f"{key}.json" try: - cache_file.write_text(json.dumps(data, ensure_ascii=False)) + cache_file.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8") except OSError as e: logger.debug("Could not write cache: %s", e) @@ -3180,7 +3180,7 @@ def _write_index_cache(key: str, data: Any) -> None: pass cache_file = INDEX_CACHE_DIR / f"{key}.json" try: - cache_file.write_text(json.dumps(data, ensure_ascii=False, default=str)) + cache_file.write_text(json.dumps(data, ensure_ascii=False, default=str), encoding="utf-8") except OSError as e: logger.debug("Could not write cache: %s", e) @@ -3220,7 +3220,7 @@ def load(self) -> dict: def save(self, data: dict) -> None: self.path.parent.mkdir(parents=True, exist_ok=True) - self.path.write_text(json.dumps(data, indent=2, ensure_ascii=False) + "\n") + self.path.write_text(json.dumps(data, indent=2, ensure_ascii=False) + "\n", encoding="utf-8") def record_install( self,