Stabilize OpenShell control sandbox workflows
This commit is contained in:
@@ -1,22 +1,5 @@
|
|||||||
import { NextResponse } from "next/server"
|
import { NextResponse } from "next/server"
|
||||||
import { execFile, spawn } from "node:child_process"
|
import { applySandboxInferenceProfile } from "@/app/lib/sandboxInferenceApply"
|
||||||
import { promisify } from "node:util"
|
|
||||||
import { getSandboxInferenceConfig, type SandboxInferenceRoute } from "@/app/lib/sandboxInferenceStore"
|
|
||||||
|
|
||||||
const execFileAsync = promisify(execFile)
|
|
||||||
const HOME = process.env.HOME || ""
|
|
||||||
const OPENSHELL_BIN = process.env.OPENSHELL_BIN || `${HOME}/.local/bin/openshell`
|
|
||||||
const DOCKER_BIN = process.env.DOCKER_BIN || "docker"
|
|
||||||
const OPENSHELL_CLUSTER_CONTAINER = process.env.OPENSHELL_CLUSTER_CONTAINER || "openshell-cluster-nemoclaw"
|
|
||||||
const HOST_PATH = [
|
|
||||||
`${HOME}/.local/bin`,
|
|
||||||
`${HOME}/.nvm/versions/node/v22.22.2/bin`,
|
|
||||||
`${HOME}/.nvm/versions/node/v22.22.1/bin`,
|
|
||||||
"/usr/local/bin",
|
|
||||||
"/usr/bin",
|
|
||||||
"/bin",
|
|
||||||
process.env.PATH || "",
|
|
||||||
].filter(Boolean).join(":")
|
|
||||||
|
|
||||||
function validateSandboxName(value: string) {
|
function validateSandboxName(value: string) {
|
||||||
if (!value || value.length > 63) throw new Error("sandbox name is required")
|
if (!value || value.length > 63) throw new Error("sandbox name is required")
|
||||||
@@ -24,116 +7,6 @@ function validateSandboxName(value: string) {
|
|||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
function modelEntry(route: SandboxInferenceRoute) {
|
|
||||||
return {
|
|
||||||
id: route.model,
|
|
||||||
name: route.model,
|
|
||||||
reasoning: false,
|
|
||||||
input: ["text"],
|
|
||||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
||||||
contextWindow: 131072,
|
|
||||||
maxTokens: 4096,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildOpenClawConfig(current: any, routes: SandboxInferenceRoute[], primary: SandboxInferenceRoute) {
|
|
||||||
const providers: Record<string, any> = {}
|
|
||||||
for (const route of routes.filter((item) => item.enabled)) {
|
|
||||||
providers[route.provider] ||= {
|
|
||||||
baseUrl: "https://inference.local/v1",
|
|
||||||
apiKey: "unused",
|
|
||||||
api: "openai-completions",
|
|
||||||
models: [],
|
|
||||||
}
|
|
||||||
providers[route.provider].models.push(modelEntry(route))
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
...current,
|
|
||||||
agents: {
|
|
||||||
...(current?.agents || {}),
|
|
||||||
defaults: {
|
|
||||||
...(current?.agents?.defaults || {}),
|
|
||||||
model: {
|
|
||||||
...(current?.agents?.defaults?.model || {}),
|
|
||||||
primary: primary.model,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
models: {
|
|
||||||
...(current?.models || {}),
|
|
||||||
mode: "merge",
|
|
||||||
providers,
|
|
||||||
},
|
|
||||||
channels: {
|
|
||||||
...(current?.channels || {}),
|
|
||||||
defaults: {
|
|
||||||
...(current?.channels?.defaults || {}),
|
|
||||||
configWrites: false,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function runOpenShell(args: string[]) {
|
|
||||||
const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, {
|
|
||||||
env: {
|
|
||||||
...process.env,
|
|
||||||
PATH: HOST_PATH,
|
|
||||||
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
|
|
||||||
NO_COLOR: "1",
|
|
||||||
CLICOLOR: "0",
|
|
||||||
CLICOLOR_FORCE: "0",
|
|
||||||
},
|
|
||||||
timeout: 60000,
|
|
||||||
maxBuffer: 20 * 1024 * 1024,
|
|
||||||
})
|
|
||||||
return { stdout: String(stdout).trim(), stderr: String(stderr).trim() }
|
|
||||||
}
|
|
||||||
|
|
||||||
async function runDockerKubectl(args: string[], input?: string) {
|
|
||||||
return await new Promise<{ stdout: string; stderr: string; code: number | null }>((resolve, reject) => {
|
|
||||||
const child = spawn(DOCKER_BIN, ["exec", ...(input ? ["-i"] : []), OPENSHELL_CLUSTER_CONTAINER, "kubectl", ...args], {
|
|
||||||
env: { ...process.env, PATH: HOST_PATH },
|
|
||||||
stdio: ["pipe", "pipe", "pipe"],
|
|
||||||
})
|
|
||||||
let stdout = ""
|
|
||||||
let stderr = ""
|
|
||||||
child.stdout.on("data", (chunk) => { stdout += String(chunk) })
|
|
||||||
child.stderr.on("data", (chunk) => { stderr += String(chunk) })
|
|
||||||
child.on("error", reject)
|
|
||||||
child.on("close", (code) => resolve({ stdout: stdout.trim(), stderr: stderr.trim(), code }))
|
|
||||||
if (input) child.stdin.end(input)
|
|
||||||
else child.stdin.end()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async function readCurrentOpenClawConfig(sandboxName: string) {
|
|
||||||
const result = await runDockerKubectl(["exec", "-n", "openshell", sandboxName, "--", "cat", "/sandbox/.openclaw/openclaw.json"])
|
|
||||||
if (result.code !== 0) throw new Error(result.stderr || "Failed to read OpenClaw config")
|
|
||||||
return JSON.parse(result.stdout)
|
|
||||||
}
|
|
||||||
|
|
||||||
async function writeOpenClawConfig(sandboxName: string, config: any) {
|
|
||||||
const payload = `${JSON.stringify(config, null, 2)}\n`
|
|
||||||
const script = [
|
|
||||||
"cat > /sandbox/.openclaw/openclaw.json",
|
|
||||||
"chmod 444 /sandbox/.openclaw/openclaw.json",
|
|
||||||
"chown root:root /sandbox/.openclaw/openclaw.json",
|
|
||||||
"sha256sum /sandbox/.openclaw/openclaw.json > /sandbox/.openclaw/.config-hash",
|
|
||||||
"chmod 444 /sandbox/.openclaw/.config-hash",
|
|
||||||
"chown root:root /sandbox/.openclaw/.config-hash",
|
|
||||||
].join(" && ")
|
|
||||||
const result = await runDockerKubectl(["exec", "-i", "-n", "openshell", sandboxName, "--", "sh", "-lc", script], payload)
|
|
||||||
if (result.code !== 0) throw new Error(result.stderr || "Failed to write OpenClaw config")
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
async function restartOpenClawGatewayIfRunning(sandboxName: string) {
|
|
||||||
const script = "for p in /proc/[0-9]*; do cmd=$(tr '\\0' ' ' < \"$p/cmdline\" 2>/dev/null || true); case \"$cmd\" in *'openclaw gateway'*) kill \"${p##*/}\" 2>/dev/null || true;; esac; done"
|
|
||||||
return await runDockerKubectl(["exec", "-n", "openshell", sandboxName, "--", "sh", "-lc", script])
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function POST(
|
export async function POST(
|
||||||
request: Request,
|
request: Request,
|
||||||
{ params }: { params: Promise<{ sandboxId: string }> }
|
{ params }: { params: Promise<{ sandboxId: string }> }
|
||||||
@@ -142,27 +15,15 @@ export async function POST(
|
|||||||
const { sandboxId } = await params
|
const { sandboxId } = await params
|
||||||
const body = await request.json().catch(() => ({}))
|
const body = await request.json().catch(() => ({}))
|
||||||
const sandboxName = validateSandboxName(typeof body?.sandboxName === "string" ? body.sandboxName : sandboxId)
|
const sandboxName = validateSandboxName(typeof body?.sandboxName === "string" ? body.sandboxName : sandboxId)
|
||||||
const config = await getSandboxInferenceConfig(sandboxId)
|
const result = await applySandboxInferenceProfile(sandboxId, sandboxName)
|
||||||
const enabledRoutes = config.routes.filter((route) => route.enabled)
|
|
||||||
if (enabledRoutes.length === 0) throw new Error("No enabled inference routes are configured for this sandbox")
|
|
||||||
const primary = enabledRoutes.find((route) => route.id === config.primaryRouteId) || enabledRoutes[0]
|
|
||||||
|
|
||||||
const currentOpenClawConfig = await readCurrentOpenClawConfig(sandboxName)
|
|
||||||
const nextOpenClawConfig = buildOpenClawConfig(currentOpenClawConfig, enabledRoutes, primary)
|
|
||||||
await writeOpenClawConfig(sandboxName, nextOpenClawConfig)
|
|
||||||
await restartOpenClawGatewayIfRunning(sandboxName)
|
|
||||||
|
|
||||||
const routeResult = await runOpenShell(["inference", "set", "--no-verify", "--provider", primary.provider, "--model", primary.model])
|
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
ok: true,
|
ok: true,
|
||||||
applied: true,
|
applied: true,
|
||||||
sandboxId,
|
sandboxId,
|
||||||
sandboxName,
|
sandboxName,
|
||||||
primaryRoute: primary,
|
...result,
|
||||||
routesApplied: enabledRoutes.length,
|
note: "OpenClaw config was patched with the routed inference provider and the OpenShell gateway was pointed at the primary route.",
|
||||||
gatewayRoute: routeResult,
|
|
||||||
note: "OpenClaw config was patched in the running container and the gateway inference route was pointed at the primary route.",
|
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message = error instanceof Error ? error.message : "Failed to apply inference config"
|
const message = error instanceof Error ? error.message : "Failed to apply inference config"
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import { NextResponse } from "next/server"
|
import { NextResponse } from "next/server"
|
||||||
import { spawn } from "node:child_process"
|
import { spawn } from "node:child_process"
|
||||||
|
import { applySandboxInferenceProfile } from "@/app/lib/sandboxInferenceApply"
|
||||||
|
import { getSandboxInferenceConfig } from "@/app/lib/sandboxInferenceStore"
|
||||||
import { inspectSandbox, resolveSandboxRef } from "@/app/lib/openshellHost"
|
import { inspectSandbox, resolveSandboxRef } from "@/app/lib/openshellHost"
|
||||||
|
|
||||||
const DOCKER_BIN = process.env.DOCKER_BIN || "docker"
|
const DOCKER_BIN = process.env.DOCKER_BIN || "docker"
|
||||||
@@ -67,6 +69,14 @@ export async function POST(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const readiness = await waitForSandboxReady(sandboxName, 90000, 2000)
|
const readiness = await waitForSandboxReady(sandboxName, 90000, 2000)
|
||||||
|
let inferenceApply = null
|
||||||
|
if (readiness.ready) {
|
||||||
|
const inferenceConfig = await getSandboxInferenceConfig(resolved.id)
|
||||||
|
if (inferenceConfig.routes.some((route) => route.enabled)) {
|
||||||
|
inferenceApply = await applySandboxInferenceProfile(resolved.id, sandboxName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
ok: readiness.ready,
|
ok: readiness.ready,
|
||||||
restarted: readiness.ready,
|
restarted: readiness.ready,
|
||||||
@@ -74,9 +84,12 @@ export async function POST(
|
|||||||
sandboxName,
|
sandboxName,
|
||||||
deletion: deleted,
|
deletion: deleted,
|
||||||
readiness,
|
readiness,
|
||||||
|
inferenceApply,
|
||||||
elapsedMs: Date.now() - startedAt,
|
elapsedMs: Date.now() - startedAt,
|
||||||
note: readiness.ready
|
note: readiness.ready
|
||||||
? "Sandbox pod restarted and OpenShell reports it Ready."
|
? inferenceApply
|
||||||
|
? "Sandbox pod restarted, OpenShell reports it Ready, and the saved inference profile was reapplied."
|
||||||
|
: "Sandbox pod restarted and OpenShell reports it Ready."
|
||||||
: "Restart was requested, but OpenShell has not reported the sandbox Ready yet.",
|
: "Restart was requested, but OpenShell has not reported the sandbox Ready yet.",
|
||||||
}, { status: readiness.ready ? 200 : 202 })
|
}, { status: readiness.ready ? 200 : 202 })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -45,11 +45,16 @@ type SandboxVerification = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const AUTHORITATIVE_SUCCESS_PHASE = "Ready"
|
const AUTHORITATIVE_SUCCESS_PHASE = "Ready"
|
||||||
|
const NO_PENDING_DEVICE_REQUESTS = /no pending device pairing requests/i
|
||||||
|
|
||||||
function elapsedMs(start: number) {
|
function elapsedMs(start: number) {
|
||||||
return Date.now() - start
|
return Date.now() - start
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function appendNote(...parts: Array<string | false | null | undefined>) {
|
||||||
|
return parts.filter(Boolean).join(" ")
|
||||||
|
}
|
||||||
|
|
||||||
async function verifySandboxCreation(sandboxName: string): Promise<SandboxVerification> {
|
async function verifySandboxCreation(sandboxName: string): Promise<SandboxVerification> {
|
||||||
const startedAt = Date.now()
|
const startedAt = Date.now()
|
||||||
console.log(`[sandbox/create] verify:start sandbox=${sandboxName}`)
|
console.log(`[sandbox/create] verify:start sandbox=${sandboxName}`)
|
||||||
@@ -207,6 +212,46 @@ async function runCreateCommandBounded(file: string, args: string[], env: NodeJS
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function approveOpenClawDeviceRequests(sandboxName: string) {
|
||||||
|
const result = await runCreateCommandBounded(OPENSHELL_BIN, [
|
||||||
|
"sandbox",
|
||||||
|
"exec",
|
||||||
|
"-n",
|
||||||
|
sandboxName,
|
||||||
|
"--",
|
||||||
|
"sh",
|
||||||
|
"-lc",
|
||||||
|
"openclaw devices approve --latest --json --timeout 10000",
|
||||||
|
], {
|
||||||
|
...process.env,
|
||||||
|
PATH: HOST_PATH,
|
||||||
|
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
|
||||||
|
NO_COLOR: "1",
|
||||||
|
CLICOLOR: "0",
|
||||||
|
CLICOLOR_FORCE: "0",
|
||||||
|
}, 15000)
|
||||||
|
|
||||||
|
const combinedOutput = `${result.stdout}\n${result.stderr}`.trim()
|
||||||
|
const noPending = NO_PENDING_DEVICE_REQUESTS.test(combinedOutput)
|
||||||
|
return {
|
||||||
|
attempted: true,
|
||||||
|
approved: result.completed && result.exitCode === 0,
|
||||||
|
noPending,
|
||||||
|
completed: result.completed,
|
||||||
|
timedOut: result.timedOut,
|
||||||
|
exitCode: result.exitCode,
|
||||||
|
signal: result.signal,
|
||||||
|
stdout: result.stdout,
|
||||||
|
stderr: result.stderr,
|
||||||
|
error: noPending ? undefined : result.error,
|
||||||
|
note: result.completed && result.exitCode === 0
|
||||||
|
? "Ran openclaw devices approve --latest inside the sandbox."
|
||||||
|
: noPending
|
||||||
|
? "Ran openclaw devices approve --latest inside the sandbox; no pending device pairing requests were present."
|
||||||
|
: "Attempted openclaw device approval inside the sandbox, but it did not complete successfully.",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function waitForSandboxReady(sandboxName: string, timeoutMs: number, intervalMs: number) {
|
async function waitForSandboxReady(sandboxName: string, timeoutMs: number, intervalMs: number) {
|
||||||
const startedAt = Date.now()
|
const startedAt = Date.now()
|
||||||
let lastVerification: SandboxVerification | null = null
|
let lastVerification: SandboxVerification | null = null
|
||||||
@@ -306,8 +351,9 @@ export async function POST(request: Request) {
|
|||||||
error: "Sandbox readiness polling produced no verification result.",
|
error: "Sandbox readiness polling produced no verification result.",
|
||||||
}
|
}
|
||||||
const created = readiness.verified
|
const created = readiness.verified
|
||||||
|
const deviceApproval = created ? await approveOpenClawDeviceRequests(sandboxName) : null
|
||||||
console.log(
|
console.log(
|
||||||
`[sandbox/create] request:complete sandbox=${sandboxName} created=${created} readinessAttempts=${readiness.attempts} elapsedMs=${elapsedMs(requestStartedAt)}`,
|
`[sandbox/create] request:complete sandbox=${sandboxName} created=${created} readinessAttempts=${readiness.attempts} deviceApproval=${deviceApproval?.approved ?? false} elapsedMs=${elapsedMs(requestStartedAt)}`,
|
||||||
)
|
)
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
@@ -325,12 +371,16 @@ export async function POST(request: Request) {
|
|||||||
attempts: readiness.attempts,
|
attempts: readiness.attempts,
|
||||||
elapsedMs: readiness.elapsedMs,
|
elapsedMs: readiness.elapsedMs,
|
||||||
},
|
},
|
||||||
|
deviceApproval,
|
||||||
stdout: result.stdout,
|
stdout: result.stdout,
|
||||||
stderr: result.stderr,
|
stderr: result.stderr,
|
||||||
note: created
|
note: created
|
||||||
? (enableTailscale
|
? appendNote(
|
||||||
? "NemoClaw blueprint workflow completed with Tailscale-enabled prerequisites. Existing healthy OpenShell gateways are reused before any new gateway start is attempted."
|
enableTailscale
|
||||||
: "NemoClaw blueprint workflow completed in local/default mode. Existing healthy OpenShell gateways are reused before any new gateway start is attempted.")
|
? "NemoClaw blueprint workflow completed with Tailscale-enabled prerequisites. Existing healthy OpenShell gateways are reused before any new gateway start is attempted."
|
||||||
|
: "NemoClaw blueprint workflow completed in local/default mode. Existing healthy OpenShell gateways are reused before any new gateway start is attempted.",
|
||||||
|
deviceApproval?.note,
|
||||||
|
)
|
||||||
: "Blueprint command reported success, but the sandbox never reached a ready verification state afterward.",
|
: "Blueprint command reported success, but the sandbox never reached a ready verification state afterward.",
|
||||||
}, { status: created ? 200 : 502 })
|
}, { status: created ? 200 : 502 })
|
||||||
}
|
}
|
||||||
@@ -363,9 +413,10 @@ export async function POST(request: Request) {
|
|||||||
error: "Sandbox readiness polling produced no verification result.",
|
error: "Sandbox readiness polling produced no verification result.",
|
||||||
}
|
}
|
||||||
const created = readiness.verified
|
const created = readiness.verified
|
||||||
|
const deviceApproval = created ? await approveOpenClawDeviceRequests(sandboxName) : null
|
||||||
const policyPrepared = Boolean(policy)
|
const policyPrepared = Boolean(policy)
|
||||||
console.log(
|
console.log(
|
||||||
`[sandbox/create] request:complete sandbox=${sandboxName} created=${created} createTimedOut=${createAttempt.timedOut} readinessAttempts=${readiness.attempts} elapsedMs=${elapsedMs(requestStartedAt)}`,
|
`[sandbox/create] request:complete sandbox=${sandboxName} created=${created} createTimedOut=${createAttempt.timedOut} readinessAttempts=${readiness.attempts} deviceApproval=${deviceApproval?.approved ?? false} elapsedMs=${elapsedMs(requestStartedAt)}`,
|
||||||
)
|
)
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
ok: created,
|
ok: created,
|
||||||
@@ -388,13 +439,17 @@ export async function POST(request: Request) {
|
|||||||
attempts: readiness.attempts,
|
attempts: readiness.attempts,
|
||||||
elapsedMs: readiness.elapsedMs,
|
elapsedMs: readiness.elapsedMs,
|
||||||
},
|
},
|
||||||
|
deviceApproval,
|
||||||
policyPrepared,
|
policyPrepared,
|
||||||
note: created
|
note: created
|
||||||
? (createAttempt.timedOut
|
? appendNote(
|
||||||
? "Custom sandbox reached Ready even though the create CLI did not exit promptly."
|
createAttempt.timedOut
|
||||||
: (policyPrepared
|
? "Custom sandbox reached Ready even though the create CLI did not exit promptly."
|
||||||
? "Custom sandbox created. Policy draft is prepared, but applying it live should be a follow-up action."
|
: (policyPrepared
|
||||||
: "Custom sandbox created."))
|
? "Custom sandbox created. Policy draft is prepared, but applying it live should be a follow-up action."
|
||||||
|
: "Custom sandbox created."),
|
||||||
|
deviceApproval?.note,
|
||||||
|
)
|
||||||
: "Create command started, but the sandbox never reached a ready verification state afterward.",
|
: "Create command started, but the sandbox never reached a ready verification state afterward.",
|
||||||
}, { status: created ? 200 : 502 })
|
}, { status: created ? 200 : 502 })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -134,6 +134,7 @@ export default function ConfigurationPanel({ sandboxId, mode = 'existing', onCre
|
|||||||
setMessage([
|
setMessage([
|
||||||
`Sandbox '${data.sandboxName}' created.`,
|
`Sandbox '${data.sandboxName}' created.`,
|
||||||
data.verification?.summary,
|
data.verification?.summary,
|
||||||
|
data.note,
|
||||||
].filter(Boolean).join('\n\n'))
|
].filter(Boolean).join('\n\n'))
|
||||||
if (createdSandboxId && onCreateSuccess) {
|
if (createdSandboxId && onCreateSuccess) {
|
||||||
await onCreateSuccess(createdSandboxId)
|
await onCreateSuccess(createdSandboxId)
|
||||||
|
|||||||
@@ -286,10 +286,42 @@ function hasDashboardToken(value: string | null) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function rewriteDashboardBootstrapOrigin(bootstrapUrl: string, dashboardUrl: string) {
|
||||||
|
const bootstrap = new URL(bootstrapUrl)
|
||||||
|
const dashboard = new URL(dashboardUrl)
|
||||||
|
bootstrap.protocol = dashboard.protocol
|
||||||
|
bootstrap.host = dashboard.host
|
||||||
|
return bootstrap.toString()
|
||||||
|
}
|
||||||
|
|
||||||
export async function resolveOpenClawDashboardBootstrap(instanceId?: string | null) {
|
export async function resolveOpenClawDashboardBootstrap(instanceId?: string | null) {
|
||||||
const instance = resolveOpenClawInstance(instanceId)
|
const instance = resolveOpenClawInstance(instanceId)
|
||||||
const defaultInstance = getDefaultOpenClawInstance()
|
const defaultInstance = getDefaultOpenClawInstance()
|
||||||
const canMintBootstrapFromCli = instance.id === defaultInstance.id
|
const canMintBootstrapFromCli = instance.id === defaultInstance.id
|
||||||
|
const sandboxInstance = resolveSandboxInstanceId(instance.id)
|
||||||
|
|
||||||
|
if (sandboxInstance) {
|
||||||
|
try {
|
||||||
|
const { stdout, stderr } = await execSandboxSsh(sandboxInstance.sandboxId, 'openclaw dashboard --no-open', 15000)
|
||||||
|
const combined = `${stdout}\n${stderr}`
|
||||||
|
const rawBootstrapUrl = normalizeDashboardBootstrapUrl(combined)
|
||||||
|
const bootstrapUrl = rawBootstrapUrl
|
||||||
|
? rewriteDashboardBootstrapOrigin(rawBootstrapUrl, instance.dashboardUrl)
|
||||||
|
: null
|
||||||
|
const bootstrapTokenPresent = hasDashboardToken(bootstrapUrl)
|
||||||
|
|
||||||
|
if (bootstrapUrl) {
|
||||||
|
return {
|
||||||
|
bootstrapUrl,
|
||||||
|
bootstrapTokenPresent,
|
||||||
|
bootstrapSource: 'openclaw-cli' as const,
|
||||||
|
bootstrapAuthority: bootstrapTokenPresent ? ('tokenized-cli' as const) : ('static-fallback' as const),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Fall through to configured instance URL when sandbox CLI minting is unavailable.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (canMintBootstrapFromCli) {
|
if (canMintBootstrapFromCli) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -0,0 +1,197 @@
|
|||||||
|
import { execFile, spawn } from "node:child_process"
|
||||||
|
import { promisify } from "node:util"
|
||||||
|
import { getSandboxInferenceConfig, type SandboxInferenceRoute } from "./sandboxInferenceStore"
|
||||||
|
|
||||||
|
const execFileAsync = promisify(execFile)
|
||||||
|
const HOME = process.env.HOME || ""
|
||||||
|
const OPENSHELL_BIN = process.env.OPENSHELL_BIN || `${HOME}/.local/bin/openshell`
|
||||||
|
const DOCKER_BIN = process.env.DOCKER_BIN || "docker"
|
||||||
|
const OPENSHELL_CLUSTER_CONTAINER = process.env.OPENSHELL_CLUSTER_CONTAINER || "openshell-cluster-nemoclaw"
|
||||||
|
const HOST_PATH = [
|
||||||
|
`${HOME}/.local/bin`,
|
||||||
|
`${HOME}/.nvm/versions/node/v22.22.2/bin`,
|
||||||
|
`${HOME}/.nvm/versions/node/v22.22.1/bin`,
|
||||||
|
"/usr/local/bin",
|
||||||
|
"/usr/bin",
|
||||||
|
"/bin",
|
||||||
|
process.env.PATH || "",
|
||||||
|
].filter(Boolean).join(":")
|
||||||
|
|
||||||
|
function modelEntry(route: SandboxInferenceRoute, openClawModelRef: string, compat: Record<string, unknown> | null) {
|
||||||
|
return {
|
||||||
|
...(compat ? { compat } : {}),
|
||||||
|
id: route.model,
|
||||||
|
name: openClawModelRef,
|
||||||
|
reasoning: false,
|
||||||
|
input: ["text"],
|
||||||
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||||
|
contextWindow: 131072,
|
||||||
|
maxTokens: 4096,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveOpenClawRoute(route: SandboxInferenceRoute) {
|
||||||
|
switch (route.provider) {
|
||||||
|
case "openai-api":
|
||||||
|
return {
|
||||||
|
providerKey: "openai",
|
||||||
|
modelRef: `openai/${route.model}`,
|
||||||
|
baseUrl: "https://inference.local/v1",
|
||||||
|
api: "openai-completions",
|
||||||
|
compat: null,
|
||||||
|
}
|
||||||
|
case "anthropic-prod":
|
||||||
|
case "compatible-anthropic-endpoint":
|
||||||
|
return {
|
||||||
|
providerKey: "anthropic",
|
||||||
|
modelRef: `anthropic/${route.model}`,
|
||||||
|
baseUrl: "https://inference.local",
|
||||||
|
api: "anthropic-messages",
|
||||||
|
compat: null,
|
||||||
|
}
|
||||||
|
case "bedrock":
|
||||||
|
case "compatible-endpoint":
|
||||||
|
case "gemini-api":
|
||||||
|
return {
|
||||||
|
providerKey: "inference",
|
||||||
|
modelRef: `inference/${route.model}`,
|
||||||
|
baseUrl: "https://inference.local/v1",
|
||||||
|
api: "openai-completions",
|
||||||
|
compat: { supportsStore: false },
|
||||||
|
}
|
||||||
|
case "nvidia-prod":
|
||||||
|
case "nvidia-nim":
|
||||||
|
case "ollama-local":
|
||||||
|
case "vllm-local":
|
||||||
|
default:
|
||||||
|
return {
|
||||||
|
providerKey: "inference",
|
||||||
|
modelRef: `inference/${route.model}`,
|
||||||
|
baseUrl: "https://inference.local/v1",
|
||||||
|
api: "openai-completions",
|
||||||
|
compat: null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildOpenClawConfig(current: any, routes: SandboxInferenceRoute[], primary: SandboxInferenceRoute) {
|
||||||
|
const providers: Record<string, any> = {}
|
||||||
|
let primaryModelRef = primary.model
|
||||||
|
const channelDefaults = { ...(current?.channels?.defaults || {}) }
|
||||||
|
delete channelDefaults.configWrites
|
||||||
|
|
||||||
|
for (const route of routes.filter((item) => item.enabled)) {
|
||||||
|
const resolved = resolveOpenClawRoute(route)
|
||||||
|
providers[resolved.providerKey] ||= {
|
||||||
|
baseUrl: resolved.baseUrl,
|
||||||
|
apiKey: "unused",
|
||||||
|
api: resolved.api,
|
||||||
|
models: [],
|
||||||
|
}
|
||||||
|
providers[resolved.providerKey].models.push(modelEntry(route, resolved.modelRef, resolved.compat))
|
||||||
|
if (route.id === primary.id) primaryModelRef = resolved.modelRef
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...current,
|
||||||
|
agents: {
|
||||||
|
...(current?.agents || {}),
|
||||||
|
defaults: {
|
||||||
|
...(current?.agents?.defaults || {}),
|
||||||
|
model: {
|
||||||
|
...(current?.agents?.defaults?.model || {}),
|
||||||
|
primary: primaryModelRef,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
models: {
|
||||||
|
...(current?.models || {}),
|
||||||
|
mode: "merge",
|
||||||
|
providers,
|
||||||
|
},
|
||||||
|
channels: {
|
||||||
|
...(current?.channels || {}),
|
||||||
|
defaults: {
|
||||||
|
...channelDefaults,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runOpenShell(args: string[]) {
|
||||||
|
const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, {
|
||||||
|
env: {
|
||||||
|
...process.env,
|
||||||
|
PATH: HOST_PATH,
|
||||||
|
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
|
||||||
|
NO_COLOR: "1",
|
||||||
|
CLICOLOR: "0",
|
||||||
|
CLICOLOR_FORCE: "0",
|
||||||
|
},
|
||||||
|
timeout: 60000,
|
||||||
|
maxBuffer: 20 * 1024 * 1024,
|
||||||
|
})
|
||||||
|
return { stdout: String(stdout).trim(), stderr: String(stderr).trim() }
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runDockerKubectl(args: string[], input?: string) {
|
||||||
|
return await new Promise<{ stdout: string; stderr: string; code: number | null }>((resolve, reject) => {
|
||||||
|
const child = spawn(DOCKER_BIN, ["exec", ...(input ? ["-i"] : []), OPENSHELL_CLUSTER_CONTAINER, "kubectl", ...args], {
|
||||||
|
env: { ...process.env, PATH: HOST_PATH },
|
||||||
|
stdio: ["pipe", "pipe", "pipe"],
|
||||||
|
})
|
||||||
|
let stdout = ""
|
||||||
|
let stderr = ""
|
||||||
|
child.stdout.on("data", (chunk) => { stdout += String(chunk) })
|
||||||
|
child.stderr.on("data", (chunk) => { stderr += String(chunk) })
|
||||||
|
child.on("error", reject)
|
||||||
|
child.on("close", (code) => resolve({ stdout: stdout.trim(), stderr: stderr.trim(), code }))
|
||||||
|
if (input) child.stdin.end(input)
|
||||||
|
else child.stdin.end()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async function readCurrentOpenClawConfig(sandboxName: string) {
|
||||||
|
const result = await runDockerKubectl(["exec", "-n", "openshell", sandboxName, "--", "cat", "/sandbox/.openclaw/openclaw.json"])
|
||||||
|
if (result.code !== 0) throw new Error(result.stderr || "Failed to read OpenClaw config")
|
||||||
|
return JSON.parse(result.stdout)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function writeOpenClawConfig(sandboxName: string, config: any) {
|
||||||
|
const payload = `${JSON.stringify(config, null, 2)}\n`
|
||||||
|
const script = [
|
||||||
|
"cat > /sandbox/.openclaw/openclaw.json",
|
||||||
|
"chmod 444 /sandbox/.openclaw/openclaw.json",
|
||||||
|
"chown root:root /sandbox/.openclaw/openclaw.json",
|
||||||
|
"sha256sum /sandbox/.openclaw/openclaw.json > /sandbox/.openclaw/.config-hash",
|
||||||
|
"chmod 444 /sandbox/.openclaw/.config-hash",
|
||||||
|
"chown root:root /sandbox/.openclaw/.config-hash",
|
||||||
|
].join(" && ")
|
||||||
|
const result = await runDockerKubectl(["exec", "-i", "-n", "openshell", sandboxName, "--", "sh", "-lc", script], payload)
|
||||||
|
if (result.code !== 0) throw new Error(result.stderr || "Failed to write OpenClaw config")
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
async function restartOpenClawGatewayIfRunning(sandboxName: string) {
|
||||||
|
const script = "for p in /proc/[0-9]*; do cmd=$(tr '\\0' ' ' < \"$p/cmdline\" 2>/dev/null || true); case \"$cmd\" in *'openclaw gateway'*) kill \"${p##*/}\" 2>/dev/null || true;; esac; done"
|
||||||
|
return await runDockerKubectl(["exec", "-n", "openshell", sandboxName, "--", "sh", "-lc", script])
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function applySandboxInferenceProfile(sandboxId: string, sandboxName: string) {
|
||||||
|
const config = await getSandboxInferenceConfig(sandboxId)
|
||||||
|
const enabledRoutes = config.routes.filter((route) => route.enabled)
|
||||||
|
if (enabledRoutes.length === 0) throw new Error("No enabled inference routes are configured for this sandbox")
|
||||||
|
const primary = enabledRoutes.find((route) => route.id === config.primaryRouteId) || enabledRoutes[0]
|
||||||
|
|
||||||
|
const currentOpenClawConfig = await readCurrentOpenClawConfig(sandboxName)
|
||||||
|
const nextOpenClawConfig = buildOpenClawConfig(currentOpenClawConfig, enabledRoutes, primary)
|
||||||
|
await writeOpenClawConfig(sandboxName, nextOpenClawConfig)
|
||||||
|
await restartOpenClawGatewayIfRunning(sandboxName)
|
||||||
|
|
||||||
|
const routeResult = await runOpenShell(["inference", "set", "--no-verify", "--provider", primary.provider, "--model", primary.model])
|
||||||
|
return {
|
||||||
|
primaryRoute: primary,
|
||||||
|
routesApplied: enabledRoutes.length,
|
||||||
|
gatewayRoute: routeResult,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -52,6 +52,29 @@ export default function Dashboard() {
|
|||||||
setTheme(theme === 'light' ? 'dark' : 'light')
|
setTheme(theme === 'light' ? 'dark' : 'light')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!inventoryEnabled || loading || isCreateMode || isDestroyMode) return
|
||||||
|
if (sandboxes.length === 0) {
|
||||||
|
if (dashboardSession.selectedSandboxId) {
|
||||||
|
setDashboardSession((current) => updateDashboardSessionSelection(current, null))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const selectedStillExists = sandboxes.some((sandbox) => sandbox.id === dashboardSession.selectedSandboxId)
|
||||||
|
if (selectedStillExists) return
|
||||||
|
|
||||||
|
const nextSelection = sandboxes.find((sandbox) => sandbox.isDefault)?.id || sandboxes[0]?.id || null
|
||||||
|
setDashboardSession((current) => updateDashboardSessionSelection(current, nextSelection))
|
||||||
|
}, [
|
||||||
|
dashboardSession.selectedSandboxId,
|
||||||
|
inventoryEnabled,
|
||||||
|
isCreateMode,
|
||||||
|
isDestroyMode,
|
||||||
|
loading,
|
||||||
|
sandboxes,
|
||||||
|
])
|
||||||
|
|
||||||
const handleSandboxSelect = (id: string | null) => {
|
const handleSandboxSelect = (id: string | null) => {
|
||||||
if (isDestroyMode && id) {
|
if (isDestroyMode && id) {
|
||||||
setDeletingSandboxId(id)
|
setDeletingSandboxId(id)
|
||||||
|
|||||||
Reference in New Issue
Block a user