@@ -7,21 +7,41 @@ const PROGRESS_WIDTH = 32;
77
88interface TokenPlanUsage {
99 per5HourPercentage : number ;
10- per5HourResetTime : number ;
10+ per5HourResetTime ? : number ;
1111 per1WeekPercentage : number ;
12- per1WeekResetTime : number ;
12+ per1WeekResetTime ? : number ;
1313}
1414
1515function readUsage ( result : unknown ) : TokenPlanUsage {
1616 const response = unwrapResponse ( result as Record < string , unknown > ) ;
17+ const percentages = [ response . per5HourPercentage , response . per1WeekPercentage ] ;
18+
19+ if (
20+ ! percentages . every (
21+ ( percentage ) => typeof percentage === "number" && Number . isFinite ( percentage ) ,
22+ )
23+ ) {
24+ throw new BailianError ( "Token Plan usage response has an unexpected format." , ExitCode . GENERAL ) ;
25+ }
26+
1727 const usage = {
1828 per5HourPercentage : response . per5HourPercentage ,
1929 per5HourResetTime : response . per5HourResetTime ,
2030 per1WeekPercentage : response . per1WeekPercentage ,
2131 per1WeekResetTime : response . per1WeekResetTime ,
2232 } ;
2333
24- if ( ! Object . values ( usage ) . every ( ( value ) => typeof value === "number" && Number . isFinite ( value ) ) ) {
34+ const resetTimes = [
35+ [ usage . per5HourPercentage , usage . per5HourResetTime ] ,
36+ [ usage . per1WeekPercentage , usage . per1WeekResetTime ] ,
37+ ] ;
38+ const hasValidResetTimes = resetTimes . every (
39+ ( [ percentage , resetTime ] ) =>
40+ ( percentage === 0 && resetTime === undefined ) ||
41+ ( typeof resetTime === "number" && Number . isFinite ( resetTime ) ) ,
42+ ) ;
43+
44+ if ( ! hasValidResetTimes ) {
2545 throw new BailianError ( "Token Plan usage response has an unexpected format." , ExitCode . GENERAL ) ;
2646 }
2747
@@ -81,18 +101,22 @@ function printView(usage: TokenPlanUsage, generatedAt: number): void {
81101 const padding = Math . max ( 0 , BOX_WIDTH - displayWidth ( ` ${ visibleContent } ` ) ) ;
82102 process . stdout . write ( `│ ${ content } ${ " " . repeat ( padding ) } │\n` ) ;
83103 } ;
84- const writeQuota = ( label : string , percentage : number , resetTime : number ) => {
104+ const writeQuota = ( label : string , percentage : number , resetTime : number | undefined ) => {
85105 const percentageText = formatPercentage ( percentage ) ;
86106 const bar = progressBar ( percentage ) ;
87107 const style = progressStyle ( percentage , color . green , color . yellow , color . red ) ;
88108 writeLine ( color . bold ( label ) , label ) ;
89109 writeLine ( `${ percentageText } used ${ style ( bar ) } ` , `${ percentageText } used ${ bar } ` ) ;
90- writeLine (
91- color . dim (
92- `Resets: ${ formatDateTime ( resetTime ) } (in ${ formatRemainingTime ( resetTime , generatedAt ) } )` ,
93- ) ,
94- `Resets: ${ formatDateTime ( resetTime ) } (in ${ formatRemainingTime ( resetTime , generatedAt ) } )` ,
95- ) ;
110+ if ( resetTime === undefined ) {
111+ writeLine (
112+ color . dim ( "Resets: not applicable (no usage yet)" ) ,
113+ "Resets: not applicable (no usage yet)" ,
114+ ) ;
115+ return ;
116+ }
117+
118+ const resetText = `Resets: ${ formatDateTime ( resetTime ) } (in ${ formatRemainingTime ( resetTime , generatedAt ) } )` ;
119+ writeLine ( color . dim ( resetText ) , resetText ) ;
96120 } ;
97121
98122 process . stdout . write ( `┌${ "─" . repeat ( BOX_WIDTH ) } ┐\n` ) ;
0 commit comments