Skip to main content

hyprstream_core/server/routes/
models.rs

1//! Model management endpoints
2
3use crate::{
4    auth::Operation,
5    server::{self, state::ServerState, AuthenticatedUser},
6    storage::paths::StoragePaths,
7};
8use axum::{
9    extract::{Json, Path, State},
10    http::StatusCode,
11    response::IntoResponse,
12    routing::{get, post},
13    Extension, Router,
14};
15use chrono;
16use serde::{Deserialize, Serialize};
17
18/// Resolve a ModelRef to its worktree path (locally derived, not from RPC).
19async fn resolve_model_path(
20    registry: &hyprstream_rpc_std::registry_client::RegistryClient,
21    model_ref: &crate::storage::ModelRef,
22) -> anyhow::Result<String> {
23    let tracked = registry.get_by_name(model_ref.name()).await?;
24    let repo = registry.repo(&tracked.id);
25    let branch = match &model_ref.git_ref {
26        crate::storage::GitRef::Branch(name) => name.clone(),
27        _ => repo.get_head().await?,
28    };
29    // Verify worktree exists
30    let wts = repo.list_worktrees().await?;
31    if !wts.iter().any(|wt| wt.branch_name == branch) {
32        anyhow::bail!("worktree for {}:{} not found", model_ref.name(), branch);
33    }
34    // Derive path locally
35    let storage_paths = StoragePaths::new()?;
36    Ok(storage_paths
37        .worktree_path(model_ref.name(), &branch)?
38        .to_string_lossy()
39        .to_string())
40}
41
42/// Create model management router
43pub fn create_router() -> Router<ServerState> {
44    Router::new()
45        .route("/list", get(list_models))
46        .route("/:id/info", get(get_model_info))
47        .route("/download", post(download_model))
48        .route("/:id/load", post(load_model))
49        .route("/:id/unload", post(unload_model))
50        .route("/cache/refresh", post(refresh_cache))
51    // REMOVED: .route("/cache/stats", get(cache_stats)) - ModelCache replaced by ZMQ
52}
53
54/// Request to download a model
55#[derive(Debug, Deserialize)]
56struct DownloadModelRequest {
57    uri: String,
58    name: Option<String>,
59}
60
61/// Response for model download
62#[derive(Debug, Serialize)]
63struct DownloadModelResponse {
64    id: String,
65    name: String,
66    status: String,
67    path: String,
68}
69
70/// Model list response item
71#[derive(Debug, Serialize)]
72struct ModelListItem {
73    id: String,
74    name: String,
75    display_name: Option<String>,
76    architecture: String,
77    size_bytes: u64,
78    is_cached: bool,
79    local_path: Option<String>,
80}
81
82#[allow(clippy::result_large_err)]
83fn policy_identity(
84    auth_user: Option<&Extension<AuthenticatedUser>>,
85) -> Result<(String, String, Option<String>), axum::response::Response> {
86    let (user, domain) = server::extract_policy_identity(auth_user).map_err(|_| {
87        (
88            StatusCode::FORBIDDEN,
89            Json(serde_json::json!({
90                "error": "Verified hosted-account tenant binding required"
91            })),
92        )
93            .into_response()
94    })?;
95    Ok((
96        user,
97        domain,
98        auth_user.and_then(|Extension(identity)| identity.token.clone()),
99    ))
100}
101
102/// List all available models
103async fn list_models(
104    State(state): State<ServerState>,
105    auth_user: Option<Extension<AuthenticatedUser>>,
106) -> impl IntoResponse {
107    let (user, domain, bearer) = match policy_identity(auth_user.as_ref()) {
108        Ok(identity) => identity,
109        Err(response) => return response,
110    };
111    let request = hyprstream_rpc_std::policy_client::PolicyCheck {
112        subject: user.clone(),
113        domain,
114        resource: "registry:*".to_owned(),
115        operation: Operation::Query.as_str().to_owned(),
116    };
117    if !crate::services::policy::check_with_verified_bearer(
118        &state.policy_client,
119        &request,
120        bearer.as_deref(),
121        &hyprstream_rpc::envelope::Subject::new(user.clone()),
122    )
123    .await
124    .unwrap_or(false)
125    {
126        return (
127            StatusCode::FORBIDDEN,
128            Json(serde_json::json!({
129                "error": format!("Permission denied: user '{}' cannot query registry", user)
130            })),
131        )
132            .into_response();
133    }
134
135    // Inline list_models: iterate repos + worktrees
136    let result: Result<Vec<ModelListItem>, anyhow::Error> = async {
137        let repos = state.registry.list().await?;
138        let mut model_list = Vec::new();
139        for repo in repos {
140            if repo.name.is_empty() {
141                continue;
142            }
143            let name = &repo.name;
144            match state.registry.repo(&repo.id).list_worktrees().await {
145                Ok(worktrees) => {
146                    for wt in worktrees {
147                        if wt.branch_name.is_empty() {
148                            continue;
149                        }
150                        let display = format!("{}:{}", name, wt.branch_name);
151                        model_list.push(ModelListItem {
152                            id: name.clone(),
153                            name: display.clone(),
154                            display_name: Some(display),
155                            architecture: "language_model".to_owned(),
156                            size_bytes: 0,
157                            is_cached: true,
158                            local_path: None, // path no longer exposed via RPC
159                        });
160                    }
161                }
162                Err(e) => {
163                    tracing::warn!("Failed to list worktrees for {}: {}", name, e);
164                }
165            }
166        }
167        Ok(model_list)
168    }
169    .await;
170
171    match result {
172        Ok(model_list) => Json(model_list).into_response(),
173        Err(e) => (
174            StatusCode::INTERNAL_SERVER_ERROR,
175            Json(serde_json::json!({
176                "error": format!("Failed to list models: {}", e)
177            })),
178        )
179            .into_response(),
180    }
181}
182
183/// Get information about a specific model
184async fn get_model_info(
185    State(state): State<ServerState>,
186    auth_user: Option<Extension<AuthenticatedUser>>,
187    Path(id): Path<String>,
188) -> impl IntoResponse {
189    let (user, domain, bearer) = match policy_identity(auth_user.as_ref()) {
190        Ok(identity) => identity,
191        Err(response) => return response,
192    };
193    let resource = format!("model:{id}");
194    let request = hyprstream_rpc_std::policy_client::PolicyCheck {
195        subject: user.clone(),
196        domain,
197        resource: resource.clone(),
198        operation: Operation::Query.as_str().to_owned(),
199    };
200    if !crate::services::policy::check_with_verified_bearer(
201        &state.policy_client,
202        &request,
203        bearer.as_deref(),
204        &hyprstream_rpc::envelope::Subject::new(user.clone()),
205    )
206    .await
207    .unwrap_or(false)
208    {
209        return (
210            StatusCode::FORBIDDEN,
211            Json(serde_json::json!({
212                "error": format!("Permission denied: user '{}' cannot query '{}'", user, resource)
213            })),
214        )
215            .into_response();
216    }
217
218    // Parse model reference and resolve path
219    use crate::storage::model_ref::ModelRef;
220    if let Ok(model_ref) = ModelRef::parse(&id) {
221        let path_result: Result<String, anyhow::Error> =
222            async { resolve_model_path(&state.registry, &model_ref).await }.await;
223        if let Ok(_path) = path_result {
224            // Create metadata for the found model
225            let metadata = crate::storage::ModelMetadata {
226                name: model_ref.name().to_owned(),
227                display_name: Some(id.clone()),
228                model_type: "language_model".to_owned(),
229                created_at: chrono::Utc::now().timestamp(),
230                updated_at: chrono::Utc::now().timestamp(),
231                size_bytes: None,
232                tags: vec![],
233                is_dirty: false, // Not available for single model lookup
234            };
235            return Json(metadata).into_response();
236        }
237    }
238
239    (
240        StatusCode::NOT_FOUND,
241        Json(serde_json::json!({
242            "error": "Model not found"
243        })),
244    )
245        .into_response()
246}
247
248/// Download a model from a registry
249async fn download_model(
250    State(state): State<ServerState>,
251    auth_user: Option<Extension<AuthenticatedUser>>,
252    Json(request): Json<DownloadModelRequest>,
253) -> impl IntoResponse {
254    let (user, domain, bearer) = match policy_identity(auth_user.as_ref()) {
255        Ok(identity) => identity,
256        Err(response) => return response,
257    };
258    let policy_request = hyprstream_rpc_std::policy_client::PolicyCheck {
259        subject: user.clone(),
260        domain,
261        resource: "registry:*".to_owned(),
262        operation: Operation::Write.as_str().to_owned(),
263    };
264    if !crate::services::policy::check_with_verified_bearer(
265        &state.policy_client,
266        &policy_request,
267        bearer.as_deref(),
268        &hyprstream_rpc::envelope::Subject::new(user.clone()),
269    )
270    .await
271    .unwrap_or(false)
272    {
273        return (
274            StatusCode::FORBIDDEN,
275            Json(serde_json::json!({
276                "error": format!("Permission denied: user '{}' cannot write to registry", user)
277            })),
278        )
279            .into_response();
280    }
281
282    // Basic validation - must be a non-empty string
283    let uri = request.uri.clone();
284    if uri.is_empty() {
285        return (
286            StatusCode::BAD_REQUEST,
287            Json(serde_json::json!({
288                "error": "Model URI cannot be empty"
289            })),
290        )
291            .into_response();
292    }
293
294    // Get storage paths and token
295    let storage_paths = match StoragePaths::new() {
296        Ok(paths) => paths,
297        Err(e) => {
298            return (
299                StatusCode::INTERNAL_SERVER_ERROR,
300                Json(serde_json::json!({
301                    "error": format!("Failed to get storage paths: {}", e)
302                })),
303            )
304                .into_response();
305        }
306    };
307
308    let _models_dir = match storage_paths.models_dir() {
309        Ok(dir) => dir,
310        Err(e) => {
311            return (
312                StatusCode::INTERNAL_SERVER_ERROR,
313                Json(serde_json::json!({
314                    "error": format!("Failed to get models directory: {}", e)
315                })),
316            )
317                .into_response();
318        }
319    };
320
321    // Derive model name from URL if not provided
322    let model_name = request.name.clone().unwrap_or_else(|| {
323        request
324            .uri
325            .split('/')
326            .next_back()
327            .unwrap_or("")
328            .trim_end_matches(".git")
329            .to_owned()
330    });
331
332    if model_name.is_empty() {
333        return (
334            StatusCode::BAD_REQUEST,
335            Json(serde_json::json!({
336                "error": "Cannot derive model name from URI. Please provide a name."
337            })),
338        )
339            .into_response();
340    }
341
342    // Use registry client to clone model (no duplicate service)
343    if let Err(e) = state
344        .registry
345        .clone(&hyprstream_rpc_std::registry_client::CloneRequest {
346            url: request.uri.clone(),
347            name: model_name.clone(),
348            shallow: true,
349            depth: 1,
350            branch: String::new(),
351        })
352        .await
353    {
354        return (
355            StatusCode::INTERNAL_SERVER_ERROR,
356            Json(serde_json::json!({
357                "error": format!("Failed to download model: {}", e)
358            })),
359        )
360            .into_response();
361    }
362
363    // Get model path for response
364    let model_ref = crate::storage::ModelRef::new(model_name.clone());
365    let model_path = match resolve_model_path(&state.registry, &model_ref).await {
366        Ok(path) => path,
367        Err(e) => {
368            return (
369                StatusCode::INTERNAL_SERVER_ERROR,
370                Json(serde_json::json!({
371                    "error": format!("Model cloned but failed to get path: {}", e)
372                })),
373            )
374                .into_response();
375        }
376    };
377
378    Json(DownloadModelResponse {
379        id: crate::storage::ModelId::new().to_string(),
380        name: model_name,
381        status: "downloaded".to_owned(),
382        path: model_path.clone(),
383    })
384    .into_response()
385}
386
387/// Load a model into the engine pool
388async fn load_model(
389    State(state): State<ServerState>,
390    auth_user: Option<Extension<AuthenticatedUser>>,
391    Path(id): Path<String>,
392) -> impl IntoResponse {
393    let (user, domain, bearer) = match policy_identity(auth_user.as_ref()) {
394        Ok(identity) => identity,
395        Err(response) => return response,
396    };
397    let resource = format!("model:{id}");
398    let request = hyprstream_rpc_std::policy_client::PolicyCheck {
399        subject: user.clone(),
400        domain,
401        resource: resource.clone(),
402        operation: Operation::Manage.as_str().to_owned(),
403    };
404    if !crate::services::policy::check_with_verified_bearer(
405        &state.policy_client,
406        &request,
407        bearer.as_deref(),
408        &hyprstream_rpc::envelope::Subject::new(user.clone()),
409    )
410    .await
411    .unwrap_or(false)
412    {
413        return (
414            StatusCode::FORBIDDEN,
415            Json(serde_json::json!({
416                "error": format!("Permission denied: user '{}' cannot manage '{}'", user, resource)
417            })),
418        )
419            .into_response();
420    }
421
422    // Parse model reference
423    let model_ref = match crate::storage::ModelRef::parse(&id) {
424        Ok(model_ref) => model_ref,
425        Err(_) => {
426            return (
427                StatusCode::BAD_REQUEST,
428                Json(serde_json::json!({
429                    "error": format!("Invalid model reference: '{}'", id)
430                })),
431            )
432                .into_response();
433        }
434    };
435
436    // Get model path
437    let model_path = match resolve_model_path(&state.registry, &model_ref).await {
438        Ok(path) => path,
439        Err(e) => {
440            return (
441                StatusCode::NOT_FOUND,
442                Json(serde_json::json!({
443                    "error": format!("Model not found: {}", e)
444                })),
445            )
446                .into_response();
447        }
448    };
449
450    // Load into engine pool
451    Json(serde_json::json!({
452        "status": "loaded",
453        "id": id,
454        "path": model_path
455    }))
456    .into_response()
457}
458
459/// Unload a model from the engine pool
460async fn unload_model(
461    State(state): State<ServerState>,
462    auth_user: Option<Extension<AuthenticatedUser>>,
463    Path(id): Path<String>,
464) -> impl IntoResponse {
465    let (user, domain, bearer) = match policy_identity(auth_user.as_ref()) {
466        Ok(identity) => identity,
467        Err(response) => return response,
468    };
469    let resource = format!("model:{id}");
470    let request = hyprstream_rpc_std::policy_client::PolicyCheck {
471        subject: user.clone(),
472        domain,
473        resource: resource.clone(),
474        operation: Operation::Manage.as_str().to_owned(),
475    };
476    if !crate::services::policy::check_with_verified_bearer(
477        &state.policy_client,
478        &request,
479        bearer.as_deref(),
480        &hyprstream_rpc::envelope::Subject::new(user.clone()),
481    )
482    .await
483    .unwrap_or(false)
484    {
485        return (
486            StatusCode::FORBIDDEN,
487            Json(serde_json::json!({
488                "error": format!("Permission denied: user '{}' cannot manage '{}'", user, resource)
489            })),
490        )
491            .into_response();
492    }
493
494    Json(serde_json::json!({
495        "status": "unloaded",
496        "id": id
497    }))
498    .into_response()
499}
500
501/// Refresh the model cache (rescans disk for models)
502async fn refresh_cache(
503    State(state): State<ServerState>,
504    auth_user: Option<Extension<AuthenticatedUser>>,
505) -> impl IntoResponse {
506    let (user, domain, bearer) = match policy_identity(auth_user.as_ref()) {
507        Ok(identity) => identity,
508        Err(response) => return response,
509    };
510    let request = hyprstream_rpc_std::policy_client::PolicyCheck {
511        subject: user.clone(),
512        domain,
513        resource: "registry:*".to_owned(),
514        operation: Operation::Manage.as_str().to_owned(),
515    };
516    if !crate::services::policy::check_with_verified_bearer(
517        &state.policy_client,
518        &request,
519        bearer.as_deref(),
520        &hyprstream_rpc::envelope::Subject::new(user.clone()),
521    )
522    .await
523    .unwrap_or(false)
524    {
525        return (
526            StatusCode::FORBIDDEN,
527            Json(serde_json::json!({
528                "error": format!("Permission denied: user '{}' cannot manage registry", user)
529            })),
530        )
531            .into_response();
532    }
533
534    // Cache is automatically maintained
535    Json(serde_json::json!({
536        "status": "success",
537        "message": "Model cache is automatically maintained"
538    }))
539    .into_response()
540}
541
542// REMOVED: cache_stats endpoint - ModelCache replaced by ZMQ ModelService