Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,27 @@ jobs:
- run: uv run ruff check .
- run: uv run ruff format --check .
- run: uv run pytest
- name: Frontend script parses
if: runner.os == 'Linux'

frontend:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: astral-sh/setup-uv@v5
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: uv sync --frozen
- run: npm ci
- name: Scripts parse
run: for f in static/js/*.js scripts/*.js scripts/*.mjs; do node --check "$f"; done
- name: Vendored files and compiled CSS are up to date
run: |
npm run build
git diff --exit-code -- static/vendor static/css
- run: npx playwright install --with-deps chromium
- name: UI smoke test (fake data)
run: |
python3 -c "import re;s=open('static/index.html',encoding='utf-8').read();open('/tmp/app.js','w').write(re.findall(r'<script>(.*?)</script>',s,re.S)[-1])"
node --check /tmp/app.js
uv run python scripts/fake_server.py &
for i in $(seq 1 30); do curl -sf http://127.0.0.1:8001/api/health && break; sleep 1; done
node scripts/ui_smoke.js
17 changes: 15 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ uv run python -m watchstock # run the app at http://127.0.0.1:8000 (opens b
uv run python -m watchstock --no-browser
uv run pytest # tests (no network needed)
uv run ruff check . && uv run ruff format --check .

# frontend tooling (developers only; outputs are committed, end users never need Node)
npm ci
npm run build # re-vendor pinned libs + recompile Tailwind CSS
uv run python scripts/fake_server.py & node scripts/ui_smoke.js # browser test, fake data
```

End users run `start.bat` (Windows). It installs `uv` if missing; `uv` fetches
Expand All @@ -37,8 +42,16 @@ the right Python and dependencies by itself. Nothing needs to be pre-installed.
- Single user, bound to `127.0.0.1`. No accounts, no auth. Don't add them back.
- SQLite via stdlib `sqlite3`, schema changes as new entries in
`db.MIGRATIONS` (never edit an applied migration).
- Frontend: plain HTML/CSS/JS, **no build step required to run**. Third-party JS is
vendored and pinned under `static/vendor/` (see HANDOFF for status).
- Frontend: plain HTML/CSS/JS, **no build step required to run**. `static/js/*.js` are
classic scripts sharing one global scope, loaded in the order listed in `index.html`;
top-level code may only call functions from earlier files (startup lives in `main.js`).
Third-party JS/fonts are vendored and pinned in `static/vendor/` via `npm run vendor`.
Tailwind is precompiled to `static/css/tailwind.css`. After adding Tailwind classes,
run `npm run build:css`; CI fails if the committed CSS is stale.
- LLM output is untrusted: render markdown only through `renderMarkdown()` (DOMPurify).
Never assign model text to `innerHTML` directly. Escape external text with `esc()`.
- Money: use `fmtMoney`/`fmtBig` with `priceCurrency()` for prices and
`statementCurrency()` for statement values. Never hardcode `$`.
- Keep the UI simple. Every new panel must earn its place for a portfolio owner.

## Workflow
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ uv sync
uv run python -m watchstock # run
uv run pytest # tests, no network needed
uv run python scripts/fake_server.py # UI with fake data on :8001
npm ci && node scripts/ui_smoke.js # browser test against the fake server
npm run build # after changing vendored libs or Tailwind classes
```

## License
Expand Down
24 changes: 23 additions & 1 deletion docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Architecture

```
Browser (static/index.html, vanilla JS)
Browser (static/index.html + static/js/*.js, plain scripts, no build step)
│ JSON over /api/*
▼
watchstock/app.py FastAPI app factory, error → 502 mapping, static files
Expand All @@ -21,6 +21,28 @@ watchstock/app.py FastAPI app factory, error → 502 mapping, static
└── openrouter.py chat completions
```

## Frontend (`static/`)

| file | role |
|---|---|
| `index.html` | markup only; loads CSS, vendored libs, then `js/*.js` in order |
| `css/tailwind.css` | precompiled Tailwind (generated from `tailwind.config.js`; `npm run build:css`) |
| `css/app.css` | fonts, theme variables, component styles, markdown (`.prose`) styles |
| `vendor/` | pinned Chart.js, marked, DOMPurify, Inter font (`npm run vendor`) |
| `js/core.js` | `api()`, `esc()`, `renderMarkdown()`, money formatting, DOM handles, toast |
| `js/charts.js` | Chart.js helpers |
| `js/scores.js` | breakdown popover + safety modal |
| `js/settings.js` | settings modal |
| `js/home.js` | indices, market news, movers, watchlist, search history |
| `js/dashboard.js` | sidebar, finance tab, metric cards, news tab |
| `js/analysis.js` | AI report rendering/requesting, JSON export |
| `js/chat.js` | chat panel and the UI actions the model may request |
| `js/search.js` | layout, autocomplete, `executeSearch`/`loadStock`, refresh, overlay |
| `js/main.js` | startup calls (runs last) |

Classic scripts share one global scope (see D11). `scripts/ui_smoke.js` drives the real
UI against `scripts/fake_server.py` in CI.

## Request flow: opening a stock

1. UI calls `GET /api/stocks/MSFT?name=...` (`refresh=true` from the REFRESH button).
Expand Down
12 changes: 12 additions & 0 deletions docs/DECISIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,15 @@ cached snapshots in the old shape are refetched instead of breaking the UI.
Windows console QuickEdit mode blocks any process writing to a console the user has
clicked into, which looks like a freeze. Logs go to `data/watchstock.log`; the console only
shows the startup banner. The browser opens after `/api/health` answers.

## D11 — Frontend: split classic scripts, vendored libs, precompiled Tailwind (2026-11-04)
The 2,400-line `index.html` is now `index.html` + `css/app.css` + ten files in `js/`.
They're classic scripts sharing one global scope, not ES modules: the markup uses inline
`onclick` handlers that need globals, and modules would mean rewriting every handler
for no user-visible gain. Converting to modules later is mechanical if it's ever needed.
Chart.js, marked, DOMPurify and the Inter font are vendored at pinned versions
(`package.json`, `scripts/vendor.mjs`): the app no longer depends on CDNs, and an
unpinned CDN update can't break it again (that's how the report links broke). Tailwind's
Play CDN (a runtime compiler, not meant for production) is replaced by a precompiled
`static/css/tailwind.css`, which CI checks is up to date. Node is a developer-only tool.
All LLM markdown goes through DOMPurify, with images forbidden.
46 changes: 30 additions & 16 deletions docs/HANDOFF.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ Read this first. Update it at the end of every batch or before your context runs
- [x] Default model `nvidia/nemotron-3.5-super-vl:free` (owner's choice for trials); LLM timeout 150 s
- [x] Snapshot `schema` version (D9): old cached snapshots are refetched, not rendered broken

### Batch 3 — Frontend cleanup
- Split `static/index.html` into `index.html` + `css/` + `js/` ES modules
- Vendor + pin Chart.js, marked, DOMPurify; sanitise LLM HTML; fix marked link renderer (broken in marked ≥13)
- Tailwind Play CDN → decide (D6)
- Show data age ("as of"), currency-aware formatting, simpler settings
- Drop the chat's HTML-injection UI action
### Batch 3 — Frontend cleanup *(branch `batch-3-frontend`, PR #3, stacked on #2)* ✅
- [x] `static/index.html` split into `css/app.css` + 10 classic scripts in `js/` (D11)
- [x] Chart.js 4.6.1, marked 18.1.3, DOMPurify 3.4.17, Inter 5.3.0 vendored + pinned; no CDNs left
except TradingView and logos
- [x] Tailwind Play CDN → precompiled `css/tailwind.css` (Tailwind 3.4.19); CI checks it's fresh
- [x] All LLM markdown sanitised (`renderMarkdown`, images forbidden); chat's raw-HTML
`update_tab_content` action removed
- [x] Currency-aware money (`fmtMoney`/`fmtBig`): prices in `currency`, FCF in `financialCurrency`,
GBp/ZAc/ILA minor units, compact $3.08T style; watchlist quotes carry currency
- [x] Plain-language wording (Analyze, New AI report, Ask about this stock, disclaimer line)
- [x] Markdown styles for AI text (headings, lists, quotes)
- [x] `scripts/ui_smoke.js` is now a pass/fail browser test (sanitising, currencies, breakdowns,
watchlist, refresh, chat, no-key hint, failure toast) and runs in CI

### Batch 4 — Portfolio
- Holdings (ticker, shares, cost basis, currency), P/L, allocation by sector/country,
Expand All @@ -62,18 +69,19 @@ Read this first. Update it at the end of every batch or before your context runs
- Industry peer comparison; thesis tracking ("why I bought" checked against new data)

## Status
Batches 1 and 2 done. PR #1 and PR #2 (stacked) green on CI and waiting for the owner to
merge. Files copied to the owner's folder. Next: batch 3 (frontend cleanup).
Batches 1-3 done. PRs #1, #2, #3 (stacked) wait for the owner to merge; files are copied
to the owner's folder after each batch. Next: batch 4 (portfolio).

Notes for the next agent:
- The chat still supports `update_tab_content` on the frontend (raw HTML). The backend
prompt no longer offers it; remove the frontend handler in batch 3 with DOMPurify.
- The Tailwind Play CDN, Chart.js and marked still load from CDNs (marked unpinned; the
link renderer uses the >=13 token API). Batch 3 vendors them into `static/vendor/`.
- For screenshots in the sandbox: run `scripts/fake_server.py`, then `scripts/ui_smoke.js`
(header explains how it serves CDN scripts from npm copies).
- Money is still formatted with `$` everywhere in the frontend; statement values (FCF,
revenue) should use `financialCurrency`, prices `currency`. Batch 3.
- Batch 4 plan: `holdings` table (ticker, shares, avg cost, currency, opened_at, notes) via a new
migration; `/api/portfolio` returning positions with live quotes, P/L, weights, sector/country
allocation (sector from the cached snapshot, else a light `fetch_profile`), concentration flags
(single position > 20%, sector > 40%), projected annual dividends. UI: a "Portfolio" view on the
home screen next to the watchlist; keep it to one table + two small allocation charts.
Multi-currency totals: show per-currency subtotals rather than inventing FX conversion; dad is
mostly USD.
- For screenshots/tests: `scripts/fake_server.py` + `node scripts/ui_smoke.js` (needs
`PLAYWRIGHT_BROWSERS_PATH` pointing at a Chromium matching playwright 1.63.0).
- The old hackathon DB (`watchstock.db` at repo root) is not migrated; the new DB is
`data/watchstock.db`.

Expand All @@ -92,6 +100,12 @@ Batch 2:
- [ ] Request a second report on the same stock → "Since the Last Report" section appears
- [ ] If anything misbehaves: send `data\watchstock.log`

Batch 3:
- [ ] Looks the same as before (fonts, layout, colours), with nothing loaded from CDNs
- [ ] AI report: headings/bullets readable, links clickable
- [ ] `TLV.RO` shows prices in RON (e.g. "RON 29.50"), MSFT in $
- [ ] Market cap shows compact (e.g. "$3.08T")

## Open questions for the owner
- (answered) Dad holds mostly US stocks → currency formatting stays in batch 3, no rush.
- (answered) Default model: `nvidia/nemotron-3.5-super-vl:free` for now.
Loading
Loading