-
Notifications
You must be signed in to change notification settings - Fork 425
Add OCO_API_CUSTOM_HEADERS #467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comment please |
||
| if (config.baseURL) { | ||
| clientOptions.baseURL = config.baseURL; | ||
| } | ||
|
|
||
| // Add custom headers if present | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comment please |
||
| if (config.customHeaders) { | ||
| try { | ||
| let headers = config.customHeaders; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| // If the headers are a string, try to parse them as JSON | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comment please |
||
| 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; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,12 +16,29 @@ 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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comment please |
||
| customHeaders = JSON.parse(config.OCO_API_CUSTOM_HEADERS); | ||
| } | ||
| } catch (error) { | ||
| console.warn('Invalid OCO_API_CUSTOM_HEADERS format, ignoring custom headers'); | ||
| } | ||
| } | ||
|
|
||
| const DEFAULT_CONFIG = { | ||
| model: config.OCO_MODEL!, | ||
| maxTokensOutput: config.OCO_TOKENS_MAX_OUTPUT!, | ||
| maxTokensInput: config.OCO_TOKENS_MAX_INPUT!, | ||
| baseURL: config.OCO_API_URL!, | ||
| apiKey: config.OCO_API_KEY! | ||
| apiKey: config.OCO_API_KEY!, | ||
| customHeaders // Add custom headers to the configuration | ||
| }; | ||
|
|
||
| switch (provider) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please remove comments and
any