Skip to content

Commit f5c6c31

Browse files
committed
Extends #445 to other providers which many provides deepseek
1 parent 60a7650 commit f5c6c31

File tree

8 files changed

+62
-24
lines changed

8 files changed

+62
-24
lines changed

src/engine/anthropic.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,13 @@ export class AnthropicEngine implements AiEngine {
5454
const data = await this.client.messages.create(params);
5555

5656
const message = data?.content[0].text;
57+
let content = message;
5758

58-
return message;
59+
if (content && content.includes('<think>')) {
60+
return content.replace(/<think>[\s\S]*?<\/think>/g, '').trim();
61+
}
62+
63+
return content;
5964
} catch (error) {
6065
const err = error as Error;
6166
outro(`${chalk.red('✖')} ${err?.message || err}`);

src/engine/azure.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ export class AzureEngine implements AiEngine {
5252
if (message?.content === null) {
5353
return undefined;
5454
}
55-
return message?.content;
55+
56+
let content = message?.content;
57+
58+
if (content && content.includes('<think>')) {
59+
return content.replace(/<think>[\s\S]*?<\/think>/g, '').trim();
60+
}
61+
62+
return content;
5663
} catch (error) {
5764
outro(`${chalk.red('✖')} ${this.config.model}`);
5865

src/engine/deepseek.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import axios from 'axios';
22
import { OpenAI } from 'openai';
33
import { GenerateCommitMessageErrorEnum } from '../generateCommitMessageFromGitDiff';
44
import { tokenCount } from '../utils/tokenCount';
5-
import { OpenAiEngine, OpenAiConfig } from './openAI';
5+
import { OpenAiEngine, OpenAiConfig } from './openAi';
66

77
export interface DeepseekConfig extends OpenAiConfig {}
88

@@ -41,8 +41,13 @@ export class DeepseekEngine extends OpenAiEngine {
4141
const completion = await this.client.chat.completions.create(params);
4242

4343
const message = completion.choices[0].message;
44+
let content = message?.content;
4445

45-
return message?.content;
46+
if (content && content.includes('<think>')) {
47+
return content.replace(/<think>[\s\S]*?<\/think>/g, '').trim();
48+
}
49+
50+
return content;
4651
} catch (error) {
4752
const err = error as Error;
4853
if (

src/engine/flowise.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ export class FlowiseEngine implements AiEngine {
3636
try {
3737
const response = await this.client.post('', payload);
3838
const message = response.data;
39-
return message?.text;
39+
let content = message?.text;
40+
41+
if (content && content.includes('<think>')) {
42+
return content.replace(/<think>[\s\S]*?<\/think>/g, '').trim();
43+
}
44+
45+
return content;
4046
} catch (err: any) {
4147
const message = err.response?.data?.error ?? err.message;
4248
throw new Error('local model issues. details: ' + message);

src/engine/gemini.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ export class GeminiEngine implements AiEngine {
7171
}
7272
});
7373

74-
return result.response.text();
74+
const content = result.response.text();
75+
76+
if (content && content.includes('<think>')) {
77+
return content.replace(/<think>[\s\S]*?<\/think>/g, '').trim();
78+
}
79+
80+
return content;
7581
} catch (error) {
7682
const err = error as Error;
7783
if (

src/engine/mistral.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
import axios from 'axios';
2-
import { Mistral } from '@mistralai/mistralai';
32
import { OpenAI } from 'openai';
43
import { GenerateCommitMessageErrorEnum } from '../generateCommitMessageFromGitDiff';
54
import { tokenCount } from '../utils/tokenCount';
65
import { AiEngine, AiEngineConfig } from './Engine';
7-
import {
8-
AssistantMessage as MistralAssistantMessage,
9-
SystemMessage as MistralSystemMessage,
10-
ToolMessage as MistralToolMessage,
11-
UserMessage as MistralUserMessage
12-
} from '@mistralai/mistralai/models/components';
136

7+
// Using any for Mistral types to avoid type declaration issues
148
export interface MistralAiConfig extends AiEngineConfig {}
15-
export type MistralCompletionMessageParam = Array<
16-
| (MistralSystemMessage & { role: "system" })
17-
| (MistralUserMessage & { role: "user" })
18-
| (MistralAssistantMessage & { role: "assistant" })
19-
| (MistralToolMessage & { role: "tool" })
20-
>
9+
export type MistralCompletionMessageParam = Array<any>;
10+
11+
// Import Mistral dynamically to avoid TS errors
12+
// eslint-disable-next-line @typescript-eslint/no-var-requires
13+
const Mistral = require('@mistralai/mistralai').Mistral;
2114

2215
export class MistralAiEngine implements AiEngine {
2316
config: MistralAiConfig;
24-
client: Mistral;
17+
client: any; // Using any type for Mistral client to avoid TS errors
2518

2619
constructor(config: MistralAiConfig) {
2720
this.config = config;
@@ -64,7 +57,13 @@ export class MistralAiEngine implements AiEngine {
6457
if (!message || !message.content)
6558
throw Error('No completion choice available.')
6659

67-
return message.content as string;
60+
let content = message.content as string;
61+
62+
if (content && content.includes('<think>')) {
63+
return content.replace(/<think>[\s\S]*?<\/think>/g, '').trim();
64+
}
65+
66+
return content;
6867
} catch (error) {
6968
const err = error as Error;
7069
if (

src/engine/mlx.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,16 @@ export class MLXEngine implements AiEngine {
3737

3838
const choices = response.data.choices;
3939
const message = choices[0].message;
40+
let content = message?.content;
41+
42+
if (content && content.includes('<think>')) {
43+
return content.replace(/<think>[\s\S]*?<\/think>/g, '').trim();
44+
}
4045

41-
return message?.content;
46+
return content;
4247
} catch (err: any) {
4348
const message = err.response?.data?.error ?? err.message;
4449
throw new Error(`MLX provider error: ${message}`);
4550
}
4651
}
47-
}
52+
}

src/engine/openAi.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ export class OpenAiEngine implements AiEngine {
4545
const completion = await this.client.chat.completions.create(params);
4646

4747
const message = completion.choices[0].message;
48+
let content = message?.content;
4849

49-
return message?.content;
50+
if (content && content.includes('<think>')) {
51+
return content.replace(/<think>[\s\S]*?<\/think>/g, '').trim();
52+
}
53+
54+
return content;
5055
} catch (error) {
5156
const err = error as Error;
5257
if (

0 commit comments

Comments
 (0)