UNPKG

@tomisakae/tomibot

Version:

TomiBot - AI Chatbot CLI với Google Genkit. Một chatbot AI thông minh chạy trên command line với giao diện đẹp.

180 lines 7.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StatusComponent = void 0; const display_util_1 = require("../utils/display.util"); const logger_util_1 = require("../../utils/logger.util"); class StatusComponent { constructor() { this.logger = new logger_util_1.Logger('StatusComponent'); } renderWelcomeBox(config) { const content = [ `🤖 ${config.name} v${config.version}`, '', `📍 Môi trường: ${config.environment}`, `🧠 AI Model: ${config.aiModel}`, `🌐 Ngôn ngữ: ${config.language}`, `⚡ AI Chatbot CLI với Google Genkit`, ].join('\n'); return display_util_1.DisplayUtils.createBox(content, { title: '🚀 AI Chatbot Information', borderColor: 'cyan', padding: 1, }); } renderSystemStatus(state) { this.logger.debug('Rendering system status'); const aiService = state.services.ai; const modelInfo = aiService.getModelInfo(); const statusData = [ ['TomiBot Version', state.config.version], ['Environment', state.config.environment], ['Node.js Version', process.version], ['Platform', process.platform], ['AI Provider', modelInfo.provider || 'N/A'], ['AI Model', modelInfo.model || 'N/A'], ['Genkit Status', modelInfo.initialized ? '✅ Ready' : '❌ Not Ready'], ['API Key', this.getAPIKeyStatus()], ['Chat History', `${modelInfo.historyCount || 0} messages`], ['Memory Usage', this.getMemoryUsage()], ['Uptime', this.getUptime()], ]; const statusTable = display_util_1.DisplayUtils.createTable(['Component', 'Status'], statusData); return display_util_1.DisplayUtils.createBox(statusTable, { title: '📊 System Status', borderColor: 'green', }); } renderConfigurationStatus(config) { const configData = [ ['Bot Name', config.name], ['Version', config.version], ['Environment', config.environment], ['AI Model', config.aiModel], ['Language', config.language], ['API Key Status', this.getAPIKeyStatus()], ['Node.js Version', process.version], ['Platform', process.platform], ]; const configTable = display_util_1.DisplayUtils.createTable(['Setting', 'Value'], configData); return display_util_1.DisplayUtils.createBox(configTable, { title: '📋 Configuration Info', borderColor: 'blue', }); } async renderHealthCheck(state) { this.logger.debug('Performing health check'); const results = []; const aiHealthy = state.services.ai.isReady(); results.push([ 'AI Service', aiHealthy ? '✅ Healthy' : '❌ Unhealthy', aiHealthy ? 'Service is ready' : 'Check API key configuration' ]); const chatHealthy = state.services.chat.getCurrentSession() !== null || true; results.push([ 'Chat Service', chatHealthy ? '✅ Healthy' : '❌ Unhealthy', chatHealthy ? 'Service is operational' : 'Service unavailable' ]); const memoryUsage = process.memoryUsage(); const memoryHealthy = memoryUsage.heapUsed < 100 * 1024 * 1024; results.push([ 'Memory', memoryHealthy ? '✅ Healthy' : '⚠️ Warning', `${Math.round(memoryUsage.heapUsed / 1024 / 1024)}MB used` ]); if (aiHealthy) { try { const connectionOk = await state.services.ai.testConnection(); results.push([ 'AI Connection', connectionOk ? '✅ Connected' : '❌ Failed', connectionOk ? 'Connection test passed' : 'Unable to reach AI service' ]); } catch (error) { results.push([ 'AI Connection', '❌ Error', 'Connection test failed' ]); } } const healthTable = display_util_1.DisplayUtils.createTable(['Component', 'Status', 'Details'], results); return display_util_1.DisplayUtils.createBox(healthTable, { title: '🏥 Health Check', borderColor: 'yellow', }); } renderPerformanceMetrics() { const metrics = [ ['CPU Usage', this.getCPUUsage()], ['Memory Usage', this.getMemoryUsage()], ['Uptime', this.getUptime()], ['Event Loop Lag', this.getEventLoopLag()], ]; const metricsTable = display_util_1.DisplayUtils.createTable(['Metric', 'Value'], metrics); return display_util_1.DisplayUtils.createBox(metricsTable, { title: '📈 Performance Metrics', borderColor: 'magenta', }); } createStatusIndicator(status, message) { const indicators = { online: '🟢 Online', offline: '🔴 Offline', connecting: '🟡 Connecting', error: '🔴 Error' }; const statusText = indicators[status]; return message ? `${statusText} - ${message}` : statusText; } render() { return this.createStatusIndicator('online', 'TomiBot is running'); } update(data) { this.logger.debug('Status component updated', { data }); } getAPIKeyStatus() { const apiKey = process.env['GEMINI_API_KEY']; return apiKey ? '✅ Configured' : '❌ Not Set'; } getMemoryUsage() { const usage = process.memoryUsage(); const heapUsed = Math.round(usage.heapUsed / 1024 / 1024); const heapTotal = Math.round(usage.heapTotal / 1024 / 1024); return `${heapUsed}MB / ${heapTotal}MB`; } getUptime() { const uptime = process.uptime(); const hours = Math.floor(uptime / 3600); const minutes = Math.floor((uptime % 3600) / 60); const seconds = Math.floor(uptime % 60); if (hours > 0) { return `${hours}h ${minutes}m ${seconds}s`; } else if (minutes > 0) { return `${minutes}m ${seconds}s`; } else { return `${seconds}s`; } } getCPUUsage() { const usage = process.cpuUsage(); const total = usage.user + usage.system; const percentage = Math.round((total / 1000000) * 100) / 100; return `${percentage}%`; } getEventLoopLag() { const start = process.hrtime.bigint(); setImmediate(() => { const lag = Number(process.hrtime.bigint() - start) / 1000000; return `${lag.toFixed(2)}ms`; }); return 'Measuring...'; } } exports.StatusComponent = StatusComponent; //# sourceMappingURL=status.component.js.map