Harden Windows Ollama probing

This commit is contained in:
mmckeen-nv
2026-05-19 19:08:25 -07:00
parent 635cdae9a0
commit e0a0588659
2 changed files with 75 additions and 16 deletions
+69 -15
View File
@@ -14,6 +14,7 @@ const OLLAMA_BASE_URL = normalizeOllamaBaseUrl(
const OLLAMA_PROBE_TIMEOUT_MS = parsePositiveInteger(process.env.OPENSHELL_OLLAMA_PROBE_TIMEOUT_MS, 2500) const OLLAMA_PROBE_TIMEOUT_MS = parsePositiveInteger(process.env.OPENSHELL_OLLAMA_PROBE_TIMEOUT_MS, 2500)
const WINDOWS_CURL_TIMEOUT_SECONDS = Math.max(1, Math.ceil(OLLAMA_PROBE_TIMEOUT_MS / 1000)) const WINDOWS_CURL_TIMEOUT_SECONDS = Math.max(1, Math.ceil(OLLAMA_PROBE_TIMEOUT_MS / 1000))
const WINDOWS_CURL_OUTPUT_LIMIT = 10 * 1024 * 1024 const WINDOWS_CURL_OUTPUT_LIMIT = 10 * 1024 * 1024
const WINDOWS_LOCAL_OLLAMA_URLS = [DEFAULT_OLLAMA_URL, "http://localhost:11434"]
const execFileAsync = promisify(execFile) const execFileAsync = promisify(execFile)
export const runtime = "nodejs" export const runtime = "nodejs"
@@ -38,7 +39,7 @@ type OllamaProbeCandidate = {
source: string source: string
hostKind: OllamaHostKind hostKind: OllamaHostKind
hostLabel: string hostLabel: string
via: "fetch" | "windows-curl" via: "fetch" | "windows-curl" | "windows-powershell"
} }
type OllamaProbeResult = OllamaProbeCandidate & { type OllamaProbeResult = OllamaProbeCandidate & {
@@ -213,17 +214,16 @@ function addFetchCandidate(
candidates.push({ baseUrl, source, hostKind, hostLabel, via: "fetch" }) candidates.push({ baseUrl, source, hostKind, hostLabel, via: "fetch" })
} }
function addWindowsCurlCandidate( function addWindowsInteropCandidates(
candidates: OllamaProbeCandidate[], candidates: OllamaProbeCandidate[],
value: string | undefined, value: string | undefined,
source: string, source: string,
hostKind: OllamaHostKind,
hostLabel: string,
) { ) {
if (process.env.OPENSHELL_OLLAMA_WINDOWS_INTEROP === "0") return if (process.env.OPENSHELL_OLLAMA_WINDOWS_INTEROP === "0") return
const baseUrl = normalizeOllamaBaseUrl(value) const baseUrl = normalizeOllamaBaseUrl(value)
if (!baseUrl) return if (!baseUrl) return
candidates.push({ baseUrl, source, hostKind, hostLabel, via: "windows-curl" }) candidates.push({ baseUrl, source, hostKind: "win", hostLabel: "WIN", via: "windows-curl" })
candidates.push({ baseUrl, source, hostKind: "win", hostLabel: "WIN", via: "windows-powershell" })
} }
function dedupeCandidates(candidates: OllamaProbeCandidate[]) { function dedupeCandidates(candidates: OllamaProbeCandidate[]) {
@@ -267,13 +267,13 @@ async function buildProbeCandidates() {
addFetchCandidate(candidates, hostUrl(host), "wsl-windows-host", "win", "WIN") addFetchCandidate(candidates, hostUrl(host), "wsl-windows-host", "win", "WIN")
} }
addFetchCandidate(candidates, hostUrl("host.docker.internal"), "host.docker.internal", "win", "WIN") addFetchCandidate(candidates, hostUrl("host.docker.internal"), "host.docker.internal", "win", "WIN")
addWindowsCurlCandidate( const windowsLocalUrls = new Set([
candidates, process.env.OPENSHELL_WINDOWS_OLLAMA_BASE_URL,
process.env.OPENSHELL_WINDOWS_OLLAMA_BASE_URL || DEFAULT_OLLAMA_URL, ...WINDOWS_LOCAL_OLLAMA_URLS,
"windows-localhost", ].filter((value): value is string => Boolean(value)))
"win", for (const url of windowsLocalUrls) {
"WIN", addWindowsInteropCandidates(candidates, url, "windows-localhost")
) }
} }
return dedupeCandidates(candidates) return dedupeCandidates(candidates)
@@ -357,6 +357,14 @@ function windowsCurlExecutable() {
return "curl.exe" return "curl.exe"
} }
function windowsPowerShellExecutable() {
const override = String(process.env.OPENSHELL_WINDOWS_POWERSHELL_EXE || "").trim()
if (override) return override
const windowsPowerShell = "/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe"
if (existsSync(windowsPowerShell)) return windowsPowerShell
return "powershell.exe"
}
async function probeWindowsCurlCandidate(candidate: OllamaProbeCandidate): Promise<OllamaProbeResult> { async function probeWindowsCurlCandidate(candidate: OllamaProbeCandidate): Promise<OllamaProbeResult> {
const startedAt = Date.now() const startedAt = Date.now()
try { try {
@@ -389,10 +397,52 @@ async function probeWindowsCurlCandidate(candidate: OllamaProbeCandidate): Promi
} }
} }
async function probeWindowsPowerShellCandidate(candidate: OllamaProbeCandidate): Promise<OllamaProbeResult> {
const startedAt = Date.now()
try {
const { stdout } = await execFileAsync(windowsPowerShellExecutable(), [
"-NoLogo",
"-NoProfile",
"-NonInteractive",
"-ExecutionPolicy",
"Bypass",
"-Command",
"$ProgressPreference = 'SilentlyContinue'; (Invoke-WebRequest -UseBasicParsing -TimeoutSec $args[0] -Uri $args[1]).Content",
String(WINDOWS_CURL_TIMEOUT_SECONDS),
tagsUrl(candidate.baseUrl),
], {
timeout: OLLAMA_PROBE_TIMEOUT_MS + 1000,
maxBuffer: WINDOWS_CURL_OUTPUT_LIMIT,
windowsHide: true,
})
const data = JSON.parse(String(stdout || "{}"))
if (!data || typeof data !== "object") throw new Error("Ollama returned a non-JSON response")
return {
...candidate,
ok: true,
data,
status: 200,
elapsedMs: Date.now() - startedAt,
}
} catch (error) {
return {
...candidate,
ok: false,
elapsedMs: Date.now() - startedAt,
error: probeErrorMessage(error),
}
}
}
async function probeCandidate(candidate: OllamaProbeCandidate) { async function probeCandidate(candidate: OllamaProbeCandidate) {
return candidate.via === "windows-curl" switch (candidate.via) {
? probeWindowsCurlCandidate(candidate) case "windows-curl":
: probeFetchCandidate(candidate) return probeWindowsCurlCandidate(candidate)
case "windows-powershell":
return probeWindowsPowerShellCandidate(candidate)
default:
return probeFetchCandidate(candidate)
}
} }
function unreachableMessage(wsl: boolean) { function unreachableMessage(wsl: boolean) {
@@ -427,6 +477,8 @@ export async function GET() {
via: result.via, via: result.via,
count: modelRowsFromTags(result.data, result).length, count: modelRowsFromTags(result.data, result).length,
})), })),
checkedHostLabels: Array.from(new Set(results.map((result) => result.hostLabel))),
availableHostLabels: Array.from(new Set(availableResults.map((result) => result.hostLabel))),
models, models,
count: models.length, count: models.length,
elapsedMs: Date.now() - startedAt, elapsedMs: Date.now() - startedAt,
@@ -442,6 +494,8 @@ export async function GET() {
count: 0, count: 0,
elapsedMs: Date.now() - startedAt, elapsedMs: Date.now() - startedAt,
error: unreachableMessage(wsl), error: unreachableMessage(wsl),
checkedHostLabels: Array.from(new Set(results.map((result) => result.hostLabel))),
availableHostLabels: [],
probed: results.map(publicProbeResult), probed: results.map(publicProbeResult),
}, { status: 200 }) }, { status: 200 })
} }
+6 -1
View File
@@ -88,6 +88,10 @@ function ollamaSourceSummary(models: OllamaModel[]) {
return Array.from(new Set(models.map((item) => item.hostLabel).filter(Boolean))).join(" + ") return Array.from(new Set(models.map((item) => item.hostLabel).filter(Boolean))).join(" + ")
} }
function ollamaProbeSummary(labels: unknown) {
return Array.isArray(labels) ? labels.filter((item): item is string => typeof item === "string" && Boolean(item)).join(" + ") : ""
}
function VllmAdvancedConfiguration({ function VllmAdvancedConfiguration({
model, model,
port, port,
@@ -478,8 +482,9 @@ export default function InferenceEndpointPanel() {
} }
const models = Array.isArray(data.models) ? data.models : [] const models = Array.isArray(data.models) ? data.models : []
const sources = ollamaSourceSummary(models) const sources = ollamaSourceSummary(models)
const checked = ollamaProbeSummary(data.checkedHostLabels)
setOllamaModels(models) setOllamaModels(models)
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.") setOllamaMessage(models.length > 0 ? `${models.length} Ollama model${models.length === 1 ? "" : "s"} available${sources ? ` from ${sources}` : ""}${checked && checked !== sources ? `. Checked ${checked}.` : ""} Click one to load the Ollama endpoint preset.` : (checked ? `Checked ${checked}; no Ollama models reported.` : "Ollama is reachable but has no models installed."))
if (models[0]?.name) setModel((current) => current || models[0].name) if (models[0]?.name) setModel((current) => current || models[0].name)
} catch (error) { } catch (error) {
setOllamaModels([]) setOllamaModels([])