Skip to main content

agent_hub_runtime/core/task_browser/
cli.rs

1use super::{Binding, CliReader, TaskFailure};
2use serde_json::Value;
3use sha2::{Digest, Sha256};
4use std::{
5    path::PathBuf,
6    sync::atomic::{AtomicBool, Ordering},
7};
8
9pub struct SystemCli {
10    pub cwd: PathBuf,
11    pub proxy_url: Option<String>,
12}
13impl CliReader for SystemCli {
14    fn identity_scope(&self, binding: &Binding) -> Option<String> {
15        // Project launchers can choose accounts by cwd; standard CLIs share their global login.
16        #[cfg(feature = "gitlab")]
17        let detected = if binding.provider == super::Provider::Gitlab && binding.launcher.is_none()
18        {
19            crate::core::platforms::gitlab::cli::GlabCli::default()
20                .local_launcher()
21                .ok()?
22        } else {
23            None
24        };
25        #[cfg(not(feature = "gitlab"))]
26        let detected: Option<super::CliLauncher> = None;
27        let scope = serde_json::to_vec(&(
28            binding.provider,
29            binding.host.to_ascii_lowercase(),
30            &binding.launcher,
31            detected,
32            binding.launcher.as_ref().map(|_| &self.cwd),
33        ))
34        .ok()?;
35        Some(format!("{:x}", Sha256::digest(scope)))
36    }
37
38    fn read(
39        &self,
40        binding: &Binding,
41        args: &[String],
42        cancel: &AtomicBool,
43    ) -> std::result::Result<Value, TaskFailure> {
44        if cancel.load(Ordering::Acquire) {
45            return Err(TaskFailure::new("cancelled", "Query cancelled"));
46        }
47        let text = crate::core::platforms::cli::PlatformCli {
48            provider: binding.provider,
49            host: &binding.host,
50            launcher: binding.launcher.as_ref(),
51            cwd: &self.cwd,
52            proxy_url: self.proxy_url.as_deref(),
53        }
54        .run(args, cancel)?;
55        // GitLab returns HTTP 204 with an empty body after deleting a runner.
56        if args.get(2).is_some_and(|method| method == "DELETE") && text.trim().is_empty() {
57            return Ok(Value::Null);
58        }
59        serde_json::from_str(&text)
60            .map_err(|_| TaskFailure::new("invalid_response", "Platform CLI returned invalid JSON"))
61    }
62}