Skip to content

Commit 4d49373

Browse files
committed
fix: resolve CodeRabbit recommendations for custom-base-url
- Fix base URL duplication bug by checking if /openai/v1 already exists - Add URL validation for CLI and config setBaseUrl methods - Properly handle URL format errors with clear error messages - Maintain correct precedence: ENV > config > default
1 parent ccf89c5 commit 4d49373

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/core/agent.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,9 +618,17 @@ function debugLog(message: string, data?: any) {
618618
*/
619619
function resolveEffectiveBaseUrl(configManager: ConfigManager): string {
620620
const env = process.env.GROQ_BASE_URL;
621-
if (env) return env.replace(/\/+$/, '') + '/openai/v1';
621+
if (env) {
622+
// Don't append /openai/v1 if it's already in the URL
623+
const normalized = env.replace(/\/+$/, '');
624+
return normalized.includes('/openai/v1') ? normalized : normalized + '/openai/v1';
625+
}
622626
const cfg = configManager.getBaseUrl();
623-
if (cfg) return cfg.replace(/\/+$/, '') + '/openai/v1';
627+
if (cfg) {
628+
// Don't append /openai/v1 if it's already in the URL
629+
const normalized = cfg.replace(/\/+$/, '');
630+
return normalized.includes('/openai/v1') ? normalized : normalized + '/openai/v1';
631+
}
624632
return 'https://api.groq.com/openai/v1';
625633
}
626634

src/core/cli.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ async function startChat(
3838
try {
3939
// Set base URL if provided via CLI
4040
if (baseUrl) {
41+
// Validate URL format
42+
try {
43+
new URL(baseUrl);
44+
} catch (error) {
45+
console.log(chalk.red(`Invalid base URL format: ${baseUrl}`));
46+
process.exit(1);
47+
}
4148
process.env.GROQ_BASE_URL = baseUrl;
4249
}
4350

src/utils/local-settings.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,17 @@ export class ConfigManager {
144144
* This URL will be used when creating the Groq client if GROQ_BASE_URL env var is not set.
145145
*
146146
* @param baseUrl - The custom API base URL to save (e.g., "https://custom-api.example.com")
147-
* @throws Error if unable to save the configuration
147+
* @throws Error if unable to save the configuration or URL is invalid
148148
*/
149149
public setBaseUrl(baseUrl: string): void {
150150
try {
151+
// Validate URL format
152+
try {
153+
new URL(baseUrl);
154+
} catch (error) {
155+
throw new Error(`Invalid URL format: ${baseUrl}`);
156+
}
157+
151158
this.ensureConfigDir();
152159

153160
let config: Config = {};

0 commit comments

Comments
 (0)