""" Lab 2.9: Context-Aware Agent Implement sliding window + summarization for long agent sessions. """ class ContextManager: """Manages the message context with sliding window + summarization.""" def __init__(self, max_recent_turns: int = 5, system_prompt: str = ""): self.system_prompt = system_prompt self.max_recent_turns = max_recent_turns self.summary = "" self.recent_messages = [] def add_message(self, role: str, content: str): """Add a message and manage context window.""" # TODO: Add message to recent_messages # If recent_messages exceeds max_recent_turns * 2 (for user+assistant pairs): # 1. Summarize the oldest messages # 2. Update self.summary # 3. Remove those messages from recent pass def build_context(self) -> list[dict]: """ Build the full context: [system_prompt] + [summary (if exists)] + [recent_messages] """ pass # TODO def summarize(self, messages: list[dict]) -> str: """Summarize a list of messages into a condensed form.""" # TODO: Either use LLM summarization or simple truncation pass if __name__ == "__main__": cm = ContextManager(system_prompt="You are a helpful assistant.") # Simulate a long conversation for i in range(20): user_msg = f"This is user message number {i+1} asking about topic A" asst_msg = f"This is assistant response number {i+1}" cm.add_message("user", user_msg) cm.add_message("assistant", asst_msg) context = cm.build_context() print(f"Context length: {len(context)} messages") print(f"Summary: {context[1] if len(context) > 1 else 'No summary'}")