Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cua-driver): build client-kind SDK constructors without rebuilding the driver (#5896)

injaneity7e6ee3aab1b8
diff --git a/libs/cua-driver/rust/crates/cua-driver-sdk/src/lib.rs b/libs/cua-driver/rust/crates/cua-driver-sdk/src/lib.rs
index 121e841ab9..db9010199f 100644
--- a/libs/cua-driver/rust/crates/cua-driver-sdk/src/lib.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-sdk/src/lib.rs
@@ -813,18 +813,7 @@ impl CuaDriver {
/// `cua-driver` and never opens daemon IPC.
#[uniffi::constructor]
pub fn create(options: Option<DriverOptions>) -> Result<Arc<Self>, DriverError> {
- cua_driver_core::authorization::validate_startup_authorization().map_err(|error| {
- DriverError::Configuration {
- reason: format!("authorization configuration is invalid: {error}"),
- }
- })?;
- let options = options.unwrap_or_default();
- Ok(Arc::new(Self {
- backend: DriverBackend::Embedded(Arc::new(NativeAbiDriver::create(
- options.claude_code_compatibility,
- )?)),
- client_kind: DaemonClientKind::Unknown,
- }))
+ create_for_client(options, DaemonClientKind::Unknown)
}
/// Create a same-process runtime with an explicit immutable authorization
@@ -832,13 +821,7 @@ impl CuaDriver {
/// agent tool. Existing `create()` callers remain unchanged.
#[uniffi::constructor]
pub fn create_configured(options: ConfiguredDriverOptions) -> Result<Arc<Self>, DriverError> {
- let native_options = configured_driver_options_json(&options);
- Ok(Arc::new(Self {
- backend: DriverBackend::Embedded(Arc::new(NativeAbiDriver::create_configured(
- native_options,
- )?)),
- client_kind: DaemonClientKind::Unknown,
- }))
+ create_configured_for_client(options, DaemonClientKind::Unknown)
}
/// Create a configured same-process runtime with an optional residual
@@ -852,17 +835,11 @@ impl CuaDriver {
options: ConfiguredDriverOptions,
host: Arc<dyn DriverAuthorizationHost>,
) -> Result<Arc<Self>, DriverError> {
- let provider = authorization_host::SdkDriverAuthorizationHost::new(host);
- Ok(Arc::new(Self {
- backend: DriverBackend::Embedded(Arc::new(
- NativeAbiDriver::create_configured_with_authorization_provider(
- configured_driver_options_json(&options),
- provider,
- None,
- )?,
- )),
- client_kind: DaemonClientKind::Unknown,
- }))
+ create_configured_with_authorization_host_for_client(
+ options,
+ host,
+ DaemonClientKind::Unknown,
+ )
}
/// Create a configured same-process runtime with a content-free activity
@@ -872,15 +849,11 @@ impl CuaDriver {
options: ConfiguredDriverOptions,
observer: Arc<dyn DriverActivityObserver>,
) -> Result<Arc<Self>, DriverError> {
- Ok(Arc::new(Self {
- backend: DriverBackend::Embedded(Arc::new(
- NativeAbiDriver::create_configured_with_activity_observer(
- configured_driver_options_json(&options),
- observer,
- )?,
- )),
- client_kind: DaemonClientKind::Unknown,
- }))
+ create_configured_with_activity_observer_for_client(
+ options,
+ observer,
+ DaemonClientKind::Unknown,
+ )
}
/// Create a configured same-process runtime with both residual
@@ -891,17 +864,12 @@ impl CuaDriver {
host: Arc<dyn DriverAuthorizationHost>,
observer: Arc<dyn DriverActivityObserver>,
) -> Result<Arc<Self>, DriverError> {
- let provider = authorization_host::SdkDriverAuthorizationHost::new(host);
- Ok(Arc::new(Self {
- backend: DriverBackend::Embedded(Arc::new(
- NativeAbiDriver::create_configured_with_authorization_provider(
- configured_driver_options_json(&options),
- provider,
- Some(observer),
- )?,
- )),
- client_kind: DaemonClientKind::Unknown,
- }))
+ create_configured_with_host_integrations_for_client(
+ options,
+ host,
+ observer,
+ DaemonClientKind::Unknown,
+ )
}
/// Language-package entry point for a configured protected-host runtime.
@@ -911,14 +879,7 @@ impl CuaDriver {
host: Arc<dyn DriverAuthorizationHost>,
client_kind: SdkClientKind,
) -> Result<Arc<Self>, DriverError> {
- let driver = Self::create_configured_with_authorization_host(options, host)?;
- let DriverBackend::Embedded(runtime) = &driver.backend else {
- unreachable!("protected-host constructor always returns an embedded runtime")
- };
- Ok(Arc::new(Self {
- backend: DriverBackend::Embedded(runtime.clone()),
- client_kind: client_kind.into(),
- }))
+ create_configured_with_authorization_host_for_client(options, host, client_kind.into())
}
/// Language-package entry point for a configured activity-observer runtime.
@@ -928,14 +889,7 @@ impl CuaDriver {
observer: Arc<dyn DriverActivityObserver>,
client_kind: SdkClientKind,
) -> Result<Arc<Self>, DriverError> {
- let driver = Self::create_configured_with_activity_observer(options, observer)?;
- let DriverBackend::Embedded(runtime) = &driver.backend else {
- unreachable!("activity-observer constructor always returns an embedded runtime")
- };
- Ok(Arc::new(Self {
- backend: DriverBackend::Embedded(runtime.clone()),
- client_kind: client_kind.into(),
- }))
+ create_configured_with_activity_observer_for_client(options, observer, client_kind.into())
}
/// Language-package entry point for both trusted host callbacks.
@@ -946,14 +900,12 @@ impl CuaDriver {
observer: Arc<dyn DriverActivityObserver>,
client_kind: SdkClientKind,
) -> Result<Arc<Self>, DriverError> {
- let driver = Self::create_configured_with_host_integrations(options, host, observer)?;
- let DriverBackend::Embedded(runtime) = &driver.backend else {
- unreachable!("host-integration constructor always returns an embedded runtime")
- };
- Ok(Arc::new(Self {
- backend: DriverBackend::Embedded(runtime.clone()),
- client_kind: client_kind.into(),
- }))
+ create_configured_with_host_integrations_for_client(
+ options,
+ host,
+ observer,
+ client_kind.into(),
+ )
}
/// Language-package entry point for an explicitly configured runtime.
@@ -962,14 +914,7 @@ impl CuaDriver {
options: ConfiguredDriverOptions,
client_kind: SdkClientKind,
) -> Result<Arc<Self>, DriverError> {
- let driver = Self::create_configured(options)?;
- let DriverBackend::Embedded(runtime) = &driver.backend else {
- unreachable!("create_configured always returns an embedded runtime")
- };
- Ok(Arc::new(Self {
- backend: DriverBackend::Embedded(runtime.clone()),
- client_kind: client_kind.into(),
- }))
+ create_configured_for_client(options, client_kind.into())
}
/// Language-package entry point for the same-process runtime. The wrapper
@@ -979,30 +924,14 @@ impl CuaDriver {
options: Option<DriverOptions>,
client_kind: SdkClientKind,
) -> Result<Arc<Self>, DriverError> {
- let driver = Self::create(options)?;
- let DriverBackend::Embedded(runtime) = &driver.backend else {
- unreachable!("create always returns an embedded runtime")
- };
- Ok(Arc::new(Self {
- backend: DriverBackend::Embedded(runtime.clone()),
- client_kind: client_kind.into(),
- }))
+ create_for_client(options, client_kind.into())
}
/// Connect to the default installed daemon or an explicitly selected socket.
/// This is the temporary compatibility path for released socket clients.
#[uniffi::constructor]
pub fn connect(socket_path: Option<String>) -> Result<Arc<Self>, DriverError> {
- let socket_path = socket_path.unwrap_or_else(|| socket_path_for_namespace("cua-driver"));
- if socket_path.trim().is_empty() {
- return Err(DriverError::Configuration {
- reason: "socket_path must not be empty".into(),
- });
- }
- Ok(Arc::new(Self {
- backend: DriverBackend::Daemon(DaemonBackend::new(socket_path)),
- client_kind: DaemonClientKind::Unknown,
- }))
+ connect_for_client(socket_path, DaemonClientKind::Unknown)
}
/// Language-package entry point that preserves the public `connect` API
@@ -1012,14 +941,7 @@ impl CuaDriver {
socket_path: Option<String>,
client_kind: SdkClientKind,
) -> Result<Arc<Self>, DriverError> {
- let driver = Self::connect(socket_path)?;
- let DriverBackend::Daemon(daemon) = &driver.backend else {
- unreachable!("connect always returns a daemon client")
- };
- Ok(Arc::new(Self {
- backend: DriverBackend::Daemon(daemon.clone()),
- client_kind: client_kind.into(),
- }))
+ connect_for_client(socket_path, client_kind.into())
}
/// Create a process-isolated runtime owned by this SDK object.
@@ -1072,6 +994,106 @@ impl CuaDriver {
}
}
+fn create_for_client(
+ options: Option<DriverOptions>,
+ client_kind: DaemonClientKind,
+) -> Result<Arc<CuaDriver>, DriverError> {
+ cua_driver_core::authorization::validate_startup_authorization().map_err(|error| {
+ DriverError::Configuration {
+ reason: format!("authorization configuration is invalid: {error}"),
+ }
+ })?;
+ let options = options.unwrap_or_default();
+ Ok(Arc::new(CuaDriver {
+ backend: DriverBackend::Embedded(Arc::new(NativeAbiDriver::create(
+ options.claude_code_compatibility,
+ )?)),
+ client_kind,
+ }))
+}
+
+fn create_configured_for_client(
+ options: ConfiguredDriverOptions,
+ client_kind: DaemonClientKind,
+) -> Result<Arc<CuaDriver>, DriverError> {
+ let native_options = configured_driver_options_json(&options);
+ Ok(Arc::new(CuaDriver {
+ backend: DriverBackend::Embedded(Arc::new(NativeAbiDriver::create_configured(
+ native_options,
+ )?)),
+ client_kind,
+ }))
+}
+
+fn create_configured_with_authorization_host_for_client(
+ options: ConfiguredDriverOptions,
+ host: Arc<dyn DriverAuthorizationHost>,
+ client_kind: DaemonClientKind,
+) -> Result<Arc<CuaDriver>, DriverError> {
+ let provider = authorization_host::SdkDriverAuthorizationHost::new(host);
+ Ok(Arc::new(CuaDriver {
+ backend: DriverBackend::Embedded(Arc::new(
+ NativeAbiDriver::create_configured_with_authorization_provider(
+ configured_driver_options_json(&options),
+ provider,
+ None,
+ )?,
+ )),
+ client_kind,
+ }))
+}
+
+fn create_configured_with_activity_observer_for_client(
+ options: ConfiguredDriverOptions,
+ observer: Arc<dyn DriverActivityObserver>,
+ client_kind: DaemonClientKind,
+) -> Result<Arc<CuaDriver>, DriverError> {
+ Ok(Arc::new(CuaDriver {
+ backend: DriverBackend::Embedded(Arc::new(
+ NativeAbiDriver::create_configured_with_activity_observer(
+ configured_driver_options_json(&options),
+ observer,
+ )?,
+ )),
+ client_kind,
+ }))
+}
+
+fn create_configured_with_host_integrations_for_client(
+ options: ConfiguredDriverOptions,
+ host: Arc<dyn DriverAuthorizationHost>,
+ observer: Arc<dyn DriverActivityObserver>,
+ client_kind: DaemonClientKind,
+) -> Result<Arc<CuaDriver>, DriverError> {
+ let provider = authorization_host::SdkDriverAuthorizationHost::new(host);
+ Ok(Arc::new(CuaDriver {
+ backend: DriverBackend::Embedded(Arc::new(
+ NativeAbiDriver::create_configured_with_authorization_provider(
+ configured_driver_options_json(&options),
+ provider,
+ Some(observer),
+ )?,
+ )),
+ client_kind,
+ }))
+}
+
+fn connect_for_client(
+ socket_path: Option<String>,
+ client_kind: DaemonClientKind,
+) -> Result<Arc<CuaDriver>, DriverError> {
+ let socket_path = socket_path.unwrap_or_else(|| socket_path_for_namespace("cua-driver"));
+ if socket_path.trim().is_empty() {
+ return Err(DriverError::Configuration {
+ reason: "socket_path must not be empty".into(),
+ });
+ }
+ Ok(Arc::new(CuaDriver {
+ backend: DriverBackend::Daemon(DaemonBackend::new(socket_path)),
+ client_kind,
+ }))
+}
+
fn create_private_worker_for_client(
options: PrivateWorkerOptions,
client_kind: DaemonClientKind,
@@ -2027,6 +2049,26 @@ mod snapshot_lifecycle_tests;
#[cfg(test)]
mod tests {
+ #[test]
+ fn connect_rejects_empty_socket_paths_for_every_client_kind() {
+ for path in ["", " \t"] {
+ let results = [
+ CuaDriver::connect(Some(path.into())),
+ CuaDriver::connect_with_client_kind(Some(path.into()), SdkClientKind::Python),
+ CuaDriver::connect_with_client_kind(Some(path.into()), SdkClientKind::Typescript),
+ ];
+ for result in results {
+ match result {
+ Err(DriverError::Configuration { reason }) => {
+ assert_eq!(reason, "socket_path must not be empty");
+ }
+ Err(other) => panic!("unexpected error for {path:?}: {other:?}"),
+ Ok(_) => panic!("empty socket path {path:?} was accepted"),
+ }
+ }
+ }
+ }
+
mod native_windows;
use super::*;
use cua_driver_contract::{ParseVisualRegionsOutput, VisualParseError};