All files / src/apps/user/pages/knowledge/source TrustPanel.tsx

91.3% Statements 21/23
73.07% Branches 19/26
100% Functions 7/7
90.9% Lines 20/22

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                                          2x 6x                         6x                           6x 6x                 2x 9x 9x 9x   3x     9x   3x     9x 9x 12x 9x   9x                               12x 12x                                                     6x                                                      
import { SectionCard, SkeletonLines, StatusBadge } from "@/components/detail";
import type { KnowledgeTrust, TrustFact, TrustSignalSpec } from "@/objects/Knowledge";
import { resourceHref } from "@/routes/resourceHref";
import { useApiHandler } from "@/services/useApiHandler";
import { parseServerDate } from "@/util/time";
import { useQuery } from "@tanstack/react-query";
import { ExternalLink, ShieldCheck, TriangleAlert } from "lucide-react";
import React from "react";
import { Link } from "react-router-dom";
 
import { TRUST_SIGNALS_KEY, sourceTrustKey } from "../figures";
import { TRUST_STATE, TRUST_STATE_LABEL } from "../scales";
 
/**
 * What is on record about relying on a source (svc-core
 * `services/knowledge/trust.py`): each signal's state, the sentence that
 * explains it, and the facts it read — a rule linked to its page, a licence
 * to its terms. States, never a score: the facts are of different kinds,
 * and a number would invent a weighting between them.
 */
 
const FactValue: React.FC<{ fact: TrustFact }> = ({ fact }) => {
  Iif (fact.at) {
    return (
      <time dateTime={fact.at}>
        {parseServerDate(fact.at).toLocaleString(undefined, {
          day: "numeric",
          month: "short",
          year: "numeric",
          hour: "2-digit",
          minute: "2-digit",
        })}
      </time>
    );
  }
  Iif (fact.url) {
    return (
      <a
        href={fact.url}
        target="_blank"
        rel="noopener noreferrer"
        className="inline-flex items-center gap-1 hover:underline break-all"
      >
        {fact.value}
        <ExternalLink className="size-3 shrink-0" aria-hidden="true" />
      </a>
    );
  }
  const href =
    fact.ref_kind === "rule" && fact.ref_id ? resourceHref("rule", fact.ref_id) : null;
  return href ? (
    <Link to={href} className="hover:underline">
      {fact.value}
    </Link>
  ) : (
    <>{fact.value}</>
  );
};
 
const TrustPanel: React.FC<{ knowledgeId: string }> = ({ knowledgeId }) => {
  const { getApiHandler } = useApiHandler();
  const api = getApiHandler().knowledge_router;
  const { data: trustData, isLoading } = useQuery<KnowledgeTrust | null>({
    queryKey: sourceTrustKey(knowledgeId),
    queryFn: () => api.trust(knowledgeId),
    staleTime: 60_000,
  });
  const { data: catalogueData } = useQuery<TrustSignalSpec[]>({
    queryKey: TRUST_SIGNALS_KEY,
    queryFn: () => api.trustSignals(),
    staleTime: Infinity,
  });
  const trust: KnowledgeTrust | null | undefined = trustData;
  const catalogue: TrustSignalSpec[] = catalogueData ?? [];
  const specs = new Map<string, TrustSignalSpec>(catalogue.map((s) => [s.key, s]));
  const attention = new Set(trust?.attention ?? []);
 
  return (
    <SectionCard
      icon={ShieldCheck}
      title="Trust"
      description="What is on record about relying on it"
    >
      {isLoading ? (
        <SkeletonLines count={5} />
      ) : !trust ? (
        <p className="text-sm text-gray-400 dark:text-gray-500 italic">
          Its trust signals could not be read.
        </p>
      ) : (
        <>
          <ul className="divide-y divide-gray-100 dark:divide-gray-700">
            {trust.signals.map((signal) => {
              const spec = specs.get(signal.key);
              return (
                <li key={signal.key} className="py-3 first:pt-0 last:pb-0">
                  <div className="flex items-start justify-between gap-3">
                    <div className="min-w-0">
                      <div className="flex items-center gap-1.5 text-sm font-medium text-gray-900 dark:text-gray-100">
                        {attention.has(signal.key) && (
                          <TriangleAlert
                            className="size-3.5 shrink-0 text-amber-600 dark:text-amber-400"
                            aria-label="Needs attention"
                          />
                        )}
                        <span title={spec?.description}>{spec?.label ?? signal.key}</span>
                      </div>
                      <p className="mt-0.5 text-sm text-gray-600 dark:text-gray-300">
                        {signal.summary}
                      </p>
                    </div>
                    <StatusBadge
                      status={signal.state}
                      scale={TRUST_STATE}
                      label={TRUST_STATE_LABEL[signal.state] ?? signal.state}
                      className="shrink-0"
                    />
                  </div>
                  {signal.facts.length > 0 && (
                    <dl className="mt-2 grid grid-cols-1 sm:grid-cols-[minmax(0,12rem)_1fr] gap-x-4 gap-y-1 text-xs">
                      {signal.facts.map((fact, i) => (
                        <React.Fragment key={`${fact.label}-${i}`}>
                          <dt className="text-gray-400 dark:text-gray-500 truncate" title={fact.label}>
                            {fact.label}
                          </dt>
                          <dd className="text-gray-700 dark:text-gray-200 min-w-0">
                            <FactValue fact={fact} />
                          </dd>
                        </React.Fragment>
                      ))}
                    </dl>
                  )}
                </li>
              );
            })}
          </ul>
          <p className="mt-4 text-[11px] text-gray-400 dark:text-gray-500">
            Signals read what is recorded about a source, not its content.
            Tools rules govern the web an agent searches and reads; they do
            not filter knowledge retrieval.
          </p>
        </>
      )}
    </SectionCard>
  );
};
 
export default TrustPanel;