UNPKG

@agentscope/studio

Version:

AgentScope Studio is a powerful local monitoring and visualization tool designed to provide real-time insights into your system's performance and behavior.

152 lines (151 loc) 6.15 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigManager = exports.ServerConfig = exports.PATHS = exports.ENV = void 0; const dotenv_1 = __importDefault(require("dotenv")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const common_1 = require("./common"); // Load environment variables dotenv_1.default.config(); exports.ENV = { isDevelopment: process.env.NODE_ENV === 'development', isProduction: process.env.NODE_ENV === 'production', platform: process.platform, homeDir: process.env.HOME || process.env.USERPROFILE || '', }; exports.PATHS = { getAppDataDir: () => { switch (process.platform) { case 'win32': return path_1.default.join(process.env.APPDATA || '', common_1.APP_INFO.name); case 'linux': return path_1.default.join(process.env.HOME || '', common_1.APP_INFO.name); case 'darwin': return path_1.default.join(process.env.HOME || '', 'Library', 'Application Support', common_1.APP_INFO.name); default: throw new Error(`Unsupported platform: ${process.platform}`); } }, getLogsDir: () => path_1.default.join(exports.PATHS.getAppDataDir(), 'logs'), getUserConfigPath: () => path_1.default.join(exports.PATHS.getAppDataDir(), 'config.json'), getFridayDir: () => path_1.default.join(exports.PATHS.getAppDataDir(), 'Friday'), getFridayConfigPath: () => path_1.default.join(exports.PATHS.getAppDataDir(), 'Friday', 'config.json'), getFridayDialogHistoryPath: () => path_1.default.join(exports.PATHS.getAppDataDir(), 'Friday', 'session.json'), }; exports.ServerConfig = { port: parseInt(process.env.PORT || common_1.DEFAULT_CONFIG.server.port.toString()), otelGrpcPort: parseInt(process.env.OTEL_GRPC_PORT || common_1.DEFAULT_CONFIG.server.otelGrpcPort.toString()), database: { type: 'better-sqlite3', database: path_1.default.join(exports.PATHS.getAppDataDir(), 'database.sqlite'), }, }; // 服务器端的配置管理 class ConfigManager { constructor() { this.config = exports.ServerConfig; this.loadUserConfig(); } static getInstance() { if (!ConfigManager.instance) { ConfigManager.instance = new ConfigManager(); } return ConfigManager.instance; } loadUserConfig() { const userConfigPath = exports.PATHS.getUserConfigPath(); try { if (fs_1.default.existsSync(userConfigPath)) { const userConfig = JSON.parse(fs_1.default.readFileSync(userConfigPath, 'utf8')); this.config = Object.assign(Object.assign({}, this.config), userConfig); } } catch (error) { console.error('Failed to load user config:', error); } } getConfig() { return this.config; } saveConfig() { try { const userConfigPath = exports.PATHS.getUserConfigPath(); fs_1.default.mkdirSync(path_1.default.dirname(userConfigPath), { recursive: true }); fs_1.default.writeFileSync(userConfigPath, JSON.stringify(this.config, null, 2)); } catch (error) { console.error('Failed to save config:', error); } } setPort(port) { return __awaiter(this, void 0, void 0, function* () { this.config = Object.assign(Object.assign({}, this.config), { port: port }); }); } setOtelGrpcPort(otelGrpcPort) { return __awaiter(this, void 0, void 0, function* () { this.config = Object.assign(Object.assign({}, this.config), { otelGrpcPort: otelGrpcPort }); }); } getDatabasePath() { return this.config.database.database; } getDatabaseSize() { try { const dbPath = this.getDatabasePath(); if (fs_1.default.existsSync(dbPath)) { const stats = fs_1.default.statSync(dbPath); return stats.size; } else { return 0; } } catch (error) { console.error('Failed to get database size:', error); return 0; } } getFormattedDatabaseSize() { const sizeInBytes = this.getDatabaseSize(); if (sizeInBytes === 0) { return '0 B'; } const units = ['B', 'KB', 'MB', 'GB', 'TB']; let size = sizeInBytes; let unitIndex = 0; while (size >= 1024 && unitIndex < units.length - 1) { size /= 1024; unitIndex++; } const decimalPlaces = unitIndex === 0 ? 0 : 2; const formattedSize = size.toFixed(decimalPlaces); return `${formattedSize} ${units[unitIndex]}`; } getDataStats() { const dbPath = this.getDatabasePath(); const size = this.getDatabaseSize(); const formattedSize = this.getFormattedDatabaseSize(); return { path: dbPath, size, formattedSize, fridayConfigPath: exports.PATHS.getFridayConfigPath(), fridayHistoryPath: exports.PATHS.getFridayDialogHistoryPath(), }; } } exports.ConfigManager = ConfigManager;