| 10 | export interface OpenAiConfig extends AiEngineConfig {} |
| 11 | |
| 12 | export class OpenAiEngine implements AiEngine { |
| 13 | config: OpenAiConfig; |
| 14 | client: OpenAI; |
| 15 | |
| 16 | constructor(config: OpenAiConfig) { |
| 17 | this.config = config; |
| 18 | |
| 19 | const clientOptions: OpenAI.ClientOptions = { |
| 20 | apiKey: config.apiKey |
| 21 | }; |
| 22 | |
| 23 | if (config.baseURL) { |
| 24 | clientOptions.baseURL = config.baseURL; |
| 25 | } |
| 26 | |
| 27 | const proxy = config.proxy; |
| 28 | if (proxy) { |
| 29 | clientOptions.httpAgent = new HttpsProxyAgent(proxy); |
| 30 | } |
| 31 | |
| 32 | if (config.customHeaders) { |
| 33 | const headers = parseCustomHeaders(config.customHeaders); |
| 34 | if (Object.keys(headers).length > 0) { |
| 35 | clientOptions.defaultHeaders = headers; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // The OpenAI SDK's internal fetch may resolve before globalThis.fetch is |
| 40 | // available in some Node.js versions, causing premature connection errors. |
| 41 | // Explicitly binding globalThis.fetch here defers resolution to call time, |
| 42 | // ensuring the runtime's native fetch is used instead of the SDK's bundled |
| 43 | // undici — which also avoids double-initialisation in newer Node (>=18). |
| 44 | if (!clientOptions.fetch && typeof globalThis.fetch === 'function') { |
| 45 | clientOptions.fetch = (...args) => globalThis.fetch(...args); |
| 46 | } |
| 47 | |
| 48 | this.client = new OpenAI(clientOptions); |
| 49 | } |
| 50 | |
| 51 | public generateCommitMessage = async ( |
| 52 | messages: Array<OpenAI.Chat.Completions.ChatCompletionMessageParam> |
| 53 | ): Promise<string | null> => { |
| 54 | const isReasoningModel = /^(o[1-9]|gpt-5)/.test(this.config.model); |
| 55 | |
| 56 | const params = { |
| 57 | model: this.config.model, |
| 58 | messages, |
| 59 | ...(isReasoningModel |
| 60 | ? { max_completion_tokens: this.config.maxTokensOutput } |
| 61 | : { |
| 62 | temperature: 0, |
| 63 | top_p: 0.1, |
| 64 | max_tokens: this.config.maxTokensOutput |
| 65 | }) |
| 66 | }; |
| 67 | |
| 68 | try { |
| 69 | const REQUEST_TOKENS = messages |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…