All files / src/apps/user/pages/knowledge DiscoveryProgress.tsx

85.71% Statements 12/14
80.76% Branches 21/26
71.42% Functions 5/7
84.61% Lines 11/13

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                                    11x 4x       4x                           11x           148x 39x     156x 156x 156x   156x 156x                                                                      
import {
  GenerationProgress,
  GenerationStage,
} from "@/objects/KnowledgeSetup";
import { Check, CircleDashed, Loader2 } from "lucide-react";
import React from "react";
 
// Where a background search for knowledge has got to: the stages svc-core
// records on its run (`knowledge/generation_job.py`), each with what it has
// counted. Shared by knowledge generation, the agent wizard's Knowledge
// step and "Find sources" on a source's page; only the last step's name
// differs (designing new sources, or matching found links to given ones).
 
/** The stages a run reports, in order, with what each has counted. */
export const DISCOVERY_STAGES: {
  stage: GenerationStage;
  label: string;
  counted: (p: GenerationProgress) => string | null;
}[] = [
  { stage: "planning", label: "Planning the searches", counted: () => null },
  {
    stage: "searching",
    label: "Searching the web",
    counted: (p) => (p.searches ? `${p.searches} searches` : null),
  },
  {
    stage: "checking",
    label: "Checking what was found",
    counted: (p) => (p.found ? `${p.found} results` : null),
  },
  {
    stage: "designing",
    label: "Designing the sources",
    counted: (p) => (p.usable != null ? `${p.usable} usable` : null),
  },
];
 
const DiscoveryProgress: React.FC<{
  stage: GenerationStage;
  progress: GenerationProgress;
  /** What the last stage is called, when it is not a design. */
  lastStep?: string;
}> = ({ stage, progress, lastStep }) => {
  const current = DISCOVERY_STAGES.findIndex((s) => s.stage === stage);
  return (
    <ol className="space-y-2.5" aria-label="Progress">
      {DISCOVERY_STAGES.map((s, i) => {
        const done = current > i;
        const active = current === i;
        const counted = done || active ? s.counted(progress) : null;
        const label =
          lastStep && i === DISCOVERY_STAGES.length - 1 ? lastStep : s.label;
        return (
          <li
            key={s.stage}
            className="flex items-center gap-2.5 text-sm"
            aria-current={active ? "step" : undefined}
          >
            {done ? (
              <Check className="size-4 shrink-0 text-emerald-500" />
            ) : active ? (
              <Loader2 className="size-4 shrink-0 animate-spin text-violet-500" />
            ) : (
              <CircleDashed className="size-4 shrink-0 text-gray-300 dark:text-gray-600" />
            )}
            <span
              className={
                done || active
                  ? "text-gray-700 dark:text-gray-300"
                  : "text-gray-400 dark:text-gray-500"
              }
            >
              {label}
            </span>
            {counted && (
              <span className="text-xs text-gray-400 dark:text-gray-500">
                {counted}
              </span>
            )}
          </li>
        );
      })}
    </ol>
  );
};
 
export default DiscoveryProgress;