Files
OllaHub/app/api/ollama/models/route.ts
T
Evandro Fonseca Junior 7b2386876a 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.
2025-10-29 16:21:49 -03:00

47 lines
1.4 KiB
TypeScript

import { NextResponse } from "next/server";
import { listModelsViaSdk, listModelsViaHttp, ensureOllamaAvailable } from "@/lib/ollama";
import {
estimateCpuVram,
inferDevice,
inferQuantization,
mockModels,
type ModelInfo,
} from "@/lib/models";
export const dynamic = "force-dynamic";
function normalizeModel(model: any): ModelInfo {
const name: string = model?.model || model?.name || "unknown";
const sizeBytes: number = model?.size || 0;
const sizeGB = Number(Math.max(0.1, sizeBytes / 1024 ** 3).toFixed(2));
const quantization = inferQuantization(name);
const device = inferDevice(model?.details || name);
const { cpuPct, vramGB } = estimateCpuVram({ sizeGB, quantization, device });
return {
id: name,
name,
sizeGB,
quantization,
device,
estCpuUsagePct: cpuPct,
estVramGB: vramGB,
};
}
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 {
models = await listModelsViaHttp();
}
const normalized = models.map((m) => normalizeModel(m));
return NextResponse.json({ offline: false, models: normalized });
} catch (e) {
return NextResponse.json({ offline: true, models: mockModels });
}
}