The three adapters reduce pictures to a Responses input_image, the prompt renderer writes the template's picture items and returns their sources in prompt order, and the request path encodes them (in turn with the engine), expands every image pad to its token count and sends GENI with the combined SVE1 file. The encoder is configured by a top-level vision section, stops with the engine and starts before it. Goldens recorded from the Python handlers cover fourteen Chat and Messages requests. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
157 lines
5.5 KiB
Rust
157 lines
5.5 KiB
Rust
//! Configured deployment discovery; does not acquire a worker or imply readiness.
|
|
use axum::{
|
|
Json,
|
|
extract::{Path, RawQuery, State},
|
|
http::StatusCode,
|
|
response::{IntoResponse, Response},
|
|
};
|
|
use serde_json::{Value, json};
|
|
|
|
use crate::app::AppState;
|
|
|
|
fn record(id: &str, images: bool) -> Value {
|
|
// No upstream creation/ownership metadata is supplied by the local config.
|
|
// Zero is an explicit unknown timestamp sentinel; local names the deployment.
|
|
let mut value = json!({"id":id,"object":"model","created":0,"owned_by":"local"});
|
|
if images {
|
|
// Only announced when the encoder runs: the text-only record keeps its documented shape.
|
|
value["architecture"] =
|
|
json!({"input_modalities": ["text", "image"], "output_modalities": ["text"]});
|
|
}
|
|
value
|
|
}
|
|
|
|
/// The model and each configured alias (`alias_of` names the model), like the Original; an engine
|
|
/// that is unloaded on purpose says so (`status`) and still lists: it loads on use.
|
|
async fn records(state: &AppState) -> Vec<Value> {
|
|
let unloaded = state.worker.read().await.state == crate::worker::WorkerState::Unloaded;
|
|
let mark = |mut value: Value| {
|
|
if unloaded {
|
|
value["status"] = json!({"value": "unloaded"});
|
|
}
|
|
value
|
|
};
|
|
let Some(model) = state.model.as_deref() else {
|
|
return Vec::new();
|
|
};
|
|
let images = state.vision.is_some();
|
|
let mut out = vec![mark(record(model, images))];
|
|
if !state.aliases.is_empty() {
|
|
out[0]["aliases"] = json!(*state.aliases);
|
|
}
|
|
for alias in state.aliases.iter() {
|
|
let mut value = mark(record(alias, images));
|
|
value["alias_of"] = json!(model);
|
|
out.push(value);
|
|
}
|
|
out
|
|
}
|
|
|
|
fn query_error(query: Option<String>) -> Option<Response> {
|
|
query.filter(|q| !q.is_empty()).map(|_| {
|
|
(
|
|
StatusCode::BAD_REQUEST,
|
|
Json(json!({"error":{
|
|
"type":"invalid_request_error","code":"unsupported_query","param":null,
|
|
"message":"model discovery does not support query parameters"
|
|
}})),
|
|
)
|
|
.into_response()
|
|
})
|
|
}
|
|
|
|
pub async fn list(State(state): State<AppState>, RawQuery(query): RawQuery) -> Response {
|
|
if let Some(error) = query_error(query) {
|
|
return error;
|
|
}
|
|
let data = records(&state).await;
|
|
Json(json!({"object":"list","data":data})).into_response()
|
|
}
|
|
|
|
pub async fn retrieve(
|
|
State(state): State<AppState>,
|
|
Path(id): Path<String>,
|
|
RawQuery(query): RawQuery,
|
|
) -> Response {
|
|
if let Some(error) = query_error(query) {
|
|
return error;
|
|
}
|
|
if let Some(found) = records(&state)
|
|
.await
|
|
.into_iter()
|
|
.find(|value| value["id"] == id.as_str())
|
|
{
|
|
return Json(found).into_response();
|
|
}
|
|
(
|
|
StatusCode::NOT_FOUND,
|
|
Json(json!({"error":{
|
|
"type":"invalid_request_error","code":"model_not_found","param":"model",
|
|
"message":"the requested model is not configured on this runtime"
|
|
}})),
|
|
)
|
|
.into_response()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[tokio::test]
|
|
async fn discovery_during_worker_outage_and_drain_never_schedules_generation() {
|
|
let (sender, mut receiver) = tokio::sync::mpsc::channel(1);
|
|
let state = AppState {
|
|
model: Some("org/model : v1".into()),
|
|
generation: Some(sender),
|
|
..Default::default()
|
|
};
|
|
state.lifecycle.set_draining(true);
|
|
state.worker.write().await.state = crate::worker::WorkerState::Restarting;
|
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
|
let url = format!("http://{}", listener.local_addr().unwrap());
|
|
let router = crate::app::router(state.clone());
|
|
let server = tokio::spawn(async move { axum::serve(listener, router).await });
|
|
let client = reqwest::Client::new();
|
|
for (path, expected) in [
|
|
(
|
|
"/v1/models",
|
|
json!({"object":"list","data":[{"id":"org/model : v1","object":"model","created":0,"owned_by":"local"}]}),
|
|
),
|
|
(
|
|
"/v1/models/org%2Fmodel%20%3A%20v1",
|
|
json!({"id":"org/model : v1","object":"model","created":0,"owned_by":"local"}),
|
|
),
|
|
] {
|
|
let response = client.get(format!("{url}{path}")).send().await.unwrap();
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
let body: Value = serde_json::from_str(&response.text().await.unwrap()).unwrap();
|
|
assert_eq!(body, expected);
|
|
}
|
|
for (path, code) in [
|
|
("/v1/models/unknown", 404),
|
|
("/v1/models?limit=1", 400),
|
|
("/v1/models/unknown?x=1", 400),
|
|
] {
|
|
let response = client.get(format!("{url}{path}")).send().await.unwrap();
|
|
assert_eq!(response.status().as_u16(), code);
|
|
}
|
|
assert!(receiver.try_recv().is_err());
|
|
assert_eq!(state.lifecycle.snapshot().requests_in_flight, 0);
|
|
assert_eq!(state.lifecycle.snapshot().generation_jobs, 0);
|
|
assert_eq!(
|
|
state.worker.read().await.state,
|
|
crate::worker::WorkerState::Restarting
|
|
);
|
|
server.abort();
|
|
let _ = server.await;
|
|
let response = list(State(AppState::default()), RawQuery(None)).await;
|
|
let body = axum::body::to_bytes(response.into_body(), 4096)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
serde_json::from_slice::<Value>(&body).unwrap(),
|
|
json!({"object":"list","data":[]})
|
|
);
|
|
}
|
|
}
|