agentmux_mcp/tools/
app_workspace.rs1use super::str_arg;
9use super::*;
10
11const MAX_MINUTES: u64 = 240;
13
14pub(super) async fn workspace(cx: &ToolCtx<'_>) -> Result<String> {
16 let url = format!("{}/api/v1/app/workspace", cx.local_url.trim_end_matches('/'));
17 let resp = cx.client.get(&url).header(AUTH_KEY_HEADER, cx.auth_key).send().await.map_err(|e| anyhow::anyhow!("request failed: {e}"))?;
18 let status = resp.status();
19 let body: Value = resp.json().await.unwrap_or(Value::Null);
20 if !status.is_success() {
21 anyhow::bail!("App workspace: {}", body["error"].as_str().map(str::to_string).unwrap_or_else(|| format!("srv answered {status}: {body}")));
22 }
23 Ok(describe(&body, agentmux_common::time::now_ms()))
24}
25
26pub(super) async fn keep_running(arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
28 require_agent_env(cx.local_url, cx.auth_key, cx.block_id)?;
29 let Some(minutes) = arguments.get("minutes").and_then(Value::as_u64).filter(|m| (1..=MAX_MINUTES).contains(m)) else {
30 anyhow::bail!("App keep_running: minutes is a whole number from 1 to {MAX_MINUTES}{}", str_arg(arguments, "minutes").map(|m| format!(", not {m:?}")).unwrap_or_default());
31 };
32 let url = format!("{}/api/v1/agent/keep-running", cx.local_url.trim_end_matches('/'));
33 let resp = cx
34 .client
35 .post(&url)
36 .header(AUTH_KEY_HEADER, cx.auth_key)
37 .json(&json!({ "minutes": minutes }))
38 .send()
39 .await
40 .map_err(|e| anyhow::anyhow!("request failed: {e}"))?;
41 let status = resp.status();
42 let body: Value = resp.json().await.unwrap_or(Value::Null);
43 if !status.is_success() {
44 anyhow::bail!("App keep_running: {}", body["error"].as_str().map(str::to_string).unwrap_or_else(|| format!("srv answered {status}: {body}")));
45 }
46 Ok(format!(
47 "Asked for the stop to be put back by {minutes} minutes, recorded as {}. {}",
48 body["by"].as_str().unwrap_or("you"),
49 body["note"].as_str().unwrap_or("The workspace's timer decides whether to honour it.")
50 ))
51}
52
53pub(super) fn describe(body: &Value, now_ms: i64) -> String {
56 let asked = body["keep_running"].as_object().map(|k| {
57 format!(
58 " {} asked for {} more minutes{}.",
59 k.get("by").and_then(Value::as_str).unwrap_or("an agent"),
60 k.get("minutes").and_then(Value::as_u64).unwrap_or(0),
61 if body["keep_running"]["at_ms"].is_u64() { " (waiting for the timer to answer)" } else { "" }
62 )
63 });
64 if body["stopping"] != Value::Bool(true) {
65 return "No stop is pending for this workspace.".to_string();
66 }
67 let at = body["stop_at_ms"].as_i64().unwrap_or(0);
68 let secs = at / 1000;
69 let clock = format!("{:02}:{:02} UTC", (secs / 3600) % 24, (secs / 60) % 60);
70 let left = ((at - now_ms) / 60_000).max(0);
71 let why = body["reason"].as_str().unwrap_or("");
72 let mut out = format!("This workspace stops at {clock} (in {left} minutes) because {why}.");
73 if let Some(a) = asked {
74 out.push_str(&a);
75 }
76 out
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82
83 #[test]
84 fn no_stop_says_so() {
85 assert_eq!(describe(&json!({ "stopping": false, "keep_running": null }), 0), "No stop is pending for this workspace.");
86 }
87
88 #[test]
89 fn a_stop_says_when_and_why_and_what_was_asked() {
90 let now = 1_800_000_000_000i64;
92 let stop = json!({ "stopping": true, "stop_at_ms": now + 12 * 60_000, "reason_id": "timer", "reason": "nothing has happened for 50 minutes", "keep_running": null });
93 assert_eq!(describe(&stop, now), "This workspace stops at 08:12 UTC (in 12 minutes) because nothing has happened for 50 minutes.");
94 let asked = json!({ "stopping": true, "stop_at_ms": now + 12 * 60_000, "reason_id": "person", "reason": "you asked", "keep_running": { "minutes": 30, "by": "agent:agenta", "at_ms": now } });
95 assert!(describe(&asked, now).ends_with("agent:agenta asked for 30 more minutes (waiting for the timer to answer)."));
96 }
97
98 #[test]
99 fn a_stop_in_the_past_does_not_read_as_negative_minutes() {
100 let now = 1_800_000_000_000i64;
101 let late = json!({ "stopping": true, "stop_at_ms": now - 5 * 60_000, "reason": "your credit ran out" });
102 assert!(describe(&late, now).contains("(in 0 minutes)"));
103 }
104}