@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
101 lines (100 loc) • 3.98 kB
TypeScript
/**
* Server Utilities for NeuroLink CLI
* Shared utility functions for server management commands (serve.ts and server.ts)
*/
import type { MiddlewareDefinition } from "../../types/index.js";
export declare const DEFAULT_SERVER_PORT = 3000;
/** Comma-separated API keys; when set, every route except health needs one. */
export declare const SERVER_API_KEY_ENV = "NEUROLINK_SERVER_API_KEY";
/**
* Resolve the listen port: --port, then the config file, then the PORT
* environment variable (what container platforms set), then 3000.
*/
export declare function resolveServerPort(cliPort?: number, filePort?: number): number;
export declare function readServerApiKeys(): string[];
/**
* Require one of `apiKeys` as `Authorization: Bearer <key>` or `X-API-Key`.
* Health probes stay open so orchestrators can reach them, and the dev
* playground header cannot bypass a key the operator configured.
*/
export declare function createServerApiKeyMiddleware(apiKeys: string[], basePath: string): MiddlewareDefinition;
/**
* Get the base directory for NeuroLink state files
* @returns Path to ~/.neurolink directory
*/
export declare function getNeuroLinkDir(): string;
/**
* Ensure the NeuroLink state directory exists
* Creates ~/.neurolink if it doesn't exist
*/
export declare function ensureStateDir(): void;
/**
* Check if a process with the given PID is currently running
*
* Uses `process.kill(pid, 0)` which tests if a process exists without sending a signal.
*
* **Platform Behavior:**
* - **Unix/Linux/macOS**: Returns `true` if process exists, `false` if not.
* If the process exists but belongs to another user, returns `true` (via EPERM check).
* - **Windows**: Behavior differs - `process.kill(pid, 0)` may throw even for existing
* processes if they are system processes or have restricted access. This function
* handles EPERM by returning `true`, but other Windows-specific errors may occur.
* For more reliable Windows process detection, consider using `tasklist` command.
*
* @param pid - Process ID to check
* @returns true if the process is running, false otherwise
*/
export declare function isProcessRunning(pid: number): boolean;
/**
* Whether a saved server state belongs to a different, still-running process.
* A state naming our own PID is stale: a restarted container is PID 1 again
* and would otherwise refuse to start, believing it is already running.
*/
export declare function isOtherServerRunning(pid: number): boolean;
/**
* Remove this process's state file however it exits. A graceful stop can end
* the process before its signal handler gets to clean up (once the listener
* closes, the event loop may drain), so this runs from the "exit" event.
*/
export declare function clearStateOnExit(clearState: () => void): void;
/**
* Format a duration in milliseconds to a human-readable uptime string
* @param ms - Duration in milliseconds
* @returns Formatted string like "2d 5h 30m" or "45m 30s"
*/
export declare function formatUptime(ms: number): string;
/**
* Generic state file manager for server state persistence
* @template T - Type of the state object
*/
export declare class StateFileManager<T> {
private filePath;
/**
* Create a new state file manager
* @param filename - Name of the state file (e.g., "serve-state.json")
* @param baseDir - Optional base directory (defaults to ~/.neurolink)
*/
constructor(filename: string, baseDir?: string);
/**
* Get the full path to the state file
*/
getFilePath(): string;
/**
* Save state to the state file
* @param state - State object to save
*/
save(state: T): void;
/**
* Load state from the state file
* @returns The state object, or null if the file doesn't exist or is invalid
*/
load(): T | null;
/**
* Clear (delete) the state file
*/
clear(): void;
/**
* Check if state file exists
*/
exists(): boolean;
}