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.

feat(cua-train): add stub package with PyPI publish workflow (#1918)

ddupontb87700714808
diff --git a/.github/workflows/cd-py-train.yml b/.github/workflows/cd-py-train.yml
new file mode 100644
index 0000000000..4eedc34207
--- /dev/null
+++ b/.github/workflows/cd-py-train.yml
@@ -0,0 +1,69 @@
+name: "CD: cua-train (PyPI)"
+
+on:
+ push:
+ tags:
+ - "train-v*"
+ workflow_dispatch:
+ inputs:
+ version:
+ description: "Version to publish (without v prefix)"
+ required: true
+ default: "0.1.0"
+ workflow_call:
+ inputs:
+ version:
+ description: "Version to publish"
+ required: true
+ type: string
+
+permissions:
+ contents: write
+
+jobs:
+ prepare:
+ runs-on: macos-latest
+ outputs:
+ version: ${{ steps.get-version.outputs.version }}
+ steps:
+ - uses: actions/checkout@v4
+
+ - name: Determine version
+ id: get-version
+ run: |
+ if [ -n "${{ inputs.version }}" ]; then
+ VERSION=${{ inputs.version }}
+ elif [ "${{ github.event_name }}" == "push" ]; then
+ if [[ "${{ github.ref }}" =~ ^refs/tags/train-v([0-9]+\.[0-9]+\.[0-9]+) ]]; then
+ VERSION=${BASH_REMATCH[1]}
+ else
+ echo "Invalid tag format for train"
+ exit 1
+ fi
+ elif [ -n "${{ github.event.inputs.version }}" ]; then
+ VERSION=${{ github.event.inputs.version }}
+ else
+ echo "ERROR: No version found!"
+ exit 1
+ fi
+ echo "version=$VERSION" >> $GITHUB_OUTPUT
+
+ publish:
+ needs: prepare
+ uses: ./.github/workflows/py-reusable-publish.yml
+ with:
+ package_name: "train"
+ package_dir: "libs/python/cua-train"
+ version: ${{ needs.prepare.outputs.version }}
+ base_package_name: "cua-train"
+ secrets:
+ PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
+
+ create-release:
+ needs: [prepare, publish]
+ uses: ./.github/workflows/release-github-reusable.yml
+ with:
+ tag_name: "train-v${{ needs.prepare.outputs.version }}"
+ release_name: "cua-train v${{ needs.prepare.outputs.version }}"
+ module_path: "libs/python/cua-train"
+ body: ""
diff --git a/.github/workflows/release-bump-version.yml b/.github/workflows/release-bump-version.yml
index 059cc13ab1..4e4f26abcf 100644
--- a/.github/workflows/release-bump-version.yml
+++ b/.github/workflows/release-bump-version.yml
@@ -21,6 +21,7 @@ on:
- pypi/sandbox
- pypi/sandbox-apps
- pypi/som
+ - pypi/train
- npm/cli
- npm/computer
- npm/core
@@ -113,6 +114,10 @@ jobs:
echo "directory=libs/python/som" >> $GITHUB_OUTPUT
echo "type=python" >> $GITHUB_OUTPUT
;;
+ "pypi/train")
+ echo "directory=libs/python/cua-train" >> $GITHUB_OUTPUT
+ echo "type=python" >> $GITHUB_OUTPUT
+ ;;
"npm/cli")
echo "directory=libs/typescript/cua-cli" >> $GITHUB_OUTPUT
echo "type=npm" >> $GITHUB_OUTPUT
@@ -381,6 +386,15 @@ jobs:
echo "SOM version: $VERSION"
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
+ - name: Capture bumped train version
+ if: ${{ inputs.service == 'pypi/train' }}
+ id: train_version
+ run: |
+ cd libs/python/cua-train
+ VERSION=$(python -c "import tomllib; from pathlib import Path; data = tomllib.loads(Path('pyproject.toml').read_text()); print(data['project']['version'])")
+ echo "Train version: $VERSION"
+ echo "version=$VERSION" >> "$GITHUB_OUTPUT"
+
- name: Capture bumped npm/cli version
if: ${{ inputs.service == 'npm/cli' }}
id: npm_cli_version
diff --git a/libs/python/cua-train/README.md b/libs/python/cua-train/README.md
new file mode 100644
index 0000000000..732cbeb0a8
--- /dev/null
+++ b/libs/python/cua-train/README.md
@@ -0,0 +1,23 @@
+# cua-train
+
+Python client for the [Cua Cloud](https://run.cua.ai) API.
+
+```bash
+pip install cua-train
+```
+
+```python
+from cua_train import TrainClient
+
+client = TrainClient.from_key(
+ client_id="ukey-...", # from your Cua Cloud account
+ client_secret="...",
+)
+
+# Control plane — claim a VM
+http = client.get_async_httpx_client()
+await http.post("/api/k8s/apis/osgym.cua.ai/v1alpha1/namespaces/my-pool/osgymsandboxclaims", json={...})
+
+# Data plane — exec in the VM
+await http.post("/api/svc/my-pool/my-sandbox-server/execute", json={"command": "echo hi"})
+```
diff --git a/libs/python/cua-train/pyproject.toml b/libs/python/cua-train/pyproject.toml
new file mode 100644
index 0000000000..0889d20c87
--- /dev/null
+++ b/libs/python/cua-train/pyproject.toml
@@ -0,0 +1,38 @@
+[project]
+name = "cua-train"
+version = "0.1.0"
+description = "Cua Cloud Python client — control and data plane for Cua Cloud desktop VM pools"
+readme = "README.md"
+license = "MIT"
+authors = [
+ { name = "TryCua", email = "[email protected]" }
+]
+keywords = [
+ "cua",
+ "computer-use",
+ "cloud",
+ "vm",
+ "automation",
+]
+classifiers = [
+ "Development Status :: 3 - Alpha",
+ "Intended Audience :: Developers",
+ "License :: OSI Approved :: MIT License",
+ "Programming Language :: Python :: 3",
+ "Programming Language :: Python :: 3.10",
+ "Programming Language :: Python :: 3.11",
+ "Programming Language :: Python :: 3.12",
+]
+requires-python = ">=3.10"
+dependencies = ["httpx>=0.27.0"]
+
+[project.urls]
+Homepage = "https://run.cua.ai"
+Repository = "https://github.com/trycua/cua"
+
+[build-system]
+requires = ["hatchling"]
+build-backend = "hatchling.build"
+
+[tool.hatch.build.targets.wheel]
+packages = ["src/cua_train"]
diff --git a/libs/python/cua-train/src/cua_train/__init__.py b/libs/python/cua-train/src/cua_train/__init__.py
new file mode 100644
index 0000000000..4808433189
--- /dev/null
+++ b/libs/python/cua-train/src/cua_train/__init__.py
@@ -0,0 +1,5 @@
+"""cua-train — Python client for the Cua Cloud API."""
+
+from .client import TrainClient
+
+__all__ = ["TrainClient"]
diff --git a/libs/python/cua-train/src/cua_train/client.py b/libs/python/cua-train/src/cua_train/client.py
new file mode 100644
index 0000000000..25607a352a
--- /dev/null
+++ b/libs/python/cua-train/src/cua_train/client.py
@@ -0,0 +1,123 @@
+"""TrainClient — authenticated httpx client for the Cua Cloud API."""
+
+from __future__ import annotations
+
+import threading
+import time
+
+import httpx
+
+
+class TrainClient:
+ """Authenticated client for the Cua Cloud API.
+
+ Obtain via :meth:`from_key` using a per-user key (``ukey-…``) from
+ your Cua Cloud account. The client transparently refreshes its bearer
+ token before it expires.
+
+ Both the control plane (VM claim lifecycle via ``/api/k8s``) and the
+ data plane (exec/upload/screenshot via ``/api/svc``) are reached through
+ the same base URL (default: ``https://run.cua.ai``).
+ """
+
+ _DEFAULT_TOKEN_URL = (
+ "https://auth.cua.ai/realms/cyclops-cs/protocol/openid-connect/token"
+ )
+ _DEFAULT_BASE_URL = "https://run.cua.ai"
+
+ def __init__(
+ self,
+ token: str,
+ base_url: str = _DEFAULT_BASE_URL,
+ *,
+ token_url: str = _DEFAULT_TOKEN_URL,
+ client_id: str = "",
+ client_secret: str = "",
+ token_deadline: float = 0.0,
+ ) -> None:
+ self._token = token
+ self._base_url = base_url.rstrip("/")
+ self._token_url = token_url
+ self._client_id = client_id
+ self._client_secret = client_secret
+ self._token_deadline = token_deadline
+ self._refresh_lock = threading.Lock()
+ self._async_client: httpx.AsyncClient | None = None
+
+ @classmethod
+ def from_key(
+ cls,
+ *,
+ token_url: str = _DEFAULT_TOKEN_URL,
+ client_id: str,
+ client_secret: str,
+ base_url: str = _DEFAULT_BASE_URL,
+ ) -> "TrainClient":
+ """Exchange client credentials for a bearer token.
+
+ Args:
+ token_url: Keycloak token endpoint.
+ client_id: Per-user key client ID (``ukey-…``).
+ client_secret: Client secret.
+ base_url: Cua Cloud API base URL (default: ``https://run.cua.ai``).
+ """
+ token, ttl = cls._exchange(token_url, client_id, client_secret)
+ return cls(
+ token=token,
+ base_url=base_url,
+ token_url=token_url,
+ client_id=client_id,
+ client_secret=client_secret,
+ token_deadline=time.monotonic() + ttl,
+ )
+
+ @staticmethod
+ def _exchange(
+ token_url: str, client_id: str, client_secret: str
+ ) -> tuple[str, float]:
+ resp = httpx.post(
+ token_url,
+ data={
+ "grant_type": "client_credentials",
+ "client_id": client_id,
+ "client_secret": client_secret,
+ },
+ )
+ resp.raise_for_status()
+ body = resp.json()
+ return body["access_token"], float(body.get("expires_in", 300))
+
+ def reset_token(self) -> None:
+ """Force re-exchange on the next :meth:`get_async_httpx_client` call."""
+ self._token_deadline = 0
+
+ def _refresh_token(self) -> None:
+ if not self._client_id:
+ return
+ if time.monotonic() < self._token_deadline - 30:
+ return
+ with self._refresh_lock:
+ if time.monotonic() < self._token_deadline - 30:
+ return
+ token, ttl = self._exchange(
+ self._token_url, self._client_id, self._client_secret
+ )
+ self._token = token
+ self._token_deadline = time.monotonic() + ttl
+ auth_header = f"Bearer {token}"
+ if self._async_client is not None:
+ self._async_client.headers["Authorization"] = auth_header
+
+ def get_async_httpx_client(self) -> httpx.AsyncClient:
+ """Return an async httpx client with the current bearer token.
+
+ Re-mints the token transparently when it is about to expire.
+ """
+ self._refresh_token()
+ if self._async_client is None:
+ self._async_client = httpx.AsyncClient(
+ base_url=self._base_url,
+ headers={"Authorization": f"Bearer {self._token}"},
+ follow_redirects=False,
+ )
+ return self._async_client