Skip to content

Commit 73149a5

Browse files
authored
refactor(chat ui): replace BREV_UI_URL origin detection with explicit CHAT_UI_URL (NVIDIA#11)
1 parent 99ddafb commit 73149a5

3 files changed

Lines changed: 52 additions & 26 deletions

File tree

‎brev/welcome-ui/server.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
REPO_ROOT = os.environ.get("REPO_ROOT", os.path.join(ROOT, "..", ".."))
2121
SANDBOX_DIR = os.path.join(REPO_ROOT, "sandboxes", "nemoclaw")
2222
NEMOCLAW_IMAGE = "ghcr.io/nvidia/nemoclaw-community/sandboxes/nemoclaw:local"
23-
POLICY_FILE = os.path.join(SANDBOX_DIR, "policy.yaml")
23+
# POLICY_FILE = os.path.join(SANDBOX_DIR, "policy.yaml")
2424

2525
LOG_FILE = "/tmp/nemoclaw-sandbox-create.log"
2626
BREV_ENV_ID = os.environ.get("BREV_ENV_ID", "")
@@ -114,7 +114,7 @@ def _cleanup_existing_sandbox():
114114
pass
115115

116116

117-
def _run_sandbox_create(brev_ui_url: str):
117+
def _run_sandbox_create():
118118
"""Background thread: runs nemoclaw sandbox create and monitors until ready."""
119119
global _sandbox_state
120120

@@ -125,6 +125,8 @@ def _run_sandbox_create(brev_ui_url: str):
125125

126126
_cleanup_existing_sandbox()
127127

128+
chat_ui_url = _build_openclaw_url(token=None)
129+
128130
env = os.environ.copy()
129131
# Use `env` to inject vars into the sandbox command. Avoids the
130132
# nemoclaw -e flag which has a quoting bug that causes SSH to
@@ -135,11 +137,10 @@ def _run_sandbox_create(brev_ui_url: str):
135137
"nemoclaw", "sandbox", "create",
136138
"--name", "nemoclaw",
137139
"--from", NEMOCLAW_IMAGE,
138-
"--policy", POLICY_FILE,
139140
"--forward", "18789",
140141
"--",
141142
"env",
142-
f"BREV_UI_URL={brev_ui_url}",
143+
f"CHAT_UI_URL={chat_ui_url}",
143144
"nemoclaw-start",
144145
]
145146

@@ -264,11 +265,10 @@ def _handle_install_openclaw(self):
264265
"error": "Sandbox is already running",
265266
})
266267

267-
brev_ui_url = f"http://{self.headers.get('Host', 'localhost:8080')}"
268+
_maybe_detect_brev_id(self.headers.get("Host", ""))
268269

269270
thread = threading.Thread(
270271
target=_run_sandbox_create,
271-
args=(brev_ui_url,),
272272
daemon=True,
273273
)
274274
thread.start()

‎sandboxes/nemoclaw/README.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,21 @@ docker build -t nemoclaw sandboxes/nemoclaw/
2828
```bash
2929
nemoclaw sandbox create --from sandboxes/nemoclaw \
3030
--forward 18789 \
31-
-- nemoclaw-start
31+
-- env CHAT_UI_URL=http://127.0.0.1:18789 \
32+
nemoclaw-start
3233
```
3334

3435
The `--from <path>` flag builds the image and imports it into the cluster automatically.
3536

37+
`CHAT_UI_URL` is the URL where the chat UI will be accessed. The origin is
38+
added to `allowedOrigins` so the browser can authenticate without the slow
39+
device-pairing fallback. Examples:
40+
41+
| Environment | `CHAT_UI_URL` |
42+
|---|---|
43+
| Local | `http://127.0.0.1:18789` |
44+
| Brev | `https://187890-<id>.brevlab.com` |
45+
3646
`nemoclaw-start` then:
3747

3848
1. Substitutes `__NVIDIA_*_API_KEY__` placeholders in the bundled JS with runtime environment variables (if provided)

‎sandboxes/nemoclaw/nemoclaw-start.sh‎

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@
1010
# placeholder API keys. At startup this script substitutes the real keys from
1111
# environment variables into the bundled JS, then launches the gateway.
1212
#
13-
# Required env vars (for NVIDIA model endpoints):
13+
# Required env vars:
14+
# CHAT_UI_URL — URL where the chat UI will be accessed
15+
# (e.g. http://127.0.0.1:18789 for local,
16+
# https://187890-<id>.brevlab.com for Brev)
17+
#
18+
# Optional env vars (for NVIDIA model endpoints):
1419
# NVIDIA_INFERENCE_API_KEY — key for inference-api.nvidia.com
1520
# NVIDIA_INTEGRATE_API_KEY — key for integrate.api.nvidia.com
1621
#
1722
# Usage (env vars inlined via env command to avoid nemoclaw -e quoting bug):
1823
# nemoclaw sandbox create --name nemoclaw --from sandboxes/nemoclaw/ \
1924
# --forward 18789 \
20-
# -- env NVIDIA_INFERENCE_API_KEY=<key> \
25+
# -- env CHAT_UI_URL=http://127.0.0.1:18789 \
26+
# NVIDIA_INFERENCE_API_KEY=<key> \
2127
# NVIDIA_INTEGRATE_API_KEY=<key> \
2228
# nemoclaw-start
2329
set -euo pipefail
@@ -75,25 +81,27 @@ export NVIDIA_API_KEY=" "
7581

7682
GATEWAY_PORT=18789
7783

78-
# Derive the Brev environment ID so we can build the correct gateway origin.
79-
# BREV_UI_URL (if set) points at the *welcome UI* port, not the gateway port,
80-
# so we must always compute the gateway origin separately.
81-
if [ -z "${BREV_ENV_ID:-}" ] && [ -n "${BREV_UI_URL:-}" ]; then
82-
BREV_ENV_ID=$(echo "$BREV_UI_URL" | sed -n 's|.*//[0-9]*-\([^.]*\)\.brevlab\.com.*|\1|p')
83-
fi
84-
85-
if [ -n "${BREV_ENV_ID:-}" ]; then
86-
export OPENCLAW_ORIGIN="https://${GATEWAY_PORT}0-${BREV_ENV_ID}.brevlab.com"
87-
else
88-
export OPENCLAW_ORIGIN="http://127.0.0.1:${GATEWAY_PORT}"
84+
if [ -z "${CHAT_UI_URL:-}" ]; then
85+
echo "Error: CHAT_UI_URL environment variable is required." >&2
86+
echo "Set it to the URL where the chat UI will be accessed, e.g.:" >&2
87+
echo " Local: CHAT_UI_URL=http://127.0.0.1:18789" >&2
88+
echo " Brev: CHAT_UI_URL=https://187890-<brev-id>.brevlab.com" >&2
89+
exit 1
8990
fi
9091

9192
python3 -c "
9293
import json, os
94+
from urllib.parse import urlparse
9395
cfg = json.load(open(os.environ['HOME'] + '/.openclaw/openclaw.json'))
96+
local = 'http://127.0.0.1:${GATEWAY_PORT}'
97+
parsed = urlparse(os.environ['CHAT_UI_URL'])
98+
chat_origin = f'{parsed.scheme}://{parsed.netloc}'
99+
origins = [local]
100+
if chat_origin != local:
101+
origins.append(chat_origin)
94102
cfg['gateway']['controlUi'] = {
95103
'allowInsecureAuth': True,
96-
'allowedOrigins': [os.environ['OPENCLAW_ORIGIN']]
104+
'allowedOrigins': origins,
97105
}
98106
json.dump(cfg, open(os.environ['HOME'] + '/.openclaw/openclaw.json', 'w'), indent=2)
99107
"
@@ -115,12 +123,20 @@ nohup openclaw gateway > /tmp/gateway.log 2>&1 &
115123
CONFIG_FILE="${HOME}/.openclaw/openclaw.json"
116124
token=$(grep -o '"token"\s*:\s*"[^"]*"' "${CONFIG_FILE}" 2>/dev/null | head -1 | cut -d'"' -f4 || true)
117125

118-
echo ""
119-
echo "OpenClaw gateway starting in background."
120-
echo " Logs: /tmp/gateway.log"
126+
CHAT_UI_BASE="${CHAT_UI_URL%/}"
121127
if [ -n "${token}" ]; then
122-
echo " UI: http://127.0.0.1:18789/?token=${token}"
128+
LOCAL_URL="http://127.0.0.1:18789/?token=${token}"
129+
CHAT_URL="${CHAT_UI_BASE}/?token=${token}"
123130
else
124-
echo " UI: http://127.0.0.1:18789/"
131+
LOCAL_URL="http://127.0.0.1:18789/"
132+
CHAT_URL="${CHAT_UI_BASE}/"
133+
fi
134+
135+
echo ""
136+
echo "OpenClaw gateway starting in background."
137+
echo " Logs: /tmp/gateway.log"
138+
echo " UI: ${CHAT_URL}"
139+
if [ "${CHAT_UI_BASE}" != "http://127.0.0.1:18789" ]; then
140+
echo " Local: ${LOCAL_URL}"
125141
fi
126142
echo ""

0 commit comments

Comments
 (0)