agentmux_srv_core/backend/spawn_env/
workspaces.rs1use std::collections::HashMap;
9use std::path::PathBuf;
10use std::sync::{Mutex, OnceLock};
11
12fn launch_workspaces() -> &'static Mutex<HashMap<String, PathBuf>> {
13 static W: OnceLock<Mutex<HashMap<String, PathBuf>>> = OnceLock::new();
14 W.get_or_init(Default::default)
15}
16
17pub fn record_launch_workspace(block_id: &str, env_vars: &HashMap<String, String>) {
26 container_workspaces().lock().unwrap_or_else(|p| p.into_inner()).remove(block_id);
28 let mut map = launch_workspaces().lock().unwrap_or_else(|p| p.into_inner());
29 match env_vars.get("AGENTMUX_AGENT_WORKDIR").filter(|w| !w.trim().is_empty()) {
30 Some(w) => {
31 map.insert(block_id.to_string(), PathBuf::from(w));
32 }
33 None => {
34 map.remove(block_id);
35 }
36 }
37}
38
39pub fn agent_workspace(block_id: &str) -> Option<PathBuf> {
42 launch_workspaces().lock().unwrap_or_else(|p| p.into_inner()).get(block_id).cloned()
43}
44
45fn container_workspaces() -> &'static Mutex<HashMap<String, PathBuf>> {
46 static W: OnceLock<Mutex<HashMap<String, PathBuf>>> = OnceLock::new();
47 W.get_or_init(Default::default)
48}
49
50pub fn record_container_workspace(block_id: &str, host_dir: Option<&str>) {
55 launch_workspaces().lock().unwrap_or_else(|p| p.into_inner()).remove(block_id);
56 let mut map = container_workspaces().lock().unwrap_or_else(|p| p.into_inner());
57 match host_dir.map(str::trim).filter(|d| !d.is_empty()) {
58 Some(d) => {
59 map.insert(block_id.to_string(), PathBuf::from(d));
60 }
61 None => {
62 map.remove(block_id);
63 }
64 }
65}
66
67pub fn container_workspace(block_id: &str) -> Option<PathBuf> {
69 container_workspaces().lock().unwrap_or_else(|p| p.into_inner()).get(block_id).cloned()
70}