Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(agent): fit truncated tool text to the counted token limit (#3441)
Starchaser1024cf456697c1d9
diff --git a/src/agentscope/agent/_agent.py b/src/agentscope/agent/_agent.pyindex 7ab79bc2a..eabdaa455 100644--- a/src/agentscope/agent/_agent.py+++ b/src/agentscope/agent/_agent.py@@ -3184,8 +3184,13 @@ class Agent:min(len(truncated_text), reserved_tokens),)- reserved_text = truncated_text[:reserved_tokens]- offload_text = truncated_text[reserved_tokens:]+ reserved_text = await self._fit_text_prefix_to_token_limit(+ reserved_blocks=reserved_blocks,+ text=truncated_text,+ upper_bound=reserved_tokens,+ text_block_id=boundary_block.id,+ )+ offload_text = truncated_text[len(reserved_text) :]if reserved_text:if (@@ -3243,6 +3248,58 @@ class Agent:return reserved_tool_result, offload_tool_result+ async def _fit_text_prefix_to_token_limit(+ self,+ *,+ reserved_blocks: list,+ text: str,+ upper_bound: int,+ text_block_id: str,+ ) -> str:+ """Return the longest prefix of ``text`` that stays within the limit.++ The proportional estimate uses the block's average token density,+ which breaks when the prefix has a different script mix: a Chinese+ prefix of an ASCII-first block is denser and used to exceed the+ limit. Recount the candidate with the same counter and binary-search+ the prefix length, only when the proportional estimate overshoots.+ """+ limit = self.context_config.tool_result_limit++ def candidate(length: int) -> list:+ prefix = text[:length]+ if reserved_blocks and reserved_blocks[-1].type == "text":+ merged = reserved_blocks[-1].model_copy(deep=True)+ merged.text += prefix+ return [*reserved_blocks[:-1], merged]+ return [+ *reserved_blocks,+ TextBlock(text=prefix, id=text_block_id),+ ]++ async def fits(length: int) -> bool:+ return (+ await self.model.count_tokens(+ [AssistantMsg(self.name, content=candidate(length))],+ None,+ )+ <= limit+ )++ if await fits(upper_bound):+ return text[:upper_bound]++ low, high = 0, upper_bound+ best = 0+ while low <= high:+ middle = (low + high) // 2+ if await fits(middle):+ best = middle+ low = middle + 1+ else:+ high = middle - 1+ return text[:best]+# ======================================================================# Agent internal utility methods# ======================================================================diff --git a/tests/compress_tool_result_test.py b/tests/compress_tool_result_test.pyindex 0b41da258..31d13525b 100644--- a/tests/compress_tool_result_test.py+++ b/tests/compress_tool_result_test.py@@ -7,6 +7,7 @@ from utils import MockModel, AnyStringfrom agentscope.agent import Agent, ContextConfigfrom agentscope.message import (+ AssistantMsg,ToolResultBlock,TextBlock,DataBlock,@@ -497,15 +498,12 @@ class ToolResultCompressionTest(IsolatedAsyncioTestCase):messages: list,tools: list | None = None,) -> int:- """Mock token counting function based on content length."""+ """Mock token counting function based on text and data blocks."""content = messages[0].contentif isinstance(content, list):- if len(content) == 3:- return 150- elif len(content) == 2:- return 80- elif len(content) == 1:- return 40+ text = sum(len(b.text) for b in content if hasattr(b, "text"))+ data = sum(75 for b in content if b.type == "data")+ return text + datareturn 50self.mock_model.count_tokens = mock_count_tokens@@ -654,5 +652,47 @@ class ToolResultCompressionTest(IsolatedAsyncioTestCase):},)+ async def test_multibyte_prefix_stays_within_counted_limit(+ self,+ ) -> None:+ """A dense prefix is shortened until the counter itself fits."""+ dense_char = "\u20ac"+ payload = dense_char * 400 + "a" * 1200+ tool_result = ToolResultBlock(+ id="test_mixed",+ name="test_tool",+ output=[TextBlock(text=payload, id="block1")],+ )+ original = tool_result.model_dump()+ (+ reserved,+ offload,+ ) = await self.agent._split_tool_result_for_compression(+ tool_result,+ )+ assert offload is not None+ self.assertLessEqual(+ await self.mock_model.count_tokens(+ [AssistantMsg(self.agent.name, content=reserved.output)],+ None,+ ),+ self.agent.context_config.tool_result_limit,+ )+ # 133 three-byte characters use 399 bytes; 134 use 402 (101 tokens).+ expected_block = {+ "type": "text",+ "id": "block1",+ "created_at": AnyString(),+ "finished_at": None,+ }+ self.assertListEqual(+ [reserved.model_dump(), offload.model_dump()],+ [+ {**original, "output": [{**expected_block, "text": text}]}+ for text in (payload[:133], payload[133:])+ ],+ )+ self.assertDictEqual(tool_result.model_dump(), original)+async def asyncTearDown(self) -> None:"""The async teardown method."""