Skip to main content

agentmux_srv/server/
work_act.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3//
4//! An agent's moves on the Kanban other than accept and send back
5//! (`work_delegate.rs`): edit, move, start, answer, cancel, retry, reopen,
6//! take back and reassign, with `Tasks`:
7//!
8//!   POST /agentmux/work/act   { agent_id, action, id, ... }
9//!
10//! The agent's token decides who acts. These are the same moves the Kanban
11//! makes for the operator (`app_api/kanban.rs`); the history carries the agent
12//! as the one who did it, and a message to another agent arrives as the
13//! acting agent's, never as the operator's. `target` names the agent a move
14//! is for (`agent` in the Kanban's own vocabulary is the asker here).
15
16use axum::extract::State;
17use axum::http::StatusCode;
18use axum::response::{IntoResponse, Response};
19use axum::Json;
20use serde_json::{json, Value};
21
22use super::app_api::kanban::{act_as, Actor};
23use super::caller::Caller;
24use super::AppState;
25
26pub(crate) async fn handle_work_act(
27    State(state): State<AppState>,
28    caller: Option<axum::Extension<Caller>>,
29    Json(mut body): Json<Value>,
30) -> Response {
31    let Some(uid) = caller.as_deref().and_then(Caller::uid).map(str::to_string) else {
32        return (StatusCode::FORBIDDEN, Json(json!({ "error": "FORBIDDEN: this acts for an agent, so the request must carry that agent's token" }))).into_response();
33    };
34    let claimed = body.get("agent_id").and_then(Value::as_str).unwrap_or_default().to_string();
35    let mstore = state.mstore.clone();
36    let proving = uid.clone();
37    let slug = tokio::task::spawn_blocking(move || super::actor::proven_slug(&mstore, &proving, &claimed)).await.ok().flatten();
38    let Some(slug) = slug else {
39        return (StatusCode::FORBIDDEN, Json(json!({ "error": "FORBIDDEN: the token's agent doesn't answer to that agent_id" }))).into_response();
40    };
41    if let Some(target) = body.get("target").cloned() {
42        body["agent"] = target;
43    }
44    match act_as(&state, body, &Actor::agent(&slug).with_uid(&uid)).await {
45        Ok(v) => Json(v).into_response(),
46        Err(e) => (StatusCode::CONFLICT, Json(json!({ "error": e }))).into_response(),
47    }
48}