@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
288 lines (287 loc) • 12 kB
TypeScript
/**
* Abstract Server Adapter
* Base class for all framework-specific server adapters
* Follows NeuroLink's composition and factory patterns
*/
import { EventEmitter } from "events";
import type { ExternalServerManager } from "../../mcp/externalServerManager.js";
import type { MCPToolRegistry } from "../../mcp/toolRegistry.js";
import type { NeuroLink } from "../../neurolink.js";
/** The interface an adapter binds unless told otherwise: this host only. */
export declare const DEFAULT_SERVER_HOST = "127.0.0.1";
import type { MiddlewareDefinition, RedactionConfig, RequiredServerAdapterConfig, RequiredShutdownConfig, RouteDefinition, ServerAdapterConfig, ServerAdapterEvents, ServerCarriedContext, ServerContext, ServerLifecycleState, ServerStatus, TrackedConnection } from "../../types/index.js";
/**
* Abstract base class for server adapters
* Provides common functionality and defines the interface for framework-specific implementations
*/
export declare abstract class BaseServerAdapter extends EventEmitter {
protected readonly config: RequiredServerAdapterConfig;
protected readonly redactionConfig?: RedactionConfig;
protected readonly neurolink: NeuroLink;
protected readonly toolRegistry: MCPToolRegistry;
protected readonly externalServerManager?: ExternalServerManager;
protected routes: Map<string, RouteDefinition>;
protected middlewares: MiddlewareDefinition[];
protected isRunning: boolean;
protected startTime?: Date;
private frameworkReady;
private pendingMiddlewares;
private pendingRouteKeys;
protected lifecycleState: ServerLifecycleState;
protected activeConnections: Map<string, TrackedConnection>;
protected readonly shutdownConfig: RequiredShutdownConfig;
private carryRequestedByRoutes;
private sensitiveRoutesRegistered;
private sensitiveRoutesAllowedOpen;
constructor(neurolink: NeuroLink, config?: ServerAdapterConfig);
/**
* Whether middleware marked `authenticates: true` is registered (the
* built-in authentication middleware marks itself). The tool and MCP
* execution routes ask on every request; `registerAllRoutes()` wires them.
*/
hasAuthenticationMiddleware(): boolean;
/**
* Called by `registerAllRoutes()`: routes that act on the host (tool and
* MCP execution) are mounted, so `start()` warns when no authentication
* middleware guards them. The check runs at start, when the middleware set
* is final, so registering authentication after the routes is fine.
*/
noteSensitiveRoutes(options?: {
allowUnauthenticated?: boolean;
}): void;
/**
* The warning promised by `noteSensitiveRoutes()`, always on stderr: a
* `logger.warn` is dropped unless debug logging is on, which would hide a
* notice meant for the operator who just started the server.
*/
protected warnIfSensitiveRoutesUnauthenticated(): void;
/**
* Give the handler a way to notice that the client is gone or the request
* is over time. The controller is this request's own unless middleware
* already attached one that the adapter carried; `applyTimeout` arms the
* configured request timeout, which the caller leaves off for a streaming
* route (a stream legitimately outlives it, as under Hono's own timeout
* middleware). `release()` disarms the timer once the response is written.
*/
protected attachRequestAbort(ctx: ServerContext, options: {
onClientClose: (listener: () => void) => void;
applyTimeout: boolean;
}): {
controller: AbortController;
deadline: Promise<never> | undefined;
timedOut: () => boolean;
release: () => void;
};
/**
* Initialize the underlying server framework
* May be async: adapters that dynamically import their framework package
* (e.g. Hono) return a Promise; adapters with a synchronous no-op stub that
* defer real setup to their own overridden `initialize()` (Express/Fastify/
* Koa) still return void.
*/
protected abstract initializeFramework(): void | Promise<void>;
/**
* Register a route with the framework
*/
protected abstract registerFrameworkRoute(route: RouteDefinition): void;
/**
* Register middleware with the framework
*/
protected abstract registerFrameworkMiddleware(middleware: MiddlewareDefinition): void;
/**
* Start the server
*/
abstract start(): Promise<void>;
/**
* Stop the server
*/
abstract stop(): Promise<void>;
/**
* Get the underlying framework instance (for advanced usage)
*/
abstract getFrameworkInstance(): unknown;
/**
* The listening Node `http.Server`, once `start()` has run and the runtime
* is Node (Bun and Deno serve without one). A WebSocket upgrade handler
* such as `attachTranscribeWebSocket` attaches to it; `undefined` means
* there is nothing to attach to.
*/
getNativeServer(): unknown;
/**
* Stop accepting new connections
* Called during graceful shutdown to prevent new requests
*/
protected abstract stopAcceptingConnections(): Promise<void>;
/**
* Close the underlying server
* Called after connections are drained or timeout
*/
protected abstract closeServer(): Promise<void>;
/**
* Force close all active connections
* Called when drain timeout expires and forceClose is true
*/
protected abstract forceCloseConnections(): Promise<void>;
/**
* Import an optional framework dependency (express/fastify/koa and their
* plugins), converting a missing package into a {@link MissingDependencyError}
* instead of letting the framework's `Cannot find package` propagate raw.
*
* Delegates to {@link tryImport} for the actual resolution and message
* formatting, then re-wraps only the "package genuinely absent" case — the
* one `tryImport` signals by attaching the loader's own module-not-found
* error as `cause` — into the adapter-specific error type. Any other
* failure (installed but broken, non-package specifier) passes through
* unchanged, matching `tryImport`'s own contract.
*/
protected importFrameworkDependency<T>(pkg: string, framework: string): Promise<T>;
/**
* Initialize the server adapter
* Sets up routes, middleware, and framework
*/
initialize(): Promise<void>;
/**
* Register a custom route
*/
registerRoute(route: RouteDefinition): void;
/**
* Register multiple routes from a route group
*/
registerRouteGroup(group: {
prefix: string;
routes: RouteDefinition[];
middleware?: MiddlewareDefinition[];
}): void;
/**
* Normalize a path by removing duplicate slashes and ensuring leading slash
*/
private normalizePath;
/**
* Register custom middleware
*/
registerMiddleware(middleware: MiddlewareDefinition): void;
/**
* Log a failed request. A client error (a rejected API key, a failed
* validation) is routine traffic, so it is a one-line warning rather than
* an error with a stack trace that anyone could flood the logs with.
*/
protected logRequestError(source: string, requestId: string | undefined, error: Error): void;
/**
* Notify "error" listeners about a failed request. EventEmitter throws when
* "error" has no listener, which inside a framework's error handler turns
* every failure into an empty 500, so emit only when someone is listening.
*/
protected emitRequestError(payload: ServerAdapterEvents["error"]): void;
/**
* Hand queued registrations to the framework. A route re-registered under
* the same key before initialize() is replayed once, in its final form.
*/
private flushPendingRegistrations;
/**
* Create request context from incoming request
*/
protected createContext(options: {
requestId: string;
method: string;
path: string;
headers: Record<string, string>;
query?: Record<string, string>;
params?: Record<string, string>;
body?: unknown;
remoteAddress?: string;
}): ServerContext;
/**
* Register built-in middleware
*/
protected registerBuiltInMiddleware(): void;
/**
* Register built-in routes
* Only registers health routes if disableBuiltInHealth is false (default)
*/
protected registerBuiltInRoutes(): Promise<void>;
/**
* Ask the adapter to carry middleware state (`ctx.user` and the rest) to later
* middleware and the route handler, as the `carryMiddlewareState` option does.
* `registerAllRoutes()` calls it when given `perUserAuth`, which cannot work
* without the user in the handler. It takes effect for requests that start
* after the call, so call it before the server starts, as registering routes
* is.
*/
enableMiddlewareStateCarry(): void;
/** Whether this adapter carries middleware state to the handler. */
protected carriesMiddlewareState(): boolean;
/**
* What middleware has set on its context that later middleware and the route
* handler must see. Each step gets a freshly built context, so unless this is
* parked in the framework's per-request storage an authenticated `ctx.user`
* set by middleware is gone before the handler runs. `undefined` when the
* adapter does not carry middleware state, so nothing is parked.
*/
protected captureCarriedContext(ctx: ServerContext): ServerCarriedContext | undefined;
/**
* Put what `captureCarriedContext` saved onto a freshly built context, and
* mark the context as one that received the carry. The agent routes read the
* mark: a request that never used to reach its handler with `ctx.user` must
* not start being identified by it unless the server opts in. Does nothing
* when the adapter does not carry middleware state: the context stays the
* fresh one it always was, and unmarked.
*/
protected restoreCarriedContext(ctx: ServerContext, carried: ServerCarriedContext | undefined): void;
/**
* Generate unique request ID
*/
protected generateRequestId(): string;
/**
* Get the current lifecycle state
*/
getLifecycleState(): ServerLifecycleState;
/**
* Track a new connection
* @param id Unique connection identifier
* @param socket Optional underlying socket object
* @param requestId Optional associated request ID
*/
protected trackConnection(id: string, socket?: unknown, requestId?: string): void;
/**
* Untrack a connection (when it's completed)
* @param id Connection identifier to remove
*/
protected untrackConnection(id: string): void;
/**
* Get the number of active connections
*/
getActiveConnectionCount(): number;
/**
* Perform graceful shutdown with connection draining
* This method handles the complete shutdown lifecycle
*/
protected gracefulShutdown(): Promise<void>;
/**
* Wait for all active connections to drain
* Resolves when activeConnections is empty
*/
protected drainConnections(): Promise<void>;
/**
* Reset server state for restart capability
* Call this after stop() completes to allow restart
*/
protected resetServerState(): void;
/**
* Validate lifecycle state transition
* @param operation The operation being performed
* @param allowedStates States that allow the operation
*/
protected validateLifecycleState(operation: string, allowedStates: ServerLifecycleState[]): void;
/**
* Get server status
*/
getStatus(): ServerStatus;
/**
* List all registered routes
*/
listRoutes(): RouteDefinition[];
/**
* Get configuration
*/
getConfig(): RequiredServerAdapterConfig;
}