mirror of
https://github.com/di-sukharev/opencommit.git
synced 2026-04-20 03:02:51 -04:00
This update introduces a centralized error handling mechanism for various AI engines, improving the consistency and clarity of error messages. The new `normalizeEngineError` function standardizes error responses, allowing for better user feedback and recovery suggestions. Additionally, specific error classes for insufficient credits, rate limits, and service availability have been implemented, along with user-friendly formatting for error messages. This refactor aims to enhance the overall user experience when interacting with the AI services.
40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import OpenAI from 'openai';
|
|
import axios, { AxiosInstance } from 'axios';
|
|
import { normalizeEngineError } from '../utils/engineErrorHandler';
|
|
import { AiEngine, AiEngineConfig } from './Engine';
|
|
|
|
interface AimlApiConfig extends AiEngineConfig {}
|
|
|
|
export class AimlApiEngine implements AiEngine {
|
|
client: AxiosInstance;
|
|
|
|
constructor(public config: AimlApiConfig) {
|
|
this.client = axios.create({
|
|
baseURL: config.baseURL || 'https://api.aimlapi.com/v1/chat/completions',
|
|
headers: {
|
|
Authorization: `Bearer ${config.apiKey}`,
|
|
'HTTP-Referer': 'https://github.com/di-sukharev/opencommit',
|
|
'X-Title': 'opencommit',
|
|
'Content-Type': 'application/json',
|
|
...config.customHeaders
|
|
}
|
|
});
|
|
}
|
|
|
|
public generateCommitMessage = async (
|
|
messages: Array<OpenAI.Chat.Completions.ChatCompletionMessageParam>
|
|
): Promise<string | null> => {
|
|
try {
|
|
const response = await this.client.post('', {
|
|
model: this.config.model,
|
|
messages
|
|
});
|
|
|
|
const message = response.data.choices?.[0]?.message;
|
|
return message?.content ?? null;
|
|
} catch (error) {
|
|
throw normalizeEngineError(error, 'aimlapi', this.config.model);
|
|
}
|
|
};
|
|
}
|