All files / src/apps/user/pages Knowledge.tsx

100% Statements 31/31
100% Branches 24/24
100% Functions 9/9
100% Lines 29/29

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                                                3x             3x                   3x 42x 42x 42x 42x   42x       42x 24x 1x         42x         42x     12x   42x         12x       42x 34x 34x       34x 34x 7x 7x 34x 42x 11x   42x           42x                             42x                                              
import KnowledgeGrid from "@/apps/user/pages/knowledge/KnowledgeGrid";
import RepositoriesGrid from "@/apps/user/pages/knowledge/RepositoriesGrid";
import {
  SectionTabBar,
  SectionTabConfig,
} from "@/components/nav/SectionNav/SectionNav";
import MainPage from "@/components/page/MainPage/MainPage";
import { isSystemManaged } from "@/objects/Doc";
import { Knowledge, KnowledgeRepository } from "@/objects/Knowledge";
import { INCLUDE_SYSTEM, QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useQuery } from "@tanstack/react-query";
import { BookCopy, FolderGit2 } from "lucide-react";
import React, { useEffect, useMemo } from "react";
import { useSearchParams } from "react-router-dom";
 
// ─── Section types ────────────────────────────────────────────────────────────
 
// "repositories" (the tab default) is the primary way knowledge is granted to
// agents — agents integrate whole repositories, never individual sources, so
// this is where most users should land. "sources" lists the underlying
// knowledge bases that get grouped into repositories.
type KnowledgeTab = "repositories" | "sources";
 
const TABS: KnowledgeTab[] = ["repositories", "sources"];
 
// Sources used to be a sidebar of one leaf per source type sitting under the
// tab bar — a second level of navigation for what is really one list narrowed
// five ways. The leaves are the grid's own type filter now, and that filter
// lives in the URL, so `?section=sources&type=graph` is the link for "the
// graph sources". These older `?section=` values are rewritten to it.
const LEGACY_TYPE_SECTIONS: Record<string, string> = {
  file: "file",
  graph: "graph",
  web: "web",
  db: "db",
  api: "api",
};
 
// ─── Knowledge ────────────────────────────────────────────────────────────────
 
const KnowledgePage: React.FC = () => {
  const { getApiHandler } = useApiHandler();
  const [searchParams, setSearchParams] = useSearchParams();
  const rawSection = searchParams.get("section") ?? "";
  const legacyType: string | undefined = LEGACY_TYPE_SECTIONS[rawSection];
  const activeTab: KnowledgeTab =
    legacyType || TABS.includes(rawSection as KnowledgeTab)
      ? ((legacyType ? "sources" : rawSection) as KnowledgeTab)
      : "repositories";
 
  useEffect(() => {
    if (!legacyType) return;
    setSearchParams({ section: "sources", type: legacyType }, { replace: true });
  }, [legacyType, setSearchParams]);
 
  // Switching tabs drops the previous tab's filters: they name fields the
  // other collection does not have.
  const setTab = (tab: KnowledgeTab) => setSearchParams({ section: tab });
 
  // The user's sources and the platform's in one request (the same cache
  // entry the Repositories tab names its built-in members from), listed
  // apart. Every count on the page is the user's own.
  const { data: knowledgeData, isLoading } = useQuery({
    queryKey: [QUERY_KEYS.GET.KNOWLEDGE, INCLUDE_SYSTEM],
    queryFn: () =>
      getApiHandler().knowledge_router.list(undefined, { includeSystem: true }),
  });
  const { data: repositoryData, isLoading: repositoriesLoading } = useQuery<
    KnowledgeRepository[]
  >({
    queryKey: [QUERY_KEYS.GET.REPOSITORIES, INCLUDE_SYSTEM],
    queryFn: () =>
      getApiHandler().repository_router.list({ includeSystem: true }),
  });
 
  const { knowledge, repositories, builtInSources, builtInRepositoryNames } =
    useMemo(() => {
      const allKnowledge = (knowledgeData as Knowledge[] | null) ?? [];
      const allRepositories: KnowledgeRepository[] = repositoryData ?? [];
      // A built-in source is listed for the built-in repository it belongs
      // to — a source a release dropped from its repository is not one the
      // user can use, so it is not offered.
      const names = new Map<string, string[]>();
      for (const repo of allRepositories.filter(isSystemManaged))
        for (const id of repo.knowledge_ids)
          names.set(id, [...(names.get(id) ?? []), repo.name]);
      return {
        knowledge: allKnowledge.filter((kb) => !isSystemManaged(kb)),
        repositories: allRepositories.filter((repo) => !isSystemManaged(repo)),
        builtInSources: allKnowledge.filter(
          (kb) => isSystemManaged(kb) && names.has(kb.id),
        ),
        builtInRepositoryNames: names,
      };
    }, [knowledgeData, repositoryData]);
 
  const tabs: SectionTabConfig<KnowledgeTab>[] = [
    {
      key: "repositories",
      label: "Repositories",
      Icon: FolderGit2,
      count: repositories.length,
    },
    {
      key: "sources",
      label: "Sources",
      Icon: BookCopy,
      count: knowledge.length,
    },
  ];
 
  return (
    <MainPage
      title="Knowledge"
      description="Custom knowledge you can securely and privately integrate into your LLM workflows. Group sources into repositories — the unit agents actually integrate — for focused, high-quality retrieval."
    >
      <SectionTabBar tabs={tabs} active={activeTab} onChange={setTab} />
 
      {activeTab === "repositories" && <RepositoriesGrid />}
 
      {activeTab === "sources" && (
        <KnowledgeGrid
          knowledge={knowledge}
          builtIn={builtInSources}
          builtInRepositoryNames={builtInRepositoryNames}
          loading={isLoading || repositoriesLoading}
          description="The individual knowledge bases behind your repositories — uploaded documents, knowledge graphs, crawled websites, connected databases and API sources. Filter by source type to work on one kind at a time."
        />
      )}
    </MainPage>
  );
};
 
export default KnowledgePage;