♻️ 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:
@@ -14,21 +14,17 @@ export class OpenAiEngine implements AiEngine {
|
||||
constructor(config: OpenAiConfig) {
|
||||
this.config = config;
|
||||
|
||||
// Configuration options for the OpenAI client
|
||||
const clientOptions: any = {
|
||||
const clientOptions: OpenAI.ClientOptions = {
|
||||
apiKey: config.apiKey
|
||||
};
|
||||
|
||||
// Add baseURL if present
|
||||
if (config.baseURL) {
|
||||
clientOptions.baseURL = config.baseURL;
|
||||
}
|
||||
|
||||
// Add custom headers if present
|
||||
if (config.customHeaders) {
|
||||
try {
|
||||
let headers = config.customHeaders;
|
||||
// If the headers are a string, try to parse them as JSON
|
||||
if (typeof config.customHeaders === 'string') {
|
||||
headers = JSON.parse(config.customHeaders);
|
||||
}
|
||||
@@ -37,7 +33,6 @@ export class OpenAiEngine implements AiEngine {
|
||||
clientOptions.defaultHeaders = headers;
|
||||
}
|
||||
} catch (error) {
|
||||
// Silently ignore parsing errors
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+22
-16
@@ -12,25 +12,31 @@ import { GroqEngine } from '../engine/groq';
|
||||
import { MLXEngine } from '../engine/mlx';
|
||||
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 {
|
||||
const config = getConfig();
|
||||
const provider = config.OCO_AI_PROVIDER;
|
||||
|
||||
// Parse custom headers if provided
|
||||
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 customHeaders = parseCustomHeaders(config.OCO_API_CUSTOM_HEADERS);
|
||||
|
||||
const DEFAULT_CONFIG = {
|
||||
model: config.OCO_MODEL!,
|
||||
@@ -38,7 +44,7 @@ export function getEngine(): AiEngine {
|
||||
maxTokensInput: config.OCO_TOKENS_MAX_INPUT!,
|
||||
baseURL: config.OCO_API_URL!,
|
||||
apiKey: config.OCO_API_KEY!,
|
||||
customHeaders // Add custom headers to the configuration
|
||||
customHeaders
|
||||
};
|
||||
|
||||
switch (provider) {
|
||||
|
||||
Reference in New Issue
Block a user