From f70bd3740e0989e4695cc8dd3afc9a4ccbaf09f6 Mon Sep 17 00:00:00 2001 From: Diansen Sun Date: Thu, 1 Oct 2026 20:08:21 +0800 Subject: [PATCH 1/6] feat: add Codex token usage and source filtering --- .github/workflows/tests.yml | 18 +++ CLAUDE.md | 7 +- README.md | 8 +- README.zh-CN.md | 11 +- SKILL.md | 90 ++++++--------- dashboard/server.py | 60 +++++++--- dashboard/static/app.js | 52 +++++++-- dashboard/static/index.html | 18 ++- docs/codex-usage.md | 34 ++++++ scripts/analyze.py | 9 +- scripts/codex_records.py | 149 +++++++++++++++++++++++++ scripts/count_tokens.py | 30 +++-- scripts/token_stats.py | 76 +++++++++++-- scripts/work_efficiency.py | 17 ++- tests/test_codex_usage.py | 192 ++++++++++++++++++++++++++++++++ tests/test_dashboard_sources.py | 91 +++++++++++++++ 16 files changed, 746 insertions(+), 116 deletions(-) create mode 100644 .github/workflows/tests.yml create mode 100644 docs/codex-usage.md create mode 100644 scripts/codex_records.py create mode 100644 tests/test_codex_usage.py create mode 100644 tests/test_dashboard_sources.py diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..b6a4a5a --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,18 @@ +name: Token usage tests +on: + pull_request: + push: + branches: [main] +jobs: + tests: + runs-on: ubuntu-latest + strategy: + matrix: + python: ['3.11', '3.14'] + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python }} + - run: python3 -m unittest discover -s tests -v + - run: node --check dashboard/static/app.js diff --git a/CLAUDE.md b/CLAUDE.md index db225aa..9012291 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -8,7 +8,7 @@ This file is auto-loaded by Claude Code into its context whenever you open this ## What is this project -`token-usage` is a Claude Code skill that does local-first analytics on your own Claude Code transcripts (`~/.claude/projects/**/*.jsonl`). Three interfaces: +`token-usage` is a shared skill for local-first analytics on Claude Code transcripts (`~/.claude/projects/**/*.jsonl`) and Codex rollouts (`CODEX_HOME` or `~/.codex`, sessions and archived_sessions). Three interfaces: - **CLI** — `scripts/count_tokens.py`, `scripts/analyze.py` - **Browser dashboard** — `dashboard/server.py` + static frontend @@ -26,7 +26,7 @@ These are non-negotiable. Don't suggest changes that violate them without explic 1. **Pure stdlib only.** No `numpy`, `pandas`, `scipy`, `requests`, `flask`, `fastapi`, `pyyaml`, etc. on the Python side. The dashboard frontend uses Chart.js loaded from a CDN — that's the only external runtime dependency in the whole project. 2. **Zero install steps.** A user who clones the repo into `~/.claude/skills/token-usage/` should be able to run any script with `python3 scripts/foo.py` immediately. No `pip install -r requirements.txt`. No `npm install`. -3. **Don't touch `~/.claude/projects/**/*.jsonl`.** Read-only, ever. Those are the user's transcripts. +3. **Don't touch Claude or Codex transcript files.** Read-only, ever. Those are the user's transcripts. 4. **Chinese UI strings stay Chinese.** Don't "translate" 用法画像 / 突发型 / 晚高峰 etc. to English. The user-facing language is Chinese; code/comments are English. 5. **No telemetry, no analytics, no outbound calls.** Only outbound call permitted: the local `claude` CLI subprocess from the AI 解读 button (server.py `h_interpret`). @@ -40,6 +40,7 @@ README.md English landing page (GitHub default). README.zh-CN.md Chinese landing page. scripts/ + codex_records.py Codex request/counter parsing, mirrors and fork-history dedup. token_stats.py CORE — JSONL parsing, dedup-by-message-id (field-wise max), time-window filtering, bucket aggregation, billing-equiv (5m/1h ephemeral cache split). Imported by everything else. @@ -74,6 +75,8 @@ docs/ **The single source of truth for token math is `scripts/token_stats.py`.** Don't reinvent dedup or billing-equiv logic anywhere else — import from there. +Codex cache is included in input and reasoning is included in output. Never add these subsets again. Pricing/billing-equivalent values cover Claude only; expose the unpriced Codex scope. See [Codex accounting](docs/codex-usage.md). Run `python3 -m unittest discover -s tests -v` for regression tests. + --- ## How the analysis modules talk to each other diff --git a/README.md b/README.md index cfae5c0..505511c 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # token-usage -> Local-first analytics for your Claude Code token consumption. +> Local-first analytics for your Claude Code and Codex token consumption. **English** · [简体中文](./README.zh-CN.md) -A [Claude Code](https://claude.com/claude-code) skill that reads JSONL transcripts under `~/.claude/projects/`, aggregates per model / project / day, and surfaces patterns and AI-generated interpretations through four interfaces: +A local skill for Claude Code and Codex that reads JSONL transcripts under `~/.claude/projects/` and `${CODEX_HOME:-~/.codex}/sessions/` / `archived_sessions/`, aggregates per source / model / project / day, and surfaces patterns and optional AI-generated interpretations through four interfaces: - **CLI** — `count_tokens.py` for one-shot reports in any time range - **Browser dashboard** — charts + a **Work-efficiency panel** (throughput per active minute, CDF + KDE) + a **Patterns panel** (Markov / ACF / change-point) + an in-page **🤖 AI 解读** button @@ -13,6 +13,8 @@ A [Claude Code](https://claude.com/claude-code) skill that reads JSONL transcrip Zero dependencies (pure stdlib, no `numpy` / `pandas` / `chart.js` server-side). Chart.js is loaded from CDN by the browser only. +By default both sources are counted. Use `--source claude|codex|all` on the CLIs, or the dashboard's source selector. Codex cache/reasoning subsets are never counted twice; cost and billing-equivalent estimates currently cover Claude only. See [Codex accounting and tests](docs/codex-usage.md). + --- ## Screenshots @@ -47,6 +49,8 @@ Claude Code's built-in `~/.claude/stats-cache.json` lags by one day and gives yo git clone https://github.com/WholeNightCoding/token-usage ~/.claude/skills/token-usage ``` +For shared discovery by Codex, clone into `~/.agents/skills/token-usage` instead; invoke scripts from that actual folder. Codex's data directory can be overridden with `--codex-home` or `CODEX_HOME`. + The skill is auto-discovered by Claude Code. Just open Claude Code in any project and ask: > *"How many tokens did I use today?"* diff --git a/README.zh-CN.md b/README.zh-CN.md index 8af638b..0baf751 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -1,10 +1,10 @@ # token-usage -> Claude Code token 用量本地化分析工具。 +> Claude Code 与 Codex token 用量本地化分析工具。 [English](./README.md) · 简体中文 -一个 [Claude Code](https://claude.com/claude-code) skill —— 直接读 `~/.claude/projects/` 下的 JSONL transcript,按 model / project / day 聚合,并通过四个界面把数据变成可读的洞察: +一个同时支持 Claude Code 与 Codex 的本地 skill —— 读取 `~/.claude/projects/` 和 `${CODEX_HOME:-~/.codex}/sessions/`、`archived_sessions/` 下的 JSONL,按来源 / model / project / day 聚合,并通过四个界面把数据变成可读的洞察: - **CLI** — `count_tokens.py` 任意时间区间一次性出报告 - **浏览器仪表板** — 图表 + **工作效率面板**(按活跃分钟算吞吐 + CDF/KDE 分布图)+ **Patterns 面板**(Markov / ACF / 变化点检测)+ 内嵌 **🤖 AI 解读** 按钮 @@ -13,6 +13,8 @@ 零 pip 依赖(纯 stdlib,连 numpy/pandas 都没有)。Chart.js 只在浏览器端通过 CDN 加载。 +默认统计两者;命令行可用 `--source claude|codex|all`,面板可选“全部 / Claude Code / Codex”。Codex 缓存和推理子集只记一次,金额与计费折算目前只包含 Claude。详见 [Codex 统计口径与验证](docs/codex-usage.md)。 + --- ## 截图 @@ -47,6 +49,8 @@ Claude Code 自带的 `~/.claude/stats-cache.json` 有两个不爽的地方:** git clone https://github.com/WholeNightCoding/token-usage ~/.claude/skills/token-usage ``` +希望 Codex 发现共享 skill 时,可安装到 `~/.agents/skills/token-usage`;脚本从实际安装目录执行。Codex 数据目录可通过 `--codex-home` 或 `CODEX_HOME` 指定。 + Claude Code 会自动发现这个 skill。打开任意项目,问它: > *「我今天用了多少 token?」* @@ -87,6 +91,9 @@ python3 ~/.claude/skills/token-usage/scripts/count_tokens.py \ # JSON 输出(管道用) python3 ~/.claude/skills/token-usage/scripts/count_tokens.py --json + +# 只看 Codex +python3 ~/.claude/skills/token-usage/scripts/count_tokens.py --source codex --today ``` 输出列:`MODEL | MSGS | INPUT | OUTPUT | CACHE_READ | CACHE_CREATE | TOTAL`,外加总计和按权重 `input=1×, cache_read=0.1×, cache_create_5m=1.25×, cache_create_1h=2×, output=5×` 算的「计费等效输入 token」估算。 diff --git a/SKILL.md b/SKILL.md index d0ab06f..04a8c40 100644 --- a/SKILL.md +++ b/SKILL.md @@ -1,84 +1,58 @@ --- name: token-usage -description: Use when the user asks how many Claude Code tokens were consumed — today, yesterday, this week, this month, on a specific date, in a rolling window (last Nh/Nd), or across a custom date/time range — or wants a per-model breakdown with cache-read vs. cache-create split. Also applies when `~/.claude/stats-cache.json` is stale (it only updates through the previous day) or when the user wants a billing-equivalent estimate from local transcripts. Also triggers for visual/browser-based token usage monitoring — "dashboard", "监控板", "面板", "show me in a browser", live rate, per-project breakdown — via the local web UI at `dashboard/server.py`. +description: Report local Claude Code and Codex token usage for today, a date, a rolling window, or a custom range, with model, project, day, and source breakdowns. Use the local dashboard for visual monitoring and work-efficiency analysis; use the CLI for one-shot questions. --- # Token Usage Reporter -## Overview +Read local transcripts without modifying them. Default scope is both Claude Code and Codex; use `--source claude` or `--source codex` when the user names one. These are local recorded tokens, not account quota percentages or subscription charges. -Aggregates Claude Code token `usage` records from local JSONL transcripts under `~/.claude/projects/**/*.jsonl` for any time range. More accurate than `~/.claude/stats-cache.json`, which lags by one day. +## One-shot reports -**Core principle:** parse raw transcripts → filter by `timestamp` → dedupe by `message.id` → sum per model. - -## When to Use - -Trigger symptoms: -- "How many tokens did I use today / yesterday / this week / last 6 hours?" -- "Token usage between 2026-04-01 and 2026-04-15" -- "Breakdown by model" or "how much was cache vs. new input?" -- Questions about cost / billing-equivalent tokens for a period -- `stats-cache.json` doesn't include today's data -- User wants a **visual dashboard / monitoring page** (e.g. "token 监控板", "打开 token 面板", "in a browser", per-project top-N, live rate) → use the dashboard, not a one-shot CLI table - -**Do NOT use for:** message/session counts (those are in `stats-cache.json` — read it directly). - -## Quick Reference - -Script: `~/.claude/skills/token-usage/scripts/count_tokens.py` (invoke with `python3`). - -| Range flag | Scope | -|---|---| -| `--today` (default) | Today, local tz | -| `--yesterday` | Yesterday, local tz | -| `--this-week` | Mon–Sun of current week | -| `--this-month` | Current calendar month | -| `--all` | Entire history (all transcripts) | -| `--date YYYY-MM-DD` | One specific day | -| `--last 7d` / `12h` / `30m` | Rolling window ending now | -| `--from X --to Y` | Explicit range, `YYYY-MM-DD` or `YYYY-MM-DD HH:MM[:SS]` | - -Modifiers: `--by-day` (per-day table for multi-day ranges), `--json` (machine-readable), `--projects-dir PATH` (override transcript root). - -## Dashboard (visual, browser-based) - -Use when the user wants ongoing monitoring, live rate, per-project breakdown, or just a more navigable view than a CLI table. +Run from this skill's actual directory; do not assume it lives in `~/.claude/skills` or `~/.codex/skills`. ```bash -python3 ~/.claude/skills/token-usage/dashboard/server.py -# default: 127.0.0.1:8787, auto-opens browser -# flags: --port N, --host ADDR (keep 127.0.0.1 unless asked), --no-open +python3 scripts/count_tokens.py --today +python3 scripts/count_tokens.py --source codex --yesterday +python3 scripts/count_tokens.py --source claude --this-week +python3 scripts/count_tokens.py --from 2026-10-01 --to 2026-10-07 --by-day +python3 scripts/count_tokens.py --source codex --last 12h --json ``` -Shows: total + billing-equiv + USD estimate (pay-as-you-go) + last-1h rate cards, stacked daily-trend line, per-model doughnut, top-10 projects bar, realtime 1h line (auto 10s refresh), a **work-efficiency panel** (active hours, % of wall-clock, throughput while actually working, 30-min slot rate distribution, per-day breakdown — answers "how productive am I during the hours I'm at the keyboard?"), and a model×project detail table. Range selector: Today / Yesterday / This week / This month / 7d / 30d / All time / Custom… (from–to date pickers; All time auto-clamps to the first day with activity). +Range flags: `--today` (default), `--yesterday`, `--this-week`, `--this-month`, `--all`, `--date YYYY-MM-DD`, `--last 7d|12h|30m`, or `--from X --to Y`. Dates/times use local timezone. JSON retains the existing `by_model` and `total` fields, and adds `by_source`, reasoning-output and pricing-scope metadata. -The efficiency panel reports rates against **active minutes** (minutes that consumed tokens), not wall clock — so a 20-min work block isn't averaged against the 40 idle minutes around it. Also available as a CLI: `python3 ~/.claude/skills/token-usage/scripts/work_efficiency.py [days]`. +Sources: -Server: same data source as CLI (numbers match), in-memory cached + mtime-invalidated, binds `127.0.0.1` only, no auth. +- Claude Code: `~/.claude/projects/**/*.jsonl`; override with `--projects-dir`. +- Codex CLI/Desktop: `${CODEX_HOME:-~/.codex}/sessions/**/*.jsonl` and `archived_sessions/**/*.jsonl`; override with `--codex-home` (the home containing both folders). -**Do NOT launch the dashboard just to answer a one-shot CLI-style question.** Use `count_tokens.py` instead for "how many tokens yesterday" — it's faster and fits better in chat. +## Dashboard -## Canonical Example +Use when the user asks for a monitoring page, charts, live rates or navigable analysis. Do not launch it for a one-shot token-count question. ```bash -python3 ~/.claude/skills/token-usage/scripts/count_tokens.py --from 2026-04-01 --to 2026-04-21 --by-day +python3 dashboard/server.py +# 127.0.0.1:8787; --no-open, --port N, --projects-dir PATH, --codex-home PATH ``` -Output columns: `MODEL | MSGS | INPUT | OUTPUT | CACHE_READ | CACHE_CREATE | TOTAL`, plus grand total and a **billing-equivalent input tokens** estimate using weights `input=1×, cache_read=0.1×, cache_create_5m=1.25×, cache_create_1h=2×, output=5×`. CACHE_CREATE in the table is the 5m+1h sum; the billing-equiv splits them out via the per-record `cache_creation.ephemeral_1h_input_tokens` field. +The source selector (全部 / Claude Code / Codex) applies to totals, trends, models, projects, realtime, efficiency and Patterns. The dashboard caches the corpus and invalidates it when transcript files change. Keep it bound to loopback unless the user requests another binding. -## Reading the output +Other reports use the same scanner: -- **Scope**: covers Claude Code only. Anthropic API direct calls, Claude.ai web/desktop usage are NOT in these transcripts — totals will be lower than your actual Anthropic billing. -- **Sub-agent dedup is automatic**: same `message.id` appearing in parent + sub-agent transcripts is merged via field-wise max. The numbers you see are post-dedup; no double-counting. -- `` model rows are cache-only compaction events; always sum to zero, safe to ignore. -- `cache_create_5m` is priced at 1.25× input, `cache_create_1h` at 2×; billing-equiv splits them via `cache_creation.ephemeral_1h_input_tokens` per record. +```bash +python3 scripts/work_efficiency.py 7 --source codex +python3 scripts/analyze.py --days 7 --source codex --html report.html +``` -(Internals — dedup-by-max, UTC handling, subagent scanning — see CLAUDE.md.) +The existing optional AI interpretation button uses the local `claude` CLI, regardless of the selected statistics source; ordinary statistics require only Python's standard library. -## Common Mistakes +## Interpret correctly -- **Reporting `cache_read` as new-input cost.** It's 0.1× in billing; cite billing-equiv when cost is the question. -- **Using `stats-cache.json` for "today".** It only updates through the previous day. -- **Pre-converting dates to UTC.** The script handles local-tz; pass the user's date as-is. -- **Filtering by file mtime.** Transcripts append throughout a session; always filter by the JSONL `timestamp` field. +- Claude Code: dedupe parent/subagent and streaming snapshots by message ID with field-wise maxima; cache creation retains the 5-minute/1-hour split. +- Codex: prefer per-response `token_usage_record` when present, dedupe response IDs, and suppress mirrored `token_count` events. Older rollouts use cumulative-counter differences, reading preceding counters before applying a time window. Repeated snapshots, counter resets and inherited fork history are handled separately. +- Codex cached input is a subset of input; the INPUT column shows the non-cached remainder. Reasoning output is a subset of OUTPUT. Never add either subset again to total tokens. +- Amounts and billing-equivalent values use the existing Claude pricing configuration only. Codex tokens are marked unpriced. Never apply Claude rates to Codex, or present the API estimate as the user's subscription bill. Mixed-source efficiency disables the billing view and retains raw throughput. +- Missing/deleted/local-unrecorded history cannot be reconstructed. Cloud conversations without local rollout files are outside the report. +- Filter by event timestamp, not file modification date. Do not use `stats-cache.json` to answer today's usage. +For format/dedup details and validation commands, see [docs/codex-usage.md](docs/codex-usage.md). diff --git a/dashboard/server.py b/dashboard/server.py index f511065..f7fa08a 100644 --- a/dashboard/server.py +++ b/dashboard/server.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -"""Local web dashboard for Claude Code token usage.""" +"""Local web dashboard for Claude Code and Codex token usage.""" from __future__ import annotations import argparse @@ -28,6 +28,8 @@ STATIC_DIR = os.path.join(HERE, "static") DEFAULT_PORT = 8787 +PROJECTS_ROOT = ts.PROJECTS_DIR +CODEX_ROOT = ts.CODEX_HOME # Whole-corpus records cache: one scan feeds every endpoint. Invalidated by a # signature over (path, mtime, size) of all JSONL files — so new activity reloads @@ -53,7 +55,7 @@ def _corpus_signature(): """Cheap fingerprint of all transcripts — just stat calls, no reads.""" out = [] - for f in ts.list_transcript_files(): + for f in ts.list_transcript_files(PROJECTS_ROOT, codex_home=CODEX_ROOT): try: st = os.stat(f) out.append((f, st.st_mtime_ns, st.st_size)) @@ -76,13 +78,26 @@ def get_all_records(): with _records_lock: if _records_cache["signature"] == sig: return _records_cache["records"] - records = ts.scan_records(_EPOCH, _FAR_FUTURE) + records = ts.scan_records(_EPOCH, _FAR_FUTURE, PROJECTS_ROOT, codex_home=CODEX_ROOT) with _records_lock: _records_cache["records"] = records _records_cache["signature"] = sig return records +def source_for(q: dict) -> str: + source = q.get('source', ['all'])[0] + if source not in ts.SOURCES: + raise HTTPError(400, {'error': 'source must be all, claude, or codex'}) + return source + + +def source_records(q: dict): + source = source_for(q) + records = get_all_records() + return records if source == 'all' else [r for r in records if r.source == source] + + def _range_cache_key(q: dict): """Stable key for the current query's range. @@ -104,7 +119,7 @@ def _range_cache_key(q: dict): else: key = ("custom", q.get("from", [""])[0], q.get("to", [""])[0]) minute = int(time.time()) // 60 if ROLLING else None - return key + (minute,) + return key + (source_for(q), minute) def resolve_qs_range(q: dict): @@ -116,7 +131,7 @@ def resolve_qs_range(q: dict): # Clamp to the first day with any activity — otherwise by_day / # efficiency iterate dense days from 1970 (20k+ empty rows, and # active-vs-wall-clock percentages lose all meaning). - records = get_all_records() + records = source_records(q) if records: first = min(r.t_utc for r in records).astimezone(ts.local_tz()) start = first.replace(hour=0, minute=0, second=0, microsecond=0) @@ -140,7 +155,7 @@ def records_for(q: dict): start, end, label = resolve_qs_range(q) start_utc = start.astimezone(timezone.utc) end_utc = end.astimezone(timezone.utc) - records = _filter_by_window(get_all_records(), start_utc, end_utc) + records = _filter_by_window(source_records(q), start_utc, end_utc) return records, {"from": start.isoformat(), "to": end.isoformat(), "label": label} @@ -154,7 +169,7 @@ def h_summary(q): now_local = datetime.now(ts.local_tz()) rt_start_utc = (now_local - timedelta(hours=1)).astimezone(timezone.utc) now_utc = now_local.astimezone(timezone.utc) - rt_records = _filter_by_window(get_all_records(), rt_start_utc, now_utc) + rt_records = _filter_by_window(source_records(q), rt_start_utc, now_utc) rt_tokens = sum(r.total for r in rt_records) return { @@ -162,6 +177,9 @@ def h_summary(q): "total": totals["all"], "billing_equiv": totals["billing_equiv"], "est_usd": round(usd, 2), + 'unpriced_tokens': totals['unpriced_tokens'], + 'billing_scope': 'claude', + 'by_source': ts.aggregate_by_source(records), "rate_1h_tokens": rt_tokens, "rate_per_min": int(rt_tokens / 60) if rt_tokens else 0, } @@ -191,7 +209,7 @@ def h_realtime(q): delta = ts.parse_duration(window) start = now_local - delta records = _filter_by_window( - get_all_records(), + source_records(q), start.astimezone(timezone.utc), now_local.astimezone(timezone.utc), ) @@ -231,9 +249,10 @@ def h_efficiency(q): start, end, label = resolve_qs_range(q) start_utc = start.astimezone(timezone.utc) end_utc = end.astimezone(timezone.utc) - records = _filter_by_window(get_all_records(), start_utc, end_utc) + records = _filter_by_window(source_records(q), start_utc, end_utc) result = we.compute_efficiency(records, start, end) result["range"]["label"] = label + result['billing_supported'] = all(r.source == 'claude' for r in records) and source_for(q) != 'codex' return result @@ -258,7 +277,7 @@ def h_dashboard(q): now_local = datetime.now(ts.local_tz()) rt_start_utc = (now_local - timedelta(hours=1)).astimezone(timezone.utc) rt_records = _filter_by_window( - get_all_records(), + source_records(q), rt_start_utc, now_local.astimezone(timezone.utc), ) @@ -273,10 +292,14 @@ def h_dashboard(q): "total": totals["all"], "billing_equiv": totals["billing_equiv"], "est_usd": round(usd, 2), + 'unpriced_tokens': totals['unpriced_tokens'], + 'billing_scope': 'claude', "rate_1h_tokens": rt_tokens, "rate_per_min": int(rt_tokens / 60) if rt_tokens else 0, }, "by_day": ts.aggregate_by_day(records), + 'source': source_for(q), + 'by_source': ts.aggregate_by_source(records), "by_model": ts.aggregate_by_model(records), "by_project": ts.aggregate_by_project(records, limit=limit), "detail": ts.aggregate_detail(records), @@ -380,7 +403,7 @@ def _compute_patterns(q): # --- features / markov window (uses user-chosen bucket) --- recs = _filter_by_window( - get_all_records(), + source_records(q), start_local.astimezone(timezone.utc), end_local.astimezone(timezone.utc), ) @@ -418,7 +441,7 @@ def _compute_patterns(q): # --- changepoint: daily totals, dense, over cp_days_eff ending at end_local --- cp_start = end_local - timedelta(days=cp_days_eff) recs_cp = _filter_by_window( - get_all_records(), + source_records(q), cp_start.astimezone(timezone.utc), end_local.astimezone(timezone.utc), ) @@ -436,6 +459,7 @@ def _compute_patterns(q): "bucket_sec": bucket_sec, "cp_days": cp_days_eff, "tz": now_local.strftime("%z"), + 'source': source_for(q), } if custom_range: params["from"] = start_local.date().isoformat() @@ -477,7 +501,7 @@ def h_interpret(q): model = os.environ.get("TOKEN_USAGE_LLM_MODEL", "claude-sonnet-4-6") prompt = ( - "你是一个数据分析师。下面是某 Claude Code 用户最近一段时间的 token 用量时序分析结果" + "你是一个数据分析师。下面是某用户最近一段时间的 Claude Code / Codex token 用量时序分析结果" "(包含描述统计、突发性、自相关、马尔可夫状态转移、变化点检测等指标)。\n\n" "请用中文写一份简洁的解读报告(500-800 字),结构如下:\n\n" "## TL;DR\n一句话总结用户画像(如「晚高峰个人项目型 / 高黏性 / 强 24h 周期」等)。\n\n" @@ -586,7 +610,9 @@ def do_GET(self): url = urlparse(self.path) if url.path in ROUTES: try: - result = ROUTES[url.path](parse_qs(url.query)) + query = parse_qs(url.query) + source_for(query) + result = ROUTES[url.path](query) except CLIENT_GONE: self.close_connection = True return @@ -610,6 +636,7 @@ def do_GET(self): RELOAD_WATCH_FILES = [ os.path.abspath(__file__), os.path.join(os.path.dirname(HERE), "scripts", "token_stats.py"), + os.path.join(os.path.dirname(HERE), "scripts", "codex_records.py"), os.path.join(os.path.dirname(HERE), "scripts", "work_efficiency.py"), ] @@ -644,13 +671,18 @@ def _reload_watcher(poll_sec: float = 1.0): def main(): + global PROJECTS_ROOT, CODEX_ROOT ap = argparse.ArgumentParser() ap.add_argument("--port", type=int, default=DEFAULT_PORT) ap.add_argument("--host", default="127.0.0.1") ap.add_argument("--no-open", action="store_true") + ap.add_argument('--projects-dir', default=ts.PROJECTS_DIR) + ap.add_argument('--codex-home', default=ts.CODEX_HOME) ap.add_argument("--no-reload", action="store_true", help="disable auto-reload on Python source changes") args = ap.parse_args() + PROJECTS_ROOT = os.path.expanduser(args.projects_dir) + CODEX_ROOT = os.path.expanduser(args.codex_home) srv = ThreadingHTTPServer((args.host, args.port), Handler) url = f"http://{args.host}:{args.port}/" diff --git a/dashboard/static/app.js b/dashboard/static/app.js index 0029058..04d1b01 100644 --- a/dashboard/static/app.js +++ b/dashboard/static/app.js @@ -1,6 +1,7 @@ 'use strict'; const state = { + source: 'all', range: 'this-week', // named range, or 'custom' (then customFrom/To apply) customFrom: '', customTo: '', @@ -91,16 +92,26 @@ function rangeQS() { const p = new URLSearchParams(); if (state.customFrom) p.set('from', state.customFrom); if (state.customTo) p.set('to', state.customTo); + p.set('source', state.source); return p.toString(); } - return 'range=' + encodeURIComponent(state.range); + return 'range=' + encodeURIComponent(state.range) + '&source=' + state.source; } function rangeKey() { return state.range === 'custom' - ? `custom:${state.customFrom}:${state.customTo}` - : state.range; + ? `${state.source}:custom:${state.customFrom}:${state.customTo}` + : `${state.source}:${state.range}`; } +$('#source').addEventListener('change', () => { + state.source = $('#source').value; + refreshAll(); + refreshRealtime(); + refreshPatterns(patternsParamsFromUI()); + $('#interpret-dismiss')?.click(); + lastPatternsQS = ''; +}); + // ---- dashboard data: client-side cache keyed by range ---- // Re-clicking a range served <60s ago returns instantly with zero network. // Range buttons also keep the previous panels visible while fresh data loads. @@ -446,9 +457,11 @@ function formatRtTooltipTitle(iso) { // ---- panel updaters (reuse chart instances) ---- function updateSummary(s) { $('#card-total').textContent = fmt(s.total); - $('#card-billing').textContent = fmt(s.billing_equiv); - $('#card-usd').textContent = '$' + (s.est_usd ?? 0).toFixed(2); + $('#card-billing').textContent = state.source === 'codex' ? '—' : fmt(s.billing_equiv); + $('#card-usd').textContent = state.source === 'codex' ? '—' : '$' + (s.est_usd ?? 0).toFixed(2); $('#card-rate').textContent = fmtInt(s.rate_per_min); + $('#usage-note').textContent = '仅统计本机记录;Codex 缓存已包含在输入量中,推理已包含在输出量中。' + + (s.unpriced_tokens || state.source === 'codex' ? ' Codex 暂未配置计价,金额与折算卡只包含 Claude Code。' : ' 金额与折算按现有 Claude 计价配置估算。'); } function updateDaily(rows) { @@ -686,7 +699,7 @@ function renderEffPerDay(rows) { ${escapeHtml(r.date)} ${hrs.toFixed(1)}h ${fmt(r.tokens)} - ${fmt(r.billing_equiv)} + ${effState.data?.billing_supported === false ? '—' : fmt(r.billing_equiv)} ${fmt(r.rate_per_hr)}/h `; }).join(''); @@ -694,17 +707,24 @@ function renderEffPerDay(rows) { function updateEfficiency(data) { effState.data = data; + if (data.billing_supported === false) effState.mode = 'raw'; + document.querySelectorAll('.eff-dist-toggle button').forEach(btn => { + btn.disabled = btn.dataset.effMode === 'bill' && data.billing_supported === false; + btn.classList.toggle('active', btn.dataset.effMode === effState.mode); + btn.title = btn.disabled ? 'Codex 尚未配置计价,请查看 raw 用量' : ''; + }); const s = data.summary; $('#eff-active-hours').textContent = s.active_hours.toFixed(1) + 'h'; $('#eff-active-pct').textContent = (s.active_pct * 100).toFixed(1) + '%'; $('#eff-rate-raw').textContent = fmt(s.avg_rate_raw_per_hr) + '/h'; - $('#eff-rate-bill').textContent = fmt(s.avg_rate_bill_per_hr) + '/h'; + $('#eff-rate-bill').textContent = data.billing_supported === false ? '—' : fmt(s.avg_rate_bill_per_hr) + '/h'; const sd = data.slot_distribution; $('#eff-slot-meta').innerHTML = `${fmtInt(sd.active_slots)} / ${fmtInt(sd.total_slots)} 个 30 分钟槽有活动` + ` (${(sd.active_slot_pct * 100).toFixed(1)}% 占用率)  ·  ` - + `中位 ${fmt(sd.raw_per_hr.median)}/h (raw) / ${fmt(sd.bill_per_hr.median)}/h (bill)`; + + `中位 ${fmt(sd.raw_per_hr.median)}/h (raw)` + + (data.billing_supported === false ? ' · Codex 未配置计价,计费视图不可用' : ` / ${fmt(sd.bill_per_hr.median)}/h (bill)`); renderEffDistribution(); renderEffPerDay(data.per_day); @@ -838,6 +858,7 @@ async function refreshAll({ force = false } = {}) { // drop the result. if (key !== rangeKey()) return; updateSummary(data.summary); + $('#source-totals').textContent = `Claude Code:${fmtInt(data.by_source.claude.all)} tokens · Codex:${fmtInt(data.by_source.codex.all)} tokens`; updateDaily(data.by_day); updateModel(data.by_model); updateProject(data.by_project); @@ -864,9 +885,11 @@ function fmtWindowMinutes(min) { } async function refreshRealtime() { + const source = state.source; const windowMin = computeRealtimeWindowMinutes(); - const url = `/api/realtime?bucket=${state.rtBucketSec}&window=${windowMin}m`; + const url = `/api/realtime?bucket=${state.rtBucketSec}&window=${windowMin}m&source=${source}`; const res = await fetchJSON(url); + if (source !== state.source) return; updateRealtime(res.rows); const lbl = $('#realtime-range-label'); if (lbl) lbl.textContent = `— last ${fmtWindowMinutes(windowMin)}, ${state.rtLabel} buckets (auto 10s)`; @@ -891,7 +914,7 @@ let lastPatternsQS = ''; // remember the most recent successful que function patternsParamsFromUI() { const range = $('#patterns-range')?.value || '7d'; const bucket = $('#patterns-bucket')?.value || '1800'; - const params = { bucket }; + const params = { bucket, source: state.source }; if (range === 'custom') { const f = $('#patterns-from')?.value || ''; const t = $('#patterns-to')?.value || ''; @@ -946,6 +969,8 @@ async function fetchPatterns(force = false, params = null) { } async function refreshPatterns(params = null) { + const source = state.source; + params = { ...(params || patternsParamsFromUI()), source }; const panel = $('#patterns-panel'); const applyBtn = $('#patterns-apply'); panel?.classList.add('is-loading'); @@ -957,6 +982,7 @@ async function refreshPatterns(params = null) { } try { const data = await fetchPatterns(true, params); + if (source !== state.source) return; renderPatterns(data); const lbl = $('#patterns-summary-label'); if (lbl && data?.params) lbl.textContent = patternsLabelFromParams(data.params); @@ -1440,6 +1466,7 @@ function setInterpretActions({ copy, regen, dismiss }) { } async function runInterpret() { + const source = state.source; if (interpretInFlight) return; const btn = $('#patterns-interpret'); const body = $('#interpret-body'); @@ -1457,8 +1484,11 @@ async function runInterpret() { $('#interpret-section')?.scrollIntoView({ behavior: 'smooth', block: 'start' }); try { - const res = await fetch('/api/interpret' + qs); + const query = new URLSearchParams(qs); + query.set('source', source); + const res = await fetch('/api/interpret?' + query); const data = await res.json(); + if (source !== state.source) return; if (!res.ok || data.error) { const msg = data.error || `HTTP ${res.status}`; const hint = data.hint ? `
${renderMarkdown(data.hint)}` : ''; diff --git a/dashboard/static/index.html b/dashboard/static/index.html index 9772565..17eab14 100644 --- a/dashboard/static/index.html +++ b/dashboard/static/index.html @@ -3,7 +3,7 @@ - Claude Code Token Usage + Claude Code / Codex Token Usage
-

Claude Code Token Usage

+

Claude Code / Codex Token Usage

+