How steipete/codexbar works
1. Repository shape
- This is a multi-target Swift package for a macOS menu bar app called CodexBar, plus a bundled cross-platform CLI, a WidgetKit extension, and shared core libraries.
- The top-level architecture is intentionally split into app/UI, CLI, core provider logic, and shared runtime/support.
2. Primary product surfaces
- Sources/CodexBar is the macOS app target: menu bar status item, popover menus, settings windows, notifications, onboarding, and update-related UI.
- Sources/CodexBarCLI is the command-line interface used for scripting, diagnostics, cost scans, and local service/serve mode.
- Sources/CodexBarWidget provides WidgetKit views and providers for system widgets.
- Sources/CodexBarCore contains the reusable business logic that powers both the app and CLI.
3. Core architectural boundary: provider abstraction
- The dominant design is a provider plugin model. Each AI service has its own provider folder with:
- a descriptor and fetcher/probe in CodexBarCore,
- an app-side implementation/settings store/login flow in CodexBar,
- and tests for provider-specific behavior.
- The shared provider APIs live under Sources/CodexBarCore/Providers/Shared, which define the contracts for provider discovery, settings, runtime, presentation, and token/cookie access.
4. Data sources and external integration
- Providers pull usage/limits from many different sources: OAuth APIs, API keys, browser cookies, local config files, CLI output, local storage, and local logs.
- Several providers use specialized sources such as gcloud/Vertex OAuth, AWS Cost Explorer/Bedrock signing, Claude/Codex CLI sessions, browser cookie import/decryption, and OpenAI dashboard web scraping.
- This means the system is not a single API client; it is a federation of source adapters with provider-specific auth and parsing rules.
5. Runtime flow from source to UI
- A refresh starts in the app’s UsageStore and ProviderRefreshCoordinator, which coordinate polling, caching, and failure handling.
- Provider fetchers in core produce normalized usage snapshots and status data.
- The app converts those snapshots into menu bar text, icon state, quota warnings, charts, and notifications via StatusItemController, MenuCardView, and related menu/overlay components.
- Widget data flows through the same shared snapshot models.
6. State, persistence, and configuration
- Persistent state is centered around settings/config stores, token stores, keychain-backed credentials, and usage/history stores.
- The repo supports both a modern config path and legacy migration path, with helpers in SettingsStore+Config* and CodexBarConfigMigrator.
- Storage is split by concern:
- config and toggles,
- provider credentials,
- usage history and snapshots,
- keychain/browser-cookie artifacts,
- local caches for performance and resilience.
7. UI subsystem
- The UI is mostly macOS-native and centered on a status item / popover menu experience.
- StatusItemController is the orchestrator for menu presentation, provider switching, refresh indicators, icon rendering, and transient effects like quota warnings or confetti.
- Settings live in a dedicated preferences experience with provider-specific panes, general/display/advanced sections, and debug tooling.
- There is also optional Merge Icons mode, where a single status item can present multiple providers through a switcher.
8. Login and credential management
- Authentication is handled per-provider, with separate login runners/flows for OAuth, device flow, browser-session import, or CLI bootstrap.
- The repo has substantial credential plumbing: keychain access gates, cookie header stores, token stores, and managed account reconciliation for Codex/OpenAI-style accounts.
- This is a major boundary: the app often reuses existing user sessions rather than collecting passwords, and some flows deliberately avoid persistent secrets beyond the required token/header artifacts.
9. CLI as a parallel interface
- The CLI is not just a thin wrapper; it exposes configuration management, diagnostics, caching, usage queries, serve mode, and cost analysis.
- Core CLI support includes a local HTTP server, terminal/process runners, and output/rendering layers for machine-readable and human-readable responses.
- Linux-specific tests in TestsLinux suggest the CLI is meant to be portable beyond macOS even though the app itself is macOS-first.
10. Cost, history, and analytics subsystem
- Beyond live quota display, the app tracks historical usage, spend, reset windows, and cost scans.
- The core includes vendored CostUsage scanning logic for parsing local logs and deriving spend/cost history for supported providers.
- Chart views and usage history menu components in the app consume these normalized historical models to render inline dashboards.
11. Status and resilience
- Several providers expose live status probes, and the app surfaces that in the menu bar and popover UI.
- There are watchdogs, memory-pressure relief, timeout handling, background refresh logic, and process/subprocess helpers to keep the app responsive and avoid hanging on external calls.
- This is a resilience-heavy architecture because many providers depend on flaky web sessions, local CLIs, or rate-limited endpoints.
12. Packaging, release, and distribution
- The repo includes extensive release automation: packaging scripts, signing/notarization, appcast generation, Sparkle update verification, and CLI tarball release helpers.
- GitHub Actions handles CI, release publishing, and upstream monitoring.
- The product is distributed as both a macOS app and a standalone CLI, with release assets and update metadata managed separately.
13. Testing strategy
- The test suite is large and highly provider-focused, with broad coverage for parsing, auth flows, UI snapshot behavior, configuration, caching, and resilience paths.
- The tests are a strong architectural signal: the system is designed around many small provider adapters and a shared core contract, with lots of characterization tests to prevent regressions.
- Linux tests further reinforce that core logic is separated from AppKit-specific code.
14. What to draw in an architecture diagram
- The most accurate diagram would show:
- macOS app UI layer and CLI layer on top,
- both calling into shared core provider logic,
- core talking outward to provider-specific sources (APIs, browsers, CLIs, local files, keychain),
- plus persistent config/cache/history storage in the middle,
- and optional widget/update/release side channels around the edges.
Main entry points: CodexBar CodexBar · Providers Provider Adapters · UsageStore.swift Usage Store · CodexBarCLI CodexBarCLI
Components 19
App UI
macOS menu bar app, preferences, status item, and provider-specific UI
- CodexBar · Menu bar app target with popover UI, settings, notifications, and update surfaces
Sources/CodexBar - Status Item · Orchestrates icon state, menu presentation, refresh indicators, and provider switching
Sources/CodexBar/StatusItemController.swift - Usage Store · Coordinates refresh, caching, quota status, history, and widget snapshot production
Sources/CodexBar/UsageStore.swift - Settings Store · Owns app configuration, provider toggles, token accounts, and config migration
Sources/CodexBar/SettingsStore.swift - Refresh Coordinator · Schedules provider polling, failure handling, and status updates
Sources/CodexBar/ProviderRefreshCoordinator.swift - Preferences · Provider panes, general/display/advanced sections, and debug tooling
Sources/CodexBar/PreferencesView.swift - Provider UI · Provider settings, login flows, menus, and presentation helpers
Sources/CodexBar/Providers
CLI
Command-line surface for config, diagnostics, usage, and local serving
- CodexBarCLI · Command-line interface for usage queries, config, diagnose, cost, cache, and serve mode
Sources/CodexBarCLI - Local Server · Embedded HTTP server and router for local programmatic access
Sources/CodexBarCLI/CLILocalHTTPServer.swift - CLI Output · Human and machine-readable output formatting and termination handling
Sources/CodexBarCLI/CLIRenderer.swift
Core
Shared provider engine, models, auth plumbing, storage, and adapters
- CodexBarCore · Shared provider logic, models, auth, storage, and parsing
Sources/CodexBarCore - Provider APIs · Provider descriptor, implementation, runtime, presentation, token, and menu contracts
Sources/CodexBarCore/Providers/ProviderDescriptor.swift - Provider Catalog · Catalog and registry of provider implementations and discovery metadata
Sources/CodexBarCore/Providers/Providers.swift - Provider Adapters · Provider-specific fetchers, probes, login/auth flows, and snapshot normalization
Sources/CodexBarCore/Providers - Auth & Storage · Keychain, cookie headers, token stores, managed accounts, and config storage
Sources/CodexBarCore/KeychainAccessGate.swift - History & Cost · Historical usage, spend, reset windows, cost scanning, and pace calculations
Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner.swift - Runtime Support · Process execution, HTTP client, logging, browser detection, and resilience helpers
Sources/CodexBarCore/Host
Widget
WidgetKit bundle and shared snapshot rendering
- WidgetKit · Widget bundle, provider, and views powered by shared usage snapshots
Sources/CodexBarWidget
Release
Packaging, signing, CI, and distribution automation
- Release Tooling · Packaging, signing, appcast generation, CI, and release automation
Scripts
Connections 28
- Status Item → Usage Store: reads snapshots · Menu bar UI consumes normalized usage and history state
- Status Item → Refresh Coordinator: triggers refresh · UI actions and timers request provider polling and updates
- Usage Store → Settings Store: reads config · Refresh and display logic depend on persistent settings and provider selection
- Usage Store → CodexBarCore: uses core models · App state is backed by shared provider and usage models
- Refresh Coordinator → Provider Adapters: polls providers · Coordinator invokes provider fetchers and probes through core adapters
- Provider UI → Provider Adapters: configures flows · App-side provider panes and login flows bind to provider-specific behavior
- Provider UI → Provider APIs: uses contracts · Provider UI is built against shared provider interfaces
- Preferences → Settings Store: edits settings · Preferences persist provider toggles, config, and token account choices
- Preferences → Provider UI: shows provider panes · Preferences hosts provider-specific settings and login surfaces
- CodexBar → Status Item: hosts · Main app wires the menu bar controller and presentation lifecycle
- CodexBar → Preferences: presents · App exposes the settings and debug experience
- CodexBar → Provider UI: runs login · App launches provider-specific authentication and account flows
- CodexBar → Auth & Storage: persists state · App manages keychain, cookies, tokens, and migration paths
- CodexBarCLI → CodexBarCore: queries core · CLI reuses shared provider logic, config, and usage models
- CodexBarCLI → Local Server: serve mode · CLI can expose a local HTTP interface for programmatic access
- CodexBarCLI → CLI Output: renders output · CLI formats diagnostics, usage, and cache responses
- CodexBarCore → Provider APIs: defines contracts · Core exposes shared provider APIs and runtime contracts
- Provider APIs → Provider Catalog: registers · Shared APIs feed provider discovery and registry wiring
- Provider Catalog → Provider Adapters: selects adapters · Registry resolves concrete provider implementations
- Provider Adapters → Auth & Storage: reads credentials · Providers consume keychain, cookies, tokens, and config-backed artifacts
- Provider Adapters → Runtime Support: uses runtime · Providers rely on process, HTTP, logging, and resilience helpers
- History & Cost → Provider Adapters: derives cost · Historical scanners and parsers ingest provider logs and usage traces
- History & Cost → Usage Store: feeds charts · App history views and quota displays consume normalized analytics
- WidgetKit → Usage Store: reads snapshots · Widget views use the same shared snapshot models as the app
- Release Tooling → CodexBar: packages app · Scripts and workflows build, sign, notarize, and publish the macOS app
- Release Tooling → CodexBarCLI: packages cli · Release tooling also produces CLI artifacts and tarballs
- CodexBar → WidgetKit: supports · App and widget share the same usage data model and release pipeline
- CodexBarCore → History & Cost: scans logs · Core includes cost and spend scanning for local logs and history