Repository navigation
Conversation
Sums token usage on the opencode and opencode-go providers into the agents panel's record contract: pi and omp JSONL transcripts first, because they can spend the subscription without the opencode CLI ever running, then opencode's own message database for sessions the CLI recorded itself. The scanner and cache follow the Codex collector, so the record carries the same today, last-week, active-day, and per-model fields the panel already renders.
Read the rolling, weekly, and monthly meters from the Zen gateway's usage endpoint. The key comes from OPENCODE_API_KEY, then opencode's own credential store, then pi's: pi spends the same subscription and records it under the same provider ids. A transport failure tells the panel to retry shortly, and a missing key still leaves the local token stats in place.
f3a3060 to
dddbb5f
Compare
|
@dhh this is a third OpenCode collector PR, and the first two are still open, so I'd rather sort out the overlap than have it merged blind. Three open PRs touch the same files:
What this PR does differently: one collector covers I checked #12635's session_message-first scan against a live opencode 1.18 run: assistant messages with token usage land in I'm fine reworking this onto whichever direction you want, including turning it into a user-installable collector if #12353 lands first. |
Read opencode's current `message` store first and fall back to
`session_message`, so a build that migrated does not silently read zero and a
store that has both cannot double count a session.
Cache the Go probe for 15 seconds, drop cached windows once they reset, and
keep the last good numbers when a probe fails, so opening and closing the
panel does not send a request every time. Tell a rejected key apart from a key
with no Go subscription behind it, accept the {providerID, modelID} model
object, and keep GLM capitalized.
|
Independent live verification from a machine that actually runs this, plus one finding on the plan label. Verified on a live account, not fixturesRan your collector unchanged ( Both of your test files pass here (17 ok and 5 ok, exit 0), in an isolated copy of the tree. Two things that review the local-stats question raised on #12635: on this machine the The endpoint's percents are integers ( Also, for the Console split you describe in the README: The plan label is an assertion, not a reading
No plan field — and nothing else an API key authenticates against names one. All of these 404: I did not want to open a competing collector, so the fix is a PR onto your branch: bitbonsai#1 (base
Merging #1 into your branch (or cherry-picking it — it is one commit) keeps your PR as the one upstream sees; say the word if you would rather I take it elsewhere. |
|
A design note that is worth recording here, since it is the same thread as the plan-label change. The collector currently has to reconstruct per-model usage from client stores (opencode's own database, pi/omp transcripts, and other harnesses that spent the same subscription). That is a workaround: those stores are per-machine and per-client, so they miss usage from other machines and from harnesses whose database you do not know about, and the account-level percentages in There are open feature requests for the endpoint that would replace all of it — If such an endpoint appears, this collector should prefer it and treat the local scans as a fallback for the cases where it is unavailable (no key, no entitlement, no network): one authoritative source, no per-client scans to keep in sync, and no double counting when two stores hold the same session. I have raised the requirement on #43983 with the panel's needs; no change needed here today, just worth agreeing on the direction before more local scanners get written. |
The agents panel covers Claude Code, Codex, and Fireworks but not OpenCode, even though opencode is already a default agent (
omarchy agentlaunches it, andomarchy-restart-opencodeexists). This addsomarchy-agent-usage-opencode, built like the Codex collector.Commit 1 scans local usage: pi and omp JSONL sessions on the
opencodeandopencode-goproviders first, then opencode's ownmessagestore for sessions the CLI recorded itself, falling back tosession_messagewhen a build has migrated. It fills the same fields as the other collectors (today, last week, active days, per-model) and adds the OpenCode mark plus a scanner test.Commit 2 reads the Zen Go meters from
https://opencode.ai/zen/go/v1/usage: rolling (5h), weekly, monthly. The key comes fromOPENCODE_API_KEY, then~/.local/share/opencode/auth.json, then~/.pi/agent/auth.json(pi spends the same subscription and stores it under the same provider ids). It adds a limits test and mentions the subscription inmanual/17-ai.mdand the plugin README.Commit 3 hardens the collector. The Go probe is cached for 15 seconds and cached windows are dropped once they reset, so opening and closing the panel does not repeat the request. A failed probe keeps the last good numbers, 401 and 403 get separate messages, and
{providerID, modelID}model objects plus the GLM acronym are handled.Plain Zen (
/zen/v1, theopencodeprovider) is pay-per-use and has no meters. Only/zen/go/v1/usageexists; every other usage and balance path returns 404. Without a key, local stats still show.Tests:
test/shell.d/agent-usage-opencode-scanner-test.sh(5 checks) andtest/shell.d/agent-usage-opencode-limits-test.sh(12 checks) both pass../test/allreports only the pre-existing environment failures (omarchy-pkgs checkout, omasnap, browser focus, theme file).Worth knowing before review:
~/.pi/agent/auth.jsonis the last-resort key source and can be dropped if it's unwelcome.message, sosession_messageis only a fallback. Verified with a liveopencode run.