@@ -165,26 +165,42 @@ void Invoke(Action action)
165165
166166 case SessionUsageInfoEvent usageInfo :
167167 var uData = usageInfo . Data ;
168- var uModel = uData ? . GetType ( ) . GetProperty ( "Model" ) ? . GetValue ( uData ) ? . ToString ( ) ;
169- var uCurrentTokens = uData ? . GetType ( ) . GetProperty ( "CurrentTokens" ) ? . GetValue ( uData ) as int ? ;
170- var uTokenLimit = uData ? . GetType ( ) . GetProperty ( "TokenLimit" ) ? . GetValue ( uData ) as int ? ;
171- var uInputTokens = uData ? . GetType ( ) . GetProperty ( "InputTokens" ) ? . GetValue ( uData ) as int ? ;
172- var uOutputTokens = uData ? . GetType ( ) . GetProperty ( "OutputTokens" ) ? . GetValue ( uData ) as int ? ;
173- if ( ! string . IsNullOrEmpty ( uModel ) )
174- state . Info . Model = uModel ;
175- Invoke ( ( ) => OnUsageInfoChanged ? . Invoke ( sessionName , new SessionUsageInfo ( uModel , uCurrentTokens , uTokenLimit , uInputTokens , uOutputTokens ) ) ) ;
168+ var uCurrentTokensRaw = uData ? . GetType ( ) . GetProperty ( "CurrentTokens" ) ? . GetValue ( uData ) ;
169+ var uTokenLimitRaw = uData ? . GetType ( ) . GetProperty ( "TokenLimit" ) ? . GetValue ( uData ) ;
170+ var uCurrentTokens = uCurrentTokensRaw != null ? ( int ? ) Convert . ToInt32 ( uCurrentTokensRaw ) : null ;
171+ var uTokenLimit = uTokenLimitRaw != null ? ( int ? ) Convert . ToInt32 ( uTokenLimitRaw ) : null ;
172+ Invoke ( ( ) => OnUsageInfoChanged ? . Invoke ( sessionName , new SessionUsageInfo ( null , uCurrentTokens , uTokenLimit , null , null ) ) ) ;
176173 break ;
177174
178175 case AssistantUsageEvent assistantUsage :
179176 var aData = assistantUsage . Data ;
180177 var aModel = aData ? . GetType ( ) . GetProperty ( "Model" ) ? . GetValue ( aData ) ? . ToString ( ) ;
181- var aInput = aData ? . GetType ( ) . GetProperty ( "InputTokens" ) ? . GetValue ( aData ) as int ? ;
182- var aOutput = aData ? . GetType ( ) . GetProperty ( "OutputTokens" ) ? . GetValue ( aData ) as int ? ;
178+ var aInputRaw = aData ? . GetType ( ) . GetProperty ( "InputTokens" ) ? . GetValue ( aData ) ;
179+ var aOutputRaw = aData ? . GetType ( ) . GetProperty ( "OutputTokens" ) ? . GetValue ( aData ) ;
180+ var aInput = aInputRaw != null ? ( int ? ) Convert . ToInt32 ( aInputRaw ) : null ;
181+ var aOutput = aOutputRaw != null ? ( int ? ) Convert . ToInt32 ( aOutputRaw ) : null ;
182+ QuotaInfo ? aPremiumQuota = null ;
183+ try
184+ {
185+ var quotaSnapshots = aData ? . GetType ( ) . GetProperty ( "QuotaSnapshots" ) ? . GetValue ( aData ) ;
186+ if ( quotaSnapshots is Dictionary < string , object > qs &&
187+ qs . TryGetValue ( "premium_interactions" , out var premiumObj ) &&
188+ premiumObj is System . Text . Json . JsonElement je )
189+ {
190+ var isUnlimited = je . TryGetProperty ( "isUnlimitedEntitlement" , out var u ) && u . GetBoolean ( ) ;
191+ var entitlement = je . TryGetProperty ( "entitlementRequests" , out var e ) ? e . GetInt32 ( ) : - 1 ;
192+ var used = je . TryGetProperty ( "usedRequests" , out var ur ) ? ur . GetInt32 ( ) : 0 ;
193+ var remaining = je . TryGetProperty ( "remainingPercentage" , out var rp ) ? rp . GetInt32 ( ) : 100 ;
194+ var resetDate = je . TryGetProperty ( "resetDate" , out var rd ) ? rd . GetString ( ) : null ;
195+ aPremiumQuota = new QuotaInfo ( isUnlimited , entitlement , used , remaining , resetDate ) ;
196+ }
197+ }
198+ catch { }
183199 if ( ! string . IsNullOrEmpty ( aModel ) )
184200 state . Info . Model = aModel ;
185- if ( aInput . HasValue || aOutput . HasValue )
201+ if ( aInput . HasValue || aOutput . HasValue || aPremiumQuota != null )
186202 {
187- Invoke ( ( ) => OnUsageInfoChanged ? . Invoke ( sessionName , new SessionUsageInfo ( aModel , null , null , aInput , aOutput ) ) ) ;
203+ Invoke ( ( ) => OnUsageInfoChanged ? . Invoke ( sessionName , new SessionUsageInfo ( aModel , null , null , aInput , aOutput , aPremiumQuota ) ) ) ;
188204 }
189205 break ;
190206
0 commit comments