Users, groups, permissions, model settings and reasoning display
Four features, plus the schema machinery they needed. **Schema sync.** The first live instance had data in it, and create_all only creates missing *tables* -- a new column silently never appeared. db/migrations.py now diffs the declared models against the database and ALTER TABLE ... ADD COLUMN for what is missing, deriving a backfill default from the column type (SQLite refuses a NOT NULL column without one, and a Python-side `default=dict` cannot be expressed in DDL). Verified against a copy of the live database: eight changes applied, all rows preserved, second run a no-op. Renames, drops and retypes are still manual and say so. **Permissions.** A flat set of named booleans: an instance baseline widened by each group the user belongs to. A group grants and never denies -- with denies, "why can this user not do X" cannot be answered without simulating every group. Admins bypass entirely, because an admin can grant it back to themselves in two clicks and pretending otherwise is theatre. Model *access* is separate: public, or granted to groups. The picker is not the boundary -- switching a chat to a model you cannot reach is a 403. **Model settings.** Ordering, pinned-first, an instance default and a per-user default, display names, descriptions, capability flags, and uploaded images. Images are stored and served locally rather than by URL: a remote URL makes every page render a request to a third party. Uploads are validated by magic number, not the declared content type, and stored under a random name. Models with no image get a generated initial whose hue is derived from the model id, so it is stable. **Reasoning display.** Streams into its own collapsible block above the answer, labelled "Thought for 14 seconds", collapsed once finished, and never replayed as context on the next turn. Two sources: the reasoning_content delta field, and <think> tags inline in content -- the latter needs a streaming splitter because the tags arrive split across chunks. Models emitting no reasoning show nothing, via a :has() rule rather than JavaScript. Verified against qwen35-9b on llama-swap: 694 reasoning events, 52 answer tokens, cleanly separated. Two bugs found and fixed while testing: - A bare `Mapped[list]` relationship is treated by SQLAlchemy as a scalar and returns None instead of []. It needs the element type. - FastAPI substitutes the default for an empty form value, so with `x: str | None = Form(None)` a submitted `x=` is indistinguishable from an absent field. That silently broke clearing a system prompt or a temperature. update_chat now reads the raw form and checks key presence. 143 tests, ruff clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+34
-19
@@ -110,28 +110,43 @@ def build_request(db: DBSession, chat: Chat, *, upto: Message | None = None) ->
|
||||
}
|
||||
|
||||
|
||||
def default_model(db: DBSession) -> tuple[str, str] | None:
|
||||
"""First enabled model on the first enabled connection, or None."""
|
||||
model = db.scalar(
|
||||
select(Model)
|
||||
.join(Connection)
|
||||
.where(Model.enabled.is_(True), Connection.enabled.is_(True))
|
||||
.order_by(Connection.position, Model.model_id)
|
||||
)
|
||||
if model is None:
|
||||
def default_model(db: DBSession, user=None) -> tuple[str, str] | None:
|
||||
"""The model a new chat should start with, as (model_id, connection_id).
|
||||
|
||||
Preference order: the user's own choice, then the instance default, then
|
||||
whatever is first in the admin's ordering. Each is checked against what the
|
||||
user may actually reach, so a default they have lost access to falls
|
||||
through rather than producing a chat they cannot use.
|
||||
"""
|
||||
from lembas.security import permissions
|
||||
from lembas.services import settings_store
|
||||
|
||||
reachable = permissions.models_visible_to(db, user)
|
||||
if not reachable:
|
||||
return None
|
||||
return model.model_id, model.connection_id
|
||||
|
||||
by_id = {model.model_id: model for model in reachable}
|
||||
|
||||
preferred = (user.settings_json or {}).get("default_model") if user is not None else None
|
||||
if preferred and preferred in by_id:
|
||||
return preferred, by_id[preferred].connection_id
|
||||
|
||||
instance_default = settings_store.get(db, "default_model")
|
||||
if instance_default and instance_default in by_id:
|
||||
return instance_default, by_id[instance_default].connection_id
|
||||
|
||||
# Pinned models sort first, matching what the picker shows at the top.
|
||||
ordered = sorted(reachable, key=lambda m: (not m.pinned, m.position, m.model_id))
|
||||
chosen = ordered[0]
|
||||
return chosen.model_id, chosen.connection_id
|
||||
|
||||
|
||||
def available_models(db: DBSession) -> list[Model]:
|
||||
return list(
|
||||
db.scalars(
|
||||
select(Model)
|
||||
.join(Connection)
|
||||
.where(Model.enabled.is_(True), Connection.enabled.is_(True))
|
||||
.order_by(Connection.position, Model.model_id)
|
||||
)
|
||||
)
|
||||
def available_models(db: DBSession, user=None) -> list[Model]:
|
||||
"""Models this user may start a chat with, pinned first."""
|
||||
from lembas.security import permissions
|
||||
|
||||
reachable = permissions.models_visible_to(db, user)
|
||||
return sorted(reachable, key=lambda m: (not m.pinned, m.position, m.model_id))
|
||||
|
||||
|
||||
def fallback_title(text: str) -> str:
|
||||
|
||||
Reference in New Issue
Block a user