/** * Pings the local llama.cpp server to ensure it's up, running, and has a model loaded. * If the server is offline or still loading, it terminates the pipeline gracefully. */ export async function checkLlmServer( url = "http://127.0.0.1:8080/health", ): Promise { try { const response = await fetch(url); // llama.cpp returns a 503 status if the server is up but the model weights are still loading if (response.status === 503) { console.error( "\n ⏳ Local AI engine is starting up, but the model is still loading into memory.", ); console.error( "👉 Please wait a minute for the weights to load, then run the pipeline again.\n", ); process.exit(1); } // Parse the JSON health response (expected: { status: "ok" }) const data = (await response.json()) as { status?: string }; if (response.ok && data.status === "ok") { console.log("🟢 Local AI engine is connected and ready for inference!"); return; } // Catch-all for unexpected active server responses console.error( `\n ❌ Unknown response from local AI engine health check (Status: ${response.status}).`, ); process.exit(1); } catch (_error: unknown) { console.error("\n ❌ Could not connect to the local AI engine."); console.error(`🔗 Attempted endpoint: ${url}`); console.error( "👉 Make sure your './llama-server' command is actively running in another terminal tab!\n", ); process.exit(1); } }