diff --git a/app/api/sandbox/create/route.ts b/app/api/sandbox/create/route.ts index dcee22c..e332653 100644 --- a/app/api/sandbox/create/route.ts +++ b/app/api/sandbox/create/route.ts @@ -44,10 +44,97 @@ type SandboxVerification = { const AUTHORITATIVE_SUCCESS_PHASE = "Ready" const NO_PENDING_DEVICE_REQUESTS = /no pending device pairing requests/i +type ConfiguredInferenceRoute = { + provider: string + model: string +} + function elapsedMs(start: number) { return Date.now() - start } +function stripAnsi(value: string) { + return value.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, "") +} + +function readField(output: string, label: string) { + const match = output.match(new RegExp(`^\\s*${label}:\\s*(.+?)\\s*$`, "im")) + return match ? match[1].trim() : "" +} + +function parseConfiguredInferenceRoute(output: string, label: "Gateway inference" | "System inference"): ConfiguredInferenceRoute | null { + const otherLabel = label === "Gateway inference" ? "System inference" : "Gateway inference" + const pattern = new RegExp(`${label}:\\s*([\\s\\S]*?)(?:\\n\\s*${otherLabel}:|$)`, "i") + const section = output.match(pattern)?.[1] ?? "" + if (!section || /Not configured/i.test(section)) return null + const provider = readField(section, "Provider") + const model = readField(section, "Model") + return provider && model ? { provider, model } : null +} + +async function resolveConfiguredPrimaryInferenceRoute() { + try { + const { stdout } = await execFileAsync(OPENSHELL_BIN, ["inference", "get"], { + env: hostCommandEnv({ + OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw", + }), + timeout: 30000, + maxBuffer: 5 * 1024 * 1024, + }) + const output = stripAnsi(String(stdout).trim()) + return parseConfiguredInferenceRoute(output, "Gateway inference") || parseConfiguredInferenceRoute(output, "System inference") + } catch { + return null + } +} + +function mapOpenShellProviderToNemoClawProvider(provider: string) { + const normalized = provider.trim().toLowerCase() + const mappings: Record = { + "nvidia-inference": "build", + "nvidia-prod": "build", + "nvidia": "build", + "nvidia-nim": "nim-local", + "nim-local": "nim-local", + "ollama-local": "ollama", + "ollama": "ollama", + "vllm-local": "vllm", + "vllm": "vllm", + "openai-api": "openai", + "openai": "openai", + "anthropic-prod": "anthropic", + "anthropic": "anthropic", + "compatible-anthropic-endpoint": "anthropicCompatible", + "anthropiccompatible": "anthropicCompatible", + "compatible-endpoint": "custom", + "custom": "custom", + } + return mappings[normalized] || "" +} + +function applyConfiguredInferenceToOnboardEnv(env: NodeJS.ProcessEnv, route: ConfiguredInferenceRoute | null) { + if (!route) return null + const provider = mapOpenShellProviderToNemoClawProvider(route.provider) + if (!provider) return null + + env.NEMOCLAW_PROVIDER = provider + env.NEMOCLAW_MODEL = route.model + if (provider === "nim-local" || provider === "vllm") { + env.NEMOCLAW_EXPERIMENTAL = env.NEMOCLAW_EXPERIMENTAL || "1" + } + if (provider === "custom") { + const endpointUrl = process.env.NEMOCLAW_ENDPOINT_URL || process.env.OPENAI_BASE_URL || process.env.VLLM_BASE_URL || "" + if (endpointUrl) env.NEMOCLAW_ENDPOINT_URL = endpointUrl + const providerKey = process.env.NEMOCLAW_PROVIDER_KEY || process.env.OPENAI_API_KEY || process.env.VLLM_API_KEY || "" + if (providerKey) env.NEMOCLAW_PROVIDER_KEY = providerKey + } + return { + openshellProvider: route.provider, + nemoclawProvider: provider, + model: route.model, + } +} + function appendNote(...parts: Array) { return parts.filter(Boolean).join(" ") } @@ -335,6 +422,7 @@ export async function POST(request: Request) { if (blueprint === "nemoclaw-blueprint") { const createCommand = buildNemoClawCreateCommand() + const configuredInferenceRoute = await resolveConfiguredPrimaryInferenceRoute() const env: NodeJS.ProcessEnv = hostCommandEnv({ NEMOCLAW_SANDBOX_NAME: sandboxName, NEMOCLAW_NON_INTERACTIVE: "1", @@ -342,8 +430,12 @@ export async function POST(request: Request) { NEMOCLAW_ACCEPT_THIRD_PARTY_SOFTWARE: "1", OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw", }) + const onboardInference = applyConfiguredInferenceToOnboardEnv(env, configuredInferenceRoute) + console.log( + `[sandbox/create] inference-default sandbox=${sandboxName} openshellProvider=${onboardInference?.openshellProvider || "unconfigured"} nemoclawProvider=${onboardInference?.nemoclawProvider || "default"} model=${onboardInference?.model || "default"} elapsedMs=${elapsedMs(requestStartedAt)}`, + ) - if (!enableTailscale) { + if (!enableTailscale && !onboardInference) { env.NVIDIA_API_KEY = env.NVIDIA_API_KEY || "optional-local-mode" } @@ -363,6 +455,8 @@ export async function POST(request: Request) { blueprint, sandboxName, enableTailscale, + configuredInferenceRoute, + onboardInference, }, { status: 500 }) } @@ -387,6 +481,8 @@ export async function POST(request: Request) { verification, mode: "nemoclaw-blueprint", enableTailscale, + configuredInferenceRoute, + onboardInference, createCommand, hostPath: HOST_PATH, readiness: { diff --git a/tests/sandbox-lifecycle-check.mjs b/tests/sandbox-lifecycle-check.mjs index d47d0b8..c47ca6a 100644 --- a/tests/sandbox-lifecycle-check.mjs +++ b/tests/sandbox-lifecycle-check.mjs @@ -53,6 +53,11 @@ assert.match( /readiness:\s*\{\s*attempts: readiness\.attempts,\s*elapsedMs: readiness\.elapsedMs,/, 'blueprint create response must expose readiness polling metadata', ) +assert.match(createRouteSource, /resolveConfiguredPrimaryInferenceRoute/, 'blueprint create must resolve the configured primary OpenShell inference route') +assert.match(createRouteSource, /applyConfiguredInferenceToOnboardEnv/, 'blueprint create must pass the configured inference route into NemoClaw onboarding') +assert.match(createRouteSource, /NEMOCLAW_PROVIDER/, 'blueprint create must set NemoClaw provider from inference configuration') +assert.match(createRouteSource, /NEMOCLAW_MODEL/, 'blueprint create must set NemoClaw model from inference configuration') +assert.match(createRouteSource, /!enableTailscale && !onboardInference/, 'blueprint create must not inject the optional NVIDIA placeholder when a primary inference route is configured') assert.match(hookSource, /type RefreshOptions = \{\s*force\?: boolean\s*\}/, 'inventory refresh must accept a force option') assert.match(hookSource, /!refreshOptions\?\.force && inFlightRef\.current/, 'forced inventory refresh must bypass existing in-flight reads')