Add memory graph data endpoint

This commit is contained in:
2026-03-22 23:21:59 +00:00
parent c0f6ca9fc2
commit 9f69a8a8be

View File

@@ -16,6 +16,40 @@ from timmy.memory_system import (
router = APIRouter(prefix="/memory", tags=["memory"])
@router.get("/graph_data")
async def memory_graph_data():
"""Return memory nodes and links for D3 visualization."""
from timmy.memory_system import get_connection
import json
nodes = []
links = []
with get_connection() as conn:
rows = conn.execute(
"SELECT id, content, memory_type, session_id, agent_id, embedding FROM memories ORDER BY created_at DESC LIMIT 100"
).fetchall()
for row in rows:
nodes.append({
"id": row["id"],
"content": row["content"][:50] + "..." if len(row["content"]) > 50 else row["content"],
"type": row["memory_type"],
"session": row["session_id"],
"agent": row["agent_id"]
})
# Create links based on session_id
for i in range(len(nodes)):
for j in range(i + 1, len(nodes)):
if nodes[i]["session"] and nodes[i]["session"] == nodes[j]["session"]:
links.append({"source": nodes[i]["id"], "target": nodes[j]["id"], "value": 1, "type": "session"})
elif nodes[i]["agent"] and nodes[i]["agent"] == nodes[j]["agent"]:
links.append({"source": nodes[i]["id"], "target": nodes[j]["id"], "value": 0.5, "type": "agent"})
return {"nodes": nodes, "links": links}
@router.get("", response_class=HTMLResponse)
async def memory_page(
request: Request,