Use OpenShell config home for host commands

This commit is contained in:
Codex
2026-04-28 16:24:51 +00:00
parent 33510bba85
commit 476d3a96bb
13 changed files with 82 additions and 97 deletions
+3 -8
View File
@@ -1,7 +1,7 @@
import { NextResponse } from "next/server"
import { execFile } from "node:child_process"
import { promisify } from "node:util"
import { HOST_PATH, OPENSHELL_BIN } from "@/app/lib/hostCommands"
import { OPENSHELL_BIN, hostCommandEnv } from "@/app/lib/hostCommands"
const execFileAsync = promisify(execFile)
@@ -21,15 +21,10 @@ function stripAnsi(value: string) {
async function runOpenShell(args: string[], extraEnv: Record<string, string> = {}) {
const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, {
env: {
...process.env,
env: hostCommandEnv({
...extraEnv,
PATH: HOST_PATH,
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
},
}),
timeout: 60000,
maxBuffer: 20 * 1024 * 1024,
})
+6 -18
View File
@@ -12,6 +12,7 @@ import {
NEMOCLAW_SETUP_CANDIDATES,
NODE_BIN,
OPENSHELL_BIN,
hostCommandEnv,
} from "@/app/lib/hostCommands"
const execFileAsync = promisify(execFile)
@@ -240,14 +241,9 @@ async function approveOpenClawDeviceRequests(sandboxName: string) {
"sh",
"-lc",
"openclaw devices approve --latest --json --timeout 10000",
], {
...process.env,
PATH: HOST_PATH,
], hostCommandEnv({
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
}, 15000)
}), 15000)
const combinedOutput = `${result.stdout}\n${result.stderr}`.trim()
const noPending = NO_PENDING_DEVICE_REQUESTS.test(combinedOutput)
@@ -339,15 +335,13 @@ export async function POST(request: Request) {
if (blueprint === "nemoclaw-blueprint") {
const createCommand = buildNemoClawCreateCommand()
const env: NodeJS.ProcessEnv = {
...process.env,
PATH: HOST_PATH,
const env: NodeJS.ProcessEnv = hostCommandEnv({
NEMOCLAW_SANDBOX_NAME: sandboxName,
NEMOCLAW_NON_INTERACTIVE: "1",
NEMOCLAW_RECREATE_SANDBOX: "1",
NEMOCLAW_ACCEPT_THIRD_PARTY_SOFTWARE: "1",
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
}
})
if (!enableTailscale) {
env.NVIDIA_API_KEY = env.NVIDIA_API_KEY || "optional-local-mode"
@@ -414,13 +408,7 @@ export async function POST(request: Request) {
}
if (blueprint === "custom-sandbox") {
const env: NodeJS.ProcessEnv = {
...process.env,
PATH: HOST_PATH,
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
}
const env: NodeJS.ProcessEnv = hostCommandEnv()
const createAttempt = await runCreateCommandBounded(OPENSHELL_BIN, ["sandbox", "create", "--name", sandboxName], env, 15000)
if (createAttempt.completed && createAttempt.exitCode !== 0) {
+5 -15
View File
@@ -2,7 +2,7 @@ import { NextResponse } from "next/server"
import { execFile } from "node:child_process"
import { promisify } from "node:util"
import { resolveSandboxRef } from "@/app/lib/openshellHost"
import { HOST_PATH, NEMOCLAW_BIN, NEMOCLAW_CWD, OPENSHELL_BIN } from "@/app/lib/hostCommands"
import { NEMOCLAW_BIN, NEMOCLAW_CWD, OPENSHELL_BIN, hostCommandEnv } from "@/app/lib/hostCommands"
const execFileAsync = promisify(execFile)
@@ -54,14 +54,9 @@ async function deleteSandbox(sandboxName: string) {
console.log(`[sandbox/delete] command:start sandbox=${sandboxName}`)
try {
const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, ["sandbox", "delete", sandboxName], {
env: {
...process.env,
PATH: HOST_PATH,
env: hostCommandEnv({
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
},
}),
timeout: 60000,
maxBuffer: 20 * 1024 * 1024,
})
@@ -93,14 +88,9 @@ async function cleanupNemoClawSandbox(sandboxName: string) {
try {
const { stdout, stderr } = await execFileAsync(NEMOCLAW_BIN, [sandboxName, "destroy", "--yes"], {
cwd: NEMOCLAW_CWD,
env: {
...process.env,
PATH: HOST_PATH,
env: hostCommandEnv({
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
},
}),
timeout: 90000,
maxBuffer: 20 * 1024 * 1024,
})
+2 -8
View File
@@ -2,7 +2,7 @@ import { NextResponse } from "next/server"
import { execFile } from "node:child_process"
import { hostname, networkInterfaces } from "node:os"
import { promisify } from "node:util"
import { HOST_PATH, NEMOCLAW_BIN, NODE_BIN, OPENSHELL_BIN } from "@/app/lib/hostCommands"
import { HOST_PATH, NEMOCLAW_BIN, NODE_BIN, OPENSHELL_BIN, hostCommandEnv } from "@/app/lib/hostCommands"
import { resolveRuntimeAuthority } from "@/app/lib/runtimeAuthority"
const execFileAsync = promisify(execFile)
@@ -164,13 +164,7 @@ async function execNemoclaw(args: string[]) {
async function execOpenShell(args: string[]) {
const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, {
env: {
...process.env,
PATH: HOST_PATH,
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
},
env: hostCommandEnv(),
})
return { stdout, stderr }
+47 -7
View File
@@ -11,6 +11,11 @@ function unique(values: Array<string | undefined>) {
return Array.from(new Set(values.filter((value): value is string => Boolean(value))))
}
function inferHomeFromLocalBin(commandPath: string) {
const suffix = "/.local/bin/openshell"
return commandPath.endsWith(suffix) ? commandPath.slice(0, -suffix.length) : undefined
}
function safeIsDirectory(candidate: string) {
try {
return statSync(candidate).isDirectory()
@@ -51,11 +56,40 @@ function discoverHomeFiles(relativePath: string) {
return found
}
const OPENSHELL_BIN_CANDIDATES = unique([
process.env.OPENSHELL_BIN,
HOME ? path.join(HOME, ".local/bin/openshell") : undefined,
"/opt/homebrew/bin/openshell",
"/usr/local/bin/openshell",
])
export const OPENSHELL_BIN = firstExisting(OPENSHELL_BIN_CANDIDATES, "openshell")
function homeWithOpenShellConfig(candidates: Array<string | undefined>) {
return candidates
.filter((value): value is string => Boolean(value))
.find((candidate) => existsSync(path.join(candidate, ".config/openshell")))
}
const INFERRED_OPENSHELL_HOME = inferHomeFromLocalBin(OPENSHELL_BIN)
export const OPENSHELL_HOME = homeWithOpenShellConfig([
process.env.OPENSHELL_HOME,
HOME,
INFERRED_OPENSHELL_HOME,
]) || process.env.OPENSHELL_HOME || INFERRED_OPENSHELL_HOME || HOME
export const OPENSHELL_XDG_CONFIG_HOME = firstExisting([
process.env.OPENSHELL_XDG_CONFIG_HOME,
process.env.XDG_CONFIG_HOME && existsSync(path.join(process.env.XDG_CONFIG_HOME, "openshell")) ? process.env.XDG_CONFIG_HOME : undefined,
OPENSHELL_HOME ? path.join(OPENSHELL_HOME, ".config") : undefined,
], process.env.XDG_CONFIG_HOME || (OPENSHELL_HOME ? path.join(OPENSHELL_HOME, ".config") : ""))
function pathEntries() {
return [
process.env.TERMINAL_EXTRA_PATH,
process.env.OPENSHELL_CONTROL_VENV ? path.join(process.env.OPENSHELL_CONTROL_VENV, "bin") : undefined,
path.join(process.cwd(), ".venv/bin"),
OPENSHELL_HOME ? path.join(OPENSHELL_HOME, ".local/bin") : undefined,
HOME ? path.join(HOME, ".local/bin") : undefined,
HOME ? path.join(HOME, ".nvm/versions/node/v22.22.2/bin") : undefined,
HOME ? path.join(HOME, ".nvm/versions/node/v22.22.1/bin") : undefined,
@@ -73,6 +107,19 @@ function pathEntries() {
export const HOST_PATH = unique(pathEntries()).join(":")
export function hostCommandEnv(extra: Record<string, string | undefined> = {}) {
return {
...process.env,
...(OPENSHELL_HOME ? { HOME: OPENSHELL_HOME } : {}),
...(OPENSHELL_XDG_CONFIG_HOME ? { XDG_CONFIG_HOME: OPENSHELL_XDG_CONFIG_HOME } : {}),
PATH: HOST_PATH,
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
...extra,
}
}
export const NODE_BIN = firstExisting([
process.env.NODE_BIN,
HOME ? path.join(HOME, ".nvm/versions/node/v22.22.2/bin/node") : undefined,
@@ -82,13 +129,6 @@ export const NODE_BIN = firstExisting([
"/usr/bin/node",
], "node")
export const OPENSHELL_BIN = firstExisting([
process.env.OPENSHELL_BIN,
HOME ? path.join(HOME, ".local/bin/openshell") : undefined,
"/opt/homebrew/bin/openshell",
"/usr/local/bin/openshell",
], "openshell")
export const OPENCLAW_BIN = firstExisting([
process.env.OPENCLAW_BIN,
HOME ? path.join(HOME, ".local/bin/openclaw") : undefined,
+3 -8
View File
@@ -1,6 +1,6 @@
import { execFile } from "node:child_process"
import { promisify } from "node:util"
import { HOST_PATH, OPENSHELL_BIN } from "./hostCommands"
import { HOST_PATH, OPENSHELL_BIN, hostCommandEnv } from "./hostCommands"
import { resolveSandboxRef } from "./openshellHost"
const execFileAsync = promisify(execFile)
@@ -36,15 +36,10 @@ function normalizeProxyOrigin(value: string) {
async function runOpenShell(args: string[]) {
const { stdout } = await execFileAsync(OPENSHELL_BIN, args, {
env: {
...process.env,
PATH: HOST_PATH,
env: hostCommandEnv({
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
TERM: "dumb",
},
}),
timeout: 15000,
maxBuffer: 1024 * 1024,
})
+2 -8
View File
@@ -1,6 +1,6 @@
import { execFile, spawn } from "node:child_process"
import { promisify } from "node:util"
import { HOST_PATH, OPENCLAW_BIN, OPENSHELL_BIN } from "./hostCommands"
import { HOST_PATH, OPENCLAW_BIN, OPENSHELL_BIN, hostCommandEnv } from "./hostCommands"
import { getDefaultOpenClawInstance, getOpenClawDashboardPortForSandbox, resolveOpenClawInstance } from "./openclawInstances"
const execFileAsync = promisify(execFile)
@@ -44,13 +44,7 @@ export type DashboardProbe = {
export async function execOpenShell(args: string[]) {
const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, {
env: {
...process.env,
PATH: HOST_PATH,
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
},
env: hostCommandEnv(),
})
return { stdout, stderr }
}
+3 -8
View File
@@ -1,6 +1,6 @@
import { spawn } from "node:child_process"
import path from "node:path"
import { HOST_PATH, OPENSHELL_BIN } from "./hostCommands"
import { OPENSHELL_BIN, hostCommandEnv } from "./hostCommands"
import { resolveSandboxRef } from "./openshellHost"
export const MAX_FILE_BYTES = Number.parseInt(process.env.SANDBOX_FILE_TRANSFER_MAX_BYTES || String(128 * 1024 * 1024), 10)
@@ -58,14 +58,9 @@ export function assertRequestContentLength(request: Request, maxBytes = MAX_MULT
function runSandboxExec(sandboxName: string, script: string, input?: Buffer, timeoutMs = 60000) {
return new Promise<{ stdout: Buffer; stderr: string; code: number | null }>((resolve, reject) => {
const child = spawn(OPENSHELL_BIN, ["sandbox", "exec", "-n", sandboxName, "--", "sh", "-lc", script], {
env: {
...process.env,
PATH: HOST_PATH,
env: hostCommandEnv({
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
},
}),
stdio: ["pipe", "pipe", "pipe"],
})
const stdout: Buffer[] = []
+3 -8
View File
@@ -1,6 +1,6 @@
import { execFile, spawn } from "node:child_process"
import { promisify } from "node:util"
import { HOST_PATH, OPENSHELL_BIN } from "./hostCommands"
import { HOST_PATH, OPENSHELL_BIN, hostCommandEnv } from "./hostCommands"
import { getSandboxInferenceConfig, type SandboxInferenceRoute } from "./sandboxInferenceStore"
const execFileAsync = promisify(execFile)
@@ -110,14 +110,9 @@ function buildOpenClawConfig(current: any, routes: SandboxInferenceRoute[], prim
async function runOpenShell(args: string[]) {
const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, {
env: {
...process.env,
PATH: HOST_PATH,
env: hostCommandEnv({
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
},
}),
timeout: 60000,
maxBuffer: 20 * 1024 * 1024,
})
+3 -8
View File
@@ -1,6 +1,6 @@
import { execFile } from "node:child_process"
import { promisify } from "node:util"
import { HOST_PATH, OPENSHELL_BIN } from "./hostCommands"
import { OPENSHELL_BIN, hostCommandEnv } from "./hostCommands"
import { resolveSandboxRef } from "./openshellHost"
const execFileAsync = promisify(execFile)
@@ -25,15 +25,10 @@ type BrokerNetworkAction = {
function runOpenShell(args: string[], timeout = 60000) {
return execFileAsync(OPENSHELL_BIN, args, {
env: {
...process.env,
PATH: HOST_PATH,
env: hostCommandEnv({
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
TERM: "dumb",
},
}),
timeout,
maxBuffer: 10 * 1024 * 1024,
})