feat: OpenAI-kompatible API mit Keys, Registry und serve-Befehl (M2.3)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
48
crates/nexus-api/src/models.rs
Normal file
48
crates/nexus-api/src/models.rs
Normal file
@ -0,0 +1,48 @@
|
||||
//! `GET /v1/models` und `GET /v1/models/{id}`.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::Json;
|
||||
use axum::extract::{Path, State};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::auth::Authed;
|
||||
use crate::error::ApiError;
|
||||
use crate::state::AppState;
|
||||
|
||||
fn entry(name: &str) -> Value {
|
||||
json!({ "id": name, "object": "model", "created": 0, "owned_by": "nexus" })
|
||||
}
|
||||
|
||||
/// Auch Modelle, die gerade nicht geladen sind, erscheinen im Katalog.
|
||||
pub async fn list(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Authed(key): Authed,
|
||||
) -> Result<Json<Value>, ApiError> {
|
||||
let models = state.store().list_models(true).map_err(|e| {
|
||||
eprintln!("FEHLER Modellliste: {e}");
|
||||
ApiError::internal()
|
||||
})?;
|
||||
let data: Vec<Value> = models
|
||||
.iter()
|
||||
.filter(|m| key.rights.allows(&m.name))
|
||||
.map(|m| entry(&m.name))
|
||||
.collect();
|
||||
Ok(Json(json!({ "object": "list", "data": data })))
|
||||
}
|
||||
|
||||
pub async fn retrieve(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Authed(key): Authed,
|
||||
Path(id): Path<String>,
|
||||
) -> Result<Json<Value>, ApiError> {
|
||||
let models = state.store().list_models(true).map_err(|e| {
|
||||
eprintln!("FEHLER Modellliste: {e}");
|
||||
ApiError::internal()
|
||||
})?;
|
||||
if key.rights.allows(&id) && models.iter().any(|m| m.name == id) {
|
||||
Ok(Json(entry(&id)))
|
||||
} else {
|
||||
Err(ApiError::model_not_found(&id))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user