How tridibbanik17/stock_agent works
Purpose and Entry Points
Stock Agent is a privacy-first Chrome Manifest V3 watchlist grader. The extension keeps holdings and Gemini keys locally, requests ticker-only snapshots from a FastAPI backend, and optionally subscribes users to scheduled email reports. Primary entry points are extension/manifest.json, extension/popup/popup.js, backend/app/main.py, and the scheduled dispatcher backend/worker/cron_dispatch.py.
Extension Client and Privacy Boundary
The browser extension is the user-facing runtime: popup UI manages watchlists, local portfolio values, themes, subscription settings, and refreshes. Local storage is the hard privacy boundary for share counts, buy prices, and optional Gemini key; backend calls send only tickers and email scheduling data. extension/background.js supports MV3 background behavior, while extension/lib/api.js and extension/lib/storage.js separate network and persistence concerns.
Paths: extension/popup/popup.js, extension/lib/api.js, extension/lib/storage.js.
Snapshot API and Request Controls
FastAPI exposes subscription, quote snapshot, unsubscribe, and protected internal dispatch routes. Routing is centralized separately from startup/configuration, with Pydantic schemas enforcing request/response contracts. Public endpoints are bounded by body validation and per-IP rate limiting; the internal dispatch route authenticates with a shared dispatch secret.
Paths: backend/app/main.py, backend/app/api/routes.py, backend/app/api/abuse.py, backend/app/models/schemas.py.
Market Data and Deterministic Grading
The snapshot pipeline obtains live market/fundamental data and news, then grades each ticker with explainable rules rather than AI. The grading service applies sector-aware D/E, PEG, ROE, 200-SMA, RSI, and news-risk logic to produce STRONG BUY, HOLD, or AVOID outcomes. Market-provider failures/fallback behavior belongs at the market-data boundary rather than in extension UI logic.
Paths: backend/app/services/market_data.py, backend/app/services/grading.py, backend/app/services/news.py.
Subscription Persistence and Email Delivery
Subscription requests upsert email, tickers, schedule, timezone, and delivery state in Supabase PostgreSQL. Due reports are assembled from fresh quote/grade data, sent through Resend, and recorded to prevent duplicate or excessive sends. Schema plus incremental migrations define operational state including unsubscribe tokens, last-send timestamps, delivery logs, grade-change email support, and daily caps.
Paths: backend/app/services/supabase_client.py, backend/app/services/email_report.py, backend/database_schema.sql, backend/migrations/004_add_delivery_logs.sql.
Scheduled Dispatch Control Plane
AWS EventBridge and Lambda invoke the hosted protected dispatch endpoint every five minutes; actual database access and email sending remain in the backend. A local worker provides equivalent cron-trigger behavior for development or fallback. The deployment target is Heroku using the backend Docker image/Procfile runtime.
Paths: backend/app/services/dispatch.py, backend/worker/cron_dispatch.py, backend/Dockerfile, Procfile.
External Dependencies and Delivery
Material external services are Supabase/PostgreSQL for subscriptions, yfinance for market data, Resend for email, Google Gemini directly from the browser for optional explanations, and AWS EventBridge/Lambda for triggering. GitHub Actions runs tests and automates extension version bumps; the disabled cron workflow remains a reference/fallback.
Paths: .github/workflows/test.yml, .github/workflows/bump-extension-version.yml, .github/workflows/cron-dispatch.yml.
Main entry points: routes.py API Routes · email_report.py Email Report Service · market_data.py Market Data Service · dispatch.py Dispatch Service
Components 20
Chrome Extension
Privacy-first browser runtime.
- Popup UI · Manages watchlists, settings, and refreshes.
extension/popup/popup.js - Local Storage · Retains holdings, prices, and optional Gemini key locally.
extension/lib/storage.js - Backend API Client · Sends ticker-only snapshots and subscription data.
extension/lib/api.js - MV3 Background
extension/background.js
FastAPI Backend
Hosted snapshot and subscription service.
- FastAPI App · Initializes hosted API configuration and startup.
backend/app/main.py - API Routes · Exposes snapshots, subscriptions, unsubscribe, and internal dispatch.
backend/app/api/routes.py - Request Controls · Applies public request validation and per-IP limits.
backend/app/api/abuse.py - Pydantic Schemas
backend/app/models/schemas.py - Market Data Service · Fetches live market and fundamental data with fallbacks.
backend/app/services/market_data.py - News Service
backend/app/services/news.py - Deterministic Grading · Produces explainable STRONG BUY, HOLD, or AVOID grades.
backend/app/services/grading.py
Report Delivery
Scheduled dispatch and email reporting.
- Supabase Client · Upserts subscriptions and tracks delivery state.
backend/app/services/supabase_client.py - Dispatch Service · Finds due reports and coordinates report delivery.
backend/app/services/dispatch.py - Email Report Service · Builds fresh graded reports and records sends.
backend/app/services/email_report.py - Local Cron Worker · Provides equivalent local or fallback dispatch triggering.
backend/worker/cron_dispatch.py
External Services
- Google Gemini · Receives the optional local user key directly from the browser.
- yfinance
- Supabase PostgreSQL · Stores schedules, tokens, send state, logs, caps, and grade changes.
backend/database_schema.sql - Resend
- AWS EventBridge + Lambda · Invokes protected dispatch every five minutes.
Connections 24
- Popup UI → Local Storage: reads and writes
- Popup UI → Backend API Client: requests snapshots
- Popup UI → Google Gemini: optional explanations
- MV3 Background → Local Storage: uses
- Backend API Client → API Routes: tickers and subscription data
- FastAPI App → API Routes: mounts
- API Routes → Request Controls: public request controls
- API Routes → Pydantic Schemas: validates contracts
- API Routes → Market Data Service: snapshot tickers
- Market Data Service → yfinance: fetches market data
- API Routes → News Service: retrieves news
- Market Data Service → Deterministic Grading: fundamentals and prices
- News Service → Deterministic Grading: news risk
- API Routes → Supabase Client: upserts subscriptions
- Supabase Client → Supabase PostgreSQL: persists state
- AWS EventBridge + Lambda → API Routes: protected dispatch call
- Local Cron Worker → Dispatch Service: triggers
- API Routes → Dispatch Service: authenticated internal dispatch
- Dispatch Service → Supabase Client: loads due subscriptions
- Dispatch Service → Email Report Service: creates due reports
- Email Report Service → Market Data Service: fresh quote data
- Email Report Service → Deterministic Grading: fresh grades
- Email Report Service → Resend: sends email
- Email Report Service → Supabase Client: records delivery