forked from vhaudiquet/rhema-together
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.
This commit is contained in:
1 parent
2d36790fe1
commit
2216f37422
13 files changed
+724
-20
No files matched your search
@@ -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),
|
||||
|
||||
+5
-6
@@ -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(
|
||||
|
||||
@@ -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<Shared>,
|
||||
Path(id): Path<String>,
|
||||
) -> AppResult<Json<Value>> {
|
||||
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<String>>(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(
|
||||
|
||||
Reference in new issue
Block a user