File tree Expand file tree Collapse file tree 10 files changed +37
-55
lines changed
Expand file tree Collapse file tree 10 files changed +37
-55
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ import axios from 'axios';
88import chalk from 'chalk' ;
99import { OpenAI } from 'openai' ;
1010import { GenerateCommitMessageErrorEnum } from '../generateCommitMessageFromGitDiff' ;
11+ import { removeContentTags } from '../utils/removeContentTags' ;
1112import { tokenCount } from '../utils/tokenCount' ;
1213import { AiEngine , AiEngineConfig } from './Engine' ;
1314
@@ -55,12 +56,7 @@ export class AnthropicEngine implements AiEngine {
5556
5657 const message = data ?. content [ 0 ] . text ;
5758 let content = message ;
58-
59- if ( content && content . includes ( '<think>' ) ) {
60- return content . replace ( / < t h i n k > [ \s \S ] * ?< \/ t h i n k > / g, '' ) . trim ( ) ;
61- }
62-
63- return content ;
59+ return removeContentTags ( content , 'think' ) ;
6460 } catch ( error ) {
6561 const err = error as Error ;
6662 outro ( `${ chalk . red ( '✖' ) } ${ err ?. message || err } ` ) ;
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import axios from 'axios';
77import chalk from 'chalk' ;
88import { OpenAI } from 'openai' ;
99import { GenerateCommitMessageErrorEnum } from '../generateCommitMessageFromGitDiff' ;
10+ import { removeContentTags } from '../utils/removeContentTags' ;
1011import { tokenCount } from '../utils/tokenCount' ;
1112import { AiEngine , AiEngineConfig } from './Engine' ;
1213
@@ -54,12 +55,7 @@ export class AzureEngine implements AiEngine {
5455 }
5556
5657 let content = message ?. content ;
57-
58- if ( content && content . includes ( '<think>' ) ) {
59- return content . replace ( / < t h i n k > [ \s \S ] * ?< \/ t h i n k > / g, '' ) . trim ( ) ;
60- }
61-
62- return content ;
58+ return removeContentTags ( content , 'think' ) ;
6359 } catch ( error ) {
6460 outro ( `${ chalk . red ( '✖' ) } ${ this . config . model } ` ) ;
6561
Original file line number Diff line number Diff line change 11import axios from 'axios' ;
22import { OpenAI } from 'openai' ;
33import { GenerateCommitMessageErrorEnum } from '../generateCommitMessageFromGitDiff' ;
4+ import { removeContentTags } from '../utils/removeContentTags' ;
45import { tokenCount } from '../utils/tokenCount' ;
56import { OpenAiEngine , OpenAiConfig } from './openAi' ;
67
@@ -42,12 +43,7 @@ export class DeepseekEngine extends OpenAiEngine {
4243
4344 const message = completion . choices [ 0 ] . message ;
4445 let content = message ?. content ;
45-
46- if ( content && content . includes ( '<think>' ) ) {
47- return content . replace ( / < t h i n k > [ \s \S ] * ?< \/ t h i n k > / g, '' ) . trim ( ) ;
48- }
49-
50- return content ;
46+ return removeContentTags ( content , 'think' ) ;
5147 } catch ( error ) {
5248 const err = error as Error ;
5349 if (
Original file line number Diff line number Diff line change 11import axios , { AxiosInstance } from 'axios' ;
22import { OpenAI } from 'openai' ;
3+ import { removeContentTags } from '../utils/removeContentTags' ;
34import { AiEngine , AiEngineConfig } from './Engine' ;
45
56interface FlowiseAiConfig extends AiEngineConfig { }
@@ -37,12 +38,7 @@ export class FlowiseEngine implements AiEngine {
3738 const response = await this . client . post ( '' , payload ) ;
3839 const message = response . data ;
3940 let content = message ?. text ;
40-
41- if ( content && content . includes ( '<think>' ) ) {
42- return content . replace ( / < t h i n k > [ \s \S ] * ?< \/ t h i n k > / g, '' ) . trim ( ) ;
43- }
44-
45- return content ;
41+ return removeContentTags ( content , 'think' ) ;
4642 } catch ( err : any ) {
4743 const message = err . response ?. data ?. error ?? err . message ;
4844 throw new Error ( 'local model issues. details: ' + message ) ;
Original file line number Diff line number Diff line change 77} from '@google/generative-ai' ;
88import axios from 'axios' ;
99import { OpenAI } from 'openai' ;
10+ import { removeContentTags } from '../utils/removeContentTags' ;
1011import { AiEngine , AiEngineConfig } from './Engine' ;
1112
1213interface GeminiConfig extends AiEngineConfig { }
@@ -72,12 +73,7 @@ export class GeminiEngine implements AiEngine {
7273 } ) ;
7374
7475 const content = result . response . text ( ) ;
75-
76- if ( content && content . includes ( '<think>' ) ) {
77- return content . replace ( / < t h i n k > [ \s \S ] * ?< \/ t h i n k > / g, '' ) . trim ( ) ;
78- }
79-
80- return content ;
76+ return removeContentTags ( content , 'think' ) ;
8177 } catch ( error ) {
8278 const err = error as Error ;
8379 if (
Original file line number Diff line number Diff line change 11import axios from 'axios' ;
22import { OpenAI } from 'openai' ;
33import { GenerateCommitMessageErrorEnum } from '../generateCommitMessageFromGitDiff' ;
4+ import { removeContentTags } from '../utils/removeContentTags' ;
45import { tokenCount } from '../utils/tokenCount' ;
56import { AiEngine , AiEngineConfig } from './Engine' ;
67
@@ -58,12 +59,7 @@ export class MistralAiEngine implements AiEngine {
5859 throw Error ( 'No completion choice available.' )
5960
6061 let content = message . content as string ;
61-
62- if ( content && content . includes ( '<think>' ) ) {
63- return content . replace ( / < t h i n k > [ \s \S ] * ?< \/ t h i n k > / g, '' ) . trim ( ) ;
64- }
65-
66- return content ;
62+ return removeContentTags ( content , 'think' ) ;
6763 } catch ( error ) {
6864 const err = error as Error ;
6965 if (
Original file line number Diff line number Diff line change 11import axios , { AxiosInstance } from 'axios' ;
22import { OpenAI } from 'openai' ;
3+ import { removeContentTags } from '../utils/removeContentTags' ;
34import { AiEngine , AiEngineConfig } from './Engine' ;
4- import { chown } from 'fs' ;
55
66interface MLXConfig extends AiEngineConfig { }
77
@@ -38,12 +38,7 @@ export class MLXEngine implements AiEngine {
3838 const choices = response . data . choices ;
3939 const message = choices [ 0 ] . message ;
4040 let content = message ?. content ;
41-
42- if ( content && content . includes ( '<think>' ) ) {
43- return content . replace ( / < t h i n k > [ \s \S ] * ?< \/ t h i n k > / g, '' ) . trim ( ) ;
44- }
45-
46- return content ;
41+ return removeContentTags ( content , 'think' ) ;
4742 } catch ( err : any ) {
4843 const message = err . response ?. data ?. error ?? err . message ;
4944 throw new Error ( `MLX provider error: ${ message } ` ) ;
Original file line number Diff line number Diff line change 11import axios , { AxiosInstance } from 'axios' ;
22import { OpenAI } from 'openai' ;
3+ import { removeContentTags } from '../utils/removeContentTags' ;
34import { AiEngine , AiEngineConfig } from './Engine' ;
45
56interface OllamaConfig extends AiEngineConfig { }
@@ -35,12 +36,7 @@ export class OllamaEngine implements AiEngine {
3536
3637 const { message } = response . data ;
3738 let content = message ?. content ;
38-
39- if ( content && content . includes ( '<think>' ) ) {
40- return content . replace ( / < t h i n k > [ \s \S ] * ?< \/ t h i n k > / g, '' ) . trim ( ) ;
41- }
42-
43- return content ;
39+ return removeContentTags ( content , 'think' ) ;
4440 } catch ( err : any ) {
4541 const message = err . response ?. data ?. error ?? err . message ;
4642 throw new Error ( `Ollama provider error: ${ message } ` ) ;
Original file line number Diff line number Diff line change 11import axios from 'axios' ;
22import { OpenAI } from 'openai' ;
33import { GenerateCommitMessageErrorEnum } from '../generateCommitMessageFromGitDiff' ;
4+ import { removeContentTags } from '../utils/removeContentTags' ;
45import { tokenCount } from '../utils/tokenCount' ;
56import { AiEngine , AiEngineConfig } from './Engine' ;
67
@@ -46,12 +47,7 @@ export class OpenAiEngine implements AiEngine {
4647
4748 const message = completion . choices [ 0 ] . message ;
4849 let content = message ?. content ;
49-
50- if ( content && content . includes ( '<think>' ) ) {
51- return content . replace ( / < t h i n k > [ \s \S ] * ?< \/ t h i n k > / g, '' ) . trim ( ) ;
52- }
53-
54- return content ;
50+ return removeContentTags ( content , 'think' ) ;
5551 } catch ( error ) {
5652 const err = error as Error ;
5753 if (
Original file line number Diff line number Diff line change 1+ /**
2+ * Removes content wrapped in specified tags from a string
3+ * @param content The content string to process
4+ * @param tag The tag name without angle brackets (e.g., 'think' for '<think></think>')
5+ * @returns The content with the specified tags and their contents removed, and trimmed
6+ */
7+ export function removeContentTags < T extends string | null | undefined > ( content : T , tag : string ) : T {
8+ if ( ! content || typeof content !== 'string' ) {
9+ return content ;
10+ }
11+
12+ const tagRegex = new RegExp ( `<${ tag } >[\s\S]*?<\/${ tag } >` , 'g' ) ;
13+
14+ if ( content . includes ( `<${ tag } >` ) ) {
15+ return content . replace ( tagRegex , '' ) . trim ( ) as T ;
16+ }
17+
18+ return content ;
19+ }
You can’t perform that action at this time.
0 commit comments