import { Cross2Icon } from "@radix-ui/react-icons"; import { Badge, Heading, IconButton, Popover } from "@radix-ui/themes"; import { type RefObject, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"; import type { ToolView } from "../model.js"; import { Data, type DataState, Metadata } from "./components.js"; import { NavigationTabs } from "./navigation-tabs.js"; import { PaneDrawer, useNarrow } from "./panes.js"; import { boundedSearch, searchNeedle } from "./search.js"; import { ParameterSchema } from "./tool-inspector.js"; import { compactGroupLabel, type InventoryGrouping, type InventoryRow, inventoryGroups, inventoryRows, type ToolColumn, toolCell, toolColumns, } from "./tool-inventory.js"; import "./tool-explorer.css"; const eligibility = "Eligibility through Pi's nested-tool API, used by codemode. Runner availability, permission and execution success are not verified. This is not observed usage."; const detailTabs = [ { value: "overview", label: "Overview" }, { value: "parameters", label: "Parameters" }, { value: "source", label: "Source" }, ] as const; type DetailTab = (typeof detailTabs)[number]["value"]; interface DetailReading { tab: DetailTab; schema: DataState; source: DataState; } function initialReading(): DetailReading { return { tab: "overview", schema: { open: false, format: "tree" }, source: { open: false, format: "tree" } }; } function ToolDetails({ tool, reading, change, scroll, parameterExpansion, scope, }: { tool: ToolView; reading: DetailReading; change(reading: DetailReading): void; scroll: RefObject; parameterExpansion: Map; scope: string; }) { const { tab } = reading; const content = useRef(null); const previousTab = useRef(tab); useLayoutEffect(() => { if (previousTab.current !== tab) scroll.current = 0; previousTab.current = tab; if (content.current) content.current.scrollTop = scroll.current; }, [scroll, tab]); return (
{ scroll.current = event.currentTarget.scrollTop; }} > {tool.name}
{tool.exposure} {tool.callable ? "Codemode eligible" : "Not eligible"} {tool.active ? "Active" : "Inactive"}
{tool.metadataTruncated &&

Tool metadata truncated.

} { change({ ...reading, tab: value as DetailTab, source: value === "source" ? { ...reading.source, open: true } : reading.source, }); }} />
{tab === "overview" && ( <>

{tool.description || "Description unavailable."}

{eligibility}

)} {tab === "parameters" && ( <> {tool.schema.truncated && (

Captured schema truncated; parameter documentation may be partial.

)} )} {(tab === "overview" || tab === "source") && ( change({ ...reading, source })} /> )} {tab !== "source" && ( change({ ...reading, schema })} /> )}
); } function SourceNavigation({ rows, grouping, selected, change, }: { rows: InventoryRow[]; grouping: InventoryGrouping; selected: string; change(id: string): void; }) { const groups = inventoryGroups(rows); const categories = grouping === "exposure" ? [{ label: "Exposure", kinds: ["unknown"] }] : [ { label: "Built-in", kinds: ["builtin"] }, { label: "Namespaces", kinds: ["namespace"] }, { label: "Extensions", kinds: ["extension"] }, { label: "Packages", kinds: ["package"] }, { label: "Unknown", kinds: ["unknown"] }, ]; return ( ); } export function ToolExplorer({ tools, active }: { tools: ToolView[]; active: boolean }) { const narrow = useNarrow(); const [grouping, setGrouping] = useState("namespace"); const [source, setSource] = useState("all"); const [query, setQuery] = useState(""); const [exposure, setExposure] = useState("all"); const [columns, setColumns] = useState>(() => new Set(["exposure", "codemode", "active"])); const [selected, setSelected] = useState(); const [detailsOpen, setDetailsOpen] = useState(false); const [sourcesOpen, setSourcesOpen] = useState(false); const [columnsOpen, setColumnsOpen] = useState(false); const [reading, setReading] = useState(initialReading); const detailScroll = useRef(0); const parameterExpansion = useRef(new Map()); const activeOwner = useRef(active); activeOwner.current = active; const pendingDetailFocus = useRef(false); const reopenTrigger = useRef(null); const searchInput = useRef(null); const sourceTrigger = useRef(null); const detailTrigger = useRef(null); const rows = useMemo(() => inventoryRows(tools, grouping), [tools, grouping]); const groups = useMemo(() => inventoryGroups(rows), [rows]); const selectedSource = groups.find(({ group }) => group.id === source)?.group; // A disappearing source returns to All; a disappearing tool never selects a replacement by position. const effectiveSource = selectedSource ? source : "all"; const current = rows.find((row) => row.id === selected); useEffect(() => { if (source !== "all" && !selectedSource) setSource("all"); if (selected && !current) { setSelected(undefined); setDetailsOpen(false); if (active) searchInput.current?.focus({ preventScroll: true }); } if (exposure !== "all" && !tools.some((tool) => tool.exposure === exposure)) setExposure("all"); }, [source, selectedSource, selected, current, active, exposure, tools]); useEffect(() => { if (!narrow) setSourcesOpen(false); if (!active) { setSourcesOpen(false); if (narrow) setDetailsOpen(false); setColumnsOpen(false); } }, [active, narrow]); const needle = searchNeedle(query); const visible = rows.filter( ({ tool, group }) => (effectiveSource === "all" || group.id === effectiveSource) && (exposure === "all" || tool.exposure === exposure) && [tool.name, tool.description, tool.namespace, tool.sourceGroup?.label].some((value) => value?.toLowerCase().includes(needle), ), ); function selectSource(id: string) { setSource(id); setSourcesOpen(false); } const restoreDetailFocus = useCallback(() => { if (!activeOwner.current) return; for (const target of [detailTrigger.current, reopenTrigger.current, searchInput.current]) { if (target?.isConnected && !target.disabled && target.getClientRects().length) { target.focus({ preventScroll: true }); break; } } }, []); function closeDetails() { pendingDetailFocus.current = true; setDetailsOpen(false); } useEffect(() => { if (!detailsOpen && pendingDetailFocus.current) { pendingDetailFocus.current = false; restoreDetailFocus(); } }, [detailsOpen, restoreDetailFocus]); const sources = ; const details = current && ( ); return (

Current active ≠ provider-visible. Codemode shows nested-call eligibility, not observed usage.

{!narrow && }
{selectedSource ? compactGroupLabel(selectedSource) : "All tools"} {visible.length} / {tools.length} tools
{narrow && ( )} setQuery(boundedSearch(event.target.value))} /> { if (!activeOwner.current) event.preventDefault(); }} >
Visible columns {toolColumns.map(({ id, label }) => ( ))}
{current && !detailsOpen && ( )}
{/* biome-ignore lint/a11y/noNoninteractiveTabindex: The bounded table region needs keyboard focus for native horizontal and vertical scrolling. */}
{toolColumns .filter(({ id }) => columns.has(id)) .map(({ id, label }) => ( ))} {visible.map(({ id, tool }) => ( {toolColumns .filter(({ id }) => columns.has(id)) .map(({ id: column }) => ( ))} ))}
Current tools. Select a name to inspect its metadata.
Name {label}
{toolCell(tool, column)}
{!tools.length ? (

No tools in this snapshot.

) : ( !visible.length && (

No tools match these filters.

) )}

Codemode runner availability is not verified. Namespace/source metadata does not prove MCP server ownership.

{!narrow && detailsOpen && current && ( )}
{narrow && ( <>
{sources}
{details}
)}
); }