Skip to content

Opencode support#5

Open
apre wants to merge 7 commits into
hanbu97:mainfrom
apre:opencode-support
Open

Opencode support#5
apre wants to merge 7 commits into
hanbu97:mainfrom
apre:opencode-support

Conversation

@apre

@apre apre commented Jul 13, 2026

Copy link
Copy Markdown

Disclaimer: everything generated by AI, I don’t understand rust and never practiced it. at least it seems to work, but no idea if it’s slop or not. tested it only quickly on my macos machine.


What this adds

Adds opencode as a first-class usage source, on par with Claude and Codex — feeding the daily/today/monthly/weekly/session/top reports, the merged dashboard, per-project/model/source breakdowns, a tu opencode subcommand, and a tu live tab.

Why it's a bit different from the other sources

Claude and Codex write newline-delimited JSON logs that the existing streaming parser walks line by line. opencode instead stores usage in a SQLite database (~/.local/share/opencode/opencode.db), so this PR adds a dedicated reader (src/pipeline/opencode.rs) that queries the message table read-only and produces the same UsageEvents the rest of the pipeline already consumes. Everything downstream is source-generic and needed no changes.

  • DB location honours $XDG_DATA_HOME and the $OPENCODE_DB override.
  • Opened read-only so a running opencode instance (WAL lock) doesn't block reads; a locked/missing DB warns and skips rather than aborting. Malformed rows are skipped individually.
  • Token buckets map as: input → input, cache.write → cache_creation, cache.read → cache_read, output + reasoning → output, reasoning → reasoning (matching opencode's own stats convention).

Cost: estimated vs. logged

opencode records its own per-message cost, which matters because it's often used with custom/local models the built-in pricing table doesn't know. New flag --opencode-cost <estimated|logged> (config mirror opencodeCost):

  • estimated (default) — recompute from the pricing table, consistent with Claude/Codex.
  • logged — trust opencode's recorded cost.

New surface

  • Subcommand: tu opencode
  • Flags: --no-opencode, --opencode-db <path> (repeatable), --opencode-cost <estimated|logged>
  • Live tab: tu live opencode
  • Config mirrors: noOpencode, opencodeDb, opencodeCost
  • Public Config API gains no_opencode / opencode_db

Notes for review

  • Dependency added: rusqlite with the bundled feature (no system SQLite lib required).
  • One extra commit fixes pre-existing clippy lints (sort_bysort_by_key, redundant .into_iter()) that a newer stable clippy flags in unrelated code, so CI's @stable clippy step stays green. Happy to drop it if you'd rather keep the PR narrow.
  • cargo fmt, cargo clippy -D warnings, and the full test suite pass (added: mapper unit tests, a fixture-DB reader test, and an end-to-end daily-report test).

apre and others added 7 commits July 13, 2026 10:09
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Discover the opencode DB in build_sources, exclude it from JSONL
discovery, and append its events in both the one-shot and live load
paths. Add an end-to-end test over a fixture database.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
These lints were introduced by a newer stable clippy than the repo was
last linted against and are unrelated to the opencode feature.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@apre apre force-pushed the opencode-support branch from 96f65b9 to 14410bf Compare July 13, 2026 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant