Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(tool): group streamed data blocks by id and type in append_chunk (#3457)
Wilson_Jiang961df6d2188f
diff --git a/src/agentscope/tool/_response.py b/src/agentscope/tool/_response.pyindex c77e5dde2..e950f631d 100644--- a/src/agentscope/tool/_response.py+++ b/src/agentscope/tool/_response.py@@ -75,62 +75,32 @@ class ToolResponse(BaseModel):"""Append a tool chunk to the current tool response, accumulate thedata blocks and update the state and metadata."""- # Update content blocks- current_ids_to_index = {- _.id: index for index, _ in enumerate(self.content)+ # Only Base64 fragments can continue an earlier block by ID.+ streams = {+ block.id: block+ for block in self.content+ if block.type == "data" and block.source.type == "base64"}- for chunk_block in chunk.content:- if chunk_block.id in current_ids_to_index:- # Append to the existing block- target_block = self.content[- current_ids_to_index[chunk_block.id]- ]-- if isinstance(target_block, TextBlock) and isinstance(- chunk_block,- TextBlock,- ):- target_block.text += chunk_block.text- elif isinstance(target_block, DataBlock) and isinstance(- chunk_block,- DataBlock,- ):- if isinstance(- target_block.source,- Base64Source,- ) and isinstance(chunk_block.source, Base64Source):- # Accumulate independently encoded base64 chunks.- target_block.source.data = _merge_base64_chunks(- target_block.source.data,- chunk_block.source.data,- )- # Update the newest media type and name if provided- target_block.name = (- chunk_block.name or target_block.name- )- target_block.source.media_type = (- chunk_block.source.media_type- or target_block.source.media_type- )- else:- raise ValueError(- "Cannot append DataBlock with URL source or "- f"different source types: {target_block.source} "- f"vs {chunk_block.source}",- )- else:- # For different block types with the same ID, we just- # append the new block with a new ID to avoid the conflict- new_chunk_block = chunk_block.model_copy(deep=True)- new_chunk_block.id = _generate_id()- self.content.append(new_chunk_block)-+ for block in chunk.content:+ if block.type == "text" or block.source.type == "url":+ self.content.append(block.model_copy(deep=True))+ continue++ target = streams.get(block.id)+ if target is None:+ target = block.model_copy(deep=True)+ self.content.append(target)+ streams[block.id] = targetelse:- # Append a copy to avoid modifying the original chunk- self.content.append(chunk_block.model_copy(deep=True))-- # Update the index mapping for the new block- current_ids_to_index[chunk_block.id] = len(self.content) - 1+ assert isinstance(target.source, Base64Source)+ target.source.data = _merge_base64_chunks(+ target.source.data,+ block.source.data,+ )+ target.name = block.name or target.name+ target.source.media_type = (+ block.source.media_type or target.source.media_type+ )# Update id, state and metadata# Only reserve the failure state and keep the previous state if notdiff --git a/tests/toolkit_test.py b/tests/toolkit_test.pyindex 32a4b0a8b..1a3cfe068 100644--- a/tests/toolkit_test.py+++ b/tests/toolkit_test.py@@ -9,15 +9,19 @@ from unittest.async_case import IsolatedAsyncioTestCasefrom pydantic import BaseModel, Field-from utils import AnyString+from utils import AnyString, MockModel+from agentscope.agent import Agent+from agentscope.event import ToolResultStartEventfrom agentscope.mcp import HttpMCPConfig, MCPClientfrom agentscope.state import AgentStatefrom agentscope.message import (+ AssistantMsg,TextBlock,ToolCallBlock,DataBlock,Base64Source,+ URLSource,)from agentscope.tool import (Toolkit,@@ -451,7 +455,7 @@ class ToolkitTest(IsolatedAsyncioTestCase):async def test_tool_response_merges_base64_chunks_by_bytes(self) -> None:"""Base64 chunks with padding should merge as bytes, not strings."""- response = ToolResponse()+ response = ToolResponse(id="tool")first = base64.b64encode(b"hello").decode("ascii")second = base64.b64encode(b"world").decode("ascii")@@ -482,13 +486,113 @@ class ToolkitTest(IsolatedAsyncioTestCase):),)- self.assertEqual(len(response.content), 1)- merged = response.content[0]- self.assertIsInstance(merged, DataBlock)self.assertEqual(- base64.b64decode(merged.source.data),- b"helloworld",+ response.model_dump(),+ {+ "id": "tool",+ "state": "success",+ "metadata": {},+ "content": [+ {+ "type": "data",+ "id": "image",+ "source": {+ "type": "base64",+ "media_type": "image/png",+ "data": base64.b64encode(b"helloworld").decode(),+ },+ "name": None,+ "created_at": AnyString(),+ "finished_at": None,+ },+ ],+ },+ )++ async def test_tool_response_groups_streamed_blocks(self) -> None:+ """The response and event replay preserve interleaved tool output."""++ class CustomDataBlock(DataBlock):+ """A tool-provided data block subclass."""++ text = TextBlock(id="s", text="progress")+ before = TextBlock(id="s", text="before")+ after = TextBlock(id="s", text="after")+ first = CustomDataBlock(+ id="s",+ source=Base64Source(data="YQ==", media_type="audio/wav"),+ )+ second = DataBlock(+ id="s",+ source=Base64Source(data="Yg==", media_type="audio/wav"),)+ urls = [+ DataBlock(+ id="s",+ source=URLSource(url=url, media_type="image/png"),+ )+ for url in ("https://example.org/one", "https://example.org/two")+ ]+ blocks = [text, urls[0], first, before, urls[1], second, after]+ expected: dict[str, Any] = {+ "id": "tool",+ "state": "success",+ "metadata": {},+ "content": [+ text.model_dump(),+ urls[0].model_dump(),+ {+ **first.model_dump(),+ "source": {+ "type": "base64",+ "data": "YWI=",+ "media_type": "audio/wav",+ },+ },+ before.model_dump(),+ urls[1].model_dump(),+ after.model_dump(),+ ],+ }++ agent = Agent(name="agent", system_prompt="", model=MockModel())+ for chunks in ([blocks], [[block] for block in blocks]):+ with self.subTest(chunk_count=len(chunks)):+ response = ToolResponse(id="tool")+ message = AssistantMsg(+ name=agent.name,+ id=agent.state.reply_id,+ content=[],+ )+ message.append_event(+ ToolResultStartEvent(+ reply_id=message.id,+ tool_call_id=response.id,+ tool_call_name="stream",+ ),+ )+ for content in chunks:+ response.append_chunk(ToolChunk(content=content))+ # pylint: disable-next=protected-access+ async for event in agent._convert_tool_chunk_to_event(+ response.id,+ content,+ ):+ message.append_event(event)+ self.assertEqual(response.model_dump(), expected)+ self.assertEqual(+ message.content[0].model_dump()["output"],+ [+ {+ **block,+ "created_at": AnyString(),+ "id": AnyString()+ if block["type"] == "text"+ else block["id"],+ }+ for block in expected["content"]+ ],+ )class RegisterFunctionTest(IsolatedAsyncioTestCase):