import { FormEvent, useCallback, useEffect, useRef, useState } from "react"; import type { ReactNode } from "react"; import { CapabilityDetails } from "./CapabilityDetails"; import { PromotionGate } from "./PromotionGate"; import type { OperatorIdentity } from "../lib/api"; import { CircleGauge, Clock3, KeyRound, Play, RefreshCw, ShieldCheck } from "lucide-react"; import { api, ApiError, operatorCredentialReference, operatorSessionVersion } from "../lib/api"; import { SafetyDialog } from "./SafetyDialog"; import { TabList, TabPanel } from "./TabList"; import type { TabDescriptor } from "./TabList"; import type { CapabilityDeployment, CapabilityContractRecord, CapabilityEstate, DeploymentCandidate, GatewayInvokeResponse, GatewayRequest, OCRInvokeResponse, PlacementPlan, ProductionApproval, RuntimeProfile, SchedulerBudget, SchedulerPolicy, ServiceClient, ServiceClientCreated, SpeechTranscriptionResponse, VisionEmbeddingResponse, } from "../types"; type View = "overview" | "scheduler" | "requests" | "clients" | "promotion" | "playground"; type CapabilityReadFailure = { kind: "offline" | "error" | "unknown"; message: string; code: string; correlationId: string | null; retryable: boolean; nextAction: string; }; type CapabilityReadState = | { kind: "loading" } | { kind: "fresh"; lastUpdatedAt: number } | { kind: "unavailable"; failure: CapabilityReadFailure } | { kind: "stale"; failure: CapabilityReadFailure; lastUpdatedAt: number }; const capabilityViews: TabDescriptor[] = [ { key: "overview", label: "overview" }, { key: "scheduler", label: "scheduler" }, { key: "requests", label: "requests" }, { key: "clients", label: "clients" }, { key: "promotion", label: "promotion" }, { key: "playground", label: "playground" }, ]; export function CapabilityWorkspace({ onError, role = "viewer", targetDeploymentId }: { onError: (message: string | null) => void; role?: OperatorIdentity["role"]; targetDeploymentId?: string | null }) { const sessionReference = operatorCredentialReference(); const sessionActive = Boolean(sessionReference) && role === "admin"; const generation = operatorSessionVersion(); const [view, setView] = useState("overview"); const [deployments, setDeployments] = useState([]); const [contracts, setContracts] = useState([]); const [estate, setEstate] = useState([]); const [budgets, setBudgets] = useState([]); const [profiles, setProfiles] = useState([]); const [candidates, setCandidates] = useState([]); const [requests, setRequests] = useState([]); const [clients, setClients] = useState([]); const [approvals, setApprovals] = useState([]); const [plans, setPlans] = useState([]); const [policy, setPolicy] = useState(null); const adminToken = sessionReference; const adminUnlocked = sessionActive; const [loading, setLoading] = useState(true); const [refreshing, setRefreshing] = useState(false); const [publicRead, setPublicRead] = useState({ kind: "loading" }); const publicRequest = useRef(0); const publicSnapshotAt = useRef(null); const mounted = useRef(false); useEffect(() => { mounted.current = true; return () => { mounted.current = false; publicRequest.current += 1; }; }, []); const loadPublic = useCallback(async () => { const request = ++publicRequest.current; const session = operatorSessionVersion(); const current = () => mounted.current && request === publicRequest.current && session === operatorSessionVersion(); try { const [nextDeployments, nextBudgets, nextCandidates, nextEstate, nextProfiles, nextContracts] = await Promise.all([ api.capabilityDeployments(), api.scheduler(), api.deploymentCandidates(), api.capabilityEstate(), api.runtimeProfiles(), api.capabilityContracts(), ]); if (!current()) return; setContracts(nextContracts); setDeployments(nextDeployments); setBudgets(nextBudgets); setCandidates(nextCandidates); setEstate(nextEstate); setProfiles(nextProfiles); const lastUpdatedAt = Date.now(); publicSnapshotAt.current = lastUpdatedAt; setPublicRead({ kind: "fresh", lastUpdatedAt }); } catch (cause: unknown) { if (!current()) return; const failure = capabilityReadFailure(cause); const lastUpdatedAt = publicSnapshotAt.current; setPublicRead(lastUpdatedAt === null ? { kind: "unavailable", failure } : { kind: "stale", failure, lastUpdatedAt }); } }, []); const loadAdmin = useCallback(async (token: string) => { if (!sessionActive || generation !== operatorSessionVersion()) return; const [nextRequests, nextClients, nextApprovals, nextPlans, nextPolicy] = await Promise.all([ api.gatewayRequests(token), api.serviceClients(token), api.productionApprovals(token), api.placementHistory(token), api.schedulerPolicy(token), ]); if (generation !== operatorSessionVersion()) return; setRequests(nextRequests); setClients(nextClients); setApprovals(nextApprovals); setPlans(nextPlans); setPolicy(nextPolicy); }, [sessionActive, generation]); useEffect(() => { Promise.all([loadPublic(), sessionActive ? loadAdmin(sessionReference) : Promise.resolve()]) .catch((cause: unknown) => onError(errorMessage(cause))) .finally(() => setLoading(false)); }, [loadAdmin, loadPublic, onError, sessionActive, sessionReference]); useEffect(() => { const interval = window.setInterval(() => { void loadPublic(); if (adminUnlocked) loadAdmin(adminToken).catch((cause: unknown) => onError(errorMessage(cause))); }, 5000); return () => window.clearInterval(interval); }, [adminToken, adminUnlocked, loadAdmin, loadPublic, onError]); const refresh = async () => { onError(null); setRefreshing(true); try { await Promise.all([loadPublic(), sessionActive ? loadAdmin(adminToken) : Promise.resolve()]); } catch (cause: unknown) { onError(errorMessage(cause)); } finally { setRefreshing(false); } }; if (loading || publicRead.kind === "loading") return
Loading capability serving state…
; if (publicRead.kind === "unavailable") return ; return
{publicRead.kind === "stale" ? : null}
MULTI-CAPABILITY ESTATE

{contracts.length} declared contracts

Capability-first routing across RAG, document, vision and audio · no client-visible model names

{deployments.length} deployment recordsInspect health per exact binding below.
{view === "overview" ? : null} {view === "scheduler" ? <><> : null} {view === "requests" ? : null} {view === "clients" ? loadAdmin(adminToken)} onError={onError} /> : null} {view === "promotion" ? : null} {view === "playground" ? : null}
; } function CapabilityReadNotice({ failure, lastUpdatedAt, onRetry, refreshing }: { failure: CapabilityReadFailure; lastUpdatedAt?: number; onRetry: () => Promise; refreshing: boolean; }) { const hasSnapshot = lastUpdatedAt !== undefined; const stateLabel = failure.kind === "offline" ? "OFFLINE" : hasSnapshot ? "STALE" : failure.kind.toUpperCase(); const stateClass = failure.kind === "offline" ? "offline" : hasSnapshot ? "stale" : failure.kind === "unknown" ? "unknown" : "error"; return
{stateLabel}

{hasSnapshot ? "Capability data may be out of date" : "Capability serving state is unavailable"}

{hasSnapshot ?

The last successful snapshot was read {formatTime(new Date(lastUpdatedAt).toISOString())}. Counts below show that snapshot; the latest read failed.

:

No authoritative snapshot was loaded, so contract and deployment totals are unknown. A failed read does not mean the estate is empty.

}

Next action: {failure.nextAction}

; } function SchedulerView({ budgets, deployments, plans, policy, token, unlocked, onChanged, onError }: { budgets: SchedulerBudget[]; deployments: CapabilityDeployment[]; plans: PlacementPlan[]; policy: SchedulerPolicy | null; token: string; unlocked: boolean; onChanged: () => Promise; onError: (message: string | null) => void }) { const [latest, setLatest] = useState>({}); const [pendingUnload, setPendingUnload] = useState(null); const [unloadAcknowledged, setUnloadAcknowledged] = useState(false); const [unloadPending, setUnloadPending] = useState(false); const [unloadError, setUnloadError] = useState(null); const [unloadOutcome, setUnloadOutcome] = useState(null); if (!budgets.length) return
No accelerator telemetry is available for scheduling.
; const simulate = async (deploymentId: string) => { try { const plan = await api.dryRunPlacement(deploymentId, "interactive", token); setLatest((current) => ({ ...current, [deploymentId]: plan })); } catch (cause: unknown) { onError(errorMessage(cause)); } }; const previewUnload = (deployment: CapabilityDeployment) => { setUnloadAcknowledged(false); setUnloadError(null); setUnloadOutcome(null); setPendingUnload(deployment); }; const confirmUnload = async (deployment: CapabilityDeployment) => { // Guarded against a replayed handler so one acknowledgement can never evict residency twice. if (unloadPending) return; setUnloadPending(true); setUnloadError(null); try { await api.unloadDeployment(deployment.id, token); setPendingUnload(null); setUnloadAcknowledged(false); setUnloadOutcome(`Residency for ${deployment.capability}@${deployment.contract_version} was unloaded.`); await onChanged(); } catch (cause: unknown) { setUnloadError(errorMessage(cause)); } finally { setUnloadPending(false); } }; const changePolicy = async (deployment: CapabilityDeployment, value: string) => { try { await api.updateResidencyPolicy(deployment.id, value, token); await onChanged(); } catch (cause: unknown) { onError(errorMessage(cause)); } }; const pauseLab = async () => { try { await api.updateSchedulerPolicy(!Boolean(policy?.configuration.lab_paused), token); await onChanged(); } catch (cause: unknown) { onError(errorMessage(cause)); } }; return
{budgets.map((budget) => { const segments = [ ["External / unmanaged", budget.external_vram_bytes], ["Safety reserve", budget.safety_reserve_bytes], ["ModelForge resident", budget.resident_vram_bytes], ["Active leases", budget.leased_vram_bytes], ["Schedulable free", budget.schedulable_free_vram_bytes], ] as const; return
GPU SCHEDULING COCKPIT · {budget.node_name}

{budget.accelerator_name}

{budget.pressure_state}
{formatBytes(budget.total_vram_bytes)}Total VRAM · attribution {budget.attribution_confidence}
{segments.map(([label, bytes], index) => )}
{segments.map(([label, bytes]) => )}
Observed {formatBytes(budget.observed_used_vram_bytes)} · invariant delta {formatBytes(budget.invariant_delta_bytes)} · policy {budget.policy_revision} · telemetry {formatTime(budget.observed_at)}. External usage is observation-only and has no controls.
; })}
{unlocked ? <>
RESIDENCY MAP · WHAT CAN RUN NOW?

Managed capability placements

{deployments.map((item) => { const plan = latest[item.id]; return
{item.capability}@{item.contract_version} · {item.residency.state.toUpperCase()}{item.production ? "production" : "lab"} · {formatBytes(item.residency.measured_resident_vram_bytes)} · {item.residency.active_requests} active · cold load {formatMs(item.resource_envelope.cold_load_time_ms)}{plan ? {plan.verdict} · {plan.reason_codes.join(", ")} · after {formatBytes(plan.headroom_after_bytes)}{plan.evictions.length ? ` · evict ${plan.evictions.map((entry) => entry.capability).join(", ")}` : ""} : null}
; })}
PLACEMENT HISTORY

Recent bounded decisions

{plans.slice(0, 20).map((plan) => )}
: null} {unloadOutcome ?

{unloadOutcome}

: null} {pendingUnload ? setPendingUnload(null)} onConfirm={() => void confirmUnload(pendingUnload)} /> : null}
; } function RequestHistory({ requests }: { requests: GatewayRequest[] }) { if (!requests.length) return
No gateway request metadata exists yet. Input text is never displayed or persisted.
; return
PRIVACY-PRESERVING HISTORY

Gateway requests

RequestClientStatePathQueueTotalTime
{requests.map((request) =>
{request.request_id.slice(0, 8)}…{request.client ?? "rejected identity"}{request.failure_code ?? request.status}{request.cold == null ? "—" : request.cold ? "cold" : "warm"}{formatMs(request.queue_time_ms)}{formatMs(request.total_latency_ms)}{formatTime(request.created_at)}
)}
; } function Clients({ clients, token, onChanged, onError }: { clients: ServiceClient[]; token: string; onChanged: () => Promise; onError: (message: string | null) => void }) { const [name, setName] = useState("modelforge-m5-acceptance"); const [rpm, setRpm] = useState(60); const [created, setCreated] = useState(null); const submit = async (event: FormEvent) => { event.preventDefault(); onError(null); try { setCreated(await api.createServiceClient({ name, allowed_capabilities: ["rag.embedding@1"], requests_per_minute: rpm, max_concurrent_requests: 1 }, token)); await onChanged(); } catch (cause: unknown) { onError(errorMessage(cause)); } }; const revoke = async (clientId: string) => { onError(null); try { await api.revokeServiceCredential(clientId, token); await onChanged(); } catch (cause: unknown) { onError(errorMessage(cause)); } }; return
SCOPED IDENTITY

Create service client

Scope is fixed to rag.embedding@1; concurrency starts conservatively at one.

{created ?
Copy this credential now{created.credential}Only its hash is stored. It cannot be retrieved again.
: null}
CLIENTS

Authorized consumers

{clients.length ? clients.map((client) =>
{client.name}{client.allowed_capabilities.join(", ")} · {client.requests_per_minute}/min · last used {formatTime(client.last_used_at)}
) :

No service clients created.

}
; } type ModalityResult = GatewayInvokeResponse | OCRInvokeResponse | VisionEmbeddingResponse | SpeechTranscriptionResponse; async function fileBase64(file: File): Promise { const bytes = new Uint8Array(await file.arrayBuffer()); let binary = ""; for (const byte of bytes) binary += String.fromCharCode(byte); return btoa(binary); } function Playground() { const [capability, setCapability] = useState<"rag.embedding" | "document.ocr" | "vision.embedding" | "speech.transcription">("rag.embedding"); const [credential, setCredential] = useState(""); const [input, setInput] = useState("ModelForge capability gateway test"); const [file, setFile] = useState(null); const [result, setResult] = useState(null); const [failure, setFailure] = useState(null); const [running, setRunning] = useState(false); const invoke = async (event: FormEvent) => { event.preventDefault(); setFailure(null); setRunning(true); try { if (capability === "rag.embedding") setResult(await api.invokeEmbedding(input, credential)); else { if (!file) throw new Error("Choose a local fixture first"); const encoded = await fileBase64(file); if (capability === "document.ocr") { if (file.type !== "image/png" && file.type !== "image/jpeg") throw new Error("OCR accepts PNG or JPEG"); setResult(await api.invokeOcr(encoded, file.type, credential)); } else if (capability === "vision.embedding") { if (file.type !== "image/png" && file.type !== "image/jpeg") throw new Error("Vision accepts PNG or JPEG"); setResult(await api.invokeVisionEmbedding(encoded, file.type, credential)); } else { if (file.type !== "audio/wav" && !file.name.toLowerCase().endsWith(".wav")) throw new Error("Transcription accepts WAV"); setResult(await api.invokeTranscription(encoded, credential)); } } } catch (cause: unknown) { setFailure(errorMessage(cause)); } finally { setRunning(false); } }; const output = result ? ("data" in result ? `${result.data.length} × ${result.dimension} finite values` : "text" in result ? result.text || "valid empty text" : "typed output") : ""; return
REAL GATEWAY PATH

Capability playground

The client selects a capability contract. No model name, path, or runtime engine is accepted here.

{capability === "rag.embedding" ?