diff --git a/docs/chat-apps.md b/docs/chat-apps.md index f90f3934c..297599f00 100644 --- a/docs/chat-apps.md +++ b/docs/chat-apps.md @@ -391,6 +391,7 @@ nanobot plugins enable matrix | `groupPolicy` | `open` (default), `mention`, `allowlist`, or `listen`. | | `groupAllowFrom` | Room allowlist (used when policy is `allowlist`). | | `allowRoomMentions` | Accept `@room` mentions in mention mode. | +| `replyToMessage` | Quote the user's message in non-threaded replies (default `true`). Set `false` to send without quotes; thread replies are unchanged. | | `e2eeEnabled` | E2EE support (default `true`). Set `false` for plaintext-only. | | `sasVerification` | Complete Element-initiated SAS device verification for allowed users (default `false`). This does not add cross-signing, clear Element's cross-signing trust warning, or let the bot initiate verification. | | `maxMediaBytes` | Max attachment size (default `20MB`). Set `0` to block all media. | diff --git a/nanobot/channels/matrix/manifest.py b/nanobot/channels/matrix/manifest.py index 902e1b883..d7c875d13 100644 --- a/nanobot/channels/matrix/manifest.py +++ b/nanobot/channels/matrix/manifest.py @@ -23,6 +23,7 @@ SETUP_SPEC = ChannelSetupSpec( "maxConcurrentMediaDownloads": field("int", default=2), "groupAllowFrom": field("list"), "allowRoomMentions": field("bool", default=False), + "replyToMessage": field("bool", default=True), "streaming": field("bool", default=False), "useProxy": field("bool", default=False), }, diff --git a/nanobot/channels/matrix/runtime.py b/nanobot/channels/matrix/runtime.py index 730d32e59..caa05048f 100644 --- a/nanobot/channels/matrix/runtime.py +++ b/nanobot/channels/matrix/runtime.py @@ -318,6 +318,7 @@ class MatrixConfig(Base): group_policy: MatrixGroupPolicy = "open" group_allow_from: list[str] = Field(default_factory=list) allow_room_mentions: bool = False + reply_to_message: bool = True streaming: bool = False use_proxy: bool = False @@ -1282,19 +1283,13 @@ class MatrixChannel(BaseChannel): return {"rel_type": "m.thread", "event_id": root_id, "m.in_reply_to": {"event_id": reply_to}, "is_falling_back": True} - @classmethod def _build_outbound_relates_to( - cls, metadata: dict[str, Any] | None + self, metadata: dict[str, Any] | None ) -> dict[str, Any] | None: - """Build Matrix relation metadata for outbound messages. - - Threaded turns keep the existing ``m.thread`` relation. Non-thread - turns attach a plain ``m.in_reply_to`` so room-level responses link - back to the user event that started the agent turn (issue #5274). - """ - if thread := cls._build_thread_relates_to(metadata): + """Keep thread relations; optionally link room-level replies to the user event.""" + if thread := self._build_thread_relates_to(metadata): return thread - if not metadata: + if not self.config.reply_to_message or not metadata: return None reply_to = metadata.get("message_id") or metadata.get("event_id") if not isinstance(reply_to, str) or not reply_to: diff --git a/nanobot/channels/matrix/tests/test_matrix_channel.py b/nanobot/channels/matrix/tests/test_matrix_channel.py index 72080ec7f..40449e860 100644 --- a/nanobot/channels/matrix/tests/test_matrix_channel.py +++ b/nanobot/channels/matrix/tests/test_matrix_channel.py @@ -36,6 +36,14 @@ def test_default_e2ee_matches_platform_support() -> None: assert MatrixConfig().e2ee_enabled is (sys.platform != "win32") +def test_reply_to_message_config() -> None: + assert MatrixConfig().reply_to_message is True + assert MatrixChannel.default_config()["replyToMessage"] is True + config = MatrixConfig.model_validate({"replyToMessage": False}) + assert config.reply_to_message is False + assert config.model_dump(by_alias=True)["replyToMessage"] is False + + class _DummyTask: def __init__(self) -> None: self.cancelled = False @@ -1756,8 +1764,9 @@ async def test_on_message_sets_message_id_for_room_level_event() -> None: {"event_id": "$user1"}, {"message_id": "$user1", "event_id": "$older"}, ]) -async def test_send_adds_in_reply_to_for_room_level_message_id(metadata) -> None: - channel = MatrixChannel(_make_config(), MessageBus()) +@pytest.mark.parametrize("reply_to_message", [True, False]) +async def test_send_room_level_reply_respects_config(metadata, reply_to_message) -> None: + channel = MatrixChannel(_make_config(reply_to_message=reply_to_message), MessageBus()) client = _FakeAsyncClient("", "", "", None) channel.client = client @@ -1771,7 +1780,11 @@ async def test_send_adds_in_reply_to_for_room_level_message_id(metadata) -> None ) content = client.room_send_calls[0]["content"] - assert content["m.relates_to"] == {"m.in_reply_to": {"event_id": "$user1"}} + assert content["body"] == "Hi" + if reply_to_message: + assert content["m.relates_to"] == {"m.in_reply_to": {"event_id": "$user1"}} + else: + assert "m.relates_to" not in content @pytest.mark.asyncio @@ -1790,8 +1803,11 @@ async def test_send_without_valid_reply_target_stays_top_level(metadata) -> None @pytest.mark.asyncio @pytest.mark.parametrize("encrypted", [False, True]) -async def test_send_room_reply_attachment_keeps_source_event(tmp_path, encrypted) -> None: - channel = MatrixChannel(_make_config(e2ee_enabled=encrypted), MessageBus()) +@pytest.mark.parametrize("reply_to_message", [True, False]) +async def test_send_room_reply_attachment_respects_config(tmp_path, encrypted, reply_to_message) -> None: + channel = MatrixChannel( + _make_config(e2ee_enabled=encrypted, reply_to_message=reply_to_message), MessageBus(), + ) client = _FakeAsyncClient("", "", "", None) client.rooms["!room:matrix.org"] = SimpleNamespace(encrypted=encrypted) channel.client = client @@ -1806,12 +1822,16 @@ async def test_send_room_reply_attachment_keeps_source_event(tmp_path, encrypted assert client.upload_calls[0]["encrypt"] is encrypted assert len(client.room_send_calls) == 2 for call in client.room_send_calls: - assert call["content"]["m.relates_to"] == {"m.in_reply_to": {"event_id": "$user"}} + if reply_to_message: + assert call["content"]["m.relates_to"] == {"m.in_reply_to": {"event_id": "$user"}} + else: + assert "m.relates_to" not in call["content"] @pytest.mark.asyncio -async def test_send_prefers_thread_relation_over_plain_reply() -> None: - channel = MatrixChannel(_make_config(), MessageBus()) +@pytest.mark.parametrize("reply_to_message", [True, False]) +async def test_send_prefers_thread_relation_over_plain_reply(reply_to_message) -> None: + channel = MatrixChannel(_make_config(reply_to_message=reply_to_message), MessageBus()) client = _FakeAsyncClient("", "", "", None) channel.client = client @@ -1889,8 +1909,9 @@ async def test_send_does_not_parse_attachment_marker_without_media(tmp_path) -> @pytest.mark.asyncio -async def test_send_passes_thread_relates_to_to_attachment_upload(monkeypatch) -> None: - channel = MatrixChannel(_make_config(), MessageBus()) +@pytest.mark.parametrize("reply_to_message", [True, False]) +async def test_send_passes_thread_relates_to_to_attachment_upload(monkeypatch, reply_to_message) -> None: + channel = MatrixChannel(_make_config(reply_to_message=reply_to_message), MessageBus()) client = _FakeAsyncClient("", "", "", None) channel.client = client channel._server_upload_limit_checked = True @@ -2558,8 +2579,11 @@ async def test_send_delta_starts_threaded_stream_inside_thread() -> None: @pytest.mark.asyncio -async def test_send_delta_threaded_edit_keeps_replace_and_thread_relation(monkeypatch) -> None: - channel = MatrixChannel(_make_config(), MessageBus()) +@pytest.mark.parametrize("reply_to_message", [True, False]) +async def test_send_delta_threaded_edit_keeps_replace_and_thread_relation( + monkeypatch, reply_to_message, +) -> None: + channel = MatrixChannel(_make_config(reply_to_message=reply_to_message), MessageBus()) client = _FakeAsyncClient("", "", "", None) channel.client = client client.room_send_response.event_id = "event-1" @@ -2579,6 +2603,9 @@ async def test_send_delta_threaded_edit_keeps_replace_and_thread_relation(monkey edit_content = client.room_send_calls[1]["content"] final_content = client.room_send_calls[2]["content"] + assert client.room_send_calls[0]["content"]["m.relates_to"] == ( + edit_content["m.new_content"]["m.relates_to"] + ) assert edit_content["m.relates_to"] == { "rel_type": "m.replace", "event_id": "event-1", @@ -2602,8 +2629,9 @@ async def test_send_delta_threaded_edit_keeps_replace_and_thread_relation(monkey @pytest.mark.asyncio -async def test_send_delta_room_reply_edits_the_bot_event(monkeypatch) -> None: - channel = MatrixChannel(_make_config(), MessageBus()) +@pytest.mark.parametrize("reply_to_message", [True, False]) +async def test_send_delta_room_reply_edits_the_bot_event(monkeypatch, reply_to_message) -> None: + channel = MatrixChannel(_make_config(reply_to_message=reply_to_message), MessageBus()) client = _FakeAsyncClient("", "", "", None) channel.client = client client.room_send_response.event_id = "$bot" @@ -2616,17 +2644,23 @@ async def test_send_delta_room_reply_edits_the_bot_event(monkeypatch) -> None: await channel.send_delta("!source:example.org", "", metadata, stream_end=True) assert len(client.room_send_calls) == 3 - assert client.room_send_calls[0]["content"]["m.relates_to"] == { - "m.in_reply_to": {"event_id": "$user"}, - } + initial_content = client.room_send_calls[0]["content"] + assert initial_content["body"] == "Hello" + if reply_to_message: + assert initial_content["m.relates_to"] == {"m.in_reply_to": {"event_id": "$user"}} + else: + assert "m.relates_to" not in initial_content for call in client.room_send_calls[1:]: assert call["content"]["m.relates_to"] == { "rel_type": "m.replace", "event_id": "$bot", } assert call["content"]["m.new_content"]["body"] == "Hello world" - assert call["content"]["m.new_content"]["m.relates_to"] == { - "m.in_reply_to": {"event_id": "$user"}, - } + if reply_to_message: + assert call["content"]["m.new_content"]["m.relates_to"] == { + "m.in_reply_to": {"event_id": "$user"}, + } + else: + assert "m.relates_to" not in call["content"]["m.new_content"] @pytest.mark.asyncio diff --git a/nanobot/channels/matrix/webui/locales/en.json b/nanobot/channels/matrix/webui/locales/en.json index 629953e9d..e75cb14ca 100644 --- a/nanobot/channels/matrix/webui/locales/en.json +++ b/nanobot/channels/matrix/webui/locales/en.json @@ -68,6 +68,13 @@ "false": "Off" } }, + "replyToMessage": { + "label": "Quote replies", + "choices": { + "true": "On", + "false": "Off" + } + }, "streaming": { "label": "Streaming", "choices": { diff --git a/nanobot/channels/matrix/webui/locales/es.json b/nanobot/channels/matrix/webui/locales/es.json index e9d6695f6..082f565ed 100644 --- a/nanobot/channels/matrix/webui/locales/es.json +++ b/nanobot/channels/matrix/webui/locales/es.json @@ -68,6 +68,13 @@ "false": "Apagado" } }, + "replyToMessage": { + "label": "Citar al responder", + "choices": { + "true": "Encendido", + "false": "Apagado" + } + }, "streaming": { "label": "Transmisión", "choices": { diff --git a/nanobot/channels/matrix/webui/locales/fr.json b/nanobot/channels/matrix/webui/locales/fr.json index 231ad3234..f880e409a 100644 --- a/nanobot/channels/matrix/webui/locales/fr.json +++ b/nanobot/channels/matrix/webui/locales/fr.json @@ -68,6 +68,13 @@ "false": "Désactivé" } }, + "replyToMessage": { + "label": "Citer le message", + "choices": { + "true": "Activé", + "false": "Désactivé" + } + }, "streaming": { "label": "Diffusion en continu", "choices": { diff --git a/nanobot/channels/matrix/webui/locales/id.json b/nanobot/channels/matrix/webui/locales/id.json index a5f00ee4e..145d74dac 100644 --- a/nanobot/channels/matrix/webui/locales/id.json +++ b/nanobot/channels/matrix/webui/locales/id.json @@ -68,6 +68,13 @@ "false": "Mati" } }, + "replyToMessage": { + "label": "Kutip pesan", + "choices": { + "true": "Aktif", + "false": "Mati" + } + }, "streaming": { "label": "Streaming", "choices": { diff --git a/nanobot/channels/matrix/webui/locales/ja.json b/nanobot/channels/matrix/webui/locales/ja.json index 94087132e..c65b21e6a 100644 --- a/nanobot/channels/matrix/webui/locales/ja.json +++ b/nanobot/channels/matrix/webui/locales/ja.json @@ -68,6 +68,13 @@ "false": "オフ" } }, + "replyToMessage": { + "label": "引用返信", + "choices": { + "true": "オン", + "false": "オフ" + } + }, "streaming": { "label": "ストリーミング", "choices": { diff --git a/nanobot/channels/matrix/webui/locales/ko.json b/nanobot/channels/matrix/webui/locales/ko.json index f49752d02..5fc72daa3 100644 --- a/nanobot/channels/matrix/webui/locales/ko.json +++ b/nanobot/channels/matrix/webui/locales/ko.json @@ -68,6 +68,13 @@ "false": "끄기" } }, + "replyToMessage": { + "label": "인용 답장", + "choices": { + "true": "켜기", + "false": "끄기" + } + }, "streaming": { "label": "스트리밍", "choices": { diff --git a/nanobot/channels/matrix/webui/locales/pt-BR.json b/nanobot/channels/matrix/webui/locales/pt-BR.json index 0bf0a55a5..12a2b0715 100644 --- a/nanobot/channels/matrix/webui/locales/pt-BR.json +++ b/nanobot/channels/matrix/webui/locales/pt-BR.json @@ -68,6 +68,13 @@ "false": "Desligado" } }, + "replyToMessage": { + "label": "Citar ao responder", + "choices": { + "true": "Ligado", + "false": "Desligado" + } + }, "streaming": { "label": "Transmissão", "choices": { diff --git a/nanobot/channels/matrix/webui/locales/vi.json b/nanobot/channels/matrix/webui/locales/vi.json index d4b07854a..63d1968d7 100644 --- a/nanobot/channels/matrix/webui/locales/vi.json +++ b/nanobot/channels/matrix/webui/locales/vi.json @@ -68,6 +68,13 @@ "false": "Tắt" } }, + "replyToMessage": { + "label": "Trả lời trích dẫn", + "choices": { + "true": "Bật", + "false": "Tắt" + } + }, "streaming": { "label": "Truyền phát", "choices": { diff --git a/nanobot/channels/matrix/webui/locales/zh-CN.json b/nanobot/channels/matrix/webui/locales/zh-CN.json index b4a7799fa..b62a376cc 100644 --- a/nanobot/channels/matrix/webui/locales/zh-CN.json +++ b/nanobot/channels/matrix/webui/locales/zh-CN.json @@ -68,6 +68,13 @@ "false": "关闭" } }, + "replyToMessage": { + "label": "引用回复", + "choices": { + "true": "开启", + "false": "关闭" + } + }, "streaming": { "label": "流式输出", "choices": { diff --git a/nanobot/channels/matrix/webui/locales/zh-TW.json b/nanobot/channels/matrix/webui/locales/zh-TW.json index 0334b56bb..76d8d1a41 100644 --- a/nanobot/channels/matrix/webui/locales/zh-TW.json +++ b/nanobot/channels/matrix/webui/locales/zh-TW.json @@ -68,6 +68,13 @@ "false": "關閉" } }, + "replyToMessage": { + "label": "引用回覆", + "choices": { + "true": "開啟", + "false": "關閉" + } + }, "streaming": { "label": "串流輸出", "choices": { diff --git a/tests/channels/test_channel_setup.py b/tests/channels/test_channel_setup.py index 1da3fea37..ff3d4c7a1 100644 --- a/tests/channels/test_channel_setup.py +++ b/tests/channels/test_channel_setup.py @@ -118,6 +118,9 @@ def test_channel_setup_spec_separates_writable_and_snapshot_fields() -> None: assert discord is not None assert "allowFrom" in matrix.route_field_types assert "allowFrom" in matrix.snapshot_fields + assert matrix.route_field_types["replyToMessage"] == "bool" + assert "replyToMessage" in matrix.snapshot_fields + assert matrix.fields["replyToMessage"].default is True assert "allowFrom" in discord.route_field_types assert "allowFrom" not in discord.snapshot_fields