All files / src/apps/user/pages/agents/agent AgentContextPanel.tsx

95.65% Statements 22/23
92.3% Branches 36/39
90.9% Functions 10/11
95.65% Lines 22/23

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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202                              3x         3x   3x               35x 35x 35x 35x 35x 35x                                                     7x                           2x     1x                               1x             1x                 2x                                                                         3x               47x 47x 47x   20x     47x                             35x                                              
import { SectionCard, SkeletonLines, StatusBadge } from "@/components/detail";
import type { AgentContext, AgentContextKind } from "@/objects/Agent";
import { APP_ROUTES } from "@/routes/const";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useQuery } from "@tanstack/react-query";
import { ArrowRight, Compass, Lightbulb, Plus, Sparkles } from "lucide-react";
import React, { useState } from "react";
import { Link } from "react-router-dom";
import GenerateKnowledgeModal from "../../knowledge/GenerateKnowledgeModal";
import { isKnowledgeCategory } from "../../knowledge/categories";
import type { KnowledgeCategoryKey } from "@/objects/Knowledge";
import { AGENT_CONTEXT, AGENT_CONTEXT_ICONS, AGENT_CONTEXT_LABEL } from "./agentContext";
 
/** The page another kind changes on, by svc-core's `Place.target`. */
const PAGES: Record<string, { href: string; label: string }> = {
  rules: { href: APP_ROUTES.USER.GOVERNANCE.rules, label: "Rules" },
};
 
const step =
  "inline-flex items-center gap-1 rounded-md px-2 py-1 text-xs font-medium text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors";
 
const KindRow: React.FC<{
  kind: AgentContextKind;
  onTab: (tab: string) => void;
  onAttach?: (category: KnowledgeCategoryKey) => void;
  onAttachBuiltIn?: (repositoryId: string) => void;
  attaching?: boolean;
  onGenerate?: (category: KnowledgeCategoryKey) => void;
}> = ({ kind, onTab, onAttach, onAttachBuiltIn, attaching, onGenerate }) => {
  const Icon = AGENT_CONTEXT_ICONS[kind.key] ?? Compass;
  const page = kind.place?.kind === "page" ? PAGES[kind.place.target] : undefined;
  const category = isKnowledgeCategory(kind.category) ? kind.category : null;
  const missingKnowledge = kind.state === "missing" && category !== null;
  const builtIns = missingKnowledge && onAttachBuiltIn ? (kind.suggestions ?? []) : [];
  return (
    <li className="flex items-start gap-3 py-3 first:pt-0 last:pb-0">
      <span className="shrink-0 rounded-lg bg-gray-100 dark:bg-gray-700 p-1.5 text-gray-600 dark:text-gray-300">
        <Icon className="size-4" aria-hidden="true" />
      </span>
      <div className="min-w-0 flex-1">
        <div className="flex flex-wrap items-center gap-x-2 gap-y-1">
          <h3 className="text-sm font-semibold text-gray-900 dark:text-gray-100">
            {kind.label}
          </h3>
          <span className="text-[11px] text-gray-400 dark:text-gray-500">
            {kind.owner_label}
          </span>
          <StatusBadge
            status={kind.state}
            scale={AGENT_CONTEXT}
            label={AGENT_CONTEXT_LABEL[kind.state] ?? kind.state}
            className="ml-auto shrink-0"
          />
        </div>
        <p className="mt-0.5 text-xs text-gray-700 dark:text-gray-300">{kind.summary}</p>
        <p className="mt-0.5 text-[11px] text-gray-400 dark:text-gray-500">
          {kind.description}
        </p>
        {kind.facts.length > 0 && (
          <dl className="mt-1 flex flex-wrap gap-x-4 gap-y-0.5 text-[11px]">
            {kind.facts.map((f) => (
              <div key={f.label} className="flex gap-1">
                <dt className="text-gray-400 dark:text-gray-500">{f.label}</dt>
                <dd className="text-gray-700 dark:text-gray-300">{f.value}</dd>
              </div>
            ))}
          </dl>
        )}
        {builtIns.length > 0 && (
          <div className="mt-1.5">
            <p className="text-[11px] text-gray-400 dark:text-gray-500">
              Built-in, ready to attach:
            </p>
            <div className="mt-1 flex flex-wrap gap-1.5">
              {builtIns.map((repo) => (
                <button
                  key={repo.id}
                  type="button"
                  onClick={() => onAttachBuiltIn?.(repo.id)}
                  disabled={attaching}
                  title={repo.description}
                  className="inline-flex items-center gap-1 rounded-full border border-gray-200 dark:border-gray-600 px-2 py-0.5 text-xs text-gray-700 dark:text-gray-200 hover:border-violet-400 dark:hover:border-violet-500 disabled:opacity-50 transition-colors"
                >
                  <Plus className="size-3" aria-hidden="true" />
                  {repo.name}
                </button>
              ))}
            </div>
          </div>
        )}
        <div className="mt-1.5 -ml-2 flex flex-wrap gap-1">
          {missingKnowledge ? (
            <>
              {onAttach && (
                <button type="button" onClick={() => onAttach(category)} className={step}>
                  <Plus className="size-3" aria-hidden="true" /> Attach a repository
                </button>
              )}
              {onGenerate && (
                <button
                  type="button"
                  onClick={() => onGenerate(category)}
                  className={step}
                >
                  <Sparkles className="size-3" aria-hidden="true" /> Generate{" "}
                  {kind.label.toLowerCase()} knowledge
                </button>
              )}
            </>
          ) : kind.place?.kind === "tab" ? (
            <button type="button" onClick={() => onTab(kind.place!.target)} className={step}>
              Open <ArrowRight className="size-3" aria-hidden="true" />
            </button>
          ) : page ? (
            <Link to={page.href} className={step}>
              {page.label} <ArrowRight className="size-3" aria-hidden="true" />
            </Link>
          ) : null}
        </div>
      </div>
    </li>
  );
};
 
export interface AgentContextPanelProps {
  agentId: string;
  /** Opens a tab of the agent's page. */
  onTab: (tab: string) => void;
  /** Opens the page's attach picker on a kind; absent where the viewer may
   *  not. */
  onAttach?: (category: KnowledgeCategoryKey) => void;
  /** Grants a built-in repository a missing kind suggests, in one step. */
  onAttachBuiltIn?: (repositoryId: string) => void;
  /** A grant is under way. */
  attaching?: boolean;
  /** Whether the viewer may add knowledge (and so generate it) here. */
  mayGenerate?: boolean;
}
 
/**
 * What an agent draws on, in eight kinds: its knowledge by kind (Domain,
 * Temporal, Reasoning) and the five that belong to other features — skills,
 * the conversation, memory and the decision record, tools, rules and
 * instructions. Each says what it has, which feature owns it and where to
 * change it; a missing kind of knowledge can be attached (a built-in of that
 * kind in one step) or generated here.
 */
const AgentContextPanel: React.FC<AgentContextPanelProps> = ({
  agentId,
  onTab,
  onAttach,
  onAttachBuiltIn,
  attaching,
  mayGenerate = true,
}) => {
  const { getApiHandler } = useApiHandler();
  const [generating, setGenerating] = useState<KnowledgeCategoryKey | null>(null);
  const { data, isLoading } = useQuery<AgentContext | null>({
    queryKey: [QUERY_KEYS.GET.AGENT_CONTEXT_KINDS, agentId],
    queryFn: () => getApiHandler().agent_router.contextKinds(agentId),
  });
 
  return (
    <SectionCard
      icon={Lightbulb}
      title="What this agent draws on"
      description="Eight kinds of context. Three are knowledge, by the kind of its repositories; the other five each belong to a feature of their own."
    >
      {isLoading ? (
        <SkeletonLines count={4} />
      ) : !data ? (
        <p className="text-xs text-gray-500 dark:text-gray-400">
          What it draws on could not be read just now.
        </p>
      ) : (
        <ul className="divide-y divide-gray-100 dark:divide-gray-700">
          {data.kinds.map((kind: AgentContextKind) => (
            <KindRow
              key={kind.key}
              kind={kind}
              onTab={onTab}
              onAttach={onAttach}
              onAttachBuiltIn={onAttachBuiltIn}
              attaching={attaching}
              onGenerate={mayGenerate ? setGenerating : undefined}
            />
          ))}
        </ul>
      )}
      {generating && (
        <GenerateKnowledgeModal
          initialCategory={generating}
          onClose={() => setGenerating(null)}
        />
      )}
    </SectionCard>
  );
};
 
export default AgentContextPanel;