"""
ContextualMemory
Generated by Eden via recursive self-improvement
2025-10-28 08:06:16.116728
"""

class ContextualMemory:
    """
    A class to store and manage contextual information about user interactions.
    
    Attributes:
        memory (list): List of dictionaries storing interaction data.
        max_storage (int): Maximum number of interactions to keep in memory.
        
    Methods:
        __init__(self, max_storage=10):
            Initializes the ContextualMemory object with specified storage capacity.
            
        add_interaction(self, query: str, response: str) -> None:
            Stores a new interaction in memory.
            
        get_recent_interactions(self, n: int = 5) -> list:
            Returns the most recent interactions.
            
        search_by_keyword(self, keyword: str) -> list:
            Searches for interactions containing specified keyword and returns matching results.
            
        clear_memory(self) -> None:
            Clears all stored interactions.
    """

    def __init__(self, max_storage: int = 10):
        """
        Initializes the ContextualMemory object.

        Args:
            max_storage (int): Maximum number of interactions to store. Defaults to 10.
        """
        self.memory = []
        self.max_storage = max_storage

    def add_interaction(self, query: str, response: str) -> None:
        """
        Stores a new interaction in memory.

        Args:
            query (str): The user's input/query.
            response (str): The system's response.
        """
        interaction = {
            "query": query,
            "response": response,
            "timestamp": time.time()
        }
        self.memory.append(interaction)
        
        # Enforce max storage limit
        if len(self.memory) > self.max_storage:
            self.memory.pop(0)

    def get_recent_interactions(self, n: int = 5) -> list:
        """
        Returns the most recent interactions.

        Args:
            n (int): Number of recent interactions to return. Defaults to 5.

        Returns:
            list: List of interaction dictionaries.
        """
        return self.memory[-n:] if len(self.memory) >= n else self.memory

    def search_by_keyword(self, keyword: str) -> list:
        """
        Searches through stored interactions for the specified keyword and returns all matches.

        Args:
            keyword (str): The keyword to search for.

        Returns:
            list: List of interaction dictionaries containing the keyword.
        """
        matches = []
        for interaction in self.memory:
            if keyword in interaction["query"] or keyword in interaction["response"]:
                matches.append(interaction)
        return matches

    def clear_memory(self) -> None:
        """Clears all stored interactions."""
        self.memory = []
# Initialize memory with max storage of 10 interactions
memory = ContextualMemory()

# Add some interactions
memory.add_interaction("hello", "Hello! How can I help you today?")
memory.add_interaction("what's the time", "I don't have access to real-time data.")
memory.add_interaction("weather in new york", "I'm unable to provide实时天气 information.")

# Get recent interactions (default 5)
print(memory.get_recent_interactions())

# Search for a keyword
results = memory.search_by_keyword("time")
for result in results:
    print(f"Query: {result['query']}, Response: {result['response']}")

# Clear all interactions
memory.clear_memory()