feat(ollama): add availability check before model operations
Add `ensureOllamaAvailable` function to lib/ollama.ts for checking and waiting for Ollama server health with configurable timeout. Integrate the check in GET /api/ollama/models route with 3s timeout to prevent UI blocking, and in POST /api/ollama/pull route with 20s timeout to ensure availability before pulling models. Update ChatContainer to always render ModelDropdown for better UX when no model is selected.
This commit is contained in:
1 parent
f96d4ed696
commit
7b2386876a
12 files changed
+211
-20
No files matched your search
@@ -1,4 +1,6 @@
|
||||
import { Ollama } from "ollama";
|
||||
import os from "node:os";
|
||||
import { spawn } from "node:child_process";
|
||||
|
||||
export const OLLAMA_HOST = process.env.OLLAMA_HOST || "http://localhost:11434";
|
||||
|
||||
@@ -20,6 +22,51 @@ export async function listModelsViaHttp() {
|
||||
return data?.models ?? [];
|
||||
}
|
||||
|
||||
async function isOllamaHealthy(): Promise<boolean> {
|
||||
try {
|
||||
await listModelsViaHttp();
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function waitUntilHealthy(timeoutMs: number, intervalMs = 800): Promise<boolean> {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
while (Date.now() < deadline) {
|
||||
if (await isOllamaHealthy()) return true;
|
||||
await new Promise((r) => setTimeout(r, intervalMs));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function ensureOllamaAvailable(options?: { timeoutMs?: number }): Promise<boolean> {
|
||||
const timeoutMs = options?.timeoutMs ?? 20000;
|
||||
if (await isOllamaHealthy()) return true;
|
||||
|
||||
// Windows: tentar iniciar via PowerShell script (WSL-first fallback Windows)
|
||||
if (os.platform() === "win32") {
|
||||
try {
|
||||
const child = spawn(
|
||||
"powershell",
|
||||
[
|
||||
"-ExecutionPolicy",
|
||||
"Bypass",
|
||||
"-File",
|
||||
"scripts/ensure-ollama.ps1",
|
||||
],
|
||||
{ stdio: "ignore", detached: true }
|
||||
);
|
||||
child.unref();
|
||||
} catch (e) {
|
||||
// Ignorar, tentaremos apenas aguardar
|
||||
}
|
||||
}
|
||||
|
||||
// Aguardar saúde
|
||||
return await waitUntilHealthy(timeoutMs);
|
||||
}
|
||||
|
||||
export async function chatWithStream(
|
||||
model: string,
|
||||
messages: Array<{ role: string; content: string }>,
|
||||
|
||||
Reference in new issue
Block a user