feat(matrix): make non-threaded reply quoting optional
Add replyToMessage, enabled by default, so deployments can turn off automatic room-level reply quotes without changing thread relations or streaming replacements. Expose the setting in the channel contract and all locales, document it, and cover both settings in the existing send tests. Co-authored-by: Orrin Witt <orrin.witt@gmail.com>
This commit is contained in:
1 parent
4e77d9c3f2
commit
c5cee9f204
15 files changed
+134
-30
No files matched your search
@@ -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. |
|
||||
|
||||
@@ -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),
|
||||
},
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -68,6 +68,13 @@
|
||||
"false": "Off"
|
||||
}
|
||||
},
|
||||
"replyToMessage": {
|
||||
"label": "Quote replies",
|
||||
"choices": {
|
||||
"true": "On",
|
||||
"false": "Off"
|
||||
}
|
||||
},
|
||||
"streaming": {
|
||||
"label": "Streaming",
|
||||
"choices": {
|
||||
|
||||
@@ -68,6 +68,13 @@
|
||||
"false": "Apagado"
|
||||
}
|
||||
},
|
||||
"replyToMessage": {
|
||||
"label": "Citar al responder",
|
||||
"choices": {
|
||||
"true": "Encendido",
|
||||
"false": "Apagado"
|
||||
}
|
||||
},
|
||||
"streaming": {
|
||||
"label": "Transmisión",
|
||||
"choices": {
|
||||
|
||||
@@ -68,6 +68,13 @@
|
||||
"false": "Désactivé"
|
||||
}
|
||||
},
|
||||
"replyToMessage": {
|
||||
"label": "Citer le message",
|
||||
"choices": {
|
||||
"true": "Activé",
|
||||
"false": "Désactivé"
|
||||
}
|
||||
},
|
||||
"streaming": {
|
||||
"label": "Diffusion en continu",
|
||||
"choices": {
|
||||
|
||||
@@ -68,6 +68,13 @@
|
||||
"false": "Mati"
|
||||
}
|
||||
},
|
||||
"replyToMessage": {
|
||||
"label": "Kutip pesan",
|
||||
"choices": {
|
||||
"true": "Aktif",
|
||||
"false": "Mati"
|
||||
}
|
||||
},
|
||||
"streaming": {
|
||||
"label": "Streaming",
|
||||
"choices": {
|
||||
|
||||
@@ -68,6 +68,13 @@
|
||||
"false": "オフ"
|
||||
}
|
||||
},
|
||||
"replyToMessage": {
|
||||
"label": "引用返信",
|
||||
"choices": {
|
||||
"true": "オン",
|
||||
"false": "オフ"
|
||||
}
|
||||
},
|
||||
"streaming": {
|
||||
"label": "ストリーミング",
|
||||
"choices": {
|
||||
|
||||
@@ -68,6 +68,13 @@
|
||||
"false": "끄기"
|
||||
}
|
||||
},
|
||||
"replyToMessage": {
|
||||
"label": "인용 답장",
|
||||
"choices": {
|
||||
"true": "켜기",
|
||||
"false": "끄기"
|
||||
}
|
||||
},
|
||||
"streaming": {
|
||||
"label": "스트리밍",
|
||||
"choices": {
|
||||
|
||||
@@ -68,6 +68,13 @@
|
||||
"false": "Desligado"
|
||||
}
|
||||
},
|
||||
"replyToMessage": {
|
||||
"label": "Citar ao responder",
|
||||
"choices": {
|
||||
"true": "Ligado",
|
||||
"false": "Desligado"
|
||||
}
|
||||
},
|
||||
"streaming": {
|
||||
"label": "Transmissão",
|
||||
"choices": {
|
||||
|
||||
@@ -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": {
|
||||
|
||||
@@ -68,6 +68,13 @@
|
||||
"false": "关闭"
|
||||
}
|
||||
},
|
||||
"replyToMessage": {
|
||||
"label": "引用回复",
|
||||
"choices": {
|
||||
"true": "开启",
|
||||
"false": "关闭"
|
||||
}
|
||||
},
|
||||
"streaming": {
|
||||
"label": "流式输出",
|
||||
"choices": {
|
||||
|
||||
@@ -68,6 +68,13 @@
|
||||
"false": "關閉"
|
||||
}
|
||||
},
|
||||
"replyToMessage": {
|
||||
"label": "引用回覆",
|
||||
"choices": {
|
||||
"true": "開啟",
|
||||
"false": "關閉"
|
||||
}
|
||||
},
|
||||
"streaming": {
|
||||
"label": "串流輸出",
|
||||
"choices": {
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in new issue
Block a user