feat: implement models endpoint

This commit is contained in:
2026-09-30 00:56:09 -04:00
parent c6c0849cf5
commit f0e8516d7a
3 changed files with 72 additions and 5 deletions

View File

@@ -1 +1,2 @@
pub mod chat; pub mod chat;
pub mod models;

43
src/openai/models.rs Normal file
View File

@@ -0,0 +1,43 @@
use serde::{Deserialize, Serialize};
/// The object type, which is always "model".
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ModelObject {
Model,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Model<Id> {
/// The model identifier, which can be referenced in the API endpoints.
pub id: Id,
/// The Unix timestamp (in seconds) when the model was created.
pub created: i64,
/// The object type, which is always "model".
pub object: ModelObject,
/// The organization that owns the model.
pub owned_by: String,
/// The date when the model will shut down, or null if not announced.
#[serde(skip_serializing_if = "Option::is_none")]
pub shutdown_date: Option<String>,
}
/// The object type, which is always "list".
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ListObject {
List,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ListModelsResponse<Id> {
/// The list of currently available models.
pub data: Vec<Model<Id>>,
/// The object type, which is always "list".
pub object: ListObject,
}

View File

@@ -10,6 +10,7 @@ use axum::response::Sse;
use axum_extra::routing::RouterExt; use axum_extra::routing::RouterExt;
use axum_extra::routing::TypedPath; use axum_extra::routing::TypedPath;
use ecow::EcoString; use ecow::EcoString;
use enumset::{EnumSet, EnumSetType};
use headers::Header; use headers::Header;
use headers::authorization::Bearer; use headers::authorization::Bearer;
use mitsein::vec1::Vec1; use mitsein::vec1::Vec1;
@@ -50,7 +51,7 @@ enum Provider {
OpenRouter, OpenRouter,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, EnumSetType)]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
enum Shortcut { enum Shortcut {
XS, XS,
@@ -372,9 +373,7 @@ async fn create_chat_completion(
.tls_certs_only( .tls_certs_only(
webpki_root_certs::TLS_SERVER_ROOT_CERTS webpki_root_certs::TLS_SERVER_ROOT_CERTS
.iter() .iter()
.filter_map(|cert| { .filter_map(|cert| Certificate::from_der(&cert).ok()),
Certificate::from_der(&cert).ok()
}),
) )
.build() .build()
.expect("TODO"); .expect("TODO");
@@ -451,8 +450,32 @@ async fn create_chat_completion(
Err(ChatCompletionError::NoneSucceeded) Err(ChatCompletionError::NoneSucceeded)
} }
#[derive(Deserialize, TypedPath)]
#[typed_path("/v1/models")]
struct V1ModelsPath;
#[tracing::instrument]
async fn list_models(_: V1ModelsPath) -> Json<openai::models::ListModelsResponse<Shortcut>> {
let models = EnumSet::all()
.into_iter()
.map(|shortcut| openai::models::Model {
id: shortcut,
created: 0,
object: openai::models::ModelObject::Model,
owned_by: "nobody".into(),
shutdown_date: None,
});
Json(openai::models::ListModelsResponse {
data: models.collect(),
object: openai::models::ListObject::List,
})
}
pub fn create_router() -> Router<AppState> { pub fn create_router() -> Router<AppState> {
Router::new().typed_post(create_chat_completion) Router::new()
.typed_post(create_chat_completion)
.typed_get(list_models)
} }
pub fn create_app(state: AppState) -> Router<()> { pub fn create_app(state: AppState) -> Router<()> {