Skip to main content

agentmux_srv_core/ambient/
cli_locator.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Where the ambient calls find a provider's CLI. The lookup behind `install.check` and the launch lives in the
5//! server layer (`server::cli_handlers::locate_provider_cli`), which registers it here once at startup
6//! ([`set_locator`]), so `ambient` does not name `server` (Area54 #1350). Before that registration nothing is found,
7//! which the callers treat as any other CLI that is not there.
8
9use std::future::Future;
10use std::pin::Pin;
11use std::sync::OnceLock;
12
13/// A lookup of the CLI a launch of `provider_id` would run: its path, or `None`.
14pub type Locator = for<'a> fn(&'a str) -> Pin<Box<dyn Future<Output = Option<String>> + Send + 'a>>;
15
16static LOCATOR: OnceLock<Locator> = OnceLock::new();
17
18/// Register the lookup. Later calls change nothing.
19pub fn set_locator(f: Locator) {
20    let _ = LOCATOR.set(f);
21}
22
23/// The CLI path for `provider_id`, or `None` when none is installed or no lookup is registered yet.
24pub async fn locate(provider_id: &str) -> Option<String> {
25    match LOCATOR.get() {
26        Some(f) => f(provider_id).await,
27        None => None,
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    fn found(id: &str) -> Pin<Box<dyn Future<Output = Option<String>> + Send + '_>> {
36        Box::pin(async move { (id == "known").then(|| "/bin/known".to_string()) })
37    }
38
39    #[tokio::test]
40    async fn it_asks_the_registered_lookup_and_finds_nothing_without_one() {
41        // Whether or not another test registered one first, an unknown provider is not found.
42        assert_eq!(locate("nope").await, None);
43        set_locator(found);
44        assert_eq!(locate("known").await.as_deref(), Some("/bin/known"));
45        assert_eq!(locate("nope").await, None);
46    }
47}