~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""
Per-request context for the authenticated user.
Service-layer code historically read `flask_session.get("username")` to
discover who the current user is. Under FastAPI there is no Flask request
context, so those reads return None and the caller crashes (or silently
drops to an "anonymous" branch).
This module exposes a `contextvars`-backed username context that the
DatabaseMiddleware sets at the start of each authenticated request and
clears at the end. Service code can call `get_current_username()` as a
Flask-free fallback.
contextvars (not `threading.local`) is the correct primitive here because
it isolates per-asyncio-task as well as per-thread, which matches FastAPI's
mixed sync/async execution model.
"""
from __future__ import annotations
import contextvars
from contextlib import contextmanager
from typing import Generator, Optional
_username_var: contextvars.ContextVar[Optional[str]] = contextvars.ContextVar(
"ldr_request_username", default=None
)
_session_id_var: contextvars.ContextVar[Optional[str]] = contextvars.ContextVar(
"ldr_request_session_id", default=None
)
def set_request_user(
username: Optional[str], session_id: Optional[str] = None
) -> tuple:
"""Set the request-scoped username and session_id.
Returns reset handles so the caller can restore the previous values
via `reset_request_user(handles)`.
"""
user_handle = _username_var.set(username)
session_handle = _session_id_var.set(session_id)
return (user_handle, session_handle)
def reset_request_user(handles: tuple) -> None:
"""Restore the previous request-scoped username and session_id."""
user_handle, session_handle = handles
try:
_username_var.reset(user_handle)
except (ValueError, LookupError):
pass
try:
_session_id_var.reset(session_handle)
except (ValueError, LookupError):
pass
def get_current_username() -> Optional[str]:
"""Return the username for the current request, or None.
Reads the contextvar populated by ``DatabaseMiddleware`` (and by
background workers / scheduler jobs that explicitly push a context
via ``request_user(...)``). Returns None outside any of those
contexts.
"""
return _username_var.get()
def get_current_session_id() -> Optional[str]:
"""Return the session_id for the current request, or None.
Same source as ``get_current_username`` — the contextvar populated
by ``DatabaseMiddleware`` / explicit ``request_user(...)``.
"""
return _session_id_var.get()
@contextmanager
def request_user(
username: Optional[str], session_id: Optional[str] = None
) -> Generator[None, None, None]:
"""Context manager: set request user inside the block, restore on exit."""
tokens = set_request_user(username, session_id)
try:
yield
finally:
reset_request_user(tokens)