Apply sandbox inference routes live

This commit is contained in:
Codex
2026-04-24 18:14:33 +00:00
parent a221e6d249
commit 546ec21cb6
3 changed files with 210 additions and 0 deletions
@@ -0,0 +1,177 @@
import { NextResponse } from "next/server"
import { execFile, spawn } from "node:child_process"
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) {
if (!value || value.length > 63) throw new Error("sandbox name is required")
if (!/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/.test(value)) throw new Error("invalid sandbox name")
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(
request: Request,
{ params }: { params: Promise<{ sandboxId: string }> }
) {
try {
const { sandboxId } = await params
const body = await request.json().catch(() => ({}))
const sandboxName = validateSandboxName(typeof body?.sandboxName === "string" ? body.sandboxName : sandboxId)
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 NextResponse.json({
ok: true,
applied: true,
sandboxId,
sandboxName,
primaryRoute: primary,
routesApplied: enabledRoutes.length,
gatewayRoute: routeResult,
note: "OpenClaw config was patched in the running container and the gateway inference route was pointed at the primary route.",
})
} catch (error) {
const message = error instanceof Error ? error.message : "Failed to apply inference config"
return NextResponse.json({
ok: false,
applied: false,
rebuildRequired: true,
error: message,
note: "Live apply failed. Rebuild or recreate the sandbox with this inference profile.",
}, { status: /required|configured|invalid/.test(message) ? 400 : 500 })
}
}
+26
View File
@@ -57,6 +57,7 @@ function dedupeRoutes(routes: SandboxInferenceRoute[]) {
export default function SandboxInferencePanel({ sandbox }: { sandbox: SandboxInventoryItem }) { export default function SandboxInferencePanel({ sandbox }: { sandbox: SandboxInventoryItem }) {
const [loading, setLoading] = useState(true) const [loading, setLoading] = useState(true)
const [saving, setSaving] = useState(false) const [saving, setSaving] = useState(false)
const [applying, setApplying] = useState(false)
const [message, setMessage] = useState("") const [message, setMessage] = useState("")
const [providers, setProviders] = useState<ProviderSummary[]>([]) const [providers, setProviders] = useState<ProviderSummary[]>([])
const [routes, setRoutes] = useState<SandboxInferenceRoute[]>([]) const [routes, setRoutes] = useState<SandboxInferenceRoute[]>([])
@@ -197,6 +198,28 @@ export default function SandboxInferencePanel({ sandbox }: { sandbox: SandboxInv
} }
} }
async function applyToContainer() {
try {
setApplying(true)
setMessage("")
const cleanRoutes = dedupeRoutes(routes)
if (cleanRoutes.length === 0) throw new Error("Add at least one provider/model route before applying.")
await save()
const response = await fetch(`/api/sandbox/${encodeURIComponent(sandbox.id)}/inference/apply`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ sandboxName: sandbox.name }),
})
const data = await response.json()
if (!response.ok) throw new Error(data.note ? `${data.error}\n\n${data.note}` : data.error || "Failed to apply routes")
setMessage(`${data.routesApplied} route${data.routesApplied === 1 ? "" : "s"} applied to ${sandbox.name}. ${data.note}`)
} catch (error) {
setMessage(error instanceof Error ? error.message : "Failed to apply routes")
} finally {
setApplying(false)
}
}
return ( return (
<div className="panel p-6"> <div className="panel p-6">
<div className="flex items-center justify-between gap-4 border-b border-[var(--border-subtle)] pb-4"> <div className="flex items-center justify-between gap-4 border-b border-[var(--border-subtle)] pb-4">
@@ -309,6 +332,9 @@ export default function SandboxInferencePanel({ sandbox }: { sandbox: SandboxInv
<button onClick={save} disabled={saving} className="px-4 py-2 rounded-sm bg-[var(--nvidia-green)] text-white text-xs font-mono uppercase tracking-wider disabled:opacity-50"> <button onClick={save} disabled={saving} className="px-4 py-2 rounded-sm bg-[var(--nvidia-green)] text-white text-xs font-mono uppercase tracking-wider disabled:opacity-50">
{saving ? "Saving..." : "Save Sandbox Routes"} {saving ? "Saving..." : "Save Sandbox Routes"}
</button> </button>
<button onClick={applyToContainer} disabled={saving || applying} className="px-4 py-2 rounded-sm bg-[var(--background-tertiary)] text-[var(--foreground)] text-xs font-mono uppercase tracking-wider hover:border-[var(--nvidia-green)] border border-[var(--border-subtle)] disabled:opacity-50">
{applying ? "Applying..." : "Apply to Running Container"}
</button>
{updatedAt && <span className="text-[11px] text-[var(--foreground-dim)]">Updated {new Date(updatedAt).toLocaleString()}</span>} {updatedAt && <span className="text-[11px] text-[var(--foreground-dim)]">Updated {new Date(updatedAt).toLocaleString()}</span>}
{message && <span className="text-xs text-[var(--foreground-dim)] whitespace-pre-wrap">{message}</span>} {message && <span className="text-xs text-[var(--foreground-dim)] whitespace-pre-wrap">{message}</span>}
</div> </div>
+7
View File
@@ -9,6 +9,7 @@ const telemetryRoutePath = path.join(root, 'app/api/telemetry/real/route.ts')
const inferenceRoutePath = path.join(root, 'app/api/inference/route.ts') const inferenceRoutePath = path.join(root, 'app/api/inference/route.ts')
const ollamaModelsRoutePath = path.join(root, 'app/api/ollama/models/route.ts') const ollamaModelsRoutePath = path.join(root, 'app/api/ollama/models/route.ts')
const sandboxInferenceRoutePath = path.join(root, 'app/api/sandbox/[sandboxId]/inference/route.ts') const sandboxInferenceRoutePath = path.join(root, 'app/api/sandbox/[sandboxId]/inference/route.ts')
const sandboxInferenceApplyRoutePath = path.join(root, 'app/api/sandbox/[sandboxId]/inference/apply/route.ts')
const hookPath = path.join(root, 'app/hooks/useSandboxInventory.ts') const hookPath = path.join(root, 'app/hooks/useSandboxInventory.ts')
const pagePath = path.join(root, 'app/page.tsx') const pagePath = path.join(root, 'app/page.tsx')
const configPanelPath = path.join(root, 'app/components/ConfigurationPanel.tsx') const configPanelPath = path.join(root, 'app/components/ConfigurationPanel.tsx')
@@ -23,6 +24,7 @@ const telemetryRouteSource = await readFile(telemetryRoutePath, 'utf8')
const inferenceRouteSource = await readFile(inferenceRoutePath, 'utf8') const inferenceRouteSource = await readFile(inferenceRoutePath, 'utf8')
const ollamaModelsRouteSource = await readFile(ollamaModelsRoutePath, 'utf8') const ollamaModelsRouteSource = await readFile(ollamaModelsRoutePath, 'utf8')
const sandboxInferenceRouteSource = await readFile(sandboxInferenceRoutePath, 'utf8') const sandboxInferenceRouteSource = await readFile(sandboxInferenceRoutePath, 'utf8')
const sandboxInferenceApplyRouteSource = await readFile(sandboxInferenceApplyRoutePath, 'utf8')
const hookSource = await readFile(hookPath, 'utf8') const hookSource = await readFile(hookPath, 'utf8')
const pageSource = await readFile(pagePath, 'utf8') const pageSource = await readFile(pagePath, 'utf8')
const configPanelSource = await readFile(configPanelPath, 'utf8') const configPanelSource = await readFile(configPanelPath, 'utf8')
@@ -71,10 +73,15 @@ assert.match(sandboxInferenceRouteSource, /parseRoutes/, 'sandbox inference rout
assert.match(sandboxInferenceRouteSource, /primaryRouteId/, 'sandbox inference route must preserve the primary route selection') assert.match(sandboxInferenceRouteSource, /primaryRouteId/, 'sandbox inference route must preserve the primary route selection')
assert.match(sandboxInferenceRouteSource, /saveSandboxInferenceConfig/, 'sandbox inference route must persist per-sandbox inference profiles') assert.match(sandboxInferenceRouteSource, /saveSandboxInferenceConfig/, 'sandbox inference route must persist per-sandbox inference profiles')
assert.match(sandboxInferencePanelSource, /Save Sandbox Routes/, 'selected sandbox UI must expose multi-route inference saving') assert.match(sandboxInferencePanelSource, /Save Sandbox Routes/, 'selected sandbox UI must expose multi-route inference saving')
assert.match(sandboxInferencePanelSource, /Apply to Running Container/, 'selected sandbox UI must expose live apply for running containers')
assert.match(sandboxInferencePanelSource, /\/api\/sandbox\/\$\{encodeURIComponent\(sandbox\.id\)\}\/inference/, 'selected sandbox UI must load and save sandbox-specific inference profiles') assert.match(sandboxInferencePanelSource, /\/api\/sandbox\/\$\{encodeURIComponent\(sandbox\.id\)\}\/inference/, 'selected sandbox UI must load and save sandbox-specific inference profiles')
assert.match(sandboxInferencePanelSource, /\/api\/sandbox\/\$\{encodeURIComponent\(sandbox\.id\)\}\/inference\/apply/, 'selected sandbox UI must call live apply endpoint')
assert.match(sandboxInferencePanelSource, /\/api\/ollama\/models/, 'selected sandbox UI must offer Ollama model selection') assert.match(sandboxInferencePanelSource, /\/api\/ollama\/models/, 'selected sandbox UI must offer Ollama model selection')
assert.match(sandboxInferencePanelSource, /Enabled Routes/, 'selected sandbox UI must show multiple enabled inference routes') assert.match(sandboxInferencePanelSource, /Enabled Routes/, 'selected sandbox UI must show multiple enabled inference routes')
assert.match(sandboxInferencePanelSource, /Add Route/, 'selected sandbox UI must allow adding provider/model routes') assert.match(sandboxInferencePanelSource, /Add Route/, 'selected sandbox UI must allow adding provider/model routes')
assert.match(sandboxInferenceApplyRouteSource, /\/sandbox\/\.openclaw\/openclaw\.json/, 'live apply must patch OpenClaw config inside the running sandbox')
assert.match(sandboxInferenceApplyRouteSource, /sha256sum \/sandbox\/\.openclaw\/openclaw\.json > \/sandbox\/\.openclaw\/\.config-hash/, 'live apply must refresh the immutable OpenClaw config hash')
assert.match(sandboxInferenceApplyRouteSource, /\["inference", "set", "--no-verify", "--provider", primary\.provider, "--model", primary\.model\]/, 'live apply must point OpenShell inference at the primary route')
assert.match(sandboxListSource, /<SandboxInferencePanel sandbox=\{selectedSandbox\} \/>/, 'sandbox list must render the per-sandbox inference profile panel for the selected sandbox') assert.match(sandboxListSource, /<SandboxInferencePanel sandbox=\{selectedSandbox\} \/>/, 'sandbox list must render the per-sandbox inference profile panel for the selected sandbox')
assert.match(sidebarSource, /Inference Endpoints/, 'sidebar settings item must be named Inference Endpoints') assert.match(sidebarSource, /Inference Endpoints/, 'sidebar settings item must be named Inference Endpoints')
assert.match(pageSource, /<InferenceEndpointPanel \/>/, 'settings view must render inference endpoint panel') assert.match(pageSource, /<InferenceEndpointPanel \/>/, 'settings view must render inference endpoint panel')