learning_ai_common_plat/packages/ollama-client/src/health.ts
2026-03-29 12:43:01 -07:00

45 lines
1.4 KiB
TypeScript

/**
* Check if the Ollama server is reachable.
*
* @param baseUrl - Ollama server base URL
* @param timeoutMs - Timeout in milliseconds (default: 3000)
* @returns `true` if the server responds to GET /api/tags within the timeout
*/
export async function checkHealth(baseUrl: string, timeoutMs: number = 3000): Promise<boolean> {
try {
const res = await fetch(`${baseUrl}/api/tags`, {
signal: AbortSignal.timeout(timeoutMs),
});
return res.ok;
} catch {
return false;
}
}
/**
* Check health and return structured result with version info.
*/
export async function checkHealthDetailed(
baseUrl: string,
timeoutMs: number = 3000
): Promise<{ online: boolean; version: string | null; url: string }> {
try {
const [tagsRes, versionRes] = await Promise.allSettled([
fetch(`${baseUrl}/api/tags`, { signal: AbortSignal.timeout(timeoutMs) }),
fetch(`${baseUrl}/api/version`, { signal: AbortSignal.timeout(timeoutMs) }),
]);
const online = tagsRes.status === 'fulfilled' && tagsRes.value.ok;
let version: string | null = null;
if (versionRes.status === 'fulfilled' && versionRes.value.ok) {
const data = (await versionRes.value.json()) as { version?: string };
version = data.version ?? null;
}
return { online, version, url: baseUrl };
} catch {
return { online: false, version: null, url: baseUrl };
}
}