UNPKG

@tehreet/conduit

Version:

LLM API gateway with intelligent routing, robust process management, and health monitoring

113 lines 3.8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createConduitServer = exports.ConduitServer = void 0; const llms_1 = __importDefault(require("@musistudio/llms")); const gracefulShutdown_1 = require("./utils/gracefulShutdown"); const deepseek_1 = require("./transformers/deepseek"); /** * Enhanced server with graceful shutdown support */ class ConduitServer { constructor(config) { this.server = new llms_1.default({ jsonPath: config.jsonPath, initialConfig: config.initialConfig, }); this.gracefulShutdown = new gracefulShutdown_1.GracefulShutdown({ timeout: config.gracefulShutdownTimeout || 30000, onShutdown: config.onShutdown, }); } /** * Add middleware/hooks to the server */ addHook(event, handler) { this.server.addHook(event, handler); } /** * Add health check endpoints */ addHealthEndpoints() { // Basic health check this.server.addHook('preHandler', async (req, reply) => { if (req.url === '/health') { await reply.status(200).send({ status: 'ok', timestamp: new Date().toISOString(), uptime: process.uptime(), pid: process.pid, }); return; } }); // Readiness check this.server.addHook('preHandler', async (req, reply) => { if (req.url === '/ready') { if (this.gracefulShutdown.isShuttingDownNow()) { await reply.status(503).send({ ready: false, reason: 'shutting_down', }); } else { await reply.status(200).send({ ready: true, timestamp: new Date().toISOString(), }); } return; } }); // Liveness check this.server.addHook('preHandler', async (req, reply) => { if (req.url === '/alive') { await reply.status(200).send({ alive: true, pid: process.pid, memory: process.memoryUsage(), uptime: process.uptime(), }); return; } }); } /** * Start the server */ async start() { // Register custom transformers this.server.transformerService.registerTransformer('deepseek', new deepseek_1.DeepseekTransformer()); // Add health endpoints this.addHealthEndpoints(); // Start the server await this.server.start(); // Get the underlying HTTP server (if accessible) // This depends on the @musistudio/llms implementation if (this.server.server) { this.httpServer = this.server.server; this.gracefulShutdown.registerServer(this.httpServer); } else if (this.server.fastify?.server) { this.httpServer = this.server.fastify.server; this.gracefulShutdown.registerServer(this.httpServer); } } /** * Get the underlying server instance */ getServer() { return this.server; } } exports.ConduitServer = ConduitServer; /** * Create server with enhanced features */ const createConduitServer = (config) => { return new ConduitServer(config); }; exports.createConduitServer = createConduitServer; //# sourceMappingURL=serverWrapper.js.map