How soulduse/ai-token-monitor works
Purpose and Runtime
AI Token Monitor is a desktop system-tray application for macOS and Windows that locally reads AI coding-tool session logs, calculates token/credit usage and cost, and presents live usage dashboards and alerts. It runs as a Tauri v2 application: React/Vite renderer plus Rust native backend. Primary entry points are src/main.tsx, src/App.tsx, src-tauri/src/main.rs, and src-tauri/src/lib.rs.
Desktop Host and Command Boundary
The Rust Tauri host owns native lifecycle, tray behavior, window control, file access, secure preferences, updater integration, and frontend IPC command registration. The frontend must cross this boundary rather than directly accessing local logs or OS services. Core integration points are src-tauri/src/lib.rs, src-tauri/src/commands.rs, src-tauri/tauri.conf.json, and src-tauri/capabilities/default.json.
Local Usage Ingestion and Aggregation
Provider-specific Rust adapters parse local Claude Code, Codex, OpenCode, GJC, Grok, GLM, Kimi, and Kiro records. They normalize provider data, deduplicate sessions/requests, apply pricing, and feed aggregated stats to the UI; file changes trigger refreshes. Important provider abstractions and implementations are src-tauri/src/providers/traits.rs, src-tauri/src/providers/types.rs, src-tauri/src/providers/mod.rs, src-tauri/src/providers/claude_code.rs, src-tauri/src/providers/codex.rs, and src-tauri/src/providers/kiro.rs.
Pricing, State Persistence, and Notifications
Pricing is provider/model-aware, including cache behavior and Kiro’s credit-based accounting. Hydration persists/restores local application state; webhook handling sends opt-in threshold/reset notifications; OAuth usage can retrieve plan-limit data. These backend stages are centered in src-tauri/src/providers/pricing.rs, src-tauri/src/hydration.rs, src-tauri/src/webhooks.rs, and src-tauri/src/oauth_usage.rs.
Renderer Application and Local Analytics
src/App.tsx composes the tabbed dashboard and overlays, while React contexts hold auth, settings, onboarding, and profile state. Hooks invoke Tauri commands and combine provider statistics into UI-ready daily, period, activity, and token/cost views. Key paths: src/App.tsx, src/contexts/SettingsContext.tsx, src/hooks/useCombinedStats.ts, src/hooks/useToday.ts, src/components/ActivityGraph.tsx, and src/components/UsageAlertBar.tsx.
Opt-In Cloud, Social, and Realtime Features
Supabase is the external backend only for opted-in identity, aggregated leaderboard snapshots, profiles, badges, and chat. The renderer’s Supabase client and realtime channel encapsulate cloud access; schema evolution and the badge endpoint live with Supabase deployment assets. Relevant paths: src/lib/supabase.ts, src/realtime/chatChannel.ts, src/hooks/useLeaderboardSync.ts, supabase/migrations/20260321_init.sql, and supabase/functions/badge/index.ts.
External-Service Boundaries
GitHub OAuth enables leaderboard identity; Discord, Slack, and Telegram receive configured webhooks. Optional chat translation calls a user-selected Gemini, OpenAI, or Anthropic service, with the Rust backend managing translation and encrypted local secrets. Main paths are src/contexts/AuthContext.tsx, src-tauri/src/ai_translate.rs, src-tauri/src/webhooks.rs, and src/components/SettingsOverlay.tsx.
Delivery
Node/Vite builds the React assets and Cargo/Tauri packages native installers. GitHub Actions runs validation and release workflows; version synchronization bridges JS and Rust/package metadata. Delivery paths: package.json, src-tauri/Cargo.toml, .github/workflows/test.yml, .github/workflows/release.yml, and scripts/sync-version.mjs.
Main entry points: lib.rs Tauri host · App.tsx Dashboard shell · useCombinedStats.ts Usage analytics hooks · supabase.ts Cloud client
Components 21
React Renderer
- Renderer entry
src/main.tsx - Dashboard shell · Composes tabs, dashboards, and overlays.
src/App.tsx - App state · Settings, auth, onboarding, and profile state.
src/contexts/SettingsContext.tsx - Usage analytics hooks · Invokes native commands and prepares usage views.
src/hooks/useCombinedStats.ts - Usage dashboard · Activity and threshold-alert views.
src/components/ActivityGraph.tsx - Cloud client · Encapsulates opted-in cloud access.
src/lib/supabase.ts - Leaderboard sync
src/hooks/useLeaderboardSync.ts - Realtime chat
src/realtime/chatChannel.ts
Tauri Native Host
- Tauri host · Owns lifecycle, tray, windows, secure preferences, and IPC registration.
src-tauri/src/lib.rs - IPC commands · Frontend access to local logs and OS services.
src-tauri/src/commands.rs - Provider adapters · Parse local records from supported coding tools.
src-tauri/src/providers/mod.rs - Normalized usage · Deduplicated session and request data.
src-tauri/src/providers/types.rs - Pricing engine · Model, cache, and credit-based cost accounting.
src-tauri/src/providers/pricing.rs - State hydration · Restores persisted application state.
src-tauri/src/hydration.rs - Webhook alerts · Sends opt-in threshold and reset notifications.
src-tauri/src/webhooks.rs - AI translation · Uses encrypted local provider secrets.
src-tauri/src/ai_translate.rs
Opt-in Cloud
- Supabase backend · Opt-in profiles, snapshots, badges, and chat.
supabase/migrations/20260321_init.sql - Badge endpoint
supabase/functions/badge/index.ts
Build & Delivery
- Desktop packaging · Builds renderer assets and native installers.
package.json - Release workflow · Validates and releases installers.
.github/workflows/release.yml - Version sync · Synchronizes JavaScript and Rust package versions.
scripts/sync-version.mjs
Connections 21
- Renderer entry → Dashboard shell: mounts
- Dashboard shell → App state: uses
- Dashboard shell → Usage analytics hooks: renders data from
- Usage analytics hooks → Usage dashboard: supplies
- Usage analytics hooks → IPC commands: invokes
- IPC commands → Tauri host: registered by
- Tauri host → Provider adapters: refreshes local logs
- Provider adapters → Normalized usage: normalizes and deduplicates
- Normalized usage → Pricing engine: prices usage
- Pricing engine → IPC commands: returns aggregate stats
- Tauri host → State hydration: hydrates
- Tauri host → Webhook alerts: triggers
- Tauri host → AI translation: manages
- App state → Cloud client: authenticates opted-in users
- Cloud client → Leaderboard sync: syncs through
- Cloud client → Realtime chat: opens
- Leaderboard sync → Supabase backend: reads and writes snapshots
- Realtime chat → Supabase backend: realtime chat
- Badge endpoint → Supabase backend: uses profiles and badges
- Desktop packaging → Release workflow: release input
- Version sync → Desktop packaging: aligns versions