@rfc0/patty 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -30,7 +30,7 @@ and ask for the **`tour`** prompt on the `patty-demo` server. Details in [`docs/
30
30
 
31
31
  Before running `npx`, make sure these tools are installed:
32
32
 
33
- - **Node.js 25.x**
33
+ - **Node.js 26.x**
34
34
  - **npm 11+**
35
35
 
36
36
  Quick check:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rfc0/patty",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "AI-first personal finance manager — MCP server (local-first, encrypted libSQL).",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -30,7 +30,7 @@
30
30
  "access": "public"
31
31
  },
32
32
  "engines": {
33
- "node": ">=25 <26",
33
+ "node": ">=26 <27",
34
34
  "npm": ">=11"
35
35
  },
36
36
  "scripts": {
package/ui/app.js CHANGED
@@ -43,6 +43,17 @@
43
43
  var DEFAULT_DASHBOARD_NAME = 'overview';
44
44
  var UI_PATH_PREFIX = '/ui/';
45
45
  var EVENTS_PATH = '/ui/events';
46
+ var LOGIN_PATH = '/ui/login';
47
+ var HTTP_UNAUTHORIZED = 401;
48
+ var EVENT_SOURCE_CLOSED = 2; // EventSource.CLOSED — the browser gave up retrying
49
+ // Fixed, no backoff: each attempt costs one cheap local /api/status call.
50
+ var RECONNECT_DELAY_MS = 3000;
51
+ // Every entity tag a bundled dashboard listens for (tested against ui/dashboards/*.html); a
52
+ // resync after a gap in the event stream sends all of them so every dashboard refetches.
53
+ var ALL_ENTITY_TAGS = [
54
+ 'account', 'bill', 'budget', 'category', 'creditCard', 'currency_rate',
55
+ 'dashboard', 'recurring', 'tag', 'transaction', 'userSettings',
56
+ ];
46
57
  var currentLocale = 'en';
47
58
 
48
59
  var DASHBOARD_ICONS = {
@@ -102,6 +113,9 @@
102
113
  // attached, which would silently stop live update. Also guards connectEvents() itself against
103
114
  // ever opening a second connection.
104
115
  var eventSource = null;
116
+ // Set by onerror, consumed by the next onopen: the stream was down, so events may have been missed.
117
+ var streamErrored = false;
118
+ var reconnectTimer = null;
105
119
 
106
120
  var Api = {
107
121
  status: '/api/status',
@@ -355,17 +369,65 @@
355
369
  navigate(currentDashboardName(), false);
356
370
  });
357
371
 
358
- // ADR-027 Phase 3 — live update. Opened once, for the page's whole lifetime; fragment swaps
359
- // (navigate/renderFragment above) never touch this connection, only the
360
- // window.onAppDataChanged slot it delivers into. EventSource reconnects on its own after a
361
- // drop (the browser's built-in retry), so no reconnect logic is needed here; a message that
362
- // arrives with no fragment currently registered (or between "torn down" and "registered again")
363
- // is simply dropped — the next live-relevant fetch a user triggers (period change, manual
364
- // refresh) always reflects current data regardless.
372
+ // ADR-027 Phase 3 live update, ADR-036 §4 recovery (the as-built flow is in docs/ui.md). One
373
+ // EventSource serves the whole page: fragment swaps never touch it, only the
374
+ // window.onAppDataChanged slot it delivers into, and a message with no fragment registered is
375
+ // dropped (the next fetch reflects current data anyway). Events that pass while the stream is
376
+ // down are lost for good, hence the resync on reopen.
377
+ function forwardEntities(entities) {
378
+ var forward = function () {
379
+ if (typeof window.onAppDataChanged === 'function') window.onAppDataChanged(entities);
380
+ };
381
+ if (entities.indexOf('userSettings') === -1) return forward();
382
+ // A base-currency change updates the default before the fragment re-renders with it.
383
+ fetchJson(Api.preferences)
384
+ .then(function (prefs) {
385
+ window.setBaseCurrency(prefs && prefs.baseCurrency);
386
+ })
387
+ .catch(function (err) {
388
+ console.error('Could not refresh preferences:', err);
389
+ })
390
+ .then(forward);
391
+ }
392
+
393
+ function goToLogin() {
394
+ var here = window.location.pathname + (window.location.search || '');
395
+ window.location.href = LOGIN_PATH + '?next=' + encodeURIComponent(here);
396
+ }
397
+
398
+ function scheduleReconnect() {
399
+ if (reconnectTimer) return;
400
+ reconnectTimer = setTimeout(function () {
401
+ reconnectTimer = null;
402
+ connectEvents();
403
+ }, RECONNECT_DELAY_MS);
404
+ }
405
+
406
+ function handleStreamClosed() {
407
+ eventSource = null;
408
+ fetch(Api.status, { headers: { accept: 'application/json' } })
409
+ .then(function (res) {
410
+ if (res.status === HTTP_UNAUTHORIZED) return goToLogin();
411
+ scheduleReconnect();
412
+ })
413
+ .catch(scheduleReconnect);
414
+ }
415
+
365
416
  function connectEvents() {
366
417
  if (typeof EventSource === 'undefined' || eventSource) return;
367
- eventSource = new EventSource(EVENTS_PATH);
368
- eventSource.onmessage = function (event) {
418
+ var source = new EventSource(EVENTS_PATH);
419
+ eventSource = source;
420
+ source.onopen = function () {
421
+ if (!streamErrored) return;
422
+ streamErrored = false;
423
+ forwardEntities(ALL_ENTITY_TAGS.slice()); // a copy: fragments receive the array and may mutate it
424
+ };
425
+ source.onerror = function () {
426
+ streamErrored = true;
427
+ // A replaced source can still report late; only the current one may reconnect or check the session.
428
+ if (source.readyState === EVENT_SOURCE_CLOSED && eventSource === source) handleStreamClosed();
429
+ };
430
+ source.onmessage = function (event) {
369
431
  var payload;
370
432
  try {
371
433
  payload = JSON.parse(event.data);
@@ -373,19 +435,7 @@
373
435
  return;
374
436
  }
375
437
  if (!Array.isArray(payload.entities)) return;
376
- var forward = function () {
377
- if (typeof window.onAppDataChanged === 'function') window.onAppDataChanged(payload.entities);
378
- };
379
- if (payload.entities.indexOf('userSettings') === -1) return forward();
380
- // A base-currency change updates the default before the fragment re-renders with it.
381
- fetchJson(Api.preferences)
382
- .then(function (prefs) {
383
- window.setBaseCurrency(prefs && prefs.baseCurrency);
384
- })
385
- .catch(function (err) {
386
- console.error('Could not refresh preferences:', err);
387
- })
388
- .then(forward);
438
+ forwardEntities(payload.entities);
389
439
  };
390
440
  }
391
441
 
@@ -3178,7 +3178,7 @@ function initCustomSelects() {
3178
3178
  // ADR-027 Live-update listener
3179
3179
  window.onAppDataChanged = function(entities) {
3180
3180
  var affected = entities.some(function(tag) {
3181
- return ['account', 'transaction'].indexOf(tag) !== -1;
3181
+ return ['account', 'transaction', 'category', 'tag', 'currency_rate', 'userSettings'].indexOf(tag) !== -1;
3182
3182
  });
3183
3183
  if (affected) {
3184
3184
  console.log("accounts refetching due to entity change:", entities);
@@ -513,7 +513,7 @@
513
513
  </div>
514
514
  <div class="controls-group">
515
515
  <label for="currency-select" data-i18n="currency_label">Currency:</label>
516
- <select id="currency-select" class="select-custom" onchange="onFilterChange()">
516
+ <select id="currency-select" class="select-custom" onchange="onCurrencyChange()">
517
517
  <option value="" data-i18n="currency_none">No conversion</option>
518
518
  </select>
519
519
  </div>
@@ -782,6 +782,8 @@ var compareChart = null;
782
782
  var trendChart = null;
783
783
  var currentLocale = 'en';
784
784
  var currentDisplayCurrency = '';
785
+ // True once the person picks a currency; until then the selection follows the base currency.
786
+ var displayCurrencyChosen = false;
785
787
 
786
788
  // Sync the reference selectors to ?month&year query params (deep links) or today's date, instead
787
789
  // of always loading the HTML's static placeholder selection — mirrors pending.html's pattern.
@@ -899,6 +901,21 @@ function onFilterChange() {
899
901
  loadDashboardData();
900
902
  }
901
903
 
904
+ // Only the currency selector counts as a choice; changing the month or year leaves the default following the base.
905
+ function onCurrencyChange() {
906
+ displayCurrencyChosen = true;
907
+ onFilterChange();
908
+ }
909
+
910
+ // The shell has already refreshed window.BASE_CURRENCY when a userSettings event reaches this fragment.
911
+ function followBaseCurrency() {
912
+ const select = document.getElementById('currency-select');
913
+ if (displayCurrencyChosen || !select || !window.BASE_CURRENCY) return;
914
+ if (!Object.prototype.hasOwnProperty.call(window.__appCurrencies || {}, window.BASE_CURRENCY)) return;
915
+ select.value = window.BASE_CURRENCY;
916
+ currentDisplayCurrency = select.value;
917
+ }
918
+
902
919
  async function fetchBudgetStatus(month, year, displayCurrency) {
903
920
  try {
904
921
  const params = new URLSearchParams({ month: String(month), year: String(year) });
@@ -1705,9 +1722,10 @@ function escapeHtml(str) {
1705
1722
  // ADR-027 Live-update listener
1706
1723
  window.onAppDataChanged = function(entities) {
1707
1724
  var affected = entities.some(function(tag) {
1708
- return ['budget', 'transaction', 'category', 'recurring'].indexOf(tag) !== -1;
1725
+ return ['budget', 'transaction', 'category', 'recurring', 'userSettings', 'account', 'currency_rate'].indexOf(tag) !== -1;
1709
1726
  });
1710
1727
  if (affected) {
1728
+ if (entities.indexOf('userSettings') !== -1) followBaseCurrency();
1711
1729
  console.log("budget refetching due to entity change:", entities);
1712
1730
  loadDashboardData();
1713
1731
  }
@@ -4021,7 +4021,7 @@ function initCustomSelects() {
4021
4021
  // ADR-027 Live-update listener
4022
4022
  window.onAppDataChanged = function(entities) {
4023
4023
  var affected = entities.some(function(tag) {
4024
- return ['creditCard', 'transaction', 'account'].indexOf(tag) !== -1;
4024
+ return ['creditCard', 'transaction', 'account', 'bill', 'category', 'tag'].indexOf(tag) !== -1;
4025
4025
  });
4026
4026
  if (affected) {
4027
4027
  console.log("cards refetching due to entity change:", entities);
@@ -1651,6 +1651,7 @@
1651
1651
  if (hasInitialized) return;
1652
1652
  hasInitialized = true;
1653
1653
  const ownFragmentMarker = panelsLoading;
1654
+ comparativeMarker = ownFragmentMarker;
1654
1655
  await checkLocale();
1655
1656
  if (!ownFragmentMarker || !ownFragmentMarker.isConnected) return;
1656
1657
 
@@ -1662,6 +1663,16 @@
1662
1663
  if (hmSel) hmSel.addEventListener('change', loadTransactionsData);
1663
1664
 
1664
1665
  await initDashboard();
1666
+ comparativeBootDone = true;
1667
+ }
1668
+
1669
+ // Live update: runInit() is one-shot, so an event after boot refetches through initDashboard() directly.
1670
+ // An event while boot is still in flight is dropped (boot fetches current data), as is one for a torn-down fragment.
1671
+ var comparativeBootDone = false;
1672
+ var comparativeMarker = null;
1673
+ function refreshComparative() {
1674
+ if (!comparativeBootDone || !comparativeMarker || !comparativeMarker.isConnected) return Promise.resolve();
1675
+ return initDashboard();
1665
1676
  }
1666
1677
 
1667
1678
  // Run immediately since DOM is already parsed (script is at bottom of body)
@@ -2457,7 +2468,7 @@ window.onAppDataChanged = function(entities) {
2457
2468
  });
2458
2469
  if (affected) {
2459
2470
  console.log("comparative refetching due to entity change:", entities);
2460
- runInit();
2471
+ refreshComparative();
2461
2472
  }
2462
2473
  };
2463
2474
 
@@ -2394,7 +2394,7 @@ function handleAppDataChanged(entities) {
2394
2394
  // (ADR-026) — omitting them would leave the badge stale after a rate or home-currency update
2395
2395
  // until some unrelated entity change happened to trigger a refetch.
2396
2396
  var affected = entities.some(function(tag) {
2397
- return ['transaction', 'account', 'category', 'recurring', 'currency_rate', 'userSettings'].indexOf(tag) !== -1;
2397
+ return ['transaction', 'account', 'category', 'recurring', 'currency_rate', 'userSettings', 'creditCard', 'bill'].indexOf(tag) !== -1;
2398
2398
  });
2399
2399
  if (affected) {
2400
2400
  console.log("overview refetching due to entity change:", entities);
@@ -2962,7 +2962,7 @@ function initCustomSelects() {
2962
2962
  // ADR-027 Live-update listener
2963
2963
  window.onAppDataChanged = function(entities) {
2964
2964
  var affected = entities.some(function(tag) {
2965
- return ['transaction', 'recurring'].indexOf(tag) !== -1;
2965
+ return ['transaction', 'recurring', 'account', 'category', 'creditCard', 'tag', 'userSettings'].indexOf(tag) !== -1;
2966
2966
  });
2967
2967
  if (affected) {
2968
2968
  console.log("pending refetching due to entity change:", entities);
@@ -1704,6 +1704,7 @@
1704
1704
  if (hasInitialized) return;
1705
1705
  hasInitialized = true;
1706
1706
  const ownFragmentMarker = heatmapLoading;
1707
+ projectionsMarker = ownFragmentMarker;
1707
1708
  await checkLocale();
1708
1709
  if (!ownFragmentMarker || !ownFragmentMarker.isConnected) return;
1709
1710
 
@@ -1715,6 +1716,16 @@
1715
1716
  if (hmSel) hmSel.addEventListener('change', loadTransactionsData);
1716
1717
 
1717
1718
  await initDashboard();
1719
+ projectionsBootDone = true;
1720
+ }
1721
+
1722
+ // Live update: runInit() is one-shot, so an event after boot refetches through initDashboard() directly.
1723
+ // An event while boot is still in flight is dropped (boot fetches current data), as is one for a torn-down fragment.
1724
+ var projectionsBootDone = false;
1725
+ var projectionsMarker = null;
1726
+ function refreshProjections() {
1727
+ if (!projectionsBootDone || !projectionsMarker || !projectionsMarker.isConnected) return Promise.resolve();
1728
+ return initDashboard();
1718
1729
  }
1719
1730
 
1720
1731
  // Run immediately since DOM is already parsed (script is at bottom of body)
@@ -2506,11 +2517,11 @@ function initCustomSelects() {
2506
2517
  // ADR-027 Live-update listener
2507
2518
  window.onAppDataChanged = function(entities) {
2508
2519
  var affected = entities.some(function(tag) {
2509
- return ['transaction', 'recurring'].indexOf(tag) !== -1;
2520
+ return ['transaction', 'recurring', 'account', 'category', 'userSettings'].indexOf(tag) !== -1;
2510
2521
  });
2511
2522
  if (affected) {
2512
2523
  console.log("projections refetching due to entity change:", entities);
2513
- runInit();
2524
+ refreshProjections();
2514
2525
  }
2515
2526
  };
2516
2527