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

14.7 KB
Raw Blame
import { useCallback, useEffect, useRef, useState } from 'react';
import { CheckCircle2, Circle, Download, ExternalLink, RefreshCw, ShieldCheck } from 'lucide-react';
import toast from '../ui/Toast';
import BrailleSpinner from '../BrailleSpinner';
import PromptGuardHfAccessNotice from '../imageGen/PromptGuardHfAccessNotice.jsx';
import { useHfTokenStatus } from '../../hooks/useHfTokenStatus';
import {
  cancelModelAbuseGuardInstall,
  getModelAbuseGuardStatus,
  installModelAbuseGuard,
} from '../../services/api';
import socket from '../../services/socket';
import UntrustedContentPolicyPanel from './UntrustedContentPolicyPanel.jsx';

const FALLBACK_STAGES = [
  { id: 'huggingface-token', label: 'Hugging Face access token', description: 'A read token plus gated-model approval on the Prompt Guard model card.' },
  { id: 'python', label: 'Host Python', description: 'Python 3.10 or newer, with a supported PyTorch wheel for this machine.' },
  { id: 'venv', label: 'Dedicated Prompt Guard runtime', description: 'A private virtualenv that never shares packages with image or video generation.' },
  { id: 'packages', label: 'Classifier packages', description: 'Pinned torch, transformers, safetensors, and huggingface_hub imports.' },
  { id: 'model', label: 'Pinned model snapshot', description: 'The five required Prompt Guard files from the pinned revision.' },
];

function stagesFromStatus(status) {
  return Array.isArray(status?.stages) && status.stages.length ? status.stages : FALLBACK_STAGES.map((stage) => {
    if (stage.id === 'packages') return { ...stage, ready: status?.runtimeReady === true };
    if (stage.id === 'model') return { ...stage, ready: status?.modelCached === true };
    if (stage.id === 'venv') return { ...stage, ready: status?.venvReady === true };
    if (stage.id === 'python') return { ...stage, ready: status?.pythonAvailable === true };
    return { ...stage, ready: false };
  });
}

export default function ModelAbuseGuardPanel() {
  const [guardStatus, setGuardStatus] = useState(null);
  const [statusError, setStatusError] = useState(false);
  const [installing, setInstalling] = useState(false);
  const [progressMsg, setProgressMsg] = useState('');
  const [installError, setInstallError] = useState('');
  const [installingStage, setInstallingStage] = useState(null);
  const progressTimer = useRef(null);
  const { present: tokenPresent, source: tokenSource, refresh: refreshToken } = useHfTokenStatus();

  const loadGuardStatus = useCallback(() => (
    getModelAbuseGuardStatus({ silent: true })
      .then((res) => {
        if (res) { setGuardStatus(res); setStatusError(false); }
        return res;
      })
      .catch(() => { setStatusError(true); return null; })
  ), []);

  useEffect(() => { loadGuardStatus(); }, [loadGuardStatus]);

  useEffect(() => {
    const handleProgress = (data) => {
      if (data?.scope !== 'security-guard') return;
      clearTimeout(progressTimer.current);
      if (data?.stage) setInstallingStage(data.stage);
      setProgressMsg(data?.message || '');
      if (data?.event === 'complete') {
        setInstalling(false);
        setInstallingStage(null);
        progressTimer.current = setTimeout(() => setProgressMsg(''), 3000);
        loadGuardStatus();
      }
      if (data?.event === 'error') {
        setInstalling(false);
        setInstallError(data.message || 'Installation failed. Refresh status for diagnostics.');
        loadGuardStatus();
      }
    };
    socket.on('localLlm:progress', handleProgress);
    return () => {
      socket.off('localLlm:progress', handleProgress);
      clearTimeout(progressTimer.current);
    };
  }, [loadGuardStatus]);

  const installGuard = () => {
    setInstallError('');
    setInstallingStage(null);
    setInstalling(true);
    setProgressMsg('Installing the dedicated guard…');
    return installModelAbuseGuard({ silent: true })
      .then((result) => {
        if (result?.ready === true) toast.success('Model-abuse guard installed and ready');
        return loadGuardStatus();
      })
      .catch((error) => { setInstallError(error.message || 'Installation failed.'); return loadGuardStatus(); })
      .finally(() => {
        setInstalling(false);
        setInstallingStage(null);
      });
  };

  const cancelGuard = () => cancelModelAbuseGuardInstall({ silent: true })
    .then(loadGuardStatus)
    .catch(() => null);

  const stages = stagesFromStatus(guardStatus).map((stage) => {
    if (stage.id !== 'huggingface-token' || tokenPresent === null) return stage;
    return { ...stage, ready: tokenPresent === true };
  });
  const currentStageId = installingStage || (installing ? stages.find((stage) => !stage.ready)?.id : null);
  const overallReady = guardStatus?.ready === true;
  const contributionBlocked = guardStatus?.contributionAssessment?.ready === false;
  const incomplete = guardStatus?.setupState === 'incomplete' || (!overallReady && (guardStatus?.modelCached || guardStatus?.venvReady));

  return (
    <section
      id="llm-management-panel-abuse"
      role="tabpanel"
      aria-labelledby="model-abuse-guard-heading"
      data-testid="model-abuse-guard-card"
      className="max-w-6xl bg-port-card border border-port-border rounded-xl p-4 sm:p-6 space-y-5"
    >
      <div className="flex items-start justify-between gap-3 flex-wrap">
        <div className="flex items-start gap-2">
          <ShieldCheck size={18} className="text-port-accent mt-0.5" aria-hidden="true" />
          <div>
            <h2 id="model-abuse-guard-heading" className="text-lg font-semibold text-white">Model-abuse guard</h2>
            <p className="text-xs text-port-accent mt-0.5">Required by default · local classifier</p>
          </div>
        </div>
        {contributionBlocked && overallReady ? (
          <span className="text-xs px-2 py-1 rounded border border-port-warning/40 text-port-warning">Not ready</span>
        ) : overallReady ? (
          <span className="text-xs px-2 py-1 rounded border border-port-success/40 text-port-success">Ready</span>
        ) : guardStatus ? (
          <span className="text-xs px-2 py-1 rounded border border-port-warning/40 text-port-warning">{incomplete ? 'Setup incomplete' : 'Not installed'}</span>
        ) : statusError ? (
          <span role="status" className="text-xs text-port-warning">Status unavailable</span>
        ) : (
          <span className="text-xs text-gray-500">Checking status…</span>
        )}
      </div>
      <p className="text-sm text-gray-300 max-w-2xl leading-relaxed">
        Screens external issues, pull requests, and messages before analysis. Runs locally on CPU; no GPU required.
        Required screening blocks analysis when it fails or cannot finish.
      </p>
      {guardStatus?.contributionAssessment?.ready === false && (
        <p role="status" className="text-xs text-port-warning max-w-2xl">
          GitHub pull request and issue assessment is not ready ({guardStatus.contributionAssessment.code}), so the pending-merge coordinator cannot merge.{guardStatus.contributionAssessment.code === 'untrusted-content-model-required' ? ' Pick a provider and model in Content safety policies below.' : ''}
        </p>
      )}
      <details className="text-sm rounded-lg border border-port-border p-3">
        <summary className="cursor-pointer text-port-accent">How screening works and its limits</summary>
        <div className="mt-3 space-y-2 text-xs leading-relaxed text-gray-400 max-w-2xl">
          <p>Deterministic checks and a local classifier screen content before isolated, tool-free analysis and server-validated actions. A passing scan does not grant trust, prove attachments safe, or authorize private-record access. Classifiers can miss attacks and flag legitimate security examples.</p>
          <p>This installer uses the pinned Llama Prompt Guard 2 86M model for multilingual detection, scanning overlapping 512-token windows. Screening needs no chat model or cloud account. Status refreshes make no model calls.</p>
          <p>Private message analysis also needs a local API provider. Install a text model in <a className="text-port-accent hover:underline" href="/models/llms">LLMs</a>, then choose its provider below.</p>
        </div>
      </details>
      <div className="space-y-3">
        <div className="flex flex-wrap items-baseline justify-between gap-2">
          <h3 className="text-sm font-semibold text-white">Installation</h3>
          <span className="text-xs text-gray-400">{stages.filter(stage => stage.ready).length} of {stages.length} checks ready</span>
        </div>
        <details open={!overallReady || tokenPresent !== true} className="text-sm">
          <summary className="cursor-pointer text-port-accent mb-2">Hugging Face access &amp; token</summary>
          <PromptGuardHfAccessNotice
            tokenPresent={tokenPresent}
            tokenSource={tokenSource}
            model={guardStatus}
            onSaved={() => { refreshToken(); loadGuardStatus(); }}
          />
        </details>
        <ol className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-2 list-none p-0" aria-label="Abuse guard setup stages">
          {stages.map((stage) => {
            const current = installing && currentStageId === stage.id;
            const waiting = installing && !stage.ready && currentStageId && currentStageId !== stage.id;
            return (
              <li
                key={stage.id}
                data-testid={`abuse-guard-stage-${stage.id}`}
                data-ready={stage.ready ? 'true' : 'false'}
                className="flex items-start gap-2 rounded-lg border border-port-border/70 bg-port-bg/40 px-3 py-2"
              >
                <span className="mt-0.5 shrink-0" aria-hidden="true">
                  {stage.ready ? (
                    <CheckCircle2 size={14} className="text-port-success" />
                  ) : current ? (
                    <BrailleSpinner />
                  ) : (
                    <Circle size={14} className={waiting ? 'text-gray-600' : 'text-port-warning'} />
                  )}
                </span>
                <div className="min-w-0">
                  <div className="flex items-center gap-2 flex-wrap">
                    <p className="text-xs font-medium text-white">{stage.label}</p>
                    <span className={`text-[11px] ${stage.ready ? 'text-port-success' : current ? 'text-gray-300' : 'text-gray-500'}`}>
                      {stage.ready ? 'Ready' : current ? 'Installing…' : waiting ? 'Waiting' : 'Not ready'}
                    </span>
                  </div>
                  <details className="mt-1 text-xs text-gray-400">
                    <summary className="cursor-pointer hover:text-port-accent">Details</summary>
                    <p className="mt-1 leading-relaxed">{stage.description}</p>
                  </details>
                  {stage.id === 'python' && !stage.ready && (
                    <a href="https://www.python.org/downloads/" target="_blank" rel="noopener noreferrer" className="text-xs text-port-accent hover:underline">Install Python, then refresh status</a>
                  )}
                  {current && progressMsg && (
                    <p className="text-[11px] text-gray-400 mt-1">{progressMsg}</p>
                  )}
                </div>
              </li>
            );
          })}
        </ol>
        <div className="flex items-center gap-2 flex-wrap text-[11px] text-gray-500">
          <span>{guardStatus?.name || 'Llama Prompt Guard 2 86M'}</span>
          <span>·</span>
          <span>86M · offline · no tools</span>
          <a
            href={guardStatus?.sourceUrl || 'https://huggingface.co/meta-llama/Llama-Prompt-Guard-2-86M'}
            target="_blank"
            rel="noopener noreferrer"
            className="text-port-accent hover:underline inline-flex items-center gap-1"
          >
            Model card <ExternalLink size={11} />
          </a>
        </div>
        <div className="flex items-center gap-2 flex-wrap">
          <button type="button" onClick={loadGuardStatus} disabled={installing} className="px-2.5 py-1 text-xs border border-port-border text-gray-300 rounded flex items-center gap-1 disabled:opacity-50">
            <RefreshCw size={12} /> Refresh status
          </button>
          {overallReady ? (
            <span className="text-xs text-port-success">Installed from the pinned model revision.</span>
          ) : installing ? (
            <>
              <span className="flex items-center gap-1.5 text-xs text-gray-300"><BrailleSpinner /> Installing the dedicated guard…</span>
              <button
                type="button"
                onClick={cancelGuard}
                className="px-2.5 py-1 text-xs bg-port-border hover:bg-port-border/70 text-gray-300 rounded"
              >
                Cancel
              </button>
            </>
          ) : (
            <button
              type="button"
              onClick={installGuard}
              disabled={!guardStatus || tokenPresent !== true || guardStatus.pythonAvailable !== true}
              className="px-2.5 py-1 text-xs bg-port-accent/20 hover:bg-port-accent/30 text-port-accent rounded disabled:opacity-50 disabled:cursor-not-allowed flex items-center gap-1"
            >
              <Download size={12} /> {incomplete ? 'Repair model-abuse guard' : 'Install model-abuse guard'}
            </button>
          )}
          {installing && progressMsg && !installingStage && (
            <span className="text-[11px] text-gray-500">{progressMsg}</span>
          )}
        </div>
        {(installError || guardStatus?.lastInstallFailure) && (
          <div role="alert" className="text-xs text-port-warning space-y-1 break-words">
            {installError && <p>{installError}</p>}
            {guardStatus?.lastInstallFailure && <p>{guardStatus.lastInstallFailure.stage}: {guardStatus.lastInstallFailure.code}{Number.isInteger(guardStatus.lastInstallFailure.exitCode) ? ` (exit ${guardStatus.lastInstallFailure.exitCode})` : ''}. {guardStatus.lastInstallFailure.message} {guardStatus.lastInstallFailure.action}</p>}
          </div>
        )}
        {guardStatus?.runtimeIssue && <p className="text-xs text-port-warning">{guardStatus.runtimeIssue.message} {guardStatus.runtimeIssue.action}</p>}
        {guardStatus?.expectedPackages && <details className="text-xs text-gray-400 break-words"><summary>Expected classifier packages</summary><p>{guardStatus.expectedPackages.join(', ')}</p></details>}
        {incomplete && <p role="status" className="text-xs text-port-warning">A partial or failed installation blocks screening, including sources with an optional classifier. Repair the setup before retrying those tasks.</p>}
        <p className="text-xs text-gray-400">Install downloads Python packages and model weights. Accept the model terms and add a read token first.</p>
      </div>
      <UntrustedContentPolicyPanel />
    </section>
  );
}