♻️ refactor OpenAI client options and unify custom headers parsing

Use OpenAI.ClientOptions for stronger typing and clarity
Extract custom headers parsing into parseCustomHeaders util
Simplify getEngine by delegating header parsing to helper
Improve maintainability and reduce code duplication
This commit is contained in:
EmilienMottet
2025-04-30 14:46:54 +02:00
parent 6c48c935e2
commit 71a44fac28
2 changed files with 23 additions and 22 deletions
+1 -6
View File
@@ -14,21 +14,17 @@ export class OpenAiEngine implements AiEngine {
constructor(config: OpenAiConfig) { constructor(config: OpenAiConfig) {
this.config = config; this.config = config;
// Configuration options for the OpenAI client const clientOptions: OpenAI.ClientOptions = {
const clientOptions: any = {
apiKey: config.apiKey apiKey: config.apiKey
}; };
// Add baseURL if present
if (config.baseURL) { if (config.baseURL) {
clientOptions.baseURL = config.baseURL; clientOptions.baseURL = config.baseURL;
} }
// Add custom headers if present
if (config.customHeaders) { if (config.customHeaders) {
try { try {
let headers = config.customHeaders; let headers = config.customHeaders;
// If the headers are a string, try to parse them as JSON
if (typeof config.customHeaders === 'string') { if (typeof config.customHeaders === 'string') {
headers = JSON.parse(config.customHeaders); headers = JSON.parse(config.customHeaders);
} }
@@ -37,7 +33,6 @@ export class OpenAiEngine implements AiEngine {
clientOptions.defaultHeaders = headers; clientOptions.defaultHeaders = headers;
} }
} catch (error) { } catch (error) {
// Silently ignore parsing errors
} }
} }
+22 -16
View File
@@ -12,25 +12,31 @@ import { GroqEngine } from '../engine/groq';
import { MLXEngine } from '../engine/mlx'; import { MLXEngine } from '../engine/mlx';
import { DeepseekEngine } from '../engine/deepseek'; import { DeepseekEngine } from '../engine/deepseek';
function parseCustomHeaders(headers: any): Record<string, string> {
let parsedHeaders = {};
if (!headers) {
return parsedHeaders;
}
try {
if (typeof headers === 'object' && !Array.isArray(headers)) {
parsedHeaders = headers;
} else {
parsedHeaders = JSON.parse(headers);
}
} catch (error) {
console.warn('Invalid OCO_API_CUSTOM_HEADERS format, ignoring custom headers');
}
return parsedHeaders;
}
export function getEngine(): AiEngine { export function getEngine(): AiEngine {
const config = getConfig(); const config = getConfig();
const provider = config.OCO_AI_PROVIDER; const provider = config.OCO_AI_PROVIDER;
// Parse custom headers if provided const customHeaders = parseCustomHeaders(config.OCO_API_CUSTOM_HEADERS);
let customHeaders = {};
if (config.OCO_API_CUSTOM_HEADERS) {
try {
// If it's already an object, no need to parse it
if (typeof config.OCO_API_CUSTOM_HEADERS === 'object' && !Array.isArray(config.OCO_API_CUSTOM_HEADERS)) {
customHeaders = config.OCO_API_CUSTOM_HEADERS;
} else {
// Try to parse as JSON
customHeaders = JSON.parse(config.OCO_API_CUSTOM_HEADERS);
}
} catch (error) {
console.warn('Invalid OCO_API_CUSTOM_HEADERS format, ignoring custom headers');
}
}
const DEFAULT_CONFIG = { const DEFAULT_CONFIG = {
model: config.OCO_MODEL!, model: config.OCO_MODEL!,
@@ -38,7 +44,7 @@ export function getEngine(): AiEngine {
maxTokensInput: config.OCO_TOKENS_MAX_INPUT!, maxTokensInput: config.OCO_TOKENS_MAX_INPUT!,
baseURL: config.OCO_API_URL!, baseURL: config.OCO_API_URL!,
apiKey: config.OCO_API_KEY!, apiKey: config.OCO_API_KEY!,
customHeaders // Add custom headers to the configuration customHeaders
}; };
switch (provider) { switch (provider) {