Spaces:
Sleeping
Sleeping
feat: make llm setup optional and move handoff rules into SOP (#3)
Browse files* feat: make llm setup optional and move handoff rules into SOP
Signed-off-by: tenfyzhong <tenfy@tenfy.cn>
* docs: improve agent hand-off prompt and input question template
Signed-off-by: tenfyzhong <tenfy@tenfy.cn>
* docs: make agent hand-off prompt minimal
Signed-off-by: tenfyzhong <tenfy@tenfy.cn>
* feat: enforce ubuntu ownership and refine HF auth SOP
Signed-off-by: tenfyzhong <tenfy@tenfy.cn>
---------
Signed-off-by: tenfyzhong <tenfy@tenfy.cn>
- Dockerfile +0 -2
- README.md +117 -20
- scripts/bootstrap-hf.sh +4 -3
- scripts/openclaw-entrypoint.sh +59 -27
- tests/test_entrypoint_config.py +111 -0
Dockerfile
CHANGED
|
@@ -85,9 +85,7 @@ ENV OPENCLAW_GATEWAY_CONTROLUI_DANGEROUSLY_ALLOW_HOST_HEADER_ORIGIN_FALLBACK=tru
|
|
| 85 |
ENV OPENCLAW_GATEWAY_CONTROLUI_ALLOW_INSECURE_AUTH=false
|
| 86 |
ENV OPENCLAW_GATEWAY_CONTROLUI_DANGEROUSLY_DISABLE_DEVICE_AUTH=false
|
| 87 |
ENV OPENCLAW_LLM_PROVIDER=thirdparty
|
| 88 |
-
ENV OPENCLAW_LLM_MODEL=gpt-4o-mini
|
| 89 |
ENV OPENCLAW_LLM_API=openai-completions
|
| 90 |
-
ENV OPENCLAW_LLM_BASE_URL=https://api.openai.com/v1
|
| 91 |
ENV OPENCLAW_BACKUP_ENABLED=true
|
| 92 |
ENV OPENCLAW_BACKUP_CRON="*/30 * * * *"
|
| 93 |
ENV OPENCLAW_BACKUP_SOURCE_DIR=/home/ubuntu
|
|
|
|
| 85 |
ENV OPENCLAW_GATEWAY_CONTROLUI_ALLOW_INSECURE_AUTH=false
|
| 86 |
ENV OPENCLAW_GATEWAY_CONTROLUI_DANGEROUSLY_DISABLE_DEVICE_AUTH=false
|
| 87 |
ENV OPENCLAW_LLM_PROVIDER=thirdparty
|
|
|
|
| 88 |
ENV OPENCLAW_LLM_API=openai-completions
|
|
|
|
| 89 |
ENV OPENCLAW_BACKUP_ENABLED=true
|
| 90 |
ENV OPENCLAW_BACKUP_CRON="*/30 * * * *"
|
| 91 |
ENV OPENCLAW_BACKUP_SOURCE_DIR=/home/ubuntu
|
README.md
CHANGED
|
@@ -29,21 +29,31 @@ This setup is designed to provide the following:
|
|
| 29 |
- `scripts/openclaw-restore.sh`: Startup restore entrypoint
|
| 30 |
- `scripts/bootstrap-hf.sh`: Helper script to create a Space + Dataset
|
| 31 |
- `tests/test_backup.py`: Unit tests for the backup module
|
|
|
|
| 32 |
|
| 33 |
## Required Variables (Space Settings)
|
| 34 |
|
| 35 |
In your Hugging Face Space (`Settings -> Variables and secrets`), configure at least:
|
| 36 |
|
| 37 |
-
- Secret: `OPENCLAW_LLM_API_KEY`: Third-party API key
|
| 38 |
-
- Variable: `OPENCLAW_LLM_BASE_URL`: Third-party base URL (for example OpenAI-compatible `/v1`)
|
| 39 |
- Variable: `OPENCLAW_BACKUP_DATASET_REPO`: Backup target Dataset in `username/dataset-name` format
|
| 40 |
- Secret: `HF_TOKEN`: Used to write backups to the Dataset (must have write permission to that Dataset)
|
| 41 |
- Secret: `OPENCLAW_GATEWAY_TOKEN`: Fixed gateway token
|
| 42 |
- Secret: `OPENCLAW_GATEWAY_PASSWORD`: Gateway password (optional)
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
## Common Optional Variables
|
| 45 |
|
| 46 |
-
- `OPENCLAW_LLM_MODEL` (default
|
| 47 |
- `OPENCLAW_LLM_PROVIDER` (default: `thirdparty`)
|
| 48 |
- `OPENCLAW_LLM_API` (default: `openai-completions`)
|
| 49 |
- `OPENCLAW_STATE_DIR` (default: `/home/ubuntu/.openclaw`)
|
|
@@ -82,7 +92,7 @@ hf upload <space_repo_id> . --repo-type space --exclude '.git/**' --exclude '.gi
|
|
| 82 |
Copy and send to your agent:
|
| 83 |
|
| 84 |
```
|
| 85 |
-
Please
|
| 86 |
```
|
| 87 |
|
| 88 |
## Agent Deployment SOP (Collapsed)
|
|
@@ -130,17 +140,34 @@ hf version
|
|
| 130 |
|
| 131 |
### 2. Collect required inputs from user first
|
| 132 |
|
| 133 |
-
Agent must
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
- `OPENCLAW_LLM_BASE_URL`
|
| 136 |
-
- `OPENCLAW_LLM_MODEL`
|
| 137 |
-
- `OPENCLAW_LLM_API_KEY`
|
| 138 |
- `OPENCLAW_GATEWAY_TOKEN`
|
| 139 |
- `OPENCLAW_GATEWAY_PASSWORD`
|
| 140 |
- `space_name`
|
| 141 |
- `dataset_name`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
Before asking for values, agent must pick explanation language from runtime context:
|
| 146 |
|
|
@@ -156,19 +183,62 @@ USER_LANG="${LC_ALL:-${LC_MESSAGES:-${LANG:-en_US.UTF-8}}}"
|
|
| 156 |
|
| 157 |
Agent must explain what each variable is used for in the selected language when asking user to provide values:
|
| 158 |
|
| 159 |
-
- `
|
| 160 |
-
- `
|
| 161 |
-
- `
|
|
|
|
|
|
|
| 162 |
- `OPENCLAW_GATEWAY_TOKEN`: gateway token for token auth mode (secret).
|
| 163 |
- `OPENCLAW_GATEWAY_PASSWORD`: gateway password for password auth mode (secret).
|
| 164 |
- `space_name`: Hugging Face Space name; final repo ID becomes `<hf_username>/<space_name>`.
|
| 165 |
- `dataset_name`: Hugging Face Dataset name for backup; final repo ID becomes `<hf_username>/<dataset_name>`.
|
| 166 |
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
```bash
|
| 174 |
hf auth whoami
|
|
@@ -181,10 +251,18 @@ hf auth login --token <HF_WRITE_TOKEN>
|
|
| 181 |
hf auth whoami
|
| 182 |
```
|
| 183 |
|
| 184 |
-
|
| 185 |
|
| 186 |
```bash
|
| 187 |
-
HF_TOKEN="$(cat "$
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
```
|
| 189 |
|
| 190 |
Derive `hf_username`, `space_repo_id`, and `dataset_repo_id` from HF CLI login + user-provided names (do not ask user for full repo IDs):
|
|
@@ -204,8 +282,10 @@ cd openclaw-hf
|
|
| 204 |
|
| 205 |
Use values to set:
|
| 206 |
|
| 207 |
-
-
|
| 208 |
-
-
|
|
|
|
|
|
|
| 209 |
|
| 210 |
Agent must also set these additional variables:
|
| 211 |
|
|
@@ -233,7 +313,24 @@ Then configure the Space Settings (`Variables and secrets`) using all values abo
|
|
| 233 |
|
| 234 |
### 5. Final user hand-off (required)
|
| 235 |
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
Build URLs from `space_repo_id`:
|
| 239 |
|
|
|
|
| 29 |
- `scripts/openclaw-restore.sh`: Startup restore entrypoint
|
| 30 |
- `scripts/bootstrap-hf.sh`: Helper script to create a Space + Dataset
|
| 31 |
- `tests/test_backup.py`: Unit tests for the backup module
|
| 32 |
+
- `tests/test_entrypoint_config.py`: Unit tests for gateway config generation behavior
|
| 33 |
|
| 34 |
## Required Variables (Space Settings)
|
| 35 |
|
| 36 |
In your Hugging Face Space (`Settings -> Variables and secrets`), configure at least:
|
| 37 |
|
|
|
|
|
|
|
| 38 |
- Variable: `OPENCLAW_BACKUP_DATASET_REPO`: Backup target Dataset in `username/dataset-name` format
|
| 39 |
- Secret: `HF_TOKEN`: Used to write backups to the Dataset (must have write permission to that Dataset)
|
| 40 |
- Secret: `OPENCLAW_GATEWAY_TOKEN`: Fixed gateway token
|
| 41 |
- Secret: `OPENCLAW_GATEWAY_PASSWORD`: Gateway password (optional)
|
| 42 |
|
| 43 |
+
## Optional LLM Variables (All-Or-None)
|
| 44 |
+
|
| 45 |
+
Set all of these together only when you want OpenClaw to preconfigure a custom third-party model:
|
| 46 |
+
|
| 47 |
+
- Variable: `OPENCLAW_LLM_BASE_URL`: Third-party base URL (for example OpenAI-compatible `/v1`)
|
| 48 |
+
- Variable: `OPENCLAW_LLM_MODEL`: Third-party model ID
|
| 49 |
+
- Secret: `OPENCLAW_LLM_API_KEY`: Third-party API key
|
| 50 |
+
|
| 51 |
+
If any of the three is missing, entrypoint skips custom model generation.
|
| 52 |
+
In that case, you can still configure from inside the container (for example via `sshx`).
|
| 53 |
+
|
| 54 |
## Common Optional Variables
|
| 55 |
|
| 56 |
+
- `OPENCLAW_LLM_MODEL` (unset by default; used only when custom model preconfiguration is enabled)
|
| 57 |
- `OPENCLAW_LLM_PROVIDER` (default: `thirdparty`)
|
| 58 |
- `OPENCLAW_LLM_API` (default: `openai-completions`)
|
| 59 |
- `OPENCLAW_STATE_DIR` (default: `/home/ubuntu/.openclaw`)
|
|
|
|
| 92 |
Copy and send to your agent:
|
| 93 |
|
| 94 |
```
|
| 95 |
+
Please deploy OpenClaw to Hugging Face by strictly following the "Agent Deployment SOP (Collapsed)" section in https://github.com/tenfyzhong/openclaw-hf/blob/main/README.md.
|
| 96 |
```
|
| 97 |
|
| 98 |
## Agent Deployment SOP (Collapsed)
|
|
|
|
| 140 |
|
| 141 |
### 2. Collect required inputs from user first
|
| 142 |
|
| 143 |
+
Agent must ask the user for inputs in one consolidated message, then wait for response.
|
| 144 |
+
|
| 145 |
+
Required user inputs:
|
| 146 |
|
|
|
|
|
|
|
|
|
|
| 147 |
- `OPENCLAW_GATEWAY_TOKEN`
|
| 148 |
- `OPENCLAW_GATEWAY_PASSWORD`
|
| 149 |
- `space_name`
|
| 150 |
- `dataset_name`
|
| 151 |
+
- `hf_auth_mode`: choose one of:
|
| 152 |
+
- `current-login`: use current local `hf auth` login session
|
| 153 |
+
- `new-token`: use a user-provided `HF_TOKEN` for this deployment
|
| 154 |
+
|
| 155 |
+
Conditional required user input:
|
| 156 |
+
|
| 157 |
+
- `HF_TOKEN` (secret), required only when `hf_auth_mode=new-token`.
|
| 158 |
+
Agent must explain that it will backup the current local HF token first, use this new token for deployment, then restore the previous local token after deployment.
|
| 159 |
|
| 160 |
+
Optional user inputs (all-or-none, only for preconfiguring a custom model):
|
| 161 |
+
|
| 162 |
+
- `OPENCLAW_LLM_BASE_URL`
|
| 163 |
+
- `OPENCLAW_LLM_MODEL`
|
| 164 |
+
- `OPENCLAW_LLM_API_KEY`
|
| 165 |
+
|
| 166 |
+
If user skips the optional LLM triplet, agent must continue without custom model preconfiguration and ask:
|
| 167 |
+
|
| 168 |
+
- Whether to set `OPENCLAW_SSHX_AUTO_START=true` so user can configure LLM via `sshx` after startup.
|
| 169 |
+
|
| 170 |
+
When asking for missing values, agent must only ask for missing required keys. The optional LLM triplet above should be offered as an all-or-none option, not treated as required.
|
| 171 |
|
| 172 |
Before asking for values, agent must pick explanation language from runtime context:
|
| 173 |
|
|
|
|
| 183 |
|
| 184 |
Agent must explain what each variable is used for in the selected language when asking user to provide values:
|
| 185 |
|
| 186 |
+
- `hf_auth_mode`: whether to deploy with current HF login (`current-login`) or a new token (`new-token`).
|
| 187 |
+
- `HF_TOKEN` (conditional): HF token used only when `hf_auth_mode=new-token`.
|
| 188 |
+
- `OPENCLAW_LLM_BASE_URL` (optional): LLM API base URL endpoint.
|
| 189 |
+
- `OPENCLAW_LLM_MODEL` (optional): LLM model ID used by OpenClaw.
|
| 190 |
+
- `OPENCLAW_LLM_API_KEY` (optional): LLM provider API key (secret).
|
| 191 |
- `OPENCLAW_GATEWAY_TOKEN`: gateway token for token auth mode (secret).
|
| 192 |
- `OPENCLAW_GATEWAY_PASSWORD`: gateway password for password auth mode (secret).
|
| 193 |
- `space_name`: Hugging Face Space name; final repo ID becomes `<hf_username>/<space_name>`.
|
| 194 |
- `dataset_name`: Hugging Face Dataset name for backup; final repo ID becomes `<hf_username>/<dataset_name>`.
|
| 195 |
|
| 196 |
+
Recommended question template:
|
| 197 |
+
|
| 198 |
+
```text
|
| 199 |
+
Before deployment, choose HF auth mode:
|
| 200 |
+
- current-login: use your current local hf login account
|
| 201 |
+
- new-token: use a new HF_TOKEN for this deployment (I will backup local token first and restore it after deployment)
|
| 202 |
|
| 203 |
+
Then provide these required values:
|
| 204 |
+
- OPENCLAW_GATEWAY_TOKEN: gateway token for token auth mode (secret)
|
| 205 |
+
- OPENCLAW_GATEWAY_PASSWORD: gateway password for password auth mode (secret)
|
| 206 |
+
- space_name: Space name (final repo: <hf_username>/<space_name>)
|
| 207 |
+
- dataset_name: Dataset name (final repo: <hf_username>/<dataset_name>)
|
| 208 |
|
| 209 |
+
If you choose new-token mode, also provide:
|
| 210 |
+
- HF_TOKEN: token with write access to the target Space and Dataset
|
| 211 |
+
|
| 212 |
+
Optional (provide all 3 together only if you want custom LLM preconfiguration now):
|
| 213 |
+
- OPENCLAW_LLM_BASE_URL
|
| 214 |
+
- OPENCLAW_LLM_MODEL
|
| 215 |
+
- OPENCLAW_LLM_API_KEY
|
| 216 |
+
|
| 217 |
+
If you skip optional LLM preconfiguration, do you want me to set OPENCLAW_SSHX_AUTO_START=true so you can configure via sshx later?
|
| 218 |
+
```
|
| 219 |
+
|
| 220 |
+
On follow-up turns, agent must ask only for missing required keys (do not repeatedly ask for values already provided).
|
| 221 |
+
|
| 222 |
+
### 3. Backup HF token, authenticate according to mode, derive IDs, and clone repo
|
| 223 |
+
|
| 224 |
+
Before switching auth context, backup current local HF token state:
|
| 225 |
+
|
| 226 |
+
```bash
|
| 227 |
+
HF_TOKEN_FILE="$HOME/.cache/huggingface/token"
|
| 228 |
+
HF_TOKEN_BACKUP_FILE="$HOME/.cache/huggingface/token.openclaw.backup"
|
| 229 |
+
HF_TOKEN_BACKUP_STATE_FILE="$HOME/.cache/huggingface/token.openclaw.backup.state"
|
| 230 |
+
|
| 231 |
+
mkdir -p "$(dirname "$HF_TOKEN_FILE")"
|
| 232 |
+
if [ -f "$HF_TOKEN_FILE" ]; then
|
| 233 |
+
cp "$HF_TOKEN_FILE" "$HF_TOKEN_BACKUP_FILE"
|
| 234 |
+
printf 'present\n' > "$HF_TOKEN_BACKUP_STATE_FILE"
|
| 235 |
+
else
|
| 236 |
+
rm -f "$HF_TOKEN_BACKUP_FILE"
|
| 237 |
+
printf 'absent\n' > "$HF_TOKEN_BACKUP_STATE_FILE"
|
| 238 |
+
fi
|
| 239 |
+
```
|
| 240 |
+
|
| 241 |
+
If `hf_auth_mode=current-login`:
|
| 242 |
|
| 243 |
```bash
|
| 244 |
hf auth whoami
|
|
|
|
| 251 |
hf auth whoami
|
| 252 |
```
|
| 253 |
|
| 254 |
+
Then derive `HF_TOKEN` from local HF CLI credentials:
|
| 255 |
|
| 256 |
```bash
|
| 257 |
+
HF_TOKEN="$(cat "$HF_TOKEN_FILE")"
|
| 258 |
+
```
|
| 259 |
+
|
| 260 |
+
If `hf_auth_mode=new-token`:
|
| 261 |
+
|
| 262 |
+
```bash
|
| 263 |
+
HF_TOKEN="<USER_PROVIDED_HF_TOKEN>"
|
| 264 |
+
hf auth login --token "$HF_TOKEN"
|
| 265 |
+
hf auth whoami
|
| 266 |
```
|
| 267 |
|
| 268 |
Derive `hf_username`, `space_repo_id`, and `dataset_repo_id` from HF CLI login + user-provided names (do not ask user for full repo IDs):
|
|
|
|
| 282 |
|
| 283 |
Use values to set:
|
| 284 |
|
| 285 |
+
- Required secrets: `OPENCLAW_GATEWAY_TOKEN`, `OPENCLAW_GATEWAY_PASSWORD`, `HF_TOKEN` (from selected auth mode)
|
| 286 |
+
- Required variables: `OPENCLAW_BACKUP_DATASET_REPO`
|
| 287 |
+
- Optional LLM values (set together): `OPENCLAW_LLM_BASE_URL`, `OPENCLAW_LLM_MODEL`, `OPENCLAW_LLM_API_KEY`
|
| 288 |
+
- Optional access helper: `OPENCLAW_SSHX_AUTO_START=true` (recommended when skipping LLM preconfiguration and you plan to configure via `sshx`)
|
| 289 |
|
| 290 |
Agent must also set these additional variables:
|
| 291 |
|
|
|
|
| 313 |
|
| 314 |
### 5. Final user hand-off (required)
|
| 315 |
|
| 316 |
+
Before final response, restore local HF token state from backup:
|
| 317 |
+
|
| 318 |
+
```bash
|
| 319 |
+
HF_TOKEN_FILE="$HOME/.cache/huggingface/token"
|
| 320 |
+
HF_TOKEN_BACKUP_FILE="$HOME/.cache/huggingface/token.openclaw.backup"
|
| 321 |
+
HF_TOKEN_BACKUP_STATE_FILE="$HOME/.cache/huggingface/token.openclaw.backup.state"
|
| 322 |
+
|
| 323 |
+
if [ -f "$HF_TOKEN_BACKUP_STATE_FILE" ]; then
|
| 324 |
+
if [ "$(cat "$HF_TOKEN_BACKUP_STATE_FILE")" = "present" ] && [ -f "$HF_TOKEN_BACKUP_FILE" ]; then
|
| 325 |
+
cp "$HF_TOKEN_BACKUP_FILE" "$HF_TOKEN_FILE"
|
| 326 |
+
else
|
| 327 |
+
rm -f "$HF_TOKEN_FILE"
|
| 328 |
+
fi
|
| 329 |
+
rm -f "$HF_TOKEN_BACKUP_FILE" "$HF_TOKEN_BACKUP_STATE_FILE"
|
| 330 |
+
fi
|
| 331 |
+
```
|
| 332 |
+
|
| 333 |
+
After deployment is complete and local HF token state is restored, agent must show final access URLs and remind the user about keep-alive.
|
| 334 |
|
| 335 |
Build URLs from `space_repo_id`:
|
| 336 |
|
scripts/bootstrap-hf.sh
CHANGED
|
@@ -20,13 +20,14 @@ echo "visibility: private/private"
|
|
| 20 |
echo
|
| 21 |
echo "Set these Space variables/secrets:"
|
| 22 |
echo " OPENCLAW_BACKUP_DATASET_REPO=$DATASET_REPO_ID"
|
| 23 |
-
echo " OPENCLAW_LLM_BASE_URL=<your third-party base url>"
|
| 24 |
-
echo "
|
|
|
|
| 25 |
echo " OPENCLAW_GATEWAY_TOKEN=<gateway token>"
|
| 26 |
echo " OPENCLAW_GATEWAY_PASSWORD=<gateway password>"
|
| 27 |
echo " OPENCLAW_GATEWAY_AUTH_MODE=token|password (default token)"
|
| 28 |
echo " OPENCLAW_GATEWAY_CONTROLUI_DANGEROUSLY_DISABLE_DEVICE_AUTH=true|false (default false)"
|
| 29 |
-
echo " OPENCLAW_SSHX_AUTO_START=true|false (default false)"
|
| 30 |
echo " HF_TOKEN=<token with write access to dataset>"
|
| 31 |
|
| 32 |
echo
|
|
|
|
| 20 |
echo
|
| 21 |
echo "Set these Space variables/secrets:"
|
| 22 |
echo " OPENCLAW_BACKUP_DATASET_REPO=$DATASET_REPO_ID"
|
| 23 |
+
echo " OPENCLAW_LLM_BASE_URL=<your third-party base url> (optional, set together with model+api key)"
|
| 24 |
+
echo " OPENCLAW_LLM_MODEL=<your third-party model id> (optional, set together with base url+api key)"
|
| 25 |
+
echo " OPENCLAW_LLM_API_KEY=<your third-party api key> (optional, set together with base url+model)"
|
| 26 |
echo " OPENCLAW_GATEWAY_TOKEN=<gateway token>"
|
| 27 |
echo " OPENCLAW_GATEWAY_PASSWORD=<gateway password>"
|
| 28 |
echo " OPENCLAW_GATEWAY_AUTH_MODE=token|password (default token)"
|
| 29 |
echo " OPENCLAW_GATEWAY_CONTROLUI_DANGEROUSLY_DISABLE_DEVICE_AUTH=true|false (default false)"
|
| 30 |
+
echo " OPENCLAW_SSHX_AUTO_START=true|false (default false; set true if you want to configure LLM via sshx)"
|
| 31 |
echo " HF_TOKEN=<token with write access to dataset>"
|
| 32 |
|
| 33 |
echo
|
scripts/openclaw-entrypoint.sh
CHANGED
|
@@ -12,7 +12,7 @@ OPENCLAW_GATEWAY_PORT="${OPENCLAW_GATEWAY_PORT:-7860}"
|
|
| 12 |
OPENCLAW_INIT_GATEWAY_MODE="${OPENCLAW_INIT_GATEWAY_MODE:-local}"
|
| 13 |
OPENCLAW_GATEWAY_AUTH_MODE="${OPENCLAW_GATEWAY_AUTH_MODE:-token}"
|
| 14 |
OPENCLAW_LLM_PROVIDER="${OPENCLAW_LLM_PROVIDER:-thirdparty}"
|
| 15 |
-
OPENCLAW_LLM_MODEL="${OPENCLAW_LLM_MODEL:-
|
| 16 |
OPENCLAW_LLM_API="${OPENCLAW_LLM_API:-openai-completions}"
|
| 17 |
OPENCLAW_LLM_BASE_URL_ENV="${OPENCLAW_LLM_BASE_URL_ENV:-OPENCLAW_LLM_BASE_URL}"
|
| 18 |
OPENCLAW_LLM_API_KEY_ENV="${OPENCLAW_LLM_API_KEY_ENV:-OPENCLAW_LLM_API_KEY}"
|
|
@@ -68,7 +68,7 @@ fix_permissions() {
|
|
| 68 |
chown "$OPENCLAW_USER:$OPENCLAW_GROUP" "$OPENCLAW_HOME" "$OPENCLAW_STATE_DIR" "$OPENCLAW_WORKSPACE_DIR"
|
| 69 |
chown "$OPENCLAW_USER:$OPENCLAW_GROUP" "$OPENCLAW_STDOUT_LOG_PATH" "$OPENCLAW_STDERR_LOG_PATH"
|
| 70 |
chown "$OPENCLAW_USER:$OPENCLAW_GROUP" "$(dirname "$OPENCLAW_STDOUT_LOG_PATH")" "$(dirname "$OPENCLAW_STDERR_LOG_PATH")"
|
| 71 |
-
find "$
|
| 72 |
}
|
| 73 |
|
| 74 |
write_or_update_config() {
|
|
@@ -121,6 +121,7 @@ PY
|
|
| 121 |
python3 - <<'PY'
|
| 122 |
import json
|
| 123 |
import os
|
|
|
|
| 124 |
from pathlib import Path
|
| 125 |
|
| 126 |
config_path = Path(os.environ["OPENCLAW_CONFIG_PATH"])
|
|
@@ -132,11 +133,40 @@ if config_path.exists():
|
|
| 132 |
config = {}
|
| 133 |
|
| 134 |
provider = os.environ.get("OPENCLAW_LLM_PROVIDER", "thirdparty").strip() or "thirdparty"
|
| 135 |
-
model = os.environ.get("OPENCLAW_LLM_MODEL"
|
| 136 |
-
model_ref = f"{provider}/{model}"
|
| 137 |
api = os.environ.get("OPENCLAW_LLM_API", "openai-completions").strip() or "openai-completions"
|
| 138 |
base_env_name = os.environ.get("OPENCLAW_LLM_BASE_URL_ENV", "OPENCLAW_LLM_BASE_URL").strip() or "OPENCLAW_LLM_BASE_URL"
|
| 139 |
key_env_name = os.environ.get("OPENCLAW_LLM_API_KEY_ENV", "OPENCLAW_LLM_API_KEY").strip() or "OPENCLAW_LLM_API_KEY"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
fallback_allowed_origins = [f"http://127.0.0.1:{os.environ.get('OPENCLAW_GATEWAY_PORT', '7860')}"]
|
| 142 |
allowed_origins = fallback_allowed_origins
|
|
@@ -221,32 +251,34 @@ if not isinstance(agents, dict):
|
|
| 221 |
defaults = agents.get("defaults")
|
| 222 |
if not isinstance(defaults, dict):
|
| 223 |
defaults = {}
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
allow_models
|
| 229 |
-
|
|
|
|
| 230 |
agents["defaults"] = defaults
|
| 231 |
config["agents"] = agents
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
models_cfg
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
provider_cfg
|
| 244 |
-
provider_cfg["
|
| 245 |
-
provider_cfg["
|
| 246 |
-
provider_cfg["
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
|
|
|
| 250 |
|
| 251 |
config_path.parent.mkdir(parents=True, exist_ok=True)
|
| 252 |
config_path.write_text(json.dumps(config, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
|
|
|
|
| 12 |
OPENCLAW_INIT_GATEWAY_MODE="${OPENCLAW_INIT_GATEWAY_MODE:-local}"
|
| 13 |
OPENCLAW_GATEWAY_AUTH_MODE="${OPENCLAW_GATEWAY_AUTH_MODE:-token}"
|
| 14 |
OPENCLAW_LLM_PROVIDER="${OPENCLAW_LLM_PROVIDER:-thirdparty}"
|
| 15 |
+
OPENCLAW_LLM_MODEL="${OPENCLAW_LLM_MODEL:-}"
|
| 16 |
OPENCLAW_LLM_API="${OPENCLAW_LLM_API:-openai-completions}"
|
| 17 |
OPENCLAW_LLM_BASE_URL_ENV="${OPENCLAW_LLM_BASE_URL_ENV:-OPENCLAW_LLM_BASE_URL}"
|
| 18 |
OPENCLAW_LLM_API_KEY_ENV="${OPENCLAW_LLM_API_KEY_ENV:-OPENCLAW_LLM_API_KEY}"
|
|
|
|
| 68 |
chown "$OPENCLAW_USER:$OPENCLAW_GROUP" "$OPENCLAW_HOME" "$OPENCLAW_STATE_DIR" "$OPENCLAW_WORKSPACE_DIR"
|
| 69 |
chown "$OPENCLAW_USER:$OPENCLAW_GROUP" "$OPENCLAW_STDOUT_LOG_PATH" "$OPENCLAW_STDERR_LOG_PATH"
|
| 70 |
chown "$OPENCLAW_USER:$OPENCLAW_GROUP" "$(dirname "$OPENCLAW_STDOUT_LOG_PATH")" "$(dirname "$OPENCLAW_STDERR_LOG_PATH")"
|
| 71 |
+
find "$OPENCLAW_HOME" -xdev -exec chown "$OPENCLAW_USER:$OPENCLAW_GROUP" {} +
|
| 72 |
}
|
| 73 |
|
| 74 |
write_or_update_config() {
|
|
|
|
| 121 |
python3 - <<'PY'
|
| 122 |
import json
|
| 123 |
import os
|
| 124 |
+
import sys
|
| 125 |
from pathlib import Path
|
| 126 |
|
| 127 |
config_path = Path(os.environ["OPENCLAW_CONFIG_PATH"])
|
|
|
|
| 133 |
config = {}
|
| 134 |
|
| 135 |
provider = os.environ.get("OPENCLAW_LLM_PROVIDER", "thirdparty").strip() or "thirdparty"
|
| 136 |
+
model = (os.environ.get("OPENCLAW_LLM_MODEL") or "").strip()
|
|
|
|
| 137 |
api = os.environ.get("OPENCLAW_LLM_API", "openai-completions").strip() or "openai-completions"
|
| 138 |
base_env_name = os.environ.get("OPENCLAW_LLM_BASE_URL_ENV", "OPENCLAW_LLM_BASE_URL").strip() or "OPENCLAW_LLM_BASE_URL"
|
| 139 |
key_env_name = os.environ.get("OPENCLAW_LLM_API_KEY_ENV", "OPENCLAW_LLM_API_KEY").strip() or "OPENCLAW_LLM_API_KEY"
|
| 140 |
+
base_url_value = (os.environ.get(base_env_name) or "").strip()
|
| 141 |
+
api_key_value = (os.environ.get(key_env_name) or "").strip()
|
| 142 |
+
|
| 143 |
+
missing_llm_env = []
|
| 144 |
+
if not model:
|
| 145 |
+
missing_llm_env.append("OPENCLAW_LLM_MODEL")
|
| 146 |
+
if not base_url_value:
|
| 147 |
+
missing_llm_env.append(base_env_name)
|
| 148 |
+
if not api_key_value:
|
| 149 |
+
missing_llm_env.append(key_env_name)
|
| 150 |
+
custom_llm_ready = not missing_llm_env
|
| 151 |
+
model_ref = f"{provider}/{model}" if custom_llm_ready else ""
|
| 152 |
+
|
| 153 |
+
if missing_llm_env:
|
| 154 |
+
print(
|
| 155 |
+
"openclaw: skip custom LLM model configuration because missing: "
|
| 156 |
+
+ ", ".join(missing_llm_env),
|
| 157 |
+
file=sys.stderr,
|
| 158 |
+
)
|
| 159 |
+
sshx_auto_start = (os.environ.get("OPENCLAW_SSHX_AUTO_START") or "false").strip().lower() in {
|
| 160 |
+
"1",
|
| 161 |
+
"true",
|
| 162 |
+
"yes",
|
| 163 |
+
"on",
|
| 164 |
+
}
|
| 165 |
+
if not sshx_auto_start:
|
| 166 |
+
print(
|
| 167 |
+
"openclaw: if you want to configure LLM via sshx, consider setting OPENCLAW_SSHX_AUTO_START=true",
|
| 168 |
+
file=sys.stderr,
|
| 169 |
+
)
|
| 170 |
|
| 171 |
fallback_allowed_origins = [f"http://127.0.0.1:{os.environ.get('OPENCLAW_GATEWAY_PORT', '7860')}"]
|
| 172 |
allowed_origins = fallback_allowed_origins
|
|
|
|
| 251 |
defaults = agents.get("defaults")
|
| 252 |
if not isinstance(defaults, dict):
|
| 253 |
defaults = {}
|
| 254 |
+
if custom_llm_ready:
|
| 255 |
+
defaults["model"] = {"primary": model_ref}
|
| 256 |
+
allow_models = defaults.get("models")
|
| 257 |
+
if not isinstance(allow_models, dict):
|
| 258 |
+
allow_models = {}
|
| 259 |
+
allow_models.setdefault(model_ref, {"alias": "Default"})
|
| 260 |
+
defaults["models"] = allow_models
|
| 261 |
agents["defaults"] = defaults
|
| 262 |
config["agents"] = agents
|
| 263 |
|
| 264 |
+
if custom_llm_ready:
|
| 265 |
+
models_cfg = config.get("models")
|
| 266 |
+
if not isinstance(models_cfg, dict):
|
| 267 |
+
models_cfg = {}
|
| 268 |
+
models_cfg["mode"] = "merge"
|
| 269 |
+
providers = models_cfg.get("providers")
|
| 270 |
+
if not isinstance(providers, dict):
|
| 271 |
+
providers = {}
|
| 272 |
+
provider_cfg = providers.get(provider)
|
| 273 |
+
if not isinstance(provider_cfg, dict):
|
| 274 |
+
provider_cfg = {}
|
| 275 |
+
provider_cfg["baseUrl"] = f"${{{base_env_name}}}"
|
| 276 |
+
provider_cfg["apiKey"] = f"${{{key_env_name}}}"
|
| 277 |
+
provider_cfg["api"] = api
|
| 278 |
+
provider_cfg["models"] = [{"id": model, "name": model}]
|
| 279 |
+
providers[provider] = provider_cfg
|
| 280 |
+
models_cfg["providers"] = providers
|
| 281 |
+
config["models"] = models_cfg
|
| 282 |
|
| 283 |
config_path.parent.mkdir(parents=True, exist_ok=True)
|
| 284 |
config_path.write_text(json.dumps(config, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
|
tests/test_entrypoint_config.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import subprocess
|
| 4 |
+
import tempfile
|
| 5 |
+
import unittest
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class EntrypointConfigTests(unittest.TestCase):
|
| 10 |
+
@staticmethod
|
| 11 |
+
def _repo_root() -> Path:
|
| 12 |
+
return Path(__file__).resolve().parents[1]
|
| 13 |
+
|
| 14 |
+
@classmethod
|
| 15 |
+
def _entrypoint_path(cls) -> Path:
|
| 16 |
+
return cls._repo_root() / "scripts" / "openclaw-entrypoint.sh"
|
| 17 |
+
|
| 18 |
+
@staticmethod
|
| 19 |
+
def _write_fake_openclaw(bin_dir: Path) -> None:
|
| 20 |
+
fake_openclaw = bin_dir / "openclaw"
|
| 21 |
+
fake_openclaw.write_text("#!/usr/bin/env bash\nexit 0\n", encoding="utf-8")
|
| 22 |
+
fake_openclaw.chmod(0o755)
|
| 23 |
+
|
| 24 |
+
def _run_gateway(self, extra_env=None):
|
| 25 |
+
if extra_env is None:
|
| 26 |
+
extra_env = {}
|
| 27 |
+
|
| 28 |
+
with tempfile.TemporaryDirectory() as tmp:
|
| 29 |
+
tmp_path = Path(tmp)
|
| 30 |
+
state_dir = tmp_path / "state"
|
| 31 |
+
home_dir = tmp_path / "home"
|
| 32 |
+
workspace_dir = state_dir / "workspace"
|
| 33 |
+
logs_dir = tmp_path / "logs"
|
| 34 |
+
bin_dir = tmp_path / "bin"
|
| 35 |
+
config_path = state_dir / "openclaw.json"
|
| 36 |
+
|
| 37 |
+
state_dir.mkdir(parents=True)
|
| 38 |
+
home_dir.mkdir(parents=True)
|
| 39 |
+
workspace_dir.mkdir(parents=True)
|
| 40 |
+
logs_dir.mkdir(parents=True)
|
| 41 |
+
bin_dir.mkdir(parents=True)
|
| 42 |
+
self._write_fake_openclaw(bin_dir)
|
| 43 |
+
|
| 44 |
+
env = os.environ.copy()
|
| 45 |
+
for key in ("OPENCLAW_LLM_BASE_URL", "OPENCLAW_LLM_MODEL", "OPENCLAW_LLM_API_KEY"):
|
| 46 |
+
env.pop(key, None)
|
| 47 |
+
env.update(
|
| 48 |
+
{
|
| 49 |
+
"PATH": f"{bin_dir}:{env.get('PATH', '')}",
|
| 50 |
+
"OPENCLAW_HOME": str(home_dir),
|
| 51 |
+
"OPENCLAW_STATE_DIR": str(state_dir),
|
| 52 |
+
"OPENCLAW_CONFIG_PATH": str(config_path),
|
| 53 |
+
"OPENCLAW_WORKSPACE_DIR": str(workspace_dir),
|
| 54 |
+
"OPENCLAW_STDOUT_LOG_PATH": str(logs_dir / "gateway.stdout.log"),
|
| 55 |
+
"OPENCLAW_STDERR_LOG_PATH": str(logs_dir / "gateway.stderr.log"),
|
| 56 |
+
"OPENCLAW_BACKUP_ENABLED": "false",
|
| 57 |
+
"OPENCLAW_SSHX_AUTO_START": "false",
|
| 58 |
+
}
|
| 59 |
+
)
|
| 60 |
+
env.update(extra_env)
|
| 61 |
+
|
| 62 |
+
result = subprocess.run(
|
| 63 |
+
[str(self._entrypoint_path()), "gateway"],
|
| 64 |
+
cwd=self._repo_root(),
|
| 65 |
+
env=env,
|
| 66 |
+
capture_output=True,
|
| 67 |
+
text=True,
|
| 68 |
+
check=True,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
with config_path.open("r", encoding="utf-8") as fh:
|
| 72 |
+
config = json.load(fh)
|
| 73 |
+
|
| 74 |
+
return config, result
|
| 75 |
+
|
| 76 |
+
def test_gateway_configures_custom_model_when_llm_env_present(self):
|
| 77 |
+
config, _ = self._run_gateway(
|
| 78 |
+
{
|
| 79 |
+
"OPENCLAW_LLM_BASE_URL": "https://example.com/v1",
|
| 80 |
+
"OPENCLAW_LLM_MODEL": "demo-model",
|
| 81 |
+
"OPENCLAW_LLM_API_KEY": "demo-key",
|
| 82 |
+
}
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
self.assertEqual(config["agents"]["defaults"]["model"]["primary"], "thirdparty/demo-model")
|
| 86 |
+
self.assertEqual(
|
| 87 |
+
config["agents"]["defaults"]["models"]["thirdparty/demo-model"]["alias"],
|
| 88 |
+
"Default",
|
| 89 |
+
)
|
| 90 |
+
self.assertEqual(config["models"]["mode"], "merge")
|
| 91 |
+
self.assertEqual(config["models"]["providers"]["thirdparty"]["baseUrl"], "${OPENCLAW_LLM_BASE_URL}")
|
| 92 |
+
self.assertEqual(config["models"]["providers"]["thirdparty"]["apiKey"], "${OPENCLAW_LLM_API_KEY}")
|
| 93 |
+
self.assertEqual(config["models"]["providers"]["thirdparty"]["api"], "openai-completions")
|
| 94 |
+
self.assertEqual(
|
| 95 |
+
config["models"]["providers"]["thirdparty"]["models"],
|
| 96 |
+
[{"id": "demo-model", "name": "demo-model"}],
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
def test_gateway_skips_custom_model_when_llm_env_missing(self):
|
| 100 |
+
config, result = self._run_gateway()
|
| 101 |
+
|
| 102 |
+
defaults = config.get("agents", {}).get("defaults", {})
|
| 103 |
+
providers = config.get("models", {}).get("providers", {})
|
| 104 |
+
self.assertNotIn("model", defaults)
|
| 105 |
+
self.assertNotIn("models", defaults)
|
| 106 |
+
self.assertNotIn("thirdparty", providers)
|
| 107 |
+
self.assertIn("OPENCLAW_SSHX_AUTO_START=true", result.stderr)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
+
unittest.main()
|