Skip to content

[Duplicate Code] Provider API target/basePath configuration repeated across 4 providers #3611

@github-actions

Description

@github-actions

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 ·

  • expires on Jun 21, 2026, 10:01 PM UTC

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions