Newer
Older
navi-1 / navi / api / routes / admin / memory.py
@Eugene Sukhodolskiy Eugene Sukhodolskiy 1 day ago 1 KB api: split the admin routes into a package
"""Memory facts across all users."""

from typing import Annotated

from fastapi import Depends

from navi.api.deps import get_session_store, require_permission
from navi.auth import User
from navi.core import SessionStore

from ._router import router


@router.get("/memory")
async def admin_list_memory(
    store: Annotated[SessionStore, Depends(get_session_store)],
    user: Annotated[User, Depends(require_permission("navi.memory.read_all"))],
    limit: int = 50,
    offset: int = 0,
    search: str | None = None,
    sort_by: str = "updated_at",
    sort_order: str = "desc",
    user_id: str | None = None,
    include_merged: bool = False,
):
    """Return memory facts with pagination, search and sorting.

    `include_merged` also returns the facts hidden behind a merge loser, each
    carrying the id of the fact it points at — this is the only place the mark
    is visible, and therefore the only way to undo one.
    """
    from navi.api.deps import get_memory_store

    memory = get_memory_store()
    facts = await memory.get_all_facts(
        limit=limit,
        offset=offset,
        search=search or None,
        sort_by=sort_by,
        sort_order=sort_order,
        user_id=user_id,
        all_users=user_id is None,
        include_merged=include_merged,
    )
    total = await memory.fact_count(
        user_id=user_id,
        all_users=user_id is None,
        search=search or None,
        include_merged=include_merged,
    )
    return {"total": total, "limit": limit, "offset": offset, "items": facts}