Skip to main content

adk_kernel/control/
agents.rs

1//! The agents a person adds, read over the control plane (spec 3.3 §5.7, §10 K7).
2//!
3//! | Route (under `/api`) | Caller | Does |
4//! |---|---|---|
5//! | `GET /kernel/agents` | operator, or a device with `read` | Every agent with its state — suggested, active, paused, being removed, removed — but refused suggestions, with why this kernel cannot start one ([`AgentsResponse`]) |
6//! | `GET /kernel/agents/{id}` | operator, or a device with `read` | One agent, tombstones included ([`AgentView`]); `404` for an unknown id |
7//!
8//! No route creates, changes, pauses or removes an agent: changes go through the kernel's
9//! agent table from the web home's engine in the kernel's process, and from the terminal
10//! through the `/agent` command, which `adk-rust up` serves with the REPL's other commands.
11//! The read routes sit under `/api/kernel/agents` because `adk-server`'s agent registry
12//! already serves `/api/agents`.
13
14use 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/// `GET /kernel/agents` answer (spec 3.3 §10 K7).
28///
29/// # Example
30///
31/// ```rust
32/// use adk_kernel::control::api::AgentsResponse;
33///
34/// let answer: AgentsResponse = serde_json::from_value(serde_json::json!({ "agents": [] })).unwrap();
35/// assert!(answer.agents.is_empty());
36/// ```
37#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
38#[serde(rename_all = "camelCase")]
39pub struct AgentsResponse {
40    /// The agents with their states, removed ones included, ordered by id.
41    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}