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.

2.9 KB
Raw Blame
"""Validation for user-supplied search overrides.

Shared by the research-start route and the queue dispatcher so an override
is validated identically whether it arrives on a fresh request or is replayed
from a persisted queued row.
"""

from collections.abc import Mapping
from typing import Final, TypeAlias

MAX_RESULTS_MIN: Final = 1
MAX_RESULTS_MAX: Final = 50
MAX_QUERY_LENGTH: Final = 10_000
ALLOWED_TIME_PERIODS: Final = frozenset({"d", "w", "m", "y", "all"})

ITERATIONS_MIN: Final = 1
ITERATIONS_MAX: Final = 100
QUESTIONS_PER_ITERATION_MIN: Final = 1
QUESTIONS_PER_ITERATION_MAX: Final = 20

JsonScalar: TypeAlias = str | int | float | bool | None
JsonValue: TypeAlias = JsonScalar | list["JsonValue"] | dict[str, "JsonValue"]


def validate_research_query_length(query: object) -> str | None:
    """Return the shared query-length error for an over-cap string.

    Research requests can reach the worker either immediately or after a
    persisted queue replay.  Keeping this check here prevents the two entry
    points from drifting and accidentally giving replayed rows a larger
    prompt/storage budget than fresh requests.
    """
    if isinstance(query, str) and len(query) > MAX_QUERY_LENGTH:
        return f"Query exceeds maximum length of {MAX_QUERY_LENGTH} characters"
    return None


def validate_search_overrides(data: Mapping[str, JsonValue]) -> str | None:
    """Return an error message for an invalid override, or None if valid.

    ``type(x) is not int`` is deliberate rather than ``isinstance``: ``bool``
    is a subclass of ``int``, so ``isinstance(True, int)`` is True and would
    let ``max_results: true`` silently coerce to 1. The exact-type check
    rejects booleans (and floats) instead.
    """
    max_results = data.get("max_results")
    if max_results is not None and (
        type(max_results) is not int
        or not MAX_RESULTS_MIN <= max_results <= MAX_RESULTS_MAX
    ):
        return "max_results must be an integer between 1 and 50"

    time_period = data.get("time_period")
    if time_period is not None and (
        type(time_period) is not str or time_period not in ALLOWED_TIME_PERIODS
    ):
        return "time_period must be one of: d, w, m, y, all"

    iterations = data.get("iterations")
    if iterations is not None and (
        type(iterations) is not int
        or not ITERATIONS_MIN <= iterations <= ITERATIONS_MAX
    ):
        return f"iterations must be an integer between {ITERATIONS_MIN} and {ITERATIONS_MAX}"

    questions = data.get("questions_per_iteration")
    if questions is not None and (
        type(questions) is not int
        or not QUESTIONS_PER_ITERATION_MIN
        <= questions
        <= QUESTIONS_PER_ITERATION_MAX
    ):
        return (
            f"questions_per_iteration must be an integer between "
            f"{QUESTIONS_PER_ITERATION_MIN} and {QUESTIONS_PER_ITERATION_MAX}"
        )

    return None