1-
21use openai_api_rust:: chat:: * ;
32use openai_api_rust:: * ;
43use std:: process:: Command ;
54
65use crate :: cli;
76use crate :: config:: { self , Config } ;
8- use crate :: constants:: { DEFAULT_OPENAI_MODEL , DEFAULT_PROMPT_TEMPLATE , STOP_WORDS } ;
7+ use crate :: constants:: {
8+ DEFAULT_MAX_TOKENS , DEFAULT_OPENAI_MODEL , DEFAULT_PROMPT_TEMPLATE , STOP_WORDS ,
9+ } ;
910use crate :: template_engine:: { render_template, TemplateContext } ;
1011
1112async fn generate_commit_message ( diff : & str , config : & config:: Config ) -> anyhow:: Result < String > {
@@ -48,7 +49,7 @@ async fn generate_commit_message(diff: &str, config: &config::Config) -> anyhow:
4849 n : None ,
4950 stream : Some ( false ) ,
5051 stop : Some ( STOP_WORDS . to_owned ( ) ) ,
51- max_tokens : Some ( 2048 ) ,
52+ max_tokens : Some ( DEFAULT_MAX_TOKENS as i32 ) ,
5253 presence_penalty : None ,
5354 frequency_penalty : None ,
5455 logit_bias : None ,
@@ -67,16 +68,46 @@ async fn generate_commit_message(diff: &str, config: &config::Config) -> anyhow:
6768 Ok ( commit_message)
6869}
6970
71+ fn delete_thinking_contents ( orig : & str ) -> String {
72+ let start_tag = "<think>" ;
73+ let end_tag = "</think>" ;
74+
75+ let start_idx = orig. find ( start_tag) . unwrap_or ( orig. len ( ) ) ;
76+ let end_idx = orig. find ( end_tag) . unwrap_or_else ( || 0 ) ;
77+ let s = if start_idx < end_idx {
78+ let mut result = orig[ ..start_idx] . to_string ( ) ;
79+ result. push_str ( & orig[ end_idx..] ) ;
80+ log:: debug!(
81+ "Delete thinking contents, start_idx: {}, end_idx: {}: {:?} => {:?}" ,
82+ start_idx,
83+ end_idx,
84+ orig,
85+ result
86+ ) ;
87+ result
88+ } else {
89+ orig. to_string ( )
90+ } ;
91+ s
92+ }
93+
7094fn extract_commit_message ( response : & str ) -> anyhow:: Result < String > {
7195 let start_tag = "<aicommit>" ;
7296 let end_tag = "</aicommit>" ;
7397
98+ let response = delete_thinking_contents ( response) ;
99+
74100 let start_idx = response
75101 . find ( start_tag)
76102 . ok_or ( anyhow:: anyhow!( "Start tag <aicommit> not found" ) ) ?
77103 + start_tag. len ( ) ;
78- let end_idx = response
79- . find ( end_tag) . unwrap_or_else ( || response. len ( ) ) ;
104+ let end_idx = response. find ( end_tag) . unwrap_or_else ( || response. len ( ) ) ;
105+
106+ if start_idx >= end_idx {
107+ return Err ( anyhow:: anyhow!(
108+ "End tag </aicommit> not found or misplaced"
109+ ) ) ;
110+ }
80111
81112 let commit_message = response[ start_idx..end_idx] . trim ( ) . to_string ( ) ;
82113 Ok ( commit_message)
0 commit comments