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.

fix(backend/copilot-bot): separate AutoPilot text blocks so they don't run together in Discord (#13424)

Bentlyc12060cc63c5
diff --git a/autogpt_platform/backend/backend/copilot/bot/bot_backend.py b/autogpt_platform/backend/backend/copilot/bot/bot_backend.py
index 60e38bb148..492b0f548a 100644
--- a/autogpt_platform/backend/backend/copilot/bot/bot_backend.py
+++ b/autogpt_platform/backend/backend/copilot/bot/bot_backend.py
@@ -369,6 +369,13 @@ class BotBackend:
setup_notified = False
setup_drop_notified = False
+ # Track which text block each delta belongs to. AutoPilot emits text in
+ # separate blocks around tool calls / reasoning (each with its own id);
+ # the frontend renders them as distinct parts, but here we concatenate
+ # into one message, so insert a paragraph break when the block changes —
+ # otherwise the end of one block and the start of the next run together
+ # ("…first thought.second thought…").
+ last_text_block_id: str | None = None
try:
while True:
@@ -388,6 +395,12 @@ class BotBackend:
)
if isinstance(chunk, StreamTextDelta):
if chunk.delta:
+ if (
+ last_text_block_id is not None
+ and chunk.id != last_text_block_id
+ ):
+ yield "\n\n"
+ last_text_block_id = chunk.id
yield chunk.delta
elif isinstance(chunk, StreamToolOutputAvailable):
setup_output = _extract_setup_requirements(chunk.output)
diff --git a/autogpt_platform/backend/backend/copilot/bot/bot_backend_test.py b/autogpt_platform/backend/backend/copilot/bot/bot_backend_test.py
index b67195cef9..f2fc52f008 100644
--- a/autogpt_platform/backend/backend/copilot/bot/bot_backend_test.py
+++ b/autogpt_platform/backend/backend/copilot/bot/bot_backend_test.py
@@ -154,8 +154,9 @@ class TestStreamChat:
api._client.start_chat_turn = AsyncMock(return_value=handle)
queue: asyncio.Queue = asyncio.Queue()
+ # Same block id — a continuous text stream, no separator inserted.
await queue.put(StreamTextDelta(id="1", delta="Hello "))
- await queue.put(StreamTextDelta(id="2", delta="world"))
+ await queue.put(StreamTextDelta(id="1", delta="world"))
await queue.put(StreamFinish())
captured_session_ids: list[str] = []
@@ -185,6 +186,40 @@ class TestStreamChat:
assert "".join(chunks) == "Hello world"
assert captured_session_ids == ["sess"]
+ @pytest.mark.asyncio
+ async def test_inserts_paragraph_break_between_text_blocks(self, api: BotBackend):
+ # AutoPilot emits text in separate blocks around tool calls / reasoning,
+ # each with its own id. Concatenating them without a separator runs the
+ # blocks together ("first thought.second thought"); a paragraph break
+ # keeps them readable, matching the frontend's distinct-part rendering.
+ handle = ChatTurnHandle(session_id="sess", turn_id="turn", user_id="u1")
+ api._client.start_chat_turn = AsyncMock(return_value=handle)
+
+ queue: asyncio.Queue = asyncio.Queue()
+ await queue.put(StreamTextDelta(id="1", delta="first thought."))
+ await queue.put(StreamTextDelta(id="2", delta="second thought."))
+ await queue.put(StreamFinish())
+
+ with (
+ patch(
+ "backend.copilot.bot.bot_backend.stream_registry.subscribe_to_session",
+ new=AsyncMock(return_value=queue),
+ ),
+ patch(
+ "backend.copilot.bot.bot_backend.stream_registry.unsubscribe_from_session",
+ new=AsyncMock(),
+ ),
+ ):
+ chunks: list[str] = []
+ async for chunk in api.stream_chat(
+ platform="discord",
+ platform_user_id="u1",
+ message="hi",
+ ):
+ chunks.append(chunk)
+
+ assert "".join(chunks) == "first thought.\n\nsecond thought."
+
@pytest.mark.asyncio
async def test_surfaces_stream_error(self, api: BotBackend):
handle = ChatTurnHandle(session_id="sess", turn_id="turn", user_id="u1")