Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ douanectl endpoint remove groq

The same fields go through `POST /admin/api/accounts/import` with `{"type":"openai_compatible","route":...,"base_url":...,"api_key":...}`, and `PATCH /admin/api/accounts/{id}` changes `route`, `base_url`, `dialect`, `models` or `api_key`. An account without a route keeps its previous behavior under `openai_compatible/`.

The **Models** page lists models under their provider or route, each one folded until opened. A provider rereads its catalog on its own (`POST /admin/api/models/refresh?pool=openrouter`; no `pool` rereads all), and its switch turns every shown model on or off in one write (`PATCH /admin/api/models` with `{"ids":[...],"enabled":false}`). When a live listing fails, the provider keeps its previous models, is marked as such, and is reread every minute.

## Routing and quotas

Pick a strategy in **Settings**, globally or per provider. It applies to new conversations:
Expand Down
5 changes: 4 additions & 1 deletion crates/douane/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ pub fn router(state: Arc<AppState>) -> Router {
post(management::redeem_reset),
)
.route("/quotas/refresh", post(management::refresh_quotas))
.route("/models", get(management::models))
.route(
"/models",
get(management::models).patch(management::set_models),
)
.route("/models/refresh", post(management::refresh_models))
.route("/models/{*id}", patch(management::set_model))
.route("/calls", get(management::calls))
Expand Down
10 changes: 4 additions & 6 deletions crates/douane/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,10 @@ async fn main() -> Result<()> {
poller.quotas.save().await;
}
// A live model listing that failed is read again every minute.
if pass.is_multiple_of(4)
&& poller
.models_incomplete
.load(std::sync::atomic::Ordering::Relaxed)
{
poller.refresh_models().await;
if pass.is_multiple_of(4) {
for pool in poller.stale_pools() {
poller.refresh_models_in(Some(&pool)).await;
}
}
tokio::time::sleep(Duration::from_secs(15)).await;
}
Expand Down
89 changes: 70 additions & 19 deletions crates/douane/src/management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,11 +640,28 @@ pub async fn refresh_quotas(
Json(json!({"quotas":state.quotas.list().await}))
}
pub async fn models(State(state): State<Arc<AppState>>) -> Json<Value> {
Json(json!({"models":*state.models.read().await}))
model_listing(&state).await
}
pub async fn refresh_models(State(state): State<Arc<AppState>>) -> Json<Value> {
state.refresh_models().await;
Json(json!({"models":*state.models.read().await}))
async fn model_listing(state: &AppState) -> Json<Value> {
let models = state.models.read().await.clone();
let listings = state
.model_listings
.lock()
.unwrap_or_else(|e| e.into_inner())
.clone();
Json(json!({"models":models,"listings":listings}))
}
#[derive(Default, Deserialize)]
pub struct RefreshModels {
/// One pool (a provider or a custom endpoint route); every pool without it.
pub pool: Option<String>,
}
pub async fn refresh_models(
State(state): State<Arc<AppState>>,
Query(query): Query<RefreshModels>,
) -> Json<Value> {
state.refresh_models_in(query.pool.as_deref()).await;
model_listing(&state).await
}
pub async fn set_model(
State(state): State<Arc<AppState>>,
Expand All @@ -654,29 +671,63 @@ pub async fn set_model(
let Some(enabled) = body["enabled"].as_bool() else {
return error(StatusCode::BAD_REQUEST, "enabled must be a boolean");
};
match set_models_enabled(&state, std::slice::from_ref(&id), enabled).await {
Some(refusal) => refusal,
None => Json(json!({"id":id,"enabled":enabled})).into_response(),
}
}
#[derive(Deserialize)]
pub struct ModelsPatch {
pub ids: Vec<String>,
pub enabled: bool,
}
/// Turns several models on or off in one configuration write, such as a
/// whole provider from the Models page.
pub async fn set_models(
State(state): State<Arc<AppState>>,
Json(body): Json<ModelsPatch>,
) -> Response {
match set_models_enabled(&state, &body.ids, body.enabled).await {
Some(refusal) => refusal,
None => Json(json!({"ids":body.ids,"enabled":body.enabled})).into_response(),
}
}
/// Saves the change, or returns the refusal to send.
async fn set_models_enabled(state: &AppState, ids: &[String], enabled: bool) -> Option<Response> {
let _write = state.config_write.lock().await;
let mut config = state.config.read().await.clone();
if !state.models.read().await.iter().any(|m| m.id == id) {
return error(StatusCode::NOT_FOUND, "model not found");
}
if enabled {
config.disabled_models.remove(&id);
} else {
config.disabled_models.insert(id.clone());
{
let models = state.models.read().await;
if ids.iter().any(|id| !models.iter().any(|m| &m.id == id)) {
return Some(error(StatusCode::NOT_FOUND, "model not found"));
}
}
if let Some(path) = &state.config_path {
if config.save(path).await.is_err() {
return error(
StatusCode::INTERNAL_SERVER_ERROR,
"cannot save configuration",
);
for id in ids {
if enabled {
config.disabled_models.remove(id);
} else {
config.disabled_models.insert(id.clone());
}
}
if let Some(path) = &state.config_path
&& config.save(path).await.is_err()
{
return Some(error(
StatusCode::INTERNAL_SERVER_ERROR,
"cannot save configuration",
));
}
*state.config.write().await = config;
for model in state.models.write().await.iter_mut().filter(|m| m.id == id) {
for model in state
.models
.write()
.await
.iter_mut()
.filter(|m| ids.contains(&m.id))
{
model.enabled = enabled;
}
Json(json!({"id":id,"enabled":enabled})).into_response()
None
}
#[derive(Default, Deserialize)]
pub struct Params {
Expand Down
12 changes: 9 additions & 3 deletions crates/douane/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,13 @@ async fn run(mut downstream: WebSocket, state: Arc<AppState>, headers: HeaderMap
track_call(&mut calls, &state, &account, &initial, client.as_ref());
// Retain at most one request, and release its payload on the first forwarded event.
let mut retry_request = Some(initial);
let mut heartbeat = tokio::time::interval(Duration::from_secs(25));
let beat = Duration::from_millis(
state
.socket_heartbeat_ms
.load(std::sync::atomic::Ordering::Relaxed),
);
let silence = beat * 18 / 5;
let mut heartbeat = tokio::time::interval(beat);
heartbeat.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
heartbeat.tick().await;
let mut last_upstream = tokio::time::Instant::now();
Expand Down Expand Up @@ -648,11 +654,11 @@ async fn run(mut downstream: WebSocket, state: Arc<AppState>, headers: HeaderMap
}
}
_ = heartbeat.tick() => {
if last_downstream.elapsed() > Duration::from_secs(90) {
if last_downstream.elapsed() > silence {
close_for_recovery(&mut downstream).await;
break;
}
if last_upstream.elapsed() > Duration::from_secs(90) {
if last_upstream.elapsed() > silence {
reject(&mut downstream, "upstream websocket heartbeat timed out").await;
break;
}
Expand Down
Loading
Loading