import { useCallback, useMemo, useState } from "react"; import { Alert, Badge, Button, Card, Code, Flex, Group, NavLink, ScrollArea, Stack, Table, Text, TextInput, } from "@mantine/core"; import { MdRefresh, MdSearch, MdVerifiedUser } from "react-icons/md"; import type { SkillEntry, SkillResource, } from "@inspector/core/mcp/skillsSchemas.js"; import { DYNAMIC_RESOURCES } from "@inspector/core/mcp/skillsSchemas.js"; import { checkSkillConformance, skillDisplayName, totalSkillBytes, verifySkillResource, type SkillIssue, type SkillVerification, } from "@inspector/core/mcp/skills.js"; import { ContentViewer } from "../../elements/ContentViewer/ContentViewer"; import { useValueChange } from "../../../hooks/useValueChange"; import { skillFileBytes, type SkillFileContents, } from "../../../utils/skillFileBytes"; /** Per-file verification progress, keyed by the manifest entry's URI. */ type FileState = | { status: "pending" } | { status: "done"; verification: SkillVerification } | { status: "error"; message: string }; export interface SkillsScreenProps { skills: SkillEntry[]; /** Pages the last `skills/list` walk took; shown so pagination is visible. */ pageCount: number; /** A failed list walk, rendered above the sidebar list. */ loadError?: Error | null; ui: SkillsUiState; onUiChange: (next: SkillsUiState) => void; onRefreshList: () => void; /** Fetch one skill file's contents via `resources/read`, on demand. */ onReadSkillFile: (uri: string) => Promise; } /** * Selection and the sidebar search — controlled by the parent (App) as one * object so they persist across tab navigation within a live session (#1417). * Verification results stay local to the screen: they are derived from a live * `resources/read` round trip that is torn down with the screen, so persisting * them would restore a verdict without the fetch that produced it. */ export interface SkillsUiState { selectedSkillUri?: string; search: string; } const ScreenLayout = Flex.withProps({ variant: "screen", h: "calc(100dvh - var(--app-shell-header-height, 0px) - var(--app-shell-footer-height, 0px))", gap: "md", p: "xl", align: "flex-start", }); const Sidebar = Stack.withProps({ w: 340, flex: "0 0 auto", }); const SidebarCard = Card.withProps({ withBorder: true, padding: "lg", }); const DetailCard = Card.withProps({ withBorder: true, padding: "lg", flex: 1, h: "100%", }); const DetailScroll = ScrollArea.withProps({ type: "auto", scrollbars: "y", offsetScrollbars: true, h: "100%", }); const EmptyState = Text.withProps({ c: "dimmed", ta: "center", py: "xl", }); const ControlsRow = Group.withProps({ justify: "space-between", wrap: "nowrap", gap: "sm", }); const SearchInput = TextInput.withProps({ size: "xs", flex: 1, leftSection: , }); const RefreshButton = Button.withProps({ variant: "subtle", size: "compact-xs", leftSection: , }); const VerifyButton = Button.withProps({ variant: "light", size: "compact-sm", leftSection: , }); // A `Text` renders a `

`, so a section heading must never *wrap* the count // badge beside it — a `

` inside a `

` is invalid HTML that React reports // as a hydration error and the Storybook run fails on. Heading and badge sit // side by side in an `InlineRow` instead. const SectionHeading = Text.withProps({ fw: 600, size: "sm", }); const MonoCaption = Text.withProps({ size: "xs", c: "dimmed", ff: "monospace", }); const DetailStack = Stack.withProps({ gap: "md", }); const IssueStack = Stack.withProps({ gap: "xs", }); const ManifestTable = Table.withProps({ striped: true, withTableBorder: true, fz: "xs", verticalSpacing: "xs", }); const CountBadge = Badge.withProps({ size: "xs", variant: "light", }); // A tight, non-wrapping row — used for the detail-pane action pair and for the // badge + Verify button inside a manifest cell. const InlineRow = Group.withProps({ gap: "xs", wrap: "nowrap", }); const RowVerifyButton = Button.withProps({ variant: "subtle", size: "compact-xs", }); const SkillTitle = Text.withProps({ fw: 600, size: "lg", truncate: true, }); /** Colour token for a finding's severity — errors read as failures. */ function issueColor(issue: SkillIssue): string { return issue.severity === "error" ? "red" : "yellow"; } /** Colour token for a per-file verification verdict. */ function verificationColor(status: SkillVerification["status"]): string { if (status === "verified") return "green"; if (status === "mismatch") return "red"; return "yellow"; } /** * The short label a manifest row shows for its verdict. `—` (not yet run) is * deliberately distinct from `unverifiable` (run, but nothing to compare * against): conflating them would hide the fact that a server advertised no * digest. */ function verificationLabel(state: FileState | undefined): string { if (!state) return "—"; if (state.status === "pending") return "checking…"; if (state.status === "error") return "read failed"; return state.verification.status; } /** `sha256:abcd…wxyz`, so a long digest stays readable in a table cell. */ function shortDigest(digest: string | undefined): string { if (!digest) return "—"; return digest.length <= 24 ? digest : `${digest.slice(0, 16)}…`; } /** * The Skills screen (SEP-2640) — a conformance view, not just a list. * * The sidebar lists the skills the server enumerated; the detail pane shows the * entry's frontmatter, every conformance finding * (`checkSkillConformance`), and the resource manifest with a per-file * verification verdict. Verification is on demand: SEP-2640 says a * `resources/read` of a skill file is not a load and confers no standing, so * the Inspector fetches only what the user asks it to. */ export function SkillsScreen({ skills, pageCount, loadError, ui, onUiChange, onRefreshList, onReadSkillFile, }: SkillsScreenProps) { const { selectedSkillUri, search } = ui; const [fileStates, setFileStates] = useState>({}); const [preview, setPreview] = useState(null); const [previewError, setPreviewError] = useState(null); // Changing the selection invalidates every verdict and the SKILL.md preview: // they belong to the skill that was selected. Adjusted DURING RENDER via // `useValueChange` rather than in an effect, so the new skill never paints // for a frame carrying the previous one's verification results. useValueChange(selectedSkillUri, () => { setFileStates({}); setPreview(null); setPreviewError(null); }); const filtered = useMemo(() => { const needle = search.trim().toLowerCase(); if (!needle) return skills; return skills.filter( (skill) => skillDisplayName(skill).toLowerCase().includes(needle) || skill.uri.toLowerCase().includes(needle), ); }, [skills, search]); const selected = useMemo( () => skills.find((skill) => skill.uri === selectedSkillUri), [skills, selectedSkillUri], ); const issues = useMemo( () => (selected ? checkSkillConformance(selected) : []), [selected], ); const manifest: SkillResource[] = useMemo( () => selected && selected.resources !== DYNAMIC_RESOURCES ? selected.resources : [], [selected], ); const verifyFile = useCallback( async (resource: SkillResource) => { setFileStates((prev) => ({ ...prev, [resource.uri]: { status: "pending" }, })); try { const contents = await onReadSkillFile(resource.uri); const verification = await verifySkillResource( resource, skillFileBytes(contents), ); setFileStates((prev) => ({ ...prev, [resource.uri]: { status: "done", verification }, })); } catch (err) { setFileStates((prev) => ({ ...prev, [resource.uri]: { status: "error", message: err instanceof Error ? err.message : String(err), }, })); } }, [onReadSkillFile], ); const verifyAll = useCallback(() => { // Held rather than floated: each `verifyFile` owns its own failures (it // records them as per-file state), and this handler cannot be async, so the // settled promise is discarded explicitly at one place instead of per file. void Promise.all(manifest.map((resource) => verifyFile(resource))); }, [manifest, verifyFile]); const showSkillMd = useCallback(() => { if (!selected) return; // A click handler cannot await, and this chain terminates in its own // `catch` that surfaces the message in the preview slot. void onReadSkillFile(selected.uri) .then((contents) => { setPreview(contents); setPreviewError(null); }) .catch((err: unknown) => { setPreview(null); setPreviewError(err instanceof Error ? err.message : String(err)); }); }, [onReadSkillFile, selected]); const errorCount = issues.filter((i) => i.severity === "error").length; const warningCount = issues.length - errorCount; return ( // `data-*` readiness contract for the headless tab smoke (#2148); see // clients/web/README.md#core-tab-automation-contract. onUiChange({ ...ui, search: event.currentTarget.value }) } /> Refresh {loadError && ( {loadError.message} )} {filtered.length === 0 ? ( No skills ) : ( filtered.map((skill) => { const skillIssues = checkSkillConformance(skill); const errors = skillIssues.filter( (i) => i.severity === "error", ).length; return ( onUiChange({ ...ui, selectedSkillUri: skill.uri }) } rightSection={ skillIssues.length > 0 ? ( 0 ? "red" : "yellow"}> {skillIssues.length} ) : undefined } /> ); }) )} {skills.length} skill(s) over {pageCount} page(s) {!selected ? ( Select a skill to view details ) : ( {skillDisplayName(selected)} {selected.uri} {selected.frontmatter.description && ( {selected.frontmatter.description} )} Conformance 0 ? "red" : "green"}> {errorCount} error(s), {warningCount} warning(s) {issues.length === 0 ? ( No structural issues found in this entry. ) : ( {issues.map((issue) => ( {issue.message} {issue.resourceUri && ( {issue.resourceUri} )} ))} )} Resources {manifest.length} file(s), {totalSkillBytes(manifest)}{" "} bytes View SKILL.md Verify all {selected.resources === DYNAMIC_RESOURCES ? ( This skill declares{" "} resources: "dynamic" — its files are generated, so no manifest is advertised and integrity cannot be verified. ) : ( URI Size Digest Verification {manifest.map((resource) => { const state = fileStates[resource.uri]; const color = state?.status === "done" ? verificationColor(state.verification.status) : state?.status === "error" ? "red" : "gray"; return ( {resource.uri} {resource.size ?? "—"} {shortDigest(resource.digest)} {verificationLabel(state)} void verifyFile(resource)} > Verify ); })} )} {manifest.map((resource) => { const state = fileStates[resource.uri]; if (state?.status === "done") { const { verification } = state; if (verification.status === "mismatch") { return ( {resource.uri} expected {verification.expectedDigest} actual {verification.actualDigest} ); } return null; } if (state?.status === "error") { return ( {resource.uri} {state.message} ); } return null; })} {previewError && ( {previewError} )} {preview && ( SKILL.md )} Frontmatter )} ); }