mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-08-07 01:48:53 +00:00
fix: prevent empty Matrix messages when progress callback sends empty content
Agent-Logs-Url: https://github.com/halldorjanetzko/nanobot/sessions/df528c59-8214-41a0-9b79-9d1d41857107 Co-authored-by: halldorjanetzko <158819146+halldorjanetzko@users.noreply.github.com>
This commit is contained in:
parent
15007afd4a
commit
0284174df9
@ -524,7 +524,7 @@ class MatrixChannel(BaseChannel):
|
|||||||
failures.append(fail)
|
failures.append(fail)
|
||||||
if failures:
|
if failures:
|
||||||
text = f"{text.rstrip()}\n{chr(10).join(failures)}" if text.strip() else "\n".join(failures)
|
text = f"{text.rstrip()}\n{chr(10).join(failures)}" if text.strip() else "\n".join(failures)
|
||||||
if text or not candidates:
|
if text.strip():
|
||||||
content = _build_matrix_text_content(text)
|
content = _build_matrix_text_content(text)
|
||||||
if relates_to:
|
if relates_to:
|
||||||
content["m.relates_to"] = relates_to
|
content["m.relates_to"] = relates_to
|
||||||
|
|||||||
@ -1246,6 +1246,44 @@ async def test_send_progress_keeps_typing_keepalive_running() -> None:
|
|||||||
await channel.stop()
|
await channel.stop()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_send_empty_content_does_not_call_room_send() -> None:
|
||||||
|
"""Progress messages with empty content must not produce an empty body: '' event."""
|
||||||
|
channel = MatrixChannel(_make_config(), MessageBus())
|
||||||
|
client = _FakeAsyncClient("", "", "", None)
|
||||||
|
channel.client = client
|
||||||
|
|
||||||
|
await channel.send(
|
||||||
|
OutboundMessage(
|
||||||
|
channel="matrix",
|
||||||
|
chat_id="!room:matrix.org",
|
||||||
|
content="",
|
||||||
|
metadata={"_progress": True},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
assert client.room_send_calls == []
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_send_whitespace_only_content_does_not_call_room_send() -> None:
|
||||||
|
"""Progress messages with whitespace-only content must not produce an empty message."""
|
||||||
|
channel = MatrixChannel(_make_config(), MessageBus())
|
||||||
|
client = _FakeAsyncClient("", "", "", None)
|
||||||
|
channel.client = client
|
||||||
|
|
||||||
|
await channel.send(
|
||||||
|
OutboundMessage(
|
||||||
|
channel="matrix",
|
||||||
|
chat_id="!room:matrix.org",
|
||||||
|
content=" \n\n ",
|
||||||
|
metadata={"_progress": True},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
assert client.room_send_calls == []
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_send_clears_typing_when_send_fails() -> None:
|
async def test_send_clears_typing_when_send_fails() -> None:
|
||||||
channel = MatrixChannel(_make_config(), MessageBus())
|
channel = MatrixChannel(_make_config(), MessageBus())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user