@@ -6,42 +6,35 @@ const BOX_WIDTH = 76;
66const PROGRESS_WIDTH = 32 ;
77
88interface TokenPlanUsage {
9- per5HourPercentage : number ;
9+ per5HourPercentage ? : number ;
1010 per5HourResetTime ?: number ;
11- per1WeekPercentage : number ;
11+ per1WeekPercentage ? : number ;
1212 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-
2717 const usage = {
2818 per5HourPercentage : response . per5HourPercentage ,
2919 per5HourResetTime : response . per5HourResetTime ,
3020 per1WeekPercentage : response . per1WeekPercentage ,
3121 per1WeekResetTime : response . per1WeekResetTime ,
3222 } ;
3323
34- const resetTimes = [
24+ const quotas = [
3525 [ usage . per5HourPercentage , usage . per5HourResetTime ] ,
3626 [ usage . per1WeekPercentage , usage . per1WeekResetTime ] ,
3727 ] ;
38- const hasValidResetTimes = resetTimes . every (
28+ const hasValidQuotas = quotas . every (
3929 ( [ percentage , resetTime ] ) =>
40- ( percentage === 0 && resetTime === undefined ) ||
41- ( typeof resetTime === "number" && Number . isFinite ( resetTime ) ) ,
30+ ( percentage === undefined && resetTime === undefined ) ||
31+ ( typeof percentage === "number" &&
32+ Number . isFinite ( percentage ) &&
33+ ( ( percentage === 0 && resetTime === undefined ) ||
34+ ( typeof resetTime === "number" && Number . isFinite ( resetTime ) ) ) ) ,
4235 ) ;
4336
44- if ( ! hasValidResetTimes ) {
37+ if ( ! hasValidQuotas ) {
4538 throw new BailianError ( "Token Plan usage response has an unexpected format." , ExitCode . GENERAL ) ;
4639 }
4740
@@ -101,11 +94,21 @@ function printView(usage: TokenPlanUsage, generatedAt: number): void {
10194 const padding = Math . max ( 0 , BOX_WIDTH - displayWidth ( ` ${ visibleContent } ` ) ) ;
10295 process . stdout . write ( `│ ${ content } ${ " " . repeat ( padding ) } │\n` ) ;
10396 } ;
104- const writeQuota = ( label : string , percentage : number , resetTime : number | undefined ) => {
97+ const writeQuota = (
98+ label : string ,
99+ unlimitedMessage : string ,
100+ percentage : number | undefined ,
101+ resetTime : number | undefined ,
102+ ) => {
103+ writeLine ( color . bold ( label ) , label ) ;
104+ if ( percentage === undefined ) {
105+ writeLine ( color . dim ( unlimitedMessage ) , unlimitedMessage ) ;
106+ return ;
107+ }
108+
105109 const percentageText = formatPercentage ( percentage ) ;
106110 const bar = progressBar ( percentage ) ;
107111 const style = progressStyle ( percentage , color . green , color . yellow , color . red ) ;
108- writeLine ( color . bold ( label ) , label ) ;
109112 writeLine ( `${ percentageText } used ${ style ( bar ) } ` , `${ percentageText } used ${ bar } ` ) ;
110113 if ( resetTime === undefined ) {
111114 writeLine (
@@ -124,9 +127,19 @@ function printView(usage: TokenPlanUsage, generatedAt: number): void {
124127 const generatedAtText = `Generated at: ${ formatDateTime ( generatedAt ) } (local time)` ;
125128 writeLine ( color . dim ( generatedAtText ) , generatedAtText ) ;
126129 process . stdout . write ( `├${ "─" . repeat ( BOX_WIDTH ) } ┤\n` ) ;
127- writeQuota ( "5-hour quota" , usage . per5HourPercentage , usage . per5HourResetTime ) ;
130+ writeQuota (
131+ "5-hour quota" ,
132+ "5小时限额当前可能无限制,请到百炼 Token Plan 控制台核实。" ,
133+ usage . per5HourPercentage ,
134+ usage . per5HourResetTime ,
135+ ) ;
128136 process . stdout . write ( `├${ "─" . repeat ( BOX_WIDTH ) } ┤\n` ) ;
129- writeQuota ( "1-week quota" , usage . per1WeekPercentage , usage . per1WeekResetTime ) ;
137+ writeQuota (
138+ "1-week quota" ,
139+ "1周限额当前可能无限制,请到百炼 Token Plan 控制台核实。" ,
140+ usage . per1WeekPercentage ,
141+ usage . per1WeekResetTime ,
142+ ) ;
130143 process . stdout . write ( `└${ "─" . repeat ( BOX_WIDTH ) } ┘\n` ) ;
131144}
132145
0 commit comments