# Uses the Z.AI REST API to show current usage of the Coding Plan. # Needs the ZAI_USAGE_API_KEY environment variable. export def usage [] { let token = $env | get --optional ZAI_USAGE_API_KEY if ($token | is-empty) { error make { msg: "Token for Z.AI missing in environment" label: { text: $"Set ZAI_USAGE_API_KEY in your secrets.nuon file" } } } let headers = { Authorization: $'Bearer ($token)' Content-Type: 'application/json' } let response = http get --headers $headers 'https://api.z.ai/api/monitor/usage/quota/limit' if ($response.code) != 200 { error make { msg: $response.msg label: { text: $"Z.AI API error — check ZAI_USAGE_API_KEY" } } } let unit_label = { 3: daily, 5: weekly, 6: monthly } $response.data.limits | each { |row| let period_key = ($row.unit | into string) let period = ($unit_label | get -o $period_key | default $row.unit) let used_opt = ($row | get --optional usage) let used = if ($used_opt | is-empty) { $row.number } else { $used_opt } let reset = ($row.nextResetTime // 1000 | into datetime --timezone u --format '%s') { type: $row.type period: $period used: $used total: $row.number pct: $row.percentage resets: $reset } } }