From 2216f37422843b380f65d2d575c5962297279b08 Mon Sep 17 00:00:00 2001 From: Valentin Haudiquet Date: Sat, 28 Nov 2026 17:20:19 +0100 Subject: [PATCH] feat: pick the room model from a catalog dialog The room model was a free-text field. It now opens a dialog that lists the provider's live catalog through the same adapter a turn uses, with context, output, price, and vision from models.dev. Rows filter by text, vision, or free price, and a custom id remains available when the provider lists nothing. The dialog is nested over room settings, so BaseDialog now gives Escape and the focus trap to the top dialog only. Clearing the model stores NULL, which returns the room to the provider's default. --- README.md | 4 +- specs/rhema-together.md | 3 + src/api/mod.rs | 4 + src/api/rooms.rs | 11 +- src/api/settings.rs | 77 +++++++ web/app/assets/css/main.css | 227 +++++++++++++++++++++ web/app/components/BaseDialog.vue | 15 +- web/app/components/ModelPickerDialog.vue | 235 ++++++++++++++++++++++ web/app/components/NewRoomDialog.vue | 44 +++- web/app/components/RoomSettingsDialog.vue | 54 ++++- web/app/types.ts | 24 +++ web/app/utils/dialogStack.ts | 21 ++ web/app/utils/format.ts | 25 +++ 13 files changed, 724 insertions(+), 20 deletions(-) create mode 100644 web/app/components/ModelPickerDialog.vue create mode 100644 web/app/utils/dialogStack.ts diff --git a/README.md b/README.md index 3b8c9ce..3b8f61f 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,9 @@ issue certificates. 3. **New room.** Pick the target and provider, give the workspace directory (for example `/home/me/rooms/demo`), and optionally a git URL. The workspace is created on the first mention and seeded from - the repository or a README. + the repository or a README. The Model field opens the provider's + model list with context, output, price, and vision. Pick a row, or + type an id the list does not carry. 4. **Invite.** The room's invite dialog creates a single-use link such as `https://together.example.com/join/`. A friend opens it, picks a display name, and lands in the room as a guest. diff --git a/specs/rhema-together.md b/specs/rhema-together.md index 246e5f1..1732b0f 100644 --- a/specs/rhema-together.md +++ b/specs/rhema-together.md @@ -665,6 +665,9 @@ This repository, as committed: - Rooms with members, a per-room provider config, a sandbox target, a workspace, an optional repository URL, and an owner-only model and provider switch. +- A model picker over the provider's live catalog, with context, + output, price, and vision from the adapter and models.dev. A custom + id is always accepted. - One conversation per room, a `@rhema` mention starts a turn, human messages are stamped with their author's name, and only the `message` tool puts agent text in the room. diff --git a/src/api/mod.rs b/src/api/mod.rs index fe5bc4d..b2ed95e 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -41,6 +41,10 @@ pub fn router(state: Shared) -> Router { get(settings::list_providers).post(settings::create_provider), ) .route("/provider-configs/{id}", delete(settings::delete_provider)) + .route( + "/provider-configs/{id}/models", + get(settings::list_provider_models), + ) .route( "/sandbox-targets", get(settings::list_targets).post(settings::create_target), diff --git a/src/api/rooms.rs b/src/api/rooms.rs index a144645..8cde5c9 100644 --- a/src/api/rooms.rs +++ b/src/api/rooms.rs @@ -341,12 +341,11 @@ pub async fn update( } if let Some(model) = body.model.as_deref() { let model = model.trim(); - if !model.is_empty() { - state.db.conn().execute( - "UPDATE rooms SET model = ?1 WHERE id = ?2", - rusqlite::params![model, id], - )?; - } + let model = (!model.is_empty()).then_some(model); + state.db.conn().execute( + "UPDATE rooms SET model = ?1 WHERE id = ?2", + rusqlite::params![model, id], + )?; } if let Some(steps) = body.max_turn_steps { state.db.conn().execute( diff --git a/src/api/settings.rs b/src/api/settings.rs index aa48de4..ea1c907 100644 --- a/src/api/settings.rs +++ b/src/api/settings.rs @@ -6,6 +6,7 @@ use std::time::Duration; use axum::Json; use axum::extract::{Path, State}; +use axum::http::StatusCode; use rhema_sandbox::Sandbox; use serde::Deserialize; use serde_json::{Value, json}; @@ -112,6 +113,82 @@ pub async fn create_provider( Ok(Json(json!({ "id": id }))) } +/// The provider's live model catalog. The registry builds the same +/// adapter a turn uses and asks it for the rows a coding agent can +/// drive; the model-facts decorator fills price, context, and vision +/// from models.dev where the provider leaves them blank. +pub async fn list_provider_models( + user: AuthUser, + State(state): State, + Path(id): Path, +) -> AppResult> { + let (kind, model, base_url, ciphertext) = state + .db + .conn() + .query_row( + "SELECT kind, model, base_url, api_key_ciphertext FROM provider_configs + WHERE id = ?1 AND account_id = ?2", + rusqlite::params![id, user.user.id], + |row| { + Ok(( + row.get::<_, String>(0)?, + row.get::<_, String>(1)?, + row.get::<_, Option>(2)?, + row.get::<_, String>(3)?, + )) + }, + ) + .map_err(|err| match err { + rusqlite::Error::QueryReturnedNoRows => AppError::not_found("provider not found"), + other => other.into(), + })?; + let key = state.secrets.open_str(&ciphertext)?; + let llama_url = (kind == "llama-cpp") + .then_some(base_url.as_deref()) + .flatten(); + let built = + rhema_provider_registry::build(&kind, Some(&key), llama_url, base_url.as_deref(), None) + .map_err(|err| AppError::bad_request(format!("provider: {err}")))?; + let models = tokio::time::timeout(Duration::from_secs(30), built.listing.list_models()) + .await + .map_err(|_| { + AppError::new( + StatusCode::GATEWAY_TIMEOUT, + "the provider did not answer in time", + ) + })? + .map_err(|err| { + tracing::warn!(provider = %id, error = %err, "provider catalog"); + AppError::new(StatusCode::BAD_GATEWAY, catalog_error(&err)) + })?; + Ok(Json(json!({ + "provider": { "id": id, "kind": kind, "model": model }, + "models": models, + }))) +} + +/// A short reason for a failed catalog fetch. The provider's own body +/// can be an HTML error page and is not worth showing; the full error +/// stays in the log. +fn catalog_error(err: &rhema_core::ProviderError) -> String { + use rhema_core::ProviderError; + match err { + ProviderError::Api { + status: Some(status), + .. + } => format!("the provider answered with HTTP {status}"), + ProviderError::Api { status: None, .. } => { + "the provider returned an unreadable catalog".to_owned() + } + ProviderError::RateLimited { .. } => { + "the provider rate limited the catalog request".to_owned() + } + ProviderError::Network(_) => "could not reach the provider".to_owned(), + ProviderError::Timeout => "the provider did not answer in time".to_owned(), + other => other.to_string(), + } +} + /// Removes a provider configuration. Rooms that used it are left with /// no provider until the owner picks another. pub async fn delete_provider( diff --git a/web/app/assets/css/main.css b/web/app/assets/css/main.css index f522d1a..265f0e8 100644 --- a/web/app/assets/css/main.css +++ b/web/app/assets/css/main.css @@ -309,6 +309,66 @@ button { color: var(--faint); } +.model-trigger { + display: flex; + align-items: center; + justify-content: space-between; + gap: var(--sp-3); + width: 100%; + min-height: 36px; + padding: var(--sp-2) var(--sp-3); + background: var(--surface); + border: 1px solid var(--border-strong); + border-radius: var(--r-control); + color: var(--text); + font-size: var(--fs-ui); + text-align: left; +} + +.model-trigger:hover:not(:disabled) { + background: var(--surface-2); +} + +.model-trigger:focus-visible { + border-color: var(--accent); + outline: 2px solid var(--accent); + outline-offset: -1px; +} + +.model-trigger:disabled { + background: var(--surface-2); + color: var(--faint); + cursor: not-allowed; +} + +.model-trigger > svg { + flex: none; + color: var(--muted); +} + +.model-trigger:disabled > svg { + color: var(--faint); +} + +.model-trigger-value { + display: flex; + align-items: baseline; + gap: var(--sp-2); + min-width: 0; +} + +.model-trigger-name { + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.model-trigger-note { + flex: none; + color: var(--muted); + font-size: var(--fs-small); +} + .btn { display: inline-flex; align-items: center; @@ -536,6 +596,173 @@ button { margin-top: var(--sp-6); } +.model-picker { + display: flex; + flex-direction: column; + gap: var(--sp-3); +} + +.model-search { + position: relative; +} + +.model-search-icon { + position: absolute; + left: var(--sp-3); + top: 50%; + transform: translateY(-50%); + color: var(--faint); + pointer-events: none; +} + +.model-search-input { + padding-left: var(--sp-8); +} + +.model-filter-bar { + display: flex; + align-items: center; + gap: var(--sp-4); +} + +.model-check { + display: inline-flex; + align-items: center; + gap: var(--sp-2); + color: var(--muted); + font-size: var(--fs-meta); +} + +.model-check input { + accent-color: var(--accent); +} + +.model-count { + margin-left: auto; + color: var(--faint); + font-size: var(--fs-small); +} + +.model-state { + padding: var(--sp-4) 0; + color: var(--muted); + font-size: var(--fs-ui); +} + +.model-list { + max-height: min(52vh, 520px); + overflow-y: auto; + border-top: 1px solid var(--border); + border-bottom: 1px solid var(--border); +} + +.model-row { + display: flex; + width: 100%; + padding: var(--sp-3) var(--sp-2); + border: 0; + border-bottom: 1px solid var(--border); + background: transparent; + color: var(--text); + text-align: left; + cursor: pointer; +} + +.model-row:last-child { + border-bottom: 0; +} + +.model-row:hover { + background: var(--surface-2); +} + +.model-row:focus-visible { + outline: 2px solid var(--accent); + outline-offset: -2px; +} + +.model-row-active, +.model-row-active:hover { + background: var(--accent-soft); +} + +.model-row-body { + display: flex; + flex: 1; + flex-direction: column; + gap: var(--sp-1); + min-width: 0; +} + +.model-row-head { + display: flex; + align-items: center; + gap: var(--sp-2); +} + +.model-row-title { + font-size: var(--fs-ui); + font-weight: 500; +} + +.model-row-check { + flex: none; + margin-left: auto; + color: var(--accent); +} + +.model-row-id { + color: var(--muted); + font-family: var(--font-mono); + font-size: var(--fs-small); + overflow-wrap: anywhere; +} + +.model-row-desc { + display: -webkit-box; + overflow: hidden; + color: var(--muted); + font-size: var(--fs-meta); + line-height: var(--lh-body); + -webkit-box-orient: vertical; + -webkit-line-clamp: 2; +} + +.model-row-facts { + display: flex; + flex-wrap: wrap; + gap: var(--sp-1) var(--sp-3); + color: var(--muted); + font-size: var(--fs-small); +} + +.model-fact { + white-space: nowrap; +} + +.model-fact-accent { + color: var(--accent); +} + +.model-fact-muted { + color: var(--faint); +} + +.model-error { + display: flex; + align-items: flex-start; + justify-content: space-between; + gap: var(--sp-3); + padding: var(--sp-4) var(--sp-2); +} + +.model-empty { + padding: var(--sp-6) var(--sp-2); + color: var(--muted); + font-size: var(--fs-ui); + text-align: center; +} + .code-block { margin: 0; padding: var(--sp-2); diff --git a/web/app/components/BaseDialog.vue b/web/app/components/BaseDialog.vue index d654a79..e4a3628 100644 --- a/web/app/components/BaseDialog.vue +++ b/web/app/components/BaseDialog.vue @@ -1,6 +1,7 @@ diff --git a/web/app/components/ModelPickerDialog.vue b/web/app/components/ModelPickerDialog.vue new file mode 100644 index 0000000..09e895f --- /dev/null +++ b/web/app/components/ModelPickerDialog.vue @@ -0,0 +1,235 @@ + + + diff --git a/web/app/components/NewRoomDialog.vue b/web/app/components/NewRoomDialog.vue index af122fc..165e327 100644 --- a/web/app/components/NewRoomDialog.vue +++ b/web/app/components/NewRoomDialog.vue @@ -1,6 +1,7 @@