Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

26.7 KB
Raw Blame
import { useState, useEffect, useMemo } from 'react';
import { useNavigate } from 'react-router';
import {
  Heart,
  FileText,
  CheckCircle,
  Sparkles,
  Download,
  Play,
  Settings,
  AlertCircle,
  AlertTriangle,
  Target,
  RefreshCw,
  ChevronRight
} from 'lucide-react';
import * as api from '../../../services/api';
import toast from '../../ui/Toast';
import ListLoadError from '../../ui/ListLoadError';
import useProviderList from '../../../hooks/useProviderList';
import { FormField } from '../../ui/FormField';
import TwinProviderPicker from '../TwinProviderPicker.jsx';

import {
  DOCUMENT_CATEGORIES,
  getHealthColor,
  getHealthLabel
} from '../constants';
import { timeAgo } from '../../../utils/formatters';
import { clickableProps, onActivateKeyDown } from '../../../lib/a11yKeyboard.js';
import SoulWizard from '../SoulWizard';
import PersonalityMap from '../PersonalityMap';
import ConfidenceGauge from '../ConfidenceGauge';
import GapRecommendations from '../GapRecommendations';
import NextActionBanner from '../NextActionBanner';

export default function OverviewTab({ status, settings, onRefresh, onSettingsChange }) {
  const navigate = useNavigate();
  const [showSettings, setShowSettings] = useState(false);
  const [settingsForm, setSettingsForm] = useState(settings || {});
  const [saving, setSaving] = useState(false);
  const [completeness, setCompleteness] = useState(null);
  const [loadingCompleteness, setLoadingCompleteness] = useState(false);
  const [showValidation, setShowValidation] = useState(false);
  const [contradictions, setContradictions] = useState(null);
  const [checkingContradictions, setCheckingContradictions] = useState(false);
  const providerList = useProviderList();
  const providers = useMemo(() => providerList.items.filter(p => p.enabled), [providerList.items]);
  // Until the user picks one, default to the first enabled provider.
  const [pickedProvider, setSelectedProvider] = useState(null);
  const selectedProvider = pickedProvider
    ?? (providers[0] ? { providerId: providers[0].id, model: providers[0].defaultModel } : null);
  const [showWizard, setShowWizard] = useState(false);
  const [traits, setTraits] = useState(null);
  const [confidence, setConfidence] = useState(null);
  const [gaps, setGaps] = useState([]);
  // A failed insights read keeps whatever loaded before and surfaces a Retry
  // instead of rendering as an empty personality map / no gaps.
  const [insightsError, setInsightsError] = useState(null);

  useEffect(() => {
    if (!showSettings) setSettingsForm(settings || {});
  }, [settings, showSettings]);

  useEffect(() => {
    loadCompleteness();
    loadTraitsAndConfidence();
  }, []);

  const loadTraitsAndConfidence = async () => {
    setInsightsError(null);
    const [traitsRes, confidenceRes, gapsRes] = await Promise.allSettled([
      api.getDigitalTwinTraits({ silent: true }),
      api.getDigitalTwinConfidence({ silent: true }),
      api.getDigitalTwinGaps({ silent: true })
    ]);
    if (traitsRes.status === 'fulfilled') setTraits(traitsRes.value.traits);
    if (confidenceRes.status === 'fulfilled') setConfidence(confidenceRes.value.confidence);
    if (gapsRes.status === 'fulfilled') setGaps(gapsRes.value.gaps || []);
    const failed = [traitsRes, confidenceRes, gapsRes].find(r => r.status === 'rejected');
    if (failed) setInsightsError(failed.reason instanceof Error ? failed.reason : new Error('Request failed'));
  };

  const loadCompleteness = async () => {
    setLoadingCompleteness(true);
    const data = await api.getDigitalTwinCompleteness().catch(() => null);
    setCompleteness(data);
    setLoadingCompleteness(false);
  };

  const checkContradictions = async () => {
    if (!selectedProvider) {
      toast.error('Select a provider first');
      return;
    }
    setCheckingContradictions(true);
    // silent: the failure is surfaced inline below (contradictions.error), so
    // letting request() toast as well would double-report the same error.
    const result = await api.detectSoulContradictions(
      selectedProvider.providerId,
      selectedProvider.model,
      { silent: true }
    ).catch(e => ({ error: e.message }));
    setContradictions(result);
    setCheckingContradictions(false);
  };

  const isEmpty = !status || status.documentCount === 0;

  const handleSaveSettings = async () => {
    setSaving(true);
    // request() owns the failure toast, but the spinner must clear either way:
    // an uncaught rejection here left `saving` true, stranding the button
    // permanently disabled on "Saving..." with no way to retry.
    const ok = await api.updateSoulSettings(settingsForm).then(saved => {
      onSettingsChange?.(saved);
      return true;
    }).catch(() => false);
    setSaving(false);
    if (!ok) return;
    toast.success('Settings updated');
    setShowSettings(false);
    onRefresh();
  };

  if (isEmpty) {
    if (showWizard) {
      return (
        <div className="max-w-2xl mx-auto p-8">
          <SoulWizard
            onComplete={() => {
              setShowWizard(false);
              onRefresh();
            }}
            onCancel={() => setShowWizard(false)}
          />
        </div>
      );
    }

    return (
      <div className="flex flex-col items-center justify-center h-full max-w-2xl mx-auto text-center p-8">
        <Heart className="w-16 h-16 text-pink-500 mb-6" />
        <h2 className="text-2xl font-bold text-white mb-4">Create Your Digital Twin</h2>
        <p className="text-gray-400 mb-8">
          A soul document is a comprehensive identity scaffold that defines who you are -
          your values, preferences, communication style, and cognitive patterns.
          When loaded into an LLM, it creates a digital twin that thinks and responds like you.
        </p>

        <div className="grid grid-cols-1 sm:grid-cols-3 gap-4 w-full mb-8">
          <div className="p-4 bg-port-card rounded-lg border border-port-border">
            <FileText className="w-8 h-8 text-port-accent-2 mb-3" />
            <h3 className="font-semibold text-white mb-2">Core Identity</h3>
            <p className="text-sm text-gray-400">Define your values, philosophy, and worldview</p>
          </div>
          <div className="p-4 bg-port-card rounded-lg border border-port-border">
            <Sparkles className="w-8 h-8 text-port-warning mb-3" />
            <h3 className="font-semibold text-white mb-2">Enrichment</h3>
            <p className="text-sm text-gray-400">Answer questions to expand your profile</p>
          </div>
          <div className="p-4 bg-port-card rounded-lg border border-port-border">
            <CheckCircle className="w-8 h-8 text-port-success mb-3" />
            <h3 className="font-semibold text-white mb-2">Testing</h3>
            <p className="text-sm text-gray-400">Verify alignment across different models</p>
          </div>
        </div>

        <div className="flex flex-col sm:flex-row gap-3 sm:gap-4">
          <button
            onClick={() => setShowWizard(true)}
            className="px-6 py-3 min-h-[44px] bg-port-accent text-white rounded-lg font-medium hover:bg-port-accent/80 transition-colors"
          >
            Start Wizard
          </button>
          <button
            onClick={() => navigate('/digital-twin/enrich')}
            className="px-6 py-3 min-h-[44px] bg-port-card text-white rounded-lg font-medium border border-port-border hover:bg-port-border transition-colors"
          >
            Start Enrichment
          </button>
          <button
            onClick={() => navigate('/digital-twin/documents')}
            className="px-6 py-3 min-h-[44px] bg-port-card text-white rounded-lg font-medium border border-port-border hover:bg-port-border transition-colors"
          >
            Create Document
          </button>
        </div>
      </div>
    );
  }

  const handleBannerRefresh = () => {
    loadTraitsAndConfidence();
    onRefresh();
  };

  return (
    <div className="space-y-6">
      {/* Next Action Banner */}
      <NextActionBanner
        gaps={gaps}
        status={status}
        traits={traits}
        onRefresh={handleBannerRefresh}
      />

      {/* Health Score Card */}
      <div className="bg-port-card rounded-lg border border-port-border p-6">
        <div className="flex items-center justify-between mb-4">
          <h2 className="text-lg font-semibold text-white">Soul Health</h2>
          <button
            onClick={() => setShowSettings(!showSettings)}
            className="p-2 min-h-[40px] min-w-[40px] flex items-center justify-center text-gray-400 hover:text-white transition-colors"
            title="Settings" aria-label="Settings"
          >
            <Settings size={20} />
          </button>
        </div>

        <div className="flex flex-col sm:flex-row items-center gap-6">
          <div className="relative w-28 h-28 sm:w-32 sm:h-32 shrink-0">
            <svg className="w-full h-full transform -rotate-90" viewBox="0 0 128 128">
              <circle
                cx="64"
                cy="64"
                r="56"
                stroke="currentColor"
                strokeWidth="8"
                fill="none"
                className="text-port-border"
              />
              <circle
                cx="64"
                cy="64"
                r="56"
                stroke="currentColor"
                strokeWidth="8"
                fill="none"
                strokeDasharray={`${status.healthScore * 3.52} 352`}
                strokeLinecap="round"
                className={getHealthColor(status.healthScore)}
              />
            </svg>
            <div className="absolute inset-0 flex flex-col items-center justify-center">
              <span className={`text-2xl sm:text-3xl font-bold ${getHealthColor(status.healthScore)}`}>
                {status.healthScore}%
              </span>
              <span className="text-xs text-gray-500">
                {getHealthLabel(status.healthScore)}
              </span>
            </div>
          </div>

          <div className="flex-1 grid grid-cols-3 gap-2 sm:gap-4 w-full">
            <div className="text-center sm:text-left">
              <div className="text-xl sm:text-2xl font-bold text-white">{status.enabledDocuments}</div>
              <div className="text-xs sm:text-sm text-gray-500">Active Docs</div>
            </div>
            <div className="text-center sm:text-left">
              <div className="text-xl sm:text-2xl font-bold text-white">
                {status.enrichmentProgress?.completedCategories || 0}/{status.enrichmentProgress?.totalCategories || 10}
              </div>
              <div className="text-xs sm:text-sm text-gray-500">Enriched</div>
            </div>
            <div className="text-center sm:text-left">
              <div className="text-xl sm:text-2xl font-bold text-white">
                {status.lastTestRun ? `${Math.round(status.lastTestRun.score * 100)}%` : '—'}
              </div>
              <div className="text-xs sm:text-sm text-gray-500">Test Score</div>
            </div>
          </div>
        </div>

        {/* Settings Panel */}
        {showSettings && (
          <div className="mt-6 pt-6 border-t border-port-border">
            <h3 className="text-sm font-medium text-gray-400 mb-4">Settings</h3>
            <div className="space-y-4">
              <label className="flex items-center gap-3">
                <input
                  type="checkbox"
                  checked={settingsForm.autoInjectToCoS || false}
                  onChange={(e) => setSettingsForm({ ...settingsForm, autoInjectToCoS: e.target.checked })}
                  className="w-4 h-4 rounded border-port-border bg-port-bg text-port-accent focus:ring-port-accent"
                />
                <span className="text-white">Auto-inject soul context into CoS agents</span>
              </label>
              <label className="flex items-start gap-3">
                <input
                  type="checkbox"
                  checked={settingsForm.includePrivacyContext || false}
                  onChange={(e) => setSettingsForm({ ...settingsForm, includePrivacyContext: e.target.checked })}
                  className="w-4 h-4 mt-1 rounded border-port-border bg-port-bg text-port-accent focus:ring-port-accent"
                />
                <span className="text-white">
                  Inject Privacy Vault identity facts
                  <span className="block text-xs text-gray-500 mt-0.5">
                    Adds vault records you marked &ldquo;share with twin&rdquo; (legal name, contact
                    details, addresses) plus a one-line org summary to twin &amp; CoS agent prompts.
                    Decrypted only at injection time, never cached. Off by default — sharing PII is
                    opt-in per-field <em>and</em> globally here.
                  </span>
                </span>
              </label>
              <FormField labelClassName="block text-sm text-gray-400 mb-2" label="Max context tokens">
                <input
                  type="number"
                  value={settingsForm.maxContextTokens || 4000}
                  onChange={(e) => setSettingsForm({ ...settingsForm, maxContextTokens: parseInt(e.target.value, 10) })}
                  className="w-32 px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white"
                />
              </FormField>
              <button
                onClick={handleSaveSettings}
                disabled={saving}
                className="px-4 py-2 bg-port-accent text-white rounded-lg text-sm hover:bg-port-accent/80 disabled:opacity-50"
              >
                {saving ? 'Saving...' : 'Save Settings'}
              </button>
            </div>
          </div>
        )}
      </div>

      {insightsError && (
        <ListLoadError what="personality traits, confidence and gaps" error={insightsError} onRetry={loadTraitsAndConfidence} />
      )}

      {/* Personality Traits & Confidence - New Phase 1 & 2 Components */}
      <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
        <PersonalityMap
          traits={traits}
          confidence={confidence}
          providers={providers}
          onAnalyze={loadTraitsAndConfidence}
        />
        <ConfidenceGauge
          confidence={confidence}
          onRecalculate={loadTraitsAndConfidence}
        />
      </div>

      {/* Gap Recommendations */}
      {gaps.length > 0 && (
        <GapRecommendations gaps={gaps} maxDisplay={3} />
      )}

      {/* Completeness Score */}
      <div className="bg-port-card rounded-lg border border-port-border p-6">
        <div className="flex items-center justify-between mb-4">
          <h2 className="text-lg font-semibold text-white flex items-center gap-2">
            <Target size={20} />
            Completeness
          </h2>
          <button
            onClick={() => setShowValidation(!showValidation)}
            className="text-sm text-port-accent hover:text-white transition-colors"
          >
            {showValidation ? 'Hide Details' : 'Show Details'}
          </button>
        </div>

        {loadingCompleteness ? (
          <div className="flex items-center gap-2 text-gray-400">
            <RefreshCw className="w-4 h-4 animate-spin" />
            Analyzing...
          </div>
        ) : completeness ? (
          <div>
            <div className="flex items-center gap-4 mb-4">
              <div className={`text-3xl font-bold ${
                completeness.score >= 80 ? 'text-port-success' :
                completeness.score >= 50 ? 'text-port-warning' : 'text-port-error'
              }`}>
                {completeness.score}%
              </div>
              <div>
                <div className="text-white">{completeness.found}/{completeness.total} sections covered</div>
                <div className="text-sm text-gray-500">
                  {completeness.missing.length === 0 ? 'All core sections present' : `${completeness.missing.length} sections missing`}
                </div>
              </div>
            </div>

            {/* Progress bar */}
            <div className="h-2 bg-port-border rounded-full overflow-hidden mb-4">
              <div
                className={`h-full transition-all ${
                  completeness.score >= 80 ? 'bg-port-success' :
                  completeness.score >= 50 ? 'bg-port-warning' : 'bg-port-error'
                }`}
                style={{ width: `${completeness.score}%` }}
              />
            </div>

            {showValidation && completeness.missing.length > 0 && (
              <div className="space-y-3 mt-4 pt-4 border-t border-port-border">
                <h3 className="text-sm font-medium text-gray-400">Missing Sections</h3>
                {completeness.missing.map(section => (
                  <div
                    key={section.id}
                    className="p-3 bg-port-bg rounded-lg border border-port-border"
                  >
                    <div className="flex items-center justify-between">
                      <div>
                        <div className="font-medium text-white">{section.label}</div>
                        <div className="text-sm text-gray-400">{section.description}</div>
                      </div>
                      {section.enrichmentCategory && (
                        <button
                          onClick={() => navigate(`/digital-twin/enrich?category=${section.enrichmentCategory}`)}
                          className="flex items-center gap-1 text-sm text-port-accent hover:text-white"
                        >
                          Add via Enrich
                          <ChevronRight size={14} />
                        </button>
                      )}
                    </div>
                  </div>
                ))}
              </div>
            )}
          </div>
        ) : (
          <div className="text-gray-500">Unable to analyze completeness</div>
        )}
      </div>

      {/* Contradiction Detection */}
      <div className="bg-port-card rounded-lg border border-port-border p-6">
        <div className="flex items-center justify-between mb-4">
          <h2 className="text-lg font-semibold text-white flex items-center gap-2">
            <AlertTriangle size={20} />
            Consistency Check
          </h2>
        </div>

        <p className="text-sm text-gray-400 mb-4">
          Use AI to detect contradictions or inconsistencies between your soul documents.
        </p>

        <div className="flex flex-col sm:flex-row items-stretch sm:items-center gap-3 sm:gap-4 mb-4">
          {providerList.state === 'error' ? (
            <ListLoadError className="flex-1 min-w-0" what="AI providers" error={providerList.error} onRetry={providerList.retry} />
          ) : (
            <TwinProviderPicker
              compact
              className="flex-1 min-w-0"
              providers={providers}
              selected={selectedProvider}
              onChange={setSelectedProvider}
            />
          )}

          <button
            onClick={checkContradictions}
            disabled={checkingContradictions}
            className="px-4 py-3 min-h-[44px] bg-port-accent text-white rounded-lg text-sm hover:bg-port-accent/80 disabled:opacity-50 flex items-center justify-center gap-2"
          >
            {checkingContradictions ? (
              <>
                <RefreshCw className="w-4 h-4 animate-spin" />
                Analyzing...
              </>
            ) : (
              'Check for Contradictions'
            )}
          </button>
        </div>

        {contradictions && (
          <div className="mt-4 pt-4 border-t border-port-border">
            {contradictions.error ? (
              <div className="text-port-error text-sm">{contradictions.error}</div>
            ) : contradictions.issues?.length === 0 ? (
              <div className="flex items-center gap-2 text-port-success">
                <CheckCircle size={18} />
                No contradictions detected
              </div>
            ) : (
              <div className="space-y-3">
                <div className="text-sm text-gray-400">{contradictions.summary}</div>
                {contradictions.issues?.map((issue, i) => (
                  <div
                    key={i}
                    className={`p-3 rounded-lg border ${
                      issue.severity === 'high' ? 'bg-port-error/10 border-port-error/30' :
                      issue.severity === 'medium' ? 'bg-port-warning/10 border-port-warning/30' :
                      'bg-port-accent/10 border-port-accent/30'
                    }`}
                  >
                    <div className="font-medium text-white mb-1">
                      {issue.docs?.join(' & ')}
                    </div>
                    <div className="text-sm text-gray-300 mb-2">{issue.explanation}</div>
                    {issue.suggestion && (
                      <div className="text-sm text-gray-400">
                        Suggestion: {issue.suggestion}
                      </div>
                    )}
                  </div>
                ))}
              </div>
            )}
          </div>
        )}
      </div>

      {/* Document Categories */}
      <div className="bg-port-card rounded-lg border border-port-border p-4 sm:p-6">
        <h2 className="text-lg font-semibold text-white mb-4">Documents by Category</h2>
        <div className="grid grid-cols-2 sm:grid-cols-4 gap-3 sm:gap-4">
          {Object.entries(DOCUMENT_CATEGORIES).map(([key, config]) => {
            const count = status.documentsByCategory?.[key] || 0;
            const Icon = config.icon;
            return (
              <div
                key={key}
                className={`p-4 rounded-lg border ${config.color} cursor-pointer hover:opacity-80 transition-opacity`}
                onClick={() => navigate('/digital-twin/documents')}
                {...clickableProps(() => navigate('/digital-twin/documents'))}
                onKeyDown={onActivateKeyDown(() => navigate('/digital-twin/documents'))}
              >
                <div className="flex items-center gap-2 mb-2">
                  <Icon size={16} />
                  <span className="font-medium">{config.label}</span>
                </div>
                <div className="text-2xl font-bold">{count}</div>
              </div>
            );
          })}
        </div>
      </div>

      {/* Quick Actions */}
      <div className="grid grid-cols-1 sm:grid-cols-3 gap-3 sm:gap-4">
        <button
          onClick={() => navigate('/digital-twin/test')}
          className="flex items-center gap-3 sm:gap-4 p-4 min-h-[72px] bg-port-card rounded-lg border border-port-border hover:border-port-accent transition-colors"
        >
          <div className="p-2.5 sm:p-3 rounded-lg bg-port-success/20 shrink-0">
            <Play className="w-5 h-5 sm:w-6 sm:h-6 text-port-success" />
          </div>
          <div className="text-left min-w-0">
            <div className="font-medium text-white">Run Tests</div>
            <div className="text-sm text-gray-500 truncate">
              {status.lastTestRun
                ? `Last: ${timeAgo(status.lastTestRun.timestamp)}`
                : 'Test behavioral alignment'}
            </div>
          </div>
        </button>

        <button
          onClick={() => navigate(gaps[0]?.suggestedCategory ? `/digital-twin/enrich?category=${gaps[0].suggestedCategory}` : '/digital-twin/enrich')}
          className="flex items-center gap-3 sm:gap-4 p-4 min-h-[72px] bg-port-card rounded-lg border border-port-border hover:border-port-accent transition-colors"
        >
          <div className="p-2.5 sm:p-3 rounded-lg bg-port-warning/20 shrink-0">
            <Sparkles className="w-5 h-5 sm:w-6 sm:h-6 text-port-warning" />
          </div>
          <div className="text-left min-w-0">
            <div className="font-medium text-white">Enrich Soul</div>
            <div className="text-sm text-gray-500 truncate">
              {status.enrichmentProgress?.completedCategories || 0} categories complete
            </div>
          </div>
        </button>

        <button
          onClick={() => navigate('/digital-twin/export')}
          className="flex items-center gap-3 sm:gap-4 p-4 min-h-[72px] bg-port-card rounded-lg border border-port-border hover:border-port-accent transition-colors"
        >
          <div className="p-2.5 sm:p-3 rounded-lg bg-port-accent/20 shrink-0">
            <Download className="w-5 h-5 sm:w-6 sm:h-6 text-port-accent" />
          </div>
          <div className="text-left min-w-0">
            <div className="font-medium text-white">Export Soul</div>
            <div className="text-sm text-gray-500 truncate">Download for external LLMs</div>
          </div>
        </button>
      </div>

      {/* Last Test Result */}
      {status.lastTestRun && (
        <div className="bg-port-card rounded-lg border border-port-border p-4 sm:p-6">
          <h2 className="text-lg font-semibold text-white mb-4">Latest Test Run</h2>
          <div className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4">
            <div className="flex items-center gap-4">
              <div className={`text-2xl sm:text-3xl font-bold ${
                status.lastTestRun.score >= 0.8 ? 'text-port-success' :
                status.lastTestRun.score >= 0.5 ? 'text-port-warning' : 'text-port-error'
              }`}>
                {Math.round(status.lastTestRun.score * 100)}%
              </div>
              <div>
                <div className="text-white font-medium">
                  {status.lastTestRun.passed}/{status.lastTestRun.total} tests passed
                </div>
                <div className="text-xs sm:text-sm text-gray-500">
                  Model: {status.lastTestRun.model} • {timeAgo(status.lastTestRun.timestamp)}
                </div>
              </div>
            </div>
            <button
              onClick={() => navigate('/digital-twin/test')}
              className="px-3 py-1.5 text-sm text-port-accent hover:text-white transition-colors"
            >
              View Details
            </button>
          </div>
        </div>
      )}

      {/* CoS Integration Status */}
      <div className="bg-port-card rounded-lg border border-port-border p-4">
        <div className="flex items-center gap-3">
          {settings?.autoInjectToCoS ? (
            <>
              <CheckCircle className="w-5 h-5 text-port-success" />
              <span className="text-white">Soul context is being injected into CoS agents</span>
            </>
          ) : (
            <>
              <AlertCircle className="w-5 h-5 text-port-warning" />
              <span className="text-white">Soul context injection is disabled</span>
              <button
                onClick={() => setShowSettings(true)}
                className="ml-auto text-sm text-port-accent hover:text-white"
              >
                Enable
              </button>
            </>
          )}
        </div>
      </div>
    </div>
  );
}