|
10 | 10 | Dict,
|
11 | 11 | List,
|
12 | 12 | Mapping,
|
| 13 | + Optional, |
13 | 14 | Sequence,
|
14 | 15 | )
|
15 | 16 |
|
16 | 17 | from autogen_core import CancellationToken, FunctionCall
|
17 | 18 | from autogen_core.model_context import (
|
18 |
| - BufferedChatCompletionContext, |
19 | 19 | ChatCompletionContext,
|
| 20 | + UnboundedBufferedChatCompletionContext, |
20 | 21 | )
|
21 | 22 | from autogen_core.models import (
|
22 | 23 | AssistantMessage,
|
@@ -227,7 +228,7 @@ def __init__(
|
227 | 228 | self,
|
228 | 229 | name: str,
|
229 | 230 | model_client: ChatCompletionClient,
|
230 |
| - model_context: ChatCompletionContext = BufferedChatCompletionContext(0), |
| 231 | + model_context: Optional[ChatCompletionContext] = None, |
231 | 232 | *,
|
232 | 233 | tools: List[Tool | Callable[..., Any] | Callable[..., Awaitable[Any]]] | None = None,
|
233 | 234 | handoffs: List[HandoffBase | str] | None = None,
|
@@ -286,7 +287,8 @@ def __init__(
|
286 | 287 | raise ValueError(
|
287 | 288 | f"Handoff names must be unique from tool names. Handoff names: {handoff_tool_names}; tool names: {tool_names}"
|
288 | 289 | )
|
289 |
| - self._model_context = model_context |
| 290 | + if not model_context: |
| 291 | + self._model_context = UnboundedBufferedChatCompletionContext() |
290 | 292 | self._reflect_on_tool_use = reflect_on_tool_use
|
291 | 293 | self._tool_call_summary_format = tool_call_summary_format
|
292 | 294 | self._is_running = False
|
@@ -420,11 +422,14 @@ async def on_reset(self, cancellation_token: CancellationToken) -> None:
|
420 | 422 |
|
421 | 423 | async def save_state(self) -> Mapping[str, Any]:
|
422 | 424 | """Save the current state of the assistant agent."""
|
423 |
| - return AssistantAgentState(llm_messages=await self._model_context.get_messages()).model_dump() |
| 425 | + current_model_ctx_state = self._model_context.save_state() |
| 426 | + return AssistantAgentState(llm_messages=current_model_ctx_state["messages"]).model_dump() |
424 | 427 |
|
425 | 428 | async def load_state(self, state: Mapping[str, Any]) -> None:
|
426 | 429 | """Load the state of the assistant agent"""
|
427 | 430 | assistant_agent_state = AssistantAgentState.model_validate(state)
|
428 | 431 | await self._model_context.clear()
|
429 |
| - for message in assistant_agent_state.llm_messages: |
430 |
| - await self._model_context.add_message(message) |
| 432 | + |
| 433 | + current_model_ctx_state = dict(self._model_context.save_state()) |
| 434 | + current_model_ctx_state["messages"] = assistant_agent_state.llm_messages |
| 435 | + self._model_context.load_state(current_model_ctx_state) |
0 commit comments