From fe8b8fe831340bbd0414f6a1904ac4635957ed38 Mon Sep 17 00:00:00 2001 From: 6vision Date: Sat, 30 May 2026 16:33:49 +0800 Subject: [PATCH] fix(wechatmp): support local file:// images in send Agent-generated images are sent as IMAGE_URL with a file:// path, but the wechatmp channel always used requests.get, which fails on file:// with InvalidSchema. Now read local files directly (file:// or local path) and fall back to HTTP download for remote URLs, in both passive and active reply modes. Co-authored-by: Cursor --- channel/wechatmp/wechatmp_channel.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/channel/wechatmp/wechatmp_channel.py b/channel/wechatmp/wechatmp_channel.py index c066f286..dc0ffb26 100644 --- a/channel/wechatmp/wechatmp_channel.py +++ b/channel/wechatmp/wechatmp_channel.py @@ -134,10 +134,16 @@ class WechatMPChannel(ChatChannel): elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片 img_url = reply.content - pic_res = requests.get(img_url, stream=True) image_storage = io.BytesIO() - for block in pic_res.iter_content(1024): - image_storage.write(block) + if img_url.startswith("file://") or os.path.isfile(img_url): + # Local file produced by the agent (e.g. a generated image) + local_path = img_url[len("file://"):] if img_url.startswith("file://") else img_url + with open(local_path, "rb") as f: + image_storage.write(f.read()) + else: + pic_res = requests.get(img_url, stream=True) + for block in pic_res.iter_content(1024): + image_storage.write(block) image_storage.seek(0) image_type = imghdr.what(image_storage) filename = receiver + "-" + str(context["msg"].msg_id) + "." + image_type @@ -258,10 +264,16 @@ class WechatMPChannel(ChatChannel): logger.info("[wechatmp] Do send voice to {}".format(receiver)) elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片 img_url = reply.content - pic_res = requests.get(img_url, stream=True) image_storage = io.BytesIO() - for block in pic_res.iter_content(1024): - image_storage.write(block) + if img_url.startswith("file://") or os.path.isfile(img_url): + # Local file produced by the agent (e.g. a generated image) + local_path = img_url[len("file://"):] if img_url.startswith("file://") else img_url + with open(local_path, "rb") as f: + image_storage.write(f.read()) + else: + pic_res = requests.get(img_url, stream=True) + for block in pic_res.iter_content(1024): + image_storage.write(block) image_storage.seek(0) image_type = imghdr.what(image_storage) filename = receiver + "-" + str(context["msg"].msg_id) + "." + image_type