All files / src/components/badge AIProvenance.tsx

100% Statements 8/8
94.73% Branches 18/19
100% Functions 4/4
100% Lines 7/7

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                                                          104x                                       11x 11x   11x     2x           11x                                                                   3x                                                  
import { encodeResourceId } from "@/components/detail";
import GenerationDetails from "@/components/diff/GenerationDetails/GenerationDetails";
import { Agent } from "@/objects/Agent";
import { APP_ROUTES, ROUTE_PARAMS } from "@/routes/const";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useQuery } from "@tanstack/react-query";
import { ArrowRight, ChevronDown, Sparkles } from "lucide-react";
import React, { useState } from "react";
import { Link } from "react-router-dom";
 
/**
 * Provenance strip for a generated resource's detail page: what made this,
 * and — on request — the exact invocation behind it.
 *
 * Two shapes of answer, because generated resources arrive two ways:
 *
 *   * **directly** — somebody described what they wanted and this is what
 *     came back (`POST /app/dashboards/generate`, the skillset and toolkit
 *     setup routers);
 *   * **alongside a parent** — the agent setup flow creates skills, workers,
 *     knowledge stubs, repositories and rules as dependents of the agent it
 *     was building, and `generated_by_agent_id` names that agent.
 *
 * Both collapse to one line by default. Provenance is a fact about the
 * resource, not a warning about it, so it does not open a panel of JSON in
 * front of the thing the reader came to look at — "View invocation" does
 * that, and only then is the run fetched.
 */
const AIProvenance: React.FC<{
  /** Singular noun for the resource: "agent", "knowledge base". */
  resourceLabel: string;
  /** The invocation that produced it. Absent on resources generated before
   *  generation runs were recorded, which is why the strip still renders
   *  without one. */
  generationId?: string | null;
  /** The agent this resource was created alongside, for dependents. */
  generatedByAgentId?: string | null;
  /** This resource's own id, so it is not listed among what the run produced
   *  — the reader is looking at it. */
  resourceId?: string | null;
  className?: string;
}> = ({
  resourceLabel,
  generationId,
  generatedByAgentId,
  resourceId,
  className = "",
}) => {
  const { getApiHandler } = useApiHandler();
  const [open, setOpen] = useState(false);
 
  const { data: parentAgent } = useQuery<Agent | null>({
    queryKey: [QUERY_KEYS.GET.AGENT_BY_ID, generatedByAgentId],
    queryFn: async () =>
      generatedByAgentId
        ? await getApiHandler().agent_router.id(generatedByAgentId)
        : null,
    enabled: !!generatedByAgentId,
  });
 
  return (
    <div
      className={`rounded-xl border border-violet-200 dark:border-violet-800 bg-violet-50 dark:bg-violet-950/40 px-4 py-2.5 ${className}`}
    >
      <div className="flex flex-wrap items-center gap-x-3 gap-y-1">
        <span className="inline-flex items-center gap-1.5 text-sm font-semibold text-violet-700 dark:text-violet-300 shrink-0">
          <Sparkles className="size-4" />
          AI Generated
        </span>
 
        <span className="text-sm text-violet-600/90 dark:text-violet-300/80">
          {generatedByAgentId
            ? `This ${resourceLabel} was created alongside an agent by the generative setup`
            : `This ${resourceLabel} was created by a generative flow`}
          {generatedByAgentId && !parentAgent && " of an agent that no longer exists"}
          .
        </span>
 
        {parentAgent && (
          <Link
            to={APP_ROUTES.USER.INFERENCE.AGENTS.id.replace(
              ROUTE_PARAMS.AGENT.id,
              encodeResourceId(parentAgent.id),
            )}
            className="inline-flex items-center gap-1 text-sm font-medium text-violet-700 dark:text-violet-300 hover:underline shrink-0"
          >
            Created with agent “{parentAgent.name}”
            <ArrowRight className="size-3.5" />
          </Link>
        )}
 
        {generationId && (
          <button
            type="button"
            onClick={() => setOpen((wasOpen) => !wasOpen)}
            aria-expanded={open}
            className="ml-auto inline-flex items-center gap-1 text-sm font-medium text-violet-700 dark:text-violet-300 hover:underline shrink-0"
          >
            <ChevronDown
              className={`size-3.5 transition-transform ${open ? "" : "-rotate-90"}`}
              aria-hidden="true"
            />
            {open ? "Hide invocation" : "View invocation"}
          </button>
        )}
      </div>
 
      {generationId && open && (
        <GenerationDetails
          generationId={generationId}
          variant="full"
          excludeResourceId={resourceId}
        />
      )}
    </div>
  );
};
 
export default AIProvenance;