Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

763 B
Raw Blame
"""Shared offset validation for history and library listings."""

MAX_OFFSET = 10_000
OFFSET_LIMIT_ERROR = f"Offset must not exceed {MAX_OFFSET}"


def parse_bounded_offset(value: str | None) -> int:
    """Return an offset within the listing limit, or raise ``ValueError``.

    Missing, malformed and negative values retain the legacy zero fallback.
    Values above the ceiling are rejected rather than clamped to another page.
    Callers use ``OFFSET_LIMIT_ERROR`` for the HTTP 400 response rather than
    exposing exception text.
    """
    try:
        offset = max(0, int(value)) if value is not None else 0
    except (TypeError, ValueError):
        return 0

    if offset > MAX_OFFSET:
        raise ValueError(OFFSET_LIMIT_ERROR)
    return offset