UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

76 lines (75 loc) • 2.68 kB
export function createProxyReadinessState(startTimeMs = Date.now()) { return { startTimeMs, acceptingConnections: false, ready: false, drainingForUpdate: false, }; } export function markProxyReady(state, readyAtMs = Date.now()) { state.acceptingConnections = true; state.ready = true; state.drainingForUpdate = false; state.readyAtMs = readyAtMs; } /** Stop admitting new inference requests while existing responses drain. */ export function markProxyDrainingForUpdate(state) { if (!state.ready) { return false; } state.acceptingConnections = false; state.drainingForUpdate = true; return true; } /** Reopen inference admission after a deferred or failed update attempt. */ export function resumeProxyConnections(state) { if (!state.ready) { return false; } state.acceptingConnections = true; state.drainingForUpdate = false; return true; } export function buildProxyHealthResponse(state, options) { const now = options.now ?? Date.now(); return { status: state.ready ? "ok" : "starting", ready: state.ready, acceptingConnections: state.acceptingConnections, drainingForUpdate: state.drainingForUpdate, strategy: options.strategy, passthrough: options.passthrough, version: options.version, startedAt: new Date(state.startTimeMs).toISOString(), readyAt: state.readyAtMs ? new Date(state.readyAtMs).toISOString() : null, uptime: Math.max(0, (now - state.startTimeMs) / 1000), healthPath: "/health", statusPath: "/status", }; } function sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } export async function waitForProxyReadiness(args) { const timeoutMs = args.timeoutMs ?? 5_000; const intervalMs = args.intervalMs ?? 100; const fetchImpl = args.fetchImpl ?? fetch; const deadline = Date.now() + timeoutMs; let lastError; while (Date.now() < deadline) { try { const response = await fetchImpl(`http://${args.host}:${args.port}/health`, { signal: AbortSignal.timeout(Math.min(intervalMs * 4, 1_000)), }); if (response.ok) { return; } lastError = `health endpoint returned ${response.status}`; } catch (error) { lastError = error instanceof Error ? error.message : String(error); } await sleep(intervalMs); } throw new Error(`Proxy failed readiness check on http://${args.host}:${args.port}/health within ${timeoutMs}ms${lastError ? ` (${lastError})` : ""}`); }