mirror of
https://github.com/di-sukharev/opencommit.git
synced 2026-01-29 15:38:01 -05:00
♻️ 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:
@@ -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