Watch
0
0
Fork
You've already forked hyperhive
0

swarm-controller: GET /api/models lists the model names the swarm already knows

This commit is contained in:
iris 2026-11-22 13:40:34 +01:00
commit ef817ed5a0
2 changed files with 136 additions and 2 deletions

View file

@ -28,8 +28,8 @@ use axum::http::StatusCode;
use futures_util::StreamExt as _;
use problem_details::ProblemDetails;
use swarm_controller_api::inference::{
AgentInferenceView, EffectiveInference, InferenceSettings, MAX_SOFT_CONTEXT_PERCENT,
MAX_SUBAGENTS, MIN_SOFT_CONTEXT_PERCENT,
AgentInferenceView, EffectiveInference, InferenceSettings, KnownModels,
MAX_SOFT_CONTEXT_PERCENT, MAX_SUBAGENTS, MIN_SOFT_CONTEXT_PERCENT,
};
use swarm_queue_client::agent_inference;
use swarm_wire::agent_inference::{AgentInference, Effective};
@ -202,6 +202,16 @@ impl Reported {
}
}
/// The models the agents reported running on.
fn models(&self) -> Vec<String> {
self.latest
.lock()
.unwrap_or_else(PoisonError::into_inner)
.values()
.map(|e| e.model.clone())
.collect()
}
fn get(&self, agent: &str) -> Option<Effective> {
self.latest
.lock()
@ -267,6 +277,61 @@ pub(crate) async fn get_agent_inference(
}))
}
/// The distinct model names among `stored` settings and the `reported` ones,
/// sorted.
fn known_models<'a>(
stored: impl IntoIterator<Item = &'a AgentInference>,
reported: Vec<String>,
) -> Vec<String> {
let mut models: BTreeSet<String> = reported.into_iter().collect();
for settings in stored {
models.extend(settings.model.iter().cloned());
models.extend(settings.subagent_models.iter().flatten().cloned());
}
models.into_iter().filter(|m| !m.is_empty()).collect()
}
#[utoipa::path(
get,
path = "/api/models",
responses(
(status = 200, description = "the model names the swarm already knows: those in any \
agent's stored inference settings and those the agents reported running on",
body = KnownModels),
(status = 503, description = "no swarm queue is configured on this host, or it is \
not connected (problem+json)", body = String),
(status = 500, description = "the settings could not be read (problem+json)",
body = String),
),
tag = "agents"
)]
pub(crate) async fn get_models(
State(state): State<AppState>,
) -> Result<Json<KnownModels>, ProblemDetails> {
let client = queue(&state).map_err(refused)?;
let store = open(&client).await?;
let mut stored = Vec::new();
let mut keys = store
.keys()
.await
.map_err(|e| queue_problem(&anyhow::Error::new(e)))?;
while let Some(key) = keys.next().await {
let key = key.map_err(|e| queue_problem(&anyhow::Error::new(e)))?;
let (current, _, _) = read_or_default::<Option<AgentInference>>(&store, &key)
.await
.map_err(|e| queue_problem(&e))?;
stored.extend(current);
}
let reported = state
.reported_inference
.as_ref()
.map(|r| r.models())
.unwrap_or_default();
Ok(Json(KnownModels {
models: known_models(&stored, reported),
}))
}
#[utoipa::path(
put,
path = "/api/agents/{name}/inference",
@ -562,6 +627,26 @@ mod tests {
);
}
#[test]
fn known_models_merge_the_stored_and_the_reported_without_repeats() {
let stored = [
into_stored(
settings(r#"{"model":"opus","subagent_models":["haiku","sonnet"]}"#).unwrap(),
)
.unwrap(),
into_stored(settings(r#"{"effort":"high"}"#).unwrap()).unwrap(),
into_stored(settings(r#"{"model":"sonnet"}"#).unwrap()).unwrap(),
];
assert_eq!(
known_models(
&stored,
vec!["sonnet".to_owned(), "claude-opus-4-8".to_owned()]
),
["claude-opus-4-8", "haiku", "opus", "sonnet"]
);
assert!(known_models(&[], Vec::new()).is_empty());
}
#[test]
fn bad_model_names_are_refused() {
for json in [
@ -703,6 +788,37 @@ mod tests {
assert_eq!(&entry[..], b"{}");
}
/// The list reads every agent's key, counts an undecodable value as no
/// settings, and answers an empty list for an empty bucket.
#[tokio::test]
async fn the_models_list_reads_every_key_and_skips_an_undecodable_one() {
let nats = crate::test_nats::TestNats::start();
let client = nats.client().await;
let state = crate::agent_limits::tests::declared(&client).await;
let store = agent_inference::open_or_create(&client)
.await
.expect("bucket");
let none = get_models(State(state.clone())).await.expect("read");
assert!(none.0.models.is_empty());
for (agent, value) in [
(
"atlas",
&br#"{"model":"opus","subagent_models":["haiku"]}"#[..],
),
("iris", &br#"{"model":"sonnet"}"#[..]),
("argus", &br#"{"effort":"later"}"#[..]),
] {
store
.put(agent, value.to_vec().into())
.await
.expect("written");
}
let list = get_models(State(state)).await.expect("read");
assert_eq!(list.0.models, ["haiku", "opus", "sonnet"]);
}
/// `restore` puts back the settings a `store` replaced, or takes the key
/// out again when there was none.
#[tokio::test]

View file

@ -703,6 +703,7 @@ pub(crate) fn build_app(state: AppState) -> axum::Router {
.routes(routes!(config_prs::get_agent_config_pr))
.routes(routes!(agents::get_agent_icon))
.routes(routes!(agents_md::get_agents_md, agents_md::put_agents_md))
.routes(routes!(inference::get_models))
.routes(routes!(
inference::get_agent_inference,
inference::put_agent_inference,