Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | /** * V3 Configuration Loader * Load configuration from various sources */ import { readFile } from 'fs/promises'; import { join, resolve } from 'path'; import { existsSync } from 'fs'; import type { SystemConfig } from './schema.js'; import { validateSystemConfig, type ValidationResult } from './validator.js'; import { defaultSystemConfig, mergeWithDefaults } from './defaults.js'; /** * Configuration source type */ export type ConfigSource = 'file' | 'env' | 'default' | 'merged'; /** * Loaded configuration with metadata */ export interface LoadedConfig { config: SystemConfig; source: ConfigSource; path?: string; warnings?: string[]; } /** * Configuration file names to search for */ const CONFIG_FILE_NAMES = [ 'claude-flow.config.json', 'claude-flow.config.js', 'claude-flow.json', '.claude-flow.json', ]; /** * Find configuration file in directory */ async function findConfigFile(directory: string): Promise<string | null> { for (const name of CONFIG_FILE_NAMES) { const path = join(directory, name); if (existsSync(path)) { return path; } } return null; } /** * Load configuration from JSON file */ async function loadJsonConfig(path: string): Promise<unknown> { const content = await readFile(path, 'utf8'); return JSON.parse(content); } /** * Load configuration from environment variables */ function loadEnvConfig(): Partial<SystemConfig> { const config: Partial<SystemConfig> = {}; // Orchestrator settings if (process.env.CLAUDE_FLOW_MAX_AGENTS) { config.orchestrator = { ...defaultSystemConfig.orchestrator, lifecycle: { ...defaultSystemConfig.orchestrator.lifecycle, maxConcurrentAgents: parseInt(process.env.CLAUDE_FLOW_MAX_AGENTS, 10), }, }; } // Data directory if (process.env.CLAUDE_FLOW_DATA_DIR) { config.orchestrator = { ...config.orchestrator, ...defaultSystemConfig.orchestrator, session: { ...defaultSystemConfig.orchestrator.session, dataDir: process.env.CLAUDE_FLOW_DATA_DIR, }, }; } // Memory type if (process.env.CLAUDE_FLOW_MEMORY_TYPE) { const memoryType = process.env.CLAUDE_FLOW_MEMORY_TYPE as NonNullable<SystemConfig['memory']>['type']; if (['sqlite', 'agentdb', 'hybrid', 'redis', 'memory'].includes(memoryType)) { config.memory = { ...(defaultSystemConfig.memory ?? { type: 'hybrid' }), type: memoryType, }; } } // MCP transport const defaultMcp = defaultSystemConfig.mcp ?? { name: 'claude-flow', version: '3.0.0', transport: { type: 'stdio' as const } }; if (process.env.CLAUDE_FLOW_MCP_TRANSPORT) { const transport = process.env.CLAUDE_FLOW_MCP_TRANSPORT as 'stdio' | 'http' | 'websocket'; if (['stdio', 'http', 'websocket'].includes(transport)) { config.mcp = { ...defaultMcp, transport: { ...defaultMcp.transport, type: transport, }, }; } } if (process.env.CLAUDE_FLOW_MCP_PORT) { config.mcp = { ...config.mcp, ...defaultMcp, transport: { ...config.mcp?.transport, ...defaultMcp.transport, port: parseInt(process.env.CLAUDE_FLOW_MCP_PORT, 10), }, }; } // Swarm topology const defaultSwarm = defaultSystemConfig.swarm ?? { topology: 'hierarchical-mesh' as const, maxAgents: 20 }; if (process.env.CLAUDE_FLOW_SWARM_TOPOLOGY) { const topology = process.env.CLAUDE_FLOW_SWARM_TOPOLOGY as NonNullable<SystemConfig['swarm']>['topology']; if (['hierarchical', 'mesh', 'ring', 'star', 'adaptive', 'hierarchical-mesh'].includes(topology)) { config.swarm = { ...defaultSwarm, topology, }; } } return config; } /** * Configuration loader class */ export class ConfigLoader { private searchPaths: string[] = []; constructor(additionalPaths?: string[]) { // Default search paths this.searchPaths = [ process.cwd(), resolve(process.cwd(), '..'), resolve(process.env.HOME ?? '', '.claude-flow'), ]; if (additionalPaths) { this.searchPaths.push(...additionalPaths); } } /** * Load configuration from all sources */ async load(): Promise<LoadedConfig> { const warnings: string[] = []; // Start with defaults let config: SystemConfig = { ...defaultSystemConfig }; let source: ConfigSource = 'default'; let path: string | undefined; // Try to load from file for (const searchPath of this.searchPaths) { const configPath = await findConfigFile(searchPath); if (configPath) { try { const fileConfig = await loadJsonConfig(configPath); const validation = validateSystemConfig(fileConfig); if (validation.success) { config = mergeWithDefaults(validation.data!, defaultSystemConfig) as SystemConfig; source = 'file'; path = configPath; break; } else { warnings.push(`Invalid config at ${configPath}: ${validation.errors?.map(e => e.message).join(', ')}`); } } catch (error) { warnings.push(`Failed to load config from ${configPath}: ${(error as Error).message}`); } } } // Merge with environment variables const envConfig = loadEnvConfig(); if (Object.keys(envConfig).length > 0) { config = this.deepMerge(config, envConfig) as SystemConfig; source = source === 'default' ? 'env' : 'merged'; } return { config, source, path, warnings: warnings.length > 0 ? warnings : undefined, }; } /** * Load configuration from specific file */ async loadFromFile(filePath: string): Promise<LoadedConfig> { const absolutePath = resolve(filePath); const fileConfig = await loadJsonConfig(absolutePath); const validation = validateSystemConfig(fileConfig); if (!validation.success) { throw new Error(`Invalid configuration: ${validation.errors?.map(e => e.message).join(', ')}`); } const config = mergeWithDefaults(validation.data!, defaultSystemConfig) as SystemConfig; return { config, source: 'file', path: absolutePath, }; } /** * Deep merge objects */ private deepMerge(target: Record<string, unknown>, source: Record<string, unknown>): Record<string, unknown> { const result = { ...target }; for (const key of Object.keys(source)) { const sourceValue = source[key]; const targetValue = target[key]; if ( sourceValue && typeof sourceValue === 'object' && !Array.isArray(sourceValue) && targetValue && typeof targetValue === 'object' && !Array.isArray(targetValue) ) { result[key] = this.deepMerge( targetValue as Record<string, unknown>, sourceValue as Record<string, unknown>, ); } else if (sourceValue !== undefined) { result[key] = sourceValue; } } return result; } } /** * Load configuration (convenience function) */ export async function loadConfig(options?: { paths?: string[]; file?: string }): Promise<LoadedConfig> { const loader = new ConfigLoader(options?.paths); if (options?.file) { return loader.loadFromFile(options.file); } return loader.load(); } |