from __future__ import annotations import logging import re from typing import Any logger = logging.getLogger(__name__) MEDIA_TAG_RE = re.compile(r"<(qqmedia|qqimg)>.*?") class StreamingMediaHandler: def __init__(self, streaming_controller: Any, outbound: Any): self._streaming = streaming_controller self._outbound = outbound async def handle_media_interrupt(self, openid: str, text: str) -> tuple[str, bool]: match = MEDIA_TAG_RE.search(text) if not match: return text, False before = text[: match.start()] tag_text = match.group(0) after = text[match.end() :] if before.strip(): try: await self._streaming.flush(openid) except Exception: pass await self._streaming.abort(openid) if tag_text: try: from yuxi.channel.extensions.qqbot.media_tags import parse_media_tags _, tags = parse_media_tags(tag_text) for tag in tags: await self._outbound.send_media(openid, tag.url, "auto") except Exception as e: logger.warning("Failed to send media in stream: %s", e) return after, True async def resume_after_media(self, openid: str, text: str) -> None: if not text.strip(): return stream_id = await self._streaming.start_streaming_session(openid) await self._streaming.feed(openid, text)