mirror of
https://github.com/di-sukharev/opencommit.git
synced 2026-01-19 18:48:19 -05: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.
48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import axios, { AxiosInstance } from 'axios';
|
|
import { OpenAI } from 'openai';
|
|
import { normalizeEngineError } from '../utils/engineErrorHandler';
|
|
import { removeContentTags } from '../utils/removeContentTags';
|
|
import { AiEngine, AiEngineConfig } from './Engine';
|
|
|
|
interface FlowiseAiConfig extends AiEngineConfig {}
|
|
|
|
export class FlowiseEngine implements AiEngine {
|
|
config: FlowiseAiConfig;
|
|
client: AxiosInstance;
|
|
|
|
constructor(config) {
|
|
this.config = config;
|
|
this.client = axios.create({
|
|
url: `${config.baseURL}/${config.apiKey}`,
|
|
headers: { 'Content-Type': 'application/json' }
|
|
});
|
|
}
|
|
|
|
async generateCommitMessage(
|
|
messages: Array<OpenAI.Chat.Completions.ChatCompletionMessageParam>
|
|
): Promise<string | undefined> {
|
|
const gitDiff = (messages[messages.length - 1]?.content as string)
|
|
.replace(/\\/g, '\\\\')
|
|
.replace(/"/g, '\\"')
|
|
.replace(/\n/g, '\\n')
|
|
.replace(/\r/g, '\\r')
|
|
.replace(/\t/g, '\\t');
|
|
|
|
const payload = {
|
|
question: gitDiff,
|
|
overrideConfig: {
|
|
systemMessagePrompt: messages[0]?.content
|
|
},
|
|
history: messages.slice(1, -1)
|
|
};
|
|
try {
|
|
const response = await this.client.post('', payload);
|
|
const message = response.data;
|
|
let content = message?.text;
|
|
return removeContentTags(content, 'think');
|
|
} catch (error) {
|
|
throw normalizeEngineError(error, 'flowise', this.config.model);
|
|
}
|
|
}
|
|
}
|