Skip to content

Commit ccf89c5

Browse files
committed
Apply CodeRabbit recommendations
- Fix base URL precedence: ENV now properly wins over config - Improve curl command generation with safe URL joining - Add better API key masking for short keys - Normalize base URLs by removing trailing slashes
1 parent 94849a6 commit ccf89c5

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/core/agent.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,19 @@ When asked about your identity, you should identify yourself as a coding assista
154154
debugLog('API key provided:', apiKey ? `${apiKey.substring(0, 8)}...` : 'empty');
155155
this.apiKey = apiKey;
156156

157-
// Check for base URL in config file (SDK handles GROQ_BASE_URL env var automatically)
157+
// Base URL precedence: 1) ENV (handled by SDK), 2) config fallback
158+
const envBaseURL = process.env.GROQ_BASE_URL?.trim();
158159
const configBaseURL = this.configManager.getBaseUrl();
159-
if (configBaseURL) {
160-
debugLog(`Using base URL from config: ${configBaseURL}`);
161-
this.client = new Groq({ apiKey, baseURL: configBaseURL });
160+
if (envBaseURL) {
161+
debugLog(`Using base URL from environment: ${envBaseURL}`);
162+
// Let SDK pick up GROQ_BASE_URL automatically
163+
this.client = new Groq({ apiKey });
164+
} else if (configBaseURL) {
165+
const normalized = configBaseURL.replace(/\/+$/, '');
166+
debugLog(`Using base URL from config: ${normalized}`);
167+
this.client = new Groq({ apiKey, baseURL: normalized });
162168
} else {
163-
// SDK will automatically use GROQ_BASE_URL env var if set
169+
// Default SDK behavior
164170
this.client = new Groq({ apiKey });
165171
}
166172

@@ -621,14 +627,20 @@ function resolveEffectiveBaseUrl(configManager: ConfigManager): string {
621627
function generateCurlCommand(apiKey: string, requestBody: any, requestCount: number, baseUrl: string): string {
622628
if (!debugEnabled) return '';
623629

624-
const maskedApiKey = `${apiKey.substring(0, 8)}...${apiKey.substring(apiKey.length - 8)}`;
630+
const maskApiKey = (key: string) => {
631+
if (key.length <= 6) return '***';
632+
if (key.length <= 12) return `${key.slice(0, 3)}***${key.slice(-2)}`;
633+
return `${key.slice(0, 6)}...${key.slice(-4)}`;
634+
};
635+
const maskedApiKey = maskApiKey(apiKey);
625636

626637
// Write request body to JSON file
627638
const jsonFileName = `debug-request-${requestCount}.json`;
628639
const jsonFilePath = path.join(process.cwd(), jsonFileName);
629640
fs.writeFileSync(jsonFilePath, JSON.stringify(requestBody, null, 2));
630641

631-
const curlCmd = `curl -X POST "${baseUrl}/chat/completions" \\
642+
const endpoint = `${baseUrl.replace(/\/+$/, '')}/chat/completions`;
643+
const curlCmd = `curl -X POST "${endpoint}" \\
632644
-H "Authorization: Bearer ${maskedApiKey}" \\
633645
-H "Content-Type: application/json" \\
634646
-d @${jsonFileName}`;

0 commit comments

Comments
 (0)