Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 0 additions & 110 deletions packages/config/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,116 +606,6 @@ export function getEnvironmentDefaults(env: Environment): HelmConfig {
}
}

// ============================================
// Load Config Function
// ============================================

export async function loadConfig(sources: ConfigSources = {}): Promise<LoadedConfig> {
const warnings: string[] = [];
let config: Record<string, unknown> = {};
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<string, unknown>);

// 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<string, unknown> = {};
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<string, unknown> = {};
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
Comment on lines 609 to 610
// ============================================
Expand Down
Loading