diff --git a/app/api/ollama/models/route.ts b/app/api/ollama/models/route.ts index 21f6745..4805562 100644 --- a/app/api/ollama/models/route.ts +++ b/app/api/ollama/models/route.ts @@ -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,19 +124,28 @@ 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 || "", - modifiedAt: item.modified_at || null, - size: typeof item.size === "number" ? item.size : null, - sizeLabel: formatBytes(item.size), - digest: item.digest || null, - family: item.details?.family || null, - parameterSize: item.details?.parameter_size || null, - quantization: item.details?.quantization_level || null, - })).filter((item: { name: string }) => 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), + digest: item.digest || null, + family: item.details?.family || null, + parameterSize: item.details?.parameter_size || null, + quantization: item.details?.quantization_level || null, + 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() + 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 + 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, diff --git a/app/components/InferenceEndpointPanel.tsx b/app/components/InferenceEndpointPanel.tsx index ae216c8..95ec7d7 100644 --- a/app/components/InferenceEndpointPanel.tsx +++ b/app/components/InferenceEndpointPanel.tsx @@ -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 } +function OllamaHostBadge({ label }: { label?: string | null }) { + if (!label) return null + return [{label}] +} + +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 ? (
{ollamaModels.map((item) => ( -