1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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 }))
}