Fix bulk actions, create chats lazily, rework the UI
Seven reported problems.
**Bulk model actions 404'd.** /admin/models/{model_id} was registered
before /admin/models/bulk, and FastAPI matches in registration order, so
"bulk" was parsed as a model id. Moved above the parameterised route,
with a comment saying why, and a regression test.
**Empty chats piled up.** There is now no endpoint that creates one.
"New chat" is a link to /chat, which renders a composer with no row
behind it, and POST /api/chats/start writes the chat together with its
first message. Opening one and walking away leaves nothing.
**Pinning meant two different things.** The picker is now always in the
administrator's position order; pinned models get shortcuts in the chat
sidebar and nothing else. A picker whose order silently differs from the
admin screen is just confusing.
**Model images were missing in chat.** Assistant bubbles now show the
avatar of the model that actually wrote the turn -- which is not always
the model the chat is set to now -- falling back to the LLeMbas mark.
The picker shows it too.
**No global or per-model system prompt.** Three layers now: instance
(Admin -> General), model (Admin -> Models), chat. Precedence, not
concatenation: most specific wins outright. Stacking them reads well in
a settings screen and badly in practice, because two layers that
disagree give the model contradictory instructions and nobody can tell
which is losing. The chat panel shows the inherited prompt as
placeholder text so "leave empty to inherit" is not a guess.
**Alignment and button sizing.** Added --control-h and friends to
tokens.css; every button, input and select takes its height from them,
so a mixed row is flush by construction rather than by per-instance
nudging. Icon buttons are square at that height. Added .btn-row,
.card__header/.card__footer and .grid so pages stop carrying inline
styles, and moved every admin page onto them.
**Settings needed structure.** The user settings page is now tabbed
(Account / Models / Appearance / Security) using radio inputs and
sibling selectors -- no JavaScript, and the browser keeps the chosen tab
across a re-render.
Caught while checking: the chat.css surgery had deleted the attachment,
chip and dropzone rules. Restored, and there is now a check that every
literal class used in a template has a CSS rule.
197 tests, ruff clean.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -62,6 +62,7 @@ async def save_general(
|
||||
user: AdminUser,
|
||||
instance_name: str = Form("LLeMbas"),
|
||||
allow_signup: bool = Form(False),
|
||||
system_prompt: str = Form(""),
|
||||
) -> Response:
|
||||
"""Save instance settings.
|
||||
|
||||
@@ -73,6 +74,7 @@ async def save_general(
|
||||
{
|
||||
"instance_name": instance_name.strip()[:120] or "LLeMbas",
|
||||
"allow_signup": allow_signup,
|
||||
"system_prompt": system_prompt.strip()[:8000],
|
||||
},
|
||||
)
|
||||
log.info("registration %s by %s", "opened" if allow_signup else "closed", user.email)
|
||||
|
||||
@@ -59,12 +59,43 @@ async def models_page(request: Request, db: Db, user: AdminUser, saved: str = ""
|
||||
"groups": list(db.scalars(select(Group).order_by(Group.name))),
|
||||
"connections": list(db.scalars(select(Connection).order_by(Connection.name))),
|
||||
"default_model": settings_store.get(db, "default_model") or "",
|
||||
"instance_prompt": settings_store.get(db, "system_prompt") or "",
|
||||
"capabilities": CAPABILITIES,
|
||||
"saved": saved,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
# Registered BEFORE /{model_id}: FastAPI matches in registration order, so
|
||||
# with the parameterised route first, "bulk" is captured as a model id and
|
||||
# the handler 404s on a model that does not exist.
|
||||
@router.post("/admin/models/bulk")
|
||||
async def bulk_models(
|
||||
db: Db, user: AdminUser, action: str = Form(...), model_ids: list[str] = Form(default=[])
|
||||
) -> Response:
|
||||
"""Enable or disable several models at once.
|
||||
|
||||
A freshly refreshed connection can advertise dozens of models; turning them
|
||||
off one at a time is not a reasonable way to spend an afternoon.
|
||||
"""
|
||||
models = list(db.scalars(select(Model).where(Model.id.in_(model_ids or []))))
|
||||
for model in models:
|
||||
if action == "enable":
|
||||
model.enabled = True
|
||||
elif action == "disable":
|
||||
model.enabled = False
|
||||
elif action == "public":
|
||||
model.public = True
|
||||
model.groups = []
|
||||
elif action == "private":
|
||||
model.public = False
|
||||
db.commit()
|
||||
_renumber(db)
|
||||
return RedirectResponse(
|
||||
f"/admin/models?saved={len(models)}+model(s)+updated.", status_code=303
|
||||
)
|
||||
|
||||
|
||||
@router.post("/admin/models/{model_id}")
|
||||
async def update_model(
|
||||
db: Db,
|
||||
@@ -72,6 +103,7 @@ async def update_model(
|
||||
model_id: str,
|
||||
display_name: str = Form(""),
|
||||
description: str = Form(""),
|
||||
system_prompt: str = Form(""),
|
||||
enabled: bool = Form(False),
|
||||
pinned: bool = Form(False),
|
||||
public: bool = Form(False),
|
||||
@@ -82,6 +114,7 @@ async def update_model(
|
||||
|
||||
model.display_name = display_name.strip()[:300]
|
||||
model.description = description.strip()[:2000]
|
||||
model.system_prompt = system_prompt.strip()[:8000]
|
||||
model.enabled = enabled
|
||||
model.pinned = pinned
|
||||
model.public = public
|
||||
@@ -165,33 +198,6 @@ async def delete_model_image(db: Db, user: AdminUser, model_id: str) -> Response
|
||||
return RedirectResponse("/admin/models?saved=Image+removed.", status_code=303)
|
||||
|
||||
|
||||
@router.post("/admin/models/bulk")
|
||||
async def bulk_models(
|
||||
db: Db, user: AdminUser, action: str = Form(...), model_ids: list[str] = Form(default=[])
|
||||
) -> Response:
|
||||
"""Enable or disable several models at once.
|
||||
|
||||
A freshly refreshed connection can advertise dozens of models; turning them
|
||||
off one at a time is not a reasonable way to spend an afternoon.
|
||||
"""
|
||||
models = list(db.scalars(select(Model).where(Model.id.in_(model_ids or []))))
|
||||
for model in models:
|
||||
if action == "enable":
|
||||
model.enabled = True
|
||||
elif action == "disable":
|
||||
model.enabled = False
|
||||
elif action == "public":
|
||||
model.public = True
|
||||
model.groups = []
|
||||
elif action == "private":
|
||||
model.public = False
|
||||
db.commit()
|
||||
_renumber(db)
|
||||
return RedirectResponse(
|
||||
f"/admin/models?saved={len(models)}+model(s)+updated.", status_code=303
|
||||
)
|
||||
|
||||
|
||||
# --- Serving model images ----------------------------------------------------
|
||||
@router.get("/uploads/models/{filename}")
|
||||
async def model_image(user: RequiredUser, filename: str) -> Response:
|
||||
|
||||
+64
-6
@@ -42,9 +42,18 @@ def _owned_chat(db: DBSession, chat_id: str, user_id: str) -> Chat:
|
||||
return chat
|
||||
|
||||
|
||||
@router.post("", dependencies=[Depends(require_permission("chat.create"))])
|
||||
async def create_chat(db: Db, user: RequiredUser, folder_id: str = Form("")) -> Response:
|
||||
chosen = chat_service.default_model(db, user)
|
||||
def _new_chat(db: DBSession, user: User, *, folder_id: str = "", model_id: str = "") -> Chat:
|
||||
"""Create a chat row, resolving which model it should use."""
|
||||
chosen = None
|
||||
if model_id:
|
||||
match = next(
|
||||
(m for m in chat_service.available_models(db, user) if m.model_id == model_id), None
|
||||
)
|
||||
if match is not None:
|
||||
chosen = (match.model_id, match.connection_id)
|
||||
if chosen is None:
|
||||
chosen = chat_service.default_model(db, user)
|
||||
|
||||
chat = Chat(
|
||||
user_id=user.id,
|
||||
folder_id=folder_id or None,
|
||||
@@ -53,14 +62,48 @@ async def create_chat(db: Db, user: RequiredUser, folder_id: str = Form("")) ->
|
||||
)
|
||||
db.add(chat)
|
||||
db.commit()
|
||||
return chat
|
||||
|
||||
|
||||
@router.post("/start", dependencies=[Depends(require_permission("chat.create"))])
|
||||
async def start_chat(
|
||||
db: Db,
|
||||
user: RequiredUser,
|
||||
content: str = Form(""),
|
||||
file_ids: list[str] = Form(default=[]),
|
||||
folder_id: str = Form(""),
|
||||
model_id: str = Form(""),
|
||||
) -> Response:
|
||||
"""Create a chat from its first message.
|
||||
|
||||
Chats are made here rather than by a "New chat" button so that an opened-
|
||||
and-abandoned chat never exists: the row appears only once there is
|
||||
something in it. The reply then streams the same way as any other, because
|
||||
/chat/{id} renders the unfinished assistant message with its sse-connect.
|
||||
"""
|
||||
content = content.strip()
|
||||
if not content and not file_ids:
|
||||
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
chat = _new_chat(db, user, folder_id=folder_id, model_id=model_id)
|
||||
|
||||
user_message = chat_service.create_message(db, chat, ROLE_USER, content)
|
||||
if file_ids:
|
||||
files_service.claim(db, ids=file_ids, user_id=user.id, message_id=user_message.id)
|
||||
chat_service.create_message(
|
||||
db, chat, ROLE_ASSISTANT, "", complete_=False, model_id=chat.model_id
|
||||
)
|
||||
|
||||
# HX-Redirect rather than a swap: a new chat is a new URL, and the address
|
||||
# bar has to follow so the chat can be reloaded or bookmarked.
|
||||
response = Response(status_code=status.HTTP_204_NO_CONTENT)
|
||||
response.headers["HX-Redirect"] = f"/chat/{chat.id}"
|
||||
return response
|
||||
|
||||
|
||||
# There is deliberately no route that creates an empty chat. Starting one is
|
||||
# navigation to /chat (optionally ?model=...), and the row is written by
|
||||
# /start when the first message is actually sent.
|
||||
|
||||
|
||||
@router.post("/{chat_id}/messages")
|
||||
async def post_message(
|
||||
request: Request,
|
||||
@@ -103,6 +146,9 @@ async def post_message(
|
||||
"assistant_message": assistant_message,
|
||||
"chat": chat,
|
||||
"user": user,
|
||||
"models_by_id": {
|
||||
m.model_id: m for m in chat_service.available_models(db, user)
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
@@ -281,6 +327,9 @@ async def _generate(chat_id: str, message_id: str) -> AsyncIterator[str]:
|
||||
# template shares both roles, and a missing `user` would only
|
||||
# blow up on whichever branch is not being exercised here.
|
||||
"user": db.get(User, chat.user_id),
|
||||
"models_by_id": {
|
||||
m.model_id: m for m in chat_service.available_models(db, None)
|
||||
},
|
||||
}
|
||||
)
|
||||
title_html = templates.get_template("chat/_title_oob.html").render(
|
||||
@@ -432,7 +481,16 @@ async def regenerate(
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"chat/_message.html",
|
||||
{"request": request, "message": message, "chat": chat, "body_html": "", "user": user},
|
||||
{
|
||||
"request": request,
|
||||
"message": message,
|
||||
"chat": chat,
|
||||
"body_html": "",
|
||||
"user": user,
|
||||
"models_by_id": {
|
||||
m.model_id: m for m in chat_service.available_models(db, user)
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
|
||||
+39
-8
@@ -25,15 +25,16 @@ def _chat_context(db: DBSession, user: User, chat: Chat | None) -> dict:
|
||||
"""
|
||||
models = chat_service.available_models(db, user)
|
||||
current = next((m for m in models if m.model_id == chat.model_id), None) if chat else None
|
||||
pinned = [m for m in models if m.pinned]
|
||||
return {
|
||||
"models": models,
|
||||
"pinned_models": pinned,
|
||||
# Excludes the pinned ones: they already have their own optgroup, and
|
||||
# listing a model twice gives the <select> two options with the same
|
||||
# value, both marked selected.
|
||||
"other_models": [m for m in models if not m.pinned] if pinned else models,
|
||||
# For the sidebar shortcuts only. The picker lists `models` in the
|
||||
# administrator's order, pinned or not.
|
||||
"pinned_models": [m for m in models if m.pinned],
|
||||
"current_model": current,
|
||||
# Assistant bubbles show the avatar of the model that wrote them, which
|
||||
# may not be the model the chat is set to now. Keyed by model_id, the
|
||||
# denormalised value stored on each message.
|
||||
"models_by_id": {m.model_id: m for m in models},
|
||||
}
|
||||
|
||||
|
||||
@@ -74,14 +75,26 @@ async def home(user: RequiredUser):
|
||||
|
||||
|
||||
@router.get("/chat")
|
||||
async def chat_index(request: Request, db: Db, user: RequiredUser):
|
||||
async def chat_index(request: Request, db: Db, user: RequiredUser, model: str = ""):
|
||||
"""A composer with no chat behind it yet.
|
||||
|
||||
`?model=` preselects one, which is how the pinned shortcuts work without
|
||||
creating a row for a chat that may never be sent.
|
||||
"""
|
||||
context = _chat_context(db, user, None)
|
||||
preselected = next(
|
||||
(m for m in context["models"] if m.model_id == model), None
|
||||
) or (context["models"][0] if context["models"] else None)
|
||||
|
||||
return render(
|
||||
request,
|
||||
"chat/index.html",
|
||||
{
|
||||
"chat": None,
|
||||
"messages": [],
|
||||
**_chat_context(db, user, None),
|
||||
"bodies": {},
|
||||
**context,
|
||||
"current_model": preselected,
|
||||
**_sidebar_context(db, user),
|
||||
},
|
||||
)
|
||||
@@ -108,6 +121,22 @@ async def chat_detail(request: Request, db: Db, user: RequiredUser, chat_id: str
|
||||
if message.role == "assistant" and message.content
|
||||
}
|
||||
|
||||
# What the chat would use if its own prompt were empty, so the settings
|
||||
# panel can show it as placeholder text rather than leaving the user to
|
||||
# guess what "inherited" means.
|
||||
inherited, inherited_from = "", ""
|
||||
current = next(
|
||||
(m for m in chat_service.available_models(db, user) if m.model_id == chat.model_id), None
|
||||
)
|
||||
if current is not None and (current.system_prompt or "").strip():
|
||||
inherited, inherited_from = current.system_prompt.strip(), "model"
|
||||
else:
|
||||
from lembas.services import settings_store
|
||||
|
||||
instance_prompt = (settings_store.get(db, "system_prompt") or "").strip()
|
||||
if instance_prompt:
|
||||
inherited, inherited_from = instance_prompt, "instance"
|
||||
|
||||
return render(
|
||||
request,
|
||||
"chat/index.html",
|
||||
@@ -115,6 +144,8 @@ async def chat_detail(request: Request, db: Db, user: RequiredUser, chat_id: str
|
||||
"chat": chat,
|
||||
"messages": messages,
|
||||
"bodies": bodies,
|
||||
"inherited_prompt": inherited,
|
||||
"inherited_from": inherited_from,
|
||||
**_chat_context(db, user, chat),
|
||||
**_sidebar_context(db, user),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user