Skip to content

Your lightweight open-source personal agent.

feat(qq): show the agent the message a user quoted

GZY-SUPER-HACKER634d0e9188c4
diff --git a/nanobot/channels/qq/runtime.py b/nanobot/channels/qq/runtime.py
index da72a8068..3de44ff1b 100644
--- a/nanobot/channels/qq/runtime.py
+++ b/nanobot/channels/qq/runtime.py
@@ -27,6 +27,7 @@ import os
import re
import time
from collections import deque
+from collections.abc import Callable, Mapping
from contextlib import suppress
from pathlib import Path
from typing import Any, BinaryIO, Literal, cast
@@ -176,6 +177,119 @@ def _make_bot_class(channel: QQChannel) -> type[Any]:
return _Bot
+# ---------------------------------------------------------------------------
+# Quoted messages
+#
+# QQ's C2C/group events do not carry ``message_reference``; botpy models that
+# field because the object is shared with guild messages, but QQ never sends it
+# here. The quoted original arrives in ``msg_elements``, linked from
+# ``message_scene.ext`` by ``ref_msg_idx``. botpy parses neither field, and
+# ``BaseMessage.__slots__`` drops the raw payload, so
+# ``_install_qq_payload_capture`` keeps the payload from the constructor before
+# it is discarded. If a future botpy exposes those fields on the message
+# object, ``_quoted_content_for`` prefers that copy and the hook becomes dead
+# weight rather than a correctness dependency.
+# ---------------------------------------------------------------------------
+
+QQ_QUOTED_CONTEXT_MAX_CHARS = 4_000
+QQ_RAW_PAYLOAD_CACHE = 200
+
+_qq_raw_payloads: dict[str, dict[str, Any]] = {}
+
+
+def _flatten_scene_ext(scene: Any) -> dict[str, str]:
+ """Turn QQ's ``message_scene.ext`` (``["key=value", ...]``) into a mapping."""
+ if not isinstance(scene, Mapping):
+ return {}
+ ext = cast("Mapping[str, Any]", scene).get("ext")
+ if not isinstance(ext, list):
+ return {}
+ flattened: dict[str, str] = {}
+ for item in cast("list[Any]", ext):
+ if isinstance(item, str) and "=" in item:
+ key, _, value = item.partition("=")
+ flattened[key] = value
+ return flattened
+
+
+def extract_quoted_content(raw: Mapping[str, Any] | None) -> str | None:
+ """Return the text of the message the sender quoted, if any.
+
+ QQ points at the quoted message from ``message_scene.ext``: ``ref_msg_idx=X``
+ matches the ``msg_elements`` entry whose ``msg_idx`` is ``X``, and that
+ entry's ``content`` is the original text. Matching on the index instead of
+ taking the first element keeps this correct if QQ ever sends more than one.
+ """
+ if not isinstance(raw, Mapping):
+ return None
+
+ ref_idx = _flatten_scene_ext(raw.get("message_scene")).get("ref_msg_idx")
+ if not ref_idx:
+ return None
+
+ elements = raw.get("msg_elements")
+ if not isinstance(elements, list):
+ return None
+
+ for element in cast("list[Any]", elements):
+ if not isinstance(element, Mapping):
+ continue
+ entry = cast("Mapping[str, Any]", element)
+ if entry.get("msg_idx") != ref_idx:
+ continue
+ text = entry.get("content")
+ if not isinstance(text, str) or not text.strip():
+ return None
+ text = text.strip()
+ if len(text) > QQ_QUOTED_CONTEXT_MAX_CHARS:
+ text = text[:QQ_QUOTED_CONTEXT_MAX_CHARS] + "..."
+ return text
+ return None
+
+
+def _install_qq_payload_capture() -> None:
+ """Keep raw QQ payloads that botpy would otherwise discard (idempotent)."""
+ if not QQ_AVAILABLE:
+ return
+ try:
+ from botpy import message as botpy_message
+
+ for message_cls in (botpy_message.C2CMessage, botpy_message.GroupMessage):
+ original_init = cast("Callable[..., Any]", message_cls.__init__)
+ if getattr(original_init, "_nanobot_qq_capture", False):
+ continue
+
+ def _capture(
+ self: Any,
+ api: Any,
+ event_id: Any,
+ data: Any,
+ _original: Callable[..., Any] = original_init,
+ ) -> Any:
+ if isinstance(data, Mapping):
+ payload = cast("Mapping[str, Any]", data)
+ message_id = payload.get("id")
+ if isinstance(message_id, str):
+ _qq_raw_payloads[message_id] = dict(payload)
+ while len(_qq_raw_payloads) > QQ_RAW_PAYLOAD_CACHE:
+ _qq_raw_payloads.pop(next(iter(_qq_raw_payloads)))
+ return _original(self, api, event_id, data)
+
+ setattr(_capture, "_nanobot_qq_capture", True)
+ setattr(message_cls, "__init__", _capture)
+ except Exception: # pragma: no cover - optional dependency shape may vary
+ logger.debug("QQ quoted-message payload capture unavailable")
+
+
+def _quoted_content_for(message: Any) -> str | None:
+ """Resolve the quoted text for one inbound QQ message."""
+ scene = getattr(message, "message_scene", None)
+ elements = getattr(message, "msg_elements", None)
+ if scene is not None or elements is not None:
+ return extract_quoted_content({"message_scene": scene, "msg_elements": elements})
+ return extract_quoted_content(_qq_raw_payloads.get(str(getattr(message, "id", ""))))
+
+
class QQConfig(Base):
"""QQ channel configuration using botpy SDK."""
@@ -225,6 +339,9 @@ class QQChannel(BaseChannel):
self._msg_seq: int = 1 # used to avoid QQ API dedup
self._chat_type_cache: dict[str, str] = {}
+ # QQ delivers the quoted original in a payload field botpy discards.
+ _install_qq_payload_capture()
+
self._media_root: Path = self._init_media_root()
# ---------------------------
@@ -598,6 +715,14 @@ class QQChannel(BaseChannel):
)
media_paths, recv_lines, att_meta = await self._handle_attachments(attachments)
+ # Surface the quoted message to the agent. The inbound reply context
+ # is always on, matching Telegram; QQ needs the payload hook above
+ # because it exposes no way to fetch a message by id.
+ quoted = _quoted_content_for(message)
+ if quoted:
+ quote_tag = f"[Reply to: {quoted}]"
+ content = f"{quote_tag}\n{content}" if content else quote_tag
+
# Compose content that always contains actionable saved paths
if recv_lines:
tag = (
diff --git a/nanobot/channels/qq/tests/test_qq_reply_context.py b/nanobot/channels/qq/tests/test_qq_reply_context.py
new file mode 100644
index 000000000..ce73f32bb
--- /dev/null
+++ b/nanobot/channels/qq/tests/test_qq_reply_context.py
@@ -0,0 +1,328 @@
+"""Quoted-message context for the QQ channel.
+
+QQ's C2C/group events never send ``message_reference``: the quoted original
+arrives in ``msg_elements``, linked from ``message_scene.ext`` by
+``ref_msg_idx``. The payload shapes below are copied from real traffic
+captured from the QQ gateway (two C2C and two group messages, plus plain
+messages as the control group).
+"""
+
+import pytest
+
+from nanobot.channels.qq.runtime import (
+ QQ_QUOTED_CONTEXT_MAX_CHARS,
+ _flatten_scene_ext,
+ _quoted_content_for,
+ extract_quoted_content,
+)
+
+# --- Real payload shapes ----------------------------------------------------
+
+# C2C message quoting the bot. A plain message has no `ref_msg_idx` and no
+# `msg_elements`; both appear only when the user quotes something.
+C2C_QUOTING_BOT = {
+ "id": "ROBOT1.0_DBiKWe9lNhRwiQwghzMwQg3fHOmxJ90S.B57.sHfcrAMcKMJhlJ4wk5MgsMmqcYidhSYdP-5IbsrfZQ6pOu0mf9OC35zpbTlRPq8A8ILhi0!",
+ "content": "test reference agent",
+ "message_scene": {
+ "ext": [
+ "ref_msg_idx=REFIDX_uBOgqP76jtuIYTCmDNJGthZK49O4ei6cIos3sHgdtfubgjFV4RVX2AD1URlzskeW",
+ "msg_idx=REFIDX_+sv65NCtgisKm7eB0fzltmMfoKf14gEtdgeLHxxNuP3t1pc1n2JSZfwgAS8W12E0INOltlnT1lPyL6yrJZbYWQ",
+ ],
+ "source": "default",
+ },
+ "msg_elements": [
+ {
+ "content": "查完了,两处都没有叫 reference 的东西。",
+ "message_type": 103,
+ "msg_idx": "REFIDX_uBOgqP76jtuIYTCmDNJGthZK49O4ei6cIos3sHgdtfubgjFV4RVX2AD1URlzskeW",
+ }
+ ],
+}
+
+# Group message quoting the user. Group `ext` carries an extra `auth_token`.
+GROUP_QUOTING_USER = {
+ "id": "ROBOT1.0_uZIDPkWlheMbQUYJKntzLfHJEMIGUOnTsFp74wy65spVBJWKV4-TxT7wTHV9biFHC82a05rtkXGkQqRfQFWGypBMNmwvGII5silxxRb-AK4NIqrVm3QhC6-bhktADRFg",
+ "content": " test reference user",
+ "message_scene": {
+ "ext": [
+ "ref_msg_idx=REFIDX_9xpTdjgG3080nj0v1YGMwHdZWBXeRChx7OORXdh2Xc9VHvNpgceQ0PgIx6/LBhbdd3VXksZNcPQOxLTl1He34rbmIMmRJhq3rxXmKV+9mqxQvHemngO5h/ijdsYPFAbE",
+ "msg_idx=REFIDX_QHGqkOiPA6mM2Zu45iSXLndZWBXeRChx7OORXdh2Xc9VHvNpgceQ0PgIx6/LBhbdd3VXksZNcPQOxLTl1He34rbmIMmRJhq3rxXmKV+9mqxQvHemngO5h/ijdsYPFAbE",
+ "auth_token=N-JqY9nnTNRjjnyC6WLN7xyd7gHuhtiEfDrwWB7WF82c_5_go-2Y-l13UYmbD9yGgQUK8MVjimVtCPrhORwWsDQC4VlWMgC6B0wrSXBWHCcBwKQpHSbLbpH9Dq6n",
+ ],
+ "source": "default",
+ },
+ "msg_elements": [
+ {
+ "content": " test",
+ "message_type": 103,
+ "msg_idx": "REFIDX_9xpTdjgG3080nj0v1YGMwHdZWBXeRChx7OORXdh2Xc9VHvNpgceQ0PgIx6/LBhbdd3VXksZNcPQOxLTl1He34rbmIMmRJhq3rxXmKV+9mqxQvHemngO5h/ijdsYPFAbE",
+ }
+ ],
+}
+
+# Control group: a plain message. Same key set, but no quote links.
+PLAIN_MESSAGE = {
+ "id": "ROBOT1.0_DBiKWe9lNhRwiQwghzMwQssT-SFcYlBDrD9xdX4A1hFupGIiqoLgkdtaAy.MKGwqGdR9Jb8mCS5DnjCudEwIjP9OC35zpbTlRPq8A8ILhi0!",
+ "content": "接下来我要发几条测试消息,你忽略就行",
+ "message_scene": {
+ "ext": [
+ "msg_idx=REFIDX_vZ+qxc1KXcODuYUzB/x9XGMfoKf14gEtdgeLHxxNuP3t1pc1n2JSZfwgAS8W12E0INOltlnT1lPyL6yrJZbYWQ",
+ ],
+ "source": "default",
+ },
+ "msg_elements": None,
+}
+
+
+# --- extract_quoted_content -------------------------------------------------
+
+
+def test_extracts_c2c_quoted_message():
+ assert extract_quoted_content(C2C_QUOTING_BOT) == "查完了,两处都没有叫 reference 的东西。"
+
+
+def test_extracts_group_quoted_message():
+ assert extract_quoted_content(GROUP_QUOTING_USER) == "test"
+
+
+def test_plain_message_has_no_quoted_content():
+ """The control group: no ref_msg_idx, so nothing to surface."""
+ assert extract_quoted_content(PLAIN_MESSAGE) is None
+
+
+def test_picks_element_matching_ref_idx_not_the_first():
+ """Elements are matched by index, so extra elements cannot shadow the quote."""
+ payload = {
+ "message_scene": {"ext": ["ref_msg_idx=wanted"]},
+ "msg_elements": [
+ {"content": "unrelated", "msg_idx": "other"},
+ {"content": "the quoted text", "msg_idx": "wanted"},
+ ],
+ }
+ assert extract_quoted_content(payload) == "the quoted text"
+
+
+def test_ref_idx_without_matching_element_returns_none():
+ """Degradation: the quote is announced but the original is not attached."""
+ payload = {
+ "message_scene": {"ext": ["ref_msg_idx=missing"]},
+ "msg_elements": [{"content": "something", "msg_idx": "other"}],
+ }
+ assert extract_quoted_content(payload) is None
+
+
+def test_ref_idx_with_no_elements_returns_none():
+ payload = {"message_scene": {"ext": ["ref_msg_idx=x"]}, "msg_elements": None}
+ assert extract_quoted_content(payload) is None
+
+
+def test_matched_element_without_text_returns_none():
+ payload = {
+ "message_scene": {"ext": ["ref_msg_idx=x"]},
+ "msg_elements": [{"content": " ", "msg_idx": "x"}],
+ }
+ assert extract_quoted_content(payload) is None
+
+
+def test_quoted_content_is_truncated():
+ payload = {
+ "message_scene": {"ext": ["ref_msg_idx=x"]},
+ "msg_elements": [{"content": "大" * (QQ_QUOTED_CONTEXT_MAX_CHARS + 50), "msg_idx": "x"}],
+ }
+ result = extract_quoted_content(payload)
+ assert result is not None
+ assert len(result) == QQ_QUOTED_CONTEXT_MAX_CHARS + len("...")
+ assert result.endswith("...")
+
+
+ "payload",
+ [
+ None,
+ {},
+ {"message_scene": None},
+ {"message_scene": {}},
+ {"message_scene": {"ext": "ref_msg_idx=x"}},
+ {"message_scene": {"ext": ["ref_msg_idx=x"]}, "msg_elements": "not-a-list"},
+ {"message_scene": {"ext": ["ref_msg_idx=x"]}, "msg_elements": ["not-a-mapping"]},
+ ],
+)
+def test_malformed_payloads_return_none(payload):
+ """Unexpected shapes must degrade, never raise."""
+ assert extract_quoted_content(payload) is None
+
+
+# --- _flatten_scene_ext -----------------------------------------------------
+
+
+def test_flatten_scene_ext_splits_on_first_equals_only():
+ """Values (e.g. base64 indexes) may themselves contain '='."""
+ assert _flatten_scene_ext({"ext": ["a=b=c", "plain", "k=v"]}) == {"a": "b=c", "k": "v"}
+
+
+def test_flatten_scene_ext_ignores_non_string_entries():
+ assert _flatten_scene_ext({"ext": [123, None, "k=v"]}) == {"k": "v"}
+
+
+# --- _quoted_content_for ----------------------------------------------------
+
+
+class _FakeMessage:
+ """Duck-typed stand-in for a botpy message object."""
+
+ def __init__(self, message_id: str, **attrs):
+ self.id = message_id
+ for key, value in attrs.items():
+ setattr(self, key, value)
+
+
+def test_quoted_content_prefers_fields_on_the_message_object(monkeypatch):
+ """A botpy that parses these fields makes the payload hook redundant."""
+ message = _FakeMessage(
+ "whatever",
+ message_scene=C2C_QUOTING_BOT["message_scene"],
+ msg_elements=C2C_QUOTING_BOT["msg_elements"],
+ )
+ assert _quoted_content_for(message) == "查完了,两处都没有叫 reference 的东西。"
+
+
+def test_quoted_content_falls_back_to_captured_payload():
+ from nanobot.channels.qq import runtime
+
+ runtime._qq_raw_payloads.clear()
+ runtime._qq_raw_payloads[C2C_QUOTING_BOT["id"]] = C2C_QUOTING_BOT
+ try:
+ assert _quoted_content_for(_FakeMessage(C2C_QUOTING_BOT["id"])) == (
+ "查完了,两处都没有叫 reference 的东西。"
+ )
+ finally:
+ runtime._qq_raw_payloads.clear()
+
+
+def test_quoted_content_is_none_for_unknown_message():
+ from nanobot.channels.qq import runtime
+
+ runtime._qq_raw_payloads.clear()
+ assert _quoted_content_for(_FakeMessage("never-seen")) is None
+
+
+# --- End-to-end: capture -> lookup -> content ------------------------------
+#
+# These exercise the real botpy message classes, so they are skipped wherever
+# the optional QQ dependency is absent (same gate as the other QQ test modules).
+
+from types import SimpleNamespace # noqa: E402
+from unittest.mock import AsyncMock # noqa: E402
+
+from nanobot.bus.queue import MessageBus # noqa: E402
+from nanobot.channels.qq import runtime as qq_runtime # noqa: E402
+from nanobot.channels.qq.runtime import QQChannel, QQConfig # noqa: E402
+
+requires_botpy = pytest.mark.skipif(
+ not getattr(qq_runtime, "QQ_AVAILABLE", False),
+ reason="QQ dependencies not installed (qq-botpy)",
+)
+
+
+def _c2c_message(data: dict) -> SimpleNamespace:
+ return SimpleNamespace(
+ id=data["id"],
+ content=data.get("content") or "",
+ author=SimpleNamespace(user_openid="user1"),
+ attachments=[],
+ )
+
+
+def _group_message(data: dict) -> SimpleNamespace:
+ return SimpleNamespace(
+ id=data["id"],
+ content=data.get("content") or "",
+ group_openid="group123",
+ author=SimpleNamespace(member_openid="user1"),
+ attachments=[],
+ )
+
+
+def captured():
+ """Seed the payload cache the way the botpy hook does, and clean up after."""
+ qq_runtime._qq_raw_payloads.clear()
+ yield qq_runtime._qq_raw_payloads
+ qq_runtime._qq_raw_payloads.clear()
+
+
+@requires_botpy
+def test_capture_hook_keeps_payload_from_real_botpy_class(captured):
+ """botpy drops these fields, so the constructor hook is the only way in."""
+ from botpy import message as botpy_message
+
+ qq_runtime._install_qq_payload_capture()
+ message = botpy_message.C2CMessage(api=None, event_id="ev1", data=C2C_QUOTING_BOT)
+
+ assert captured[C2C_QUOTING_BOT["id"]] == C2C_QUOTING_BOT
+ assert getattr(message, "msg_elements", None) is None, "botpy never parses these"
+ assert _quoted_content_for(message) == "查完了,两处都没有叫 reference 的东西。"
+
+
+@requires_botpy
+async def test_c2c_quote_is_prefixed_onto_content(captured):
+ channel = QQChannel(
+ QQConfig(app_id="app", secret="secret", allow_from=["user1"]), MessageBus()
+ )
+ channel._handle_message = AsyncMock()
+ captured[C2C_QUOTING_BOT["id"]] = C2C_QUOTING_BOT
+
+ await channel._on_message(_c2c_message(C2C_QUOTING_BOT), is_group=False)
+
+ content = channel._handle_message.await_args.kwargs["content"]
+ assert content.startswith("[Reply to: 查完了,两处都没有叫 reference 的东西。]")
+ assert content.endswith("test reference agent")
+
+
+@requires_botpy
+async def test_group_quote_is_prefixed_onto_content(captured):
+ channel = QQChannel(
+ QQConfig(app_id="app", secret="secret", allow_from=["user1"]), MessageBus()
+ )
+ channel._handle_message = AsyncMock()
+ captured[GROUP_QUOTING_USER["id"]] = GROUP_QUOTING_USER
+
+ await channel._on_message(_group_message(GROUP_QUOTING_USER), is_group=True)
+
+ content = channel._handle_message.await_args.kwargs["content"]
+ assert content.startswith("[Reply to: test]")
+ assert content.endswith("test reference user")
+
+
+@requires_botpy
+async def test_plain_message_content_is_unchanged(captured):
+ """Control group: no quote link, so the message must reach the agent as-is."""
+ channel = QQChannel(
+ QQConfig(app_id="app", secret="secret", allow_from=["user1"]), MessageBus()
+ )
+ channel._handle_message = AsyncMock()
+ captured[PLAIN_MESSAGE["id"]] = PLAIN_MESSAGE
+
+ await channel._on_message(_c2c_message(PLAIN_MESSAGE), is_group=False)
+
+ assert channel._handle_message.await_args.kwargs["content"] == PLAIN_MESSAGE["content"]
+
+
+@requires_botpy
+async def test_quote_without_text_is_not_dropped(captured):
+ """A quote-only message carries meaning, so it must survive the empty-content gate."""
+ channel = QQChannel(
+ QQConfig(app_id="app", secret="secret", allow_from=["user1"]), MessageBus()
+ )
+ channel._handle_message = AsyncMock()
+ payload = dict(C2C_QUOTING_BOT, content="")
+ captured[payload["id"]] = payload
+
+ await channel._on_message(_c2c_message(payload), is_group=False)
+
+ content = channel._handle_message.await_args.kwargs["content"]
+ assert content == "[Reply to: 查完了,两处都没有叫 reference 的东西。]"