"""The crowd: several models answering one turn, in any chat. Its own module because it is its own page, and it is its own page because as a card on `/admin/agents` it read as an agent-chat feature. It is not one: a crowd works in an ordinary conversation, and the owner reasonably concluded otherwise from where the switch was sitting. """ from __future__ import annotations import logging from fastapi import APIRouter, Form, Request, Response, status from fastapi.responses import RedirectResponse from lembas.api.deps import AdminUser, Db from lembas.services import settings_store from lembas.web.templating import render log = logging.getLogger(__name__) router = APIRouter(prefix="/admin/crowd", tags=["admin-crowd"]) @router.get("") async def crowd_page(request: Request, db: Db, user: AdminUser, saved: str = ""): """Its own page, for the reason its template records: as a card on the Agents screen it read as an agent-chat feature, which it is not.""" return render( request, "admin/crowd.html", {"crowd": settings_store.crowd(db), "saved": saved}, ) @router.post("") async def save_crowd( db: Db, user: AdminUser, enabled: bool = Form(False), max_models: int = Form(4), max_rounds: int = Form(2), wall_seconds: int = Form(900), collapse_agreement: bool = Form(False), ) -> Response: """One group, one form, one route. The bounds are clamped here as well as in `settings_store.crowd`, which is the same belt-and-braces `save_subagents` in `admin_agents.py` uses: a value posted past this route -- by an older page, or by hand -- still reads back sane. """ settings_store.update( db, { "enabled": enabled, # Every floor is one: a zero would be the feature switched off # wearing the switch's clothes. "max_models": min(max(max_models, 1), 8), "max_rounds": min(max(max_rounds, 1), 5), "wall_seconds": min(max(wall_seconds, 60), 7200), "collapse_agreement": collapse_agreement, }, key=settings_store.CROWD, ) log.info("crowd %s by %s", "enabled" if enabled else "disabled", user.email) return RedirectResponse("/admin/crowd?saved=1", status_code=status.HTTP_303_SEE_OTHER)