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

@@ -34,6 +34,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_OPENWEBUI_BASE_URL,
DEFAULT_TEMPERATURE
} from '../constants.js';
// ============================================================================
// TYPES AND INTERFACES
@@ -281,8 +287,8 @@ export class OpenWebUIProvider extends BaseAIProvider {
super(config);
// Set OpenWebUI-specific defaults and normalize configuration
this.defaultModel = config.defaultModel || 'llama3.1:latest';
this.baseUrl = this.normalizeBaseUrl(config.baseUrl || 'http://localhost:3000');
this.defaultModel = config.defaultModel || DEFAULT_MODELS.openwebui;
this.baseUrl = this.normalizeBaseUrl(config.baseUrl || DEFAULT_OPENWEBUI_BASE_URL);
this.useOllamaProxy = config.useOllamaProxy ?? false;
this.dangerouslyAllowInsecureConnections = config.dangerouslyAllowInsecureConnections ?? true;
@@ -689,8 +695,8 @@ export class OpenWebUIProvider extends BaseAIProvider {
const requestBody = {
model: params.model || this.defaultModel,
messages: this.convertMessages(params.messages),
max_tokens: params.maxTokens || 1000,
temperature: params.temperature ?? 0.7,
max_tokens: params.maxTokens || DEFAULT_MAX_TOKENS,
temperature: params.temperature ?? DEFAULT_TEMPERATURE,
top_p: params.topP,
stop: params.stopSequences,
stream: false
@@ -724,9 +730,9 @@ export class OpenWebUIProvider extends BaseAIProvider {
prompt: prompt,
stream: false,
options: {
temperature: params.temperature ?? 0.7,
temperature: params.temperature ?? DEFAULT_TEMPERATURE,
top_p: params.topP,
num_predict: params.maxTokens || 1000,
num_predict: params.maxTokens || DEFAULT_MAX_TOKENS,
stop: params.stopSequences
}
};
@@ -754,8 +760,8 @@ export class OpenWebUIProvider extends BaseAIProvider {
const requestBody = {
model: params.model || this.defaultModel,
messages: this.convertMessages(params.messages),
max_tokens: params.maxTokens || 1000,
temperature: params.temperature ?? 0.7,
max_tokens: params.maxTokens || DEFAULT_MAX_TOKENS,
temperature: params.temperature ?? DEFAULT_TEMPERATURE,
top_p: params.topP,
stop: params.stopSequences,
stream: true
@@ -850,9 +856,9 @@ export class OpenWebUIProvider extends BaseAIProvider {
prompt: prompt,
stream: true,
options: {
temperature: params.temperature ?? 0.7,
temperature: params.temperature ?? DEFAULT_TEMPERATURE,
top_p: params.topP,
num_predict: params.maxTokens || 1000,
num_predict: params.maxTokens || DEFAULT_MAX_TOKENS,
stop: params.stopSequences
}
};