Scheduler 0.1.5, Strata 0.1.6, ComfyUI 0.1.7, Weboberfläche und PWA 0.1.8. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
147 lines
4.8 KiB
Rust
147 lines
4.8 KiB
Rust
//! OpenAI-kompatible HTTP-API und Gateway-Endpunkte (Spezifikation, Abschnitt 7).
|
|
//!
|
|
//! Stand M2.3: `GET /v1/models`, `GET /v1/models/{id}`,
|
|
//! `POST /v1/chat/completions` (mit SSE-Streaming), `GET /gateway/v1/health`,
|
|
//! API-Key-Authentifizierung und das OpenAI-Fehlerformat. Weitere Endpunkte
|
|
//! (Completions, Embeddings, Bilder) und die Gateway-Verwaltung folgen später.
|
|
//!
|
|
//! Seit 0.1.5 teilt der Scheduler (Modul `runtime`, Logik in
|
|
//! `nexus-scheduler`) jeder Anfrage eine Instanz zu: Ressourcen je GPU,
|
|
//! Warteschlange mit Prioritäten, Verdrängung und Leerlauf.
|
|
|
|
mod access;
|
|
mod auth;
|
|
mod chat;
|
|
mod error;
|
|
mod metrics;
|
|
mod models;
|
|
mod runtime;
|
|
mod state;
|
|
mod tap;
|
|
mod ui;
|
|
mod usage;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use axum::Router;
|
|
use axum::extract::DefaultBodyLimit;
|
|
use axum::http::StatusCode;
|
|
use axum::middleware;
|
|
use axum::response::IntoResponse;
|
|
use axum::routing::{get, post};
|
|
|
|
pub use error::ApiError;
|
|
pub use metrics::{Metrics, series_samples};
|
|
pub use runtime::{
|
|
Lease, PlannedInstance, Runtime, RuntimeError, RuntimeOptions, SchedulerSettings,
|
|
SchedulerView, WaitingJob,
|
|
};
|
|
pub use state::AppState;
|
|
|
|
/// Größtes erlaubtes Anfragen-Datenvolumen (Bilder und lange Verläufe).
|
|
const MAX_BODY_BYTES: usize = 32 * 1024 * 1024;
|
|
|
|
/// Baut den Router des Gateways.
|
|
pub fn router(state: Arc<AppState>) -> Router {
|
|
Router::new()
|
|
.route("/v1/models", get(models::list))
|
|
.route("/v1/models/{id}", get(models::retrieve))
|
|
.route("/v1/chat/completions", post(chat::completions))
|
|
.route("/gateway/v1/health", get(health))
|
|
.route("/gateway/v1/metrics", get(metrics_endpoint))
|
|
.route("/gateway/v1/status", get(status_endpoint))
|
|
.merge(ui::routes())
|
|
.fallback(not_found)
|
|
.layer(DefaultBodyLimit::max(MAX_BODY_BYTES))
|
|
.layer(middleware::from_fn_with_state(
|
|
state.clone(),
|
|
access::observe,
|
|
))
|
|
.with_state(state)
|
|
}
|
|
|
|
/// Metriken im Prometheus-Textformat; nur für Keys mit dem Recht `metrics`.
|
|
async fn metrics_endpoint(
|
|
axum::extract::State(state): axum::extract::State<Arc<AppState>>,
|
|
auth::Authed(key): auth::Authed,
|
|
) -> Result<impl IntoResponse, ApiError> {
|
|
if !key.rights.metrics {
|
|
return Err(ApiError::new(
|
|
StatusCode::FORBIDDEN,
|
|
"invalid_request_error",
|
|
"permission_denied",
|
|
"Dieser Key hat nicht das Recht \"metrics\".",
|
|
));
|
|
}
|
|
Ok((
|
|
[(
|
|
axum::http::header::CONTENT_TYPE,
|
|
"text/plain; version=0.0.4; charset=utf-8",
|
|
)],
|
|
state.runtime.metrics.render(),
|
|
))
|
|
}
|
|
|
|
/// Zustand des Gateways als JSON (für die Oberfläche und Skripte); für Keys
|
|
/// mit dem Recht `metrics` oder `ui`.
|
|
async fn status_endpoint(
|
|
axum::extract::State(state): axum::extract::State<Arc<AppState>>,
|
|
auth::Authed(key): auth::Authed,
|
|
) -> Result<impl IntoResponse, ApiError> {
|
|
if !key.rights.metrics && !key.rights.ui {
|
|
return Err(ApiError::new(
|
|
StatusCode::FORBIDDEN,
|
|
"invalid_request_error",
|
|
"permission_denied",
|
|
"Dieser Key hat weder das Recht \"metrics\" noch \"ui\".",
|
|
));
|
|
}
|
|
let instances = state.runtime.metrics.instances();
|
|
let scheduler = state.runtime.scheduler_view();
|
|
let models: Vec<serde_json::Value> = {
|
|
let store = state.store();
|
|
store
|
|
.list_models(false)
|
|
.unwrap_or_default()
|
|
.into_iter()
|
|
.map(|m| {
|
|
let plan = store.start_plan(&m.name).ok().flatten();
|
|
serde_json::json!({
|
|
"name": m.name,
|
|
"enabled": m.enabled,
|
|
"engine": plan.as_ref().map(|p| p.engine_name.clone()),
|
|
"gpus": plan.as_ref().map(|p| p.gpus.clone()),
|
|
"context": plan.as_ref().and_then(|p| p.context),
|
|
"loaded": scheduler
|
|
.instances
|
|
.iter()
|
|
.any(|i| i.model == m.name && i.state == "ready"),
|
|
"vram_mb": plan.as_ref().and_then(|p| p.vram_mb),
|
|
})
|
|
})
|
|
.collect()
|
|
};
|
|
Ok(axum::Json(serde_json::json!({
|
|
"version": nexus_core::VERSION,
|
|
"uptime_s": state.started.elapsed().as_secs(),
|
|
"totals": state.runtime.metrics.totals(),
|
|
"instances": instances,
|
|
"gpus": state.runtime.metrics.gpus(),
|
|
"models": models,
|
|
"scheduler": scheduler,
|
|
})))
|
|
}
|
|
|
|
async fn health() -> impl IntoResponse {
|
|
axum::Json(serde_json::json!({ "status": "ok", "version": nexus_core::VERSION }))
|
|
}
|
|
|
|
async fn not_found() -> ApiError {
|
|
ApiError::new(
|
|
StatusCode::NOT_FOUND,
|
|
"invalid_request_error",
|
|
"unknown_url",
|
|
"Unbekannte Adresse.",
|
|
)
|
|
}
|