♻️ 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) {
|
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+19
-13
@@ -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';
|
||||||
|
|
||||||
export function getEngine(): AiEngine {
|
function parseCustomHeaders(headers: any): Record<string, string> {
|
||||||
const config = getConfig();
|
let parsedHeaders = {};
|
||||||
const provider = config.OCO_AI_PROVIDER;
|
|
||||||
|
if (!headers) {
|
||||||
|
return parsedHeaders;
|
||||||
|
}
|
||||||
|
|
||||||
// Parse custom headers if provided
|
|
||||||
let customHeaders = {};
|
|
||||||
if (config.OCO_API_CUSTOM_HEADERS) {
|
|
||||||
try {
|
try {
|
||||||
// If it's already an object, no need to parse it
|
if (typeof headers === 'object' && !Array.isArray(headers)) {
|
||||||
if (typeof config.OCO_API_CUSTOM_HEADERS === 'object' && !Array.isArray(config.OCO_API_CUSTOM_HEADERS)) {
|
parsedHeaders = headers;
|
||||||
customHeaders = config.OCO_API_CUSTOM_HEADERS;
|
|
||||||
} else {
|
} else {
|
||||||
// Try to parse as JSON
|
parsedHeaders = JSON.parse(headers);
|
||||||
customHeaders = JSON.parse(config.OCO_API_CUSTOM_HEADERS);
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn('Invalid OCO_API_CUSTOM_HEADERS format, ignoring custom headers');
|
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;
|
||||||
|
|
||||||
|
const customHeaders = parseCustomHeaders(config.OCO_API_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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user