Coverage for src / agent / memory / __init__.py: 77%

35 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-11 14:30 +0000

1# Copyright 2025-2026 Microsoft Corporation 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15"""Memory management for Agent conversations. 

16 

17This module provides in-memory and semantic (mem0) storage capabilities for 

18maintaining conversation context across multiple interactions, enabling agents 

19to remember preferences, recall previous conversations, and provide personalized 

20experiences. 

21 

22Key Components: 

23 - MemoryManager: Abstract base class for memory operations 

24 - InMemoryStore: In-memory implementation with keyword search 

25 - Mem0Store: Semantic memory with vector-based search (optional) 

26 - MemoryPersistence: Serialization and persistence utilities 

27 

28Example: 

29 >>> from agent.memory import create_memory_manager 

30 >>> memory = create_memory_manager(config) 

31 >>> await memory.add([{"role": "user", "content": "Hello"}]) 

32""" 

33 

34import logging 

35from typing import TYPE_CHECKING 

36 

37from agent.memory.context_provider import MemoryContextProvider 

38from agent.memory.manager import MemoryManager 

39from agent.memory.store import InMemoryStore 

40 

41# Conditional import for optional mem0 dependency 

42try: 

43 from agent.memory.mem0_store import Mem0Store 

44except ImportError: 

45 # mem0 not available, but that's okay - it's optional 

46 pass 

47 

48if TYPE_CHECKING: 

49 from agent.config.schema import AgentSettings 

50 

51logger = logging.getLogger(__name__) 

52 

53__all__ = [ 

54 "MemoryManager", 

55 "InMemoryStore", 

56 "MemoryContextProvider", 

57 "create_memory_manager", 

58] 

59 

60# Only export Mem0Store if it's available 

61try: 

62 from agent.memory.mem0_store import Mem0Store # noqa: F401 

63 

64 __all__.append("Mem0Store") 

65except ImportError: 

66 # Mem0Store is optional; ignore if mem0ai package not installed 

67 pass 

68 

69 

70def create_memory_manager(config: "AgentSettings") -> MemoryManager: 

71 """Factory function to create memory manager based on config. 

72 

73 Routes to appropriate memory backend based on config.memory_type: 

74 - "in_memory": InMemoryStore (keyword search, ephemeral) 

75 - "mem0": Mem0Store (semantic search, persistent) 

76 

77 Falls back to InMemoryStore if mem0 initialization fails or provider is incompatible. 

78 

79 Args: 

80 config: AgentSettings instance with memory settings 

81 

82 Returns: 

83 MemoryManager instance 

84 

85 Example: 

86 >>> config = AgentConfig(memory_enabled=True, memory_type="mem0") 

87 >>> manager = create_memory_manager(config) 

88 """ 

89 if config.memory_type == "mem0": 

90 # Check provider compatibility first 

91 try: 

92 from agent.memory.mem0_utils import SUPPORTED_PROVIDERS, is_provider_compatible 

93 

94 is_compatible, reason = is_provider_compatible(config) 

95 if not is_compatible: 

96 logger.warning( 

97 f"mem0 not available: {reason}. " 

98 f"Falling back to InMemoryStore. " 

99 f"To use mem0, switch to a supported provider ({', '.join(SUPPORTED_PROVIDERS)})." 

100 ) 

101 return InMemoryStore(config) 

102 except ImportError: 

103 # mem0_utils not available, continue and let Mem0Store import fail 

104 pass 

105 

106 try: 

107 # Lazy import to avoid dependency if not using mem0 

108 from agent.memory.mem0_store import Mem0Store 

109 

110 logger.info("Creating Mem0Store for semantic memory") 

111 return Mem0Store(config) 

112 except Exception as e: 

113 logger.warning( 

114 f"Failed to initialize Mem0Store: {e}. " 

115 "Falling back to InMemoryStore. " 

116 "Ensure mem0ai package is installed: uv pip install -e '.[mem0]'" 

117 ) 

118 # Fall back to InMemoryStore 

119 return InMemoryStore(config) 

120 else: 

121 # Default to InMemoryStore 

122 logger.debug(f"Creating InMemoryStore for memory type: {config.memory_type}") 

123 return InMemoryStore(config)