|
| 1 | +import fs from 'fs/promises'; |
1 | 2 | import path from 'path'; |
| 3 | +import Base from '../../../../src/core/Base.mjs'; |
2 | 4 |
|
3 | | -const config = { |
| 5 | +/** |
| 6 | + * Default configuration object. |
| 7 | + * Defines the structure and default values for the server configuration. |
| 8 | + */ |
| 9 | +const defaultConfig = { |
4 | 10 | /** |
5 | 11 | * Global debug flag for all MCP servers. |
6 | 12 | * @type {boolean} |
@@ -79,4 +85,85 @@ const config = { |
79 | 85 | } |
80 | 86 | }; |
81 | 87 |
|
82 | | -export default config; |
| 88 | +/** |
| 89 | + * Configuration manager for the Knowledge Base MCP server. |
| 90 | + * Supports loading configuration from a custom file and merging with defaults. |
| 91 | + * @class Neo.ai.mcp.server.knowledge-base.Config |
| 92 | + * @extends Neo.core.Base |
| 93 | + * @singleton |
| 94 | + */ |
| 95 | +class Config extends Base { |
| 96 | + static config = { |
| 97 | + /** |
| 98 | + * @member {String} className='Neo.ai.mcp.server.knowledge-base.Config' |
| 99 | + * @protected |
| 100 | + */ |
| 101 | + className: 'Neo.ai.mcp.server.knowledge-base.Config', |
| 102 | + /** |
| 103 | + * @member {Boolean} singleton=true |
| 104 | + * @protected |
| 105 | + */ |
| 106 | + singleton: true |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * The current configuration object. |
| 111 | + * Starts with defaults and can be updated via load(). |
| 112 | + * @member {Object} data |
| 113 | + */ |
| 114 | + data = null; |
| 115 | + |
| 116 | + /** |
| 117 | + * Initializes the configuration object by deep cloning the defaults. |
| 118 | + * @param {Object} config |
| 119 | + */ |
| 120 | + construct(config) { |
| 121 | + super.construct(config); |
| 122 | + this.data = Neo.clone(defaultConfig, true); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Loads configuration from a JSON file and merges it with defaults. |
| 127 | + * @param {string} filePath - The path to the configuration file. |
| 128 | + * @returns {Promise<void>} |
| 129 | + */ |
| 130 | + async load(filePath) { |
| 131 | + if (!filePath) return; |
| 132 | + |
| 133 | + try { |
| 134 | + const absolutePath = path.resolve(filePath); |
| 135 | + const ext = path.extname(absolutePath); |
| 136 | + let customConfig; |
| 137 | + |
| 138 | + if (ext === '.mjs' || ext === '.js') { |
| 139 | + const module = await import(absolutePath); |
| 140 | + customConfig = module.default; |
| 141 | + } else { |
| 142 | + const content = await fs.readFile(absolutePath, 'utf-8'); |
| 143 | + customConfig = JSON.parse(content); |
| 144 | + } |
| 145 | + |
| 146 | + // Deep merge custom config into the data object |
| 147 | + Neo.merge(this.data, customConfig); |
| 148 | + |
| 149 | + console.log(`[Config] Loaded custom configuration from ${absolutePath}`); |
| 150 | + |
| 151 | + } catch (error) { |
| 152 | + console.error(`[Config] Failed to load configuration from ${filePath}:`, error.message); |
| 153 | + throw error; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +const instance = Neo.setupClass(Config); |
| 159 | + |
| 160 | +export default new Proxy(instance, { |
| 161 | + get(target, prop, receiver) { |
| 162 | + // 1. Prefer properties/methods on the instance itself (e.g. load, className) |
| 163 | + if (Reflect.has(target, prop)) { |
| 164 | + return Reflect.get(target, prop, receiver); |
| 165 | + } |
| 166 | + // 2. Fallback to the data object (e.g. port, batchSize) |
| 167 | + return target.data[prop]; |
| 168 | + } |
| 169 | +}); |
0 commit comments