Support multiple inference routes per sandbox

This commit is contained in:
Codex
2026-04-24 18:07:16 +00:00
parent c1e7fdc0e2
commit a221e6d249
4 changed files with 214 additions and 54 deletions
+144 -47
View File
@@ -16,20 +16,42 @@ type OllamaModel = {
quantization: string | null
}
type SandboxInferenceRoute = {
id: string
provider: string
model: string
enabled: boolean
label: string
}
type SandboxInferenceConfig = {
sandboxId: string
provider: string
primaryModel: string
models: string[]
routes: SandboxInferenceRoute[]
primaryRouteId: string
updatedAt: string | null
}
function emptyConfig(sandboxId: string): SandboxInferenceConfig {
return { sandboxId, provider: "", primaryModel: "", models: [], updatedAt: null }
function makeRoute(provider: string, model: string, label = ""): SandboxInferenceRoute {
return {
id: `${provider}::${model}`,
provider,
model,
enabled: true,
label,
}
}
function uniqueModels(values: string[]) {
return Array.from(new Set(values.map((value) => value.trim()).filter(Boolean)))
function routeKey(route: Pick<SandboxInferenceRoute, "provider" | "model">) {
return `${route.provider}::${route.model}`
}
function dedupeRoutes(routes: SandboxInferenceRoute[]) {
return Array.from(new Map(routes
.filter((route) => route.provider.trim() && route.model.trim())
.map((route) => [route.id || routeKey(route), { ...route, id: route.id || routeKey(route) }])).values())
}
export default function SandboxInferencePanel({ sandbox }: { sandbox: SandboxInventoryItem }) {
@@ -37,17 +59,21 @@ export default function SandboxInferencePanel({ sandbox }: { sandbox: SandboxInv
const [saving, setSaving] = useState(false)
const [message, setMessage] = useState("")
const [providers, setProviders] = useState<ProviderSummary[]>([])
const [provider, setProvider] = useState("")
const [primaryModel, setPrimaryModel] = useState("")
const [modelsText, setModelsText] = useState("")
const [routes, setRoutes] = useState<SandboxInferenceRoute[]>([])
const [primaryRouteId, setPrimaryRouteId] = useState("")
const [draftProvider, setDraftProvider] = useState("")
const [draftModel, setDraftModel] = useState("")
const [draftLabel, setDraftLabel] = useState("")
const [updatedAt, setUpdatedAt] = useState<string | null>(null)
const [ollamaModels, setOllamaModels] = useState<OllamaModel[]>([])
const [ollamaLoading, setOllamaLoading] = useState(false)
const isOllamaProvider = provider.toLowerCase().includes("ollama")
const selectedModels = useMemo(
() => uniqueModels([primaryModel, ...modelsText.split(/\r?\n/)]),
[primaryModel, modelsText]
const draftProviderIsOllama = draftProvider.toLowerCase().includes("ollama")
const anyOllamaRoute = routes.some((route) => route.provider.toLowerCase().includes("ollama"))
const shouldPollOllama = draftProviderIsOllama || anyOllamaRoute
const primaryRoute = useMemo(
() => routes.find((route) => route.id === primaryRouteId) || routes[0] || null,
[routes, primaryRouteId]
)
const loadOllamaModels = useCallback(async () => {
@@ -75,16 +101,22 @@ export default function SandboxInferencePanel({ sandbox }: { sandbox: SandboxInv
if (!configResponse.ok) throw new Error(configData.error || "Failed to load sandbox inference config")
const nextProviders = Array.isArray(inferenceData.providers) ? inferenceData.providers : []
const config = (configData.config ?? emptyConfig(sandbox.id)) as SandboxInferenceConfig
const config = configData.config as SandboxInferenceConfig
const fallbackProvider = inferenceData.gateway?.provider || nextProviders[0]?.name || ""
const fallbackModel = inferenceData.gateway?.model || ""
const models = config.models.length > 0 ? config.models : (fallbackModel ? [fallbackModel] : [])
const nextRoutes = dedupeRoutes(Array.isArray(config?.routes) && config.routes.length > 0
? config.routes
: fallbackProvider && fallbackModel
? [makeRoute(fallbackProvider, fallbackModel, "Gateway default")]
: [])
setProviders(nextProviders)
setProvider(config.provider || fallbackProvider)
setPrimaryModel(config.primaryModel || models[0] || "")
setModelsText(models.join("\n"))
setUpdatedAt(config.updatedAt)
setRoutes(nextRoutes)
setPrimaryRouteId(config?.primaryRouteId || nextRoutes[0]?.id || "")
setDraftProvider(fallbackProvider)
setDraftModel("")
setDraftLabel("")
setUpdatedAt(config?.updatedAt || null)
} catch (error) {
setMessage(error instanceof Error ? error.message : "Failed to load sandbox inference config")
} finally {
@@ -97,42 +129,67 @@ export default function SandboxInferencePanel({ sandbox }: { sandbox: SandboxInv
}, [load])
useEffect(() => {
if (!isOllamaProvider) return
if (!shouldPollOllama) return
loadOllamaModels()
const interval = window.setInterval(loadOllamaModels, 10000)
return () => window.clearInterval(interval)
}, [isOllamaProvider, loadOllamaModels])
}, [shouldPollOllama, loadOllamaModels])
function toggleModel(modelName: string) {
const next = selectedModels.includes(modelName)
? selectedModels.filter((item) => item !== modelName)
: [...selectedModels, modelName]
setModelsText(next.join("\n"))
setPrimaryModel((current) => next.includes(current) ? current : next[0] || "")
function addRoute(provider = draftProvider, model = draftModel, label = draftLabel) {
const cleanProvider = provider.trim()
const cleanModel = model.trim()
if (!cleanProvider || !cleanModel) {
setMessage("Choose a provider and model before adding a route.")
return
}
const nextRoute = makeRoute(cleanProvider, cleanModel, label.trim())
setRoutes((current) => {
const next = dedupeRoutes([...current, nextRoute])
if (!primaryRouteId) setPrimaryRouteId(nextRoute.id)
return next
})
setDraftModel("")
setDraftLabel("")
setMessage("")
}
function updateRoute(id: string, updates: Partial<SandboxInferenceRoute>) {
setRoutes((current) => current.map((route) => route.id === id ? { ...route, ...updates } : route))
}
function removeRoute(id: string) {
setRoutes((current) => {
const next = current.filter((route) => route.id !== id)
if (primaryRouteId === id) setPrimaryRouteId(next[0]?.id || "")
return next
})
}
async function save() {
try {
setSaving(true)
setMessage("")
const models = selectedModels
const cleanRoutes = dedupeRoutes(routes)
if (cleanRoutes.length === 0) throw new Error("Add at least one provider/model route.")
const primary = cleanRoutes.find((route) => route.id === primaryRouteId) || cleanRoutes[0]
const response = await fetch(`/api/sandbox/${encodeURIComponent(sandbox.id)}/inference`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
provider,
primaryModel: primaryModel || models[0],
models,
provider: primary.provider,
primaryModel: primary.model,
models: cleanRoutes.map((route) => route.model),
routes: cleanRoutes,
primaryRouteId: primary.id,
}),
})
const data = await response.json()
if (!response.ok) throw new Error(data.error || "Failed to save sandbox inference config")
const config = data.config as SandboxInferenceConfig
setProvider(config.provider)
setPrimaryModel(config.primaryModel)
setModelsText(config.models.join("\n"))
setRoutes(config.routes)
setPrimaryRouteId(config.primaryRouteId)
setUpdatedAt(config.updatedAt)
setMessage(`Saved inference profile for ${sandbox.name}.`)
setMessage(`Saved ${config.routes.length} inference route${config.routes.length === 1 ? "" : "s"} for ${sandbox.name}.`)
} catch (error) {
setMessage(error instanceof Error ? error.message : "Failed to save sandbox inference config")
} finally {
@@ -145,10 +202,10 @@ export default function SandboxInferencePanel({ sandbox }: { sandbox: SandboxInv
<div className="flex items-center justify-between gap-4 border-b border-[var(--border-subtle)] pb-4">
<div>
<h4 className="text-sm font-semibold text-[var(--foreground)] uppercase tracking-wider">
{sandbox.name} - INFERENCE PROFILE
{sandbox.name} - INFERENCE ROUTES
</h4>
<p className="mt-1 text-xs text-[var(--foreground-dim)]">
Container-specific provider and model selection.
Enable multiple endpoint/model routes for this sandbox.
</p>
</div>
<button
@@ -161,24 +218,39 @@ export default function SandboxInferencePanel({ sandbox }: { sandbox: SandboxInv
</div>
{loading ? (
<div className="py-8 text-xs uppercase tracking-wider text-[var(--foreground-dim)]">Loading inference profile...</div>
<div className="py-8 text-xs uppercase tracking-wider text-[var(--foreground-dim)]">Loading inference routes...</div>
) : (
<div className="mt-5 space-y-5">
<div className="grid grid-cols-1 lg:grid-cols-3 gap-4">
{primaryRoute && (
<div className="metric p-4">
<p className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">Primary Route</p>
<p className="mt-2 text-sm font-mono text-[var(--foreground)]">{primaryRoute.provider}</p>
<p className="mt-1 text-xs font-mono text-[var(--foreground-dim)]">{primaryRoute.model}</p>
</div>
)}
<div className="grid grid-cols-1 lg:grid-cols-[minmax(180px,240px)_minmax(0,1fr)_minmax(160px,220px)_auto] gap-3 items-end">
<div className="space-y-2">
<label className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">Endpoint Provider</label>
<select value={provider} onChange={(event) => setProvider(event.target.value)} className="w-full rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] px-3 py-2 text-sm font-mono text-[var(--foreground)] focus:outline-none focus:border-[var(--nvidia-green)]">
<label className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">Endpoint</label>
<select value={draftProvider} onChange={(event) => setDraftProvider(event.target.value)} className="w-full rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] px-3 py-2 text-sm font-mono text-[var(--foreground)] focus:outline-none focus:border-[var(--nvidia-green)]">
<option value="">Select provider</option>
{providers.map((item) => item.name ? <option key={item.name} value={item.name}>{item.name}</option> : null)}
</select>
</div>
<div className="space-y-2 lg:col-span-2">
<label className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">Primary Model</label>
<input value={primaryModel} onChange={(event) => setPrimaryModel(event.target.value)} className="w-full rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] px-3 py-2 text-sm font-mono text-[var(--foreground)] focus:outline-none focus:border-[var(--nvidia-green)]" />
<div className="space-y-2">
<label className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">Model</label>
<input value={draftModel} onChange={(event) => setDraftModel(event.target.value)} placeholder="model id" className="w-full rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] px-3 py-2 text-sm font-mono text-[var(--foreground)] focus:outline-none focus:border-[var(--nvidia-green)]" />
</div>
<div className="space-y-2">
<label className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">Label</label>
<input value={draftLabel} onChange={(event) => setDraftLabel(event.target.value)} placeholder="optional" className="w-full rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] px-3 py-2 text-sm font-mono text-[var(--foreground)] focus:outline-none focus:border-[var(--nvidia-green)]" />
</div>
<button onClick={() => addRoute()} 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)]">
Add Route
</button>
</div>
{isOllamaProvider && (
{draftProviderIsOllama && (
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] p-4">
<div className="flex items-center justify-between gap-4">
<h5 className="text-xs uppercase tracking-wider text-[var(--foreground)]">Ollama Models</h5>
@@ -191,7 +263,7 @@ export default function SandboxInferencePanel({ sandbox }: { sandbox: SandboxInv
) : (
<div className="mt-3 grid grid-cols-1 md:grid-cols-2 gap-2">
{ollamaModels.map((item) => (
<button key={item.name} type="button" onClick={() => toggleModel(item.name)} className={`rounded-sm border p-3 text-left ${selectedModels.includes(item.name) ? "border-[var(--nvidia-green)] bg-[rgba(118,185,0,0.08)]" : "border-[var(--border-subtle)] bg-[var(--background)]"}`}>
<button key={item.name} type="button" onClick={() => addRoute(draftProvider, item.name, "Ollama")} className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] p-3 text-left hover:border-[var(--nvidia-green)]">
<div className="text-xs font-mono text-[var(--foreground)]">{item.name}</div>
<div className="mt-1 text-[11px] text-[var(--foreground-dim)]">
{[item.parameterSize, item.quantization, item.sizeLabel].filter(Boolean).join(" · ") || "local model"}
@@ -204,13 +276,38 @@ export default function SandboxInferencePanel({ sandbox }: { sandbox: SandboxInv
)}
<div className="space-y-2">
<label className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">Model List</label>
<textarea value={modelsText} onChange={(event) => setModelsText(event.target.value)} rows={4} className="w-full rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] px-3 py-2 text-sm font-mono text-[var(--foreground)] focus:outline-none focus:border-[var(--nvidia-green)]" />
<h5 className="text-xs uppercase tracking-wider text-[var(--foreground)]">Enabled Routes</h5>
{routes.length === 0 ? (
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] p-4 text-xs text-[var(--foreground-dim)]">No routes enabled for this sandbox.</div>
) : (
<div className="space-y-2">
{routes.map((route) => (
<div key={route.id} className="grid grid-cols-1 lg:grid-cols-[auto_minmax(160px,220px)_minmax(0,1fr)_minmax(140px,200px)_auto] gap-3 rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] p-3 items-center">
<label className="flex items-center gap-2 text-xs text-[var(--foreground-dim)]">
<input type="radio" checked={primaryRouteId === route.id} onChange={() => setPrimaryRouteId(route.id)} />
Primary
</label>
<span className="text-xs font-mono text-[var(--foreground)]">{route.provider}</span>
<input value={route.model} onChange={(event) => updateRoute(route.id, { model: event.target.value })} className="w-full rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] px-3 py-2 text-xs font-mono text-[var(--foreground)] focus:outline-none focus:border-[var(--nvidia-green)]" />
<input value={route.label} onChange={(event) => updateRoute(route.id, { label: event.target.value })} placeholder="label" className="w-full rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] px-3 py-2 text-xs font-mono text-[var(--foreground)] focus:outline-none focus:border-[var(--nvidia-green)]" />
<div className="flex items-center justify-end gap-3">
<label className="flex items-center gap-2 text-xs text-[var(--foreground-dim)]">
<input type="checkbox" checked={route.enabled} onChange={(event) => updateRoute(route.id, { enabled: event.target.checked })} />
Enabled
</label>
<button onClick={() => removeRoute(route.id)} className="px-3 py-2 rounded-sm bg-[var(--background)] text-[var(--foreground)] text-xs font-mono uppercase tracking-wider hover:border-[var(--status-stopped)] border border-[var(--border-subtle)]">
Remove
</button>
</div>
</div>
))}
</div>
)}
</div>
<div className="flex items-center gap-3 flex-wrap">
<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 Profile"}
{saving ? "Saving..." : "Save Sandbox Routes"}
</button>
{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>}