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.

9.2 KB
Raw Blame
import math
from typing import Any, Dict, Optional, Union

from loguru import logger
from sqlalchemy.orm import Session

from ...database.models import Setting
from ...settings.exceptions import SettingsValueError
from ...settings.manager import DYNAMIC_SETTINGS, get_typed_setting_value
from ...utilities.db_utils import get_settings_manager
from ...utilities.type_utils import to_positive_int

# Settings with dynamically populated options (excluded from validation).
# Single source of truth lives in `settings/manager.py` (it is also used by
# `import_settings` there); re-exported here for the web save path. The
# re-export must stay identity-equal to the manager's list — pinned by
# tests/settings/test_import_settings_validation.py.


def set_setting(
    key: str,
    value: Any,
    commit: bool = True,
    db_session: Optional[Session] = None,
) -> bool:
    """
    Set a setting value

    Args:
        key: Setting key
        value: Setting value
        commit: Whether to commit the change
        db_session: Optional database session

    Returns:
        bool: True if successful
    """
    manager = get_settings_manager(db_session)
    return bool(manager.set_setting(key, value, commit))


def get_all_settings() -> Dict[str, Any]:
    """
    Get all settings, optionally filtered by type

    Returns:
        Dict[str, Any]: Dictionary of settings

    """
    manager = get_settings_manager()
    return manager.get_all_settings()  # type: ignore[no-any-return]


def create_or_update_setting(
    setting: Union[Dict[str, Any], Setting],
    commit: bool = True,
    db_session: Optional[Session] = None,
) -> Optional[Setting]:
    """
    Create or update a setting

    Args:
        setting: Setting dictionary or object
        commit: Whether to commit the change
        db_session: Optional database session

    Returns:
        Optional[Setting]: The setting object if successful
    """
    manager = get_settings_manager(db_session)
    return manager.create_or_update_setting(setting, commit)  # type: ignore[no-any-return]


def invalidate_settings_caches(username=None):
    """Invalidate all settings-related caches after a settings mutation.

    Call this after any route that creates, updates, or deletes settings
    so background services pick up changes immediately.

    Safe to call from anywhere — silently skips caches that aren't initialized.

    Args:
        username: If provided, invalidates only that user's scheduler cache.
                  If None, invalidates all users' scheduler caches.
    """
    # News scheduler per-user settings cache (TTLCache, 5-min TTL).
    # NOTE: the LLM provider dropdown is served by the auto-discovery path
    # (llm/providers/auto_discovery.get_discovered_provider_options), which
    # rebuilds per call and has no cache to invalidate here.
    try:
        from ...scheduler.background import get_background_job_scheduler

        scheduler = get_background_job_scheduler()
        if username is not None:
            scheduler.invalidate_user_settings_cache(username)
        else:
            scheduler.invalidate_all_settings_cache()
    except Exception:
        logger.debug("Could not invalidate scheduler cache", exc_info=True)


def reschedule_document_jobs_if_needed(username, changed_keys):
    """Reschedule a user's document-scheduler jobs after a settings change.

    Cache invalidation alone (``invalidate_settings_caches``) only clears the
    scheduler's cached settings — it does not (re)create or tear down the
    per-user interval jobs. So toggling ``document_scheduler.*`` settings (e.g.
    ``sweep_library_collections`` or the legacy ``generate_rag``) would not take
    effect until the user logged out and back in. Call this AFTER
    ``invalidate_settings_caches`` so the scheduler re-reads fresh settings and
    the change applies on the next tick.

    Only reschedules when a ``document_scheduler.*`` key actually changed, so an
    unrelated settings save never churns (and resets the timer of) the document
    jobs. Best-effort and silent when the scheduler isn't running (e.g. tests,
    CLI), mirroring ``invalidate_settings_caches``.

    Args:
        username: The user whose jobs should be re-evaluated.
        changed_keys: Iterable of setting keys written by the request.
    """
    if not username or not changed_keys:
        return
    if not any(
        str(key).startswith("document_scheduler.") for key in changed_keys
    ):
        return
    try:
        from ...scheduler.background import get_background_job_scheduler

        scheduler = get_background_job_scheduler()
        scheduler.reschedule_document_jobs(username)
    except Exception:
        logger.debug(
            "Could not reschedule document jobs after settings change",
            exc_info=True,
        )


def reschedule_zotero_jobs_if_needed(username, changed_keys):
    """Reschedule a user's Zotero auto-sync job after a settings change.

    Mirrors :func:`reschedule_document_jobs_if_needed` for ``zotero.*`` keys.
    Without it, toggling ``zotero.auto_sync_enabled`` (or changing
    ``zotero.sync_interval_minutes``) would not create/refresh the interval job
    until the user logged out and back in — the schedule is otherwise only built
    on the login path. Call AFTER ``invalidate_settings_caches`` so the
    scheduler re-reads fresh settings. Only reschedules when a ``zotero.`` key
    actually changed. Best-effort and silent when the scheduler isn't running
    (e.g. tests, CLI).

    Args:
        username: The user whose Zotero job should be re-evaluated.
        changed_keys: Iterable of setting keys written by the request.
    """
    if not username or not changed_keys:
        return
    if not any(str(key).startswith("zotero.") for key in changed_keys):
        return
    try:
        from ...scheduler.background import get_background_job_scheduler

        scheduler = get_background_job_scheduler()
        scheduler.reschedule_zotero_jobs(username)
    except Exception:
        logger.debug(
            "Could not reschedule Zotero jobs after settings change",
            exc_info=True,
        )


def validate_setting(
    setting: Setting, value: Any
) -> tuple[bool, Optional[str]]:
    """
    Validate a setting value based on its type and constraints.

    Args:
        setting: The Setting object to validate against
        value: The value to validate

    Returns:
        tuple[bool, Optional[str]]: (is_valid, error_message)
    """
    if setting.key == "search.fetch.summary_input_max_length":
        try:
            to_positive_int(value)
        except ValueError:
            return False, "Value must be a positive integer"

    # Validate the exact summary integer before generic number conversion
    # or the blank-input handling for other optional numeric settings.
    raw_value = value
    numeric_ui = setting.ui_element in ("number", "slider", "range")
    # Blank form inputs explicitly clear optional numeric settings; they are
    # normalized to None on write, not used as a fallback for bad numbers.
    if numeric_ui and (
        raw_value is None
        or (isinstance(raw_value, str) and not raw_value.strip())
    ):
        return True, None
    try:
        value = get_typed_setting_value(
            key=str(setting.key),
            value=value,
            ui_element=str(setting.ui_element),
            default=None,
            check_env=False,
        )
    except SettingsValueError as error:
        if numeric_ui:
            return False, "Value must be a number"
        if setting.ui_element == "checkbox":
            return False, "Value must be a boolean"
        return False, error.reason

    # Validate based on UI element type
    if setting.ui_element == "checkbox":
        # After conversion, should be boolean
        if not isinstance(value, bool):
            return False, "Value must be a boolean"

    elif setting.ui_element in ("number", "slider", "range"):
        # After conversion, should be numeric
        if (
            isinstance(raw_value, bool)
            or isinstance(value, bool)
            or not isinstance(value, (int, float))
            or (isinstance(value, float) and not math.isfinite(value))
        ):
            return False, "Value must be a number"

        # Check min/max constraints if defined
        if setting.min_value is not None and value < setting.min_value:
            return False, f"Value must be at least {setting.min_value}"
        if setting.max_value is not None and value > setting.max_value:
            return False, f"Value must be at most {setting.max_value}"

    elif setting.ui_element == "select":
        # Check if value is in the allowed options
        if setting.options:
            # Skip options validation for dynamically populated dropdowns
            if setting.key not in DYNAMIC_SETTINGS:
                allowed_values = [
                    opt.get("value") if isinstance(opt, dict) else opt
                    for opt in list(setting.options)
                ]
                if value not in allowed_values:
                    return (
                        False,
                        f"Value must be one of: {', '.join(str(v) for v in allowed_values)}",
                    )

    # All checks passed
    return True, None