orign 0.2.3

A globally distributed container orchestrator
Documentation
use crate::models::{Models, V1UserProfile};
use crate::state::AppState;
use axum::{
    extract::Extension, extract::Json, extract::State, http::StatusCode, response::IntoResponse,
};

#[axum::debug_handler]
pub async fn list_models(
    State(state): State<AppState>,
    Extension(user_profile): Extension<V1UserProfile>,
) -> impl IntoResponse {
    let mut owners = Vec::new();
    owners.push(user_profile.email.clone());

    // Add organization IDs
    let organization_ids: Vec<String> = if let Some(orgs) = &user_profile.organizations {
        orgs.keys().cloned().collect()
    } else {
        Vec::new()
    };
    owners.extend(organization_ids);

    // Initialize a vector to hold the adapter names
    let mut models = Vec::new();

    // Iterate over all namespaces and collect adapters
    for owner in owners {
        // Construct the path to the adapters directory for this namespace
        let models_path = format!("/orign/{}/models", owner);

        // Read the directories in the adapters path
        match tokio::fs::read_dir(&models_path).await {
            Ok(mut dir) => {
                while let Ok(Some(entry)) = dir.next_entry().await {
                    let file_type = match entry.file_type().await {
                        Ok(ft) => ft,
                        Err(err) => {
                            return Err((
                                StatusCode::INTERNAL_SERVER_ERROR,
                                format!("Failed to get file type: {}", err),
                            ));
                        }
                    };

                    if file_type.is_dir() {
                        if let Some(name) = entry.file_name().to_str() {
                            // Determine the display name based on whether "owner" is the user's email or part of orgs
                            let resolved_owner = if owner == user_profile.email {
                                user_profile
                                    .handle
                                    .clone()
                                    .unwrap_or(user_profile.email.clone())
                            } else {
                                user_profile
                                    .organizations
                                    .as_ref()
                                    .and_then(|orgs| orgs.get(&owner))
                                    .and_then(|org_data| org_data.get("org_name"))
                                    .cloned()
                                    .unwrap_or(owner.clone())
                            };

                            models.push(format!("{}/{}", resolved_owner, name));
                        }
                    }
                }
            }
            Err(_) => {
                // If the directory doesn't exist, skip this namespace
                continue;
            }
        }
    }

    // Return the list of adapters
    Ok(Json(Models { models }))
}