q81801/agent
0
1---2title: OpenClaw HF Space3emoji: ๐ฆ4colorFrom: blue5colorTo: indigo6sdk: docker7app_port: 78608pinned: false9---10 11# OpenClaw on Hugging Face Space (Docker)12 13This setup is designed to provide the following:14 15- Build the OpenClaw container on top of `ubuntu:24.04`16- Serve the OpenClaw dashboard directly on port `7860` (default Space access port)17- Use third-party OpenAI-compatible `base_url + api_key` by default (injected via environment variables)18- Store OpenClaw config/workspace under `/root/.openclaw`19- Restore state automatically from a Hugging Face Dataset on startup20- Run scheduled backups of OpenClaw data to a Hugging Face Dataset via `cron` (as `root` user)21- Preinstall `python3`, `uv`, `vim`, `neovim`, `chromium` (via Chrome for Testing archive), `gh`, `hf`, `opencode`, `codex`, `claude` (Claude Code CLI), `@larksuite/cli` (with `npx skills add larksuite/cli -y -g`), and `sshx` in the image for interactive terminal use22 23## Repository Layout24 25- `Dockerfile`: Runtime image for the Space26- `scripts/openclaw-entrypoint.sh`: Main startup flow (restore, config generation, cron setup, gateway start)27- `openclaw_hf/backup.py`: Backup/restore implementation28- `scripts/openclaw-backup-cron.sh`: Cron entrypoint for backup jobs29- `scripts/openclaw-restore.sh`: Startup restore entrypoint30- `scripts/openclaw-gateway-restart`: Kill running `openclaw-gateway` processes31- `scripts/bootstrap-hf.sh`: Interactive bootstrap for Space/Dataset creation, upload, and Space variables/secrets setup (macOS/Linux)32- `scripts/bootstrap-hf.ps1`: Interactive bootstrap for Space/Dataset creation, upload, and Space variables/secrets setup (Windows PowerShell)33- `tests/test_backup.py`: Unit tests for the backup module34- `tests/test_entrypoint_config.py`: Unit tests for gateway config generation behavior35 36## Required Variables (Space Settings)37 38In your Hugging Face Space (`Settings -> Variables and secrets`), configure at least:39 40- Variable: `OPENCLAW_BACKUP_DATASET_REPO`: Backup target Dataset in `username/dataset-name` format41- Secret: `HF_TOKEN`: Used to write backups to the Dataset (must have write permission to that Dataset)42- Secret: `OPENCLAW_GATEWAY_TOKEN`: Gateway token (recommended; if omitted in deployment workflow, generate a random 32-character value)43- Secret: `OPENCLAW_GATEWAY_PASSWORD`: Gateway password (optional; if omitted in deployment workflow, generate a random 16-character value)44 45When using `./scripts/bootstrap-hf.sh` (macOS/Linux) or `./scripts/bootstrap-hf.ps1` (Windows PowerShell), these values are configured automatically on the target Space.46 47## Optional LLM Variables (All-Or-None)48 49Set all of these together only when you want OpenClaw to preconfigure a custom third-party model:50 51- Variable: `OPENCLAW_LLM_BASE_URL`: Third-party base URL (for example OpenAI-compatible `/v1`)52- Variable: `OPENCLAW_LLM_MODEL`: Third-party model ID53- Secret: `OPENCLAW_LLM_API_KEY`: Third-party API key54 55If any of the three is missing, entrypoint skips custom model generation.56In that case, you can still configure from inside the container (for example via `sshx`).57 58## Common Optional Variables59 60- `OPENCLAW_LLM_MODEL` (unset by default; used only when custom model preconfiguration is enabled)61- `OPENCLAW_LLM_PROVIDER` (default: `thirdparty`)62- `OPENCLAW_LLM_API` (default: `openai-completions`)63- `OPENCLAW_VERSION` (used by Docker install step; bootstrap prompts for it and defaults to the latest version detected from npm registry, fallback `latest`)64- `OPENCLAW_STATE_DIR` (default: `/root/.openclaw`)65- `OPENCLAW_USER` (default: `root`, runtime user for gateway and cron jobs)66- `OPENCLAW_GROUP` (default: `root`, runtime group for gateway and cron jobs)67- `OPENCLAW_CONFIG_PATH` (default: `/root/.openclaw/openclaw.json`)68- `OPENCLAW_WORKSPACE_DIR` (default: `/root/.openclaw/workspace`)69- `OPENCLAW_BACKUP_CRON` (default: `*/30 * * * *`, backup every 30 minutes)70- `OPENCLAW_BACKUP_SOURCE_DIR` (default: `/root/.openclaw`, backup/restore base directory for `openclaw-state`)71- `OPENCLAW_BACKUP_ROOT_CONFIG_DIR` (default: `/root/.config`, additional backup/restore directory for `root-config`)72- `OPENCLAW_BACKUP_ROOT_CODEX_DIR` (default: `/root/.codex`, additional backup/restore directory for `root-codex`)73- `OPENCLAW_BACKUP_ROOT_CLAUDE_DIR` (default: `/root/.claude`, additional backup/restore directory for `root-claude`)74- `OPENCLAW_BACKUP_ROOT_AGENTS_DIR` (default: `/root/.agents`, additional backup/restore directory for `root-agents`)75- `OPENCLAW_BACKUP_ROOT_SSH_DIR` (default: `/root/.ssh`, additional backup/restore directory for `root-ssh`)76- `OPENCLAW_BACKUP_ROOT_NPM_DIR` (default: `/root/.npm`, additional backup/restore directory for `root-npm`)77- `OPENCLAW_BACKUP_ROOT_LARK_CLI_DIR` (default: `/root/.lark-cli`, additional backup/restore directory for `root-lark-cli`)78- `OPENCLAW_BACKUP_PATH_PREFIX` (default: `backups`)79- `OPENCLAW_BACKUP_LATEST_NAME` (default: `latest-backup.tar.gz`)80- `OPENCLAW_BACKUP_KEEP_COUNT` (default: `48`, keep newest N timestamped backup archives; older ones are auto-deleted)81- `OPENCLAW_SSHX_AUTO_START` (default: `false`; set `true` to auto-run `sshx` in background on startup)82- `OPENCLAW_GATEWAY_AUTH_MODE` (default: `token`, optional: `password`)83- `OPENCLAW_GATEWAY_CONTROLUI_ALLOW_INSECURE_AUTH` (default: `false`)84- `OPENCLAW_GATEWAY_CONTROLUI_DANGEROUSLY_DISABLE_DEVICE_AUTH` (default: `false`; set `true` to bypass pairing, strongly discouraged on public networks)85 86## Quick Deployment87 88Run the interactive bootstrap script from repo root:89 90```bash91./scripts/bootstrap-hf.sh92```93 94```powershell95powershell -ExecutionPolicy ByPass -File .\scripts\bootstrap-hf.ps196```97 98`bootstrap-hf.sh` / `bootstrap-hf.ps1` will:99 100- Check/install `hf` CLI:101 - macOS/Linux: `curl -LsSf https://hf.co/cli/install.sh | bash`102 - Windows PowerShell: `powershell -ExecutionPolicy ByPass -c "irm https://hf.co/cli/install.ps1 | iex"`103- Resolve HF auth first (before all other variables):104 - if `hf auth whoami` is not logged in: prompt `HF_TOKEN` and run `hf auth login --token <HF_TOKEN>`105 - if already logged in: ask whether to use current user106 - choose `yes`: continue107 - choose `no`: backup current token, prompt new `HF_TOKEN`, run `hf auth login --token <HF_TOKEN>`, and restore the previous token at the end108- Ask for `space_name`, `dataset_name`, `OPENCLAW_VERSION`, gateway token/password, and optional LLM settings109- Default `OPENCLAW_VERSION` to latest detected from npm registry (`openclaw`), fallback `latest` when detection fails110- Auto-generate `OPENCLAW_GATEWAY_TOKEN` (32 chars) and `OPENCLAW_GATEWAY_PASSWORD` (16 chars) if left empty111- Create private Space + Dataset and upload this repository112- Configure Space `Variables and secrets` automatically, including:113 - `OPENCLAW_BACKUP_DATASET_REPO`114 - `OPENCLAW_VERSION`115 - `HF_TOKEN`116 - `OPENCLAW_GATEWAY_TOKEN`117 - `OPENCLAW_GATEWAY_PASSWORD`118 - `OPENCLAW_GATEWAY_CONTROLUI_ALLOW_INSECURE_AUTH=false`119 - `OPENCLAW_GATEWAY_CONTROLUI_DANGEROUSLY_DISABLE_DEVICE_AUTH=false`120- Optionally configure LLM triplet and set `OPENCLAW_SSHX_AUTO_START` from prompt choice (`true`/`false`)121- Print planned deployment settings and require a final confirmation before creating/updating Space/Dataset resources122- Print Hugging Face Space page URL, app URL, and `/healthz`123 124If gateway token/password were auto-generated, the script prints them at the end.125 126## Agent Hand-off Prompt127 128Copy and send to your agent:129 130```131Please deploy OpenClaw to Hugging Face by strictly following the deployment skill in https://github.com/tenfyzhong/openclaw-hf/blob/main/SKILL.md132```133 134## Hugging Face Keep-Alive135 136How to keep a Space available depends on hardware tier:137 138- Free `cpu-basic`: the Space sleeps after inactivity (currently around 48h). It cannot be configured to run forever on free hardware.139- Paid hardware: the Space runs continuously by default. In `Settings -> Hardware`, set `Sleep time` to `Never` (or use API with `sleep_time=-1`) for true 24/7 availability.140- Cost-saving mode on paid hardware: set a custom `Sleep time` (for example `3600` seconds) so it auto-sleeps and auto-wakes on the next visit.141 142Space URL composition:143 144- Space repo ID format: `<owner>/<space_name>` (example: `tenfyzhong/openclaw-hf`)145- Public runtime host format: `https://<owner>-<space_name>.hf.space`146- OpenClaw health check URL: `https://<owner>-<space_name>.hf.space/healthz`147- Inside the Space runtime, Hugging Face also provides `SPACE_HOST`, so health URL can be built as `https://${SPACE_HOST}/healthz`.148 149Example:150 151```bash152SPACE_ID="tenfyzhong/openclaw-hf"153SPACE_HOST="${SPACE_ID/\//-}.hf.space"154HEALTH_URL="https://${SPACE_HOST}/healthz"155echo "$HEALTH_URL"156```157 158Keep-alive by periodic health checks:159 160```bash161*/5 * * * * HF_TOKEN=hf_xxx /path/to/repo/scripts/check-space-health.sh tenfyzhong/openclaw-hf >/dev/null || true162```163 164Notes:165 166- For private Spaces, unauthenticated calls to `https://<owner>-<space_name>.hf.space/healthz` return a Hub 404 page. This is expected access control behavior.167- For private Spaces, include `Authorization: Bearer <HF_TOKEN>` (the helper script above does this automatically via `HF_TOKEN` or `HUGGINGFACE_HUB_TOKEN`).168- This ping strategy is a practical workaround for reducing idle sleep on free hardware, but it is not a guaranteed always-on method.169- If you need strict 24/7 uptime, use paid hardware and set sleep time to `Never`.170 171References:172 173- <https://huggingface.co/docs/hub/spaces-gpus#sleep-time>174- <https://huggingface.co/docs/huggingface_hub/package_reference/space_runtime>175- <https://huggingface.co/docs/hub/spaces-overview>176 177Programmatic options (owner token required):178 179```python180from huggingface_hub import HfApi181 182api = HfApi(token="hf_xxx")183repo_id = "your-username/your-space"184 185# Keep running (paid hardware)186api.set_space_sleep_time(repo_id=repo_id, sleep_time=-1)187 188# Or sleep after 1 hour of inactivity189api.set_space_sleep_time(repo_id=repo_id, sleep_time=3600)190 191# Manual control192api.pause_space(repo_id=repo_id)193api.restart_space(repo_id=repo_id)194```195 196For this project, if you need stable dashboard access without cold starts, use paid hardware and set sleep time to `Never`.197 198## Backup/Restore Flow199 200- Startup restore: on container startup, it tries to fetch `latest-backup.tar.gz` from the Dataset and restore:201 - `openclaw-state` -> `OPENCLAW_BACKUP_SOURCE_DIR` (default `/root/.openclaw`)202 - `root-config` -> `OPENCLAW_BACKUP_ROOT_CONFIG_DIR` (default `/root/.config`, restored only when present in archive)203 - `root-codex` -> `OPENCLAW_BACKUP_ROOT_CODEX_DIR` (default `/root/.codex`, restored only when present in archive)204 - `root-claude` -> `OPENCLAW_BACKUP_ROOT_CLAUDE_DIR` (default `/root/.claude`, restored only when present in archive)205 - `root-agents` -> `OPENCLAW_BACKUP_ROOT_AGENTS_DIR` (default `/root/.agents`, restored only when present in archive)206 - `root-ssh` -> `OPENCLAW_BACKUP_ROOT_SSH_DIR` (default `/root/.ssh`, restored only when present in archive)207 - `root-npm` -> `OPENCLAW_BACKUP_ROOT_NPM_DIR` (default `/root/.npm`, restored only when present in archive)208 - `root-lark-cli` -> `OPENCLAW_BACKUP_ROOT_LARK_CLI_DIR` (default `/root/.lark-cli`, restored only when present in archive)209- Scheduled backup: cron runs based on `OPENCLAW_BACKUP_CRON`210- Shutdown backup: when the container receives a stop signal, one final backup is uploaded before exit211- Each backup upload includes:212 - `backups/openclaw-backup-<timestamp>.tar.gz`213 - archive root `openclaw-state/`214 - archive root `root-config/` (included when `OPENCLAW_BACKUP_ROOT_CONFIG_DIR` exists)215 - archive root `root-codex/` (included when `OPENCLAW_BACKUP_ROOT_CODEX_DIR` exists)216 - archive root `root-claude/` (included when `OPENCLAW_BACKUP_ROOT_CLAUDE_DIR` exists)217 - archive root `root-agents/` (included when `OPENCLAW_BACKUP_ROOT_AGENTS_DIR` exists)218 - archive root `root-ssh/` (included when `OPENCLAW_BACKUP_ROOT_SSH_DIR` exists)219 - archive root `root-npm/` (included when `OPENCLAW_BACKUP_ROOT_NPM_DIR` exists)220 - archive root `root-lark-cli/` (included when `OPENCLAW_BACKUP_ROOT_LARK_CLI_DIR` exists)221 - `latest-backup.tar.gz`222 - `latest-backup.json`223- Retention: after upload, only the newest `OPENCLAW_BACKUP_KEEP_COUNT` timestamped archives are kept (default `48`); older timestamped archives are deleted automatically224 225## Use sshx Inside the Container226 227`sshx` is preinstalled in the image.228 2291. Auto-start `sshx` in background via environment variables:230 231```bash232OPENCLAW_SSHX_AUTO_START=true233```234 235When enabled, entrypoint starts `sshx` in background and sends `sshx` output directly to container stdout/stderr logs (no file logging).236 2372. Manual start inside container:238 239```bash240sshx241```242 2433. Let OpenClaw start a process itself (run in OpenClaw terminal/tool):244 245```bash246nohup sshx >/proc/1/fd/1 2>/proc/1/fd/2 &247```248 2494. After use, close `sshx` process promptly:250 251```bash252pgrep -fa sshx253pkill -TERM -f '(^|/)sshx($| )'254```255 256## Local Test257 258```bash259python3 -m unittest discover -s tests -p 'test_*.py'260```261 262Pull Requests to `main` run GitHub Actions CI automatically (`.github/workflows/pr-ci.yml`):263- Unit tests: `python3 -m unittest discover -s tests -p 'test_*.py'`264- Docker image build: `docker build` (via Buildx) with `OPENCLAW_VERSION=latest`265 266## License267 268MIT. See `LICENSE`.269 