(base_url: &str, api_key: Option<&str>)
| 53 | } |
| 54 | |
| 55 | async fn fetch_openai_models(base_url: &str, api_key: Option<&str>) -> AppResult<Vec<String>> { |
| 56 | let url = format!("{}/models", base_url.trim_end_matches('/')); |
| 57 | let client = http_client::request_client()?; |
| 58 | let mut req = client.get(&url); |
| 59 | |
| 60 | if let Some(key) = api_key { |
| 61 | if !key.is_empty() { |
| 62 | req = req.bearer_auth(key); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | let resp = req |
| 67 | .send() |
| 68 | .await |
| 69 | .map_err(|e| AppError::Internal(format!("Failed to fetch models: {e}")))?; |
| 70 | |
| 71 | if !resp.status().is_success() { |
| 72 | let status = resp.status(); |
| 73 | let body = resp.text().await.unwrap_or_default(); |
| 74 | return Err(AppError::Internal(format!( |
| 75 | "Models endpoint returned {status}: {body}" |
| 76 | ))); |
| 77 | } |
| 78 | |
| 79 | let list: ModelList = resp |
| 80 | .json() |
| 81 | .await |
| 82 | .map_err(|e| AppError::Internal(format!("Failed to parse models response: {e}")))?; |
| 83 | |
| 84 | let mut ids: Vec<String> = list.data.into_iter().map(|m| m.id).collect(); |
| 85 | ids.sort(); |
| 86 | Ok(ids) |
| 87 | } |
| 88 | |
| 89 | /// Fetch models from an Anthropic-format endpoint. |
| 90 | /// |
no test coverage detected