agentmux_srv/server/
work_act.rs1use 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}