@harmonia-core/ui 1.2.5 → 1.2.7

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.
@@ -1,47 +1,8 @@
1
1
  "use client";
2
+ import { __spreadValues, __spreadProps, __async } from '../chunk-E3LOUS7X.mjs';
2
3
  import { createContext, useState, useRef, useEffect, useMemo, useCallback, useContext } from 'react';
3
4
  import { jsx } from 'react/jsx-runtime';
4
5
 
5
- var __defProp = Object.defineProperty;
6
- var __defProps = Object.defineProperties;
7
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
8
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
- var __hasOwnProp = Object.prototype.hasOwnProperty;
10
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
11
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
- var __spreadValues = (a, b) => {
13
- for (var prop in b || (b = {}))
14
- if (__hasOwnProp.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- if (__getOwnPropSymbols)
17
- for (var prop of __getOwnPropSymbols(b)) {
18
- if (__propIsEnum.call(b, prop))
19
- __defNormalProp(a, prop, b[prop]);
20
- }
21
- return a;
22
- };
23
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
24
- var __async = (__this, __arguments, generator) => {
25
- return new Promise((resolve, reject) => {
26
- var fulfilled = (value) => {
27
- try {
28
- step(generator.next(value));
29
- } catch (e) {
30
- reject(e);
31
- }
32
- };
33
- var rejected = (value) => {
34
- try {
35
- step(generator.throw(value));
36
- } catch (e) {
37
- reject(e);
38
- }
39
- };
40
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
41
- step((generator = generator.apply(__this, __arguments)).next());
42
- });
43
- };
44
-
45
6
  // lib/capacity/validation.ts
46
7
  function detectConflicts(field) {
47
8
  var _a;
@@ -394,474 +355,6 @@ function getModeBadgeColor(label) {
394
355
  return "oklch(0.5 0 0)";
395
356
  }
396
357
  }
397
-
398
- // lib/capacity/signals/detectors/time-detector.ts
399
- var TimeDetector = class {
400
- constructor() {
401
- this.name = "TimeDetector";
402
- this.weight = 0.6;
403
- }
404
- // Medium weight — time is significant but a broad generalisation
405
- /**
406
- * Detects and returns SignalReadings based on the current time and day.
407
- * Returns two readings: cognitive (hour-of-day) and temporal (weekday/weekend).
408
- */
409
- detect() {
410
- const now = /* @__PURE__ */ new Date();
411
- const hour = now.getHours();
412
- const dayOfWeek = now.getDay();
413
- const ts = now.getTime();
414
- let cognitiveValue;
415
- if (hour >= 9 && hour < 12) {
416
- cognitiveValue = 0.8;
417
- } else if (hour >= 14 && hour < 17) {
418
- cognitiveValue = 0.6;
419
- } else if (hour >= 17 && hour < 20) {
420
- cognitiveValue = 0.5;
421
- } else if (hour >= 20 || hour < 6) {
422
- cognitiveValue = 0.3;
423
- } else {
424
- cognitiveValue = 0.7;
425
- }
426
- const temporalValue = dayOfWeek >= 1 && dayOfWeek <= 5 ? 0.7 : 0.9;
427
- return [
428
- {
429
- dimension: "cognitive",
430
- value: cognitiveValue,
431
- confidence: 0.7,
432
- // Medium — population average, not personalised
433
- timestamp: ts,
434
- detectorName: this.name
435
- },
436
- {
437
- dimension: "temporal",
438
- value: temporalValue,
439
- confidence: 0.6,
440
- // Slightly lower — weekday/weekend is a coarser signal
441
- timestamp: ts,
442
- detectorName: this.name
443
- }
444
- ];
445
- }
446
- };
447
-
448
- // lib/capacity/signals/detectors/session-detector.ts
449
- var SessionDetector = class {
450
- constructor() {
451
- this.name = "SessionDetector";
452
- this.weight = 0.7;
453
- this.sessionStartTime = Date.now();
454
- }
455
- /**
456
- * Detects and returns a SignalReading based on the current session duration.
457
- * It provides insights into the temporal dimension.
458
- *
459
- * @returns {SignalReading} A reading indicating the inferred capacity.
460
- */
461
- detect() {
462
- const now = Date.now();
463
- const sessionDurationMinutes = (now - this.sessionStartTime) / (1e3 * 60);
464
- let temporalValue;
465
- let confidence;
466
- if (sessionDurationMinutes < 15) {
467
- temporalValue = 0.9;
468
- confidence = 0.8;
469
- } else if (sessionDurationMinutes < 60) {
470
- temporalValue = 0.7;
471
- confidence = 0.7;
472
- } else if (sessionDurationMinutes < 180) {
473
- temporalValue = 0.5;
474
- confidence = 0.6;
475
- } else {
476
- temporalValue = 0.3;
477
- confidence = 0.7;
478
- }
479
- return [{
480
- dimension: "temporal",
481
- value: temporalValue,
482
- confidence,
483
- timestamp: now,
484
- detectorName: this.name
485
- }];
486
- }
487
- };
488
-
489
- // lib/capacity/signals/detectors/scroll-detector.ts
490
- var DEBOUNCE_TIME_MS = 100;
491
- var ScrollDetector = class {
492
- constructor() {
493
- this.name = "ScrollDetector";
494
- this.weight = 0.5;
495
- // Moderate weight, as scroll velocity can indicate engagement or frustration
496
- this.lastScrollY = 0;
497
- this.lastScrollTime = 0;
498
- this.scrollVelocity = 0;
499
- this.timeoutId = null;
500
- /**
501
- * Handles the scroll event, debouncing it and calculating scroll velocity.
502
- * @private
503
- */
504
- this.handleScroll = () => {
505
- if (this.timeoutId) {
506
- clearTimeout(this.timeoutId);
507
- }
508
- this.timeoutId = setTimeout(() => {
509
- const now = Date.now();
510
- const scrollY = window.scrollY;
511
- const distance = Math.abs(scrollY - this.lastScrollY);
512
- const timeElapsed = now - this.lastScrollTime;
513
- if (timeElapsed > 0) {
514
- this.scrollVelocity = distance / timeElapsed * 1e3;
515
- } else {
516
- this.scrollVelocity = 0;
517
- }
518
- this.lastScrollY = scrollY;
519
- this.lastScrollTime = now;
520
- }, DEBOUNCE_TIME_MS);
521
- };
522
- if (typeof window !== "undefined") {
523
- window.addEventListener("scroll", this.handleScroll, { passive: true });
524
- }
525
- }
526
- /**
527
- * Detects and returns a SignalReading based on the current scroll velocity.
528
- * It provides insights into the cognitive dimension.
529
- *
530
- * @returns {SignalReading} A reading indicating the inferred capacity.
531
- */
532
- detect() {
533
- const now = Date.now();
534
- let cognitiveValue;
535
- let confidence;
536
- if (this.scrollVelocity > 1500) {
537
- cognitiveValue = 0.4;
538
- confidence = 0.6;
539
- } else if (this.scrollVelocity > 500) {
540
- cognitiveValue = 0.7;
541
- confidence = 0.8;
542
- } else if (this.scrollVelocity > 50) {
543
- cognitiveValue = 0.6;
544
- confidence = 0.7;
545
- } else {
546
- cognitiveValue = 0.5;
547
- confidence = 0.5;
548
- }
549
- return [{
550
- dimension: "cognitive",
551
- value: cognitiveValue,
552
- confidence,
553
- timestamp: now,
554
- detectorName: this.name
555
- }];
556
- }
557
- /**
558
- * Cleans up the scroll event listener when the detector is no longer needed.
559
- */
560
- destroy() {
561
- if (typeof window !== "undefined") {
562
- window.removeEventListener("scroll", this.handleScroll);
563
- if (this.timeoutId) {
564
- clearTimeout(this.timeoutId);
565
- }
566
- }
567
- }
568
- };
569
-
570
- // lib/capacity/signals/detectors/interaction-detector.ts
571
- var IDLE_THRESHOLD_MS = 15e3;
572
- var CLICK_WINDOW_MS = 6e4;
573
- var InteractionDetector = class {
574
- constructor() {
575
- this.name = "InteractionDetector";
576
- this.weight = 0.7;
577
- this.lastMouseMoveTime = 0;
578
- this.lastClickTime = 0;
579
- this.lastClickPosition = null;
580
- this.clickHistory = [];
581
- // rolling 60-second window
582
- this.idleTimer = null;
583
- this.isIdle = false;
584
- this.resetIdleTimer = () => {
585
- if (this.idleTimer) clearTimeout(this.idleTimer);
586
- this.isIdle = false;
587
- this.idleTimer = setTimeout(() => {
588
- this.isIdle = true;
589
- }, IDLE_THRESHOLD_MS);
590
- };
591
- this.handleMouseMove = () => {
592
- this.lastMouseMoveTime = Date.now();
593
- this.resetIdleTimer();
594
- };
595
- this.handleClick = (event) => {
596
- this.resetIdleTimer();
597
- const now = Date.now();
598
- this.lastClickTime = now;
599
- let distance = 0;
600
- if (this.lastClickPosition) {
601
- const dx = event.clientX - this.lastClickPosition.x;
602
- const dy = event.clientY - this.lastClickPosition.y;
603
- distance = Math.sqrt(dx * dx + dy * dy);
604
- }
605
- this.lastClickPosition = { x: event.clientX, y: event.clientY };
606
- this.clickHistory.push({ time: now, distance });
607
- };
608
- if (typeof window !== "undefined") {
609
- window.addEventListener("mousemove", this.handleMouseMove, { passive: true });
610
- window.addEventListener("click", this.handleClick, { passive: true });
611
- }
612
- this.resetIdleTimer();
613
- }
614
- detect() {
615
- const now = Date.now();
616
- const cutoff = now - CLICK_WINDOW_MS;
617
- this.clickHistory = this.clickHistory.filter((c) => c.time >= cutoff);
618
- const clickCount = this.clickHistory.length;
619
- const avgClickDistance = clickCount > 0 ? this.clickHistory.reduce((sum, c) => sum + c.distance, 0) / clickCount : 0;
620
- const timeSinceLastClick = now - this.lastClickTime;
621
- let cognitiveValue;
622
- let confidence;
623
- if (this.isIdle) {
624
- cognitiveValue = 0.4;
625
- confidence = 0.6;
626
- } else if (timeSinceLastClick < 500 && clickCount > 5 && avgClickDistance < 20) {
627
- cognitiveValue = 0.9;
628
- confidence = 0.9;
629
- } else if (timeSinceLastClick < 1500 && clickCount > 1 && avgClickDistance < 50) {
630
- cognitiveValue = 0.7;
631
- confidence = 0.7;
632
- } else if (avgClickDistance > 100) {
633
- cognitiveValue = 0.3;
634
- confidence = 0.6;
635
- } else {
636
- cognitiveValue = 0.5;
637
- confidence = 0.5;
638
- }
639
- return [{
640
- dimension: "cognitive",
641
- value: cognitiveValue,
642
- confidence,
643
- timestamp: now,
644
- detectorName: this.name
645
- }];
646
- }
647
- destroy() {
648
- if (typeof window !== "undefined") {
649
- window.removeEventListener("mousemove", this.handleMouseMove);
650
- window.removeEventListener("click", this.handleClick);
651
- }
652
- if (this.idleTimer) clearTimeout(this.idleTimer);
653
- }
654
- };
655
-
656
- // lib/capacity/signals/detectors/input-detector.ts
657
- var TYPING_SPEED_SAMPLE_SIZE = 10;
658
- var ERROR_CHECK_WINDOW = 5e3;
659
- var InputDetector = class {
660
- // timestamps of recent Backspace/Delete presses
661
- constructor() {
662
- this.name = "InputDetector";
663
- this.weight = 0.6;
664
- this.keyPressTimes = [];
665
- this.errorTimes = [];
666
- this.handleKeyDown = (event) => {
667
- const now = Date.now();
668
- this.keyPressTimes.push(now);
669
- if (this.keyPressTimes.length > TYPING_SPEED_SAMPLE_SIZE) {
670
- this.keyPressTimes.shift();
671
- }
672
- if (event.key === "Backspace" || event.key === "Delete") {
673
- this.errorTimes.push(now);
674
- }
675
- };
676
- if (typeof window !== "undefined") {
677
- window.addEventListener("keydown", this.handleKeyDown, { passive: true });
678
- }
679
- }
680
- detect() {
681
- const now = Date.now();
682
- let typingSpeedCPM = 0;
683
- if (this.keyPressTimes.length > 1) {
684
- const elapsed = this.keyPressTimes[this.keyPressTimes.length - 1] - this.keyPressTimes[0];
685
- if (elapsed > 0) {
686
- typingSpeedCPM = this.keyPressTimes.length / elapsed * 6e4;
687
- }
688
- }
689
- const cutoff = now - ERROR_CHECK_WINDOW;
690
- this.errorTimes = this.errorTimes.filter((t) => t >= cutoff);
691
- const recentErrorCount = this.errorTimes.length;
692
- let cognitiveValue;
693
- let confidence;
694
- if (typingSpeedCPM > 100 && recentErrorCount === 0) {
695
- cognitiveValue = 0.9;
696
- confidence = 0.8;
697
- } else if (typingSpeedCPM > 40 && recentErrorCount <= 1) {
698
- cognitiveValue = 0.7;
699
- confidence = 0.7;
700
- } else if (recentErrorCount > 2 || typingSpeedCPM < 20) {
701
- cognitiveValue = 0.4;
702
- confidence = 0.6;
703
- } else {
704
- cognitiveValue = 0.6;
705
- confidence = 0.5;
706
- }
707
- return [{
708
- dimension: "cognitive",
709
- value: cognitiveValue,
710
- confidence,
711
- timestamp: now,
712
- detectorName: this.name
713
- }];
714
- }
715
- destroy() {
716
- if (typeof window !== "undefined") {
717
- window.removeEventListener("keydown", this.handleKeyDown);
718
- }
719
- }
720
- };
721
-
722
- // lib/capacity/signals/detectors/environment-detector.ts
723
- var EnvironmentDetector = class {
724
- constructor() {
725
- this.name = "EnvironmentDetector";
726
- this.weight = 0.8;
727
- // High weight — these are explicit user preferences
728
- this.mqlReducedMotion = null;
729
- this.mqlDarkMode = null;
730
- this.handleChange = () => {
731
- };
732
- if (typeof window !== "undefined") {
733
- this.mqlReducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
734
- this.mqlDarkMode = window.matchMedia("(prefers-color-scheme: dark)");
735
- this.mqlReducedMotion.addEventListener("change", this.handleChange);
736
- this.mqlDarkMode.addEventListener("change", this.handleChange);
737
- }
738
- }
739
- /**
740
- * Returns two readings:
741
- * - temporal: based on prefers-reduced-motion (low → less time pressure on animations)
742
- * - emotional: based on prefers-color-scheme (dark → slightly lower emotional load)
743
- */
744
- detect() {
745
- const now = Date.now();
746
- const prefersReducedMotion = this.mqlReducedMotion != null ? this.mqlReducedMotion.matches : typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
747
- const prefersDarkMode = this.mqlDarkMode != null ? this.mqlDarkMode.matches : typeof window !== "undefined" && window.matchMedia("(prefers-color-scheme: dark)").matches;
748
- return [
749
- {
750
- dimension: "temporal",
751
- // prefers-reduced-motion → user may have lower tolerance for demanding UIs
752
- value: prefersReducedMotion ? 0.3 : 0.8,
753
- confidence: 0.9,
754
- timestamp: now,
755
- detectorName: this.name
756
- },
757
- {
758
- dimension: "emotional",
759
- // Dark mode preference → slightly lower emotional capacity or reduced-stimulation preference
760
- value: prefersDarkMode ? 0.6 : 0.7,
761
- confidence: 0.9,
762
- timestamp: now,
763
- detectorName: this.name
764
- }
765
- ];
766
- }
767
- /**
768
- * Removes the event listeners registered in the constructor.
769
- * Uses the stored refs so the same function reference is unregistered.
770
- */
771
- destroy() {
772
- var _a, _b;
773
- (_a = this.mqlReducedMotion) == null ? void 0 : _a.removeEventListener("change", this.handleChange);
774
- (_b = this.mqlDarkMode) == null ? void 0 : _b.removeEventListener("change", this.handleChange);
775
- this.mqlReducedMotion = null;
776
- this.mqlDarkMode = null;
777
- }
778
- };
779
-
780
- // lib/capacity/signals/aggregator.ts
781
- var _SignalAggregator = class _SignalAggregator {
782
- constructor() {
783
- this.detectors = [
784
- new TimeDetector(),
785
- new SessionDetector(),
786
- new ScrollDetector(),
787
- new InteractionDetector(),
788
- new InputDetector(),
789
- new EnvironmentDetector()
790
- ];
791
- }
792
- /**
793
- * Collects signal readings from all detectors and aggregates them into a
794
- * confidence-weighted CapacityField.
795
- */
796
- aggregateSignals() {
797
- return __async(this, null, function* () {
798
- const readings = [];
799
- for (const detector of this.detectors) {
800
- const detectorReadings = yield detector.detect();
801
- readings.push(...detectorReadings);
802
- }
803
- const weightedSums = {
804
- cognitive: 0,
805
- temporal: 0,
806
- emotional: 0,
807
- valence: 0
808
- };
809
- const totalWeights = {
810
- cognitive: 0,
811
- temporal: 0,
812
- emotional: 0,
813
- valence: 0
814
- };
815
- for (const reading of readings) {
816
- const effectiveWeight = reading.confidence * this.getDetectorWeight(reading.dimension, reading.detectorName);
817
- weightedSums[reading.dimension] += reading.value * effectiveWeight;
818
- totalWeights[reading.dimension] += effectiveWeight;
819
- }
820
- return {
821
- cognitive: totalWeights.cognitive > 0 ? weightedSums.cognitive / totalWeights.cognitive : 0.5,
822
- temporal: totalWeights.temporal > 0 ? weightedSums.temporal / totalWeights.temporal : 0.5,
823
- emotional: totalWeights.emotional > 0 ? weightedSums.emotional / totalWeights.emotional : 0.5,
824
- valence: totalWeights.valence > 0 ? weightedSums.valence / totalWeights.valence : 0
825
- };
826
- });
827
- }
828
- /**
829
- * Returns the effective weight for a detector/dimension pair.
830
- * Checks DIMENSION_WEIGHTS first; falls back to detector.weight.
831
- */
832
- getDetectorWeight(dimension, detectorName) {
833
- var _a, _b, _c;
834
- const override = (_a = _SignalAggregator.DIMENSION_WEIGHTS[detectorName]) == null ? void 0 : _a[dimension];
835
- if (override !== void 0) return override;
836
- return (_c = (_b = this.detectors.find((d) => d.name === detectorName)) == null ? void 0 : _b.weight) != null ? _c : 0;
837
- }
838
- /** Cleans up all detector resources (event listeners, timers). */
839
- destroy() {
840
- var _a;
841
- for (const detector of this.detectors) {
842
- (_a = detector.destroy) == null ? void 0 : _a.call(detector);
843
- }
844
- }
845
- };
846
- /**
847
- * Per-detector, per-dimension weight overrides.
848
- * Falls back to detector.weight for any unlisted combination.
849
- *
850
- * Rationale for asymmetries:
851
- * - TimeDetector: cognitive signal is stronger (diurnal pattern) than temporal
852
- * (weekday/weekend is coarser)
853
- * - EnvironmentDetector: emotional signal (color scheme) is a stronger explicit
854
- * preference than temporal (reduced-motion)
855
- */
856
- _SignalAggregator.DIMENSION_WEIGHTS = {
857
- TimeDetector: { cognitive: 0.6, temporal: 0.5 },
858
- EnvironmentDetector: { emotional: 0.8, temporal: 0.7 },
859
- InteractionDetector: { cognitive: 0.7 },
860
- InputDetector: { cognitive: 0.6 },
861
- SessionDetector: { temporal: 0.7 },
862
- ScrollDetector: { cognitive: 0.5 }
863
- };
864
- var SignalAggregator = _SignalAggregator;
865
358
  var CapacityContext = createContext(null);
866
359
  var AUTO_EMA_ALPHA = 0.2;
867
360
  function applyEMA(prev, next, alpha) {
@@ -881,7 +374,9 @@ function CapacityProvider({ children }) {
881
374
  const smoothedFieldRef = useRef(null);
882
375
  const aggregatorRef = useRef(null);
883
376
  useEffect(() => {
884
- aggregatorRef.current = new SignalAggregator();
377
+ import('../aggregator-TH7FATMR.mjs').then(({ SignalAggregator }) => {
378
+ aggregatorRef.current = new SignalAggregator();
379
+ });
885
380
  const unsubscribe = FieldManager.subscribe((newContext) => {
886
381
  setContext(newContext);
887
382
  });