@iflow-mcp/promptx
Version:
AI角色创建平台和智能工具开发平台,基于MCP协议提供专业AI能力注入
241 lines • 6.76 kB
JavaScript
// src/serverConfigManager/index.ts
import { readFileSync, writeFileSync, existsSync, mkdirSync } from "fs";
import { join, dirname } from "path";
import { homedir } from "os";
var DEFAULT_CONFIG = {
port: 5203,
host: "localhost",
transport: "stdio",
corsEnabled: false,
debug: false
};
var ServerConfigManager = class {
/**
* 构造函数
*
* @param {string} [configDir] - 可选的配置目录路径,默认为用户主目录下的 .promptx
*/
constructor(configDir) {
const defaultConfigDir = join(homedir(), ".promptx");
const actualConfigDir = configDir || defaultConfigDir;
this.configPath = join(actualConfigDir, "server-config.json");
if (!existsSync(actualConfigDir)) {
mkdirSync(actualConfigDir, { recursive: true });
}
this.config = this.loadConfig();
}
/**
* 从文件加载配置
*
* @private
* @returns {ServerConfig} 加载的配置对象
*/
loadConfig() {
try {
if (existsSync(this.configPath)) {
const configData = readFileSync(this.configPath, "utf-8");
const parsedConfig = JSON.parse(configData);
return {
...DEFAULT_CONFIG,
...parsedConfig
};
}
} catch (error) {
console.warn(`Failed to load server config from ${this.configPath}:`, error);
}
return { ...DEFAULT_CONFIG };
}
/**
* 保存配置到文件
*
* @private
* @throws {Error} 当保存失败时抛出错误
*/
saveConfig() {
try {
const configDir = dirname(this.configPath);
if (!existsSync(configDir)) {
mkdirSync(configDir, { recursive: true });
}
writeFileSync(this.configPath, JSON.stringify(this.config, null, 2), "utf-8");
} catch (error) {
throw new Error(`Failed to save server config to ${this.configPath}: ${error}`);
}
}
/**
* 获取当前完整配置
*
* @returns {ServerConfig} 当前配置对象的副本
*/
getConfig() {
return { ...this.config };
}
/**
* 获取端口号
*
* @returns {number} 当前配置的端口号
*/
getPort() {
return this.config.port;
}
/**
* 设置端口号
*
* @param {number} port - 要设置的端口号
* @throws {Error} 当端口号无效时抛出错误
*/
setPort(port) {
if (!Number.isInteger(port) || port < 1 || port > 65535) {
throw new Error(`Invalid port number: ${port}. Port must be an integer between 1 and 65535.`);
}
this.config.port = port;
this.saveConfig();
}
/**
* 获取主机地址
*
* @returns {string} 当前配置的主机地址
*/
getHost() {
return this.config.host;
}
/**
* 设置主机地址
*
* @param {string} host - 要设置的主机地址
* @throws {Error} 当主机地址无效时抛出错误
*/
setHost(host) {
if (!host || typeof host !== "string" || host.trim().length === 0) {
throw new Error(`Invalid host address: ${host}. Host must be a non-empty string.`);
}
this.config.host = host.trim();
this.saveConfig();
}
/**
* 获取传输类型
*
* @returns {'stdio' | 'http'} 当前配置的传输类型
*/
getTransport() {
return this.config.transport;
}
/**
* 设置传输类型
*
* @param {'stdio' | 'http'} transport - 要设置的传输类型
* @throws {Error} 当传输类型无效时抛出错误
*/
setTransport(transport) {
if (transport !== "stdio" && transport !== "http") {
throw new Error(`Invalid transport type: ${transport}. Transport must be 'stdio' or 'http'.`);
}
this.config.transport = transport;
this.saveConfig();
}
/**
* 获取 CORS 启用状态
*
* @returns {boolean} 当前 CORS 启用状态
*/
getCorsEnabled() {
return this.config.corsEnabled;
}
/**
* 设置 CORS 启用状态
*
* @param {boolean} enabled - 是否启用 CORS
*/
setCorsEnabled(enabled) {
this.config.corsEnabled = Boolean(enabled);
this.saveConfig();
}
/**
* 获取调试模式状态
*
* @returns {boolean} 当前调试模式状态
*/
getDebug() {
return this.config.debug;
}
/**
* 设置调试模式状态
*
* @param {boolean} enabled - 是否启用调试模式
*/
setDebug(enabled) {
this.config.debug = Boolean(enabled);
this.saveConfig();
}
/**
* 批量更新配置
*
* @param {Partial<ServerConfig>} updates - 要更新的配置项
* @throws {Error} 当配置项无效时抛出错误
*/
updateConfig(updates) {
if (updates.port !== void 0) {
if (!Number.isInteger(updates.port) || updates.port < 1 || updates.port > 65535) {
throw new Error(`Invalid port number: ${updates.port}. Port must be an integer between 1 and 65535.`);
}
}
if (updates.host !== void 0) {
if (!updates.host || typeof updates.host !== "string" || updates.host.trim().length === 0) {
throw new Error(`Invalid host address: ${updates.host}. Host must be a non-empty string.`);
}
}
if (updates.transport !== void 0) {
if (updates.transport !== "stdio" && updates.transport !== "http") {
throw new Error(`Invalid transport type: ${updates.transport}. Transport must be 'stdio' or 'http'.`);
}
}
this.config = {
...this.config,
...updates,
host: updates.host ? updates.host.trim() : this.config.host,
corsEnabled: updates.corsEnabled !== void 0 ? Boolean(updates.corsEnabled) : this.config.corsEnabled,
debug: updates.debug !== void 0 ? Boolean(updates.debug) : this.config.debug
};
this.saveConfig();
}
/**
* 重置配置为默认值
*/
resetToDefaults() {
this.config = { ...DEFAULT_CONFIG };
this.saveConfig();
}
/**
* 获取服务器 URL(仅在 HTTP 传输模式下有效)
*
* @returns {string} 服务器 URL
*/
getServerUrl() {
if (this.config.transport !== "http") {
throw new Error("Server URL is only available in HTTP transport mode");
}
return `http://${this.config.host}:${this.config.port}`;
}
/**
* 检查端口是否可用(简单检查,不进行实际绑定测试)
*
* @param {number} port - 要检查的端口号
* @returns {boolean} 端口号是否在有效范围内
*/
static isValidPort(port) {
return Number.isInteger(port) && port >= 1 && port <= 65535;
}
/**
* 检查主机地址格式是否有效(简单检查)
*
* @param {string} host - 要检查的主机地址
* @returns {boolean} 主机地址格式是否有效
*/
static isValidHost(host) {
return typeof host === "string" && host.trim().length > 0;
}
};
export {
ServerConfigManager
};
//# sourceMappingURL=index.js.map