Skip to main content

weft_core/api/
profile.rs

1use crate::api::openai_compat::AppState;
2use axum::extract::State;
3use axum::http::StatusCode;
4use axum::Json;
5
6pub async fn get_profile(State(state): State<AppState>) -> Json<serde_json::Value> {
7    let profile = state.active_profile.read().await;
8    Json(serde_json::json!({
9        "profile": profile.as_str(),
10    }))
11}
12
13pub async fn set_profile(
14    State(state): State<AppState>,
15    Json(body): Json<serde_json::Value>,
16) -> Result<Json<serde_json::Value>, (StatusCode, Json<serde_json::Value>)> {
17    let profile_str = body["profile"].as_str().ok_or_else(|| {
18        (
19            StatusCode::BAD_REQUEST,
20            Json(serde_json::json!({"error": "Missing 'profile' field"})),
21        )
22    })?;
23
24    let new_profile = crate::app::AppProfile::from_str_loose(profile_str);
25    *state.active_profile.write().await = new_profile;
26
27    Ok(Json(serde_json::json!({
28        "profile": new_profile.as_str(),
29        "status": "switched",
30    })))
31}
32
33pub async fn get_policy(State(state): State<AppState>) -> Json<serde_json::Value> {
34    let profile = state.active_profile.read().await;
35    Json(serde_json::json!({
36        "profile": profile.as_str(),
37        "rules": state.core_policy.rules,
38    }))
39}