from __future__ import annotations from typing import Any from yuxi.channels.models import Attachment, ChannelResponse class WeChatAttachmentAdapter: MAX_FILE_SIZE = 20 * 1024 * 1024 async def prepare(self, response: ChannelResponse) -> dict[str, Any]: result: dict[str, Any] = {"ok": True} for att in response.attachments: if att.size and att.size > self.MAX_FILE_SIZE: result["ok"] = False result.setdefault("errors", []).append( f"File too large: {att.filename} ({att.size} > {self.MAX_FILE_SIZE})" ) return result @staticmethod def build_attachment_inbound(attachment_data: dict) -> Attachment: return Attachment( file_id=attachment_data.get("media_id", ""), filename=attachment_data.get("filename", ""), mime_type=attachment_data.get("mime_type", "application/octet-stream"), size=attachment_data.get("size"), url=attachment_data.get("url"), ) @staticmethod def map_inbound_type(wechat_msg_type: str, mode: str = "mp") -> str: if wechat_msg_type == "image": return "image/png" elif wechat_msg_type == "voice": return "audio/amr" elif wechat_msg_type == "video": return "video/mp4" elif wechat_msg_type == "file": return "application/octet-stream" return "application/octet-stream" @staticmethod def build_attachbounds(message: dict) -> list[dict[str, Any]]: return [] @staticmethod def adapt_response(response: dict, incoming_messages: list[dict]) -> dict: return response