From 43370befdd0515481faec59400de749a3762167d Mon Sep 17 00:00:00 2001 From: Vellure Lohith <135720439+Lohith2005@users.noreply.github.com> Date: Mon, 3 Aug 2026 16:44:18 +0530 Subject: [PATCH] refactor(config): remove duplicate loadConfig to establish single source of truth --- packages/config/src/schema.ts | 110 ---------------------------------- 1 file changed, 110 deletions(-) diff --git a/packages/config/src/schema.ts b/packages/config/src/schema.ts index 874cf87..7ce6abf 100644 --- a/packages/config/src/schema.ts +++ b/packages/config/src/schema.ts @@ -606,116 +606,6 @@ export function getEnvironmentDefaults(env: Environment): HelmConfig { } } -// ============================================ -// Load Config Function -// ============================================ - -export async function loadConfig(sources: ConfigSources = {}): Promise { - const warnings: string[] = []; - let config: Record = {}; - let source: LoadedConfig['source'] = 'default'; - let configPath: string | undefined; - - // 1. Environment-specific defaults - const env = (process.env.NODE_ENV as Environment) || 'development'; - const envDefaults = getEnvironmentDefaults(env); - config = mergeConfigs(config, envDefaults as Record); - - // 2. Config file - if (sources.configFile) { - const fs = await import('fs/promises'); - const fileConfig = JSON.parse(await fs.readFile(sources.configFile, 'utf-8')); - config = mergeConfigs(config, fileConfig); - source = 'file'; - configPath = sources.configFile; - } else if (sources.configDir) { - const fs = await import('fs/promises'); - const path = await import('path'); - const candidates = [ - 'helm.config.json', - '.helm.json', - 'config/helm.json', - 'config/default.json', - ]; - for (const candidate of candidates) { - const fullPath = path.resolve(sources.configDir, candidate); - try { - const fileConfig = JSON.parse(await fs.readFile(fullPath, 'utf-8')); - config = mergeConfigs(config, fileConfig); - source = 'file'; - configPath = fullPath; - break; - } catch { - // File not found, continue - } - } - } - - // 3. Environment variables - const envPrefix = 'HELM_'; - const envConfig: Record = {}; - for (const [key, value] of Object.entries(process.env)) { - if (key.startsWith(envPrefix) && value !== undefined) { - const configKey = key.slice(envPrefix.length).toLowerCase().replace(/_/g, '.'); - setNestedValue(envConfig, configKey, value); - } - } - if (Object.keys(envConfig).length > 0) { - config = mergeConfigs(config, envConfig); - source = 'env'; - } - - // 4. CLI args - if (sources.cliArgs && Object.keys(sources.cliArgs).length > 0) { - const cliConfig: Record = {}; - for (const [key, value] of Object.entries(sources.cliArgs)) { - if (value !== undefined) { - setNestedValue(cliConfig, key, value); - } - } - config = mergeConfigs(config, cliConfig); - source = 'cli'; - } - - // 5. Validate - let validatedConfig: HelmConfig; - try { - validatedConfig = HelmConfigSchema.parse(config); - } catch (error) { - if (error instanceof z.ZodError) { - const messages = error.errors.map((e) => `${e.path.join('.')}: ${e.message}`).join('; '); - throw new Error(`Configuration validation failed: ${messages}`); - } - throw error; - } - - // 6. Additional validation - - if (validatedConfig.providers.length === 0) { - warnings.push('No AI providers configured. Add at least one provider to use AI features.'); - } - for (const provider of validatedConfig.providers.filter((p) => p.enabled)) { - const needsApiKey = !['ollama', 'lmstudio', 'vllm'].includes(provider.type); - if (needsApiKey && !provider.apiKey) { - warnings.push(`Provider "${provider.name}" (${provider.type}) requires an API key.`); - } - } - - // Production warnings - if (validatedConfig.app.environment === 'production') { - if (validatedConfig.auth.jwt.secret === 'dev-secret-change-in-production-min-32-chars-long') { - warnings.push('CRITICAL: Using default JWT secret in production! Set HELM_JWT_SECRET.'); - } - } - - return { - config: validatedConfig, - source, - path: configPath, - warnings, - }; -} - // ============================================ // Connection String Helpers // ============================================