Spaces:
Sleeping
Sleeping
yc1838 commited on
Commit ·
aab1b41
1
Parent(s): b04f018
feat: replace MemorySaver with SqliteSaver for thread persistence
Browse files- pyproject.toml +2 -0
- src/lilith_agent/app.py +13 -2
- tests/test_memory_persistence.py +20 -0
pyproject.toml
CHANGED
|
@@ -8,6 +8,8 @@ version = "0.0.0"
|
|
| 8 |
requires-python = ">=3.11"
|
| 9 |
dependencies = [
|
| 10 |
"langgraph>=1.0,<2.0",
|
|
|
|
|
|
|
| 11 |
"langchain-core>=1.0,<2.0",
|
| 12 |
"langchain-anthropic>=1.0,<2.0",
|
| 13 |
"langchain-google-genai>=2.0.0,<5.0",
|
|
|
|
| 8 |
requires-python = ">=3.11"
|
| 9 |
dependencies = [
|
| 10 |
"langgraph>=1.0,<2.0",
|
| 11 |
+
"langgraph-checkpoint-sqlite",
|
| 12 |
+
"langmem>=0.0.1",
|
| 13 |
"langchain-core>=1.0,<2.0",
|
| 14 |
"langchain-anthropic>=1.0,<2.0",
|
| 15 |
"langchain-google-genai>=2.0.0,<5.0",
|
src/lilith_agent/app.py
CHANGED
|
@@ -8,7 +8,10 @@ from typing import Callable
|
|
| 8 |
|
| 9 |
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
|
| 10 |
from langchain_core.tools import BaseTool
|
| 11 |
-
from langgraph.checkpoint.
|
|
|
|
|
|
|
|
|
|
| 12 |
from langgraph.graph import END, StateGraph
|
| 13 |
from langgraph.graph.message import MessagesState, add_messages
|
| 14 |
from typing import Annotated, TypedDict
|
|
@@ -592,5 +595,13 @@ def build_react_agent(cfg: Config):
|
|
| 592 |
graph.add_edge("tools", "model")
|
| 593 |
graph.add_edge("fail_safe", END)
|
| 594 |
|
| 595 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 596 |
return compiled.with_config({"recursion_limit": cfg.recursion_limit})
|
|
|
|
| 8 |
|
| 9 |
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
|
| 10 |
from langchain_core.tools import BaseTool
|
| 11 |
+
from langgraph.checkpoint.sqlite import SqliteSaver
|
| 12 |
+
import sqlite3
|
| 13 |
+
import os
|
| 14 |
+
from pathlib import Path
|
| 15 |
from langgraph.graph import END, StateGraph
|
| 16 |
from langgraph.graph.message import MessagesState, add_messages
|
| 17 |
from typing import Annotated, TypedDict
|
|
|
|
| 595 |
graph.add_edge("tools", "model")
|
| 596 |
graph.add_edge("fail_safe", END)
|
| 597 |
|
| 598 |
+
# Setup SQLite Saver
|
| 599 |
+
lilith_home = Path(os.getenv("LILITH_HOME", ".lilith"))
|
| 600 |
+
lilith_home.mkdir(parents=True, exist_ok=True)
|
| 601 |
+
db_path = lilith_home / "threads.sqlite"
|
| 602 |
+
|
| 603 |
+
conn = sqlite3.connect(str(db_path), check_same_thread=False)
|
| 604 |
+
memory_saver = SqliteSaver(conn)
|
| 605 |
+
|
| 606 |
+
compiled = graph.compile(checkpointer=memory_saver)
|
| 607 |
return compiled.with_config({"recursion_limit": cfg.recursion_limit})
|
tests/test_memory_persistence.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from lilith_agent.config import Config
|
| 4 |
+
from lilith_agent.app import build_react_agent
|
| 5 |
+
|
| 6 |
+
def test_build_react_agent_uses_sqlite_saver(tmp_path):
|
| 7 |
+
cfg = Config.from_env()
|
| 8 |
+
|
| 9 |
+
# Temporarily override where the agent looks for the .lilith dir
|
| 10 |
+
import os
|
| 11 |
+
os.environ["LILITH_HOME"] = str(tmp_path / ".lilith")
|
| 12 |
+
|
| 13 |
+
agent = build_react_agent(cfg)
|
| 14 |
+
|
| 15 |
+
assert agent.checkpointer is not None
|
| 16 |
+
assert type(agent.checkpointer).__name__ == "SqliteSaver"
|
| 17 |
+
|
| 18 |
+
# Check if DB file was created
|
| 19 |
+
db_path = tmp_path / ".lilith" / "threads.sqlite"
|
| 20 |
+
assert db_path.exists()
|