UNPKG

@juspay/neurolink

Version:

Universal AI Development Platform with working MCP integration, multi-provider support, voice (TTS/STT/realtime), and professional CLI. 58+ external MCP servers discoverable, multimodal file processing, RAG pipelines. Build, test, and deploy AI applicatio

172 lines (171 loc) 5.87 kB
/** * 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"; import type { MiddlewareDefinition, RedactionConfig, RequiredServerAdapterConfig, RequiredShutdownConfig, RouteDefinition, ServerAdapterConfig, 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; protected lifecycleState: ServerLifecycleState; protected activeConnections: Map<string, TrackedConnection>; protected readonly shutdownConfig: RequiredShutdownConfig; constructor(neurolink: NeuroLink, config?: ServerAdapterConfig); /** * Initialize the underlying server framework */ protected abstract initializeFramework(): 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; /** * 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>; /** * 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; /** * 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; }): 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>; /** * 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; }