From bd9f7a036a7702f0c30ecdab9de12c057c2e2db2 Mon Sep 17 00:00:00 2001 From: Codex Date: Mon, 27 Apr 2026 18:13:23 +0000 Subject: [PATCH] Add default inference endpoint rollout --- app/api/inference/route.ts | 67 +++++++++++++++++++++++ app/components/InferenceEndpointPanel.tsx | 62 +++++++++++++++++++++ tests/sandbox-lifecycle-check.mjs | 6 ++ 3 files changed, 135 insertions(+) diff --git a/app/api/inference/route.ts b/app/api/inference/route.ts index d29e9f9..efe412a 100644 --- a/app/api/inference/route.ts +++ b/app/api/inference/route.ts @@ -2,6 +2,8 @@ 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 { applySandboxInferenceProfile } from "@/app/lib/sandboxInferenceApply" +import { saveSandboxInferenceConfig } from "@/app/lib/sandboxInferenceStore" const execFileAsync = promisify(execFile) @@ -72,6 +74,38 @@ function parseProvider(output: string) { } } +function parseSandboxNames(output: string) { + return output + .split(/\r?\n/) + .map((line) => line.trim()) + .filter((line) => line && !/^name\s+/i.test(line) && !/^[-=]+$/.test(line) && !/^no sandboxes found\.?$/i.test(line)) + .map((line) => line.split(/\s{2,}/)[0]?.trim()) + .filter((name): name is string => Boolean(name)) +} + +async function readRunningSandboxes() { + const { stdout } = await runOpenShell(["sandbox", "list"]) + const names = parseSandboxNames(stdout) + const sandboxes = [] + + for (const name of names) { + try { + const details = await runOpenShell(["sandbox", "get", name]) + const rawPhase = readField(details.stdout, "Phase") || readField(details.stdout, "Status") || "" + if (!/^ready$/i.test(rawPhase)) continue + sandboxes.push({ + name: readField(details.stdout, "Name") || name, + id: readField(details.stdout, "Id") || name, + phase: rawPhase, + }) + } catch { + // Ignore sandboxes that disappear while the bulk route is being applied. + } + } + + return sandboxes +} + async function readProviders() { const { stdout } = await runOpenShell(["provider", "list", "--names"]) const names = stdout.split(/\r?\n/).map((line) => line.trim()).filter(Boolean) @@ -141,8 +175,10 @@ export async function POST(request: Request) { const system = body?.route === "system" || body?.system === true const noVerify = body?.noVerify !== false const timeout = Number(body?.timeout ?? 0) + const applyToRunningSandboxes = body?.applyToRunningSandboxes === true if (makeActive && !model) throw new Error("model is required when making the endpoint active") + if (applyToRunningSandboxes && system) throw new Error("default inference endpoint must target the sandbox route") if (!/^[A-Z_][A-Z0-9_]*$/.test(credentialKey)) throw new Error("credential key must look like an environment variable name") if (baseUrl) new URL(baseUrl) @@ -165,6 +201,36 @@ export async function POST(request: Request) { inferenceResult = await runOpenShell(routeArgs) } + const sandboxApplyResults: any[] = [] + if (applyToRunningSandboxes) { + const runningSandboxes = await readRunningSandboxes() + for (const sandbox of runningSandboxes) { + try { + await saveSandboxInferenceConfig(sandbox.id, { + provider: name, + primaryModel: model, + models: [model], + routes: [{ + id: `${name}::${model}`, + provider: name, + model, + enabled: true, + label: "Default inference endpoint", + }], + primaryRouteId: `${name}::${model}`, + }) + const applied = await applySandboxInferenceProfile(sandbox.id, sandbox.name) + sandboxApplyResults.push({ ok: true, sandbox, applied }) + } catch (error) { + sandboxApplyResults.push({ + ok: false, + sandbox, + error: error instanceof Error ? error.message : "Failed to apply default inference endpoint", + }) + } + } + } + const refreshed = await runOpenShell(["inference", "get"]) return NextResponse.json({ ok: true, @@ -172,6 +238,7 @@ export async function POST(request: Request) { active: makeActive, gateway: parseRoute(refreshed.stdout, "Gateway inference"), system: parseRoute(refreshed.stdout, "System inference"), + sandboxApplyResults, stdout: [providerResult.stdout, inferenceResult?.stdout].filter(Boolean).join("\n\n"), stderr: [providerResult.stderr, inferenceResult?.stderr].filter(Boolean).join("\n\n"), }) diff --git a/app/components/InferenceEndpointPanel.tsx b/app/components/InferenceEndpointPanel.tsx index d4d3b97..5e41eec 100644 --- a/app/components/InferenceEndpointPanel.tsx +++ b/app/components/InferenceEndpointPanel.tsx @@ -69,6 +69,8 @@ export default function InferenceEndpointPanel() { const [ollamaLoading, setOllamaLoading] = useState(false) const [ollamaMessage, setOllamaMessage] = useState("") const [deletingProvider, setDeletingProvider] = useState(null) + const [applyingDefault, setApplyingDefault] = useState(false) + const [confirmDefaultOpen, setConfirmDefaultOpen] = useState(false) const activeProvider = useMemo( () => providers.find((provider) => provider.name === gateway.provider) ?? null, @@ -186,6 +188,45 @@ export default function InferenceEndpointPanel() { } } + async function setDefaultForAllRunningSandboxes() { + try { + setConfirmDefaultOpen(false) + setApplyingDefault(true) + setMessage("") + const response = await fetch("/api/inference", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + name, + type, + model, + baseUrl, + credentialKey, + apiKey, + route: "gateway", + timeout: Number(timeout || "0"), + noVerify, + makeActive: true, + applyToRunningSandboxes: true, + }), + }) + const data = await response.json() + if (!response.ok) throw new Error(data.error || "Failed to set default inference endpoint") + const results = Array.isArray(data.sandboxApplyResults) ? data.sandboxApplyResults : [] + const applied = results.filter((item: any) => item?.ok).length + const failed = results.length - applied + setGateway(data.gateway ?? gateway) + setSystem(data.system ?? system) + setApiKey("") + setMessage(`Default inference endpoint set to '${name}'. Applied to ${applied} running sandbox${applied === 1 ? "" : "es"}${failed ? `; ${failed} failed.` : "."}`) + await load() + } catch (error) { + setMessage(error instanceof Error ? error.message : "Failed to set default inference endpoint") + } finally { + setApplyingDefault(false) + } + } + async function deleteProvider(providerName: string | null) { if (!providerName) return if (!window.confirm(`Delete inference provider '${providerName}'?`)) return @@ -333,6 +374,9 @@ export default function InferenceEndpointPanel() { + @@ -366,6 +410,24 @@ export default function InferenceEndpointPanel() { )} + {confirmDefaultOpen && ( +
+
+

Set Default Inference Endpoint

+

+ This will change the default inference endpoint to ALL running sandboxes and newly created sandboxes. +

+
+ + +
+
+
+ )} ) } diff --git a/tests/sandbox-lifecycle-check.mjs b/tests/sandbox-lifecycle-check.mjs index 916d111..6efd94b 100644 --- a/tests/sandbox-lifecycle-check.mjs +++ b/tests/sandbox-lifecycle-check.mjs @@ -74,10 +74,16 @@ assert.match(inferenceRouteSource, /\["provider", "update", name\]/, 'inference assert.match(inferenceRouteSource, /\["provider", "delete", name\]/, 'inference route must delete OpenShell providers') assert.match(inferenceRouteSource, /\["inference", "set", "--provider", name, "--model", model\]/, 'inference route must set the active OpenShell inference route') assert.match(inferenceRouteSource, /OPENAI_BASE_URL=\$\{baseUrl\}/, 'inference route must pass endpoint URL as provider config') +assert.match(inferenceRouteSource, /applyToRunningSandboxes/, 'inference route must support applying a default endpoint to running sandboxes') +assert.match(inferenceRouteSource, /readRunningSandboxes/, 'inference route must discover running sandboxes for default endpoint rollout') +assert.match(inferenceRouteSource, /applySandboxInferenceProfile/, 'inference route must live-apply the default endpoint to running sandboxes') assert.match(inferencePanelSource, /INFERENCE ENDPOINTS/, 'settings UI must expose inference endpoint configuration') assert.match(inferencePanelSource, /https:\/\/inference-api\.nvidia\.com\/v1/, 'settings UI must provide the NVIDIA inference API preset') assert.match(inferencePanelSource, /aws\/anthropic\/claude-opus-4-5/, 'settings UI must include the requested NVIDIA inference model preset') assert.match(inferencePanelSource, /method: "DELETE"/, 'settings UI must expose provider deletion') +assert.match(inferencePanelSource, /Set as Default Inference Endpoint/, 'settings UI must expose a default endpoint rollout action') +assert.match(inferencePanelSource, /This will change the default inference endpoint to ALL running sandboxes and newly created sandboxes\./, 'default endpoint action must warn about all running and new sandboxes') +assert.match(inferencePanelSource, /applyToRunningSandboxes: true/, 'settings UI must request default endpoint rollout after confirmation') assert.match(inferencePanelSource, /\/api\/ollama\/models/, 'settings UI must poll local Ollama models') assert.match(inferencePanelSource, /window\.setInterval\(\(\) => loadOllamaModels\(\{ silent: true \}\), 10000\)/, 'settings UI must autopoll Ollama while selected') assert.match(ollamaModelsRouteSource, /\/api\/tags/, 'Ollama models route must query the Ollama tags API')