frontend module conversion)
ui-state was the smallest, most isolated module: 35 lines, zero legacy-
global dependencies, single concern (localStorage helpers under the
'ped_ui/' namespace). Ideal first migration to validate the strangler-
fig pattern end-to-end.
Changes:
- New: client/ui-state.ts — exports get/set/del + UIState bundle.
Keeps (window as any).UIState = UIState assignment so the 10+ legacy
consumers (sub-nav.js, peGuide.js, app.js, etc.) keep working without
modification. As each consumer is migrated, it will replace the
window.UIState read with an explicit import.
- client/main.ts: import './ui-state' so it gets bundled into main.js.
- scripts/build-client.mjs: explicit ENTRIES list (just 'main.ts' for
now) — was scanning every top-level .ts as an entry, which produced
duplicate ui-state.js bundle.
- public/index.html: <script defer src="/js/ui-state.js"> removed; the
TS bundle (/dist/main.js) now provides window.UIState.
- public/js/ui-state.js: deleted (now lives in client/).
Verification:
- npm run typecheck:client → 0 errors
- npm run build:client → 349-byte main.js bundle (gzipped will be ~200B)
- 46/46 unit tests pass
- The 10 cross-file consumers of UIState.get/set/del still work because
the window assignment lives in the bundle.
Pattern documented for next migrations: each new client/X.ts is
imported as a side-effect from main.ts. The window-bridge stays until
every consumer also moves to ES imports.
Strangler-fig migration setup for the frontend. Legacy public/js/*.js
keeps working unchanged; new TypeScript code lives under client/ and
gets bundled into public/dist/ at image-build time.
Adds:
- esbuild@0.28 as a regular dep (so docker build can run the bundler
without --include-dev). It's small; tsx already pulled in a pinned
version transitively.
- scripts/build-client.mjs: bundles every top-level client/*.ts file
into public/dist/<name>.js as ESM with sourcemap. --watch mode for
dev. Sub-directories under client/ are bundled by their parent
entry, not separately.
- tsconfig.client.json: browser target (lib: DOM), bundler module
resolution, isolatedModules:true so esbuild + tsc agree.
- Two npm scripts: build:client, typecheck:client.
- Dockerfile: 'RUN node scripts/build-client.mjs' between COPY and CMD,
so the prod image ships with the built bundle.
- .gitignore: public/dist/ — build output, never committed.
- public/index.html: <script type="module" src="/dist/main.js">
appended after legacy script tags.
- client/main.ts: minimal entry point with a console.log marker
(window.__clientBundle = true) so we can verify the bundle loaded.
Sacred files (per project memory) kept off the migration path:
- public/js/encounters.js (save/idempotency)
- voiceDictation.js, sickVisit.js recording paths (STT plumbing)
These need explicit per-file approval from Daniel before migration.
Verification:
- npm run typecheck → 0 errors (backend)
- npm run typecheck:client → 0 errors (frontend)
- npm run build:client → public/dist/main.js + .map in 3 ms
- 46/46 unit tests pass
Next session: migrate the first real frontend module (small + isolated,
likely extensions.js or memories.js) into client/ as ES module + TS.
Four changes batched:
1. ED Encounters tab (new) — multi-stage emergency note with don't-miss
tooltips and 2023 E/M MDM finalize. New route /api/ed-encounters
(generate per-stage + finalize MDM), new ed-encounters.js owning all
client logic, new ed-encounter.html component, new template_ed memory
category. Persists draft to localStorage every keystroke and to
saved_encounters on stage advance. encounters.js touched only to
register the new tab in sessionStorage restore + tabMap (save and
idempotency code untouched).
2. Notes model selector — /notes/from-voice now accepts a client-supplied
model (validated by the existing callAI allow-list); falls back to the
admin default. Added <select class="tab-model-select"> to notes.html
so the existing app.js populator handles options + default.
3. Remove AI-learning-from-corrections — deleted correctionTracker.js,
POST /memories/correction, the corrections branch in
/memories/context, the settings UI section, the FAQ entry, and all
dead trackAIOutput/saveCorrection guards in callers. Legacy
correction_* DB rows are filtered (NOT LIKE) rather than dropped, so
no destructive migration.
4. Fix notes AI framing — /notes/from-voice prompt no longer assumes
"physician dictation". Plain notes (shopping lists, reminders,
ideas) now match the dictation tone instead of being forced into
clinical structure.
All 46 tests pass.
You were right that Playwright has been catching the easy bugs while
the high-signal bugs (lightbox stranded after the Bedside reorg, PVC
clinically wrong, prod not rebuilt) all had to be caught by you as
the user. Adding a static linter so the class of bug that produced
the lightbox regression fails CI next time instead of the app.
scripts/lint-references.js walks public/js and validates every
getElementById('X') and querySelector('#X') resolves to an id that
is defined SOMEWHERE in the repo — either a static HTML attribute,
a .id = 'X' assignment, or an id="X" substring inside a JS template
string. It also walks HTML for asset references (data-img-src,
<img src>, <audio src>, <link href>) and verifies each root-absolute
path maps to a real file on disk.
Running it on the current tree surfaced two real problems:
1. shadess.js:917 reached for #wv-note-transcript when collecting
refine source context. The actual id is #wv-transcript (no -note-
infix — the transcript element is shared across well-visit sub-
panels). The bug meant a generated well-visit note's Refine call
silently missed the original transcript as source context; the AI
still "worked" but with less signal and no error. Fixed.
2. public/js/adminMilestones.js (221 lines) referenced 17 ids that
were removed a year+ ago in commit 3173ce6 ("Remove milestone
admin UI, add CMS content refresh button"). That commit dropped
the HTML but forgot the JS, which has been dead-loaded on every
page view since. All its addEventListener calls are guarded with
optional chaining, so nothing errored — just silent cruft.
Deleted the file and dropped the <script defer> tag from
index.html.
scripts/e2e.sh runs the linter as a preflight before the Playwright
container starts; a broken reference now fails the suite before any
test even boots.
Allowlist is kept small and prefix-based for the handful of id families
that are built dynamically by JS (bedside em-* sections, BP chart
elements, etc.). When a new component is added, its ids get picked up
automatically by the repo-wide collect() pass; the allowlist rarely
needs to grow.
Suite: 294 passed / 0 failed.
Safety net for upcoming refactors:
- 26 Playwright smoke tests via @playwright/test 1.50.0 in an official
Playwright container (no host Node needed). Covers every Bedside sub-pill,
the age→weight estimator, dose calculators (seizure/sepsis/anaphylaxis/
burns/airway), and interactive widgets (lightbox, vent SVG).
- `npm run e2e` wrapper runs tests inside mcr.microsoft.com/playwright:v1.50.0-noble
on the ped-ai_default Docker network so no host port mapping is needed.
- public/e2e-harness.html + public/js/e2e-bootstrap.js load the calculators
component without the SPA auth wall (scripts external to satisfy CSP).
Server:
- server.js now re-reads public/index.html on mtime change instead of
caching at boot. Fixes the "edit HTML, restart container" friction.
- CSP upgradeInsecureRequests disabled in helmet config; Caddy still
enforces HTTPS at the reverse-proxy layer in production.
Adds `npm run maint:check` (health report) and `npm run maint:reindex`
(REINDEX DATABASE + REFRESH COLLATION VERSION + ANALYZE) for post-
upgrade maintenance, modelled after Nextcloud's occ maintenance.
Documented in README.
Also relaxes postgres image from digest pin back to tag-pin
(pgvector/pgvector:pg16) — the auto-REINDEX-on-drift check in
database.js and the COLLATE "C" protection on critical indexes
make the digest pin redundant while blocking ordinary `compose
pull` updates.
FINAL WORKING SOLUTION:
Previous attempts failed because:
- transformers.js v2.17.2 is ES module-only
- Module workers require complex CSP and external imports
- importScripts() doesn't work with ES modules
Solution:
- Use transformers.js v2.6.2 (has worker-compatible UMD build)
- Bundle library + models, serve entirely from our server
- Classic worker with importScripts() - no CSP issues
What's self-hosted:
- ✅ transformers.min.js (760KB) - at /models/transformers.min.js
- ✅ Whisper models (42MB) - at /models/Xenova/whisper-tiny.en/
Worker loads:
1. importScripts('/models/transformers.min.js') - OUR SERVER
2. Loads models from /models/ - OUR SERVER
3. ZERO external network calls
4. Works in any network (firewalled, air-gapped, etc.)
This is the production-ready, truly offline solution.
Features:
- Admin can add, edit, and delete developmental milestones via dashboard
- Milestones stored in PostgreSQL (developmental_milestones table)
- Client-side loads milestones from API instead of static file
- Import script to migrate existing static data to database
- Organized by age group and domain
- Supports sorting and filtering
Admin UI:
- New section in Admin panel for milestone management
- Filter by age group
- Add/Edit modal with validation
- Delete with confirmation
- Auto-complete for age groups and domains
API Endpoints:
- GET /api/milestones-data - Public endpoint for authenticated users
- GET /api/admin/milestones - List all milestones (admin only)
- GET /api/admin/milestones/meta - Get age groups and domains
- POST /api/admin/milestones - Create milestone
- PUT /api/admin/milestones/:id - Update milestone
- DELETE /api/admin/milestones/:id - Delete milestone
- POST /api/admin/milestones/bulk-import - Bulk import
Usage:
1. Run import script: node scripts/import-milestones.js
2. Access Admin dashboard → Developmental Milestones section
3. Add/Edit/Delete milestones as needed
BREAKING FIX: Browser Whisper now fully self-contained
Previous issue:
- Loaded transformers.js from cdn.jsdelivr.net
- Downloaded models from cdn-lfs.huggingface.co
- Failed in corporate/clinical networks with firewall
- Stuck at "Initializing..." with no progress
Solution:
- Bundle transformers.js library (~876KB)
- Bundle Whisper tiny.en model (~42MB)
- Serve everything from local server
- Works in ANY network environment
Changes:
- whisperWorker.js: Load transformers from /models/ instead of CDN
- Dockerfile: Download models during Docker build
- Add download script for local dev
- Add comprehensive setup documentation
Docker image size: +~42MB (one-time cost, runtime benefit)
Tested: Works on unrestricted and firewalled networks