Skip to content

Commit 71a44fa

Browse files
committed
♻️ 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
1 parent 6c48c93 commit 71a44fa

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

src/engine/openAi.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,17 @@ export class OpenAiEngine implements AiEngine {
1414
constructor(config: OpenAiConfig) {
1515
this.config = config;
1616

17-
// Configuration options for the OpenAI client
18-
const clientOptions: any = {
17+
const clientOptions: OpenAI.ClientOptions = {
1918
apiKey: config.apiKey
2019
};
2120

22-
// Add baseURL if present
2321
if (config.baseURL) {
2422
clientOptions.baseURL = config.baseURL;
2523
}
2624

27-
// Add custom headers if present
2825
if (config.customHeaders) {
2926
try {
3027
let headers = config.customHeaders;
31-
// If the headers are a string, try to parse them as JSON
3228
if (typeof config.customHeaders === 'string') {
3329
headers = JSON.parse(config.customHeaders);
3430
}
@@ -37,7 +33,6 @@ export class OpenAiEngine implements AiEngine {
3733
clientOptions.defaultHeaders = headers;
3834
}
3935
} catch (error) {
40-
// Silently ignore parsing errors
4136
}
4237
}
4338

src/utils/engine.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,39 @@ import { GroqEngine } from '../engine/groq';
1212
import { MLXEngine } from '../engine/mlx';
1313
import { DeepseekEngine } from '../engine/deepseek';
1414

15+
function parseCustomHeaders(headers: any): Record<string, string> {
16+
let parsedHeaders = {};
17+
18+
if (!headers) {
19+
return parsedHeaders;
20+
}
21+
22+
try {
23+
if (typeof headers === 'object' && !Array.isArray(headers)) {
24+
parsedHeaders = headers;
25+
} else {
26+
parsedHeaders = JSON.parse(headers);
27+
}
28+
} catch (error) {
29+
console.warn('Invalid OCO_API_CUSTOM_HEADERS format, ignoring custom headers');
30+
}
31+
32+
return parsedHeaders;
33+
}
34+
1535
export function getEngine(): AiEngine {
1636
const config = getConfig();
1737
const provider = config.OCO_AI_PROVIDER;
1838

19-
// Parse custom headers if provided
20-
let customHeaders = {};
21-
if (config.OCO_API_CUSTOM_HEADERS) {
22-
try {
23-
// If it's already an object, no need to parse it
24-
if (typeof config.OCO_API_CUSTOM_HEADERS === 'object' && !Array.isArray(config.OCO_API_CUSTOM_HEADERS)) {
25-
customHeaders = config.OCO_API_CUSTOM_HEADERS;
26-
} else {
27-
// Try to parse as JSON
28-
customHeaders = JSON.parse(config.OCO_API_CUSTOM_HEADERS);
29-
}
30-
} catch (error) {
31-
console.warn('Invalid OCO_API_CUSTOM_HEADERS format, ignoring custom headers');
32-
}
33-
}
39+
const customHeaders = parseCustomHeaders(config.OCO_API_CUSTOM_HEADERS);
3440

3541
const DEFAULT_CONFIG = {
3642
model: config.OCO_MODEL!,
3743
maxTokensOutput: config.OCO_TOKENS_MAX_OUTPUT!,
3844
maxTokensInput: config.OCO_TOKENS_MAX_INPUT!,
3945
baseURL: config.OCO_API_URL!,
4046
apiKey: config.OCO_API_KEY!,
41-
customHeaders // Add custom headers to the configuration
47+
customHeaders
4248
};
4349

4450
switch (provider) {

0 commit comments

Comments
 (0)