memorisdk 1.0.2__py3-none-any.whl → 2.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of memorisdk might be problematic. Click here for more details.

Files changed (48) hide show
  1. memori/__init__.py +24 -8
  2. memori/agents/conscious_agent.py +252 -414
  3. memori/agents/memory_agent.py +487 -224
  4. memori/agents/retrieval_agent.py +491 -68
  5. memori/config/memory_manager.py +323 -0
  6. memori/core/conversation.py +393 -0
  7. memori/core/database.py +386 -371
  8. memori/core/memory.py +1683 -532
  9. memori/core/providers.py +217 -0
  10. memori/database/adapters/__init__.py +10 -0
  11. memori/database/adapters/mysql_adapter.py +331 -0
  12. memori/database/adapters/postgresql_adapter.py +291 -0
  13. memori/database/adapters/sqlite_adapter.py +229 -0
  14. memori/database/auto_creator.py +320 -0
  15. memori/database/connection_utils.py +207 -0
  16. memori/database/connectors/base_connector.py +283 -0
  17. memori/database/connectors/mysql_connector.py +240 -18
  18. memori/database/connectors/postgres_connector.py +277 -4
  19. memori/database/connectors/sqlite_connector.py +178 -3
  20. memori/database/models.py +400 -0
  21. memori/database/queries/base_queries.py +1 -1
  22. memori/database/queries/memory_queries.py +91 -2
  23. memori/database/query_translator.py +222 -0
  24. memori/database/schema_generators/__init__.py +7 -0
  25. memori/database/schema_generators/mysql_schema_generator.py +215 -0
  26. memori/database/search/__init__.py +8 -0
  27. memori/database/search/mysql_search_adapter.py +255 -0
  28. memori/database/search/sqlite_search_adapter.py +180 -0
  29. memori/database/search_service.py +700 -0
  30. memori/database/sqlalchemy_manager.py +888 -0
  31. memori/integrations/__init__.py +36 -11
  32. memori/integrations/litellm_integration.py +340 -6
  33. memori/integrations/openai_integration.py +506 -240
  34. memori/tools/memory_tool.py +94 -4
  35. memori/utils/input_validator.py +395 -0
  36. memori/utils/pydantic_models.py +138 -36
  37. memori/utils/query_builder.py +530 -0
  38. memori/utils/security_audit.py +594 -0
  39. memori/utils/security_integration.py +339 -0
  40. memori/utils/transaction_manager.py +547 -0
  41. {memorisdk-1.0.2.dist-info → memorisdk-2.0.1.dist-info}/METADATA +56 -23
  42. memorisdk-2.0.1.dist-info/RECORD +66 -0
  43. memori/scripts/llm_text.py +0 -50
  44. memorisdk-1.0.2.dist-info/RECORD +0 -44
  45. memorisdk-1.0.2.dist-info/entry_points.txt +0 -2
  46. {memorisdk-1.0.2.dist-info → memorisdk-2.0.1.dist-info}/WHEEL +0 -0
  47. {memorisdk-1.0.2.dist-info → memorisdk-2.0.1.dist-info}/licenses/LICENSE +0 -0
  48. {memorisdk-1.0.2.dist-info → memorisdk-2.0.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,323 @@
1
+ """
2
+ MemoryManager - Modular memory management system for Memori
3
+
4
+ This is a working implementation that coordinates interceptors and provides
5
+ a clean interface for memory management operations.
6
+ """
7
+
8
+ import uuid
9
+ from typing import Any, Dict, List, Optional
10
+
11
+ from loguru import logger
12
+
13
+ # Interceptor system removed - using LiteLLM native callbacks only
14
+
15
+
16
+ class MemoryManager:
17
+ """
18
+ Modular memory management system that coordinates interceptors,
19
+ memory processing, and context injection.
20
+
21
+ This class provides a clean interface for memory operations while
22
+ maintaining backward compatibility with the existing Memori system.
23
+ """
24
+
25
+ def __init__(
26
+ self,
27
+ database_connect: str = "sqlite:///memori.db",
28
+ template: str = "basic",
29
+ mem_prompt: Optional[str] = None,
30
+ conscious_ingest: bool = False,
31
+ auto_ingest: bool = False,
32
+ namespace: Optional[str] = None,
33
+ shared_memory: bool = False,
34
+ memory_filters: Optional[List[str]] = None,
35
+ user_id: Optional[str] = None,
36
+ verbose: bool = False,
37
+ provider_config: Optional[Any] = None,
38
+ # Additional parameters for compatibility
39
+ openai_api_key: Optional[str] = None,
40
+ api_key: Optional[str] = None,
41
+ api_type: Optional[str] = None,
42
+ base_url: Optional[str] = None,
43
+ azure_endpoint: Optional[str] = None,
44
+ azure_deployment: Optional[str] = None,
45
+ api_version: Optional[str] = None,
46
+ azure_ad_token: Optional[str] = None,
47
+ organization: Optional[str] = None,
48
+ **kwargs,
49
+ ):
50
+ """
51
+ Initialize the MemoryManager.
52
+
53
+ Args:
54
+ database_connect: Database connection string
55
+ template: Memory template to use
56
+ mem_prompt: Optional memory prompt
57
+ conscious_ingest: Enable conscious memory ingestion
58
+ auto_ingest: Enable automatic memory ingestion
59
+ namespace: Optional namespace for memory isolation
60
+ shared_memory: Enable shared memory across agents
61
+ memory_filters: Optional memory filters
62
+ user_id: Optional user identifier
63
+ verbose: Enable verbose logging
64
+ provider_config: Provider configuration
65
+ **kwargs: Additional parameters for forward compatibility
66
+ """
67
+ self.database_connect = database_connect
68
+ self.template = template
69
+ self.mem_prompt = mem_prompt
70
+ self.conscious_ingest = conscious_ingest
71
+ self.auto_ingest = auto_ingest
72
+ self.namespace = namespace
73
+ self.shared_memory = shared_memory
74
+ self.memory_filters = memory_filters or []
75
+ self.user_id = user_id
76
+ self.verbose = verbose
77
+ self.provider_config = provider_config
78
+
79
+ # Store additional configuration
80
+ self.openai_api_key = openai_api_key
81
+ self.api_key = api_key
82
+ self.api_type = api_type
83
+ self.base_url = base_url
84
+ self.azure_endpoint = azure_endpoint
85
+ self.azure_deployment = azure_deployment
86
+ self.api_version = api_version
87
+ self.azure_ad_token = azure_ad_token
88
+ self.organization = organization
89
+ self.kwargs = kwargs
90
+
91
+ self._session_id = str(uuid.uuid4())
92
+ self._enabled = False
93
+
94
+ # LiteLLM native callback manager
95
+ self.litellm_callback_manager = None
96
+
97
+ logger.info(f"MemoryManager initialized with session: {self._session_id}")
98
+
99
+ def set_memori_instance(self, memori_instance):
100
+ """Set the parent Memori instance for memory management."""
101
+ self.memori_instance = memori_instance
102
+
103
+ # Initialize LiteLLM callback manager
104
+ try:
105
+ from ..integrations.litellm_integration import setup_litellm_callbacks
106
+
107
+ self.litellm_callback_manager = setup_litellm_callbacks(memori_instance)
108
+ if self.litellm_callback_manager:
109
+ logger.debug("LiteLLM callback manager initialized")
110
+ else:
111
+ logger.warning("Failed to initialize LiteLLM callback manager")
112
+ except ImportError as e:
113
+ logger.warning(f"Could not initialize LiteLLM callback manager: {e}")
114
+
115
+ logger.debug("MemoryManager configured with Memori instance")
116
+
117
+ def enable(self, interceptors: Optional[List[str]] = None) -> Dict[str, Any]:
118
+ """
119
+ Enable memory recording using LiteLLM native callbacks.
120
+
121
+ Args:
122
+ interceptors: Legacy parameter (ignored, using LiteLLM callbacks)
123
+
124
+ Returns:
125
+ Dict containing enablement results
126
+ """
127
+ if self._enabled:
128
+ return {
129
+ "success": True,
130
+ "message": "Already enabled",
131
+ "enabled_interceptors": ["litellm_native"],
132
+ }
133
+
134
+ if interceptors is None:
135
+ interceptors = ["litellm_native"] # Only LiteLLM native callbacks supported
136
+
137
+ try:
138
+ # Enable LiteLLM native callback system
139
+ if (
140
+ self.litellm_callback_manager
141
+ and not self.litellm_callback_manager.is_registered
142
+ ):
143
+ success = self.litellm_callback_manager.register_callbacks()
144
+ if not success:
145
+ return {
146
+ "success": False,
147
+ "message": "Failed to register LiteLLM callbacks",
148
+ }
149
+ elif not self.litellm_callback_manager:
150
+ logger.warning("No LiteLLM callback manager available")
151
+
152
+ self._enabled = True
153
+
154
+ logger.info("MemoryManager enabled with LiteLLM native callbacks")
155
+
156
+ return {
157
+ "success": True,
158
+ "message": "Enabled LiteLLM native callback system",
159
+ "enabled_interceptors": ["litellm_native"],
160
+ }
161
+ except Exception as e:
162
+ logger.error(f"Failed to enable MemoryManager: {e}")
163
+ return {"success": False, "message": str(e)}
164
+
165
+ def disable(self) -> Dict[str, Any]:
166
+ """
167
+ Disable memory recording using LiteLLM native callbacks.
168
+
169
+ Returns:
170
+ Dict containing disable results
171
+ """
172
+ if not self._enabled:
173
+ return {"success": True, "message": "Already disabled"}
174
+
175
+ try:
176
+ # Disable LiteLLM native callback system
177
+ if (
178
+ self.litellm_callback_manager
179
+ and self.litellm_callback_manager.is_registered
180
+ ):
181
+ success = self.litellm_callback_manager.unregister_callbacks()
182
+ if not success:
183
+ logger.warning("Failed to unregister LiteLLM callbacks")
184
+
185
+ self._enabled = False
186
+
187
+ logger.info("MemoryManager disabled")
188
+
189
+ return {
190
+ "success": True,
191
+ "message": "MemoryManager disabled successfully (LiteLLM native callbacks)",
192
+ }
193
+ except Exception as e:
194
+ logger.error(f"Failed to disable MemoryManager: {e}")
195
+ return {"success": False, "message": str(e)}
196
+
197
+ def get_status(self) -> Dict[str, Dict[str, Any]]:
198
+ """
199
+ Get status of memory recording system.
200
+
201
+ Returns:
202
+ Dict containing memory system status information
203
+ """
204
+ callback_status = "inactive"
205
+ if self.litellm_callback_manager:
206
+ if self.litellm_callback_manager.is_registered:
207
+ callback_status = "active"
208
+ else:
209
+ callback_status = "available_but_not_registered"
210
+ else:
211
+ callback_status = "unavailable"
212
+
213
+ return {
214
+ "litellm_native": {
215
+ "enabled": self._enabled,
216
+ "status": callback_status,
217
+ "method": "litellm_callbacks",
218
+ "session_id": self._session_id,
219
+ "callback_manager": self.litellm_callback_manager is not None,
220
+ }
221
+ }
222
+
223
+ def get_health(self) -> Dict[str, Any]:
224
+ """
225
+ Get health check of the memory management system.
226
+
227
+ Returns:
228
+ Dict containing health information
229
+ """
230
+ return {
231
+ "session_id": self._session_id,
232
+ "enabled": self._enabled,
233
+ "namespace": self.namespace,
234
+ "user_id": self.user_id,
235
+ "litellm_callback_manager": self.litellm_callback_manager is not None,
236
+ "litellm_callbacks_registered": (
237
+ self.litellm_callback_manager.is_registered
238
+ if self.litellm_callback_manager
239
+ else False
240
+ ),
241
+ "memory_filters": len(self.memory_filters),
242
+ "conscious_ingest": self.conscious_ingest,
243
+ "auto_ingest": self.auto_ingest,
244
+ "database_connect": self.database_connect,
245
+ "template": self.template,
246
+ }
247
+
248
+ # === BACKWARD COMPATIBILITY PROPERTIES ===
249
+
250
+ @property
251
+ def session_id(self) -> str:
252
+ """Get session ID for backward compatibility."""
253
+ return self._session_id
254
+
255
+ @property
256
+ def enabled(self) -> bool:
257
+ """Check if enabled for backward compatibility."""
258
+ return self._enabled
259
+
260
+ # === PLACEHOLDER METHODS FOR FUTURE MODULAR COMPONENTS ===
261
+
262
+ def record_conversation(
263
+ self,
264
+ user_input: str,
265
+ ai_output: str,
266
+ model: Optional[str] = None,
267
+ metadata: Optional[Dict[str, Any]] = None,
268
+ ) -> str:
269
+ """
270
+ Record a conversation (placeholder for future implementation).
271
+
272
+ Returns:
273
+ Placeholder conversation ID
274
+ """
275
+ logger.info(f"Recording conversation (placeholder): {user_input[:50]}...")
276
+ return str(uuid.uuid4())
277
+
278
+ def search_memories(
279
+ self,
280
+ query: str,
281
+ limit: int = 5,
282
+ memory_types: Optional[List[str]] = None,
283
+ categories: Optional[List[str]] = None,
284
+ min_importance: Optional[float] = None,
285
+ ) -> List[Dict[str, Any]]:
286
+ """
287
+ Search memories (placeholder for future implementation).
288
+
289
+ Returns:
290
+ Empty list (placeholder)
291
+ """
292
+ logger.info(f"Searching memories (placeholder): {query}")
293
+ return []
294
+
295
+ def cleanup(self):
296
+ """Cleanup resources."""
297
+ try:
298
+ if self._enabled:
299
+ self.disable()
300
+
301
+ # Clean up callback manager
302
+ if self.litellm_callback_manager:
303
+ self.litellm_callback_manager.unregister_callbacks()
304
+ self.litellm_callback_manager = None
305
+
306
+ logger.info("MemoryManager cleanup completed")
307
+ except Exception as e:
308
+ logger.error(f"Error during MemoryManager cleanup: {e}")
309
+
310
+ def __enter__(self):
311
+ """Context manager entry."""
312
+ return self
313
+
314
+ def __exit__(self, exc_type, exc_val, exc_tb):
315
+ """Context manager exit."""
316
+ self.cleanup()
317
+
318
+ def __del__(self):
319
+ """Destructor - ensure cleanup."""
320
+ try:
321
+ self.cleanup()
322
+ except:
323
+ pass