Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(agent): preserve tool data-block identity during event replay (#2551)
dvd2336642ed61b894
diff --git a/src/agentscope/agent/_agent.py b/src/agentscope/agent/_agent.pyindex 59eade694..b8f110d1e 100644--- a/src/agentscope/agent/_agent.py+++ b/src/agentscope/agent/_agent.py@@ -3910,6 +3910,8 @@ class Agent:yield ToolResultDataDeltaEvent(reply_id=self.state.reply_id,tool_call_id=tool_call_id,+ # Keep the block id so replay groups one payload.+ block_id=block.id,media_type=block.source.media_type,data=block.source.data,)@@ -3917,6 +3919,7 @@ class Agent:yield ToolResultDataDeltaEvent(reply_id=self.state.reply_id,tool_call_id=tool_call_id,+ block_id=block.id,media_type=block.source.media_type,url=str(block.source.url),)diff --git a/src/agentscope/message/_base.py b/src/agentscope/message/_base.pyindex 2fc189d03..ba955251c 100644--- a/src/agentscope/message/_base.py+++ b/src/agentscope/message/_base.py@@ -448,9 +448,33 @@ class Msg(BaseModel):media_type=event.media_type,))- block.output.append(- DataBlock(id=event.block_id, source=src),- )+ # Merge same-id Base64 chunks like+ # ToolResponse.append_chunk.+ merge_target: DataBlock | None = None+ for output_block in block.output:+ if (+ isinstance(output_block, DataBlock)+ and output_block.id == event.block_id+ and isinstance(output_block.source, Base64Source)+ ):+ merge_target = output_block+ break+ if merge_target is not None and isinstance(+ src,+ Base64Source,+ ):+ existing = base64.b64decode(merge_target.source.data)+ incoming = base64.b64decode(src.data)+ merge_target.source.data = base64.b64encode(+ existing + incoming,+ ).decode("ascii")+ merge_target.source.media_type = (+ src.media_type or merge_target.source.media_type+ )+ else:+ block.output.append(+ DataBlock(id=event.block_id, source=src),+ )else:assert isinstance(block, ToolResultBlock)block.state = event.statediff --git a/tests/agent_basic_test.py b/tests/agent_basic_test.pyindex f144861d9..61298f53f 100644--- a/tests/agent_basic_test.py+++ b/tests/agent_basic_test.py@@ -1,5 +1,6 @@# -*- coding: utf-8 -*-"""The basic test of the agent class."""+import base64from typing import Anyfrom unittest.async_case import IsolatedAsyncioTestCase@@ -18,6 +19,9 @@ from agentscope.permission import (PermissionContext,)from agentscope.message import (+ Base64Source,+ DataBlock,+ URLSource,TextBlock,ThinkingBlock,ToolCallBlock,@@ -2128,5 +2132,46 @@ class AgentBasicTest(IsolatedAsyncioTestCase):self.assertListEqual(context_dicts, expected_context)self.assertEqual(self.agent.state.cur_iter, 2)+ async def test_event_block_id_matches_data_block_id(self) -> None:+ """Base64 and URL DataBlocks keep their id on the emitted event."""+ agent = Agent(+ name="test",+ system_prompt="",+ model=MockModel(),+ toolkit=Toolkit(),+ injection_config=InjectionConfig(inject_runtime_state=False),+ )+ chunks = [+ DataBlock(+ id="b64-block",+ source=Base64Source(+ data=base64.b64encode(b"hello").decode("ascii"),+ media_type="audio/wav",+ ),+ ),+ DataBlock(+ id="url-block",+ source=URLSource(+ url="https://example.com/a.wav",+ media_type="audio/wav",+ ),+ ),+ ]+ # pylint: disable=protected-access+ events = [+ event+ async for event in agent._convert_tool_chunk_to_event(+ "tc-1",+ chunks,+ )+ ]+ self.assertListEqual(+ [(type(e).__name__, e.block_id) for e in events],+ [+ ("ToolResultDataDeltaEvent", "b64-block"),+ ("ToolResultDataDeltaEvent", "url-block"),+ ],+ )+async def asyncTearDown(self) -> None:"""The async teardown method."""diff --git a/tests/event_to_message_test.py b/tests/event_to_message_test.pyindex 5985306c2..3fdc7c7c5 100644--- a/tests/event_to_message_test.py+++ b/tests/event_to_message_test.py@@ -22,7 +22,9 @@ Coverage* Wrong reply_id → event silently skipped* Missing block → warning, no crash"""+import base64from typing import Any+from unittest import TestCasefrom unittest.async_case import IsolatedAsyncioTestCasefrom utils import AnyString@@ -54,6 +56,7 @@ from agentscope.event import (UserConfirmResultEvent,)from agentscope.message import (+ AssistantMsg,Msg,ToolCallBlock,ToolResultBlock,@@ -1058,3 +1061,158 @@ class EventToMessageTest(IsolatedAsyncioTestCase):async def asyncTearDown(self) -> None:"""No teardown needed."""+++class ToolResultDataDeltaMergeTest(TestCase):+ """Msg.append_event groups same-id Base64 deltas into one DataBlock."""++ def test_same_block_id_deltas_merge_into_one_block(self) -> None:+ """Same-id deltas merge; the payload decodes to concatenated bytes."""+ msg = AssistantMsg(name="assistant", content=[])+ msg.append_event(+ ToolResultStartEvent(+ reply_id=msg.id,+ tool_call_id="tc-1",+ tool_call_name="audio",+ ),+ )+ for payload in (b"hello", b"world"):+ msg.append_event(+ ToolResultDataDeltaEvent(+ reply_id=msg.id,+ tool_call_id="tc-1",+ block_id="audio-1",+ media_type="audio/wav",+ data=base64.b64encode(payload).decode("ascii"),+ ),+ )+ msg.append_event(+ ToolResultEndEvent(+ reply_id=msg.id,+ tool_call_id="tc-1",+ state="success",+ ),+ )++ tool_blocks = [b for b in msg.content if b.type == "tool_result"]+ self.assertEqual(len(tool_blocks), 1)+ # b"hello" + b"world" -> base64("helloworld") = "aGVsbG93b3JsZA=="+ self.assertListEqual(+ [b.model_dump() for b in tool_blocks[0].output],+ [+ {+ "type": "data",+ "id": "audio-1",+ "source": {+ "type": "base64",+ "data": "aGVsbG93b3JsZA==",+ "media_type": "audio/wav",+ },+ "name": None,+ "created_at": AnyString(),+ "finished_at": None,+ },+ ],+ )++ def test_different_block_ids_stay_separate(self) -> None:+ """Different block ids still produce separate DataBlocks."""+ msg = AssistantMsg(name="assistant", content=[])+ msg.append_event(+ ToolResultStartEvent(+ reply_id=msg.id,+ tool_call_id="tc-1",+ tool_call_name="audio",+ ),+ )+ for block_id, payload in (("a", b"hello"), ("b", b"world")):+ msg.append_event(+ ToolResultDataDeltaEvent(+ reply_id=msg.id,+ tool_call_id="tc-1",+ block_id=block_id,+ media_type="audio/wav",+ data=base64.b64encode(payload).decode("ascii"),+ ),+ )+ msg.append_event(+ ToolResultEndEvent(+ reply_id=msg.id,+ tool_call_id="tc-1",+ state="success",+ ),+ )++ tool_blocks = [b for b in msg.content if b.type == "tool_result"]+ self.assertEqual(len(tool_blocks), 1)+ self.assertListEqual(+ [b.model_dump() for b in tool_blocks[0].output],+ [+ {+ "type": "data",+ "id": "a",+ "source": {+ "type": "base64",+ "data": "aGVsbG8=",+ "media_type": "audio/wav",+ },+ "name": None,+ "created_at": AnyString(),+ "finished_at": None,+ },+ {+ "type": "data",+ "id": "b",+ "source": {+ "type": "base64",+ "data": "d29ybGQ=",+ "media_type": "audio/wav",+ },+ "name": None,+ "created_at": AnyString(),+ "finished_at": None,+ },+ ],+ )++ def test_empty_media_type_does_not_clear_existing_type(self) -> None:+ """An empty media type does not replace a prior non-empty type."""+ msg = AssistantMsg(name="assistant", content=[])+ msg.append_event(+ ToolResultStartEvent(+ reply_id=msg.id,+ tool_call_id="tc-1",+ tool_call_name="audio",+ ),+ )+ for media_type, payload in (+ ("audio/wav", b"hello"),+ ("", b"world"),+ ):+ msg.append_event(+ ToolResultDataDeltaEvent(+ reply_id=msg.id,+ tool_call_id="tc-1",+ block_id="audio-1",+ media_type=media_type,+ data=base64.b64encode(payload).decode("ascii"),+ ),+ )++ self.assertListEqual(+ [block.model_dump() for block in msg.content[0].output],+ [+ {+ "type": "data",+ "id": "audio-1",+ "source": {+ "type": "base64",+ "data": "aGVsbG93b3JsZA==",+ "media_type": "audio/wav",+ },+ "name": None,+ "created_at": AnyString(),+ "finished_at": None,+ },+ ],+ )