Skip to main content

agentmux_srv/server/app_api/
agent_routes.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `Agents routes` (`POST /api/v1/agent/routes`): the model routes a caller may choose, one row per route with its
5//! models, prices and whether its key is in the Vault. Read-only, and open to every agent, container agents included
6//! (`docs/specs/SPEC_AGENT_MODEL_ROUTES_ANY_MODEL_SPAWN_AND_SWITCH_2026_11_06.md` section 5.1).
7
8use axum::extract::State;
9use axum::response::{IntoResponse, Response};
10use axum::Json;
11use serde::Deserialize;
12
13use crate::backend::model_gateway::ask;
14use crate::backend::vault::route_key;
15use crate::server::state::AppState;
16
17#[derive(Debug, Default, Deserialize)]
18pub(crate) struct RoutesRequest {
19    /// Only the routes that serve this harness; absent, all of them.
20    #[serde(default)]
21    harness: Option<String>,
22}
23
24pub(crate) async fn handle_agent_routes(State(state): State<AppState>, Json(req): Json<RoutesRequest>) -> Response {
25    let harness = req.harness.as_deref().map(str::trim).filter(|h| !h.is_empty());
26    let listing = ask::listing(harness, |route| route_key::present(&state.identity_store, route.key_label));
27    Json(listing).into_response()
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33    use crate::server::tests::test_state;
34
35    async fn listed(state: &AppState, body: serde_json::Value) -> serde_json::Value {
36        let resp = handle_agent_routes(State(state.clone()), Json(serde_json::from_value(body).unwrap())).await;
37        let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
38        serde_json::from_slice(&bytes).unwrap()
39    }
40
41    #[tokio::test]
42    async fn the_listing_says_whether_the_key_is_in_the_vault_and_filters_by_harness() {
43        let state = test_state();
44        crate::backend::vault::seal::use_test_key();
45        assert_eq!(listed(&state, serde_json::json!({})).await["routes"][0]["key_present"], false);
46        let entry: crate::backend::vault::NewEntry = serde_json::from_value(serde_json::json!({ "kind": "api-key", "label": "OpenRouter API key", "parts": { "value": "sk-or-test" } })).unwrap();
47        crate::backend::vault::create(&state.identity_store, entry).unwrap();
48        let all = listed(&state, serde_json::json!({})).await;
49        assert_eq!(all["routes"][0]["key_present"], true);
50        assert!(!serde_json::to_string(&all).unwrap().contains("sk-or-test"));
51        assert!(listed(&state, serde_json::json!({ "harness": "codex" })).await["routes"].as_array().unwrap().is_empty());
52    }
53}