From 679ba6f36a549a0df0ab15d1108dcd4ffaa382d5 Mon Sep 17 00:00:00 2001 From: WeDone Date: Tue, 12 May 2026 11:37:01 +0800 Subject: [PATCH] =?UTF-8?q?fix(zulip):=20=E4=BF=AE=E5=A4=8D=E9=99=84?= =?UTF-8?q?=E4=BB=B6=E9=87=8D=E5=8F=91=E6=97=B6=20Zulip=20=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E8=B7=AF=E5=BE=84=E8=A2=AB=E5=BD=93=E4=BD=9C=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit LLM 回复时可能使用消息内容中的 Zulip 上传路径(如 /user_uploads/...) 作为 media 参数,而非本地已下载的文件路径,导致 open() 报错 No such file or directory。 添加 _resolve_media_path 方法,在上传前将 Zulip 路径解析为本地文件: - 本地路径直接存在则原样返回 - Zulip 上传路径(/user_uploads/...)或完整 URL 先查找已下载的本地副本 - 若本地不存在则从 Zulip 服务器重新下载 - 无法解析时记录错误并跳过 --- deeptutor/tutorbot/channels/zulip.py | 45 +++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/deeptutor/tutorbot/channels/zulip.py b/deeptutor/tutorbot/channels/zulip.py index 2c38f9fde..f81a3a0f7 100644 --- a/deeptutor/tutorbot/channels/zulip.py +++ b/deeptutor/tutorbot/channels/zulip.py @@ -532,12 +532,55 @@ class ZulipChannel(BaseChannel): if result.get("result") != "success": logger.error("Zulip send failed: {}", result.get("msg", "unknown")) + def _resolve_media_path(self, media_path: str) -> str | None: + if Path(media_path).exists(): + return media_path + + path_id: str | None = None + if media_path.startswith("/user_uploads/"): + path_id = media_path + elif self.config.site and media_path.startswith(self.config.site): + stripped = media_path[len(self.config.site.rstrip("/")):] + if stripped.startswith("/user_uploads/"): + path_id = stripped + + if not path_id: + return None + + media_dir = get_media_dir("zulip") + name = Path(unquote(path_id)).name + dest = self._attachment_destination(media_dir, name, path_id, 0) + + if dest.exists(): + return str(dest) + + url = f"{self.config.site.rstrip('/')}{path_id}" + try: + resp = requests.get( + url, + auth=(self.config.email, self.config.api_key), + timeout=self.config.timeout, + ) + resp.raise_for_status() + dest.write_bytes(resp.content) + logger.debug("Downloaded Zulip attachment for re-upload: {}", name) + return str(dest) + except Exception as e: + logger.warning("Failed to download Zulip attachment {}: {}", name, e) + return None + async def _upload_and_send(self, chat_id: str, media_path: str, metadata: dict) -> None: client = self._client if not client: return + + local_path = self._resolve_media_path(media_path) + if not local_path: + logger.error("Cannot resolve media path: {}", media_path) + return + loop = asyncio.get_running_loop() - with open(media_path, "rb") as f: + with open(local_path, "rb") as f: result = await loop.run_in_executor( None, lambda: self._call_with_retry(