11using System ;
22using System . Collections . Generic ;
33using System . Text ;
4- using System . Text . RegularExpressions ;
54using System . Threading ;
65
76using Avalonia . Threading ;
@@ -36,6 +35,8 @@ public void Exec()
3635 {
3736 try
3837 {
38+ _onResponse ? . Invoke ( "Waiting for pre-file analyzing to completed...\n \n " ) ;
39+
3940 var responseBuilder = new StringBuilder ( ) ;
4041 var summaryBuilder = new StringBuilder ( ) ;
4142 foreach ( var change in _changes )
@@ -49,18 +50,17 @@ public void Exec()
4950 var rs = new GetDiffContent ( _repo , new Models . DiffOption ( change , false ) ) . ReadToEnd ( ) ;
5051 if ( rs . IsSuccess )
5152 {
52- var hasFirstValidChar = false ;
53- var thinkingBuffer = new StringBuilder ( ) ;
5453 _service . Chat (
5554 _service . AnalyzeDiffPrompt ,
5655 $ "Here is the `git diff` output: { rs . StdOut } ",
5756 _cancelToken ,
5857 update =>
59- ProcessChatResponse ( update , ref hasFirstValidChar , thinkingBuffer ,
60- ( responseBuilder , text =>
61- _onResponse ? . Invoke (
62- $ "Waiting for pre-file analyzing to completed...\n \n { text } ") ) ,
63- ( summaryBuilder , null ) ) ) ;
58+ {
59+ responseBuilder . Append ( update ) ;
60+ summaryBuilder . Append ( update ) ;
61+
62+ _onResponse ? . Invoke ( $ "Waiting for pre-file analyzing to completed...\n \n { responseBuilder } ") ;
63+ } ) ;
6464 }
6565
6666 responseBuilder . Append ( "\n " ) ;
@@ -74,83 +74,26 @@ public void Exec()
7474
7575 var responseBody = responseBuilder . ToString ( ) ;
7676 var subjectBuilder = new StringBuilder ( ) ;
77- var hasSubjectFirstValidChar = false ;
78- var subjectThinkingBuffer = new StringBuilder ( ) ;
7977 _service . Chat (
8078 _service . GenerateSubjectPrompt ,
8179 $ "Here are the summaries changes:\n { summaryBuilder } ",
8280 _cancelToken ,
8381 update =>
84- ProcessChatResponse ( update , ref hasSubjectFirstValidChar , subjectThinkingBuffer ,
85- ( subjectBuilder , text => _onResponse ? . Invoke ( $ "{ text } \n \n { responseBody } ") ) ) ) ;
82+ {
83+ subjectBuilder . Append ( update ) ;
84+ _onResponse ? . Invoke ( $ "{ subjectBuilder } \n \n { responseBody } ") ;
85+ } ) ;
8686 }
8787 catch ( Exception e )
8888 {
8989 Dispatcher . UIThread . Post ( ( ) => App . RaiseException ( _repo , $ "Failed to generate commit message: { e } ") ) ;
9090 }
9191 }
9292
93- private void ProcessChatResponse (
94- string update ,
95- ref bool hasFirstValidChar ,
96- StringBuilder thinkingBuffer ,
97- params ( StringBuilder builder , Action < string > callback ) [ ] outputs )
98- {
99- if ( ! hasFirstValidChar )
100- {
101- update = update . TrimStart ( ) ;
102- if ( string . IsNullOrEmpty ( update ) )
103- return ;
104- if ( update . StartsWith ( "<" , StringComparison . Ordinal ) )
105- thinkingBuffer . Append ( update ) ;
106- hasFirstValidChar = true ;
107- }
108-
109- if ( thinkingBuffer . Length > 0 )
110- thinkingBuffer . Append ( update ) ;
111-
112- if ( thinkingBuffer . Length > 15 )
113- {
114- var match = REG_COT . Match ( thinkingBuffer . ToString ( ) ) ;
115- if ( match . Success )
116- {
117- update = REG_COT . Replace ( thinkingBuffer . ToString ( ) , "" ) . TrimStart ( ) ;
118- if ( update . Length > 0 )
119- {
120- foreach ( var output in outputs )
121- output . builder . Append ( update ) ;
122- thinkingBuffer . Clear ( ) ;
123- }
124- return ;
125- }
126-
127- match = REG_THINK_START . Match ( thinkingBuffer . ToString ( ) ) ;
128- if ( ! match . Success )
129- {
130- foreach ( var output in outputs )
131- output . builder . Append ( thinkingBuffer ) ;
132- thinkingBuffer . Clear ( ) ;
133- return ;
134- }
135- }
136-
137- if ( thinkingBuffer . Length == 0 )
138- {
139- foreach ( var output in outputs )
140- {
141- output . builder . Append ( update ) ;
142- output . callback ? . Invoke ( output . builder . ToString ( ) ) ;
143- }
144- }
145- }
146-
14793 private Models . OpenAIService _service ;
14894 private string _repo ;
14995 private List < Models . Change > _changes ;
15096 private CancellationToken _cancelToken ;
15197 private Action < string > _onResponse ;
152-
153- private static readonly Regex REG_COT = new ( @"^<(think|thought|thinking|thought_chain)>(.*?)</\1>" , RegexOptions . Singleline ) ;
154- private static readonly Regex REG_THINK_START = new ( @"^<(think|thought|thinking|thought_chain)>" , RegexOptions . Singleline ) ;
15598 }
15699}
0 commit comments