# marcosarnold/cash-cow architecture

> Architecture diagram of the GitHub repository marcosarnold/cash-cow, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/marcosarnold/cash-cow
- Repository: https://github.com/marcosarnold/cash-cow

## Overview

1. **Repository shape**
   - This is a **Chrome Extension MV3** with a **React/TypeScript frontend** and a **Python sidecar server**.
   - The extension UI is built with **Vite** and styled with **Tailwind CSS**.
   - The architecture is split into browser-resident logic, local persistence, and a local AI/classification service.

2. **Top-level runtime boundaries**
   - **Browser extension runtime**: popup, content scripts, background service worker, settings/dashboard UI.
   - **Local Python server** on `localhost:8080`: merchant classification via Anthropic Claude.
   - **Offline scraping pipeline**: Python scraper generates card reward data consumed by the extension.

3. **Checkout detection layer**
   - The content-script entrypoints live under `src/pages/content/`.
   - `amazon-amount.js` and related content code detect supported merchant checkout pages, extract the transaction amount and merchant context, and emit a `CHECKOUT_DETECTED`-style event.
   - This layer is intentionally narrow: it observes the page, but does not do recommendation logic itself.

4. **Extension orchestration**
   - The background service worker in `src/pages/background/index.ts` is the coordination hub.
   - It listens for messages from content scripts, updates the extension badge, and handles keyboard shortcut behavior.
   - This keeps page instrumentation separated from UI rendering and from the recommendation engine.

5. **Popup and primary user experience**
   - `popup.html`, `src/popup.tsx`, and `src/components/MainPopup.tsx` drive the main extension surface.
   - The popup shows the recommended card, supporting rationale, and a card carousel.
   - The popup is the primary point where browser-detected checkout data becomes an actionable recommendation.

6. **Rewards recommendation engine**
   - Core reward logic lives in `src/lib/rewards/`.
   - `engine.ts`, `rules.ts`, and `cardDataService.ts` load card definitions, map merchant category to reward rules, and compute expected value using transaction amount × reward rate.
   - The output is a ranked list of cards, not just a single match, which lets the UI explain tradeoffs.

7. **Card and merchant data model**
   - Static reward data is bundled in `src/lib/credit_card_rewards.json`.
   - Type definitions in `src/lib/types.ts` and `src/lib/creditCardTypes.ts` normalize card issuer/reward structures for the engine and UI.
   - Merchant classification helpers in `src/lib/merchant/agenticDetect.ts` and related parser code bridge raw merchant names to reward categories.

8. **AI classification service**
   - `server/agent.py` is a FastAPI-based local service that calls **Anthropic Claude**.
   - Its job is to classify merchants into reward categories such as dining, groceries, gas, travel, and e-commerce.
   - This is the main networked dependency in the architecture, and it sits outside the extension so the browser can keep the rest of the flow local.

9. **Data ingestion / scraping pipeline**
   - `server/scraper.py` is a separate offline acquisition tool that scrapes credit card reward data from external sources.
   - The scraper feeds structured reward metadata into the JSON used by the extension.
   - This creates a clear separation between **data collection** and **runtime recommendation**.

10. **Settings, preferences, and local state**
   - `src/components/settings/` contains account/privacy, card management, and reward preference UI.
   - `src/lib/storage.ts` is the likely persistence boundary for extension-local settings and user preferences.
   - This part of the system is designed to keep user-specific configuration in-browser rather than in the Python service.

11. **Dashboard and secondary UI surfaces**
   - `src/components/RewardsDashboard.tsx` provides a broader analytics view beyond the checkout popup.
   - UI primitives such as `MinimizedWidget`, `ThemeContext`, and the shared card components support alternate extension states and reusable rendering.
   - These are presentation layers over the same rewards data, not separate business logic.

12. **Content/page-specific UX and assets**
   - `src/pages/content/style.css` and the assets under `src/assets/` provide the embedded page-facing styling and imagery.
   - The extension also includes localization (`src/locales/en/messages.json`) and shared global styling (`src/styles/globals.css`, `src/assets/styles/tailwind.css`).
   - These assets support a polished extension experience but do not change the core data flow.

13. **End-to-end flow**
   - Page checkout is detected in the content script → background worker is notified → merchant context is classified by the local agent server when needed → the reward engine ranks cards using stored reward data → popup/dashboard renders the recommendation.
   - The system is architected so that most decision-making can happen locally, with the Python server only handling AI classification.

Main entry points: [`src/lib/rewards/engine.ts`](https://github.com/marcosarnold/cash-cow/blob/main/src/lib/rewards/engine.ts) (Rewards Engine), [`src/pages/background/index.ts`](https://github.com/marcosarnold/cash-cow/blob/main/src/pages/background/index.ts) (Background SW), [`src/components/MainPopup.tsx`](https://github.com/marcosarnold/cash-cow/blob/main/src/components/MainPopup.tsx) (Main Popup), [`src/pages/content/index.tsx`](https://github.com/marcosarnold/cash-cow/blob/main/src/pages/content/index.tsx) (Checkout Detect)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_browser["Browser Extension"]
  node_content["Checkout Detect<br/>content script<br/>[index.tsx]"]
  node_amazon["Amazon Amount<br/>content detector<br/>[amazon-amount.js]"]
  node_merchant_detect["Merchant Detect<br/>page parser<br/>[detect.ts]"]
  node_background["Background SW<br/>MV3 worker<br/>[index.ts]"]
  node_popup["Popup UI<br/>extension entry<br/>[popup.tsx]"]
  node_main_popup["Main Popup<br/>UI shell<br/>[MainPopup.tsx]"]
  node_dashboard["Rewards Dashboard<br/>dashboard UI"]
  node_settings["Settings Menu<br/>settings UI<br/>[SettingsMenu.tsx]"]
  node_manage_cards["Manage Cards<br/>settings panel<br/>[ManageCards.tsx]"]
  node_storage[("Local Storage<br/>persistence<br/>[storage.ts]")]
  node_reward_engine{{"Rewards Engine<br/>ranking logic<br/>[engine.ts]"}}
  node_rules["Reward Rules<br/>reward policy<br/>[rules.ts]"]
  node_card_data[("Card Data<br/>data service<br/>[cardDataService.ts]")]
  node_merchant_detect_ai["Agentic Detect<br/>merchant classifier<br/>[agenticDetect.ts]"]
  node_category_parser["Category Parser<br/>mapping helper<br/>[category_parser.py]"]
end

subgraph group_server["Local Python Service"]
  node_agent["Claude Agent<br/>FastAPI service<br/>[agent.py]"]
  node_scraper["Reward Scraper<br/>offline pipeline<br/>[scraper.py]"]
end

subgraph group_data["Reward Data"]
  node_rewards_json[("Reward Catalog<br/>static dataset")]
end

node_content -->|"page context"| node_merchant_detect
node_content -->|"checkout detected"| node_background
node_amazon -->|"specialized entry"| node_content
node_background -->|"open UI"| node_popup
node_popup -->|"renders"| node_main_popup
node_main_popup -->|"request ranking"| node_reward_engine
node_dashboard -->|"reuse logic"| node_reward_engine
node_reward_engine -->|"apply rules"| node_rules
node_reward_engine -->|"load cards"| node_card_data
node_card_data -->|"read catalog"| node_rewards_json
node_reward_engine -->|"classify merchant"| node_merchant_detect_ai
node_merchant_detect_ai -->|"local call"| node_agent
node_agent -->|"normalize category"| node_category_parser
node_scraper -->|"populate data"| node_rewards_json
node_storage -->|"preferences"| node_settings
node_settings -->|"configure"| node_manage_cards
node_main_popup -->|"read prefs"| node_storage
node_background -->|"sync state"| node_storage

click node_content "https://github.com/marcosarnold/cash-cow/blob/main/src/pages/content/index.tsx"
click node_amazon "https://github.com/marcosarnold/cash-cow/blob/main/src/pages/content/amazon-amount.js"
click node_merchant_detect "https://github.com/marcosarnold/cash-cow/blob/main/src/pages/content/merchant/detect.ts"
click node_background "https://github.com/marcosarnold/cash-cow/blob/main/src/pages/background/index.ts"
click node_popup "https://github.com/marcosarnold/cash-cow/blob/main/src/popup.tsx"
click node_main_popup "https://github.com/marcosarnold/cash-cow/blob/main/src/components/MainPopup.tsx"
click node_dashboard "https://github.com/marcosarnold/cash-cow/blob/main/src/components/RewardsDashboard.tsx"
click node_settings "https://github.com/marcosarnold/cash-cow/blob/main/src/components/settings/SettingsMenu.tsx"
click node_manage_cards "https://github.com/marcosarnold/cash-cow/blob/main/src/components/settings/ManageCards.tsx"
click node_storage "https://github.com/marcosarnold/cash-cow/blob/main/src/lib/storage.ts"
click node_reward_engine "https://github.com/marcosarnold/cash-cow/blob/main/src/lib/rewards/engine.ts"
click node_rules "https://github.com/marcosarnold/cash-cow/blob/main/src/lib/rewards/rules.ts"
click node_card_data "https://github.com/marcosarnold/cash-cow/blob/main/src/lib/rewards/cardDataService.ts"
click node_merchant_detect_ai "https://github.com/marcosarnold/cash-cow/blob/main/src/lib/merchant/agenticDetect.ts"
click node_agent "https://github.com/marcosarnold/cash-cow/blob/main/server/agent.py"
click node_scraper "https://github.com/marcosarnold/cash-cow/blob/main/server/scraper.py"
click node_category_parser "https://github.com/marcosarnold/cash-cow/blob/main/src/category_parser.py"
click node_rewards_json "https://github.com/marcosarnold/cash-cow/blob/main/src/lib/credit_card_rewards.json"

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_content,node_amazon,node_merchant_detect,node_background,node_popup,node_main_popup,node_dashboard,node_settings,node_manage_cards,node_storage,node_reward_engine,node_rules,node_card_data,node_merchant_detect_ai,node_category_parser toneBlue
class node_agent,node_scraper toneAmber
class node_rewards_json toneMint
```

## Components

### Browser Extension

MV3 extension runtime, UI, and local persistence in the browser.

- [Checkout Detect](https://github.com/marcosarnold/cash-cow/blob/main/src/pages/content/index.tsx): content script · Page observers that detect supported checkout pages and extract amount and merchant context. · `src/pages/content/index.tsx`
- [Amazon Amount](https://github.com/marcosarnold/cash-cow/blob/main/src/pages/content/amazon-amount.js): content detector · Merchant-specific checkout instrumentation for amount extraction. · `src/pages/content/amazon-amount.js`
- [Merchant Detect](https://github.com/marcosarnold/cash-cow/blob/main/src/pages/content/merchant/detect.ts): page parser · Merchant-page detection helpers and heuristics for supported sites. · `src/pages/content/merchant/detect.ts`
- [Background SW](https://github.com/marcosarnold/cash-cow/blob/main/src/pages/background/index.ts): MV3 worker · Coordinates messages, badge updates, and keyboard shortcut behavior. · `src/pages/background/index.ts`
- [Popup UI](https://github.com/marcosarnold/cash-cow/blob/main/src/popup.tsx): extension entry · Main popup entry that renders the checkout recommendation experience. · `src/popup.tsx`
- [Main Popup](https://github.com/marcosarnold/cash-cow/blob/main/src/components/MainPopup.tsx): UI shell · Primary popup surface with recommendation, rationale, and carousel state. · `src/components/MainPopup.tsx`
- [Rewards Dashboard](https://github.com/marcosarnold/cash-cow/blob/main/src/components/RewardsDashboard.tsx): dashboard UI · Broader rewards analytics surface beyond the checkout popup. · `src/components/RewardsDashboard.tsx`
- [Settings Menu](https://github.com/marcosarnold/cash-cow/blob/main/src/components/settings/SettingsMenu.tsx): settings UI · Entry point for preferences and account/privacy controls. · `src/components/settings/SettingsMenu.tsx`
- [Manage Cards](https://github.com/marcosarnold/cash-cow/blob/main/src/components/settings/ManageCards.tsx): settings panel · Card management and reward preference configuration. · `src/components/settings/ManageCards.tsx`
- [Local Storage](https://github.com/marcosarnold/cash-cow/blob/main/src/lib/storage.ts): persistence · Extension-local persistence boundary for settings and user preferences. · `src/lib/storage.ts`
- [Rewards Engine](https://github.com/marcosarnold/cash-cow/blob/main/src/lib/rewards/engine.ts): ranking logic · Computes expected value and ranks cards for a detected purchase. · `src/lib/rewards/engine.ts`
- [Reward Rules](https://github.com/marcosarnold/cash-cow/blob/main/src/lib/rewards/rules.ts): reward policy · Maps merchant categories and card rules into reward calculations. · `src/lib/rewards/rules.ts`
- [Card Data](https://github.com/marcosarnold/cash-cow/blob/main/src/lib/rewards/cardDataService.ts): data service · Loads card definitions and exposes structured reward metadata to the engine. · `src/lib/rewards/cardDataService.ts`
- [Agentic Detect](https://github.com/marcosarnold/cash-cow/blob/main/src/lib/merchant/agenticDetect.ts): merchant classifier · Bridges raw merchant names to reward categories, including AI-assisted classification paths. · `src/lib/merchant/agenticDetect.ts`
- [Category Parser](https://github.com/marcosarnold/cash-cow/blob/main/src/category_parser.py): mapping helper · Python-side category parsing and normalization helpers. · `src/category_parser.py`

### Local Python Service

Local merchant-classification and scraping utilities running outside the extension.

- [Claude Agent](https://github.com/marcosarnold/cash-cow/blob/main/server/agent.py): FastAPI service · Local Python classification service that calls Anthropic Claude for merchant categorization. · `server/agent.py`
- [Reward Scraper](https://github.com/marcosarnold/cash-cow/blob/main/server/scraper.py): offline pipeline · Offline data acquisition tool that scrapes reward metadata for bundling. · `server/scraper.py`

### Reward Data

Static reward catalog and merchant/category mapping used by the engine.

- [Reward Catalog](https://github.com/marcosarnold/cash-cow/blob/main/src/lib/credit_card_rewards.json): static dataset · Bundled JSON reward catalog consumed by the extension runtime. · `src/lib/credit_card_rewards.json`

## Connections

- Checkout Detect → Merchant Detect: page context · Content scripts use merchant detection to narrow supported checkout pages.
- Checkout Detect → Background SW: checkout detected · Detected checkout data is sent to the background worker for orchestration.
- Amazon Amount → Checkout Detect: specialized entry · Amazon-specific amount extraction feeds the broader content-script flow.
- Background SW → Popup UI: open UI · Background state can surface the popup experience when checkout data arrives.
- Popup UI → Main Popup: renders · Popup entry mounts the main recommendation UI.
- Main Popup → Rewards Engine: request ranking · Popup asks the reward engine for card recommendations and rationale.
- Rewards Dashboard → Rewards Engine: reuse logic · Dashboard reuses the same rewards ranking logic for analytics views.
- Rewards Engine → Reward Rules: apply rules · Engine evaluates card reward rules against the transaction amount and merchant category.
- Rewards Engine → Card Data: load cards · Engine loads structured card definitions before ranking.
- Card Data → Reward Catalog: read catalog · Card data service is backed by the bundled reward catalog.
- Rewards Engine → Agentic Detect: classify merchant · Engine-side merchant normalization can invoke merchant classification helpers.
- Agentic Detect → Claude Agent: local call · AI-assisted merchant classification delegates to the local Python service when needed.
- Claude Agent → Category Parser: normalize category · Python service uses category parsing/normalization for merchant-to-category mapping.
- Reward Scraper → Reward Catalog: populate data · Offline scraper feeds structured reward metadata into the bundled catalog.
- Local Storage → Settings Menu: preferences · Settings UI persists user configuration through the local storage boundary.
- Settings Menu → Manage Cards: configure · Settings menu routes into card and reward preference management.
- Main Popup → Local Storage: read prefs · Popup reads user-specific settings to shape recommendation behavior.
- Background SW → Local Storage: sync state · Background worker may keep extension-local state in sync with stored preferences.
