# soulduse/ai-token-monitor architecture

> Architecture diagram of the GitHub repository soulduse/ai-token-monitor, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/soulduse/ai-token-monitor
- Repository: https://github.com/soulduse/ai-token-monitor

## Overview

#### 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: [`src-tauri/src/lib.rs`](https://github.com/soulduse/ai-token-monitor/blob/main/src-tauri/src/lib.rs) (Tauri host), [`src/App.tsx`](https://github.com/soulduse/ai-token-monitor/blob/main/src/App.tsx) (Dashboard shell), [`src/hooks/useCombinedStats.ts`](https://github.com/soulduse/ai-token-monitor/blob/main/src/hooks/useCombinedStats.ts) (Usage analytics hooks), [`src/lib/supabase.ts`](https://github.com/soulduse/ai-token-monitor/blob/main/src/lib/supabase.ts) (Cloud client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_renderer["React Renderer"]
  node_renderer_entry["Renderer entry<br/>React/Vite bootstrap<br/>[main.tsx]"]
  node_app_shell["Dashboard shell<br/>React application<br/>[App.tsx]"]
  node_renderer_state["App state<br/>React contexts"]
  node_usage_hooks["Usage analytics hooks<br/>React data hooks"]
  node_usage_views["Usage dashboard<br/>React components<br/>[ActivityGraph.tsx]"]
  node_supabase_client["Cloud client<br/>Supabase client<br/>[supabase.ts]"]
  node_leaderboard_sync["Leaderboard sync<br/>React sync hook"]
  node_chat_realtime["Realtime chat<br/>Supabase channel<br/>[chatChannel.ts]"]
end

subgraph group_native["Tauri Native Host"]
  node_tauri_host{{"Tauri host<br/>Rust desktop host<br/>[lib.rs]"}}
  node_commands["IPC commands<br/>Tauri command boundary<br/>[commands.rs]"]
  node_provider_adapters["Provider adapters<br/>Rust ingestion adapters<br/>[mod.rs]"]
  node_usage_pipeline["Normalized usage<br/>provider domain types<br/>[types.rs]"]
  node_pricing["Pricing engine<br/>provider pricing<br/>[pricing.rs]"]
  node_local_state[("State hydration<br/>local persistence<br/>[hydration.rs]")]
  node_notifications["Webhook alerts<br/>notification service<br/>[webhooks.rs]"]
  node_translation["AI translation<br/>Rust AI service<br/>[ai_translate.rs]"]
end

subgraph group_cloud["Opt-in Cloud"]
  node_supabase_backend[("Supabase backend<br/>cloud data platform<br/>[20260321_init.sql]")]
  node_badge_endpoint["Badge endpoint<br/>Supabase Edge Function<br/>[index.ts]"]
end

subgraph group_delivery["Build &amp; Delivery"]
  node_build_package["Desktop packaging<br/>Node + Cargo build<br/>[package.json]"]
  node_release_workflow["Release workflow<br/>GitHub Actions<br/>[release.yml]"]
  node_version_sync["Version sync<br/>release script<br/>[sync-version.mjs]"]
end

node_renderer_entry -->|"mounts"| node_app_shell
node_app_shell -->|"uses"| node_renderer_state
node_app_shell -->|"renders data from"| node_usage_hooks
node_usage_hooks -->|"supplies"| node_usage_views
node_usage_hooks -->|"invokes"| node_commands
node_commands -->|"registered by"| node_tauri_host
node_tauri_host -->|"refreshes local logs"| node_provider_adapters
node_provider_adapters -->|"normalizes and deduplicates"| node_usage_pipeline
node_usage_pipeline -->|"prices usage"| node_pricing
node_pricing -->|"returns aggregate stats"| node_commands
node_tauri_host -->|"hydrates"| node_local_state
node_tauri_host -->|"triggers"| node_notifications
node_tauri_host -->|"manages"| node_translation
node_renderer_state -->|"authenticates opted-in users"| node_supabase_client
node_supabase_client -->|"syncs through"| node_leaderboard_sync
node_supabase_client -->|"opens"| node_chat_realtime
node_leaderboard_sync -->|"reads and writes snapshots"| node_supabase_backend
node_chat_realtime -->|"realtime chat"| node_supabase_backend
node_badge_endpoint -->|"uses profiles and badges"| node_supabase_backend
node_build_package -->|"release input"| node_release_workflow
node_version_sync -->|"aligns versions"| node_build_package

click node_renderer_entry "https://github.com/soulduse/ai-token-monitor/blob/main/src/main.tsx"
click node_app_shell "https://github.com/soulduse/ai-token-monitor/blob/main/src/App.tsx"
click node_renderer_state "https://github.com/soulduse/ai-token-monitor/blob/main/src/contexts/SettingsContext.tsx"
click node_usage_hooks "https://github.com/soulduse/ai-token-monitor/blob/main/src/hooks/useCombinedStats.ts"
click node_usage_views "https://github.com/soulduse/ai-token-monitor/blob/main/src/components/ActivityGraph.tsx"
click node_supabase_client "https://github.com/soulduse/ai-token-monitor/blob/main/src/lib/supabase.ts"
click node_leaderboard_sync "https://github.com/soulduse/ai-token-monitor/blob/main/src/hooks/useLeaderboardSync.ts"
click node_chat_realtime "https://github.com/soulduse/ai-token-monitor/blob/main/src/realtime/chatChannel.ts"
click node_tauri_host "https://github.com/soulduse/ai-token-monitor/blob/main/src-tauri/src/lib.rs"
click node_commands "https://github.com/soulduse/ai-token-monitor/blob/main/src-tauri/src/commands.rs"
click node_provider_adapters "https://github.com/soulduse/ai-token-monitor/blob/main/src-tauri/src/providers/mod.rs"
click node_usage_pipeline "https://github.com/soulduse/ai-token-monitor/blob/main/src-tauri/src/providers/types.rs"
click node_pricing "https://github.com/soulduse/ai-token-monitor/blob/main/src-tauri/src/providers/pricing.rs"
click node_local_state "https://github.com/soulduse/ai-token-monitor/blob/main/src-tauri/src/hydration.rs"
click node_notifications "https://github.com/soulduse/ai-token-monitor/blob/main/src-tauri/src/webhooks.rs"
click node_translation "https://github.com/soulduse/ai-token-monitor/blob/main/src-tauri/src/ai_translate.rs"
click node_supabase_backend "https://github.com/soulduse/ai-token-monitor/blob/main/supabase/migrations/20260321_init.sql"
click node_badge_endpoint "https://github.com/soulduse/ai-token-monitor/blob/main/supabase/functions/badge/index.ts"
click node_build_package "https://github.com/soulduse/ai-token-monitor/blob/main/package.json"
click node_release_workflow "https://github.com/soulduse/ai-token-monitor/blob/main/.github/workflows/release.yml"
click node_version_sync "https://github.com/soulduse/ai-token-monitor/blob/main/scripts/sync-version.mjs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_renderer_entry,node_app_shell,node_renderer_state,node_usage_hooks,node_usage_views,node_supabase_client,node_leaderboard_sync,node_chat_realtime toneBlue
class node_tauri_host,node_commands,node_provider_adapters,node_usage_pipeline,node_pricing,node_local_state,node_notifications,node_translation toneAmber
class node_supabase_backend,node_badge_endpoint toneMint
class node_build_package,node_release_workflow,node_version_sync toneRose
```

## Components

### React Renderer

- [Renderer entry](https://github.com/soulduse/ai-token-monitor/blob/main/src/main.tsx): React/Vite bootstrap · `src/main.tsx`
- [Dashboard shell](https://github.com/soulduse/ai-token-monitor/blob/main/src/App.tsx): React application · Composes tabs, dashboards, and overlays. · `src/App.tsx`
- [App state](https://github.com/soulduse/ai-token-monitor/blob/main/src/contexts/SettingsContext.tsx): React contexts · Settings, auth, onboarding, and profile state. · `src/contexts/SettingsContext.tsx`
- [Usage analytics hooks](https://github.com/soulduse/ai-token-monitor/blob/main/src/hooks/useCombinedStats.ts): React data hooks · Invokes native commands and prepares usage views. · `src/hooks/useCombinedStats.ts`
- [Usage dashboard](https://github.com/soulduse/ai-token-monitor/blob/main/src/components/ActivityGraph.tsx): React components · Activity and threshold-alert views. · `src/components/ActivityGraph.tsx`
- [Cloud client](https://github.com/soulduse/ai-token-monitor/blob/main/src/lib/supabase.ts): Supabase client · Encapsulates opted-in cloud access. · `src/lib/supabase.ts`
- [Leaderboard sync](https://github.com/soulduse/ai-token-monitor/blob/main/src/hooks/useLeaderboardSync.ts): React sync hook · `src/hooks/useLeaderboardSync.ts`
- [Realtime chat](https://github.com/soulduse/ai-token-monitor/blob/main/src/realtime/chatChannel.ts): Supabase channel · `src/realtime/chatChannel.ts`

### Tauri Native Host

- [Tauri host](https://github.com/soulduse/ai-token-monitor/blob/main/src-tauri/src/lib.rs): Rust desktop host · Owns lifecycle, tray, windows, secure preferences, and IPC registration. · `src-tauri/src/lib.rs`
- [IPC commands](https://github.com/soulduse/ai-token-monitor/blob/main/src-tauri/src/commands.rs): Tauri command boundary · Frontend access to local logs and OS services. · `src-tauri/src/commands.rs`
- [Provider adapters](https://github.com/soulduse/ai-token-monitor/blob/main/src-tauri/src/providers/mod.rs): Rust ingestion adapters · Parse local records from supported coding tools. · `src-tauri/src/providers/mod.rs`
- [Normalized usage](https://github.com/soulduse/ai-token-monitor/blob/main/src-tauri/src/providers/types.rs): provider domain types · Deduplicated session and request data. · `src-tauri/src/providers/types.rs`
- [Pricing engine](https://github.com/soulduse/ai-token-monitor/blob/main/src-tauri/src/providers/pricing.rs): provider pricing · Model, cache, and credit-based cost accounting. · `src-tauri/src/providers/pricing.rs`
- [State hydration](https://github.com/soulduse/ai-token-monitor/blob/main/src-tauri/src/hydration.rs): local persistence · Restores persisted application state. · `src-tauri/src/hydration.rs`
- [Webhook alerts](https://github.com/soulduse/ai-token-monitor/blob/main/src-tauri/src/webhooks.rs): notification service · Sends opt-in threshold and reset notifications. · `src-tauri/src/webhooks.rs`
- [AI translation](https://github.com/soulduse/ai-token-monitor/blob/main/src-tauri/src/ai_translate.rs): Rust AI service · Uses encrypted local provider secrets. · `src-tauri/src/ai_translate.rs`

### Opt-in Cloud

- [Supabase backend](https://github.com/soulduse/ai-token-monitor/blob/main/supabase/migrations/20260321_init.sql): cloud data platform · Opt-in profiles, snapshots, badges, and chat. · `supabase/migrations/20260321_init.sql`
- [Badge endpoint](https://github.com/soulduse/ai-token-monitor/blob/main/supabase/functions/badge/index.ts): Supabase Edge Function · `supabase/functions/badge/index.ts`

### Build & Delivery

- [Desktop packaging](https://github.com/soulduse/ai-token-monitor/blob/main/package.json): Node + Cargo build · Builds renderer assets and native installers. · `package.json`
- [Release workflow](https://github.com/soulduse/ai-token-monitor/blob/main/.github/workflows/release.yml): GitHub Actions · Validates and releases installers. · `.github/workflows/release.yml`
- [Version sync](https://github.com/soulduse/ai-token-monitor/blob/main/scripts/sync-version.mjs): release script · Synchronizes JavaScript and Rust package versions. · `scripts/sync-version.mjs`

## Connections

- 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
