refactor: extract constants, parameterize validators, simplify factory

Quick-win refactors from the Refactoring Guru catalog:

- Replace Magic Number with Symbolic Constant: extract DEFAULT_TIMEOUT_MS,
  DEFAULT_MAX_RETRIES, DEFAULT_MAX_TOKENS, DEFAULT_TEMPERATURE, default
  models per provider, and validation prompt into src/constants.ts.
- Parameterize Method: collapse validateTemperature / validateTopP /
  validateMaxTokens (and the inline timeout/maxRetries checks) into a
  single validateNumberInRange helper with bounds metadata.
- Inline Class / Remove Middle Man: drop the unused ProviderRegistry
  class from utils/factory.ts. createProvider now dispatches through
  the PROVIDER_REGISTRY const directly instead of a parallel switch.

Also fixes stale VERSION constant in src/index.ts (was 1.3.1).
This commit is contained in:
2026-05-21 13:35:12 +02:00
parent 797fad1b00
commit bb37e61eaf
8 changed files with 163 additions and 253 deletions

View File

@@ -31,6 +31,12 @@ import type {
} from '../types/index.js';
import { BaseAIProvider } from './base.js';
import { AIProviderError, AIErrorType } from '../types/index.js';
import {
DEFAULT_MAX_TOKENS,
DEFAULT_MODELS,
DEFAULT_TEMPERATURE,
VALIDATION_PROMPT
} from '../constants.js';
// ============================================================================
// TYPES AND INTERFACES
@@ -95,7 +101,7 @@ export class GeminiProvider extends BaseAIProvider {
constructor(config: GeminiConfig) {
super(config);
this.defaultModel = config.defaultModel || 'gemini-2.5-flash';
this.defaultModel = config.defaultModel || DEFAULT_MODELS.gemini;
this.safetySettings = config.safetySettings;
this.defaultGenerationConfig = config.generationConfig;
@@ -207,7 +213,7 @@ export class GeminiProvider extends BaseAIProvider {
try {
await this.client.models.generateContent({
model: this.defaultModel,
contents: [{ role: 'user', parts: [{ text: 'Hi' }] }],
contents: [{ role: 'user', parts: [{ text: VALIDATION_PROMPT }] }],
config: { maxOutputTokens: 1 }
});
} catch (error: any) {
@@ -291,8 +297,8 @@ export class GeminiProvider extends BaseAIProvider {
const defaults = this.defaultGenerationConfig;
const config: GenerateContentConfig = {
temperature: params.temperature ?? defaults?.temperature ?? 0.7,
maxOutputTokens: params.maxTokens ?? defaults?.maxOutputTokens ?? 1000
temperature: params.temperature ?? defaults?.temperature ?? DEFAULT_TEMPERATURE,
maxOutputTokens: params.maxTokens ?? defaults?.maxOutputTokens ?? DEFAULT_MAX_TOKENS
};
const topP = params.topP ?? defaults?.topP;