@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.
155 lines • 5.87 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.config = exports.ConfigManager = void 0;
const dotenv_1 = __importDefault(require("dotenv"));
const types_1 = require("./types");
dotenv_1.default.config();
class ConfigManager {
constructor() {
this._botConfig = this.loadBotConfig();
this._aiConfig = this.loadAIConfig();
this._uiConfig = this.loadUIConfig();
}
static getInstance() {
if (!ConfigManager.instance) {
ConfigManager.instance = new ConfigManager();
}
return ConfigManager.instance;
}
loadBotConfig() {
return {
name: process.env['BOT_NAME'] || 'TomiBot',
version: process.env['BOT_VERSION'] || '1.0.0',
environment: process.env['NODE_ENV'] ||
'development',
aiModel: process.env['AI_MODEL'] || 'Gemini',
language: process.env['BOT_LANGUAGE'] || 'Tiếng Việt',
};
}
get botConfig() {
return { ...this._botConfig };
}
updateBotConfig(updates) {
this._botConfig = { ...this._botConfig, ...updates };
}
loadAIConfig() {
const apiKey = process.env['GEMINI_API_KEY'] || '';
return {
provider: process.env['AI_PROVIDER'] || 'Google AI',
model: process.env['AI_MODEL_NAME'] || 'gemini-2.0-flash',
apiKey,
maxTokens: parseInt(process.env['AI_MAX_TOKENS'] || '2048'),
temperature: parseFloat(process.env['AI_TEMPERATURE'] || '0.7'),
};
}
get aiConfig() {
return { ...this._aiConfig };
}
updateAIConfig(updates) {
this._aiConfig = { ...this._aiConfig, ...updates };
if (updates.apiKey !== undefined) {
process.env['GEMINI_API_KEY'] = updates.apiKey;
}
}
hasValidAPIKey() {
return !!(this._aiConfig.apiKey && this._aiConfig.apiKey.length > 10);
}
loadUIConfig() {
return {
theme: process.env['UI_THEME'] || 'default',
colors: process.env['UI_COLORS'] !== 'false',
animations: process.env['UI_ANIMATIONS'] !== 'false',
maxWidth: parseInt(process.env['UI_MAX_WIDTH'] || '80'),
};
}
get uiConfig() {
return { ...this._uiConfig };
}
updateUIConfig(updates) {
this._uiConfig = { ...this._uiConfig, ...updates };
}
validateConfiguration() {
this.validateBotConfig();
this.validateAIConfig();
this.validateUIConfig();
}
validateBotConfig() {
const config = this._botConfig;
if (!config.name || config.name.trim().length === 0) {
throw new types_1.ConfigurationError('Bot name cannot be empty');
}
if (!config.version || !/^\d+\.\d+\.\d+$/.test(config.version)) {
throw new types_1.ConfigurationError('Invalid bot version format. Expected: x.y.z');
}
if (!['development', 'production'].includes(config.environment)) {
throw new types_1.ConfigurationError('Environment must be either "development" or "production"');
}
}
validateAIConfig() {
const config = this._aiConfig;
if (!config.provider || config.provider.trim().length === 0) {
throw new types_1.ConfigurationError('AI provider cannot be empty');
}
if (!config.model || config.model.trim().length === 0) {
throw new types_1.ConfigurationError('AI model cannot be empty');
}
if (config.maxTokens &&
(config.maxTokens < 1 || config.maxTokens > 32000)) {
throw new types_1.ConfigurationError('Max tokens must be between 1 and 32000');
}
if (config.temperature &&
(config.temperature < 0 || config.temperature > 2)) {
throw new types_1.ConfigurationError('Temperature must be between 0 and 2');
}
}
validateUIConfig() {
const config = this._uiConfig;
if (config.maxWidth && (config.maxWidth < 40 || config.maxWidth > 200)) {
throw new types_1.ConfigurationError('UI max width must be between 40 and 200');
}
}
isDevelopment() {
return this._botConfig.environment === 'development';
}
isProduction() {
return this._botConfig.environment === 'production';
}
exportConfig() {
return {
bot: this._botConfig,
ai: {
...this._aiConfig,
apiKey: this._aiConfig.apiKey ? '***HIDDEN***' : undefined,
},
ui: this._uiConfig,
};
}
getConfigSummary() {
const summary = [];
summary.push(`Bot: ${this._botConfig.name} v${this._botConfig.version}`);
summary.push(`Environment: ${this._botConfig.environment}`);
summary.push(`AI Provider: ${this._aiConfig.provider}`);
summary.push(`AI Model: ${this._aiConfig.model}`);
summary.push(`API Key: ${this.hasValidAPIKey() ? '✅ Configured' : '❌ Missing'}`);
summary.push(`UI Theme: ${this._uiConfig.theme}`);
summary.push(`Colors: ${this._uiConfig.colors ? 'Enabled' : 'Disabled'}`);
return summary;
}
reload() {
dotenv_1.default.config({ override: true });
this._botConfig = this.loadBotConfig();
this._aiConfig = this.loadAIConfig();
this._uiConfig = this.loadUIConfig();
}
reset() {
this._botConfig = this.loadBotConfig();
this._aiConfig = this.loadAIConfig();
this._uiConfig = this.loadUIConfig();
}
}
exports.ConfigManager = ConfigManager;
exports.config = ConfigManager.getInstance();
//# sourceMappingURL=config.js.map