9
9
* File Created: 2025-03-01 17:17:30
10
10
*
11
11
* Modified By: mingcheng ([email protected] )
12
- * Last Modified: 2025-03-05 10:12:30
12
+ * Last Modified: 2025-03-16 23:11:04
13
13
*/
14
14
15
15
use aigitcommit:: cli:: Cli ;
16
16
use aigitcommit:: git:: Git ;
17
17
use aigitcommit:: openai;
18
18
use aigitcommit:: openai:: OpenAI ;
19
19
use arboard:: Clipboard ;
20
+ use async_openai:: error:: OpenAIError ;
20
21
use async_openai:: types:: {
21
22
ChatCompletionRequestSystemMessageArgs , ChatCompletionRequestUserMessageArgs ,
22
23
} ;
@@ -34,17 +35,17 @@ async fn main() -> std::result::Result<(), Box<dyn Error>> {
34
35
let cli = Cli :: parse ( ) ;
35
36
36
37
// Initialize logging
37
- tracing_subscriber :: fmt ( )
38
- . with_max_level ( if cli . verbose {
39
- trace ! ( "Verbose mode enabled, set the log level to TRACE. It will makes a little bit noise." ) ;
40
- Level :: TRACE
41
- } else {
42
- debug ! ( "Verbose mode disabled, set the default log level to WARN" ) ;
43
- Level :: WARN
44
- } )
45
- . without_time ( )
46
- . with_target ( false )
47
- . init ( ) ;
38
+ if cli . verbose {
39
+ tracing_subscriber :: fmt ( )
40
+ . with_max_level ( Level :: TRACE )
41
+ . without_time ( )
42
+ . with_target ( false )
43
+ . init ( ) ;
44
+
45
+ trace ! (
46
+ "Verbose mode enabled, set the log level to TRACE. It will makes a little bit noise."
47
+ ) ;
48
+ }
48
49
49
50
// Check if the specified path is a valid directory
50
51
let repo_dir = fs:: canonicalize ( & cli. repo_path ) ?;
@@ -68,11 +69,11 @@ async fn main() -> std::result::Result<(), Box<dyn Error>> {
68
69
// Get the last 5 commit logs
69
70
// if the repository has less than 5 commits, it will return all logs
70
71
let logs = repository. get_logs ( 5 ) ?;
71
- debug ! ( "Got logs size is {}" , logs. len( ) ) ;
72
+ debug ! ( "got logs size is {}" , logs. len( ) ) ;
72
73
73
74
// If git commit log is empty, return error
74
75
if logs. is_empty ( ) {
75
- return Err ( "No commit logs found" . into ( ) ) ;
76
+ return Err ( "no commit logs found" . into ( ) ) ;
76
77
}
77
78
78
79
// Instantiate OpenAI client, ready to send requests to the OpenAI API
@@ -107,7 +108,24 @@ async fn main() -> std::result::Result<(), Box<dyn Error>> {
107
108
] ;
108
109
109
110
// Send the request to OpenAI API and get the response
110
- let result = client. chat ( & model_name. to_string ( ) , messages) . await ?;
111
+ let result = match client. chat ( & model_name. to_string ( ) , messages) . await {
112
+ Ok ( s) => s,
113
+ Err ( e) => {
114
+ let message = match e {
115
+ OpenAIError :: Reqwest ( _) | OpenAIError :: StreamError ( _) => {
116
+ "network request error" . to_string ( )
117
+ }
118
+ OpenAIError :: JSONDeserialize ( _err) => "json deserialization error" . to_string ( ) ,
119
+ OpenAIError :: InvalidArgument ( _) => "invalid argument" . to_string ( ) ,
120
+ OpenAIError :: FileSaveError ( _) | OpenAIError :: FileReadError ( _) => {
121
+ "io error" . to_string ( )
122
+ }
123
+ OpenAIError :: ApiError ( e) => format ! ( "api error {:?}" , e) ,
124
+ } ;
125
+
126
+ return Err ( message. into ( ) ) ;
127
+ }
128
+ } ;
111
129
112
130
trace ! ( "write to stdout, and finish the process" ) ;
113
131
writeln ! ( std:: io:: stdout( ) , "{}" , result) ?;
@@ -124,36 +142,36 @@ async fn main() -> std::result::Result<(), Box<dyn Error>> {
124
142
125
143
// directly commit the changes to the repository if the --commit option is enabled
126
144
if cli. commit {
127
- trace ! ( "Commit option is enabled, will commit the changes to the repository" ) ;
145
+ trace ! ( "commit option is enabled, will commit the changes to the repository" ) ;
128
146
let mut confirm = Confirm :: new ( ) ;
129
147
confirm
130
- . with_prompt ( "Do you want to commit the changes with the generated commit message?" )
148
+ . with_prompt ( "do you want to commit the changes with the generated commit message?" )
131
149
. default ( false ) ;
132
150
133
151
// Prompt the user for confirmation if --yes option is not enabled
134
152
if cli. yes || confirm. interact ( ) ? {
135
153
match repository. commit ( & result) {
136
154
Ok ( _) => {
137
- writeln ! ( std:: io:: stdout( ) , "Commit successful!" ) ?;
155
+ writeln ! ( std:: io:: stdout( ) , "commit successful!" ) ?;
138
156
}
139
157
Err ( e) => {
140
- writeln ! ( std:: io:: stderr( ) , "Commit failed: {}" , e) ?;
158
+ writeln ! ( std:: io:: stderr( ) , "commit failed: {}" , e) ?;
141
159
}
142
160
}
143
161
}
144
162
}
145
163
146
164
// If the --save option is enabled, save the commit message to a file
147
165
if !cli. save . is_empty ( ) {
148
- trace ! ( "Save option is enabled, will save the commit message to a file" ) ;
166
+ trace ! ( "save option is enabled, will save the commit message to a file" ) ;
149
167
let save_path = & cli. save ;
150
- debug ! ( "The save file path is {:?}" , & save_path) ;
168
+ debug ! ( "the save file path is {:?}" , & save_path) ;
151
169
152
170
let mut file = File :: create ( save_path) ?;
153
171
file. write_all ( result. as_bytes ( ) ) ?;
154
172
file. flush ( ) ?;
155
173
156
- writeln ! ( std:: io:: stdout( ) , "Commit message saved to {}" , & save_path) ?;
174
+ writeln ! ( std:: io:: stdout( ) , "commit message saved to {}" , & save_path) ?;
157
175
}
158
176
159
177
Ok ( ( ) )
0 commit comments