Skip to main content

a3s_box_sdk/client/
config.rs

1/// Runtime-backed SDK client for local a3s-box state.
2#[derive(Clone)]
3pub struct A3sBoxClient {
4    paths: A3sBoxPaths,
5    image_cache_size: u64,
6    execution_manager: Arc<dyn ExecutionManager>,
7    execution_session_manager: Option<Arc<dyn ExecutionSessionManager>>,
8}
9
10impl std::fmt::Debug for A3sBoxClient {
11    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12        formatter
13            .debug_struct("A3sBoxClient")
14            .field("paths", &self.paths)
15            .field("image_cache_size", &self.image_cache_size)
16            .finish_non_exhaustive()
17    }
18}
19
20impl A3sBoxClient {
21    /// Create a client for the default a3s-box home.
22    pub fn new() -> Self {
23        Self::default()
24    }
25
26    /// Create a client rooted at a custom a3s-box home directory.
27    pub fn from_home(home: impl Into<PathBuf>) -> Self {
28        Self::with_paths(A3sBoxPaths::from_home(home))
29    }
30
31    /// Create a client using explicit state paths.
32    pub fn with_paths(paths: A3sBoxPaths) -> Self {
33        let local_execution_manager = Arc::new(
34            a3s_box_runtime::LocalExecutionManager::with_vm_backend(
35                paths.boxes_file.clone(),
36                paths.home.clone(),
37            ),
38        );
39        Self {
40            paths,
41            image_cache_size: a3s_box_runtime::DEFAULT_IMAGE_CACHE_SIZE,
42            execution_manager: local_execution_manager.clone(),
43            execution_session_manager: Some(local_execution_manager),
44        }
45    }
46
47    /// Create a client with an explicit backend-neutral execution manager.
48    ///
49    /// This keeps lifecycle calls on the same canonical facade used by the CLI
50    /// and remote compatibility service while allowing an embedding application
51    /// to inject its own manager implementation.
52    pub fn with_execution_manager(
53        paths: A3sBoxPaths,
54        execution_manager: Arc<dyn ExecutionManager>,
55    ) -> Self {
56        Self {
57            paths,
58            image_cache_size: a3s_box_runtime::DEFAULT_IMAGE_CACHE_SIZE,
59            execution_manager,
60            execution_session_manager: None,
61        }
62    }
63
64    /// Create a client with explicit typed lifecycle and session managers.
65    ///
66    /// This is useful for embedding the E2B-style [`crate::Sandbox`] facade in
67    /// another local process without selecting backends by string.
68    pub fn with_execution_services(
69        paths: A3sBoxPaths,
70        execution_manager: Arc<dyn ExecutionManager>,
71        execution_session_manager: Arc<dyn ExecutionSessionManager>,
72    ) -> Self {
73        Self {
74            paths,
75            image_cache_size: a3s_box_runtime::DEFAULT_IMAGE_CACHE_SIZE,
76            execution_manager,
77            execution_session_manager: Some(execution_session_manager),
78        }
79    }
80
81    /// Override the image cache size used when opening the runtime image store.
82    pub fn with_image_cache_size(mut self, image_cache_size: u64) -> Self {
83        self.image_cache_size = image_cache_size;
84        self
85    }
86
87    /// Return the state paths used by this client.
88    pub fn paths(&self) -> &A3sBoxPaths {
89        &self.paths
90    }
91
92    /// Collect local runtime diagnostics without spawning the CLI.
93    pub fn runtime_diagnostics(&self) -> RuntimeDiagnostics {
94        RuntimeDiagnostics::collect(&self.paths)
95    }
96
97    /// Collect local runtime disk usage without spawning the CLI.
98    pub fn runtime_disk_usage(&self) -> Result<RuntimeDiskUsage> {
99        RuntimeDiskUsage::collect(&self.paths)
100    }
101}
102
103impl Default for A3sBoxClient {
104    fn default() -> Self {
105        Self::with_paths(A3sBoxPaths::default())
106    }
107}