fix: improve type safety and error handling in config-manager

- Wrap JSON.parse in try-catch with clear error message for malformed JSON
- Add iflow, kiro, ghcp providers to CLIProxyVariantConfig type
- Make settings optional in CLIProxyVariantConfig (was required with empty string fallback)
- Remove unsafe type cast in loadConfigSafe()

Addresses additional edge cases from PR #215 code review.
This commit is contained in:
kaitranntt
2025-12-27 12:28:59 -05:00
parent 2fff770b6b
commit 8a3c5a446b
2 changed files with 13 additions and 8 deletions
+4 -4
View File
@@ -17,10 +17,10 @@ export interface ProfilesConfig {
* Example: "flash" → gemini provider with gemini-2.5-flash model
*/
export interface CLIProxyVariantConfig {
/** CLIProxy provider to use (gemini, codex, agy, qwen) */
provider: 'gemini' | 'codex' | 'agy' | 'qwen';
/** Path to settings.json with custom model configuration */
settings: string;
/** CLIProxy provider to use */
provider: 'gemini' | 'codex' | 'agy' | 'qwen' | 'iflow' | 'kiro' | 'ghcp';
/** Path to settings.json with custom model configuration (optional) */
settings?: string;
/** Account identifier for multi-account support (optional, defaults to 'default') */
account?: string;
/** Unique port for variant isolation (8318-8417) */
+9 -4
View File
@@ -121,9 +121,8 @@ export function loadConfigSafe(): Config {
cliproxy = {};
for (const [name, variant] of Object.entries(unifiedConfig.cliproxy.variants)) {
cliproxy[name] = {
// Cast provider - unified has more providers than legacy type
provider: variant.provider as 'gemini' | 'codex' | 'agy' | 'qwen',
settings: variant.settings || '',
provider: variant.provider,
settings: variant.settings,
account: variant.account,
port: variant.port,
};
@@ -145,7 +144,13 @@ export function loadConfigSafe(): Config {
}
const raw = fs.readFileSync(configPath, 'utf8');
const parsed: unknown = JSON.parse(raw);
let parsed: unknown;
try {
parsed = JSON.parse(raw);
} catch (e) {
throw new Error(`Malformed JSON in config: ${configPath} - ${(e as Error).message}`);
}
if (!isConfig(parsed)) {
throw new Error(`Invalid config format: ${configPath}`);