Support multiple inference routes per sandbox
This commit is contained in:
@@ -2,6 +2,7 @@ import { NextResponse } from "next/server"
|
|||||||
import {
|
import {
|
||||||
getSandboxInferenceConfig,
|
getSandboxInferenceConfig,
|
||||||
saveSandboxInferenceConfig,
|
saveSandboxInferenceConfig,
|
||||||
|
type SandboxInferenceRoute,
|
||||||
} from "@/app/lib/sandboxInferenceStore"
|
} from "@/app/lib/sandboxInferenceStore"
|
||||||
|
|
||||||
function validateSandboxId(value: string) {
|
function validateSandboxId(value: string) {
|
||||||
@@ -19,6 +20,24 @@ function parseModels(value: unknown) {
|
|||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parseRoutes(value: unknown): SandboxInferenceRoute[] {
|
||||||
|
if (!Array.isArray(value)) return []
|
||||||
|
return value.map((item) => {
|
||||||
|
const provider = typeof item?.provider === "string" ? item.provider.trim() : ""
|
||||||
|
const model = typeof item?.model === "string" ? item.model.trim() : ""
|
||||||
|
const id = typeof item?.id === "string" && item.id.trim()
|
||||||
|
? item.id.trim()
|
||||||
|
: `${provider}::${model}`
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
provider,
|
||||||
|
model,
|
||||||
|
enabled: item?.enabled !== false,
|
||||||
|
label: typeof item?.label === "string" ? item.label.trim() : "",
|
||||||
|
}
|
||||||
|
}).filter((item) => item.provider && item.model)
|
||||||
|
}
|
||||||
|
|
||||||
export async function GET(
|
export async function GET(
|
||||||
_request: Request,
|
_request: Request,
|
||||||
{ params }: { params: Promise<{ sandboxId: string }> }
|
{ params }: { params: Promise<{ sandboxId: string }> }
|
||||||
@@ -43,14 +62,18 @@ export async function POST(
|
|||||||
const provider = typeof body?.provider === "string" ? body.provider.trim() : ""
|
const provider = typeof body?.provider === "string" ? body.provider.trim() : ""
|
||||||
const primaryModel = typeof body?.primaryModel === "string" ? body.primaryModel.trim() : ""
|
const primaryModel = typeof body?.primaryModel === "string" ? body.primaryModel.trim() : ""
|
||||||
const models = parseModels(body?.models)
|
const models = parseModels(body?.models)
|
||||||
|
const routes = parseRoutes(body?.routes)
|
||||||
|
const primaryRouteId = typeof body?.primaryRouteId === "string" ? body.primaryRouteId.trim() : ""
|
||||||
|
|
||||||
if (!provider) throw new Error("provider is required")
|
if (routes.length === 0 && !provider) throw new Error("provider is required")
|
||||||
if (!primaryModel && models.length === 0) throw new Error("at least one model is required")
|
if (routes.length === 0 && !primaryModel && models.length === 0) throw new Error("at least one model is required")
|
||||||
|
|
||||||
const config = await saveSandboxInferenceConfig(validateSandboxId(sandboxId), {
|
const config = await saveSandboxInferenceConfig(validateSandboxId(sandboxId), {
|
||||||
provider,
|
provider,
|
||||||
primaryModel: primaryModel || models[0],
|
primaryModel: primaryModel || models[0],
|
||||||
models,
|
models,
|
||||||
|
routes,
|
||||||
|
primaryRouteId,
|
||||||
})
|
})
|
||||||
return NextResponse.json({ ok: true, config })
|
return NextResponse.json({ ok: true, config })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -16,20 +16,42 @@ type OllamaModel = {
|
|||||||
quantization: string | null
|
quantization: string | null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SandboxInferenceRoute = {
|
||||||
|
id: string
|
||||||
|
provider: string
|
||||||
|
model: string
|
||||||
|
enabled: boolean
|
||||||
|
label: string
|
||||||
|
}
|
||||||
|
|
||||||
type SandboxInferenceConfig = {
|
type SandboxInferenceConfig = {
|
||||||
sandboxId: string
|
sandboxId: string
|
||||||
provider: string
|
provider: string
|
||||||
primaryModel: string
|
primaryModel: string
|
||||||
models: string[]
|
models: string[]
|
||||||
|
routes: SandboxInferenceRoute[]
|
||||||
|
primaryRouteId: string
|
||||||
updatedAt: string | null
|
updatedAt: string | null
|
||||||
}
|
}
|
||||||
|
|
||||||
function emptyConfig(sandboxId: string): SandboxInferenceConfig {
|
function makeRoute(provider: string, model: string, label = ""): SandboxInferenceRoute {
|
||||||
return { sandboxId, provider: "", primaryModel: "", models: [], updatedAt: null }
|
return {
|
||||||
|
id: `${provider}::${model}`,
|
||||||
|
provider,
|
||||||
|
model,
|
||||||
|
enabled: true,
|
||||||
|
label,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function uniqueModels(values: string[]) {
|
function routeKey(route: Pick<SandboxInferenceRoute, "provider" | "model">) {
|
||||||
return Array.from(new Set(values.map((value) => value.trim()).filter(Boolean)))
|
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 }) {
|
export default function SandboxInferencePanel({ sandbox }: { sandbox: SandboxInventoryItem }) {
|
||||||
@@ -37,17 +59,21 @@ export default function SandboxInferencePanel({ sandbox }: { sandbox: SandboxInv
|
|||||||
const [saving, setSaving] = useState(false)
|
const [saving, setSaving] = useState(false)
|
||||||
const [message, setMessage] = useState("")
|
const [message, setMessage] = useState("")
|
||||||
const [providers, setProviders] = useState<ProviderSummary[]>([])
|
const [providers, setProviders] = useState<ProviderSummary[]>([])
|
||||||
const [provider, setProvider] = useState("")
|
const [routes, setRoutes] = useState<SandboxInferenceRoute[]>([])
|
||||||
const [primaryModel, setPrimaryModel] = useState("")
|
const [primaryRouteId, setPrimaryRouteId] = useState("")
|
||||||
const [modelsText, setModelsText] = useState("")
|
const [draftProvider, setDraftProvider] = useState("")
|
||||||
|
const [draftModel, setDraftModel] = useState("")
|
||||||
|
const [draftLabel, setDraftLabel] = useState("")
|
||||||
const [updatedAt, setUpdatedAt] = useState<string | null>(null)
|
const [updatedAt, setUpdatedAt] = useState<string | null>(null)
|
||||||
const [ollamaModels, setOllamaModels] = useState<OllamaModel[]>([])
|
const [ollamaModels, setOllamaModels] = useState<OllamaModel[]>([])
|
||||||
const [ollamaLoading, setOllamaLoading] = useState(false)
|
const [ollamaLoading, setOllamaLoading] = useState(false)
|
||||||
|
|
||||||
const isOllamaProvider = provider.toLowerCase().includes("ollama")
|
const draftProviderIsOllama = draftProvider.toLowerCase().includes("ollama")
|
||||||
const selectedModels = useMemo(
|
const anyOllamaRoute = routes.some((route) => route.provider.toLowerCase().includes("ollama"))
|
||||||
() => uniqueModels([primaryModel, ...modelsText.split(/\r?\n/)]),
|
const shouldPollOllama = draftProviderIsOllama || anyOllamaRoute
|
||||||
[primaryModel, modelsText]
|
const primaryRoute = useMemo(
|
||||||
|
() => routes.find((route) => route.id === primaryRouteId) || routes[0] || null,
|
||||||
|
[routes, primaryRouteId]
|
||||||
)
|
)
|
||||||
|
|
||||||
const loadOllamaModels = useCallback(async () => {
|
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")
|
if (!configResponse.ok) throw new Error(configData.error || "Failed to load sandbox inference config")
|
||||||
|
|
||||||
const nextProviders = Array.isArray(inferenceData.providers) ? inferenceData.providers : []
|
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 fallbackProvider = inferenceData.gateway?.provider || nextProviders[0]?.name || ""
|
||||||
const fallbackModel = inferenceData.gateway?.model || ""
|
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)
|
setProviders(nextProviders)
|
||||||
setProvider(config.provider || fallbackProvider)
|
setRoutes(nextRoutes)
|
||||||
setPrimaryModel(config.primaryModel || models[0] || "")
|
setPrimaryRouteId(config?.primaryRouteId || nextRoutes[0]?.id || "")
|
||||||
setModelsText(models.join("\n"))
|
setDraftProvider(fallbackProvider)
|
||||||
setUpdatedAt(config.updatedAt)
|
setDraftModel("")
|
||||||
|
setDraftLabel("")
|
||||||
|
setUpdatedAt(config?.updatedAt || null)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setMessage(error instanceof Error ? error.message : "Failed to load sandbox inference config")
|
setMessage(error instanceof Error ? error.message : "Failed to load sandbox inference config")
|
||||||
} finally {
|
} finally {
|
||||||
@@ -97,42 +129,67 @@ export default function SandboxInferencePanel({ sandbox }: { sandbox: SandboxInv
|
|||||||
}, [load])
|
}, [load])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isOllamaProvider) return
|
if (!shouldPollOllama) return
|
||||||
loadOllamaModels()
|
loadOllamaModels()
|
||||||
const interval = window.setInterval(loadOllamaModels, 10000)
|
const interval = window.setInterval(loadOllamaModels, 10000)
|
||||||
return () => window.clearInterval(interval)
|
return () => window.clearInterval(interval)
|
||||||
}, [isOllamaProvider, loadOllamaModels])
|
}, [shouldPollOllama, loadOllamaModels])
|
||||||
|
|
||||||
function toggleModel(modelName: string) {
|
function addRoute(provider = draftProvider, model = draftModel, label = draftLabel) {
|
||||||
const next = selectedModels.includes(modelName)
|
const cleanProvider = provider.trim()
|
||||||
? selectedModels.filter((item) => item !== modelName)
|
const cleanModel = model.trim()
|
||||||
: [...selectedModels, modelName]
|
if (!cleanProvider || !cleanModel) {
|
||||||
setModelsText(next.join("\n"))
|
setMessage("Choose a provider and model before adding a route.")
|
||||||
setPrimaryModel((current) => next.includes(current) ? current : next[0] || "")
|
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() {
|
async function save() {
|
||||||
try {
|
try {
|
||||||
setSaving(true)
|
setSaving(true)
|
||||||
setMessage("")
|
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`, {
|
const response = await fetch(`/api/sandbox/${encodeURIComponent(sandbox.id)}/inference`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
provider,
|
provider: primary.provider,
|
||||||
primaryModel: primaryModel || models[0],
|
primaryModel: primary.model,
|
||||||
models,
|
models: cleanRoutes.map((route) => route.model),
|
||||||
|
routes: cleanRoutes,
|
||||||
|
primaryRouteId: primary.id,
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
const data = await response.json()
|
const data = await response.json()
|
||||||
if (!response.ok) throw new Error(data.error || "Failed to save sandbox inference config")
|
if (!response.ok) throw new Error(data.error || "Failed to save sandbox inference config")
|
||||||
const config = data.config as SandboxInferenceConfig
|
const config = data.config as SandboxInferenceConfig
|
||||||
setProvider(config.provider)
|
setRoutes(config.routes)
|
||||||
setPrimaryModel(config.primaryModel)
|
setPrimaryRouteId(config.primaryRouteId)
|
||||||
setModelsText(config.models.join("\n"))
|
|
||||||
setUpdatedAt(config.updatedAt)
|
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) {
|
} catch (error) {
|
||||||
setMessage(error instanceof Error ? error.message : "Failed to save sandbox inference config")
|
setMessage(error instanceof Error ? error.message : "Failed to save sandbox inference config")
|
||||||
} finally {
|
} 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 className="flex items-center justify-between gap-4 border-b border-[var(--border-subtle)] pb-4">
|
||||||
<div>
|
<div>
|
||||||
<h4 className="text-sm font-semibold text-[var(--foreground)] uppercase tracking-wider">
|
<h4 className="text-sm font-semibold text-[var(--foreground)] uppercase tracking-wider">
|
||||||
{sandbox.name} - INFERENCE PROFILE
|
{sandbox.name} - INFERENCE ROUTES
|
||||||
</h4>
|
</h4>
|
||||||
<p className="mt-1 text-xs text-[var(--foreground-dim)]">
|
<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>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<button
|
<button
|
||||||
@@ -161,24 +218,39 @@ export default function SandboxInferencePanel({ sandbox }: { sandbox: SandboxInv
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{loading ? (
|
{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="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">
|
<div className="space-y-2">
|
||||||
<label className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">Endpoint Provider</label>
|
<label className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">Endpoint</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)]">
|
<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>
|
<option value="">Select provider</option>
|
||||||
{providers.map((item) => item.name ? <option key={item.name} value={item.name}>{item.name}</option> : null)}
|
{providers.map((item) => item.name ? <option key={item.name} value={item.name}>{item.name}</option> : null)}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div className="space-y-2 lg:col-span-2">
|
<div className="space-y-2">
|
||||||
<label className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">Primary Model</label>
|
<label className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">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)]" />
|
<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>
|
||||||
|
<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>
|
</div>
|
||||||
|
|
||||||
{isOllamaProvider && (
|
{draftProviderIsOllama && (
|
||||||
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] p-4">
|
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] p-4">
|
||||||
<div className="flex items-center justify-between gap-4">
|
<div className="flex items-center justify-between gap-4">
|
||||||
<h5 className="text-xs uppercase tracking-wider text-[var(--foreground)]">Ollama Models</h5>
|
<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">
|
<div className="mt-3 grid grid-cols-1 md:grid-cols-2 gap-2">
|
||||||
{ollamaModels.map((item) => (
|
{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="text-xs font-mono text-[var(--foreground)]">{item.name}</div>
|
||||||
<div className="mt-1 text-[11px] text-[var(--foreground-dim)]">
|
<div className="mt-1 text-[11px] text-[var(--foreground-dim)]">
|
||||||
{[item.parameterSize, item.quantization, item.sizeLabel].filter(Boolean).join(" · ") || "local model"}
|
{[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">
|
<div className="space-y-2">
|
||||||
<label className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">Model List</label>
|
<h5 className="text-xs uppercase tracking-wider text-[var(--foreground)]">Enabled Routes</h5>
|
||||||
<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)]" />
|
{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>
|
||||||
|
|
||||||
<div className="flex items-center gap-3 flex-wrap">
|
<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">
|
<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>
|
</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>}
|
||||||
|
|||||||
@@ -6,9 +6,19 @@ export type SandboxInferenceConfig = {
|
|||||||
provider: string
|
provider: string
|
||||||
primaryModel: string
|
primaryModel: string
|
||||||
models: string[]
|
models: string[]
|
||||||
|
routes: SandboxInferenceRoute[]
|
||||||
|
primaryRouteId: string
|
||||||
updatedAt: string | null
|
updatedAt: string | null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type SandboxInferenceRoute = {
|
||||||
|
id: string
|
||||||
|
provider: string
|
||||||
|
model: string
|
||||||
|
enabled: boolean
|
||||||
|
label: string
|
||||||
|
}
|
||||||
|
|
||||||
type StoreShape = {
|
type StoreShape = {
|
||||||
sandboxes: Record<string, SandboxInferenceConfig>
|
sandboxes: Record<string, SandboxInferenceConfig>
|
||||||
}
|
}
|
||||||
@@ -39,16 +49,42 @@ async function writeStore(store: StoreShape) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function normalizeSandboxInferenceConfig(sandboxId: string, input?: Partial<SandboxInferenceConfig> | null): SandboxInferenceConfig {
|
export function normalizeSandboxInferenceConfig(sandboxId: string, input?: Partial<SandboxInferenceConfig> | null): SandboxInferenceConfig {
|
||||||
const models = Array.from(new Set([
|
const legacyProvider = typeof input?.provider === "string" ? input.provider.trim() : ""
|
||||||
|
const legacyModels = Array.from(new Set([
|
||||||
input?.primaryModel,
|
input?.primaryModel,
|
||||||
...(Array.isArray(input?.models) ? input.models : []),
|
...(Array.isArray(input?.models) ? input.models : []),
|
||||||
].map((item) => typeof item === "string" ? item.trim() : "").filter(Boolean)))
|
].map((item) => typeof item === "string" ? item.trim() : "").filter(Boolean)))
|
||||||
|
const rawRoutes: any[] = [
|
||||||
|
...(Array.isArray(input?.routes) ? input.routes : []),
|
||||||
|
...legacyModels.map((model) => ({ provider: legacyProvider, model, enabled: true, label: "" })),
|
||||||
|
]
|
||||||
|
const routeEntries: Array<[string, SandboxInferenceRoute]> = rawRoutes.map((route) => {
|
||||||
|
const provider = typeof route?.provider === "string" ? route.provider.trim() : ""
|
||||||
|
const model = typeof route?.model === "string" ? route.model.trim() : ""
|
||||||
|
const id = typeof route?.id === "string" && route.id.trim()
|
||||||
|
? route.id.trim()
|
||||||
|
: `${provider}::${model}`
|
||||||
|
return [id, {
|
||||||
|
id,
|
||||||
|
provider,
|
||||||
|
model,
|
||||||
|
enabled: route?.enabled !== false,
|
||||||
|
label: typeof route?.label === "string" ? route.label.trim() : "",
|
||||||
|
}] as [string, SandboxInferenceRoute]
|
||||||
|
}).filter(([, route]) => route.provider && route.model)
|
||||||
|
const routes = Array.from(new Map(routeEntries).values())
|
||||||
|
const primaryRouteId = typeof input?.primaryRouteId === "string" && routes.some((route) => route.id === input.primaryRouteId)
|
||||||
|
? input.primaryRouteId
|
||||||
|
: routes[0]?.id || ""
|
||||||
|
const primaryRoute = routes.find((route) => route.id === primaryRouteId) || routes[0]
|
||||||
|
|
||||||
return {
|
return {
|
||||||
sandboxId,
|
sandboxId,
|
||||||
provider: typeof input?.provider === "string" ? input.provider.trim() : "",
|
provider: primaryRoute?.provider || legacyProvider,
|
||||||
primaryModel: models[0] || "",
|
primaryModel: primaryRoute?.model || legacyModels[0] || "",
|
||||||
models,
|
models: routes.map((route) => route.model),
|
||||||
|
routes,
|
||||||
|
primaryRouteId,
|
||||||
updatedAt: input?.updatedAt || null,
|
updatedAt: input?.updatedAt || null,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,10 +67,14 @@ assert.match(inferencePanelSource, /\/api\/ollama\/models/, 'settings UI must po
|
|||||||
assert.match(inferencePanelSource, /window\.setInterval\(\(\) => loadOllamaModels\(\{ silent: true \}\), 10000\)/, 'settings UI must autopoll Ollama while selected')
|
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')
|
assert.match(ollamaModelsRouteSource, /\/api\/tags/, 'Ollama models route must query the Ollama tags API')
|
||||||
assert.match(ollamaModelsRouteSource, /OLLAMA_BASE_URL/, 'Ollama models route must support a configurable Ollama base URL')
|
assert.match(ollamaModelsRouteSource, /OLLAMA_BASE_URL/, 'Ollama models route must support a configurable Ollama base URL')
|
||||||
|
assert.match(sandboxInferenceRouteSource, /parseRoutes/, 'sandbox inference route must accept multiple provider/model routes')
|
||||||
|
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 Profile/, 'selected sandbox UI must expose per-sandbox inference profile saving')
|
assert.match(sandboxInferencePanelSource, /Save Sandbox Routes/, 'selected sandbox UI must expose multi-route inference saving')
|
||||||
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\/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, /Add Route/, 'selected sandbox UI must allow adding provider/model routes')
|
||||||
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')
|
||||||
|
|||||||
Reference in New Issue
Block a user