Duplicate Code Opportunity
Summary
- Pattern: Provider API configuration (target/basePath) setup and logging
- Locations:
src/services/api-proxy-service.ts lines 96-106, 246-250, 269-273, 290-291, 341-345
- Impact: 24+ lines of repetitive code across 4 providers (OpenAI, Anthropic, Copilot, Gemini)
Evidence
The same configuration pattern is repeated for each provider:
Environment setup (lines 96-106):
...(config.copilotApiTarget && { COPILOT_API_TARGET: stripScheme(config.copilotApiTarget) }),
...(config.copilotApiBasePath && { COPILOT_API_BASE_PATH: config.copilotApiBasePath }),
// ... repeated for openai, anthropic, gemini
...(config.openaiApiTarget && { OPENAI_API_TARGET: stripScheme(config.openaiApiTarget) }),
...(config.openaiApiBasePath && { OPENAI_API_BASE_PATH: config.openaiApiBasePath }),
...(config.anthropicApiTarget && { ANTHROPIC_API_TARGET: stripScheme(config.anthropicApiTarget) }),
...(config.anthropicApiBasePath && { ANTHROPIC_API_BASE_PATH: config.anthropicApiBasePath }),
...(config.geminiApiTarget && { GEMINI_API_TARGET: stripScheme(config.geminiApiTarget) }),
...(config.geminiApiBasePath && { GEMINI_API_BASE_PATH: config.geminiApiBasePath }),
Debug logging (repeated 4 times):
// OpenAI (lines 246-250)
if (config.openaiApiTarget) {
logger.debug(`OpenAI API target overridden to: ${config.openaiApiTarget}`);
}
if (config.openaiApiBasePath) {
logger.debug(`OpenAI API base path set to: ${config.openaiApiBasePath}`);
}
// Anthropic (lines 269-273) - identical structure
// Copilot (lines 290-291) - identical structure
// Gemini (lines 341-345) - identical structure
Suggested Refactoring
Extract a helper function that handles API target/basePath configuration for any provider:
/**
* Configures API target and basePath for a provider, with scheme stripping and debug logging.
*/
function configureProviderApi(
config: WrapperConfig,
provider: 'openai' | 'anthropic' | 'copilot' | 'gemini',
envPrefix: string
): Record<string, string> {
const targetKey = `${provider}ApiTarget` as keyof WrapperConfig;
const basePathKey = `${provider}ApiBasePath` as keyof WrapperConfig;
const result: Record<string, string> = {};
if (config[targetKey]) {
result[`${envPrefix}_API_TARGET`] = stripScheme(config[targetKey] as string);
logger.debug(`${provider} API target overridden to: ${config[targetKey]}`);
}
if (config[basePathKey]) {
result[`${envPrefix}_API_BASE_PATH`] = config[basePathKey] as string;
logger.debug(`${provider} API base path set to: ${config[basePathKey]}`);
}
return result;
}
// Usage:
environment: {
...configureProviderApi(config, 'copilot', 'COPILOT'),
...configureProviderApi(config, 'openai', 'OPENAI'),
...configureProviderApi(config, 'anthropic', 'ANTHROPIC'),
...configureProviderApi(config, 'gemini', 'GEMINI'),
// ... rest of config
}
Affected Files
src/services/api-proxy-service.ts — lines 96-106, 246-250, 269-273, 290-291, 341-345
Effort Estimate
Low — Straightforward extraction to a helper function with 4 call sites
Detected by Duplicate Code Detector workflow. Run date: 2026-05-22
Generated by Duplicate Code Detector · ● 8.1M · ◷
Duplicate Code Opportunity
Summary
src/services/api-proxy-service.tslines 96-106, 246-250, 269-273, 290-291, 341-345Evidence
The same configuration pattern is repeated for each provider:
Environment setup (lines 96-106):
Debug logging (repeated 4 times):
Suggested Refactoring
Extract a helper function that handles API target/basePath configuration for any provider:
Affected Files
src/services/api-proxy-service.ts— lines 96-106, 246-250, 269-273, 290-291, 341-345Effort Estimate
Low — Straightforward extraction to a helper function with 4 call sites
Detected by Duplicate Code Detector workflow. Run date: 2026-05-22