diff --git a/app/api/sandbox/[sandboxId]/inference/route.ts b/app/api/sandbox/[sandboxId]/inference/route.ts new file mode 100644 index 0000000..985d009 --- /dev/null +++ b/app/api/sandbox/[sandboxId]/inference/route.ts @@ -0,0 +1,60 @@ +import { NextResponse } from "next/server" +import { + getSandboxInferenceConfig, + saveSandboxInferenceConfig, +} from "@/app/lib/sandboxInferenceStore" + +function validateSandboxId(value: string) { + if (!value || value.length > 128) throw new Error("sandbox id is required") + if (!/^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,127}$/.test(value)) { + throw new Error("sandbox id contains unsupported characters") + } + return value +} + +function parseModels(value: unknown) { + if (!Array.isArray(value)) return [] + return value + .map((item) => typeof item === "string" ? item.trim() : "") + .filter(Boolean) +} + +export async function GET( + _request: Request, + { params }: { params: Promise<{ sandboxId: string }> } +) { + try { + const { sandboxId } = await params + const config = await getSandboxInferenceConfig(validateSandboxId(sandboxId)) + return NextResponse.json({ ok: true, config }) + } catch (error) { + const message = error instanceof Error ? error.message : "Failed to fetch sandbox inference config" + return NextResponse.json({ ok: false, error: message }, { status: /required|unsupported/.test(message) ? 400 : 500 }) + } +} + +export async function POST( + request: Request, + { params }: { params: Promise<{ sandboxId: string }> } +) { + try { + const { sandboxId } = await params + const body = await request.json() + const provider = typeof body?.provider === "string" ? body.provider.trim() : "" + const primaryModel = typeof body?.primaryModel === "string" ? body.primaryModel.trim() : "" + const models = parseModels(body?.models) + + if (!provider) throw new Error("provider is required") + if (!primaryModel && models.length === 0) throw new Error("at least one model is required") + + const config = await saveSandboxInferenceConfig(validateSandboxId(sandboxId), { + provider, + primaryModel: primaryModel || models[0], + models, + }) + return NextResponse.json({ ok: true, config }) + } catch (error) { + const message = error instanceof Error ? error.message : "Failed to save sandbox inference config" + return NextResponse.json({ ok: false, error: message }, { status: /required|unsupported|model/.test(message) ? 400 : 500 }) + } +} diff --git a/app/components/SandboxInferencePanel.tsx b/app/components/SandboxInferencePanel.tsx new file mode 100644 index 0000000..b9641d2 --- /dev/null +++ b/app/components/SandboxInferencePanel.tsx @@ -0,0 +1,222 @@ +"use client" + +import { useCallback, useEffect, useMemo, useState } from "react" +import type { SandboxInventoryItem } from "../hooks/useSandboxInventory" + +type ProviderSummary = { + id: string | null + name: string | null + type: string | null +} + +type OllamaModel = { + name: string + sizeLabel: string | null + parameterSize: string | null + quantization: string | null +} + +type SandboxInferenceConfig = { + sandboxId: string + provider: string + primaryModel: string + models: string[] + updatedAt: string | null +} + +function emptyConfig(sandboxId: string): SandboxInferenceConfig { + return { sandboxId, provider: "", primaryModel: "", models: [], updatedAt: null } +} + +function uniqueModels(values: string[]) { + return Array.from(new Set(values.map((value) => value.trim()).filter(Boolean))) +} + +export default function SandboxInferencePanel({ sandbox }: { sandbox: SandboxInventoryItem }) { + const [loading, setLoading] = useState(true) + const [saving, setSaving] = useState(false) + const [message, setMessage] = useState("") + const [providers, setProviders] = useState([]) + const [provider, setProvider] = useState("") + const [primaryModel, setPrimaryModel] = useState("") + const [modelsText, setModelsText] = useState("") + const [updatedAt, setUpdatedAt] = useState(null) + const [ollamaModels, setOllamaModels] = useState([]) + const [ollamaLoading, setOllamaLoading] = useState(false) + + const isOllamaProvider = provider.toLowerCase().includes("ollama") + const selectedModels = useMemo( + () => uniqueModels([primaryModel, ...modelsText.split(/\r?\n/)]), + [primaryModel, modelsText] + ) + + const loadOllamaModels = useCallback(async () => { + try { + setOllamaLoading(true) + const response = await fetch("/api/ollama/models", { cache: "no-store" }) + const data = await response.json() + setOllamaModels(data.available && Array.isArray(data.models) ? data.models : []) + } finally { + setOllamaLoading(false) + } + }, []) + + const load = useCallback(async () => { + try { + setLoading(true) + setMessage("") + const [inferenceResponse, configResponse] = await Promise.all([ + fetch("/api/inference", { cache: "no-store" }), + fetch(`/api/sandbox/${encodeURIComponent(sandbox.id)}/inference`, { cache: "no-store" }), + ]) + const inferenceData = await inferenceResponse.json() + const configData = await configResponse.json() + if (!inferenceResponse.ok) throw new Error(inferenceData.error || "Failed to load providers") + 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 fallbackProvider = inferenceData.gateway?.provider || nextProviders[0]?.name || "" + const fallbackModel = inferenceData.gateway?.model || "" + const models = config.models.length > 0 ? config.models : (fallbackModel ? [fallbackModel] : []) + + setProviders(nextProviders) + setProvider(config.provider || fallbackProvider) + setPrimaryModel(config.primaryModel || models[0] || "") + setModelsText(models.join("\n")) + setUpdatedAt(config.updatedAt) + } catch (error) { + setMessage(error instanceof Error ? error.message : "Failed to load sandbox inference config") + } finally { + setLoading(false) + } + }, [sandbox.id]) + + useEffect(() => { + load() + }, [load]) + + useEffect(() => { + if (!isOllamaProvider) return + loadOllamaModels() + const interval = window.setInterval(loadOllamaModels, 10000) + return () => window.clearInterval(interval) + }, [isOllamaProvider, 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] || "") + } + + async function save() { + try { + setSaving(true) + setMessage("") + const models = selectedModels + 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, + }), + }) + 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")) + setUpdatedAt(config.updatedAt) + setMessage(`Saved inference profile for ${sandbox.name}.`) + } catch (error) { + setMessage(error instanceof Error ? error.message : "Failed to save sandbox inference config") + } finally { + setSaving(false) + } + } + + return ( +
+
+
+

+ {sandbox.name} - INFERENCE PROFILE +

+

+ Container-specific provider and model selection. +

+
+ +
+ + {loading ? ( +
Loading inference profile...
+ ) : ( +
+
+
+ + +
+
+ + 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)]" /> +
+
+ + {isOllamaProvider && ( +
+
+
Ollama Models
+ +
+ {ollamaModels.length === 0 ? ( +

No local Ollama models reported.

+ ) : ( +
+ {ollamaModels.map((item) => ( + + ))} +
+ )} +
+ )} + +
+ +