Skip to content

Commit a6d1961

Browse files
committed
feat(redis): use RedisVL paging with contextlib.suppress; fix loop var lint; adjust next_offset/total
1 parent 576d6c5 commit a6d1961

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

agent_memory_server/vectorstore_adapter.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -921,17 +921,23 @@ async def search_memories(
921921
return_fields=return_fields,
922922
filter_expression=redis_filter,
923923
distance_threshold=float(distance_threshold),
924-
k=limit + offset,
924+
k=limit,
925925
)
926926
else:
927927
vq = VectorQuery(
928928
vector=embedding_vector,
929929
vector_field_name="vector",
930930
return_fields=return_fields,
931931
filter_expression=redis_filter,
932-
k=limit + offset,
932+
k=limit,
933933
)
934934

935+
# Apply RedisVL paging instead of manual slicing
936+
from contextlib import suppress
937+
938+
with suppress(Exception):
939+
vq.paging(offset, limit)
940+
935941
# Execute via AsyncSearchIndex if available
936942
if hasattr(index, "asearch"):
937943
raw = await index.asearch(vq)
@@ -942,9 +948,7 @@ async def search_memories(
942948
docs = getattr(raw, "docs", raw) or []
943949

944950
memory_results: list[MemoryRecordResult] = []
945-
for i, doc in enumerate(docs):
946-
if i < offset:
947-
continue
951+
for doc in docs:
948952
fields = (
949953
getattr(doc, "fields", None)
950954
or getattr(doc, "__dict__", {})
@@ -1029,14 +1033,11 @@ async def search_memories(
10291033
except Exception:
10301034
pass
10311035

1032-
next_offset = (
1033-
offset + limit
1034-
if (len(docs) if docs else 0) > offset + limit
1035-
else None
1036-
)
1036+
total_docs = len(docs) if docs else 0
1037+
next_offset = offset + limit if total_docs == limit else None
10371038
return MemoryRecordResults(
10381039
memories=memory_results[:limit],
1039-
total=(len(docs) if docs else 0),
1040+
total=offset + total_docs,
10401041
next_offset=next_offset,
10411042
)
10421043
except Exception as e:

0 commit comments

Comments
 (0)