Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions automem/api/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@

from flask import Blueprint, abort, jsonify, request

from automem.config import MEMORY_TYPES, PUBLIC_RELATIONS, RELATION_COLORS, normalize_relation_type
from automem.config import (
CONSOLIDATION_ARCHIVE_THRESHOLD,
MEMORY_TYPES,
PUBLIC_RELATIONS,
RELATION_COLORS,
normalize_relation_type,
)
from automem.utils.graph import _serialize_node

logger = logging.getLogger("automem.api.graph")
Expand Down Expand Up @@ -55,7 +61,13 @@ def snapshot() -> Any:
"""
query_start = time.perf_counter()

limit = min(int(request.args.get("limit", 500)), 2000)
try:
limit_raw = int(request.args.get("limit", 500))
except (TypeError, ValueError):
limit_raw = 500
if limit_raw < 0:
abort(400, description="limit must be >= 0")
limit = 0 if limit_raw == 0 else min(limit_raw, 50000)
min_importance = float(request.args.get("min_importance", 0.0))
types_filter = (
request.args.get("types", "").split(",") if request.args.get("types") else None
Expand All @@ -68,7 +80,7 @@ def snapshot() -> Any:

# Build Cypher query for nodes
where_clauses = ["m.importance >= $min_importance"]
params: Dict[str, Any] = {"min_importance": min_importance, "limit": limit}
params: Dict[str, Any] = {"min_importance": min_importance}

if types_filter and types_filter[0]:
where_clauses.append("m.type IN $types")
Expand All @@ -80,13 +92,18 @@ def snapshot() -> Any:

where_clause = " AND ".join(where_clauses)

# Fetch nodes
# Fetch nodes (limit=0 means return all)
limit_clause = ""
if limit > 0:
limit_clause = "LIMIT $limit"
params["limit"] = limit

node_query = f"""
MATCH (m:Memory)
WHERE {where_clause}
RETURN m
ORDER BY m.importance DESC, m.timestamp DESC
LIMIT $limit
{limit_clause}
"""

try:
Expand Down Expand Up @@ -181,6 +198,7 @@ def snapshot() -> Any:
"type_colors": TYPE_COLORS,
"relation_colors": RELATION_COLORS,
"query_time_ms": round(elapsed * 1000, 2),
"archive_threshold": CONSOLIDATION_ARCHIVE_THRESHOLD,
},
}
)
Expand Down Expand Up @@ -430,6 +448,7 @@ def stats() -> Any:
"type_colors": TYPE_COLORS,
"relation_colors": RELATION_COLORS,
"query_time_ms": round(elapsed * 1000, 2),
"archive_threshold": CONSOLIDATION_ARCHIVE_THRESHOLD,
},
}
)
Expand Down
Loading