@@ -312,7 +312,10 @@ enum CopilotAuthStatus {
312312}
313313
314314enum CopilotAuthEvent {
315- DeviceCode { user_code : String , verification_uri : String } ,
315+ DeviceCode {
316+ user_code : String ,
317+ verification_uri : String ,
318+ } ,
316319 Authenticated ,
317320 Models ( Vec < String > ) ,
318321}
@@ -648,8 +651,7 @@ pub fn run() -> InitResult {
648651 let ( test_tx, test_rx) = std:: sync:: mpsc:: channel :: < bool > ( ) ;
649652 let ( migrate_tx, migrate_rx) =
650653 std:: sync:: mpsc:: channel :: < Result < openfang_migrate:: report:: MigrationReport , String > > ( ) ;
651- let ( copilot_tx, copilot_rx) =
652- std:: sync:: mpsc:: channel :: < Result < CopilotAuthEvent , String > > ( ) ;
654+ let ( copilot_tx, copilot_rx) = std:: sync:: mpsc:: channel :: < Result < CopilotAuthEvent , String > > ( ) ;
653655
654656 let result = loop {
655657 terminal
@@ -660,7 +662,10 @@ pub fn run() -> InitResult {
660662 if state. step == Step :: CopilotAuth {
661663 while let Ok ( event) = copilot_rx. try_recv ( ) {
662664 match event {
663- Ok ( CopilotAuthEvent :: DeviceCode { user_code, verification_uri } ) => {
665+ Ok ( CopilotAuthEvent :: DeviceCode {
666+ user_code,
667+ verification_uri,
668+ } ) => {
664669 state. copilot_user_code = user_code;
665670 state. copilot_verification_uri = verification_uri;
666671 state. copilot_auth_status = CopilotAuthStatus :: WaitingForUser ;
@@ -839,7 +844,8 @@ pub fn run() -> InitResult {
839844 let rt = match tokio:: runtime:: Runtime :: new ( ) {
840845 Ok ( rt) => rt,
841846 Err ( e) => {
842- let _ = copilot_tx. send ( Err ( format ! ( "Runtime error: {e}" ) ) ) ;
847+ let _ = copilot_tx
848+ . send ( Err ( format ! ( "Runtime error: {e}" ) ) ) ;
843849 return ;
844850 }
845851 } ;
@@ -851,21 +857,31 @@ pub fn run() -> InitResult {
851857 . map_err ( |e| format ! ( "HTTP error: {e}" ) ) ;
852858 let http = match http {
853859 Ok ( h) => h,
854- Err ( e) => { let _ = copilot_tx. send ( Err ( e) ) ; return ; }
860+ Err ( e) => {
861+ let _ = copilot_tx. send ( Err ( e) ) ;
862+ return ;
863+ }
855864 } ;
856865
857866 // Step 1: request device code
858867 use openfang_runtime:: drivers:: copilot;
859- let device = match copilot:: request_device_code ( & http) . await {
860- Ok ( d) => d,
861- Err ( e) => { let _ = copilot_tx. send ( Err ( e) ) ; return ; }
862- } ;
868+ let device =
869+ match copilot:: request_device_code ( & http) . await {
870+ Ok ( d) => d,
871+ Err ( e) => {
872+ let _ = copilot_tx. send ( Err ( e) ) ;
873+ return ;
874+ }
875+ } ;
863876
864877 // Send device code to TUI for display
865- let _ = copilot_tx. send ( Ok ( CopilotAuthEvent :: DeviceCode {
866- user_code : device. user_code . clone ( ) ,
867- verification_uri : device. verification_uri . clone ( ) ,
868- } ) ) ;
878+ let _ =
879+ copilot_tx. send ( Ok ( CopilotAuthEvent :: DeviceCode {
880+ user_code : device. user_code . clone ( ) ,
881+ verification_uri : device
882+ . verification_uri
883+ . clone ( ) ,
884+ } ) ) ;
869885
870886 // Browser will be opened by user pressing Enter in TUI
871887
@@ -874,9 +890,14 @@ pub fn run() -> InitResult {
874890 & http,
875891 & device. device_code ,
876892 device. interval ,
877- ) . await {
893+ )
894+ . await
895+ {
878896 Ok ( t) => t,
879- Err ( e) => { let _ = copilot_tx. send ( Err ( e) ) ; return ; }
897+ Err ( e) => {
898+ let _ = copilot_tx. send ( Err ( e) ) ;
899+ return ;
900+ }
880901 } ;
881902
882903 // Save tokens
@@ -885,16 +906,38 @@ pub fn run() -> InitResult {
885906 return ;
886907 }
887908
888- let _ = copilot_tx. send ( Ok ( CopilotAuthEvent :: Authenticated ) ) ;
909+ let _ = copilot_tx
910+ . send ( Ok ( CopilotAuthEvent :: Authenticated ) ) ;
889911
890912 // Step 3: fetch models
891- let ct = match copilot:: exchange_copilot_token ( & http, & tokens. access_token ) . await {
913+ let ct = match copilot:: exchange_copilot_token (
914+ & http,
915+ & tokens. access_token ,
916+ )
917+ . await
918+ {
892919 Ok ( ct) => ct,
893- Err ( e) => { let _ = copilot_tx. send ( Err ( format ! ( "Token exchange: {e}" ) ) ) ; return ; }
920+ Err ( e) => {
921+ let _ = copilot_tx
922+ . send ( Err ( format ! ( "Token exchange: {e}" ) ) ) ;
923+ return ;
924+ }
894925 } ;
895- match copilot:: fetch_models ( & http, & ct. base_url , & ct. token ) . await {
896- Ok ( models) => { let _ = copilot_tx. send ( Ok ( CopilotAuthEvent :: Models ( models) ) ) ; }
897- Err ( e) => { let _ = copilot_tx. send ( Err ( format ! ( "Model fetch: {e}" ) ) ) ; }
926+ match copilot:: fetch_models (
927+ & http,
928+ & ct. base_url ,
929+ & ct. token ,
930+ )
931+ . await
932+ {
933+ Ok ( models) => {
934+ let _ = copilot_tx
935+ . send ( Ok ( CopilotAuthEvent :: Models ( models) ) ) ;
936+ }
937+ Err ( e) => {
938+ let _ = copilot_tx
939+ . send ( Err ( format ! ( "Model fetch: {e}" ) ) ) ;
940+ }
898941 }
899942 } ) ;
900943 } ) ;
@@ -924,12 +967,14 @@ pub fn run() -> InitResult {
924967 }
925968 }
926969 KeyCode :: Enter => {
927- if matches ! ( state. copilot_auth_status, CopilotAuthStatus :: WaitingForUser ) {
928- if !state. copilot_verification_uri . is_empty ( ) {
929- let _ = openfang_runtime:: drivers:: copilot:: open_verification_url (
930- & state. copilot_verification_uri ,
931- ) ;
932- }
970+ if matches ! (
971+ state. copilot_auth_status,
972+ CopilotAuthStatus :: WaitingForUser
973+ ) && !state. copilot_verification_uri . is_empty ( )
974+ {
975+ let _ = openfang_runtime:: drivers:: copilot:: open_verification_url (
976+ & state. copilot_verification_uri ,
977+ ) ;
933978 }
934979 }
935980 _ => { }
@@ -1956,14 +2001,15 @@ fn draw_copilot_auth(f: &mut Frame, area: Rect, state: &mut State) {
19562001 Constraint :: Length ( 1 ) , // code value
19572002 Constraint :: Length ( 1 ) , // blank
19582003 Constraint :: Length ( 1 ) , // url
1959- Constraint :: Min ( 0 ) , // spacer
2004+ Constraint :: Min ( 0 ) , // spacer
19602005 Constraint :: Length ( 1 ) , // hint
19612006 ] )
19622007 . split ( area) ;
19632008
1964- let title = Paragraph :: new ( Line :: from ( vec ! [
1965- Span :: styled( " GitHub Copilot Authentication" , Style :: default ( ) . fg( theme:: ACCENT ) ) ,
1966- ] ) ) ;
2009+ let title = Paragraph :: new ( Line :: from ( vec ! [ Span :: styled(
2010+ " GitHub Copilot Authentication" ,
2011+ Style :: default ( ) . fg( theme:: ACCENT ) ,
2012+ ) ] ) ) ;
19672013 f. render_widget ( title, chunks[ 0 ] ) ;
19682014
19692015 let spinner = theme:: SPINNER_FRAMES [ state. tick % theme:: SPINNER_FRAMES . len ( ) ] ;
@@ -1985,9 +2031,7 @@ fn draw_copilot_auth(f: &mut Frame, area: Rect, state: &mut State) {
19852031 ] ) ) ;
19862032 f. render_widget ( line1, chunks[ 2 ] ) ;
19872033
1988- let code_label = Paragraph :: new ( Line :: from ( vec ! [
1989- Span :: raw( " Enter this code:" ) ,
1990- ] ) ) ;
2034+ let code_label = Paragraph :: new ( Line :: from ( vec ! [ Span :: raw( " Enter this code:" ) ] ) ) ;
19912035 f. render_widget ( code_label, chunks[ 5 ] ) ;
19922036
19932037 let code_value = Paragraph :: new ( Line :: from ( vec ! [
@@ -2007,9 +2051,10 @@ fn draw_copilot_auth(f: &mut Frame, area: Rect, state: &mut State) {
20072051 ] ) ) ;
20082052 f. render_widget ( url, chunks[ 8 ] ) ;
20092053
2010- let hint = Paragraph :: new ( Line :: from ( vec ! [
2011- Span :: styled( " [Enter] Open browser" , theme:: dim_style( ) ) ,
2012- ] ) ) ;
2054+ let hint = Paragraph :: new ( Line :: from ( vec ! [ Span :: styled(
2055+ " [Enter] Open browser" ,
2056+ theme:: dim_style( ) ,
2057+ ) ] ) ) ;
20132058 f. render_widget ( hint, chunks[ 10 ] ) ;
20142059 }
20152060 CopilotAuthStatus :: FetchingModels => {
@@ -2040,9 +2085,10 @@ fn draw_copilot_auth(f: &mut Frame, area: Rect, state: &mut State) {
20402085 ] ) ) ;
20412086 f. render_widget ( line, chunks[ 2 ] ) ;
20422087
2043- let hint = Paragraph :: new ( Line :: from ( vec ! [
2044- Span :: styled( " Esc to go back" , theme:: dim_style( ) ) ,
2045- ] ) ) ;
2088+ let hint = Paragraph :: new ( Line :: from ( vec ! [ Span :: styled(
2089+ " Esc to go back" ,
2090+ theme:: dim_style( ) ,
2091+ ) ] ) ) ;
20462092 f. render_widget ( hint, chunks[ 10 ] ) ;
20472093 }
20482094 }
0 commit comments