agentmux_srv/server/
work_remote.rs1use serde::de::DeserializeOwned;
21use serde::Deserialize;
22use serde_json::{json, Value};
23
24use super::work_queue::{enqueue_core, list_items, EnqueueRequest, Filer};
25use super::AppState;
26
27#[derive(Debug, PartialEq, Eq)]
29pub(crate) struct Failure {
30 pub code: &'static str,
31 pub message: String,
32}
33
34fn fail(code: &'static str, message: impl Into<String>) -> Failure {
35 Failure { code, message: message.into() }
36}
37
38fn params_of<T: DeserializeOwned>(params: Value) -> Result<T, Failure> {
40 serde_json::from_value(params).map_err(|e| fail("bad_params", e.to_string()))
41}
42
43#[derive(Debug, Deserialize)]
45#[serde(deny_unknown_fields)]
46struct EnqueueParams {
47 title: String,
48 payload: String,
49 #[serde(default)]
50 kind: String,
51 #[serde(default)]
53 target_agent: String,
54 #[serde(default)]
55 priority: i64,
56 #[serde(default)]
57 max_attempts: Option<i64>,
58 #[serde(default)]
59 not_before: Option<i64>,
60 #[serde(default)]
61 repo: Option<String>,
62 #[serde(default)]
63 hold: bool,
64 #[serde(default)]
65 review: Option<bool>,
66 #[serde(default)]
68 after: Vec<Value>,
69 #[serde(default)]
70 first: Option<String>,
71 #[serde(default)]
72 ui_change: Option<bool>,
73 #[serde(default)]
75 board: Option<String>,
76}
77
78async fn target_uid(state: &AppState, typed: &str) -> Result<String, Failure> {
80 use crate::backend::name_resolution::NameResolution;
81 let resolved = super::name_resolution::resolve_typed_name(state, typed).await.map_err(|e| fail("failed", e))?;
83 match resolved {
84 NameResolution::One { uid, .. } => Ok(uid),
85 NameResolution::Ambiguous { candidates } => {
86 let names: Vec<String> = candidates.iter().map(|c| if c.slug.is_empty() { c.name.clone() } else { c.slug.clone() }).collect();
87 Err(fail("ambiguous_target", format!("\"{typed}\" names more than one agent here: {}. Name one by its slug", names.join(", "))))
88 }
89 NameResolution::Unidentified { .. } | NameResolution::None => Err(fail("no_such_agent", format!("no agent named \"{typed}\" on this machine"))),
91 }
92}
93
94pub(crate) async fn tasks_enqueue(state: &AppState, actor: &str, params: Value) -> Result<Value, Failure> {
98 let p: EnqueueParams = params_of(params)?;
99 let target = p.target_agent.trim().to_string();
100 let target_agent_uid = if target.is_empty() { String::new() } else { target_uid(state, &target).await? };
101 let req = EnqueueRequest {
102 title: p.title,
103 payload: p.payload,
104 kind: p.kind,
105 target_agent: target,
106 target_agent_uid,
107 target_group: String::new(),
108 priority: p.priority,
109 created_by: actor.to_string(),
110 max_attempts: p.max_attempts,
111 not_before: p.not_before,
112 repo: p.repo,
113 hold: p.hold,
114 review: p.review,
115 after: p.after,
116 first: p.first,
117 ui_change: p.ui_change,
118 board: p.board,
119 };
120 let (status, axum::response::Json(body)) = enqueue_core(state, req, Filer::Remote(actor)).await;
121 if status.is_success() {
122 return Ok(body);
123 }
124 let why = body.get("error").and_then(Value::as_str).unwrap_or("the task was not filed").to_string();
125 Err(if status == axum::http::StatusCode::BAD_REQUEST { fail("bad_params", why) } else { fail("failed", why) })
126}
127
128#[derive(Debug, Default, Deserialize)]
130#[serde(deny_unknown_fields)]
131struct ListParams {
132 #[serde(default)]
134 state: String,
135 #[serde(default)]
136 limit: Option<usize>,
137 #[serde(default)]
139 board: Option<String>,
140}
141
142pub(crate) fn tasks_list(state: &AppState, params: Value) -> Result<Value, Failure> {
144 tasks_list_for(state, None, params)
145}
146
147pub(crate) fn tasks_list_for(state: &AppState, actor: Option<&str>, params: Value) -> Result<Value, Failure> {
150 let p: ListParams = if params.is_null() { ListParams::default() } else { params_of(params)? };
152 let who = actor.map(|a| (a, ""));
153 let items = list_items(state, &p.state, p.limit, p.board.as_deref(), who)
154 .map_err(|e| fail(if e.starts_with("no board") { "bad_params" } else { "failed" }, e))?;
155 Ok(json!({ "items": items }))
156}
157
158#[cfg(test)]
159#[path = "work_remote_tests.rs"]
160mod tests;