powr-memory-sdk 1.0.1__tar.gz

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.
Files changed (37) hide show
  1. powr_memory_sdk-1.0.1/PKG-INFO +451 -0
  2. powr_memory_sdk-1.0.1/README.md +424 -0
  3. powr_memory_sdk-1.0.1/pyproject.toml +52 -0
  4. powr_memory_sdk-1.0.1/setup.cfg +4 -0
  5. powr_memory_sdk-1.0.1/src/memory_sdk/__init__.py +185 -0
  6. powr_memory_sdk-1.0.1/src/memory_sdk/base.py +110 -0
  7. powr_memory_sdk-1.0.1/src/memory_sdk/central/__init__.py +14 -0
  8. powr_memory_sdk-1.0.1/src/memory_sdk/central/client.py +201 -0
  9. powr_memory_sdk-1.0.1/src/memory_sdk/central/engine.py +84 -0
  10. powr_memory_sdk-1.0.1/src/memory_sdk/central/session.py +277 -0
  11. powr_memory_sdk-1.0.1/src/memory_sdk/factory.py +118 -0
  12. powr_memory_sdk-1.0.1/src/memory_sdk/local/__init__.py +46 -0
  13. powr_memory_sdk-1.0.1/src/memory_sdk/local/context.py +393 -0
  14. powr_memory_sdk-1.0.1/src/memory_sdk/local/engine.py +126 -0
  15. powr_memory_sdk-1.0.1/src/memory_sdk/local/exceptions.py +45 -0
  16. powr_memory_sdk-1.0.1/src/memory_sdk/local/langchain.py +155 -0
  17. powr_memory_sdk-1.0.1/src/memory_sdk/local/models.py +86 -0
  18. powr_memory_sdk-1.0.1/src/memory_sdk/local/session.py +642 -0
  19. powr_memory_sdk-1.0.1/src/memory_sdk/local/sqlite_storage.py +333 -0
  20. powr_memory_sdk-1.0.1/src/memory_sdk/local/state.py +179 -0
  21. powr_memory_sdk-1.0.1/src/memory_sdk/local/stm.py +154 -0
  22. powr_memory_sdk-1.0.1/src/memory_sdk/local/storage.py +72 -0
  23. powr_memory_sdk-1.0.1/src/memory_sdk/local/tools.py +175 -0
  24. powr_memory_sdk-1.0.1/src/memory_sdk/server/__init__.py +4 -0
  25. powr_memory_sdk-1.0.1/src/memory_sdk/server/server.py +414 -0
  26. powr_memory_sdk-1.0.1/src/memory_sdk/wrappers/__init__.py +10 -0
  27. powr_memory_sdk-1.0.1/src/memory_sdk/wrappers/openai_wrapper.py +167 -0
  28. powr_memory_sdk-1.0.1/src/powr_memory_sdk.egg-info/PKG-INFO +451 -0
  29. powr_memory_sdk-1.0.1/src/powr_memory_sdk.egg-info/SOURCES.txt +35 -0
  30. powr_memory_sdk-1.0.1/src/powr_memory_sdk.egg-info/dependency_links.txt +1 -0
  31. powr_memory_sdk-1.0.1/src/powr_memory_sdk.egg-info/entry_points.txt +2 -0
  32. powr_memory_sdk-1.0.1/src/powr_memory_sdk.egg-info/requires.txt +21 -0
  33. powr_memory_sdk-1.0.1/src/powr_memory_sdk.egg-info/top_level.txt +1 -0
  34. powr_memory_sdk-1.0.1/tests/test_central_engine.py +103 -0
  35. powr_memory_sdk-1.0.1/tests/test_local_engine.py +56 -0
  36. powr_memory_sdk-1.0.1/tests/test_openai_wrapper.py +66 -0
  37. powr_memory_sdk-1.0.1/tests/test_unified_factory.py +75 -0
@@ -0,0 +1,451 @@
1
+ Metadata-Version: 2.4
2
+ Name: powr-memory-sdk
3
+ Version: 1.0.1
4
+ Summary: Universal AI Agent Memory SDK: Embedded Local SQLite STM + Central Distributed MCP Server in one unified API.
5
+ Author: Lawazia AI Team
6
+ License-Expression: MIT
7
+ Keywords: ai,agent,memory,stm,mcp,model-context-protocol,working-memory,context,openai,langchain
8
+ Requires-Python: >=3.10
9
+ Description-Content-Type: text/markdown
10
+ Requires-Dist: pydantic<3.0,>=2.0
11
+ Requires-Dist: mcp>=1.0.0
12
+ Provides-Extra: openai
13
+ Requires-Dist: openai>=1.0.0; extra == "openai"
14
+ Provides-Extra: server
15
+ Requires-Dist: uvicorn>=0.20.0; extra == "server"
16
+ Requires-Dist: fastapi>=0.100.0; extra == "server"
17
+ Provides-Extra: dev
18
+ Requires-Dist: pytest>=7.0; extra == "dev"
19
+ Requires-Dist: pytest-cov>=4.0; extra == "dev"
20
+ Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
21
+ Provides-Extra: all
22
+ Requires-Dist: openai>=1.0.0; extra == "all"
23
+ Requires-Dist: uvicorn>=0.20.0; extra == "all"
24
+ Requires-Dist: fastapi>=0.100.0; extra == "all"
25
+ Requires-Dist: langchain-core>=0.1.0; extra == "all"
26
+ Requires-Dist: pytest>=7.0; extra == "all"
27
+
28
+ # ๐Ÿง  powr-memory-sdk
29
+
30
+ **Universal AI Agent Memory SDK** โ€” Embedded Local SQLite + Central Distributed MCP Server, one unified API.
31
+
32
+ Give your AI agents persistent memory across turns, users, and services โ€” in **4 lines of code**.
33
+
34
+ [![PyPI version](https://img.shields.io/pypi/v/powr-memory-sdk)](https://pypi.org/project/powr-memory-sdk/)
35
+ [![Python](https://img.shields.io/pypi/pyversions/powr-memory-sdk)](https://pypi.org/project/powr-memory-sdk/)
36
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
37
+
38
+ ---
39
+
40
+ ## โšก Quickstart
41
+
42
+ ```bash
43
+ pip install powr-memory-sdk
44
+ ```
45
+
46
+ ```python
47
+ from memory_sdk import mem
48
+
49
+ session = mem("user_123")
50
+ session.remember("name", "Gunit")
51
+ session.add_user("Mujhe Python sikhna hai")
52
+ messages = session.to_messages("You are a helpful assistant.")
53
+ # โ†’ Pass `messages` directly to any LLM (OpenAI, Groq, Anthropic, Ollama)
54
+ ```
55
+
56
+ **That's it.** No server to start, no config file, no database setup.
57
+
58
+ ---
59
+
60
+ ## ๐Ÿ—๏ธ Two Modes โ€” One API
61
+
62
+ | | **Local Mode** | **Central Mode** |
63
+ |---|---|---|
64
+ | **Storage** | Embedded SQLite (your machine) | Deployed MCP Server (shared) |
65
+ | **Best for** | Dev, CLI tools, single agent | Production, multi-agent, microservices |
66
+ | **Setup** | Zero โ€” just a file path | Zero โ€” uses deployed server by default |
67
+ | **Latency** | Sub-millisecond | ~5โ€“20 ms network |
68
+ | **API** | โœ… Identical | โœ… Identical |
69
+
70
+ > **The API is 100% identical across both modes** โ€” switch anytime with one line.
71
+
72
+ ---
73
+
74
+ ## ๐Ÿ“ฆ Installation
75
+
76
+ ```bash
77
+ # Basic (local SQLite memory only)
78
+ pip install powr-memory-sdk
79
+
80
+ # With OpenAI support
81
+ pip install "powr-memory-sdk[openai]"
82
+
83
+ # With LangChain support
84
+ pip install "powr-memory-sdk[openai,langchain-core]"
85
+
86
+ # Everything
87
+ pip install "powr-memory-sdk[all]"
88
+ ```
89
+
90
+ ---
91
+
92
+ ## ๐Ÿš€ Usage
93
+
94
+ ### Zero-Config โ€” `mem` Singleton (Recommended)
95
+
96
+ ```python
97
+ from memory_sdk import mem
98
+
99
+ # Uses local SQLite automatically โ€” safe, zero-config, keeps data on your machine
100
+ session = mem("user_123")
101
+ session.remember("name", "Ali")
102
+ session.remember({"city": "Lahore", "diet": "Vegetarian"})
103
+
104
+ session.add_user("Dinner mein kya khaaon?")
105
+ session.add_assistant("Lahore mein vegetarian ke liye Dal Makhani best hai!")
106
+
107
+ messages = session.to_messages("You are a helpful assistant.")
108
+ # โ†’ Ready to pass to any LLM API
109
+ ```
110
+
111
+ ### Switch Mode โ€” One Line
112
+
113
+ ```python
114
+ from memory_sdk import mem
115
+
116
+ # Custom local SQLite database file
117
+ mem.configure(mode="local", db_path="app.db")
118
+
119
+ # Central MCP server (for distributed / multi-agent architectures)
120
+ mem.configure(mode="central", url="http://localhost:8765/mcp")
121
+
122
+ # Back to default local storage
123
+ mem.configure(mode="local")
124
+ ```
125
+
126
+ ---
127
+
128
+ ## ๐Ÿ”Œ Integration Examples
129
+
130
+ ### OpenAI
131
+
132
+ ```python
133
+ from openai import OpenAI
134
+ from memory_sdk import mem
135
+
136
+ client = OpenAI()
137
+
138
+ def chat(user_id: str, message: str) -> str:
139
+ session = mem(user_id)
140
+ session.add_user(message)
141
+
142
+ response = client.chat.completions.create(
143
+ model="gpt-4o-mini",
144
+ messages=session.to_messages("You are a helpful assistant.")
145
+ )
146
+ reply = response.choices[0].message.content
147
+ session.add_assistant(reply)
148
+ return reply
149
+ ```
150
+
151
+ ### AutoOpenAI โ€” 1-Line Drop-in Replacement
152
+
153
+ ```python
154
+ # Change just this one line in your existing OpenAI project:
155
+ # from openai import OpenAI
156
+ from memory_sdk import AutoOpenAI as OpenAI
157
+
158
+ client = OpenAI(session_id="user_123") # Memory is fully automatic
159
+
160
+ response = client.chat.completions.create(
161
+ model="gpt-4o-mini",
162
+ messages=[{"role": "user", "content": "Mera naam Ali hai"}]
163
+ )
164
+ # SDK automatically saves the message and injects memory on next call
165
+ ```
166
+
167
+ ### FastAPI
168
+
169
+ ```python
170
+ from fastapi import FastAPI
171
+ from memory_sdk import mem
172
+
173
+ app = FastAPI()
174
+
175
+ @app.on_event("startup")
176
+ def setup():
177
+ mem.configure(mode="local", db_path="prod.db", window_size=10)
178
+
179
+ @app.post("/chat/{user_id}")
180
+ async def chat(user_id: str, message: str):
181
+ session = mem(user_id)
182
+ session.add_user(message)
183
+ messages = session.to_messages("You are a helpful assistant.")
184
+ # ... your LLM call here
185
+ reply = "Hello!"
186
+ session.add_assistant(reply)
187
+ return {"reply": reply}
188
+ ```
189
+
190
+ ### Flask
191
+
192
+ ```python
193
+ from flask import Flask, request, jsonify
194
+ from memory_sdk import mem
195
+
196
+ app = Flask(__name__)
197
+ mem.configure(mode="local", db_path="app.db")
198
+
199
+ @app.route("/chat/<user_id>", methods=["POST"])
200
+ def chat(user_id):
201
+ message = request.json["message"]
202
+ session = mem(user_id)
203
+ session.add_user(message)
204
+ messages = session.to_messages("You are a helpful assistant.")
205
+ return jsonify({"messages": messages})
206
+ ```
207
+
208
+ ### LangChain Agent
209
+
210
+ ```python
211
+ from langchain_openai import ChatOpenAI
212
+ from memory_sdk import mem
213
+
214
+ llm = ChatOpenAI(model="gpt-4o-mini")
215
+
216
+ def chat(user_id: str, message: str) -> str:
217
+ session = mem(user_id)
218
+ agent = llm.bind_tools(session.get_langchain_tools())
219
+
220
+ session.add_user(message)
221
+ response = agent.invoke(session.to_messages("You are helpful."))
222
+ session.add_assistant(response.content)
223
+ return response.content
224
+ ```
225
+
226
+ ### Telegram / WhatsApp Bot
227
+
228
+ ```python
229
+ from memory_sdk import mem
230
+
231
+ def handle_message(chat_id: str, message: str) -> str:
232
+ session = mem(chat_id) # chat_id = unique user identifier
233
+ session.add_user(message)
234
+ messages = session.to_messages("You are a helpful assistant.")
235
+ reply = your_llm(messages) # your LLM call
236
+ session.add_assistant(reply)
237
+ return reply
238
+ ```
239
+
240
+ ---
241
+
242
+ ## ๐Ÿ“– Full API Reference
243
+
244
+ ### `mem` โ€” Zero-Config Singleton
245
+
246
+ ```python
247
+ from memory_sdk import mem
248
+
249
+ mem.configure(mode, **kwargs) # Configure once at startup
250
+ session = mem("user_id") # Get/create a session (callable)
251
+ session = mem.session("user_id") # Same โ€” explicit style
252
+ mem.close() # Release resources
253
+ ```
254
+
255
+ ### Session Methods
256
+
257
+ ```python
258
+ session = mem("user_123")
259
+
260
+ # โ”€โ”€ Save facts โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
261
+ session.remember("key", "value") # single fact
262
+ session.remember({"k1": "v1", "k2": "v2"}) # multiple facts at once
263
+
264
+ # โ”€โ”€ Read facts โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
265
+ session.recall("key") # one fact โ†’ value or None
266
+ session.recall() # all facts โ†’ dict
267
+ session.profile() # all facts โ†’ dict (alias)
268
+
269
+ # โ”€โ”€ Conversation โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
270
+ session.add_user("...") # record user message
271
+ session.add_assistant("...") # record assistant reply
272
+ session.add_system("...") # record system message
273
+ session.history(limit=10) # recent messages list
274
+
275
+ # โ”€โ”€ LLM-ready output โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
276
+ session.to_messages(
277
+ system_prompt="You are helpful.", # optional base prompt
278
+ include_memory=True, # inject facts into system message
279
+ )
280
+ # โ†’ [{"role": "system", "content": "..."}, {"role": "user", ...}, ...]
281
+
282
+ # โ”€โ”€ Cleanup โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
283
+ session.clear(keep_state=True) # clear messages, keep facts
284
+ session.forget() # wipe everything (messages + facts)
285
+ ```
286
+
287
+ ### `mem.configure()` Options
288
+
289
+ ```python
290
+ mem.configure(
291
+ mode="central", # "central" (default) or "local"
292
+
293
+ # Central mode options:
294
+ url="http://...", # MCP server URL (or set MCP_SERVER_URL env var)
295
+ project="my_app", # namespace for session isolation
296
+
297
+ # Local mode options:
298
+ db_path="app.db", # SQLite file path (or ":memory:" for transient)
299
+ window_size=10, # max recent messages to keep in context
300
+ auto_summarize=False, # LLM-based rolling summarization
301
+ )
302
+ ```
303
+
304
+ ---
305
+
306
+ ## โš™๏ธ Environment Variables
307
+
308
+ Set these instead of hardcoding in code:
309
+
310
+ ```bash
311
+ # Central server URL (overrides built-in default)
312
+ MCP_SERVER_URL=https://your-server.run.app/mcp
313
+
314
+ # Alternate env var name
315
+ MEMORY_SERVER_URL=https://your-server.run.app/mcp
316
+
317
+ # Force a mode (optional)
318
+ MEMORY_MODE=local
319
+
320
+ # Local DB path (optional)
321
+ MEMORY_DB_PATH=./data/agent.db
322
+
323
+ # Model for auto-summarization
324
+ MEMORY_SUMMARIZER_MODEL=gpt-4o-mini
325
+
326
+ # OpenAI key (needed only for auto_summarize=True)
327
+ OPENAI_API_KEY=sk-...
328
+ ```
329
+
330
+ ---
331
+
332
+ ## ๐Ÿƒ Running Your Own Central Server
333
+
334
+ ```bash
335
+ # Install server extras
336
+ pip install "powr-memory-sdk[server]"
337
+
338
+ # Start the MCP memory server
339
+ memory-server --port 8765
340
+
341
+ # Point your app to it
342
+ export MCP_SERVER_URL=http://localhost:8765/mcp
343
+ ```
344
+
345
+ Or with Docker:
346
+
347
+ ```bash
348
+ docker build -t memory-server .
349
+ docker run -p 8765:8765 -e PORT=8765 memory-server
350
+ ```
351
+
352
+ Then in your code:
353
+
354
+ ```python
355
+ from memory_sdk import mem
356
+ mem.configure(mode="central", url="http://localhost:8765/mcp")
357
+ ```
358
+
359
+ ---
360
+
361
+ ## ๐ŸŒ Central MCP Server โ€” Live Deployed
362
+
363
+ A central memory server is already live and ready to use:
364
+
365
+ ```
366
+ URL : https://memory-module-813340036970.asia-south1.run.app/mcp
367
+ Region : asia-south1 (Mumbai)
368
+ Protocol: Streamable HTTP (MCP)
369
+ ```
370
+
371
+ This is the **default** when you use `mode="central"` without specifying a `url`. All session data is namespaced by `project` so different apps don't interfere.
372
+
373
+ ---
374
+
375
+ ## ๐Ÿงช Testing
376
+
377
+ ```bash
378
+ # Run all tests
379
+ pytest tests/ -v
380
+
381
+ # With coverage
382
+ pytest tests/ --cov=memory_sdk --cov-report=term-missing
383
+ ```
384
+
385
+ **Testing tip โ€” use in-memory SQLite:**
386
+
387
+ ```python
388
+ # conftest.py
389
+ import pytest
390
+ from memory_sdk import mem
391
+
392
+ @pytest.fixture(autouse=True)
393
+ def isolate_memory():
394
+ mem.configure(mode="local", db_path=":memory:")
395
+ yield
396
+ mem.close()
397
+ ```
398
+
399
+ ---
400
+
401
+ ## ๐Ÿ—‚๏ธ Project Structure
402
+
403
+ ```
404
+ memory-sdk/
405
+ โ”œโ”€โ”€ src/memory_sdk/
406
+ โ”‚ โ”œโ”€โ”€ __init__.py โ† mem singleton + all exports
407
+ โ”‚ โ”œโ”€โ”€ factory.py โ† Memory() factory router
408
+ โ”‚ โ”œโ”€โ”€ base.py โ† BaseMemory / BaseSession protocols
409
+ โ”‚ โ”œโ”€โ”€ local/ โ† Embedded SQLite engine
410
+ โ”‚ โ”‚ โ”œโ”€โ”€ engine.py
411
+ โ”‚ โ”‚ โ”œโ”€โ”€ session.py โ† Full session + sliding window + summarizer
412
+ โ”‚ โ”‚ โ”œโ”€โ”€ sqlite_storage.py
413
+ โ”‚ โ”‚ โ”œโ”€โ”€ tools.py โ† OpenAI function-call tool schemas
414
+ โ”‚ โ”‚ โ””โ”€โ”€ langchain.py โ† LangChain tool wrappers
415
+ โ”‚ โ”œโ”€โ”€ central/ โ† Distributed MCP engine
416
+ โ”‚ โ”‚ โ”œโ”€โ”€ engine.py
417
+ โ”‚ โ”‚ โ”œโ”€โ”€ session.py
418
+ โ”‚ โ”‚ โ””โ”€โ”€ client.py โ† Async MCP HTTP client
419
+ โ”‚ โ”œโ”€โ”€ server/
420
+ โ”‚ โ”‚ โ””โ”€โ”€ server.py โ† Standalone MCP memory server
421
+ โ”‚ โ””โ”€โ”€ wrappers/
422
+ โ”‚ โ””โ”€โ”€ openai_wrapper.py โ† AutoOpenAI drop-in replacement
423
+ โ”œโ”€โ”€ examples/
424
+ โ”‚ โ”œโ”€โ”€ 01_local_mode_quickstart.py
425
+ โ”‚ โ”œโ”€โ”€ 02_central_mode_quickstart.py
426
+ โ”‚ โ”œโ”€โ”€ 03_switching_modes.py
427
+ โ”‚ โ””โ”€โ”€ 04_openai_autopilot.py
428
+ โ”œโ”€โ”€ tests/
429
+ โ””โ”€โ”€ pyproject.toml
430
+ ```
431
+
432
+ ---
433
+
434
+ ## ๐Ÿ“‹ Compatibility
435
+
436
+ | Framework | Support |
437
+ |---|---|
438
+ | Python | 3.10+ |
439
+ | OpenAI SDK | โœ… |
440
+ | LangChain | โœ… |
441
+ | FastAPI | โœ… |
442
+ | Flask | โœ… |
443
+ | Django | โœ… |
444
+ | Groq | โœ… (same OpenAI API) |
445
+ | Anthropic | โœ… (via `to_messages()`) |
446
+ | Ollama | โœ… (via `to_messages()`) |
447
+ | Any MCP client | โœ… |
448
+
449
+ ---
450
+
451
+