| 11 | export type MistralCompletionMessageParam = Array<any>; |
| 12 | |
| 13 | export class MistralAiEngine implements AiEngine { |
| 14 | config: MistralAiConfig; |
| 15 | client: any; // Using any type for Mistral client to avoid TS errors |
| 16 | |
| 17 | constructor(config: MistralAiConfig) { |
| 18 | this.config = config; |
| 19 | |
| 20 | if (!config.baseURL) { |
| 21 | this.client = new Mistral({ apiKey: config.apiKey }); |
| 22 | } else { |
| 23 | this.client = new Mistral({ |
| 24 | apiKey: config.apiKey, |
| 25 | serverURL: config.baseURL |
| 26 | }); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | public generateCommitMessage = async ( |
| 31 | messages: Array<OpenAI.Chat.Completions.ChatCompletionMessageParam> |
| 32 | ): Promise<string | null> => { |
| 33 | const params = { |
| 34 | model: this.config.model, |
| 35 | messages: messages as MistralCompletionMessageParam, |
| 36 | topP: 0.1, |
| 37 | maxTokens: this.config.maxTokensOutput |
| 38 | }; |
| 39 | |
| 40 | try { |
| 41 | const REQUEST_TOKENS = messages |
| 42 | .map((msg) => tokenCount(msg.content as string) + 4) |
| 43 | .reduce((a, b) => a + b, 0); |
| 44 | |
| 45 | if ( |
| 46 | REQUEST_TOKENS > |
| 47 | this.config.maxTokensInput - this.config.maxTokensOutput |
| 48 | ) |
| 49 | throw new Error(GenerateCommitMessageErrorEnum.tooMuchTokens); |
| 50 | |
| 51 | const completion = await this.client.chat.complete(params); |
| 52 | |
| 53 | if (!completion.choices) throw Error('No completion choice available.'); |
| 54 | |
| 55 | const message = completion.choices[0].message; |
| 56 | |
| 57 | if (!message || !message.content) |
| 58 | throw Error('No completion choice available.'); |
| 59 | |
| 60 | let content = message.content as string; |
| 61 | return removeContentTags(content, 'think'); |
| 62 | } catch (error) { |
| 63 | throw normalizeEngineError(error, 'mistral', this.config.model); |
| 64 | } |
| 65 | }; |
| 66 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…