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.

added shared provider vm type

Dillon DuPontb29f89597f47
diff --git a/examples/cloud_api_examples.py b/examples/cloud_api_examples.py
index a57779328d..47844a55d2 100644
--- a/examples/cloud_api_examples.py
+++ b/examples/cloud_api_examples.py
@@ -1,29 +1,31 @@
import asyncio
import os
-from typing import List, Dict, Any
+from utils import load_dotenv_files
+
+load_dotenv_files()
from computer.providers.cloud.provider import CloudProvider
async def main() -> None:
api_key = os.getenv("CUA_API_KEY")
if not api_key:
- raise RuntimeError(
- "CUA_API_KEY environment variable is not set.\n"
- "Set it and re-run, e.g.:\n"
- " PowerShell: $env:CUA_API_KEY='your-key'\n"
- " bash: export CUA_API_KEY=your-key"
- )
+ raise RuntimeError("CUA_API_KEY environment variable is not set")
+ api_base = os.getenv("CUA_API_BASE")
+ if api_base:
+ print(f"Using API base: {api_base}")
+
# List VMs
provider = CloudProvider(api_key=api_key, verbose=True)
async with provider:
vms = await provider.list_vms()
- if not vms:
- print("No VMs returned (check API key or account).")
- else:
- print(f"Found {len(vms)} VM(s):")
- for i, vm in enumerate(vms, start=1):
- # Typical fields: name, status (if available), region, os_type, etc. (depends on account)
- print(f"[{i}] {vm}")
+ print(f"Found {len(vms)} VM(s)")
+ for vm in vms:
+ print(
+ f"name: {vm['name']}\n",
+ f"status: {vm['status']}\n", # pending, running, stopped, terminated, failed
+ f"api_url: {vm.get('api_url')}\n",
+ f"vnc_url: {vm.get('vnc_url')}\n",
+ )
# --- Additional operations (commented out) ---
# To stop a VM by name:
diff --git a/libs/python/computer/computer/providers/base.py b/libs/python/computer/computer/providers/base.py
index 2352609756..0c36c9137e 100644
--- a/libs/python/computer/computer/providers/base.py
+++ b/libs/python/computer/computer/providers/base.py
@@ -2,7 +2,9 @@
import abc
from enum import StrEnum
-from typing import Dict, List, Optional, Any, AsyncContextManager
+from typing import Dict, Optional, Any, AsyncContextManager
+
+from .types import ListVMsResponse
class VMProviderType(StrEnum):
@@ -42,8 +44,13 @@ class BaseVMProvider(AsyncContextManager):
pass
@abc.abstractmethod
- async def list_vms(self) -> List[Dict[str, Any]]:
- """List all available VMs."""
+ async def list_vms(self) -> ListVMsResponse:
+ """List all available VMs.
+
+ Returns:
+ ListVMsResponse: A list of minimal VM objects as defined in
+ `computer.providers.types.MinimalVM`.
+ """
pass
@abc.abstractmethod
@@ -76,6 +83,20 @@ class BaseVMProvider(AsyncContextManager):
"""
pass
+ @abc.abstractmethod
+ async def restart_vm(self, name: str, storage: Optional[str] = None) -> Dict[str, Any]:
+ """Restart a VM by name.
+
+ Args:
+ name: Name of the VM to restart
+ storage: Optional storage path override. If provided, this will be used
+ instead of the provider's default storage path.
+
+ Returns:
+ Dictionary with VM restart status and information
+ """
+ pass
+
@abc.abstractmethod
async def update_vm(self, name: str, update_opts: Dict[str, Any], storage: Optional[str] = None) -> Dict[str, Any]:
"""Update VM configuration.
diff --git a/libs/python/computer/computer/providers/cloud/provider.py b/libs/python/computer/computer/providers/cloud/provider.py
index a54e01c4e3..0e3dfd83af 100644
--- a/libs/python/computer/computer/providers/cloud/provider.py
+++ b/libs/python/computer/computer/providers/cloud/provider.py
@@ -12,6 +12,7 @@ import logging
from typing import Dict, List, Optional, Any
from ..base import BaseVMProvider, VMProviderType
+from ..types import ListVMsResponse, MinimalVM
# Setup logging
logger = logging.getLogger(__name__)
@@ -106,7 +107,7 @@ class CloudProvider(BaseVMProvider):
# Host does not resolve → not found
return {"name": name, "status": "not_found", "hostname": hostname}
- async def list_vms(self) -> List[Dict[str, Any]]:
+ async def list_vms(self) -> ListVMsResponse:
url = f"{self.api_base}/v1/vms"
headers = {
"Authorization": f"Bearer {self.api_key}",
@@ -122,7 +123,24 @@ class CloudProvider(BaseVMProvider):
logger.error(f"Failed to parse list_vms JSON: {text}")
return []
if isinstance(data, list):
- return data
+ # Enrich with convenience URLs when possible.
+ enriched: List[Dict[str, Any]] = []
+ for item in data:
+ vm = dict(item) if isinstance(item, dict) else {}
+ name = vm.get("name")
+ password = vm.get("password")
+ if isinstance(name, str) and name:
+ host = f"https://{name}.containers.cloud.trycua.com:8443"
+ # api_url: always set if missing
+ if not vm.get("api_url"):
+ vm["api_url"] = host
+ # vnc_url: only when password available
+ if not vm.get("vnc_url") and isinstance(password, str) and password:
+ vm[
+ "vnc_url"
+ ] = f"https://{host}/vnc.html?autoconnect=true&password={password}"
+ enriched.append(vm)
+ return enriched # type: ignore[return-value]
logger.warning("Unexpected response for list_vms; expected list")
return []
elif resp.status == 401:
diff --git a/libs/python/computer/computer/providers/cloud/types.py b/libs/python/computer/computer/providers/cloud/types.py
new file mode 100644
index 0000000000..a289701d2e
--- /dev/null
+++ b/libs/python/computer/computer/providers/cloud/types.py
@@ -0,0 +1,57 @@
+"""Pydantic models for the CUA Cloud provider API.
+
+Documents the response shape for the Cloud list VMs endpoint.
+
+List VMs
+- Method: GET
+- Path: `/v1/vms`
+- Description: Returns all VMs owned by the API key's user.
+- Responses:
+ - 200: Array of minimal VM objects with fields `{ name, password, status }`
+ - 401: Unauthorized (missing/invalid API key)
+
+Example curl:
+ curl -H "Authorization: Bearer $CUA_API_KEY" \
+ "https://api.cua.ai/v1/vms"
+
+Response shape:
+[
+ {
+ "name": "s-windows-x4snp46ebf",
+ "password": "49b8daa3",
+ "status": "running"
+ }
+]
+
+Status values:
+- pending : VM deployment in progress
+- running : VM is active and accessible
+- stopped : VM is stopped but not terminated
+- terminated: VM has been permanently destroyed
+- failed : VM deployment or operation failed
+"""
+from __future__ import annotations
+
+from typing import Literal, Optional
+
+# Require pydantic for typed models in provider APIs
+from pydantic import BaseModel
+
+
+CloudVMStatus = Literal["pending", "running", "stopped", "terminated", "failed"]
+
+
+class CloudVM(BaseModel):
+ """Minimal VM object returned by CUA Cloud list API.
+
+ Additional optional fields (like URLs) may be filled by callers based on
+ their environment but are not guaranteed by the API.
+ """
+
+ name: str
+ password: str
+ status: CloudVMStatus
+
+ # Optional, not guaranteed by the list API, but useful when known/derived
+ vnc_url: Optional[str] = None
+ api_url: Optional[str] = None
diff --git a/libs/python/computer/computer/providers/types.py b/libs/python/computer/computer/providers/types.py
new file mode 100644
index 0000000000..48e9f1f298
--- /dev/null
+++ b/libs/python/computer/computer/providers/types.py
@@ -0,0 +1,35 @@
+"""Shared provider type definitions for VM metadata and responses.
+
+These base types describe the common shape of objects returned by provider
+methods like `list_vms()`.
+"""
+from __future__ import annotations
+
+from typing import Literal, TypedDict, NotRequired
+
+# Core status values per product docs
+VMStatus = Literal[
+ "pending", # VM deployment in progress
+ "running", # VM is active and accessible
+ "stopped", # VM is stopped but not terminated
+ "terminated", # VM has been permanently destroyed
+ "failed", # VM deployment or operation failed
+]
+
+
+class MinimalVM(TypedDict):
+ """Minimal VM object shape returned by list calls.
+
+ Providers may include additional fields. Optional fields below are
+ common extensions some providers expose or that callers may compute.
+ """
+ name: str
+ status: VMStatus
+ # Not always included by all providers
+ password: NotRequired[str]
+ vnc_url: NotRequired[str]
+ api_url: NotRequired[str]
+
+
+# Convenience alias for list_vms() responses
+ListVMsResponse = list[MinimalVM]