agentmux_launcher/
launch_link.rs1use std::sync::{Mutex, MutexGuard};
22
23pub(crate) const OPEN_LINK_ENV: &str = "AGENTMUX_OPEN_LINK";
25
26const MAX_LINK: usize = 4096;
28
29#[derive(Debug, Default, PartialEq)]
32struct Launch {
33 link: Option<String>,
34 host_started: bool,
35}
36
37impl Launch {
38 fn started_with(&mut self, link: Option<String>) {
40 self.link = self.link.take().or(link);
41 }
42
43 fn for_host(&mut self) -> Option<String> {
45 let first = !std::mem::replace(&mut self.host_started, true);
46 self.link.clone().filter(|_| first)
47 }
48
49 fn handed_over(&mut self, link: String) -> Option<String> {
52 if self.host_started || self.link.is_some() {
53 return Some(link);
54 }
55 self.link = Some(link);
56 None
57 }
58}
59
60static LAUNCH: Mutex<Launch> = Mutex::new(Launch { link: None, host_started: false });
61
62fn launch() -> MutexGuard<'static, Launch> {
63 LAUNCH.lock().unwrap_or_else(|e| e.into_inner())
64}
65
66pub(crate) fn is_link(arg: &str) -> bool {
68 arg.get(..11).is_some_and(|s| s.eq_ignore_ascii_case("agentmux://")) && arg.len() <= MAX_LINK && !arg.chars().any(char::is_control)
69}
70
71const SEPARATOR: &str = "--";
73
74pub(crate) fn own_args() -> Vec<String> {
76 before_separator(std::env::args().collect())
77}
78
79fn before_separator(args: Vec<String>) -> Vec<String> {
80 args.into_iter().take_while(|a| a != SEPARATOR).collect()
81}
82
83pub(crate) fn split(args: Vec<String>) -> (Vec<String>, Option<String>) {
86 let link = args.iter().find(|a| is_link(a)).cloned();
87 (before_separator(args).into_iter().filter(|a| !is_link(a)).collect(), link)
88}
89
90pub(crate) fn take(args: Vec<String>) -> Vec<String> {
93 let (rest, link) = split(args);
94 launch().started_with(link);
95 rest
96}
97
98pub(crate) fn link() -> Option<String> {
100 launch().link.clone()
101}
102
103pub(crate) fn for_host() -> Option<String> {
105 launch().for_host()
106}
107
108#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
111pub(crate) fn from_os(link: &str) -> Option<String> {
112 let link = link.trim();
113 if !is_link(link) {
114 return None;
115 }
116 launch().handed_over(link.to_string())
117}
118
119pub(crate) fn set_host_env(cmd: &mut tokio::process::Command) {
121 match for_host() {
122 Some(link) => cmd.env(OPEN_LINK_ENV, link),
123 None => cmd.env_remove(OPEN_LINK_ENV),
124 };
125}
126
127#[cfg(test)]
128mod tests {
129 use super::*;
130
131 fn args(list: &[&str]) -> Vec<String> {
132 list.iter().map(|s| s.to_string()).collect()
133 }
134
135 #[test]
136 fn the_link_is_taken_out_of_the_hosts_arguments() {
137 let (rest, link) = split(args(&["--background", "agentmux://settings/recording", "--x"]));
138 assert_eq!(rest, args(&["--background", "--x"]));
139 assert_eq!(link.as_deref(), Some("agentmux://settings/recording"));
140 let (rest, link) = split(args(&["--x"]));
141 assert_eq!((rest, link), (args(&["--x"]), None));
142 }
143
144 #[test]
145 fn nothing_after_the_separator_is_an_argument_but_the_link() {
146 let (rest, link) = split(args(&["--background", "--", "agentmux://help", "--verify-update", "1.0.0", "C:/x", "f"]));
148 assert_eq!(rest, args(&["--background"]));
149 assert_eq!(link.as_deref(), Some("agentmux://help"));
150 let (rest, link) = split(args(&["--", "--verify-update", "1.0.0"]));
151 assert_eq!((rest, link), (vec![], None));
152 assert_eq!(before_separator(args(&["agentmux", "--", "agentmux://x", "--background"])), args(&["agentmux"]));
153 }
154
155 #[test]
156 fn a_link_the_os_hands_over_goes_to_the_first_host_or_is_forwarded() {
157 let mut l = Launch::default();
159 assert_eq!(l.handed_over("agentmux://help".into()), None);
160 l.started_with(None);
161 assert_eq!(l.for_host().as_deref(), Some("agentmux://help"));
162 assert_eq!(l.for_host(), None, "a host the launcher restarts doesn't open it again");
163 assert_eq!(l.handed_over("agentmux://kanban".into()).as_deref(), Some("agentmux://kanban"));
165 let mut l = Launch::default();
167 l.started_with(Some("agentmux://help".into()));
168 assert_eq!(l.handed_over("agentmux://kanban".into()).as_deref(), Some("agentmux://kanban"));
169 assert_eq!(l.for_host().as_deref(), Some("agentmux://help"));
170 let mut l = Launch::default();
172 l.handed_over("agentmux://help".into());
173 l.started_with(Some("agentmux://kanban".into()));
174 assert_eq!(l.link.as_deref(), Some("agentmux://help"));
175 }
176
177 #[test]
178 fn only_an_agentmux_link_is_one() {
179 assert!(is_link("agentmux://help"));
180 assert!(is_link("AGENTMUX://help"));
181 for a in ["https://example.com", "agentmux:/help", "agentmux", "", "agentmux://a\nb"] {
182 assert!(!is_link(a), "{a:?}");
183 }
184 assert!(!is_link(&format!("agentmux://help#{}", "a".repeat(MAX_LINK))));
185 }
186}