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:
Jaroslav Beneš
2026-07-21 12:59:52 +02:00
parent d90195015c
commit 29db54960e
29 changed files with 1238 additions and 532 deletions
+59 -18
View File
@@ -144,22 +144,61 @@ def _add_connection(db) -> Connection:
return connection
def test_new_chat_redirects_to_its_own_url(client: TestClient, db, registered):
# --- Starting a chat ---------------------------------------------------------
def test_starting_a_chat_creates_it_and_redirects(client: TestClient, db, registered):
_add_connection(db)
response = client.post("/api/chats", headers={"HX-Request": "true"})
response = client.post("/api/chats/start", data={"content": "Hello there"})
assert response.status_code == 204
assert response.headers["HX-Redirect"].startswith("/chat/")
chat = db.scalar(select(Chat))
assert chat.model_id == "test-model"
messages = db.scalars(select(Message).order_by(Message.created_at)).all()
assert [m.role for m in messages] == ["user", "assistant"]
assert messages[0].content == "Hello there"
def test_new_chat_picks_up_the_default_model(client: TestClient, db, registered):
def test_starting_with_nothing_creates_no_chat(client: TestClient, db, registered):
"""The whole point of lazy creation: an abandoned composer leaves nothing."""
_add_connection(db)
client.post("/api/chats", headers={"HX-Request": "true"})
assert client.post("/api/chats/start", data={"content": " "}).status_code == 204
assert db.scalar(select(Chat)) is None
def test_visiting_the_chat_page_creates_nothing(client: TestClient, db, registered):
_add_connection(db)
assert client.get("/chat").status_code == 200
assert db.scalar(select(Chat)) is None
def test_starting_a_chat_honours_the_requested_model(client: TestClient, db, registered):
"""The pinned-model shortcuts pass ?model=, which arrives here."""
connection = _add_connection(db)
db.add(Model(connection_id=connection.id, model_id="other-model", position=5))
db.commit()
client.post("/api/chats/start", data={"content": "hi", "model_id": "other-model"})
assert db.scalar(select(Chat)).model_id == "other-model"
def test_starting_a_chat_ignores_a_model_you_cannot_reach(client: TestClient, db, registered):
connection = _add_connection(db)
db.add(Model(connection_id=connection.id, model_id="secret", position=5, public=False))
db.commit()
client.post("/auth/logout", follow_redirects=False)
client.post(
"/auth/register",
data={"name": "Sam", "email": "sam@shire.test", "password": "potatoes-po-ta-toes"},
follow_redirects=False,
)
client.post("/api/chats/start", data={"content": "hi", "model_id": "secret"})
assert db.scalar(select(Chat)).model_id == "test-model"
def test_posting_a_message_stores_both_turns(client: TestClient, db, registered):
def test_posting_a_message_stores_both_turns(client: TestClient, db, registered, make_chat):
_add_connection(db)
chat_id = client.post("/api/chats").headers["HX-Redirect"].rsplit("/", 1)[1]
chat_id = make_chat()
response = client.post(f"/api/chats/{chat_id}/messages", data={"content": "Hello there"})
assert response.status_code == 200
@@ -174,16 +213,18 @@ def test_posting_a_message_stores_both_turns(client: TestClient, db, registered)
assert "sse-connect" in response.text
def test_empty_message_is_ignored(client: TestClient, db, registered):
def test_empty_message_is_ignored(client: TestClient, db, registered, make_chat):
_add_connection(db)
chat_id = client.post("/api/chats").headers["HX-Redirect"].rsplit("/", 1)[1]
chat_id = make_chat()
assert client.post(f"/api/chats/{chat_id}/messages", data={"content": " "}).status_code == 204
assert db.scalar(select(Message)) is None
def test_a_chat_belonging_to_someone_else_is_not_found(client: TestClient, db, registered):
def test_a_chat_belonging_to_someone_else_is_not_found(
client: TestClient, db, registered, make_chat
):
_add_connection(db)
chat_id = client.post("/api/chats").headers["HX-Redirect"].rsplit("/", 1)[1]
chat_id = make_chat()
client.post("/auth/logout", follow_redirects=False)
client.post(
@@ -195,9 +236,9 @@ def test_a_chat_belonging_to_someone_else_is_not_found(client: TestClient, db, r
assert client.get(f"/chat/{chat_id}").status_code == 404
def test_renaming_a_chat_stops_it_being_auto_titled(client: TestClient, db, registered):
def test_renaming_a_chat_stops_it_being_auto_titled(client: TestClient, db, registered, make_chat):
_add_connection(db)
chat_id = client.post("/api/chats").headers["HX-Redirect"].rsplit("/", 1)[1]
chat_id = make_chat()
client.patch(f"/api/chats/{chat_id}", data={"title": "My own title"})
chat = db.get(Chat, chat_id)
@@ -206,9 +247,9 @@ def test_renaming_a_chat_stops_it_being_auto_titled(client: TestClient, db, regi
assert chat.title_generated is True
def test_deleting_a_chat_removes_its_messages(client: TestClient, db, registered):
def test_deleting_a_chat_removes_its_messages(client: TestClient, db, registered, make_chat):
_add_connection(db)
chat_id = client.post("/api/chats").headers["HX-Redirect"].rsplit("/", 1)[1]
chat_id = make_chat()
client.post(f"/api/chats/{chat_id}/messages", data={"content": "Hello"})
client.delete(f"/api/chats/{chat_id}")
@@ -216,13 +257,13 @@ def test_deleting_a_chat_removes_its_messages(client: TestClient, db, registered
assert db.scalar(select(Message)) is None
def test_deleting_a_folder_keeps_the_chats_inside_it(client: TestClient, db, registered):
def test_deleting_a_folder_keeps_the_chats_inside_it(client: TestClient, db, registered, make_chat):
"""Losing a conversation to a mis-clicked folder delete is unforgivable."""
_add_connection(db)
client.post("/api/folders", data={"name": "Quests"})
folder = db.scalar(select(Folder))
chat_id = client.post("/api/chats").headers["HX-Redirect"].rsplit("/", 1)[1]
chat_id = make_chat()
client.patch(f"/api/chats/{chat_id}", data={"folder_id": folder.id})
client.delete(f"/api/folders/{folder.id}")
@@ -296,10 +337,10 @@ def test_system_prompt_leads_the_message_list(db, user_id):
def test_streaming_reports_an_unreachable_endpoint_in_the_thread(
client: TestClient, db, registered
):
, make_chat):
"""A failed turn must never be an unexplained blank bubble."""
_add_connection(db) # points at 127.0.0.1:1, which refuses connections
chat_id = client.post("/api/chats").headers["HX-Redirect"].rsplit("/", 1)[1]
chat_id = make_chat()
client.post(f"/api/chats/{chat_id}/messages", data={"content": "Hello"})
message = db.scalar(select(Message).where(Message.role == "assistant"))