Show Ollama model host sources

This commit is contained in:
mmckeen-nv
2026-05-19 18:48:36 -07:00
parent 87c27a5006
commit 635cdae9a0
3 changed files with 137 additions and 35 deletions
+95 -22
View File
@@ -31,9 +31,13 @@ type OllamaTag = {
}
}
type OllamaHostKind = "custom" | "linux" | "win" | "wsl2"
type OllamaProbeCandidate = {
baseUrl: string
source: string
hostKind: OllamaHostKind
hostLabel: string
via: "fetch" | "windows-curl"
}
@@ -45,6 +49,24 @@ type OllamaProbeResult = OllamaProbeCandidate & {
elapsedMs: number
}
type OllamaModelRow = {
id: string
name: string
model: string
modifiedAt: string | null
size: number | null
sizeLabel: string | null
digest: string | null
family: string | null
parameterSize: string | null
quantization: string | null
hostKind: OllamaHostKind
hostLabel: string
source: string
baseUrl: string
via: OllamaProbeCandidate["via"]
}
function parsePositiveInteger(value: string | undefined, fallback: number) {
const parsed = Number.parseInt(String(value || ""), 10)
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback
@@ -102,11 +124,14 @@ function formatBytes(size: unknown) {
return `${value >= 10 || unit === 0 ? value.toFixed(0) : value.toFixed(1)} ${units[unit]}`
}
function modelRowsFromTags(data: any) {
function modelRowsFromTags(data: any, origin: OllamaProbeCandidate): OllamaModelRow[] {
return Array.isArray(data?.models)
? data.models.map((item: OllamaTag) => ({
name: item.model || item.name || "",
model: item.model || item.name || "",
? data.models.map((item: OllamaTag) => {
const name = item.model || item.name || ""
return {
id: `${origin.hostKind}:${name}`,
name,
model: name,
modifiedAt: item.modified_at || null,
size: typeof item.size === "number" ? item.size : null,
sizeLabel: formatBytes(item.size),
@@ -114,7 +139,13 @@ function modelRowsFromTags(data: any) {
family: item.details?.family || null,
parameterSize: item.details?.parameter_size || null,
quantization: item.details?.quantization_level || null,
})).filter((item: { name: string }) => item.name)
hostKind: origin.hostKind,
hostLabel: origin.hostLabel,
source: origin.source,
baseUrl: origin.baseUrl,
via: origin.via,
}
}).filter((item: { name: string }) => item.name)
: []
}
@@ -170,17 +201,29 @@ function hostUrl(host: string, port = DEFAULT_OLLAMA_PORT) {
return `http://${formattedHost}:${port}`
}
function addFetchCandidate(candidates: OllamaProbeCandidate[], value: string | undefined, source: string) {
function addFetchCandidate(
candidates: OllamaProbeCandidate[],
value: string | undefined,
source: string,
hostKind: OllamaHostKind,
hostLabel: string,
) {
const baseUrl = normalizeOllamaBaseUrl(value)
if (!baseUrl) return
candidates.push({ baseUrl, source, via: "fetch" })
candidates.push({ baseUrl, source, hostKind, hostLabel, via: "fetch" })
}
function addWindowsCurlCandidate(candidates: OllamaProbeCandidate[], value: string | undefined, source: string) {
function addWindowsCurlCandidate(
candidates: OllamaProbeCandidate[],
value: string | undefined,
source: string,
hostKind: OllamaHostKind,
hostLabel: string,
) {
if (process.env.OPENSHELL_OLLAMA_WINDOWS_INTEROP === "0") return
const baseUrl = normalizeOllamaBaseUrl(value)
if (!baseUrl) return
candidates.push({ baseUrl, source, via: "windows-curl" })
candidates.push({ baseUrl, source, hostKind, hostLabel, via: "windows-curl" })
}
function dedupeCandidates(candidates: OllamaProbeCandidate[]) {
@@ -211,23 +254,25 @@ async function buildProbeCandidates() {
const candidates: OllamaProbeCandidate[] = []
const wsl = await isWslRuntime()
addFetchCandidate(candidates, process.env.OPENSHELL_OLLAMA_BASE_URL, "OPENSHELL_OLLAMA_BASE_URL")
addFetchCandidate(candidates, process.env.OLLAMA_BASE_URL, "OLLAMA_BASE_URL")
addFetchCandidate(candidates, process.env.OLLAMA_HOST, "OLLAMA_HOST")
addFetchCandidate(candidates, process.env.OPENSHELL_OLLAMA_BASE_URL, "OPENSHELL_OLLAMA_BASE_URL", "custom", "CUSTOM")
addFetchCandidate(candidates, process.env.OLLAMA_BASE_URL, "OLLAMA_BASE_URL", "custom", "CUSTOM")
addFetchCandidate(candidates, process.env.OLLAMA_HOST, "OLLAMA_HOST", "custom", "CUSTOM")
for (const host of customHostCandidates()) {
addFetchCandidate(candidates, customHostUrl(host), "OPENSHELL_OLLAMA_HOSTS")
addFetchCandidate(candidates, customHostUrl(host), "OPENSHELL_OLLAMA_HOSTS", "custom", "CUSTOM")
}
addFetchCandidate(candidates, DEFAULT_OLLAMA_URL, wsl ? "wsl-localhost" : "localhost")
addFetchCandidate(candidates, DEFAULT_OLLAMA_URL, wsl ? "wsl-localhost" : "localhost", wsl ? "wsl2" : "linux", wsl ? "WSL2" : "LINUX")
if (wsl) {
for (const host of await readWslHostCandidates()) {
addFetchCandidate(candidates, hostUrl(host), "wsl-windows-host")
addFetchCandidate(candidates, hostUrl(host), "wsl-windows-host", "win", "WIN")
}
addFetchCandidate(candidates, hostUrl("host.docker.internal"), "host.docker.internal")
addFetchCandidate(candidates, hostUrl("host.docker.internal"), "host.docker.internal", "win", "WIN")
addWindowsCurlCandidate(
candidates,
process.env.OPENSHELL_WINDOWS_OLLAMA_BASE_URL || DEFAULT_OLLAMA_URL,
"windows-localhost",
"win",
"WIN",
)
}
@@ -239,6 +284,8 @@ function publicProbeResult(result: OllamaProbeResult) {
ok: result.ok,
baseUrl: result.baseUrl,
source: result.source,
hostKind: result.hostKind,
hostLabel: result.hostLabel,
via: result.via,
status: result.status ?? null,
elapsedMs: result.elapsedMs,
@@ -246,6 +293,24 @@ function publicProbeResult(result: OllamaProbeResult) {
}
}
function aggregateModels(results: OllamaProbeResult[]) {
const seen = new Set<string>()
const models: OllamaModelRow[] = []
for (const result of results.filter((item) => item.ok)) {
for (const model of modelRowsFromTags(result.data, result)) {
const key = `${model.hostKind}:${model.name}`
if (seen.has(key)) continue
seen.add(key)
models.push(model)
}
}
return models.sort((a, b) => {
const sourceOrder = { wsl2: 0, win: 1, linux: 2, custom: 3 } as Record<OllamaHostKind, number>
const sourceDiff = sourceOrder[a.hostKind] - sourceOrder[b.hostKind]
return sourceDiff || a.name.localeCompare(b.name)
})
}
function probeErrorMessage(error: unknown) {
if (error instanceof Error && error.name === "AbortError") return "Ollama probe timed out"
return error instanceof Error ? error.message : "Failed to fetch Ollama models"
@@ -344,16 +409,24 @@ export async function GET() {
])
const results = await Promise.all(candidates.map(probeCandidate))
const available = results.find((result) => result.ok)
const availableResults = results.filter((result) => result.ok)
if (available) {
const models = modelRowsFromTags(available.data)
if (availableResults.length > 0) {
const models = aggregateModels(results)
return NextResponse.json({
ok: true,
available: true,
baseUrl: available.baseUrl,
source: available.source,
via: available.via,
baseUrl: availableResults[0].baseUrl,
source: availableResults[0].source,
via: availableResults[0].via,
sources: availableResults.map((result) => ({
baseUrl: result.baseUrl,
source: result.source,
hostKind: result.hostKind,
hostLabel: result.hostLabel,
via: result.via,
count: modelRowsFromTags(result.data, result).length,
})),
models,
count: models.length,
elapsedMs: Date.now() - startedAt,
+21 -4
View File
@@ -25,6 +25,7 @@ type InferenceResponse = {
}
type OllamaModel = {
id?: string
name: string
model: string
modifiedAt: string | null
@@ -32,6 +33,8 @@ type OllamaModel = {
family: string | null
parameterSize: string | null
quantization: string | null
hostLabel?: string | null
hostKind?: string | null
}
const providerTypeOptions = [
@@ -72,6 +75,19 @@ function FieldLabel({ children }: { children: React.ReactNode }) {
return <label className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">{children}</label>
}
function OllamaHostBadge({ label }: { label?: string | null }) {
if (!label) return null
return <span className="shrink-0 rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] px-1.5 py-0.5 text-[9px] font-mono uppercase tracking-wider text-[var(--foreground-dim)]">[{label}]</span>
}
function ollamaModelKey(item: OllamaModel) {
return item.id || `${item.hostLabel || "LOCAL"}:${item.name}`
}
function ollamaSourceSummary(models: OllamaModel[]) {
return Array.from(new Set(models.map((item) => item.hostLabel).filter(Boolean))).join(" + ")
}
function VllmAdvancedConfiguration({
model,
port,
@@ -461,8 +477,9 @@ export default function InferenceEndpointPanel() {
return
}
const models = Array.isArray(data.models) ? data.models : []
const sources = ollamaSourceSummary(models)
setOllamaModels(models)
setOllamaMessage(models.length > 0 ? `${models.length} Ollama model${models.length === 1 ? "" : "s"} available. Click one to load the Ollama endpoint preset.` : "Ollama is reachable but has no models installed.")
setOllamaMessage(models.length > 0 ? `${models.length} Ollama model${models.length === 1 ? "" : "s"} available${sources ? ` from ${sources}` : ""}. Click one to load the Ollama endpoint preset.` : "Ollama is reachable but has no models installed.")
if (models[0]?.name) setModel((current) => current || models[0].name)
} catch (error) {
setOllamaModels([])
@@ -627,8 +644,8 @@ export default function InferenceEndpointPanel() {
{ollamaModels.length > 0 ? (
<div className="mt-3 grid grid-cols-1 md:grid-cols-2 gap-2">
{ollamaModels.map((item) => (
<button key={item.name} type="button" onClick={() => applyOllamaModelPreset(item.name)} className={`rounded-sm border p-3 text-left ${isOllamaEndpoint && model === item.name ? "border-[var(--nvidia-green)] bg-[rgba(118,185,0,0.08)]" : "border-[var(--border-subtle)] bg-[var(--background)] hover:border-[var(--nvidia-green)]"}`}>
<div className="text-xs font-mono text-[var(--foreground)]">{item.name}</div>
<button key={ollamaModelKey(item)} type="button" onClick={() => applyOllamaModelPreset(item.name)} className={`rounded-sm border p-3 text-left ${isOllamaEndpoint && model === item.name ? "border-[var(--nvidia-green)] bg-[rgba(118,185,0,0.08)]" : "border-[var(--border-subtle)] bg-[var(--background)] hover:border-[var(--nvidia-green)]"}`}>
<div className="flex min-w-0 items-center gap-2 text-xs font-mono text-[var(--foreground)]"><span className="truncate">{item.name}</span><OllamaHostBadge label={item.hostLabel} /></div>
<div className="mt-1 text-[11px] text-[var(--foreground-dim)]">
{[item.parameterSize, item.quantization, item.sizeLabel].filter(Boolean).join(" · ") || item.family || "local model"}
</div>
@@ -655,7 +672,7 @@ export default function InferenceEndpointPanel() {
<FieldLabel>Model</FieldLabel>
{isOllamaEndpoint && ollamaModels.length > 0 ? (
<select value={model} onChange={(event) => setModel(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)]">
{ollamaModels.map((item) => <option key={item.name} value={item.name}>{item.name}</option>)}
{ollamaModels.map((item) => <option key={ollamaModelKey(item)} value={item.name}>{item.hostLabel ? `[${item.hostLabel}] ` : ""}{item.name}</option>)}
</select>
) : (
<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)]" />
+14 -2
View File
@@ -18,10 +18,13 @@ type InferenceRouteStatus = {
}
type OllamaModel = {
id?: string
name: string
sizeLabel: string | null
parameterSize: string | null
quantization: string | null
hostLabel?: string | null
hostKind?: string | null
}
type SandboxInferenceRoute = {
@@ -57,6 +60,15 @@ type SandboxInferenceConfig = {
updatedAt: string | null
}
function OllamaHostBadge({ label }: { label?: string | null }) {
if (!label) return null
return <span className="shrink-0 rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] px-1.5 py-0.5 text-[9px] font-mono uppercase tracking-wider text-[var(--foreground-dim)]">[{label}]</span>
}
function ollamaModelKey(item: OllamaModel) {
return item.id || `${item.hostLabel || "LOCAL"}:${item.name}`
}
function makeRoute(provider: string, model: string, label = ""): SandboxInferenceRoute {
return {
id: `${provider}::${model}`,
@@ -420,8 +432,8 @@ export default function SandboxInferencePanel({
) : (
<div className="mt-3 grid grid-cols-1 md:grid-cols-2 gap-2">
{ollamaModels.map((item) => (
<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>
<button key={ollamaModelKey(item)} 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="flex min-w-0 items-center gap-2 text-xs font-mono text-[var(--foreground)]"><span className="truncate">{item.name}</span><OllamaHostBadge label={item.hostLabel} /></div>
<div className="mt-1 text-[11px] text-[var(--foreground-dim)]">
{[item.parameterSize, item.quantization, item.sizeLabel].filter(Boolean).join(" · ") || "local model"}
</div>