add custom HTTP headers support via OCO_API_CUSTOM_HEADERS

Add OCO_API_CUSTOM_HEADERS variable to README, config enum,
and env parsing to allow JSON string of custom headers.
Validate that custom headers are valid JSON in config validator.
Extend AiEngineConfig with customHeaders and pass headers to
OllamaEngine and OpenAiEngine clients when creating requests.
Parse custom headers in utils/engine and warn on invalid format.
Add unit tests to ensure OCO_API_CUSTOM_HEADERS is handled
correctly and merged from env over global config.

This enables users to send additional headers such as
Authorization or tracing headers with LLM API calls.
This commit is contained in:
EmilienMottet
2025-04-29 20:51:24 +02:00
parent 25c6a0d5d4
commit 6c48c935e2
7 changed files with 99 additions and 7 deletions
+28 -5
View File
@@ -14,11 +14,34 @@ export class OpenAiEngine implements AiEngine {
constructor(config: OpenAiConfig) {
this.config = config;
if (!config.baseURL) {
this.client = new OpenAI({ apiKey: config.apiKey });
} else {
this.client = new OpenAI({ apiKey: config.apiKey, baseURL: config.baseURL });
// Configuration options for the OpenAI client
const clientOptions: any = {
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);
}
if (headers && typeof headers === 'object' && Object.keys(headers).length > 0) {
clientOptions.defaultHeaders = headers;
}
} catch (error) {
// Silently ignore parsing errors
}
}
this.client = new OpenAI(clientOptions);
}
public generateCommitMessage = async (
@@ -42,7 +65,7 @@ export class OpenAiEngine implements AiEngine {
this.config.maxTokensInput - this.config.maxTokensOutput
)
throw new Error(GenerateCommitMessageErrorEnum.tooMuchTokens);
const completion = await this.client.chat.completions.create(params);
const message = completion.choices[0].message;