Add OCO_API_CUSTOM_HEADERS#467
Conversation
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.
di-sukharev
left a comment
There was a problem hiding this comment.
asked for few changes, thank you for contributing! <3
| } else { | ||
| this.client = new OpenAI({ apiKey: config.apiKey, baseURL: config.baseURL }); | ||
| // Configuration options for the OpenAI client | ||
| const clientOptions: any = { |
There was a problem hiding this comment.
please remove comments and any
| apiKey: config.apiKey | ||
| }; | ||
|
|
||
| // Add baseURL if present |
| 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 |
| let customHeaders = {}; | ||
| if (config.OCO_API_CUSTOM_HEADERS) { | ||
| try { | ||
| // If it's already an object, no need to parse it |
There was a problem hiding this comment.
could you move this code to a function with a good semantic name please
| 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 |
|
also please pull latest master and look for failing tests @EmilienMottet, thank you! |
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
|
|
||
| if (config.customHeaders) { | ||
| try { | ||
| let headers = config.customHeaders; |
There was a problem hiding this comment.
could parseCustomHeaders be used here too?
|
added one more comment to reuse the parseCustomHeaders function on one more place and please look into the tests that fail |
|
|
Hi @di-sukharev thanks for your review, I make change now :) |
- export parseCustomHeaders from src/utils/engine.ts - use parseCustomHeaders in OpenAiEngine for config.customHeaders - remove try/catch and inline JSON.parse logic - update config test to expect headers as object and drop JSON.parse Centralize header parsing for reuse and simplify engine code Update tests to match new header format for clarity
|
Thank you for your review @di-sukharev , tell me if you want more change :) |
| }); | ||
|
|
||
| expect(config).not.toEqual(null); | ||
| expect(config.OCO_API_CUSTOM_HEADERS).toEqual({"Authorization": "Bearer token123", "X-Custom-Header": "test-value"}); |
Check failure
Code scanning / CodeQL
Hard-coded credentials Critical test
Description
This pull request implements the feature described in #466 by adding support for custom HTTP headers in API requests via the new
OCO_API_CUSTOM_HEADERSconfiguration option.Main changes
OCO_API_CUSTOM_HEADERSenvironment/config variable.Example usage
Additional context
Close #466