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:
2026-11-09 17:27:46 +01:00
parent ada5745c53
commit 4d53d5afa3
35 changed files with 3434 additions and 21 deletions

View File

@ -0,0 +1,71 @@
//! 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.
//!
//! Es gibt noch keinen Scheduler (M3): Eine Instanz startet bei der ersten
//! Anfrage und läuft bis zum Beenden des Gateways. Ressourcenplanung,
//! Verdrängung und Idle-Timeouts fehlen.
mod auth;
mod chat;
mod error;
mod models;
mod runtime;
mod state;
use std::sync::Arc;
use axum::Router;
use axum::extract::{DefaultBodyLimit, Request};
use axum::http::{HeaderValue, StatusCode};
use axum::middleware::{self, Next};
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
pub use error::ApiError;
pub use runtime::{Runtime, RuntimeError};
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))
.fallback(not_found)
.layer(DefaultBodyLimit::max(MAX_BODY_BYTES))
.layer(middleware::from_fn(request_id))
.with_state(state)
}
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.",
)
}
/// Hängt jeder Antwort eine `x-request-id` an.
async fn request_id(request: Request, next: Next) -> Response {
let id = nexus_core::random_hex(8)
.map(|h| format!("req_{h}"))
.unwrap_or_else(|_| "req_unbekannt".to_string());
let mut response = next.run(request).await;
if let Ok(value) = HeaderValue::from_str(&id) {
response.headers_mut().insert("x-request-id", value);
}
response
}