@@ -27,7 +27,8 @@ $script:DK_PATH = if ($PSCommandPath) { $PSCommandPath } elseif ($MyInvocation.M
2727$script :DK_DIR = if ($script :DK_PATH ) { Split-Path $script :DK_PATH - Parent } else { $null }
2828$script :SALT = " oroio"
2929$script :CACHE_TTL = 30
30- $script :CURL_TIMEOUT = 4
30+ $script :CURL_TIMEOUT = 8
31+ $script :CURL_RETRIES = 3
3132$script :LIST_MAX_JOBS = 6
3233
3334function Show-Usage {
@@ -388,78 +389,93 @@ function Fetch-Usage {
388389 RAW = " "
389390 }
390391
391- try {
392- $headers = @ {
393- " Authorization" = " Bearer $Key "
394- " User-Agent" = " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
395- }
396-
397- $response = Invoke-RestMethod - Uri " https://app.factory.ai/api/organization/members/chat-usage" `
398- - Headers $headers - Method Get - TimeoutSec $script :CURL_TIMEOUT - ErrorAction Stop
399-
400- $usage = $response.usage
401- if ($null -eq $usage ) {
402- $result.RAW = " no_usage"
403- return $result
404- }
405-
406- $section = $null
407- foreach ($s in @ ($usage.standard , $usage.premium , $usage.total , $usage.main )) {
408- if ($null -ne $s ) {
409- $section = $s
410- break
411- }
412- }
413-
414- if ($null -ne $section ) {
415- $total = $section.totalAllowance
416- if ($null -eq $total ) { $total = $section.basicAllowance }
417- if ($null -eq $total ) { $total = $section.allowance }
392+ $headers = @ {
393+ " Authorization" = " Bearer $Key "
394+ " User-Agent" = " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
395+ }
396+
397+ for ($attempt = 1 ; $attempt -le $script :CURL_RETRIES ; $attempt ++ ) {
398+ try {
399+ $response = Invoke-RestMethod - Uri " https://app.factory.ai/api/organization/members/chat-usage" `
400+ - Headers $headers - Method Get - TimeoutSec $script :CURL_TIMEOUT - ErrorAction Stop
418401
419- $used = $section.orgTotalTokensUsed
420- if ($null -eq $used ) { $used = $section.used }
421- if ($null -eq $used ) { $used = $section.tokensUsed }
422- if ($null -eq $used ) { $used = 0 }
402+ $usage = $response.usage
403+ if ($null -eq $usage ) {
404+ $result.RAW = " no_usage"
405+ return $result
406+ }
423407
424- $overage = $section.orgOverageUsed
425- if ($null -eq $overage ) { $overage = 0 }
426- $used = $used + $overage
408+ $section = $null
409+ foreach ($s in @ ($usage.standard , $usage.premium , $usage.total , $usage.main )) {
410+ if ($null -ne $s ) {
411+ $section = $s
412+ break
413+ }
414+ }
427415
428- if ($null -ne $total ) {
429- $result.TOTAL = [long ]$total
430- $result.USED = [long ]$used
431- $result.BALANCE_NUM = [long ]($total - $used )
432- $result.BALANCE = $result.BALANCE_NUM
416+ if ($null -ne $section ) {
417+ $total = $section.totalAllowance
418+ if ($null -eq $total ) { $total = $section.basicAllowance }
419+ if ($null -eq $total ) { $total = $section.allowance }
420+
421+ $used = $section.orgTotalTokensUsed
422+ if ($null -eq $used ) { $used = $section.used }
423+ if ($null -eq $used ) { $used = $section.tokensUsed }
424+ if ($null -eq $used ) { $used = 0 }
425+
426+ $overage = $section.orgOverageUsed
427+ if ($null -eq $overage ) { $overage = 0 }
428+ $used = $used + $overage
429+
430+ if ($null -ne $total ) {
431+ $result.TOTAL = [long ]$total
432+ $result.USED = [long ]$used
433+ $result.BALANCE_NUM = [long ]($total - $used )
434+ $result.BALANCE = $result.BALANCE_NUM
435+ }
433436 }
434- }
435-
436- $expRaw = $usage.endDate
437- if ($null -eq $expRaw ) { $expRaw = $usage.expire_at }
438- if ($null -eq $expRaw ) { $expRaw = $usage.expires_at }
439-
440- if ($null -ne $expRaw ) {
441- try {
442- if ($expRaw -match ' ^\d+$' ) {
443- $ts = [long ]$expRaw / 1000
444- $date = [DateTimeOffset ]::FromUnixTimeSeconds([long ]$ts )
445- $result.EXPIRES = $date.ToString (" yyyy-MM-dd" )
437+
438+ $expRaw = $usage.endDate
439+ if ($null -eq $expRaw ) { $expRaw = $usage.expire_at }
440+ if ($null -eq $expRaw ) { $expRaw = $usage.expires_at }
441+
442+ if ($null -ne $expRaw ) {
443+ try {
444+ if ($expRaw -match ' ^\d+$' ) {
445+ $ts = [long ]$expRaw / 1000
446+ $date = [DateTimeOffset ]::FromUnixTimeSeconds([long ]$ts )
447+ $result.EXPIRES = $date.ToString (" yyyy-MM-dd" )
448+ }
449+ else {
450+ $result.EXPIRES = $expRaw.ToString ()
451+ }
446452 }
447- else {
453+ catch {
448454 $result.EXPIRES = $expRaw.ToString ()
449455 }
450456 }
451- catch {
452- $result.EXPIRES = $expRaw.ToString ()
457+ return $result
458+ }
459+ catch [System.Net.WebException ] {
460+ $statusCode = [int ]$_.Exception.Response.StatusCode
461+ if ($statusCode -ge 400 -and $statusCode -lt 500 ) {
462+ $result.RAW = " http_$statusCode "
463+ $result.EXPIRES = " Invalid key"
464+ return $result
465+ }
466+ if ($attempt -lt $script :CURL_RETRIES ) {
467+ Start-Sleep - Milliseconds 500
468+ }
469+ }
470+ catch {
471+ if ($attempt -lt $script :CURL_RETRIES ) {
472+ Start-Sleep - Milliseconds 500
453473 }
454474 }
455- }
456- catch {
457- $result.BALANCE = 0
458- $result.BALANCE_NUM = 0
459- $result.RAW = " http_error"
460- $result.EXPIRES = " Invalid key"
461475 }
462476
477+ $result.RAW = " http_error"
478+ $result.EXPIRES = " Invalid key"
463479 return $result
464480}
465481
@@ -546,61 +562,73 @@ function Fetch-UsageParallel {
546562 param ([string []]$Keys )
547563
548564 $timeout = $script :CURL_TIMEOUT
565+ $retries = $script :CURL_RETRIES
549566 $maxJobs = $script :LIST_MAX_JOBS
550567 if ($maxJobs -lt 1 ) { $maxJobs = 6 }
551568 if ($Keys.Length -lt $maxJobs ) { $maxJobs = $Keys.Length }
552569
553570 $scriptBlock = {
554- param ([string ]$Key , [int ]$Timeout )
571+ param ([string ]$Key , [int ]$Timeout , [ int ] $Retries )
555572 $result = @ {
556573 BALANCE = 0 ; BALANCE_NUM = 0 ; TOTAL = 0 ; USED = 0 ; EXPIRES = " ?" ; RAW = " "
557574 }
558- try {
559- $headers = @ {
560- " Authorization" = " Bearer $Key "
561- " User-Agent" = " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
562- }
563- $response = Invoke-RestMethod - Uri " https://app.factory.ai/api/organization/members/chat-usage" `
564- - Headers $headers - Method Get - TimeoutSec $Timeout - ErrorAction Stop
565- $usage = $response.usage
566- if ($null -eq $usage ) { $result.RAW = " no_usage" ; return $result }
567- $section = $null
568- foreach ($s in @ ($usage.standard , $usage.premium , $usage.total , $usage.main )) {
569- if ($null -ne $s ) { $section = $s ; break }
570- }
571- if ($null -ne $section ) {
572- $total = $section.totalAllowance
573- if ($null -eq $total ) { $total = $section.basicAllowance }
574- if ($null -eq $total ) { $total = $section.allowance }
575- $used = $section.orgTotalTokensUsed
576- if ($null -eq $used ) { $used = $section.used }
577- if ($null -eq $used ) { $used = $section.tokensUsed }
578- if ($null -eq $used ) { $used = 0 }
579- $overage = $section.orgOverageUsed
580- if ($null -eq $overage ) { $overage = 0 }
581- $used = $used + $overage
582- if ($null -ne $total ) {
583- $result.TOTAL = [long ]$total
584- $result.USED = [long ]$used
585- $result.BALANCE_NUM = [long ]($total - $used )
586- $result.BALANCE = $result.BALANCE_NUM
575+ $headers = @ {
576+ " Authorization" = " Bearer $Key "
577+ " User-Agent" = " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
578+ }
579+ for ($attempt = 1 ; $attempt -le $Retries ; $attempt ++ ) {
580+ try {
581+ $response = Invoke-RestMethod - Uri " https://app.factory.ai/api/organization/members/chat-usage" `
582+ - Headers $headers - Method Get - TimeoutSec $Timeout - ErrorAction Stop
583+ $usage = $response.usage
584+ if ($null -eq $usage ) { $result.RAW = " no_usage" ; return $result }
585+ $section = $null
586+ foreach ($s in @ ($usage.standard , $usage.premium , $usage.total , $usage.main )) {
587+ if ($null -ne $s ) { $section = $s ; break }
587588 }
589+ if ($null -ne $section ) {
590+ $total = $section.totalAllowance
591+ if ($null -eq $total ) { $total = $section.basicAllowance }
592+ if ($null -eq $total ) { $total = $section.allowance }
593+ $used = $section.orgTotalTokensUsed
594+ if ($null -eq $used ) { $used = $section.used }
595+ if ($null -eq $used ) { $used = $section.tokensUsed }
596+ if ($null -eq $used ) { $used = 0 }
597+ $overage = $section.orgOverageUsed
598+ if ($null -eq $overage ) { $overage = 0 }
599+ $used = $used + $overage
600+ if ($null -ne $total ) {
601+ $result.TOTAL = [long ]$total
602+ $result.USED = [long ]$used
603+ $result.BALANCE_NUM = [long ]($total - $used )
604+ $result.BALANCE = $result.BALANCE_NUM
605+ }
606+ }
607+ $expRaw = $usage.endDate
608+ if ($null -eq $expRaw ) { $expRaw = $usage.expire_at }
609+ if ($null -eq $expRaw ) { $expRaw = $usage.expires_at }
610+ if ($null -ne $expRaw ) {
611+ try {
612+ if ($expRaw -match ' ^\d+$' ) {
613+ $ts = [long ]$expRaw / 1000
614+ $date = [DateTimeOffset ]::FromUnixTimeSeconds([long ]$ts )
615+ $result.EXPIRES = $date.ToString (" yyyy-MM-dd" )
616+ } else { $result.EXPIRES = $expRaw.ToString () }
617+ } catch { $result.EXPIRES = $expRaw.ToString () }
618+ }
619+ return $result
620+ } catch [System.Net.WebException ] {
621+ $statusCode = [int ]$_.Exception.Response.StatusCode
622+ if ($statusCode -ge 400 -and $statusCode -lt 500 ) {
623+ $result.RAW = " http_$statusCode " ; $result.EXPIRES = " Invalid key"
624+ return $result
625+ }
626+ if ($attempt -lt $Retries ) { Start-Sleep - Milliseconds 500 }
627+ } catch {
628+ if ($attempt -lt $Retries ) { Start-Sleep - Milliseconds 500 }
588629 }
589- $expRaw = $usage.endDate
590- if ($null -eq $expRaw ) { $expRaw = $usage.expire_at }
591- if ($null -eq $expRaw ) { $expRaw = $usage.expires_at }
592- if ($null -ne $expRaw ) {
593- try {
594- if ($expRaw -match ' ^\d+$' ) {
595- $ts = [long ]$expRaw / 1000
596- $date = [DateTimeOffset ]::FromUnixTimeSeconds([long ]$ts )
597- $result.EXPIRES = $date.ToString (" yyyy-MM-dd" )
598- } else { $result.EXPIRES = $expRaw.ToString () }
599- } catch { $result.EXPIRES = $expRaw.ToString () }
600- }
601- } catch {
602- $result.BALANCE = 0 ; $result.BALANCE_NUM = 0 ; $result.RAW = " http_error" ; $result.EXPIRES = " Invalid key"
603630 }
631+ $result.RAW = " http_error" ; $result.EXPIRES = " Invalid key"
604632 return $result
605633 }
606634
@@ -611,7 +639,7 @@ function Fetch-UsageParallel {
611639 for ($i = 0 ; $i -lt $Keys.Length ; $i ++ ) {
612640 $ps = [powershell ]::Create()
613641 $ps.RunspacePool = $runspacePool
614- [void ]$ps.AddScript ($scriptBlock ).AddArgument($Keys [$i ]).AddArgument($timeout )
642+ [void ]$ps.AddScript ($scriptBlock ).AddArgument($Keys [$i ]).AddArgument($timeout ).AddArgument( $retries )
615643 $jobs += @ { Index = $i ; PS = $ps ; Handle = $ps.BeginInvoke () }
616644 }
617645
0 commit comments