Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
feat(context): support context compression in Agent class (#1544)
DavdGaoe7dfee00749b
diff --git a/pyproject.toml b/pyproject.tomlindex efe94b215..21f12d17f 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -43,6 +43,7 @@ dependencies = ["tree_sitter","tree_sitter_bash","ripgrep",+ "jsonschema",][project.optional-dependencies]diff --git a/src/agentscope/agent/__init__.py b/src/agentscope/agent/__init__.pyindex f7b16dfc3..ac4165b0f 100644--- a/src/agentscope/agent/__init__.py+++ b/src/agentscope/agent/__init__.py@@ -2,5 +2,6 @@"""Initialize the agent module."""from ._state import AgentStatefrom ._agent import Agent+from ._config import CompressionConfig-__all__ = ["Agent", "AgentState"]+__all__ = ["Agent", "AgentState", "CompressionConfig"]diff --git a/src/agentscope/agent/_agent.py b/src/agentscope/agent/_agent.pyindex d87862059..32e46aaae 100644--- a/src/agentscope/agent/_agent.py+++ b/src/agentscope/agent/_agent.py@@ -10,13 +10,12 @@ import jsonschemafrom pydantic import (BaseModel,Field,- field_validator,- ValidationInfo,SerializeAsAny,PrivateAttr,ConfigDict,)+from ._config import CompressionConfigfrom ._state import AgentStatefrom ._utils import _ToolCallBatchfrom .._logging import logger@@ -54,7 +53,6 @@ from ..model import (ChatResponse,ChatUsage,ChatModelBase,- _deserialize_model,)from ..message import (Msg,@@ -82,126 +80,6 @@ from ..tool import ()-class SummarySchema(BaseModel):- """The compressed memory model, used to generate summary of old memories"""-- task_overview: str = Field(- max_length=300,- description=(- "The user's core request and success criteria.\n"- "Any clarifications or constraints they specified"- ),- )- current_state: str = Field(- max_length=300,- description=(- "What has been completed so far.\n"- "File created, modified, or analyzed (with paths if relevant).\n"- "Key outputs or artifacts produced."- ),- )- important_discoveries: str = Field(- max_length=300,- description=(- "Technical constraints or requirements uncovered.\n"- "Decisions made and their rationale.\n"- "Errors encountered and how they were resolved.\n"- "What approaches were tried that didn't work (and why)"- ),- )- next_steps: str = Field(- max_length=200,- description=(- "Specific actions needed to complete the task.\n"- "Any blockers or open questions to resolve.\n"- "Priority order if multiple steps remain"- ),- )- context_to_preserve: str = Field(- max_length=300,- description=(- "User preferences or style requirements.\n"- "Domain-specific details that aren't obvious.\n"- "Any promises made to the user"- ),- )---class CompressionConfig(BaseModel):- """The compression related configuration in AgentScope"""-- model_config = {"arbitrary_types_allowed": True}- """Allow arbitrary types in the pydantic model."""-- trigger_threshold: int = 20000- """The token threshold to trigger the compression process. When the- total token count in the memory exceeds this threshold, the- compression will be activated."""-- keep_recent: int = 5- """The number of most recent messages to keep uncompressed in the- memory to preserve the recent context."""-- compression_prompt: str = (- "<system-hint>You have been working on the task described above "- "but have not yet completed it. "- "Now write a continuation summary that will allow you to resume "- "work efficiently in a future context window where the "- "conversation history will be replaced with this summary. "- "Your summary should be structured, concise, and actionable."- "</system-hint>"- )- """The prompt used to guide the compression model to generate the- compressed summary, which will be wrapped into a user message and- attach to the end of the current memory."""-- summary_template: str = (- "<system-info>Here is a summary of your previous work\n"- "# Task Overview\n"- "{task_overview}\n\n"- "# Current State\n"- "{current_state}\n\n"- "# Important Discoveries\n"- "{important_discoveries}\n\n"- "# Next Steps\n"- "{next_steps}\n\n"- "# Context to Preserve\n"- "{context_to_preserve}"- "</system-info>"- )- """The string template to present the compressed summary to the agent,- which will be formatted with the fields from the- `compression_summary_model`."""-- summary_schema: dict = Field(- default_factory=SummarySchema.model_json_schema,- )- """The structured model used to guide the agent to generate the- structured compressed summary."""-- compression_model: SerializeAsAny[ChatModelBase] | None = None- """The compression model used to generate the compressed summary. If- not provided, the agent's model will be used."""-- @field_validator("compression_model", mode="before")- @classmethod- def validate_compression_model(cls, v: Any, info: ValidationInfo) -> Any:- """Deserialize compression_model from dict using context-injected- custom classes."""- if not isinstance(v, dict):- return v- custom_classes = (- info.context.get("custom_model_classes", [])- if info.context- else []- )- return _deserialize_model(- v,- custom_classes=custom_classes,- context=info.context,- )--class ReasoningConfig(BaseModel):"""The reasoning related configuration"""@@ -369,10 +247,184 @@ class Agent(BaseModel):context."""await self._handle_incoming_messages(msgs)- async def compress_memory(self) -> None:- """Compress the agent's memory if the token count exceeds the- threshold."""- # TODO: Implement when compression_config is available+ async def compress_context(+ self,+ compression_config: CompressionConfig | None = None,+ ) -> None:+ """Compress the agent's context if the token count exceeds the+ threshold.++ Args:+ compression_config (`CompressionConfig | None`, optional):+ If provided, compress the context with the given compression+ config. Otherwise, use the default compression config in the+ agent.+ """+ cfg: CompressionConfig = compression_config or self.compression++ # Count the current tokens+ kwargs = await self._prepare_model_input()+ estimated_tokens = await self.model.count_tokens(**kwargs)++ # Skip if no compression is needed+ threshold = cfg.trigger_ratio * self.model.context_length+ if estimated_tokens < threshold:+ return++ logger.info(+ "[AGENT %s]: Current token count %d exceeds the threshold %d, "+ "activating compression.",+ self.name,+ int(estimated_tokens),+ int(threshold),+ )++ if len(self.state.context) == 0:+ # The system prompt and the summary (if exists) exceeds the+ # threshold, which cannot be compressed, raise the error to the+ # developer!+ suffix = ""+ if self.state.summary:+ suffix = "and the compression summary "+ raise RuntimeError(+ f"The system prompt {suffix}exceed(s) the compression "+ f"threshold ({threshold} tokens), cannot be compressed.",+ )++ # Split the context into the ones to be compressed, and the others to+ # be reserved+ tools = kwargs.get("tools", [])+ (+ msgs_to_compress,+ msgs_to_reserve,+ ) = await self._split_context_for_compression(+ cfg.reserve_ratio * self.model.context_length,+ tools,+ )++ if len(msgs_to_compress) == 0:+ # The reserve ratio is too large so that although it exceeds the+ # trigger threshold, the context to be compressed is empty+ # Fallback by lowering the reserve ratio to compress more context.+ logger.warning(+ "The reserve ratio %.2f is too large to compress any context."+ "Lower the reserve ratio to 0 as a fallback.",+ cfg.reserve_ratio,+ )+ (+ msgs_to_compress,+ msgs_to_reserve,+ ) = await self._split_context_for_compression(+ 0 * self.model.context_length,+ tools,+ )++ # The msgs to be compressed cannot be empty here, unless the+ # system prompt and summary (if any) already exceed the context+ # length, which we have handled before.++ # Prepare the messages to compress+ msgs_system = [+ SystemMsg(+ name="system",+ content=await self._get_system_prompt(),+ ),+ ]+ if self.state.summary:+ msgs_system.append(UserMsg("user", self.state.summary))++ messages = (+ msgs_system+ + msgs_to_compress+ + [+ UserMsg(name="user", content=cfg.compression_prompt),+ ]+ )++ # The compression prompt may exceed the context length, here we mark+ # the overflow by a bool flag+ compression_tool_schema = [+ {+ "type": "function",+ "function": {+ "name": "generate_structured_output",+ "description": "Call this function to generate "+ "structured output required by "+ "the user.",+ "parameters": cfg.summary_schema,+ },+ },+ ]+ context_overflow = False+ estimated_compression_tokens = await self.model.count_tokens(+ messages,+ compression_tool_schema,+ )+ if estimated_compression_tokens > self.model.context_length:+ logger.warning(+ "The current context length exceeds the model's context "+ "length (%d tokens), the compression maybe failed due to "+ "insufficient reserved context for compression.",+ self.model.context_length,+ )+ context_overflow = True++ # Compress the messages+ try:+ res = await self.model.generate_structured_output(+ messages=messages,+ structured_model=cfg.summary_schema,+ )++ except Exception as e:+ if context_overflow:+ logger.warning(+ "Failed to compress context, which may be caused by "+ "insufficient reserved context for compression. "+ "Trying to compress by removing the oldest context.",+ )+ for i in range(1, len(msgs_to_compress) + 1):+ messages = (+ msgs_system+ + msgs_to_compress[i:]+ + [+ UserMsg(+ name="user",+ content=cfg.compression_prompt,+ ),+ ]+ )+ estimated_compression_tokens = (+ await self.model.count_tokens(+ messages,+ compression_tool_schema,+ )+ )+ # Considering trigger_ratio <= 0.9, at least reserve 10%+ # tokens for compression response+ if (+ estimated_compression_tokens+ < self.model.context_length * cfg.trigger_ratio+ ):+ break++ res = await self.model.generate_structured_output(+ messages=messages,+ structured_model=cfg.summary_schema,+ )++ else:+ raise e from None++ # Update the summary+ self.state.summary = cfg.summary_template.format(**res.content)+ # Update the context+ self.state.context = msgs_to_reserve++ logger.info(+ "[AGENT %s]: The context compression finished.",+ self.name,+ )# ======================================================================# Agent core methods, including _reply, _reasoning, _acting, etc.@@ -431,7 +483,7 @@ class Agent(BaseModel):# ===============================================================if action == "reasoning":# Compressed the memory if needed before reasoning- await self.compress_memory()+ await self.compress_context()# Perform reasoningasync for evt in self._reasoning():# Exit the loop when no tool calls generated and the reply@@ -538,27 +590,14 @@ class Agent(BaseModel):model_name=self.model.model_name,)- # The system prompt- messages = [- SystemMsg(name="system", content=await self._get_system_prompt()),- ]- # The compressed summary- if self.state.summary:- messages.append(- UserMsg(name="user", content=self.state.summary),- )- # The conversation context- messages.extend(self.state.context)-- # Get the tools schemas- tools = self.toolkit.get_function_schemas(- self.state.tool_context.activated_groups,- )+ # Get the input arguments for the chat model, including messages and+ # tools+ kwargs = await self._prepare_model_input()+ # Call the chat modelres = await self._call_model(- messages=messages,- tools=tools,tool_choice=tool_choice,+ **kwargs,)block_ids: dict = {"text": None, "thinking": None, "tools": []}@@ -1305,11 +1344,108 @@ class Agent(BaseModel):# Context management related methods# =======================================================================- def _split_context_for_compression(self) -> tuple[list[Msg], list[Msg]]:- """Split context into parts to compress and parts to keep recent."""- # TODO: Implement full splitting logic when compression_config is- # available- return [], list(self.state.context)+ async def _split_context_for_compression(+ self,+ to_reserved_tokens: float,+ tools: list[dict],+ ) -> tuple[list[Msg], list[Msg]]:+ """Split context into parts to compress and parts to keep recent.++ Args:+ to_reserved_tokens (`float`):+ The tokens to be reserved.+ tools (`list[dict]`):+ The tools JSON schemas used for token counting.++ Returns:+ `tuple[list[Msg], list[Msg]]`:+ The message objects to be compressed and reserved during+ context compression.+ """++ # The system prompt+ system_msg = [+ SystemMsg(name="system", content=await self._get_system_prompt()),+ ]++ # Append the current summary if exists+ if self.state.summary:+ system_msg.append(+ UserMsg("user", self.state.summary),+ )++ msg_index = len(self.state.context) - 1+ while msg_index >= 0:+ # Count the tokens when msgs after msg_index are reserved+ reserved_tokens = await self.model.count_tokens(+ system_msg + self.state.context[msg_index:],+ tools,+ )+ # If reserved tokens exceed the limit+ if reserved_tokens >= to_reserved_tokens:+ break+ msg_index -= 1++ if msg_index < 0:+ return [], deepcopy(self.state.context)++ # The msgs that won't exceed the reserved token limit+ msgs_to_compress = self.state.context[:msg_index]+ msgs_to_reserve = self.state.context[msg_index + 1 :]+ boundary_msg = self.state.context[msg_index]++ # Handle the boundary Msg+ boundary_msg_to_compress = deepcopy(boundary_msg)+ boundary_msg_to_reserve = deepcopy(boundary_msg)++ attempt_msg = deepcopy(boundary_msg)++ boundary_msg_content = boundary_msg.get_content_blocks()+ block_index = len(boundary_msg_content) - 1+ while block_index >= 0:+ attempt_msg.content = boundary_msg_content[block_index:]++ try_reserved = system_msg + [attempt_msg] + msgs_to_reserve+ reserved_tokens = await self.model.count_tokens(+ try_reserved,+ tools,+ )+ if reserved_tokens > to_reserved_tokens:+ break+ block_index -= 1++ # Adjust the block_index to avoid splitting tool call and result pairs++ # Check if the reserved part has tool results that don't have the+ # corresponding tool calls+ remain_result_ids = {}+ for i in range(len(boundary_msg_content) - 1, block_index, -1):+ block = boundary_msg_content[i]+ if isinstance(block, ToolResultBlock):+ remain_result_ids[block.id] = i+ elif isinstance(block, ToolCallBlock):+ remain_result_ids.pop(block.id, None)++ # Find the largest index of the remaining tool results, which doesn't+ # have the corresponding tool calls in the reserved parts+ if remain_result_ids:+ block_index = max(remain_result_ids.values())++ # Split the boundary msg content+ boundary_msg_to_compress.content = boundary_msg_content[+ : block_index + 1+ ]+ boundary_msg_to_reserve.content = boundary_msg_content[+ block_index + 1 :+ ]++ if len(boundary_msg_to_compress.content) > 0:+ msgs_to_compress += [boundary_msg_to_compress]++ if len(boundary_msg_to_reserve.content) > 0:+ msgs_to_reserve = [boundary_msg_to_reserve] + msgs_to_reserve++ return msgs_to_compress, msgs_to_reserve# ======================================================================# Agent internal utility methods@@ -1326,6 +1462,36 @@ class Agent(BaseModel):return "\n".join(prompt)+ async def _prepare_model_input(self) -> dict[str, Any]:+ """A unified method to prepare the chat model input according to+ the current context.++ Returns:+ `dict[str, Any]`+ The keyword arguments passed to the model.+ """+ # The system prompt+ messages = [+ SystemMsg(name="system", content=await self._get_system_prompt()),+ ]+ # The compressed summary+ if self.state.summary:+ messages.append(+ UserMsg(name="user", content=self.state.summary),+ )+ # The conversation context+ messages.extend(self.state.context)++ # Get the tools schemas+ tools = self.toolkit.get_function_schemas(+ self.state.tool_context.activated_groups,+ )++ return {+ "messages": messages,+ "tools": tools,+ }+async def _call_model(self,messages: list[Msg],diff --git a/src/agentscope/agent/_config.py b/src/agentscope/agent/_config.pynew file mode 100644index 000000000..614b3ea42--- /dev/null+++ b/src/agentscope/agent/_config.py@@ -0,0 +1,102 @@+# -*- coding: utf-8 -*-+"""The agent config classes."""++from pydantic import BaseModel, Field+++class SummarySchema(BaseModel):+ """The compressed memory model, used to generate summary of old memories"""++ task_overview: str = Field(+ max_length=300,+ description=(+ "The user's core request and success criteria.\n"+ "Any clarifications or constraints they specified"+ ),+ )+ current_state: str = Field(+ max_length=300,+ description=(+ "What has been completed so far.\n"+ "File created, modified, or analyzed (with paths if relevant).\n"+ "Key outputs or artifacts produced."+ ),+ )+ important_discoveries: str = Field(+ max_length=300,+ description=(+ "Technical constraints or requirements uncovered.\n"+ "Decisions made and their rationale.\n"+ "Errors encountered and how they were resolved.\n"+ "What approaches were tried that didn't work (and why)"+ ),+ )+ next_steps: str = Field(+ max_length=200,+ description=(+ "Specific actions needed to complete the task.\n"+ "Any blockers or open questions to resolve.\n"+ "Priority order if multiple steps remain"+ ),+ )+ context_to_preserve: str = Field(+ max_length=300,+ description=(+ "User preferences or style requirements.\n"+ "Domain-specific details that aren't obvious.\n"+ "Any promises made to the user"+ ),+ )+++class CompressionConfig(BaseModel):+ """The compression related configuration in AgentScope"""++ model_config = {"arbitrary_types_allowed": True}+ """Allow arbitrary types in the pydantic model."""++ trigger_ratio: float = Field(default=0.8, gt=0, lt=0.9)+ """When the token exceeds this ratio of the maximum context length, the+ context will be compressed. To reserve the context for context compression,+ the maximum ratio is 0.9."""++ reserve_ratio: float = Field(default=0.1, gt=0, lt=0.9)+ """The ratio of the tokens to reserve in context compression, which should+ be smaller than the trigger ratio."""++ compression_prompt: str = (+ "<system-hint>You have been working on the task described above "+ "but have not yet completed it. "+ "Now write a continuation summary that will allow you to resume "+ "work efficiently in a future context window where the "+ "conversation history will be replaced with this summary. "+ "Your summary should be structured, concise, and actionable."+ "</system-hint>"+ )+ """The prompt used to guide the compression model to generate the+ compressed summary, which will be wrapped into a user message and+ attach to the end of the current memory."""++ summary_template: str = (+ "<system-info>Here is a summary of your previous work\n"+ "# Task Overview\n"+ "{task_overview}\n\n"+ "# Current State\n"+ "{current_state}\n\n"+ "# Important Discoveries\n"+ "{important_discoveries}\n\n"+ "# Next Steps\n"+ "{next_steps}\n\n"+ "# Context to Preserve\n"+ "{context_to_preserve}"+ "</system-info>"+ )+ """The string template to present the compressed summary to the agent,+ which will be formatted with the fields from the+ `compression_summary_model`."""++ summary_schema: dict = Field(+ default_factory=SummarySchema.model_json_schema,+ )+ """The structured model used to guide the agent to generate the+ structured compressed summary."""diff --git a/src/agentscope/message/_base.py b/src/agentscope/message/_base.pyindex 4d1e73b66..93e689f66 100644--- a/src/agentscope/message/_base.py+++ b/src/agentscope/message/_base.py@@ -213,6 +213,7 @@ def UserMsg(content: str | list[TextBlock | DataBlock],metadata: dict | None = None,created_at: str | None = None,+ id: str | None = None, # pylint: disable=redefined-builtin) -> Msg:"""Create a user message with role "user".@@ -226,7 +227,8 @@ def UserMsg(The metadata of the message. Defaults to `None`.created_at (`str | None`, optional):The creation time of the message in ISO format. Defaults to `None`.-+ id (`str | None`, optional):+ The id of the message. Defaults to `None`.Returns:`Msg`:The created user message.@@ -238,6 +240,7 @@ def UserMsg(role="user",metadata=metadata or {},created_at=created_at or datetime.now().isoformat(),+ id=id or uuid.uuid4().hex,)diff --git a/src/agentscope/model/__init__.py b/src/agentscope/model/__init__.pyindex 62713a1cc..f164a88ae 100644--- a/src/agentscope/model/__init__.py+++ b/src/agentscope/model/__init__.py@@ -3,13 +3,12 @@from typing import Any, Typefrom ._model_base import ChatModelBase-from ._model_response import ChatResponse+from ._model_response import ChatResponse, StructuredResponsefrom ._dashscope_model import DashScopeChatModelfrom ._openai_model import OpenAIChatModelfrom ._anthropic_model import AnthropicChatModelfrom ._ollama_model import OllamaChatModelfrom ._gemini_model import GeminiChatModel-from ._trinity_model import TrinityChatModelfrom ._model_usage import ChatUsage# Built-in model classes (internal use only)@@ -66,10 +65,10 @@ __all__ = ["ChatUsage","ChatModelBase","ChatResponse",+ "StructuredResponse","DashScopeChatModel","OpenAIChatModel","AnthropicChatModel","OllamaChatModel","GeminiChatModel",- "TrinityChatModel",]diff --git a/src/agentscope/model/_anthropic_model.py b/src/agentscope/model/_anthropic_model.pyindex ab8e5359f..39e826cce 100644--- a/src/agentscope/model/_anthropic_model.py+++ b/src/agentscope/model/_anthropic_model.py@@ -41,6 +41,7 @@ class AnthropicChatModel(ChatModelBase):def __init__(self,model_name: str,+ context_length: int,api_key: str | None = None,max_tokens: int = 2048,stream: bool = True,@@ -57,6 +58,8 @@ class AnthropicChatModel(ChatModelBase):Args:model_name (`str`):The model names.+ context_length (`int`):+ The context length of the model, used in context compression.api_key (`str`):The anthropic API key.max_tokens (`int`):@@ -97,6 +100,7 @@ class AnthropicChatModel(ChatModelBase):super().__init__(model_name=model_name,stream=stream,+ context_length=context_length,max_retries=max_retries,fallback_model_name=fallback_model_name,formatter=formatter or AnthropicChatFormatter(),diff --git a/src/agentscope/model/_dashscope_model.py b/src/agentscope/model/_dashscope_model.pyindex dc6ab55c6..728d7e053 100644--- a/src/agentscope/model/_dashscope_model.py+++ b/src/agentscope/model/_dashscope_model.py@@ -75,6 +75,7 @@ class DashScopeChatModel(ChatModelBase):self,model_name: str,api_key: str,+ context_length: int,stream: bool = True,max_retries: int = 0,fallback_model_name: str | None = None,@@ -92,6 +93,8 @@ class DashScopeChatModel(ChatModelBase):The model names.api_key (`str`):The dashscope API key.+ context_length (`int`):+ The context length of the model.stream (`bool`):The streaming output or notenable_thinking (`bool | None`, optional):@@ -141,9 +144,10 @@ class DashScopeChatModel(ChatModelBase):super().__init__(model_name,stream,+ context_length,+ formatter or DashScopeChatFormatter(),max_retries,fallback_model_name,- formatter or DashScopeChatFormatter(),)self.api_key = api_keydiff --git a/src/agentscope/model/_gemini_model.py b/src/agentscope/model/_gemini_model.pyindex 087144173..427cd85c9 100644--- a/src/agentscope/model/_gemini_model.py+++ b/src/agentscope/model/_gemini_model.py@@ -16,7 +16,7 @@ from typing import (from pydantic import BaseModelfrom .._logging import logger-from ..formatter import FormatterBase+from ..formatter import FormatterBase, GeminiChatFormatterfrom ..message import ToolCallBlock, TextBlock, ThinkingBlockfrom ._model_usage import ChatUsagefrom ._model_base import ChatModelBase@@ -123,6 +123,7 @@ class GeminiChatModel(ChatModelBase):self,model_name: str,api_key: str,+ context_length: int,stream: bool = True,max_retries: int = 0,fallback_model_name: str | None = None,@@ -138,6 +139,8 @@ class GeminiChatModel(ChatModelBase):The name of the Gemini model to use, e.g. "gemini-2.5-flash".api_key (`str`):The API key for Google Gemini.+ context_length (`int`):+ The context length of the model, used in context compression.stream (`bool`, default `True`):Whether to use streaming output or not.max_retries (`int`, optional):@@ -168,9 +171,10 @@ class GeminiChatModel(ChatModelBase):super().__init__(model_name=model_name,stream=stream,+ context_length=context_length,max_retries=max_retries,fallback_model_name=fallback_model_name,- formatter=formatter,+ formatter=formatter or GeminiChatFormatter(),)self.client = genai.Client(diff --git a/src/agentscope/model/_model_base.py b/src/agentscope/model/_model_base.pyindex 3a5de8de4..54d1b4a2e 100644--- a/src/agentscope/model/_model_base.py+++ b/src/agentscope/model/_model_base.py@@ -1,13 +1,28 @@# -*- coding: utf-8 -*-"""The chat model base class."""-+import jsonfrom abc import abstractmethod-from typing import AsyncGenerator, Any, TYPE_CHECKING+from copy import deepcopy+from typing import AsyncGenerator, Any, TYPE_CHECKING, Type++import jsonschema+from pydantic import BaseModel-from ._model_response import ChatResponse+from ._model_response import ChatResponse, StructuredResponsefrom .._logging import logger+from .._utils._common import _json_loads_with_repairfrom ..formatter import FormatterBase-from ..message import Msg+from ..message import (+ Msg,+ TextBlock,+ ThinkingBlock,+ ToolCallBlock,+ ToolResultBlock,+ DataBlock,+ URLSource,+ Base64Source,+ UserMsg,+)if TYPE_CHECKING:from ..tool import ToolChoice@@ -29,10 +44,14 @@ class ChatModelBase:max_retries: int"""Maximum number of retries on failure"""+ context_length: int+ """The context length of the model, which will be used in context+ compression."""+fallback_model_name: str | None"""Fallback model name to use after all retries fail"""- formatter: Any | None+ formatter: FormatterBase"""The API formatter that format the messages into the required format forthe underlying API."""@@ -40,9 +59,10 @@ class ChatModelBase:self,model_name: str,stream: bool,+ context_length: int,+ formatter: FormatterBase,max_retries: int = 0,fallback_model_name: str | None = None,- formatter: FormatterBase | None = None,) -> None:"""Initialize the chat model base class.@@ -51,15 +71,19 @@ class ChatModelBase:The name of the modelstream (`bool`):Whether the model output is streaming or not+ context_length (`int`):+ The context length of the model, which will be used+ in context compression.+ formatter (`FormatterBase`):+ Formatter for message preprocessing.max_retries (`int`, optional):Maximum number of retries on failure. Defaults to 0.fallback_model_name (`str | None`, optional):Fallback model name to use after all retries fail.- formatter (`FormatterBase | None`, optional):- Formatter for message preprocessing."""self.model_name = model_nameself.stream = stream+ self.context_length = context_lengthself.max_retries = max_retriesself.fallback_model_name = fallback_model_nameself.formatter = formatter@@ -87,8 +111,6 @@ class ChatModelBase:**kwargs:Additional keyword arguments passed to the underlying API."""- if self.formatter is not None:- messages = await self.formatter.format(messages)last_error: Exception | None = None@@ -188,3 +210,253 @@ class ChatModelBase:f"Invalid tool_choice '{tool_choice}'. "f"Available options: {', '.join(sorted(all_options))}",)++ async def count_tokens(+ self,+ messages: list[Msg],+ tools: list[dict] | None,+ ) -> int:+ """A quick and unified method to estimate the token count of the+ model input by dividing the total input size in bytes by 4.++ Note a standard way to count the tokens is first formatting the input+ messages into the API required format, then use the tokenizer of the+ underlying API to count the tokens.++ Subclasses may override this method to provide a more accurate+ implementation tailored to their specific tokenizer.++ Args:+ messages (`list[Msg]`):+ The messages to send to the model.+ tools (`list[dict] | None`):+ The tools available to the model.++ Returns:+ `int`:+ The number of tokens in the model.+ """+ cnt = 0++ acc_texts = []+ data_blocks = []+ for msg in messages:+ for block in msg.get_content_blocks():+ if isinstance(block, TextBlock):+ acc_texts.append(block.text)++ elif isinstance(block, ThinkingBlock):+ acc_texts.append(block.thinking)++ elif isinstance(block, ToolCallBlock):+ acc_texts.append(block.input)++ elif isinstance(block, ToolResultBlock):+ if isinstance(block.output, str):+ acc_texts.append(block.output)+ elif isinstance(block.output, list):+ for item in block.output:+ if isinstance(item, TextBlock):+ acc_texts.append(item.text)+ elif isinstance(item, DataBlock):+ data_blocks.append(item)++ elif isinstance(block, DataBlock):+ data_blocks.append(block)++ else:+ logger.warning(+ "Unknown block type %s in token counting, skipping.",+ type(block),+ )++ # Count the tokens of the tool JSON schemas+ if tools:+ acc_texts.append(json.dumps(tools, ensure_ascii=False))++ # Add the multimodal tokens+ for block in data_blocks:+ if isinstance(block.source, URLSource):+ # We don't download the content here to avoid blocking+ acc_texts.append(str(block.source.url))+ elif isinstance(block.source, Base64Source):+ cnt += len(block.source.data) // 4++ # Count the text tokens+ acc_text = "".join(acc_texts)+ cnt += int(len(acc_text.encode("utf-8")) / 4 + 0.5)++ return cnt++ async def generate_structured_output(+ self,+ messages: list[Msg],+ structured_model: Type[BaseModel] | dict,+ **kwargs: Any,+ ) -> StructuredResponse:+ """Generate required structured output by the given model.++ Note this function also shares the fallback model and max retries+ settings with the `__call__` method.++ Args:+ messages (`list[Msg]`):+ The context for LLM to generate the structured output.+ structured_model (`Type[BaseModel] | dict`):+ A Pydantic model or a dict of JSON schemas.++ Returns:+ `StructuredResponse`:+ The structured response generated by the model.+ """++ if len(messages) == 0:+ raise ValueError(+ "The input messages cannot be empty for the "+ "`generate_structured_output` method.",+ )++ last_error: Exception | None = None++ for model_name in self._models_to_try():+ for attempt in range(self.max_retries + 1):+ try:+ return await self._call_api_with_structured_output(+ model_name,+ messages=messages,+ structured_model=structured_model,+ **kwargs,+ )+ except Exception as e:+ last_error = e+ if attempt < self.max_retries:+ logger.warning(+ "Attempt %d failed for model %s: %s. Retrying...",+ attempt + 1,+ model_name,+ str(e),+ )+ else:+ logger.warning(+ "All %d attempt(s) failed for model %s.",+ self.max_retries + 1,+ model_name,+ )++ if last_error is not None:+ raise last_error+ raise RuntimeError("No models to try")++ async def _call_api_with_structured_output(+ self,+ model_name: str,+ messages: list[Msg],+ structured_model: Type[BaseModel] | dict,+ **kwargs: Any,+ ) -> StructuredResponse:+ """This function constructs a 'generate_structured_output' tool to+ help LLM generate structured output as a compromise for LLM APIs that+ don't support structured output.++ If your subclasses inherit from `ChatModelBase` and the underlying+ API supports structured output, you can override this method to+ provide a more accurate implementation.++ Note this method use the "required" tool choice to force LLM to call+ the 'generate_structured_output' method, and adds instructions into+ the input messages. However, LLM APIs that doesn't support+ "required" tool choice may still fail (e.g. generate text output and+ ignore the tool call, or fail in validation).+ """++ if isinstance(structured_model, dict):+ input_schema = structured_model+ else:+ input_schema = structured_model.model_json_schema()++ func_name = "generate_structured_output"+ instruction = (+ "<system-reminder>Now you **MUST** call the tool named "+ f"'{func_name}' to generate the structured output required "+ "by the user. DON'T do anything else.</system-reminder>"+ )++ copied_messages = deepcopy(messages)+ # Insert instruction to ensure llm is correctly guided+ if copied_messages[-1].role == "user":+ # Insert a user message to the last+ copied_messages[-1].content = copied_messages[+ -1+ ].get_content_blocks() + [TextBlock(text=instruction)]+ else:+ copied_messages.append(+ UserMsg(name="user", content=[TextBlock(text=instruction)]),+ )++ res = await self._call_api(+ model_name=model_name,+ messages=copied_messages,+ tools=[+ {+ "type": "function",+ "function": {+ "name": func_name,+ "description": "Call this function to generate "+ "structured output required by "+ "the user.",+ "parameters": input_schema,+ },+ },+ ],+ tool_choice=func_name,+ **kwargs,+ )++ completed_response: ChatResponse | None = None+ if self.stream:+ async for chunk in res:+ if chunk.is_last:+ completed_response = chunk+ else:+ completed_response = res++ if completed_response is None:+ raise RuntimeError(+ f"Failed to get the completed response from model "+ f"{model_name}.",+ )++ structured_output: dict[str, Any] | None = None+ for _ in completed_response.content:+ if isinstance(_, ToolCallBlock) and _.name == func_name:+ structured_output = _json_loads_with_repair(+ _.input,+ input_schema,+ )+ break++ if structured_output is None:+ raise RuntimeError(+ "Failed to generate structured output for model.",+ )++ # Validate the output+ if isinstance(structured_model, dict):+ jsonschema.validate(structured_output, structured_model)++ elif issubclass(structured_model, BaseModel):+ structured_model.model_validate(structured_output)++ else:+ raise ValueError(+ "The structured_model is expected to be a subclass of "+ "Pydantic.BaseModel or a dict, "+ f"but got {type(structured_model)}.",+ )++ return StructuredResponse(+ id=completed_response.id,+ created_at=completed_response.created_at,+ content=structured_output,+ usage=completed_response.usage,+ )diff --git a/src/agentscope/model/_model_response.py b/src/agentscope/model/_model_response.pyindex cddd3351d..569b8afe3 100644--- a/src/agentscope/model/_model_response.py+++ b/src/agentscope/model/_model_response.py@@ -29,18 +29,47 @@ class ChatResponse(DictMixin):be the complete response, otherwise the content is a partial response"""id: str = field(default_factory=lambda: uuid.uuid4().hex)- """The unique identifier formatter """+ """The unique identifier."""created_at: str = field(default_factory=lambda: datetime.now().isoformat())"""When the response was created"""- type: Literal["chat"] = field(default_factory=lambda: "chat")- """The type of the response, which is always 'chat'."""+ type: Literal["chat_response"] = field(+ default_factory=lambda: "chat_response",+ )+ """The type of the response, which is always 'chat_response'."""++ usage: ChatUsage | None = field(default_factory=lambda: None)+ """The usage information of the chat response, if available."""++ metadata: dict[str, JSONSerializableObject] = field(+ default_factory=lambda: {},+ )+ """The metadata of the chat response"""+++@dataclass+class StructuredResponse:+ """The structured response of chat models."""++ content: dict+ """The structured output of the model."""++ id: str = field(default_factory=lambda: uuid.uuid4().hex)+ """The unique identifier."""++ created_at: str = field(default_factory=lambda: datetime.now().isoformat())+ """When the response was created"""++ type: Literal["structured_response"] = field(+ default_factory=lambda: "structured_response",+ )+ """The type of the response, which is always 'structured_response'."""usage: ChatUsage | None = field(default_factory=lambda: None)"""The usage information of the chat response, if available."""- metadata: dict[str, JSONSerializableObject] | None = field(- default_factory=lambda: None,+ metadata: dict[str, JSONSerializableObject] = field(+ default_factory=lambda: {},)"""The metadata of the chat response"""diff --git a/src/agentscope/model/_ollama_model.py b/src/agentscope/model/_ollama_model.pyindex 09d3190bb..c69c0a480 100644--- a/src/agentscope/model/_ollama_model.py+++ b/src/agentscope/model/_ollama_model.py@@ -39,6 +39,7 @@ class OllamaChatModel(ChatModelBase):def __init__(self,model_name: str,+ context_length: int,stream: bool = False,max_retries: int = 0,fallback_model_name: str | None = None,@@ -55,6 +56,9 @@ class OllamaChatModel(ChatModelBase):Args:model_name (`str`):The name of the model.+ context_length (`int`):+ The context length of the model, which will be used in+ context compression.stream (`bool`, default `True`):Streaming mode or not.max_retries (`int`, optional):@@ -95,6 +99,7 @@ class OllamaChatModel(ChatModelBase):super().__init__(model_name=model_name,stream=stream,+ context_length=context_length,max_retries=max_retries,fallback_model_name=fallback_model_name,formatter=formatter or OllamaChatFormatter(),diff --git a/src/agentscope/model/_openai_model.py b/src/agentscope/model/_openai_model.pyindex e1a9c266e..bcf0167da 100644--- a/src/agentscope/model/_openai_model.py+++ b/src/agentscope/model/_openai_model.py@@ -17,7 +17,7 @@ from pydantic import BaseModelfrom . import ChatResponsefrom ._model_base import ChatModelBasefrom ._model_usage import ChatUsage-from ..formatter import FormatterBase+from ..formatter import FormatterBase, OpenAIChatFormatterfrom ..message import (TextBlock,ThinkingBlock,@@ -74,6 +74,7 @@ class OpenAIChatModel(ChatModelBase):def __init__(self,model_name: str,+ context_length: int,api_key: str | None = None,stream: bool = True,max_retries: int = 0,@@ -90,6 +91,9 @@ class OpenAIChatModel(ChatModelBase):Args:model_name (`str`, default `None`):The name of the model to use in OpenAI API.+ context_length (`int`):+ The context length of the model, which will be used in+ context compression.api_key (`str`, default `None`):The API key for OpenAI API. If not specified, it willbe read from the environment variable `OPENAI_API_KEY`.@@ -124,8 +128,9 @@ class OpenAIChatModel(ChatModelBase):model_name=model_name,stream=stream,max_retries=max_retries,+ context_length=context_length,fallback_model_name=fallback_model_name,- formatter=formatter,+ formatter=formatter or OpenAIChatFormatter(),)import openaidiff --git a/src/agentscope/model/_trinity_model.py b/src/agentscope/model/_trinity_model.pydeleted file mode 100644index 332abd183..000000000--- a/src/agentscope/model/_trinity_model.py+++ /dev/null@@ -1,67 +0,0 @@-# -*- coding: utf-8 -*--"""A model class for RL Training with Trinity-RFT."""-from typing import (- Optional,- TYPE_CHECKING,-)-from typing_extensions import deprecated-from ._openai_model import OpenAIChatModel-from ..types import JSONSerializableObject---if TYPE_CHECKING:- from openai import AsyncOpenAI-else:- AsyncOpenAI = "openai.AsyncOpenAI"---@deprecated(- "TrinityChatModel is deprecated. Please use OpenAIChatModel directly.",-)-class TrinityChatModel(OpenAIChatModel):- """A model class for RL Training with Trinity-RFT."""-- def __init__(- self,- openai_async_client: AsyncOpenAI,- generate_kwargs: dict[str, JSONSerializableObject] | None = None,- enable_thinking: Optional[bool] = None,- ) -> None:- """Initialize the Trinity model class.-- Args:- openai_async_client (`AsyncOpenAI`):- The OpenAI async client instance provided by Trinity-RFT.- generate_kwargs (`dict[str, JSONSerializableObject] | None`, \- optional):- Additional keyword arguments to pass to the model's generate- method. Defaults to None.- enable_thinking (`bool`, optional):- Whether to enable the model's thinking capability. Only- applicable for Qwen3 series models. Defaults to None.- """- model_name = getattr(openai_async_client, "model_path", None)- if model_name is None:- raise ValueError(- "The provided openai_async_client does not have a "- "`model_path` attribute. Please ensure you are using "- "the instance provided by Trinity-RFT.",- )- super().__init__(- model_name=model_name,- api_key="EMPTY",- generate_kwargs=generate_kwargs,- stream=False, # RL training does not support streaming- )- if enable_thinking is not None:- if "chat_template_kwargs" not in self.generate_kwargs:- self.generate_kwargs["chat_template_kwargs"] = {}- assert isinstance(- self.generate_kwargs["chat_template_kwargs"],- dict,- ), "chat_template_kwargs must be a dictionary."- self.generate_kwargs["chat_template_kwargs"][- "enable_thinking"- ] = enable_thinking- # change the client instance to the provided one- self.client = openai_async_clientdiff --git a/src/agentscope/offload/__init__.py b/src/agentscope/offload/__init__.pynew file mode 100644index 000000000..1ecb033b4--- /dev/null+++ b/src/agentscope/offload/__init__.py@@ -0,0 +1,8 @@+# -*- coding: utf-8 -*-+"""The offload module in agentscope."""++from ._base import OffloadBase++__all__ = [+ "OffloadBase",+]diff --git a/src/agentscope/offload/_base.py b/src/agentscope/offload/_base.pynew file mode 100644index 000000000..8daaf5a6a--- /dev/null+++ b/src/agentscope/offload/_base.py@@ -0,0 +1,37 @@+# -*- coding: utf-8 -*-+"""The base class for offloading."""+from abc import abstractmethod, ABC+from typing import Any++from ..message import Msg, ToolResultBlock, DataBlock, TextBlock+++class OffloadBase(ABC):+ """The offload base class for agentic retrieval, e.g. offload the+ compressed context into accessible Markdown files so that the agent+ can read them by file reading tools.+ """++ @abstractmethod+ async def offload_context(self, msgs: list[Msg], **kwargs: Any) -> None:+ """Offload the compressed context into agent accessible content."""++ @abstractmethod+ async def offload_tool_result(+ self,+ blocks: list[ToolResultBlock],+ **kwargs: Any,+ ) -> None:+ """Offload the tool results into agent accessible content."""++ @abstractmethod+ async def offload_plan(self, plan: str, **kwargs: Any) -> None:+ """Offload the finished or deprecated plan."""++ @abstractmethod+ async def offload_summary(+ self,+ summary: list[TextBlock | DataBlock],+ **kwargs: Any,+ ) -> None:+ """Offload the outdated context summary."""diff --git a/src/agentscope/storage/__init__.py b/src/agentscope/storage/__init__.pynew file mode 100644index 000000000..7397a1a87--- /dev/null+++ b/src/agentscope/storage/__init__.py@@ -0,0 +1,8 @@+# -*- coding: utf-8 -*-+"""The storage module in agentscope."""++from ._base import StorageBase++__all__ = [+ "StorageBase",+]diff --git a/src/agentscope/storage/_base.py b/src/agentscope/storage/_base.pynew file mode 100644index 000000000..91210fe27--- /dev/null+++ b/src/agentscope/storage/_base.py@@ -0,0 +1,49 @@+# -*- coding: utf-8 -*-+"""The storage base class."""+from abc import ABC, abstractmethod+from typing import Any++from ..agent import AgentState+++class StorageBase(ABC):+ """The storage abstract base class."""++ @abstractmethod+ async def load_agent_state(+ self,+ session_id: str,+ agent_id: str,+ **kwargs: Any,+ ) -> AgentState:+ """Load the agent state from the storage.++ Args:+ session_id (`str`):+ The session id.+ agent_id (`str`):+ The agent id, in case one session has multiple agents.++ Returns:+ `AgentState`:+ The loaded agent state.+ """++ @abstractmethod+ async def save_agent_state(+ self,+ session_id: str,+ agent_id: str,+ agent_state: AgentState,+ **kwargs: Any,+ ) -> None:+ """Save the agent state to the storage.++ Args:+ session_id (`str`):+ The session id.+ agent_id (`str`):+ The agent id, in case one session has multiple agents.+ agent_state (`AgentState`):+ The agent state.+ """diff --git a/tests/context_compression_test.py b/tests/context_compression_test.pynew file mode 100644index 000000000..927d6f28f--- /dev/null+++ b/tests/context_compression_test.py@@ -0,0 +1,564 @@+# -*- coding: utf-8 -*-+"""A template test case."""+# pylint: disable=protected-access+from unittest.async_case import IsolatedAsyncioTestCase++from utils import MockModel, AnyString++from agentscope.model import StructuredResponse+from agentscope.agent import Agent, CompressionConfig, AgentState+from agentscope.message import UserMsg, AssistantMsg, TextBlock+from agentscope.tool import Toolkit+++class ContextCompressionTest(IsolatedAsyncioTestCase):+ """The template test case."""++ async def asyncSetUp(self) -> None:+ """The async setup method."""++ async def test_split_function(self) -> None:+ """The template test."""+ agent = Agent(+ name="Friday",+ system_prompt="".join(["0" for _ in range(60 * 4)]),+ model=MockModel(),+ compression=CompressionConfig(+ trigger_ratio=0.8,+ reserve_ratio=0.1,+ ),+ state=AgentState(+ session_id="123",+ context=[+ UserMsg(+ "User",+ "".join(["1" for _ in range(30 * 4)]),+ id="1",+ ),+ AssistantMsg(+ "Friday",+ "".join(["2" for _ in range(10 * 4)]),+ id="2",+ ),+ UserMsg(+ "User",+ "".join(["3" for _ in range(10 * 4)]),+ id="3",+ ),+ ],+ ),+ toolkit=Toolkit(),+ )++ # When the length of last two messages is exactly appropriate+ to_compress, to_reserve = await agent._split_context_for_compression(+ to_reserved_tokens=80,+ tools=[],+ )++ self.assertListEqual(+ [_.id for _ in to_compress],+ ["1"],+ )+ self.assertListEqual(+ [_.id for _ in to_reserve],+ ["2", "3"],+ )++ # When one message is in the dividing line+ agent.state.context = [+ UserMsg("User", "".join(["2" for _ in range(30 * 4)]), id="1"),+ AssistantMsg(+ "Friday",+ "".join(["3" for _ in range(15 * 4)]),+ id="2",+ ),+ UserMsg("User", "".join(["3" for _ in range(10 * 4)]), id="3"),+ ]++ to_compress, to_reserve = await agent._split_context_for_compression(+ to_reserved_tokens=80,+ tools=[],+ )+ self.assertListEqual(+ [_.id for _ in to_compress],+ ["1", "2"],+ )+ self.assertListEqual(+ [_.id for _ in to_reserve],+ ["3"],+ )++ # When compress all messages+ agent.state.context = [+ UserMsg("User", "".join(["2" for _ in range(30 * 4)]), id="1"),+ AssistantMsg(+ "Friday",+ "".join(["3" for _ in range(15 * 4)]),+ id="2",+ ),+ UserMsg("User", "".join(["3" for _ in range(30 * 4)]), id="3"),+ ]+ to_compress, to_reserve = await agent._split_context_for_compression(+ to_reserved_tokens=80,+ tools=[],+ )+ self.assertListEqual(+ [_.id for _ in to_compress],+ ["1", "2", "3"],+ )+ self.assertListEqual(+ [_.id for _ in to_reserve],+ [],+ )++ # When the boundary message has multiple blocks+ agent.state.context = [+ UserMsg("User", "".join(["a" for _ in range(30 * 4)]), id="1"),+ AssistantMsg(+ "Friday",+ [+ TextBlock(+ text="".join(["b" for _ in range(10 * 4)]),+ id="b",+ ),+ TextBlock(+ text="".join(["c" for _ in range(10 * 4)]),+ id="c",+ ),+ ],+ id="2",+ ),+ UserMsg("User", "".join(["d" for _ in range(10 * 4)]), id="3"),+ ]+ to_compress, to_reserve = await agent._split_context_for_compression(+ to_reserved_tokens=80,+ tools=[],+ )+ self.assertListEqual(+ [_.model_dump() for _ in to_compress],+ [+ {+ "id": "1",+ "created_at": AnyString(),+ "name": "User",+ "role": "user",+ "content": "a" * 120,+ "metadata": {},+ },+ {+ "id": "2",+ "created_at": AnyString(),+ "name": "Friday",+ "role": "assistant",+ "content": [+ {+ "id": "b",+ "text": "b" * 40,+ "type": "text",+ },+ ],+ "metadata": {},+ },+ ],+ )+ self.assertListEqual(+ [_.model_dump() for _ in to_reserve],+ [+ {+ "id": "2",+ "created_at": AnyString(),+ "name": "Friday",+ "role": "assistant",+ "content": [+ {+ "id": "c",+ "text": "c" * 40,+ "type": "text",+ },+ ],+ "metadata": {},+ },+ {+ "id": "3",+ "created_at": AnyString(),+ "name": "User",+ "role": "user",+ "content": "d" * 40,+ "metadata": {},+ },+ ],+ )++ # When the boundary message has multiple blocks+ # Cannot leave any blocks+ agent.state.context = [+ UserMsg("User", "".join(["a" for _ in range(30 * 4)]), id="1"),+ AssistantMsg(+ "Friday",+ [+ TextBlock(+ text="".join(["b" for _ in range(10 * 4)]),+ id="b",+ ),+ TextBlock(+ text="".join(["c" for _ in range(15 * 4)]),+ id="c",+ ),+ ],+ id="2",+ ),+ UserMsg("User", "".join(["d" for _ in range(10 * 4)]), id="3"),+ ]+ to_compress, to_reserve = await agent._split_context_for_compression(+ to_reserved_tokens=80,+ tools=[],+ )+ self.assertListEqual(+ [_.model_dump() for _ in to_compress],+ [+ {+ "id": "1",+ "created_at": AnyString(),+ "name": "User",+ "role": "user",+ "content": "a" * 120,+ "metadata": {},+ },+ {+ "id": "2",+ "created_at": AnyString(),+ "name": "Friday",+ "role": "assistant",+ "content": [+ {+ "id": "b",+ "text": "b" * 40,+ "type": "text",+ },+ {+ "id": "c",+ "text": "c" * 60,+ "type": "text",+ },+ ],+ "metadata": {},+ },+ ],+ )+ self.assertListEqual(+ [_.model_dump() for _ in to_reserve],+ [+ {+ "id": "3",+ "created_at": AnyString(),+ "name": "User",+ "role": "user",+ "content": "d" * 40,+ "metadata": {},+ },+ ],+ )++ # Leave the last block of the boundary message+ agent.state.context = [+ UserMsg("User", "".join(["a" for _ in range(30 * 4)]), id="1"),+ AssistantMsg(+ "Friday",+ [+ TextBlock(+ text="".join(["b" for _ in range(10 * 4)]),+ id="b",+ ),+ TextBlock(+ text="".join(["c" for _ in range(5 * 4)]),+ id="c",+ ),+ ],+ id="2",+ ),+ UserMsg("User", "".join(["d" for _ in range(10 * 4)]), id="3"),+ ]+ to_compress, to_reserve = await agent._split_context_for_compression(+ to_reserved_tokens=80,+ tools=[],+ )+ self.assertListEqual(+ [_.model_dump() for _ in to_compress],+ [+ {+ "id": "1",+ "created_at": AnyString(),+ "name": "User",+ "role": "user",+ "content": "a" * 120,+ "metadata": {},+ },+ {+ "id": "2",+ "created_at": AnyString(),+ "name": "Friday",+ "role": "assistant",+ "content": [+ {+ "id": "b",+ "text": "b" * 40,+ "type": "text",+ },+ ],+ "metadata": {},+ },+ ],+ )+ self.assertListEqual(+ [_.model_dump() for _ in to_reserve],+ [+ {+ "id": "2",+ "created_at": AnyString(),+ "name": "Friday",+ "role": "assistant",+ "content": [+ {+ "id": "c",+ "text": "c" * 20,+ "type": "text",+ },+ ],+ "metadata": {},+ },+ {+ "id": "3",+ "created_at": AnyString(),+ "name": "User",+ "role": "user",+ "content": "d" * 40,+ "metadata": {},+ },+ ],+ )++ # Leave all the blocks+ agent.state.context = [+ UserMsg("User", "".join(["a" for _ in range(30 * 4)]), id="1"),+ AssistantMsg(+ "Friday",+ [+ TextBlock(+ text="".join(["b" for _ in range(5 * 4)]),+ id="b",+ ),+ TextBlock(+ text="".join(["c" for _ in range(5 * 4)]),+ id="c",+ ),+ ],+ id="2",+ ),+ UserMsg("User", "".join(["d" for _ in range(10 * 4)]), id="3"),+ ]+ to_compress, to_reserve = await agent._split_context_for_compression(+ to_reserved_tokens=80,+ tools=[],+ )+ self.assertListEqual(+ [_.model_dump() for _ in to_compress],+ [+ {+ "id": "1",+ "created_at": AnyString(),+ "name": "User",+ "role": "user",+ "content": "a" * 120,+ "metadata": {},+ },+ ],+ )+ self.assertListEqual(+ [_.model_dump() for _ in to_reserve],+ [+ {+ "id": "2",+ "created_at": AnyString(),+ "name": "Friday",+ "role": "assistant",+ "content": [+ {+ "id": "b",+ "text": "b" * 20,+ "type": "text",+ },+ {+ "id": "c",+ "text": "c" * 20,+ "type": "text",+ },+ ],+ "metadata": {},+ },+ {+ "id": "3",+ "created_at": AnyString(),+ "name": "User",+ "role": "user",+ "content": "d" * 40,+ "metadata": {},+ },+ ],+ )++ # Leave all the messages+ agent.state.context = [+ AssistantMsg(+ "Friday",+ [+ TextBlock(+ text="".join(["b" for _ in range(5 * 4)]),+ id="b",+ ),+ TextBlock(+ text="".join(["c" for _ in range(5 * 4)]),+ id="c",+ ),+ ],+ id="2",+ ),+ UserMsg("User", "".join(["d" for _ in range(10 * 4)]), id="3"),+ ]+ to_compress, to_reserve = await agent._split_context_for_compression(+ to_reserved_tokens=80,+ tools=[],+ )+ self.assertListEqual(+ [_.model_dump() for _ in to_compress],+ [],+ )+ self.assertListEqual(+ [_.model_dump() for _ in to_reserve],+ [+ {+ "id": "2",+ "created_at": AnyString(),+ "name": "Friday",+ "role": "assistant",+ "content": [+ {+ "id": "b",+ "text": "b" * 20,+ "type": "text",+ },+ {+ "id": "c",+ "text": "c" * 20,+ "type": "text",+ },+ ],+ "metadata": {},+ },+ {+ "id": "3",+ "created_at": AnyString(),+ "name": "User",+ "role": "user",+ "content": "d" * 40,+ "metadata": {},+ },+ ],+ )++ async def test_context_compression(self) -> None:+ """Test the context compression logic."""+ model = MockModel(context_length=100)+ agent = Agent(+ name="Friday",+ system_prompt="".join(["0" for _ in range(20 * 4)]),+ model=model,+ compression=CompressionConfig(+ trigger_ratio=0.7,+ reserve_ratio=0.4,+ ),+ state=AgentState(+ session_id="123",+ context=[+ UserMsg(+ "User",+ "".join(["1" for _ in range(30 * 4)]),+ id="1",+ ),+ AssistantMsg(+ "Friday",+ "".join(["2" for _ in range(10 * 4)]),+ id="2",+ ),+ UserMsg(+ "User",+ "".join(["3" for _ in range(10 * 4)]),+ id="3",+ ),+ ],+ ),+ toolkit=Toolkit(),+ )++ model.set_structured_response(+ StructuredResponse(+ content={+ "task_overview": "1",+ "current_state": "2",+ "important_discoveries": "3",+ "next_steps": "4",+ "context_to_preserve": "5",+ },+ ),+ )++ await agent.compress_context()++ self.assertEqual(+ agent.state.summary,+ """<system-info>Here is a summary of your previous work+# Task Overview+1++# Current State+2++# Important Discoveries+3++# Next Steps+4++# Context to Preserve+5</system-info>""",+ )+ self.assertListEqual(+ [_.model_dump() for _ in agent.state.context],+ [+ {+ "id": "2",+ "created_at": AnyString(),+ "name": "Friday",+ "role": "assistant",+ "content": [+ {+ "id": AnyString(),+ "text": "2" * 40,+ "type": "text",+ },+ ],+ "metadata": {},+ },+ {+ "id": "3",+ "created_at": AnyString(),+ "name": "User",+ "role": "user",+ "content": "3" * 40,+ "metadata": {},+ },+ ],+ )++ async def asyncTearDown(self) -> None:+ """The async teardown method."""diff --git a/tests/utils.py b/tests/utils.pyindex f40a04992..90fada666 100644--- a/tests/utils.py+++ b/tests/utils.py@@ -1,8 +1,13 @@# -*- coding: utf-8 -*-"""The utility module for unit tests in agentscope."""-from typing import Any, AsyncGenerator+from typing import Any, AsyncGenerator, Type+from pydantic import BaseModel++from agentscope.formatter import DashScopeChatFormatter+from agentscope.message import Msgfrom agentscope.model import ChatModelBase, ChatResponse+from agentscope.model._model_response import StructuredResponseclass AnyString:@@ -20,16 +25,18 @@ class AnyString:class MockModel(ChatModelBase):"""A mock model for testing."""- def __init__(self) -> None:+ def __init__(self, context_length: int = 10000) -> None:"""Initialize the mock model."""super().__init__(model_name="mock-model",stream=True,+ context_length=context_length,max_retries=0,fallback_model_name=None,- formatter=None,+ formatter=DashScopeChatFormatter(),)- self.mock_responses = []+ self.mock_chat_responses = []+ self.mock_structured_response = Noneself.cnt = 0def set_responses(@@ -37,7 +44,7 @@ class MockModel(ChatModelBase):mock_responses: list[ChatResponse | list[ChatResponse]],) -> None:"""Set the mock responses."""- self.mock_responses = mock_responses+ self.mock_chat_responses = mock_responsesif all(isinstance(_, ChatResponse) for _ in mock_responses):self.stream = Falseelse:@@ -50,7 +57,7 @@ class MockModel(ChatModelBase):**kwargs: Any,) -> ChatResponse | AsyncGenerator[ChatResponse, None]:"""Mock the API call."""- mock_responses = self.mock_responses[self.cnt]+ mock_responses = self.mock_chat_responses[self.cnt]self.cnt += 1if isinstance(mock_responses, list):@@ -64,3 +71,20 @@ class MockModel(ChatModelBase):return mock_responsesraise AssertionError++ def set_structured_response(+ self,+ mock_response: StructuredResponse,+ ) -> None:+ """Set the mock structured responses."""+ self.mock_structured_response = mock_response++ async def _call_api_with_structured_output(+ self,+ model_name: str,+ messages: list[Msg],+ structured_model: Type[BaseModel] | dict,+ **kwargs: Any,+ ) -> StructuredResponse:+ """Mock the API call with structured output."""+ return self.mock_structured_response