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

141 lines (140 loc) • 6.3 kB
/** * Server Utilities for NeuroLink CLI * Shared utility functions for server management commands (serve.ts and server.ts) */ import { DEFAULT_SERVER_HOST } from "../../server/abstract/baseServerAdapter.js"; import type { MiddlewareDefinition } from "../../types/index.js"; export declare const DEFAULT_SERVER_PORT = 3000; /** The interface `neurolink serve` binds unless told otherwise: this host only. */ export { DEFAULT_SERVER_HOST }; /** Comma-separated API keys; when set, every route except the health probes needs one. */ export declare const SERVER_API_KEY_ENV = "NEUROLINK_SERVER_API_KEY"; /** * `=1` lets `neurolink serve` listen on a non-loopback host with no API key, * and serves the tool and MCP execution routes to anyone who can reach it. */ export declare const SERVER_ALLOW_UNAUTHENTICATED_ENV = "NEUROLINK_SERVER_ALLOW_UNAUTHENTICATED"; /** `=1` keeps the built-in tools (filesystem among them) in the server's registry. */ export declare const SERVER_ENABLE_BUILTIN_TOOLS_ENV = "NEUROLINK_SERVER_ENABLE_BUILTIN_TOOLS"; /** `=1` lets the transcription WebSocket take the API key as `?token=`. */ export declare const SERVER_WS_ALLOW_QUERY_TOKEN_ENV = "NEUROLINK_SERVER_WS_ALLOW_QUERY_TOKEN"; /** Whether `name` is set to `1` (trimmed) in the environment. */ export declare function envFlag(name: string): boolean; /** `true` when `host` names only this machine (`127.0.0.1`, `::1`, `localhost`). */ export declare function isLoopbackHost(host: string): boolean; /** * Why `neurolink serve` must not start as asked, or `undefined` when it may. * A listener the network can reach with no API key hands every route, tool * execution included, to anyone who finds the port; the operator has to say so. */ export declare function unauthenticatedBindRefusal(options: { host: string; apiKeyCount: number; allowUnauthenticated: boolean; }): string | undefined; /** * The paths a key never guards: the liveness, readiness and startup probes an * orchestrator calls with no credentials, and the basic health check. An * exact list: `/health/detailed` reports the runtime and the MCP inventory, * and `/version` the Node version, so both need the key like any other route. */ export declare function healthExemptPaths(basePath: string): string[]; /** * 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`. * The health probes (see `healthExemptPaths`) stay open so orchestrators can * reach them, and the dev playground header cannot bypass a key the operator * configured. The exemption is an exact match on the path, made here rather * than through the adapters' `excludePaths`, whose entries cover a subtree: * `/health` used to exempt `/health/detailed` and even `/health/api/version`. * * Every holder of a key gets the same `{ id: "api-key" }`, marked `shared` so * the agent routes know it names no person and do not warn when a request * names its own userId. */ 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; }