systemprompt_api/routes/gateway/
models.rs1use axum::Json;
7use axum::http::{HeaderMap, StatusCode};
8use serde::Serialize;
9use std::collections::BTreeMap;
10use systemprompt_config::ProfileBootstrap;
11use systemprompt_identifiers::headers::INFERENCE_PROTOCOL;
12use systemprompt_models::profile::{ApiSurface, ProviderRegistry};
13
14#[derive(Debug, Serialize)]
15pub struct RootResponse {
16 pub service: &'static str,
17 pub version: &'static str,
18 pub endpoints: Vec<&'static str>,
19}
20
21pub async fn root() -> Json<RootResponse> {
22 Json(RootResponse {
23 service: "systemprompt-gateway",
24 version: env!("CARGO_PKG_VERSION"),
25 endpoints: vec!["/v1/models", "/v1/messages"],
26 })
27}
28
29#[derive(Debug, Serialize)]
30pub struct ModelEntry {
31 #[serde(rename = "type")]
32 pub kind: &'static str,
33 pub id: String,
34 pub display_name: String,
35 pub created_at: String,
36}
37
38#[derive(Debug, Serialize)]
39pub struct ModelsResponse {
40 pub data: Vec<ModelEntry>,
41 pub has_more: bool,
42 #[serde(skip_serializing_if = "Option::is_none")]
43 pub first_id: Option<String>,
44 #[serde(skip_serializing_if = "Option::is_none")]
45 pub last_id: Option<String>,
46}
47
48pub async fn list(headers: HeaderMap) -> Result<Json<ModelsResponse>, (StatusCode, String)> {
49 let profile = ProfileBootstrap::get().map_err(|e| {
50 (
51 StatusCode::SERVICE_UNAVAILABLE,
52 format!("Profile not ready: {e}"),
53 )
54 })?;
55
56 profile
57 .gateway
58 .as_ref()
59 .and_then(systemprompt_models::profile::GatewayState::resolved)
60 .filter(|g| g.enabled)
61 .ok_or_else(|| (StatusCode::NOT_FOUND, "Gateway not enabled".to_owned()))?;
62
63 let surfaces = surfaces_from_header(&headers)?;
64 let entries = model_entries(&profile.providers, &surfaces);
65 let first_id = entries.first().map(|e| e.id.clone());
66 let last_id = entries.last().map(|e| e.id.clone());
67
68 Ok(Json(ModelsResponse {
69 data: entries,
70 has_more: false,
71 first_id,
72 last_id,
73 }))
74}
75
76pub fn surfaces_from_header(headers: &HeaderMap) -> Result<Vec<ApiSurface>, (StatusCode, String)> {
83 let Some(raw) = headers
84 .get(INFERENCE_PROTOCOL)
85 .and_then(|v| v.to_str().ok())
86 else {
87 return Ok(Vec::new());
88 };
89 let mut surfaces = Vec::new();
90 for tag in raw.split(',').map(str::trim).filter(|t| !t.is_empty()) {
91 let surface = ApiSurface::from_tag(tag)
92 .filter(|s| *s != ApiSurface::Backend)
93 .ok_or_else(|| {
94 (
95 StatusCode::BAD_REQUEST,
96 format!("unknown {INFERENCE_PROTOCOL} value: {tag}"),
97 )
98 })?;
99 surfaces.push(surface);
100 }
101 Ok(surfaces)
102}
103
104pub fn model_entries(registry: &ProviderRegistry, surfaces: &[ApiSurface]) -> Vec<ModelEntry> {
105 let mut by_id: BTreeMap<String, ModelEntry> = BTreeMap::new();
106 for id in registry.advertised_model_ids(surfaces) {
107 by_id.insert(
108 id.clone(),
109 ModelEntry {
110 kind: "model",
111 display_name: humanize_model_id(&id),
112 id,
113 created_at: "1970-01-01T00:00:00Z".to_owned(),
114 },
115 );
116 }
117 by_id.into_values().collect()
118}
119
120pub fn humanize_model_id(id: &str) -> String {
121 id.split('-')
122 .map(|part| {
123 let mut chars = part.chars();
124 chars.next().map_or_else(String::new, |c| {
125 c.to_ascii_uppercase().to_string() + chars.as_str()
126 })
127 })
128 .collect::<Vec<_>>()
129 .join(" ")
130}