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:
Evandro Fonseca Junior committed 2025-10-29 16:21:49 -03:00
1 parent f96d4ed696
commit 7b2386876a
12 files changed
+211 -20

No files matched your search

+3 -1
View File
@@ -1,5 +1,5 @@
import { NextResponse } from "next/server";
import { listModelsViaSdk, listModelsViaHttp } from "@/lib/ollama";
import { listModelsViaSdk, listModelsViaHttp, ensureOllamaAvailable } from "@/lib/ollama";
import {
estimateCpuVram,
inferDevice,
@@ -31,6 +31,8 @@ function normalizeModel(model: any): ModelInfo {
export async function GET() {
try {
let models: any[] = [];
// Tentar garantir disponibilidade rapidamente (não bloquear longamente a UI)
await ensureOllamaAvailable({ timeoutMs: 3000 }).catch(() => {});
try {
models = await listModelsViaSdk();
} catch {
+13 -1
View File
@@ -1,5 +1,5 @@
import { NextRequest } from "next/server";
import { pullModel } from "@/lib/ollama";
import { pullModel, ensureOllamaAvailable } from "@/lib/ollama";
export const dynamic = "force-dynamic";
@@ -14,6 +14,18 @@ export async function POST(req: NextRequest) {
const stream = new ReadableStream({
async start(controller) {
try {
// Garantir que o servidor esteja disponível antes do pull
const ok = await ensureOllamaAvailable({ timeoutMs: 20000 });
if (!ok) {
controller.enqueue(
new TextEncoder().encode(
JSON.stringify({ error: "Ollama indisponível" }) + "\n",
),
);
controller.close();
return;
}
const response = await pullModel(model);
for await (const chunk of response) {