adk_kernel/control/
agents.rs1use std::sync::Arc;
15
16use axum::Json;
17use axum::Router;
18use axum::extract::{Path as UrlPath, State};
19use axum::routing::get;
20use serde::{Deserialize, Serialize};
21
22use super::paths;
23use super::routes::{Caller, ControlBackend};
24use crate::agents::AgentView;
25use crate::error::{AGENT_NOT_FOUND_CODE, KernelError, Result};
26
27#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
38#[serde(rename_all = "camelCase")]
39pub struct AgentsResponse {
40 pub agents: Vec<AgentView>,
42}
43
44type Backend = State<Arc<dyn ControlBackend>>;
45
46pub(super) fn routes() -> Router<Arc<dyn ControlBackend>> {
47 Router::new().route(paths::AGENTS, get(list)).route(paths::AGENT, get(one))
48}
49
50fn table(backend: &dyn ControlBackend) -> Result<&crate::agents::Agents> {
51 backend.kernel().map(crate::Kernel::agents).ok_or_else(|| KernelError::Config {
52 reason: "this control plane serves no kernel, so it keeps no agents".into(),
53 })
54}
55
56async fn list(State(backend): Backend, caller: Caller) -> Result<Json<AgentsResponse>> {
57 caller.require_operator("read the agents")?;
58 let agents = table(backend.as_ref())?.list().await?;
59 Ok(Json(AgentsResponse { agents }))
60}
61
62async fn one(
63 State(backend): Backend,
64 caller: Caller,
65 UrlPath(id): UrlPath<String>,
66) -> Result<Json<AgentView>> {
67 caller.require_operator("read an agent")?;
68 match table(backend.as_ref())?.get(&id).await? {
69 Some(view) => Ok(Json(view)),
70 None => Err(KernelError::Core(adk_core::AdkError::new(
71 adk_core::ErrorComponent::Server,
72 adk_core::ErrorCategory::NotFound,
73 AGENT_NOT_FOUND_CODE,
74 format!("no agent `{id}`; list them with `adk-rust agents`"),
75 ))),
76 }
77}