refactor: keep agent usage off the bar Show only the icon. Fetch limits when the panel opens.
87 lines
3.4 KiB
Shell
Executable file
87 lines
3.4 KiB
Shell
Executable file
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
umask 077
|
|
|
|
db="${XDG_DATA_HOME:-$HOME/.local/share}/opencode/opencode.db"
|
|
tmp=$(mktemp -d)
|
|
trap 'rm -rf "$tmp"' EXIT
|
|
echo null >"$tmp/go.json"
|
|
echo null >"$tmp/xai.json"
|
|
echo '[]' >"$tmp/sessions.json"
|
|
|
|
exec 9>"${XDG_RUNTIME_DIR:-/tmp}/agent-usage.lock"
|
|
flock 9
|
|
|
|
cred() {
|
|
sqlite3 -readonly "$db" \
|
|
"select coalesce(json_extract(value, '$.$2'), '') from credential where integration_id='$1' limit 1;" \
|
|
2>/dev/null || true
|
|
}
|
|
|
|
if [[ -f $db ]]; then
|
|
if go=$(cred opencode-go key) && [[ -n $go ]]; then
|
|
curl -fsS --max-time 20 -H "Authorization: Bearer $go" \
|
|
https://opencode.ai/zen/go/v1/usage >"$tmp/go.json" || echo null >"$tmp/go.json"
|
|
fi
|
|
|
|
if xai=$(cred xai access) && [[ -n $xai ]]; then
|
|
curl -fsS --max-time 20 \
|
|
-H "Authorization: Bearer $xai" \
|
|
-H "Accept: application/json" \
|
|
"https://cli-chat-proxy.grok.com/v1/billing?format=credits" \
|
|
>"$tmp/xai.json" || echo null >"$tmp/xai.json"
|
|
fi
|
|
|
|
sqlite3 -readonly -json "$db" "
|
|
select
|
|
coalesce(json_extract(model, '\$.providerID'), 'unknown') as provider,
|
|
coalesce(json_extract(model, '\$.id'), '?') as model,
|
|
strftime('%Y-%m-%d', time_updated / 1000, 'unixepoch', 'localtime') as day,
|
|
tokens_input + tokens_output + tokens_reasoning
|
|
+ tokens_cache_read + tokens_cache_write as tokens
|
|
from session_v2
|
|
where model is not null
|
|
" >"$tmp/sessions.json" 2>/dev/null || true
|
|
[[ -s $tmp/sessions.json ]] || echo '[]' >"$tmp/sessions.json"
|
|
fi
|
|
|
|
jq -n --slurpfile go "$tmp/go.json" --slurpfile xai "$tmp/xai.json" --slurpfile sessions "$tmp/sessions.json" --arg today "$(date +%F)" '
|
|
def win(title; w):
|
|
if w == null then empty
|
|
else {title: title, percent: ((w.percent // 0) / 100), resetsAt: (w.resetsAt // "")} end;
|
|
def week: [range(0; 7) | (now - (. * 86400) | strflocaltime("%Y-%m-%d"))];
|
|
def grok_limits(c):
|
|
(c.currentPeriod.end // c.billingPeriodEnd // "") as $end
|
|
| if c.creditUsagePercent != null then
|
|
[{title: "Weekly", percent: (c.creditUsagePercent / 100), resetsAt: $end}]
|
|
elif (c.onDemandCap.val // 0) > 0 then
|
|
[{title: "On-demand", percent: ((c.onDemandUsed.val // 0) / c.onDemandCap.val), resetsAt: $end}]
|
|
else [] end;
|
|
(reduce ($sessions[0] // [])[] as $r ({};
|
|
.[$r.provider] |= (. // {today: 0, days: {}, models: {}})
|
|
| .[$r.provider] |= (
|
|
.days[$r.day] += $r.tokens
|
|
| .models[$r.model] += $r.tokens
|
|
| if $r.day == $today then .today += $r.tokens else . end
|
|
)
|
|
)) as $usage
|
|
| def attach(p):
|
|
($usage[p.id] // {}) as $u | p + {
|
|
todayTokens: ($u.today // 0),
|
|
recentDays: (week | reverse | map({date: ., tokens: ($u.days[.] // 0)})),
|
|
models: ($u.models // {} | to_entries | sort_by(-.value) | .[:4] | map({name: .key, tokens: .value}))
|
|
};
|
|
def go:
|
|
$go[0].usage as $u
|
|
| if $u == null and ($usage["opencode-go"] | not) then empty else attach({
|
|
id: "opencode-go", name: "OpenCode Go", tierLabel: "Go",
|
|
limits: [win("Session"; $u.rolling), win("Weekly"; $u.weekly), win("Monthly"; $u.monthly)]
|
|
}) end;
|
|
def grok:
|
|
$xai[0].config as $c
|
|
| if $c == null and ($usage.xai | not) then empty else attach({
|
|
id: "xai", name: "Grok", tierLabel: "Grok",
|
|
limits: (if $c == null then [] else grok_limits($c) end)
|
|
}) end;
|
|
{providers: [go, grok]}
|
|
' || echo '{"providers":[]}'
|