All files / lib/awareness/collective/components AISummaryPanel.tsx

96.52% Statements 111/115
85.71% Branches 18/21
100% Functions 1/1
96.52% Lines 111/115

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 1601x   1x 1x 1x             1x 1x 1x   1x   1x 1x     1x 4x 3x   4x 4x   4x 3x 3x 3x 3x 3x 3x 4x   4x 3x       3x 3x 4x   4x 3x 3x 3x 3x 285x 285x 244x 244x 244x 3x 3x 4x   4x 4x 3x 1x 4x 4x 4x 4x   4x 3x 1x   4x 4x 4x 4x 4x 4x 4x 4x 4x   4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x   4x 4x   4x 4x   4x       4x 4x 4x 4x 4x 4x           4x 4x 4x 4x 4x 4x 4x           4x 4x 4x 4x 4x 4x 4x 4x       4x 4x 4x   4x 4x 4x 4x 4x 4x 4x             4x  
"use client";
 
import React, { useEffect, useMemo, useState } from "react";
import { motion } from "framer-motion";
import {
  getEvolutionLogs,
  startAutoSnapshot,
  stopAutoSnapshot,
  EvolutionLog,
} from "@/lib/evolution/history.collector";
import type { Insight } from "../analysis/insight";
import { generateAISummary } from "@/lib/awareness/collective/analysis/ai-summary";
import { HUDContainer } from "./hud/glass-effects";
import { DepthReactiveGlass } from "@/lib/awareness/collective/components/hud/hud.depth-sync";
 
/**
 * AISummaryPanel — Phase 007.2 (Depth Sync + Ambient Glow)
 */
export const AISummaryPanel: React.FC<{
  onClose: () => void;
  initialInsight?: Insight | null;
}> = ({ onClose, initialInsight }) => {
  const [logs, setLogs] = useState<EvolutionLog[]>(() =>
    getEvolutionLogs().slice(-15)
  );
  const [display, setDisplay] = useState("");
  const [phase, setPhase] = useState<"typing" | "done">("typing");
 
  useEffect(() => {
    startAutoSnapshot({ intervalSec: 60, confidenceDelta: 0.05 });
    const t = setInterval(() => setLogs(getEvolutionLogs().slice(-15)), 20000);
    return () => {
      stopAutoSnapshot();
      clearInterval(t);
    };
  }, []);
 
  const summary = useMemo(() => {
    const intro = initialInsight
      ? `🧩 Insight: ${initialInsight.point.trend}\nConfidence: ${(initialInsight.point.confidence * 100).toFixed(
          1
        )}%\n───────────────────────────────\n`
      : "";
    return intro + generateAISummary();
  }, [initialInsight, logs]);
 
  useEffect(() => {
    setDisplay("");
    setPhase("typing");
    let i = 0;
    const timer = setInterval(() => {
      setDisplay(summary.slice(0, i++));
      if (i >= summary.length) {
        clearInterval(timer);
        setPhase("done");
      }
    }, 14);
    return () => clearInterval(timer);
  }, [summary]);
 
  const confidences = logs.map((l) => l.confidence);
  const avg = confidences.length
    ? confidences.reduce((s, c) => s + c, 0) / confidences.length
    : 0;
  const status =
    avg >= 0.7 ? "Stable" : avg >= 0.4 ? "Fluctuating" : "Critical";
  const statusColor =
    status === "Stable"
      ? "#22c55e"
      : status === "Fluctuating"
      ? "#facc15"
      : "#ef4444";
 
  const max = Math.max(...confidences, 1);
  const min = Math.min(...confidences, 0);
  const points = confidences
    .map((c, i) => {
      const x = (i / Math.max(1, confidences.length - 1)) * 100;
      const y = 100 - ((c - min) / Math.max(0.001, max - min)) * 100;
      return `${x},${y}`;
    })
    .join(" ");
 
  return (
    <DepthReactiveGlass strength={12}>
      <HUDContainer status={status}>
        <div className="flex justify-between items-center mb-6">
          <div className="flex items-center gap-3">
            <motion.div
              className="w-3 h-3 rounded-full"
              style={{ background: statusColor }}
              animate={{
                boxShadow: [
                  `0 0 0px ${statusColor}`,
                  `0 0 12px ${statusColor}`,
                  `0 0 0px ${statusColor}`,
                ],
              }}
              transition={{ duration: 2, repeat: Infinity }}
            />
            <span className="text-xs font-semibold" style={{ color: statusColor }}>
              {status}
            </span>
            <h2 className="text-[22px] font-bold text-cyan-300 tracking-wide drop-shadow-sm">
              Neural Summary Generator
            </h2>
          </div>
          <motion.button
            onClick={onClose}
            whileHover={{ scale: 1.08 }}
            whileTap={{ scale: 0.9 }}
            className="px-4 py-1.5 text-sm border border-cyan-400/40 text-cyan-200 rounded-md hover:bg-cyan-500/10 transition font-semibold"
          >
            Close
          </motion.button>
        </div>
 
        {/* Summary Text */}
        <div className="relative font-mono text-[14px] border border-cyan-400/20 rounded-lg p-5 bg-[#031524]/70 leading-relaxed shadow-inner min-h-[220px] overflow-y-auto max-h-[380px]">
          <pre className="whitespace-pre-wrap text-cyan-100">{display}</pre>
          {phase === "typing" && (
            <motion.span
              animate={{ opacity: [0, 1, 0] }}
              transition={{ duration: 0.7, repeat: Infinity }}
              className="inline-block w-[8px] h-[18px] bg-cyan-400 ml-1 rounded-sm"
            />
          )}
        </div>
 
        {/* Graph */}
        <div className="mt-8">
          <svg viewBox="0 0 100 40" className="w-full h-10">
            <polyline fill="none" stroke="url(#grad)" strokeWidth="2" points={points} />
            <defs>
              <linearGradient id="grad" x1="0%" y1="0%" x2="100%" y2="0%">
                <stop offset="0%" stopColor="#f47b46" />
                <stop offset="60%" stopColor="#0ea5e9" />
                <stop offset="100%" stopColor="#06b6d4" />
              </linearGradient>
            </defs>
          </svg>
          <div className="flex justify-between mt-1">
            <span className="text-[11px] text-cyan-300/70">Stability Index</span>
            <span className="text-[11px] text-cyan-200">{(avg * 100).toFixed(1)}%</span>
          </div>
          <motion.div className="h-2 rounded-full bg-gradient-to-r from-cyan-900/40 to-blue-800/40 mt-1 overflow-hidden">
            <motion.div
              className="h-full bg-gradient-to-r from-cyan-400 to-emerald-400"
              style={{ width: `${avg * 100}%` }}
              initial={{ width: 0 }}
              animate={{ width: `${avg * 100}%` }}
              transition={{ duration: 1, ease: "easeOut" }}
            />
          </motion.div>
        </div>
      </HUDContainer>
    </DepthReactiveGlass>
  );
};