This commit is contained in:
董宏凯 committed 2026-06-12 14:10:08 +08:00
commit 728a4b1170
10 files changed
+204 -8

No files matched your search

@@ -59,6 +59,22 @@ class ThreadMetaStore(abc.ABC):
"""
pass
@abc.abstractmethod
async def count(
self,
*,
metadata: dict | None = None,
status: str | None = None,
user_id: str | None | _AutoSentinel = AUTO,
exclude_system: bool = False,
query: str | None = None,
) -> int:
"""Count threads matching the same filters as :meth:`search`.
Backs pagination UIs that need a total alongside per-page results.
"""
pass
@abc.abstractmethod
async def update_display_name(self, thread_id: str, display_name: str, *, user_id: str | None | _AutoSentinel = AUTO) -> None:
pass
@@ -126,6 +126,50 @@ class MemoryThreadMetaStore(ThreadMetaStore):
scan_offset += len(items)
return matched[offset:needed]
async def count(
self,
*,
metadata: dict | None = None,
status: str | None = None,
user_id: str | None | _AutoSentinel = AUTO,
exclude_system: bool = False,
query: str | None = None,
) -> int:
"""Count threads matching the same filters as :meth:`search`."""
resolved_user_id = resolve_user_id(user_id, method_name="MemoryThreadMetaStore.count")
filter_dict: dict[str, Any] = {}
if metadata:
filter_dict.update(metadata)
if status:
filter_dict["status"] = status
if resolved_user_id is not None:
filter_dict["user_id"] = resolved_user_id
normalized_query = (query or "").strip().lower()
total = 0
batch_size = 500
scan_offset = 0
while True:
items = await self._store.asearch(
THREADS_NS,
filter=filter_dict or None,
limit=batch_size,
offset=scan_offset,
)
if not items:
break
for item in items:
record = self._item_to_dict(item)
if exclude_system and (record.get("metadata") or {}).get("system"):
continue
if normalized_query and normalized_query not in (record.get("display_name") or "").lower():
continue
total += 1
if len(items) < batch_size:
break
scan_offset += len(items)
return total
async def check_access(self, thread_id: str, user_id: str, *, require_existing: bool = False) -> bool:
item = await self._store.aget(THREADS_NS, thread_id)
if item is None:
@@ -167,6 +167,58 @@ class ThreadMetaRepository(ThreadMetaStore):
scan_offset += len(rows)
return matched[offset:needed]
async def count(
self,
*,
metadata: dict | None = None,
status: str | None = None,
user_id: str | None | _AutoSentinel = AUTO,
exclude_system: bool = False,
query: str | None = None,
) -> int:
"""Count threads matching the same filters as :meth:`search`."""
resolved_user_id = resolve_user_id(user_id, method_name="ThreadMetaRepository.count")
conditions = []
if resolved_user_id is not None:
conditions.append(ThreadMetaRow.user_id == resolved_user_id)
if status:
conditions.append(ThreadMetaRow.status == status)
if query and query.strip():
conditions.append(func.lower(ThreadMetaRow.display_name).contains(query.strip().lower()))
if not metadata and not exclude_system:
stmt = select(func.count()).select_from(ThreadMetaRow)
for condition in conditions:
stmt = stmt.where(condition)
async with self._sf() as session:
return int((await session.execute(stmt)).scalar_one())
# JSON filters can't run in portable SQL — scan just the metadata
# column in batches and count in Python (mirrors search()).
stmt = select(ThreadMetaRow.metadata_json).order_by(ThreadMetaRow.thread_id)
for condition in conditions:
stmt = stmt.where(condition)
total = 0
batch_size = 1000
scan_offset = 0
async with self._sf() as session:
while True:
result = await session.execute(stmt.limit(batch_size).offset(scan_offset))
rows = result.scalars().all()
if not rows:
break
for metadata_json in rows:
md = metadata_json or {}
if metadata and not all(md.get(k) == v for k, v in metadata.items()):
continue
if exclude_system and md.get("system"):
continue
total += 1
if len(rows) < batch_size:
break
scan_offset += len(rows)
return total
async def _check_ownership(self, session: AsyncSession, thread_id: str, resolved_user_id: str | None) -> bool:
"""Return True if the row exists and is owned (or filter bypassed)."""
if resolved_user_id is None: