feat: CodeBuddy IDE support + fix CLI cached-token double-counting - #39
Merged
Conversation
The existing codebuddy parser only covered ~/.codebuddy/projects JSONL, so the Tencent CodeBuddy CN IDE went undetected — its per-message JSON logs live under CodeBuddyExtension/Data/**/CodeBuddyIDE/**/history/ <session>/<conversation>/messages/. Add a new codebuddy-ide source (reported under the existing CodeBuddy tool) with a cross-platform probe, a dedicated parser that emits one record per conversation from the cumulative statsSnapshot, and a watermark cursor to skip already-imported conversations. Override the path with AIUSAGE_CODEBUDDY_IDE_PATH.
CodeBuddy CLI logs usage under both message.usage (Anthropic-shaped field names but OpenAI-style semantics, where input_tokens already includes cached tokens) and providerData.rawUsage. The generic parser assumed Anthropic semantics and added cache_read_input_tokens on top of the cache-inclusive input_tokens, inflating input by the cached amount (~100x on cache-heavy turns). Read the clean prompt_cache_hit/miss decomposition from rawUsage, falling back to message.usage with cache reads subtracted from input. Verified against real glm-5.1 CLI logs (input 27537 -> 273, 28923 -> 251) and covered by a core regression test.
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two related CodeBuddy fixes, discovered while investigating why CodeBuddy usage wasn't showing up in the dashboard.
1. CodeBuddy IDE support (new source)
The existing
codebuddyparser only covered the CLI variant (~/.codebuddy/projects/*.jsonl). The Tencent CodeBuddy IDE (incl. the CN build) stores usage completely differently — per-message JSON files under:So IDE usage was never detected. This adds a new
codebuddy-idesource (reported under the existing CodeBuddy tool, so it shows as one tool in the UI):AIUSAGE_CODEBUDDY_IDE_PATH.statsSnapshot(cache-miss input, cached input, output), with a stable id so re-parsing is idempotent (no double-counting across turns).cwdresolved from the prompt'sWorkspace Folderblock.codebuddyIdewatermark cursor to skip already-imported conversations.2. Fix: CodeBuddy CLI double-counted cached tokens
The CLI logs usage under both
message.usage(Anthropic-shaped field names but OpenAI-style semantics —input_tokensalready includes cached tokens) andproviderData.rawUsage. The generic parser assumed Anthropic semantics and addedcache_read_input_tokenson top of the cache-inclusiveinput_tokens, inflating input by the cached amount (~100× on cache-heavy turns).Now reads the clean
prompt_cache_hit/missdecomposition fromrawUsage(falling back tomessage.usagewith cache reads subtracted).Verified on real
glm-5.1CLI logs:Cache reads preserved; cost drops accordingly (was overstated ~4–5×).
Test plan
codebuddy-cli.test.ts) — rawUsage decomposition +message.usagefallback.parse-codebuddy.test.ts) — one record per conversation, skips no-usage conversations, idempotent re-parse.deepseek-v4-flash; CLIglm-5.1).Notes
deepseek-v4-flash/glm-5.1are CodeBuddy's own names; they resolve to prices via the existing LiteLLM-synced registry aliases. CodeBuddy bills in its owncreditunit, so token-based cost is an estimate.packages/web/src/lib/constants.jsneeds no change — it's keyed by tool enum, and this reusescodebuddy.