// Abilities window — per-agent visibility matrix for tools AND skills, // plus external MCP server status (design: private/mcp-hub-design.md // §4.6; skills half added 2026-08-31 on the operator's request — the same // matrix for "which agent may use which skill"). // // Left: agent picker. Main: every tool on this instance (built-in // grouped by toolset, external grouped by MCP server) and, below, every // skill, each with a visibility toggle for the selected agent. The // matrix is always editable (2026-10-08): a click is sent as "these // names on/off" (POST /agents/:name/tools/toggle, …/skills/toggle) and // the server works out the rules — a family's eye writes ONE rule for // the family (`toolset:`, `mcp____*`, skills `*`) so tools // it gains later stay off too; one tool back on inside it becomes an // exception under allow. The UI never touches agent.yaml itself. The // window's internal kind stays `tools` so saved window layouts keep // working. // // Groups are collapsible and carry their own eye (2026-09-10, Leo's // report). One MCP server can contribute dozens of tools, and turning // that server off for an agent meant clicking every single row; the // group eye writes them all in ONE request instead. Collapsed by // default for the same reason — with a big MCP connected the flat list // was hundreds of rows deep before you reached the skills. import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from 'react'; import { AlertTriangle, ChevronDown, ChevronRight, Eye, EyeOff, Moon, Plug, RefreshCw, Sparkles, } from 'lucide-react'; import { api, type AgentInfo, type AgentSkillsResponse, type AgentToolsResponse, type McpStatusResponse, } from '../lib/api'; /** Which groups the user has opened, by group key. Persisted so the * window does not forget the toolsets you actually work with every * time it is closed. Stores the OPEN ones: the default is collapsed, * so an empty/missing entry has to mean "all shut". */ const STORAGE_KEY_EXPANDED = 'somora-abilities-expanded'; /** Group key of the skills section — namespaced so it can never collide * with a toolset or an MCP server called "skills". There IS one: the * toolset that holds `skill_list`/`skill`, which is why the section's * test id says `skills-section` as well. */ const SKILLS_KEY = '\0skills'; function readExpanded(): Set { try { const raw = localStorage.getItem(STORAGE_KEY_EXPANDED); if (!raw) return new Set(); const parsed: unknown = JSON.parse(raw); return Array.isArray(parsed) ? new Set(parsed.filter((k): k is string => typeof k === 'string')) : new Set(); } catch { // Blocked or corrupt storage — everything collapsed, same as a // first visit. return new Set(); } } const groupHeadStyle: React.CSSProperties = { display: 'flex', alignItems: 'center', gap: 6, fontFamily: '"JetBrains Mono", monospace', fontSize: 11, textTransform: 'uppercase', letterSpacing: 1, color: 'var(--text-2)', marginBottom: 4, cursor: 'pointer', userSelect: 'none', }; interface GroupProps { /** Stable key for storage and test ids — `label` may be JSX. */ groupKey: string; label: ReactNode; /** How many rows the group holds, and how many of them are hidden — * the counts are the whole point of a collapsed group. */ total: number; hidden: number; expanded: boolean; onToggleExpanded: () => void; /** Show/hide every row at once. Undefined while a write is in flight. */ onToggleAll: (() => void) | undefined; /** The family is off by a rule, so tools it gains later are off too. */ ruleOff?: boolean; children: ReactNode; } /** One collapsible group: header with expander, group eye and counts. * Exported for tools-render.test.mts. */ export function Group({ groupKey, label, total, hidden, expanded, onToggleExpanded, onToggleAll, ruleOff, children }: GroupProps) { const allHidden = total > 0 && hidden === total; const someHidden = hidden > 0 && hidden < total; return (
{expanded ? : } { // The header itself expands/collapses — the eye must not // also do that on its way through. e.stopPropagation(); onToggleAll?.(); }} style={{ display: 'inline-flex', cursor: onToggleAll ? 'pointer' : 'not-allowed', // Mixed groups sit between the two states: the eye is open // (one more click hides everything) but dimmed, so "some // hidden" is not mistaken for "all visible". color: allHidden ? 'var(--text-2)' : 'var(--accent)', opacity: someHidden ? 0.55 : 1, }} > {allHidden ? : } {total} {hidden > 0 ? ` · ${hidden} hidden` : ''} {ruleOff && ( · off incl. future )}
{expanded && children}
); } export function ToolsWindow() { const [agents, setAgents] = useState([]); const [agent, setAgent] = useState(null); const [data, setData] = useState(null); const [skills, setSkills] = useState(null); const [mcp, setMcp] = useState(null); const [error, setError] = useState(null); const [saving, setSaving] = useState(false); const [expanded, setExpanded] = useState>(readExpanded); useEffect(() => { try { localStorage.setItem(STORAGE_KEY_EXPANDED, JSON.stringify([...expanded])); } catch { // Quota or blocked — the open/closed state just won't survive // this session, which is not worth surfacing. } }, [expanded]); const toggleExpanded = useCallback((key: string) => { setExpanded((prev) => { const next = new Set(prev); if (!next.delete(key)) next.add(key); return next; }); }, []); useEffect(() => { void api .agents() .then((all) => { // External agents' tools belong to their own system. const list = all.filter((a) => a.kind !== 'external'); setAgents(list); setAgent((a) => a ?? list[0]?.name ?? null); }) .catch((err) => setError((err as Error).message)); void api.mcpStatus().then(setMcp).catch(() => setMcp(null)); }, []); // The agent whose data may land in state: a reply for an agent that is // no longer selected is dropped, so a click never works on another // agent's switches (a quick click after switching agents did that). const current = useRef(null); const refresh = useCallback(async (name: string) => { try { const [t, s] = await Promise.all([api.agentTools(name), api.agentSkills(name)]); if (current.current !== name) return; setData(t); setSkills(s); setError(null); } catch (err) { if (current.current === name) setError((err as Error).message); } }, []); useEffect(() => { current.current = agent; setData(null); setSkills(null); if (agent) void refresh(agent); }, [agent, refresh]); const isBuilder = data?.kind === 'builder'; const kindDefaults = useMemo(() => new Set(data?.kindDefaults ?? []), [data]); const groups = useMemo(() => { if (!data) return []; // Two views by kind. A builder: its own set (the kind's defaults) as // one group, everything else under "more" — off until switched on, // and switching on writes the name into agent.yaml `tools.allow`. A // chat agent: the full programme grouped by toolset, minus the three // builder-only tools (task list, question, plan file). if (isBuilder) { const own = data.tools.filter((t) => kindDefaults.has(t.name)); const more = data.tools.filter((t) => !kindDefaults.has(t.name)); return [ ['builder tools', own] as [string, typeof data.tools], ['more (off unless switched on)', more] as [string, typeof data.tools], ]; } const byKey = new Map(); for (const t of data.tools) { if (t.toolset === 'builder') continue; const key = t.mcpServer ? `mcp: ${t.mcpServer}` : t.toolset; const list = byKey.get(key); if (list) list.push(t); else byKey.set(key, [t]); } // Built-in toolsets alphabetical first, MCP servers after. return [...byKey.entries()].sort(([a], [b]) => { const am = a.startsWith('mcp: ') ? 1 : 0; const bm = b.startsWith('mcp: ') ? 1 : 0; return am - bm || a.localeCompare(b); }); }, [data, isBuilder, kindDefaults]); /** Send one click and reload. One request however many names — a * family of 60 MCP tools is a single call, and its eye (`group`) * lets the server write one rule for the whole family. */ const send = useCallback( async (which: 'tools' | 'skills', names: string[], visible: boolean, group: boolean) => { // Only on the switches of the agent that is selected. const shown = which === 'tools' ? data?.agent : skills?.agent; if (!agent || saving || names.length === 0 || shown !== agent) return; setSaving(true); try { if (which === 'tools') await api.toggleAgentTools(agent, names, visible, group); else await api.toggleAgentSkills(agent, names, visible, group); await refresh(agent); } catch (err) { setError((err as Error).message); } finally { setSaving(false); } }, [agent, saving, refresh, data, skills], ); // A row's eye flips that row. A group's eye hides the group while // anything in it is still visible, otherwise brings all of it back — // so a half-hidden group goes dark in one click, back in a second. const toggle = useCallback( (toolName: string, currentlyVisible: boolean) => void send('tools', [toolName], !currentlyVisible, false), [send], ); const toggleGroup = useCallback( (list: { name: string; visible: boolean }[]) => void send('tools', list.map((t) => t.name), list.every((t) => !t.visible), true), [send], ); const toggleSkill = useCallback( (skillName: string, currentlyVisible: boolean) => void send('skills', [skillName], !currentlyVisible, false), [send], ); const toggleAllSkills = useCallback(() => { const list = skills?.skills ?? []; void send('skills', list.map((s) => s.name), list.every((s) => !s.visible), true); }, [send, skills]); /** The family rule a chat agent's group is switched off by, if any. */ const ruleOff = useCallback( (list: { toolset: string; mcpServer?: string }[]) => { if (isBuilder || list.length === 0) return false; const t = list[0]!; const rule = t.mcpServer ? `mcp__${t.mcpServer}__*` : `toolset:${t.toolset}`; return !!data?.gating?.deny.includes(rule); }, [data, isBuilder], ); const toolsLocked = saving; const skillsLocked = saving; const handWritten = data?.handWrittenRules ?? []; return (
{/* Agent picker */}
{agents.map((a) => (
setAgent(a.name)} style={{ padding: '6px 8px', borderRadius: 6, cursor: 'pointer', background: agent === a.name ? 'var(--bg-3)' : 'transparent', color: agent === a.name ? 'var(--text-1)' : 'var(--text-2)', }} > {a.icon ? `${a.icon} ` : ''} {a.name}
))}
{/* Matrix */}
{error && (
{error}
)} {handWritten.length > 0 && (
This agent's{' '} agent.yaml also carries hand-written rules ({handWritten.join(', ')}). They still apply; the switches below show their effect and stay usable.
)} {!data && !error &&
Loading…
} {groups.map(([group, list]) => ( ))} {/* Skills — same matrix, same rules, one more group. */} {skills && (
skills } total={skills.skills.length} hidden={skills.skills.filter((s) => !s.visible).length} expanded={expanded.has(SKILLS_KEY)} onToggleExpanded={() => toggleExpanded(SKILLS_KEY)} onToggleAll={skillsLocked || skills.skills.length === 0 ? undefined : toggleAllSkills} ruleOff={skills.kind !== 'builder' && !!skills.gating?.deny.includes('*')} > {skills.skills.length === 0 && (
No skills installed — see docs/skills.md.
)} {skills.skills.map((s) => (
toggleSkill(s.name, s.visible)} style={{ cursor: skillsLocked ? 'not-allowed' : 'pointer', color: s.visible ? 'var(--accent)' : 'var(--text-2)', display: 'inline-flex', }} > {s.visible ? : } {s.name} {!s.available && ( unavailable )}
))}
)}
{/* MCP server status */}
MCP servers
{!mcp?.enabled && (
None configured — add mcp.servers to config.yaml.
)} {mcp?.enabled && Object.entries(mcp.servers).map(([name, s]) => (
{name} void api .mcpReconnect(name) .then(() => api.mcpStatus().then(setMcp)) .catch((err) => setError((err as Error).message)) } style={{ marginLeft: 'auto', cursor: 'pointer', color: 'var(--text-2)' }} >
{s.state} · {s.toolCount} tool{s.toolCount === 1 ? '' : 's'} {s.transport ? ` · ${s.transport}` : ''}
{s.lastError && (
{s.lastError.slice(0, 120)}
)}
))}
); }