Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(backend/copilot): let the chat-platform bot stay silent with NO_REPLY (#14578)

Bentlybdba749ac711
diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.py
index 54a981ba91..ceb2b1f4c0 100644
--- a/autogpt_platform/backend/backend/copilot/baseline/service.py
+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py
@@ -80,6 +80,7 @@ from backend.copilot.pending_messages import (
)
from backend.copilot.prompting import (
SHARED_TOOL_NOTES,
+ get_chat_platform_supplement,
get_delegation_supplement,
get_expert_oversight_supplement,
get_graphiti_supplement,
@@ -1894,6 +1895,9 @@ async def stream_chat_completion_baseline(
team_building_supplement = get_team_building_supplement(
experts_enabled=experts_enabled, expert_id=session.expert_id
)
+ chat_platform_supplement = get_chat_platform_supplement(
+ session.metadata.source_platform
+ )
# Append the builder-session block (graph id+name + full building guide)
# AFTER the shared supplements so the system prompt is byte-identical
# across turns of the same builder session — Claude's prompt cache keeps
@@ -1906,6 +1910,7 @@ async def stream_chat_completion_baseline(
+ delegation_supplement
+ oversight_supplement
+ team_building_supplement
+ + chat_platform_supplement
+ graphiti_supplement
+ builder_session_suffix
+ expert_session_suffix
diff --git a/autogpt_platform/backend/backend/copilot/bot/text_test.py b/autogpt_platform/backend/backend/copilot/bot/text_test.py
index bdbd317891..44b84f728a 100644
--- a/autogpt_platform/backend/backend/copilot/bot/text_test.py
+++ b/autogpt_platform/backend/backend/copilot/bot/text_test.py
@@ -114,7 +114,7 @@ class TestIterChunks:
class TestFormatBatch:
def test_single_message_has_header(self):
result = format_batch([("Bently", "123", "hello")], "discord")
- assert result == "[Message sent by Bently (Discord user ID: 123)]\nhello"
+ assert result.endswith("[Message sent by Bently (Discord user ID: 123)]\nhello")
def test_multi_message_labels_each_sender(self):
result = format_batch(
diff --git a/autogpt_platform/backend/backend/copilot/bot/turn_stream.py b/autogpt_platform/backend/backend/copilot/bot/turn_stream.py
index 2eb44d2134..65d1b2b926 100644
--- a/autogpt_platform/backend/backend/copilot/bot/turn_stream.py
+++ b/autogpt_platform/backend/backend/copilot/bot/turn_stream.py
@@ -14,6 +14,7 @@ from typing import Any
from urllib.parse import quote
from uuid import uuid4
+from backend.copilot.prompting import NO_REPLY
from backend.data.redis_client import get_redis_async
from backend.data.sharing.workspace_refs import (
WorkspaceArtifactLink,
@@ -74,6 +75,11 @@ class DraftStreamer:
preview = text.strip()
if not preview or preview == self._last_text:
return
+ # A reply the model may still be spelling out as NO_REPLY must not
+ # flash as a live preview: the finished reply would then be
+ # suppressed, leaving the user with a draft of a word meant for us.
+ if NO_REPLY.startswith(preview):
+ return
try:
outcome = await self._adapter.send_stream_draft(
self._target_id, self._draft_id, preview
@@ -329,6 +335,22 @@ class TurnStreamer:
pass
await adapter.stop_typing(target_id)
+ if not sent_any_content and buffer.strip() == NO_REPLY:
+ # The model chose silence, as its system prompt allows. This is
+ # not the "didn't produce a response" case below: that exists for
+ # a model that emitted nothing, and this one said so. Whole message
+ # only, and only when nothing was already flushed mid-stream, so a
+ # long reply that happens to contain the word is still delivered.
+ logger.info("Reply suppressed by NO_REPLY for target %s", target_id)
+ self._api.track_event(
+ platform=ctx.platform,
+ event_type="reply_suppressed",
+ server_id=ctx.server_id,
+ channel_type=ctx.channel_type,
+ duration_ms=int((time.monotonic() - started_at) * 1000),
+ )
+ return
+
if buffer.strip():
if await self._send_text_and_artifacts(
adapter, target_id, buffer, ctx, active_session_id
diff --git a/autogpt_platform/backend/backend/copilot/bot/turn_stream_test.py b/autogpt_platform/backend/backend/copilot/bot/turn_stream_test.py
index c3695f80e3..c780a455f4 100644
--- a/autogpt_platform/backend/backend/copilot/bot/turn_stream_test.py
+++ b/autogpt_platform/backend/backend/copilot/bot/turn_stream_test.py
@@ -4,6 +4,8 @@ from unittest.mock import AsyncMock, MagicMock, patch
import pytest
+from backend.copilot.prompting import NO_REPLY
+
from .adapters.base import ChannelType, MessageContext, StreamDraftOutcome
from .turn_stream import DraftStreamer, TurnStreamer, _send_clarification
@@ -171,6 +173,104 @@ class TestStreamBatchDrafts:
assert adapter.send_message.await_args.args[1] == "Hello world"
+class TestNoReply:
+ """A reply of exactly NO_REPLY is the model choosing silence.
+
+ The bot had no way to not answer: an empty reply became "AutoGPT didn't
+ produce a response." Now the bot session's system prompt offers NO_REPLY, and a reply that
+ is exactly that, and nothing else, is not delivered.
+ """
+
+ @pytest.mark.asyncio
+ async def test_exact_no_reply_sends_nothing_and_is_not_the_empty_case(self):
+ adapter = _adapter()
+ api = _api([NO_REPLY])
+ with _patch_redis():
+ await TurnStreamer(api).stream_batch(
+ [("Bently", "user-1", "thanks!")], _ctx(), adapter, "42"
+ )
+ adapter.send_message.assert_not_awaited()
+ adapter.send_link.assert_not_awaited()
+ kinds = [c.kwargs.get("event_type") for c in api.track_event.call_args_list]
+ assert "reply_suppressed" in kinds
+ assert "reply_sent" not in kinds
+ assert not any(
+ c.kwargs.get("error_kind") == "empty_reply"
+ for c in api.track_event.call_args_list
+ ), "silence on purpose is not a stream error"
+
+ @pytest.mark.asyncio
+ async def test_surrounding_whitespace_still_counts(self):
+ adapter = _adapter()
+ api = _api(["\n ", NO_REPLY, "\n"])
+ with _patch_redis():
+ await TurnStreamer(api).stream_batch(
+ [("Bently", "user-1", "ok")], _ctx(), adapter, "42"
+ )
+ adapter.send_message.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_streamed_in_pieces_is_still_suppressed_and_never_previewed(self):
+ adapter = _adapter(drafts=True)
+ api = _api(["NO", "_RE", "PLY"])
+ with _patch_redis():
+ await TurnStreamer(api).stream_batch(
+ [("Bently", "user-1", "ok")], _ctx(), adapter, "42"
+ )
+ adapter.send_message.assert_not_awaited()
+ adapter.send_stream_draft.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_a_reply_that_merely_contains_the_word_is_delivered(self):
+ """Toran's false-positive concern: whole message only."""
+ adapter = _adapter()
+ text = f"{NO_REPLY} is what I'd say if this needed nothing, but it does."
+ api = _api([text])
+ with _patch_redis():
+ await TurnStreamer(api).stream_batch(
+ [("Bently", "user-1", "hi")], _ctx(), adapter, "42"
+ )
+ adapter.send_message.assert_awaited_once()
+ assert adapter.send_message.await_args.args[1] == text
+
+ @pytest.mark.asyncio
+ async def test_it_is_case_sensitive(self):
+ adapter = _adapter()
+ api = _api(["no_reply"])
+ with _patch_redis():
+ await TurnStreamer(api).stream_batch(
+ [("Bently", "user-1", "hi")], _ctx(), adapter, "42"
+ )
+ adapter.send_message.assert_awaited_once()
+ assert adapter.send_message.await_args.args[1] == "no_reply"
+
+ @pytest.mark.asyncio
+ async def test_after_a_mid_stream_flush_the_word_is_delivered(self):
+ """Once part of a long reply has already gone out, a trailing NO_REPLY
+ is content, not a decision: the user has already been answered."""
+ adapter = _adapter()
+ adapter.chunk_flush_at = 20
+ api = _api(["a paragraph long enough to flush.\n\n", NO_REPLY])
+ with _patch_redis():
+ await TurnStreamer(api).stream_batch(
+ [("Bently", "user-1", "hi")], _ctx(), adapter, "42"
+ )
+ sent = [c.args[1] for c in adapter.send_message.await_args_list]
+ assert any(NO_REPLY in s for s in sent)
+
+ @pytest.mark.asyncio
+ async def test_an_empty_reply_still_gets_the_fallback(self):
+ """The old behaviour for a model that said nothing at all is kept."""
+ adapter = _adapter()
+ api = _api([])
+ with _patch_redis():
+ await TurnStreamer(api).stream_batch(
+ [("Bently", "user-1", "hi")], _ctx(), adapter, "42"
+ )
+ adapter.send_message.assert_awaited_once()
+ assert "didn't produce a response" in adapter.send_message.await_args.args[1]
+
+
# -- Native choice buttons: the branch the feature is named after --
diff --git a/autogpt_platform/backend/backend/copilot/prompting.py b/autogpt_platform/backend/backend/copilot/prompting.py
index fbe84d7dff..255c8dc0fa 100644
--- a/autogpt_platform/backend/backend/copilot/prompting.py
+++ b/autogpt_platform/backend/backend/copilot/prompting.py
@@ -660,6 +660,36 @@ def get_sdk_supplement(use_e2b: bool) -> str:
return base + _USER_FOLLOW_UP_NOTE
+# The one reply a chat-platform bot does not deliver. A message on Discord,
+# Slack, Telegram or Teams can genuinely need no answer: an acknowledgement,
+# two humans talking in a thread the bot is subscribed to, a bare ping. Whole
+# message, exact case: a reply that merely contains the word is delivered.
+NO_REPLY = "NO_REPLY"
+
+
+def get_chat_platform_supplement(source_platform: str | None) -> str:
+ """The silence rule, appended only for sessions that a chat bot opened.
+
+ Lives in the system prompt rather than the per-turn message so the web
+ chat view of a linked session shows what the person typed and nothing
+ else. Gated on the session's source platform: a web session has none,
+ and there a human is waiting, so silence would be a bug. Constant across
+ every bot session, so the prompt cache stays warm across them.
+ """
+ if not source_platform:
+ return ""
+ return f"""
+
+### Staying silent
+You are answering through a chat platform, where not every message needs a
+reply: an acknowledgement, a message not addressed to you, people talking to
+each other in a thread you are in. When a message needs no response from you,
+reply with exactly `{NO_REPLY}` as your entire message and nothing else, and
+nothing will be posted. Otherwise answer normally. You may use tools first to
+decide. Never write `{NO_REPLY}` inside a real reply.
+"""
+
+
def get_delegation_supplement() -> str:
"""Delegation rules, appended only when the expert-team tools are enabled.
diff --git a/autogpt_platform/backend/backend/copilot/prompting_test.py b/autogpt_platform/backend/backend/copilot/prompting_test.py
index ff349a4587..ab81523161 100644
--- a/autogpt_platform/backend/backend/copilot/prompting_test.py
+++ b/autogpt_platform/backend/backend/copilot/prompting_test.py
@@ -140,6 +140,28 @@ class TestTeamBuildingSupplement:
assert "hire_expert" not in result
+class TestChatPlatformSupplement:
+ """The silence rule belongs to sessions a chat bot opened, and to no
+ others: on the web a human is waiting, and silence there is a bug."""
+
+ def test_a_web_session_gets_nothing(self):
+ assert prompting.get_chat_platform_supplement(None) == ""
+ assert prompting.get_chat_platform_supplement("") == ""
+
+ def test_every_bot_platform_gets_the_same_rule(self):
+ rules = {
+ prompting.get_chat_platform_supplement(p)
+ for p in ("discord", "slack", "telegram", "teams")
+ }
+ assert len(rules) == 1, "one string, so the prompt cache is shared"
+ rule = rules.pop()
+ assert f"exactly `{prompting.NO_REPLY}` as your entire message" in rule
+ assert "Otherwise answer normally" in rule
+
+ def test_the_word_is_exact_and_case_sensitive(self):
+ assert prompting.NO_REPLY == "NO_REPLY"
+
+
class TestExpertOversightSupplement:
"""The chat-reading tools are in the ``expert_admin`` group, so only an
Otto session with the team flag on can call them — a turn that
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index ac4df0cfbd..18b2ec7a42 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -117,6 +117,7 @@ from ..permissions import (
apply_tool_permissions,
)
from ..prompting import (
+ get_chat_platform_supplement,
get_delegation_supplement,
get_expert_oversight_supplement,
get_team_building_supplement,
@@ -1745,6 +1746,7 @@ async def _apply_building_mode_restart(
+ delegation_supplement
+ oversight_supplement
+ team_building_supplement
+ + get_chat_platform_supplement(session.metadata.source_platform)
+ graphiti_supplement
+ building_suffix
+ expert_session_suffix
@@ -4812,6 +4814,9 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
team_building_supplement = get_team_building_supplement(
experts_enabled=experts_enabled, expert_id=session.expert_id
)
+ chat_platform_supplement = get_chat_platform_supplement(
+ session.metadata.source_platform
+ )
# Append the builder-session block (graph id+name + full building
# guide) AFTER the shared supplements so the system prompt is
# byte-identical across turns of the same builder session — Claude's
@@ -4829,6 +4834,7 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
+ delegation_supplement
+ oversight_supplement
+ team_building_supplement
+ + chat_platform_supplement
+ graphiti_supplement
+ builder_session_suffix
+ expert_session_suffix