File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -252,6 +252,14 @@ impl ModelClient {
252
252
. and_then ( |v| v. to_str ( ) . ok ( ) )
253
253
. and_then ( |s| s. parse :: < u64 > ( ) . ok ( ) ) ;
254
254
255
+ if status == StatusCode :: UNAUTHORIZED {
256
+ if let Some ( a) = auth. as_ref ( ) {
257
+ let _ = a. refresh_token ( ) . await ;
258
+ }
259
+ // Retry immediately with refreshed credentials.
260
+ continue ;
261
+ }
262
+
255
263
// The OpenAI Responses endpoint returns structured JSON bodies even for 4xx/5xx
256
264
// errors. When we bubble early with only the HTTP status the caller sees an opaque
257
265
// "unexpected status 400 Bad Request" which makes debugging nearly impossible.
Original file line number Diff line number Diff line change @@ -62,6 +62,39 @@ impl CodexAuth {
62
62
}
63
63
}
64
64
65
+ pub async fn refresh_token ( & self ) -> Result < String , std:: io:: Error > {
66
+ let token_data = self
67
+ . get_current_token_data ( )
68
+ . ok_or ( std:: io:: Error :: other ( "Token data is not available." ) ) ?;
69
+ let token = token_data. refresh_token ;
70
+
71
+ let refresh_response = try_refresh_token ( token)
72
+ . await
73
+ . map_err ( std:: io:: Error :: other) ?;
74
+
75
+ let updated = update_tokens (
76
+ & self . auth_file ,
77
+ refresh_response. id_token ,
78
+ refresh_response. access_token ,
79
+ refresh_response. refresh_token ,
80
+ )
81
+ . await ?;
82
+
83
+ if let Ok ( mut auth_lock) = self . auth_dot_json . lock ( ) {
84
+ * auth_lock = Some ( updated. clone ( ) ) ;
85
+ }
86
+
87
+ let access = match updated. tokens {
88
+ Some ( t) => t. access_token ,
89
+ None => {
90
+ return Err ( std:: io:: Error :: other (
91
+ "Token data is not available after refresh." ,
92
+ ) ) ;
93
+ }
94
+ } ;
95
+ Ok ( access)
96
+ }
97
+
65
98
/// Loads the available auth information from the auth.json or
66
99
/// OPENAI_API_KEY environment variable.
67
100
pub fn from_codex_home (
You can’t perform that action at this time.
0 commit comments