Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ On a fresh macOS machine, install the prerequisites in this order:
3. Run the NemoClaw installer.

This avoids the two most common first-run failures on macOS:

- missing developer tools needed by the installer and Node.js toolchain
- Docker connection errors when no supported container runtime is installed or running

Expand Down
123 changes: 91 additions & 32 deletions bin/lib/onboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,38 @@ function hasStaleGateway(gwInfoOutput) {
return typeof gwInfoOutput === "string" && gwInfoOutput.length > 0 && gwInfoOutput.includes(GATEWAY_NAME);
}

function stripAnsi(value = "") {
let cleaned = "";
for (let i = 0; i < value.length; i += 1) {
if (value.charCodeAt(i) === 27 && value[i + 1] === "[") {
i += 2;
while (i < value.length && /[0-9;]/.test(value[i])) {
i += 1;
}
if (value[i] === "m") {
continue;
}
}
cleaned += value[i] || "";
}
return cleaned;
}

function getActiveGatewayName(statusOutput = "") {
if (typeof statusOutput !== "string" || statusOutput.length === 0) {
return "";
}
const match = stripAnsi(statusOutput)
.match(/^\s*Gateway:\s+(.+?)\s*$/m);
return match ? match[1].trim() : "";
}

function isGatewayHealthy(statusOutput = "", gwInfoOutput = "") {
const connected = typeof statusOutput === "string" && statusOutput.includes("Connected");
const activeGateway = getActiveGatewayName(statusOutput);
return connected && activeGateway === GATEWAY_NAME && hasStaleGateway(gwInfoOutput);
}

function streamSandboxCreate(command, env = process.env, options = {}) {
const child = spawn("bash", ["-lc", command], {
cwd: ROOT,
Expand Down Expand Up @@ -1237,8 +1269,16 @@ async function preflight() {
// A previous onboard run may have left the gateway container and port
// forward running. If a NemoClaw-owned gateway is still present, tear
// it down so the port check below doesn't fail on our own leftovers.
const gatewayStatus = runCaptureOpenshell(["status"], { ignoreError: true });
const gwInfo = runCaptureOpenshell(["gateway", "info", "-g", GATEWAY_NAME], { ignoreError: true });
if (hasStaleGateway(gwInfo)) {
const healthyGateway = isGatewayHealthy(gatewayStatus, gwInfo);
if (healthyGateway) {
console.log(" Reusing existing NemoClaw gateway...");
runOpenshell(["forward", "stop", "18789"], { ignoreError: true });
runOpenshell(["gateway", "select", GATEWAY_NAME], { ignoreError: true });
process.env.OPENSHELL_GATEWAY = GATEWAY_NAME;
console.log(" ✓ Existing gateway selected");
} else if (hasStaleGateway(gwInfo)) {
console.log(" Cleaning up previous NemoClaw session...");
runOpenshell(["forward", "stop", "18789"], { ignoreError: true });
runOpenshell(["gateway", "destroy", "-g", GATEWAY_NAME], { ignoreError: true });
Expand All @@ -1251,6 +1291,10 @@ async function preflight() {
{ port: 18789, label: "NemoClaw dashboard" },
];
for (const { port, label } of requiredPorts) {
if (port === 8080 && healthyGateway) {
console.log(` ✓ Port ${port} already in use by active NemoClaw gateway (${label})`);
continue;
}
const portCheck = await checkPortAvailable(port);
if (!portCheck.ok) {
console.error("");
Expand Down Expand Up @@ -1305,11 +1349,21 @@ function destroyGateway() {

// ── Step 2: Gateway ──────────────────────────────────────────────

async function startGateway(_gpu) {
async function startGatewayWithOptions(_gpu, { exitOnFailure = true } = {}) {
step(3, 7, "Starting OpenShell gateway");

// Clean up any previous gateway and its Docker volumes
destroyGateway();
const gatewayStatus = runCaptureOpenshell(["status"], { ignoreError: true });
const gwInfo = runCaptureOpenshell(["gateway", "info", "-g", GATEWAY_NAME], { ignoreError: true });
if (isGatewayHealthy(gatewayStatus, gwInfo)) {
console.log(" ✓ Reusing existing gateway");
runOpenshell(["gateway", "select", GATEWAY_NAME], { ignoreError: true });
process.env.OPENSHELL_GATEWAY = GATEWAY_NAME;
return;
}

if (hasStaleGateway(gwInfo)) {
runOpenshell(["gateway", "destroy", "-g", GATEWAY_NAME], { ignoreError: true });
}

const gwArgs = ["--name", GATEWAY_NAME];
// Do NOT pass --gpu here. On DGX Spark (and most GPU hosts), inference is
Expand All @@ -1332,22 +1386,29 @@ async function startGateway(_gpu) {
if (startResult.status !== 0) {
console.error(" Gateway failed to start. Cleaning up stale state...");
destroyGateway();
console.error(" Stale state removed. Please rerun: nemoclaw onboard");
process.exit(1);
if (exitOnFailure) {
console.error(" Stale state removed. Please rerun: nemoclaw onboard");
process.exit(1);
}
throw new Error("Gateway failed to start");
}

// Verify health
for (let i = 0; i < 5; i++) {
const status = runCaptureOpenshell(["status"], { ignoreError: true });
if (status.includes("Connected")) {
const gwInfo = runCaptureOpenshell(["gateway", "info", "-g", GATEWAY_NAME], { ignoreError: true });
if (isGatewayHealthy(status, gwInfo)) {
console.log(" ✓ Gateway is healthy");
break;
}
if (i === 4) {
console.error(" Gateway health check failed. Cleaning up stale state...");
destroyGateway();
console.error(" Stale state removed. Please rerun: nemoclaw onboard");
process.exit(1);
if (exitOnFailure) {
console.error(" Stale state removed. Please rerun: nemoclaw onboard");
process.exit(1);
}
throw new Error("Gateway failed to start");
}
sleep(2);
}
Expand All @@ -1364,6 +1425,14 @@ async function startGateway(_gpu) {
process.env.OPENSHELL_GATEWAY = GATEWAY_NAME;
}

async function startGateway(_gpu) {
return startGatewayWithOptions(_gpu, { exitOnFailure: true });
}

async function startGatewayForRecovery(_gpu) {
return startGatewayWithOptions(_gpu, { exitOnFailure: false });
}

// ── Step 3: Sandbox ──────────────────────────────────────────────

async function createSandbox(gpu, model, provider, preferredInferenceApi = null) {
Expand Down Expand Up @@ -1415,6 +1484,7 @@ async function createSandbox(gpu, model, provider, preferredInferenceApi = null)
run(`cp -r "${path.join(ROOT, "nemoclaw-blueprint")}" "${buildCtx}/nemoclaw-blueprint"`);
run(`cp -r "${path.join(ROOT, "scripts")}" "${buildCtx}/scripts"`);
run(`rm -rf "${buildCtx}/nemoclaw/node_modules"`, { ignoreError: true });
run(`bash "${buildCtx}/scripts/clean-staged-tree.sh" "${buildCtx}/nemoclaw-blueprint"`, { ignoreError: true });

// Create sandbox (use -- echo to avoid dropping into interactive shell)
// Pass the base policy so sandbox starts in proxy mode (required for policy updates later)
Expand Down Expand Up @@ -1551,9 +1621,7 @@ async function setupNim(gpu) {
const options = [];
options.push({
key: "build",
label:
"NVIDIA Endpoints" +
(!ollamaRunning && !(EXPERIMENTAL && vllmRunning) ? " (recommended)" : ""),
label: "NVIDIA Endpoints",
});
options.push({ key: "openai", label: "OpenAI" });
options.push({ key: "custom", label: "Other OpenAI-compatible endpoint" });
Expand Down Expand Up @@ -2155,7 +2223,7 @@ async function setupPolicies(sandboxName) {
// ── Dashboard ────────────────────────────────────────────────────

const CONTROL_UI_PORT = 18789;
const CONTROL_UI_CHAT_PATH = "/chat?session=main";
const CONTROL_UI_PATH = "/";

function findOpenclawJsonPath(dir) {
if (!fs.existsSync(dir)) return null;
Expand Down Expand Up @@ -2201,19 +2269,9 @@ function fetchGatewayAuthTokenFromSandbox(sandboxName) {
}
}

function buildControlUiChatUrls(token) {
function buildControlUiUrl(token) {
const hash = token ? `#token=${token}` : "";
const pathChat = `${CONTROL_UI_CHAT_PATH}${hash}`;
const bases = [
`http://127.0.0.1:${CONTROL_UI_PORT}`,
`http://localhost:${CONTROL_UI_PORT}`,
];
const chatUi = (process.env.CHAT_UI_URL || "").trim().replace(/\/$/, "");
const urls = bases.map((b) => `${b}${pathChat}`);
if (chatUi && /^https?:\/\//i.test(chatUi) && !bases.includes(chatUi)) {
urls.push(`${chatUi}${pathChat}`);
}
return [...new Set(urls)];
return `http://127.0.0.1:${CONTROL_UI_PORT}${CONTROL_UI_PATH}${hash}`;
}

function printDashboard(sandboxName, model, provider, nimContainer = null) {
Expand All @@ -2239,16 +2297,13 @@ function printDashboard(sandboxName, model, provider, nimContainer = null) {
console.log(` Model ${model} (${providerLabel})`);
console.log(` NIM ${nimLabel}`);
console.log(` ${"─".repeat(50)}`);
console.log(` Next:`);
if (token) {
note(" URLs below embed the gateway token — treat them like a password.");
console.log(` Control UI: copy one line into your browser (port ${CONTROL_UI_PORT} must be forwarded):`);
for (const u of buildControlUiChatUrls(token)) {
console.log(` ${u}`);
}
console.log(" OpenClaw UI (tokenized URL; treat it like a password)");
console.log(` ${buildControlUiUrl(token)}`);
} else {
note(" Could not read gateway token from the sandbox (download failed).");
console.log(` Control UI: http://127.0.0.1:${CONTROL_UI_PORT}${CONTROL_UI_CHAT_PATH}`);
console.log(" OpenClaw UI");
console.log(` http://127.0.0.1:${CONTROL_UI_PORT}${CONTROL_UI_PATH}`);
console.log(` Token: nemoclaw ${sandboxName} connect → jq -r '.gateway.auth.token' /sandbox/.openclaw/openclaw.json`);
console.log(` append #token=<token> to the URL, or see /tmp/gateway.log inside the sandbox.`);
}
Expand Down Expand Up @@ -2297,12 +2352,16 @@ module.exports = {
getInstalledOpenshellVersion,
getStableGatewayImageRef,
hasStaleGateway,
isGatewayHealthy,
isSandboxReady,
onboard,
preflight,
pruneStaleSandboxEntry,
runCaptureOpenshell,
setupInference,
setupNim,
startGateway,
startGatewayForRecovery,
writeSandboxConfigSyncFile,
patchStagedDockerfile,
};
Loading
Loading