Add inference endpoint configuration

This commit is contained in:
Codex
2026-04-24 17:45:40 +00:00
parent c33310fecb
commit ecaefcf7c8
4 changed files with 460 additions and 0 deletions
+201
View File
@@ -0,0 +1,201 @@
import { NextResponse } from "next/server"
import { execFile } from "node:child_process"
import { existsSync } from "node:fs"
import { promisify } from "node:util"
const execFileAsync = promisify(execFile)
const HOME = process.env.HOME || ""
const OPENSHELL_BIN_CANDIDATES = [
process.env.OPENSHELL_BIN,
HOME ? `${HOME}/.local/bin/openshell` : undefined,
"/usr/local/bin/openshell",
"/opt/homebrew/bin/openshell",
].filter((value): value is string => Boolean(value))
const OPENSHELL_BIN =
OPENSHELL_BIN_CANDIDATES.find((candidate) => existsSync(candidate)) ?? OPENSHELL_BIN_CANDIDATES[0]
const HOST_PATH = [
`${HOME}/.local/bin`,
`${HOME}/.nvm/versions/node/v22.22.2/bin`,
`${HOME}/.nvm/versions/node/v22.22.1/bin`,
"/usr/local/bin",
"/opt/homebrew/bin",
"/usr/bin",
"/bin",
process.env.PATH || "",
].filter(Boolean).join(":")
const PROVIDER_TYPES = new Set(["openai", "anthropic", "nvidia", "generic", "claude", "opencode", "codex", "copilot", "gitlab", "github", "outlook"])
type InferenceRoute = {
configured: boolean
provider: string | null
model: string | null
version: string | null
timeout: string | null
}
function stripAnsi(value: string) {
return value.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, "")
}
async function runOpenShell(args: string[]) {
const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, {
env: {
...process.env,
PATH: HOST_PATH,
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
},
timeout: 60000,
maxBuffer: 20 * 1024 * 1024,
})
return { stdout: stripAnsi(String(stdout).trim()), stderr: stripAnsi(String(stderr).trim()) }
}
function readField(output: string, label: string) {
const match = output.match(new RegExp(`^\\s*${label}:\\s*(.+?)\\s*$`, "im"))
return match ? match[1].trim() : null
}
function parseRoute(output: string, label: "Gateway inference" | "System inference"): InferenceRoute {
const otherLabel = label === "Gateway inference" ? "System inference" : "Gateway inference"
const pattern = new RegExp(`${label}:\\s*([\\s\\S]*?)(?:\\n\\s*${otherLabel}:|$)`, "i")
const section = output.match(pattern)?.[1] ?? ""
if (!section || /Not configured/i.test(section)) {
return { configured: false, provider: null, model: null, version: null, timeout: null }
}
return {
configured: true,
provider: readField(section, "Provider"),
model: readField(section, "Model"),
version: readField(section, "Version"),
timeout: readField(section, "Timeout"),
}
}
function parseProvider(output: string) {
return {
id: readField(output, "Id"),
name: readField(output, "Name"),
type: readField(output, "Type"),
credentialKeys: (readField(output, "Credential keys") || "")
.split(",")
.map((item) => item.trim())
.filter(Boolean),
configKeys: (readField(output, "Config keys") || "")
.split(",")
.map((item) => item.trim())
.filter(Boolean),
}
}
async function readProviders() {
const { stdout } = await runOpenShell(["provider", "list", "--names"])
const names = stdout.split(/\r?\n/).map((line) => line.trim()).filter(Boolean)
const providers = await Promise.all(names.map(async (name) => {
try {
const details = await runOpenShell(["provider", "get", name])
return parseProvider(details.stdout)
} catch {
return { id: null, name, type: null, credentialKeys: [], configKeys: [] }
}
}))
return providers
}
function validateName(name: unknown) {
if (typeof name !== "string" || !name.trim()) throw new Error("provider name is required")
const value = name.trim()
if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,62}$/.test(value)) {
throw new Error("provider name must start with a letter or number and contain only letters, numbers, dots, underscores, or hyphens")
}
return value
}
function validateProviderType(type: unknown) {
const value = typeof type === "string" && type.trim() ? type.trim() : "openai"
if (!PROVIDER_TYPES.has(value)) throw new Error(`unsupported provider type: ${value}`)
return value
}
function optionalString(value: unknown) {
return typeof value === "string" ? value.trim() : ""
}
function providerCredentialArg(credentialKey: string, apiKey: string) {
return apiKey ? `${credentialKey}=${apiKey}` : credentialKey
}
export async function GET() {
try {
const [inference, providers] = await Promise.all([
runOpenShell(["inference", "get"]),
readProviders(),
])
return NextResponse.json({
ok: true,
gateway: parseRoute(inference.stdout, "Gateway inference"),
system: parseRoute(inference.stdout, "System inference"),
providers,
raw: inference.stdout,
})
} catch (error) {
const message = error instanceof Error ? error.message : "Failed to read inference configuration"
return NextResponse.json({ ok: false, error: message }, { status: 500 })
}
}
export async function POST(request: Request) {
try {
const body = await request.json()
const name = validateName(body?.name)
const type = validateProviderType(body?.type)
const model = optionalString(body?.model)
const baseUrl = optionalString(body?.baseUrl)
const apiKey = optionalString(body?.apiKey)
const credentialKey = optionalString(body?.credentialKey) || "OPENAI_API_KEY"
const makeActive = body?.makeActive !== false
const system = body?.route === "system" || body?.system === true
const noVerify = body?.noVerify !== false
const timeout = Number(body?.timeout ?? 0)
if (makeActive && !model) throw new Error("model is required when making the endpoint active")
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)
const existingNames = (await readProviders()).map((provider) => provider.name).filter(Boolean)
const providerArgs = existingNames.includes(name)
? ["provider", "update", name]
: ["provider", "create", "--name", name, "--type", type]
if (!existingNames.includes(name) && !apiKey) providerArgs.push("--from-existing")
if (apiKey) providerArgs.push("--credential", providerCredentialArg(credentialKey, apiKey))
if (baseUrl) providerArgs.push("--config", `OPENAI_BASE_URL=${baseUrl}`)
const providerResult = await runOpenShell(providerArgs)
let inferenceResult = null
if (makeActive) {
const routeArgs = ["inference", "set", "--provider", name, "--model", model]
if (system) routeArgs.push("--system")
if (noVerify) routeArgs.push("--no-verify")
if (Number.isFinite(timeout) && timeout >= 0) routeArgs.push("--timeout", String(timeout))
inferenceResult = await runOpenShell(routeArgs)
}
const refreshed = await runOpenShell(["inference", "get"])
return NextResponse.json({
ok: true,
provider: name,
active: makeActive,
gateway: parseRoute(refreshed.stdout, "Gateway inference"),
system: parseRoute(refreshed.stdout, "System inference"),
stdout: [providerResult.stdout, inferenceResult?.stdout].filter(Boolean).join("\n\n"),
stderr: [providerResult.stderr, inferenceResult?.stderr].filter(Boolean).join("\n\n"),
})
} catch (error) {
const message = error instanceof Error ? error.message : "Failed to save inference endpoint"
return NextResponse.json({ ok: false, error: message }, { status: /required|unsupported|must|URL/.test(message) ? 400 : 500 })
}
}
+247
View File
@@ -0,0 +1,247 @@
"use client"
import { useEffect, useMemo, useState } from "react"
type InferenceRoute = {
configured: boolean
provider: string | null
model: string | null
version: string | null
timeout: string | null
}
type ProviderSummary = {
id: string | null
name: string | null
type: string | null
credentialKeys: string[]
configKeys: string[]
}
type InferenceResponse = {
gateway: InferenceRoute
system: InferenceRoute
providers: ProviderSummary[]
}
const providerTypeOptions = [
{ value: "openai", label: "OpenAI compatible" },
{ value: "nvidia", label: "NVIDIA" },
{ value: "anthropic", label: "Anthropic" },
{ value: "generic", label: "Generic" },
]
function emptyRoute(): InferenceRoute {
return { configured: false, provider: null, model: null, version: null, timeout: null }
}
function FieldLabel({ children }: { children: React.ReactNode }) {
return <label className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">{children}</label>
}
export default function InferenceEndpointPanel() {
const [loading, setLoading] = useState(true)
const [saving, setSaving] = useState(false)
const [message, setMessage] = useState("")
const [providers, setProviders] = useState<ProviderSummary[]>([])
const [gateway, setGateway] = useState<InferenceRoute>(emptyRoute)
const [system, setSystem] = useState<InferenceRoute>(emptyRoute)
const [name, setName] = useState("nvidia-nim")
const [type, setType] = useState("openai")
const [model, setModel] = useState("nvidia/nemotron-3-super-120b-a12b")
const [baseUrl, setBaseUrl] = useState("https://integrate.api.nvidia.com/v1")
const [credentialKey, setCredentialKey] = useState("NVIDIA_API_KEY")
const [apiKey, setApiKey] = useState("")
const [route, setRoute] = useState<"gateway" | "system">("gateway")
const [timeout, setTimeoutValue] = useState("0")
const [noVerify, setNoVerify] = useState(true)
const activeProvider = useMemo(
() => providers.find((provider) => provider.name === gateway.provider) ?? null,
[providers, gateway.provider]
)
async function load() {
try {
setLoading(true)
setMessage("")
const response = await fetch("/api/inference", { cache: "no-store" })
const data = await response.json() as InferenceResponse & { error?: string }
if (!response.ok) throw new Error(data.error || "Failed to load inference configuration")
setGateway(data.gateway ?? emptyRoute())
setSystem(data.system ?? emptyRoute())
setProviders(Array.isArray(data.providers) ? data.providers : [])
if (data.gateway?.provider) setName(data.gateway.provider)
if (data.gateway?.model) setModel(data.gateway.model)
} catch (error) {
setMessage(error instanceof Error ? error.message : "Failed to load inference configuration")
} finally {
setLoading(false)
}
}
useEffect(() => {
load()
}, [])
function selectProvider(provider: ProviderSummary) {
if (!provider.name) return
setName(provider.name)
setType(provider.type || "openai")
setCredentialKey(provider.credentialKeys[0] || "OPENAI_API_KEY")
setApiKey("")
setMessage(`Loaded ${provider.name}. Enter a model and save to make it active.`)
}
async function saveEndpoint() {
try {
setSaving(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,
timeout: Number(timeout || "0"),
noVerify,
makeActive: true,
}),
})
const data = await response.json()
if (!response.ok) throw new Error(data.error || "Failed to save inference endpoint")
setGateway(data.gateway ?? gateway)
setSystem(data.system ?? system)
setApiKey("")
setMessage(`Inference endpoint '${name}' saved and routed to ${route === "system" ? "system" : "sandbox"} inference.`)
await load()
} catch (error) {
setMessage(error instanceof Error ? error.message : "Failed to save inference endpoint")
} finally {
setSaving(false)
}
}
return (
<div className="panel p-8">
<div className="flex items-start justify-between gap-6 border-b border-[var(--border-subtle)] pb-5">
<div>
<h2 className="text-lg font-semibold text-[var(--nvidia-green)] uppercase tracking-wider">
INFERENCE ENDPOINTS
</h2>
<p className="mt-2 max-w-3xl text-sm text-[var(--foreground-dim)]">
Configure the OpenShell gateway route used by sandboxes through inference.local.
</p>
</div>
<button
onClick={load}
disabled={loading || saving}
className="px-4 py-2 rounded-sm bg-[var(--background-tertiary)] text-[var(--foreground)] text-xs font-mono uppercase tracking-wider hover:bg-[var(--background-secondary)] disabled:opacity-50"
>
Refresh
</button>
</div>
{loading ? (
<div className="py-12 text-xs uppercase tracking-wider text-[var(--foreground-dim)]">Loading inference routes...</div>
) : (
<div className="mt-6 space-y-6">
<div className="grid grid-cols-1 lg:grid-cols-2 gap-4">
<div className="metric p-4">
<p className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">Sandbox Route</p>
<p className="mt-2 text-sm font-mono text-[var(--foreground)]">{gateway.configured ? gateway.provider : "not configured"}</p>
<p className="mt-1 text-xs font-mono text-[var(--foreground-dim)]">{gateway.model || "No model selected"}</p>
{gateway.timeout && <p className="mt-1 text-[11px] text-[var(--foreground-dim)]">Timeout {gateway.timeout}</p>}
</div>
<div className="metric p-4">
<p className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">System Route</p>
<p className="mt-2 text-sm font-mono text-[var(--foreground)]">{system.configured ? system.provider : "not configured"}</p>
<p className="mt-1 text-xs font-mono text-[var(--foreground-dim)]">{system.model || "No model selected"}</p>
{system.timeout && <p className="mt-1 text-[11px] text-[var(--foreground-dim)]">Timeout {system.timeout}</p>}
</div>
</div>
<div className="grid grid-cols-1 xl:grid-cols-[minmax(0,1fr)_360px] gap-6">
<section className="space-y-4">
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div className="space-y-2">
<FieldLabel>Provider Name</FieldLabel>
<input value={name} onChange={(event) => setName(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>
<div className="space-y-2">
<FieldLabel>Provider Type</FieldLabel>
<select value={type} onChange={(event) => setType(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)]">
{providerTypeOptions.map((option) => <option key={option.value} value={option.value}>{option.label}</option>)}
</select>
</div>
<div className="space-y-2 md:col-span-2">
<FieldLabel>Model</FieldLabel>
<input value={model} onChange={(event) => setModel(event.target.value)} placeholder="provider/model-name" 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 md:col-span-2">
<FieldLabel>Endpoint URL</FieldLabel>
<input value={baseUrl} onChange={(event) => setBaseUrl(event.target.value)} placeholder="https://example.com/v1" 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">
<FieldLabel>Credential Env Key</FieldLabel>
<input value={credentialKey} onChange={(event) => setCredentialKey(event.target.value.toUpperCase())} 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">
<FieldLabel>API Key</FieldLabel>
<input value={apiKey} onChange={(event) => setApiKey(event.target.value)} type="password" placeholder={activeProvider ? "leave blank to keep stored key" : "stored in OpenShell provider"} 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">
<FieldLabel>Route</FieldLabel>
<select value={route} onChange={(event) => setRoute(event.target.value as "gateway" | "system")} 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="gateway">Sandbox inference</option>
<option value="system">System inference</option>
</select>
</div>
<div className="space-y-2">
<FieldLabel>Timeout Seconds</FieldLabel>
<input value={timeout} onChange={(event) => setTimeoutValue(event.target.value)} inputMode="numeric" 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>
<label className="flex items-center gap-3 text-sm text-[var(--foreground)]">
<input type="checkbox" checked={noVerify} onChange={(event) => setNoVerify(event.target.checked)} />
Skip provider verification while saving
</label>
<div className="flex items-center gap-3">
<button onClick={saveEndpoint} 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 Endpoint"}
</button>
{message && <p className="text-xs text-[var(--foreground-dim)] whitespace-pre-wrap">{message}</p>}
</div>
</section>
<section className="space-y-3">
<h3 className="text-xs uppercase tracking-wider text-[var(--foreground)]">Configured Providers</h3>
{providers.length === 0 ? (
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] p-4 text-xs text-[var(--foreground-dim)]">No providers configured.</div>
) : (
<div className="space-y-2">
{providers.map((provider) => (
<button key={provider.name || provider.id} onClick={() => selectProvider(provider)} className="w-full rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] p-3 text-left hover:border-[var(--nvidia-green)]">
<div className="flex items-center justify-between gap-3">
<span className="text-sm font-mono text-[var(--foreground)]">{provider.name}</span>
{provider.name === gateway.provider && <span className="text-[10px] uppercase tracking-wider text-[var(--nvidia-green)]">active</span>}
</div>
<p className="mt-1 text-[11px] text-[var(--foreground-dim)]">{provider.type || "unknown"} · credentials {provider.credentialKeys.length} · config {provider.configKeys.length}</p>
</button>
))}
</div>
)}
</section>
</div>
</div>
)}
</div>
)
}
+2
View File
@@ -3,6 +3,7 @@ import { useEffect, useMemo, useState } from 'react'
import Sidebar from './components/Sidebar'
import SandboxList from './components/SandboxList'
import ConfigurationPanel from './components/ConfigurationPanel'
import InferenceEndpointPanel from './components/InferenceEndpointPanel'
import { useSandboxInventory } from './hooks/useSandboxInventory'
import {
createHydrationSafeDashboardSessionState,
@@ -200,6 +201,7 @@ export default function Dashboard() {
</div>
</div>
</div>
<InferenceEndpointPanel />
</div>
) : isCreateMode ? (
<div className="space-y-6">