"use client"; import Tooltip from "@/shared/ui/Tooltip"; import { useStagedSettings } from "@/features/settings/store/useStagedSettings"; import { useCallback, useEffect, useMemo, useState } from "react"; import { ChevronDown, Loader2, Lock, Search, Wrench, X } from "lucide-react"; import Link from "next/link"; import { useTranslation } from "react-i18next"; import { useSettings } from "@/features/settings/store/SettingsStore"; import { SettingsPageHeader } from "@/components/settings/shared"; import { apiFetch, apiUrl } from "@/lib/api"; import { toolAvailabilityCopy, toolEffectiveEnabled, } from "@/lib/tool-availability"; type ToolParameter = { name: string; type: string; description: string; required: boolean; default: unknown; enum: string[] | null; }; type ToolHints = { short_description: string; when_to_use: string; input_format: string; guideline: string; note: string; phase: string; aliases: { name: string; description: string; phase: string }[]; }; type BuiltinTool = { name: string; description: string; parameters: ToolParameter[]; // The API types this as a language-keyed map; only ``en`` is guaranteed. hints: Partial> & { en: ToolHints }; aliases: string[]; toggleable: boolean; enabled: boolean; // ``coming_soon`` tools are listed for visibility but the chat agent // cannot invoke them. The settings UI surfaces them with a locked-off // toggle and a "Coming soon" badge. coming_soon?: boolean; // The capability that owns this tool (e.g. "solve" / "mastery"), or null // for a plain system built-in. Owned tools render in their own section // below the built-in tools. capability?: string | null; // Runtime readiness is independent of the saved composer preference. available?: boolean; unavailable_reason?: string | null; }; type ToolsResponse = { tools: BuiltinTool[]; enabled_optional_tools: string[]; }; type ToolSection = { key: string; label: string; hint: string; tools: BuiltinTool[]; }; // Display labels for capability-owned tool sections, keyed by the backend's // capability id. Falls back to the raw id for any unmapped capability. const CAPABILITY_LABELS: Record = { solve: { zh: "深度解题", en: "Deep Solve" }, mastery: { zh: "精通路径", en: "Mastery Path" }, }; export default function ToolsSettingsPage() { const { t } = useTranslation(); const { language, draftRevision } = useSettings(); const hintLanguage = language === "zh" ? "zh" : "en"; const localizedHints = useCallback((tool: BuiltinTool): ToolHints => { const hints = tool.hints[language] ?? tool.hints[hintLanguage] ?? tool.hints.en; if (language === "en" || language === "zh" || tool.hints[language]) return hints; const translate = (text: string) => text ? t(text, { defaultValue: text }) : text; return { ...hints, short_description: translate(hints.short_description), when_to_use: translate(hints.when_to_use), input_format: translate(hints.input_format), guideline: translate(hints.guideline), note: translate(hints.note), aliases: hints.aliases.map(alias => ({ ...alias, description: translate(alias.description) })), }; }, [language, hintLanguage, t]); const [tools, setTools] = useState(null); const [error, setError] = useState(null); const [expanded, setExpanded] = useState>(new Set()); const [liveTools, setLiveTools] = useState({ enabled_tools: [] as string[] }); const [toolDraft, setToolDraft] = useStagedSettings("tools", liveTools, setLiveTools); const enabled = new Set(toolDraft.enabled_tools); const pending = new Set(); const [query, setQuery] = useState(""); useEffect(() => { let cancelled = false; (async () => { try { const res = await apiFetch(apiUrl("/api/tools")); if (!res.ok) throw new Error(`HTTP ${res.status}`); const payload = (await res.json()) as ToolsResponse; if (!cancelled) { setTools(payload.tools); setLiveTools({ enabled_tools: (payload.enabled_optional_tools ?? []).slice().sort() }); } } catch (err) { if (!cancelled) { setError(err instanceof Error ? err.message : String(err)); } } })(); return () => { cancelled = true; }; }, [draftRevision]); const handleToggleEnabled = (toolName: string) => { setToolDraft((current) => { const next = new Set(current.enabled_tools); if (next.has(toolName)) next.delete(toolName); else next.add(toolName); return { enabled_tools: Array.from(next).sort() }; }); }; const sections = useMemo(() => { if (!tools) return null; const zh = language === "zh"; // Buckets: toggleable (体验增强) first, then locked-on built-ins, then one // section per capability for its owned tools. Backend order is preserved // within each bucket (mirrors USER_TOGGLEABLE_TOOL_NAMES / the // BUILTIN_TOOL_TYPES registration order). Coming-soon tools share the // toggleable bucket — same concept, just temporarily unavailable. const experience: BuiltinTool[] = []; const builtin: BuiltinTool[] = []; const capabilities = new Map(); for (const tool of tools) { if (tool.capability) { const list = capabilities.get(tool.capability) ?? []; list.push(tool); capabilities.set(tool.capability, list); } else if (tool.coming_soon) { experience.push(tool); } else { (tool.toggleable ? experience : builtin).push(tool); } } const out: ToolSection[] = []; if (experience.length) { out.push({ key: "experience", label: zh ? "体验增强" : t("Experience Enhancement"), hint: zh ? "用户可选;按需为 chat agent 开启或关闭。" : t("User-toggleable. Switch on or off to shape the chat agent's behavior."), tools: experience, }); } if (builtin.length) { out.push({ key: "builtin", label: zh ? "内置工具" : t("Built-in Tools"), hint: zh ? "Chat agent 在需要时自动挂载,无需手动开关。" : t("Mounted automatically by the chat agent when needed. Not user-toggleable."), tools: builtin, }); } for (const [cap, list] of capabilities) { const capLabel = CAPABILITY_LABELS[cap]; const label = capLabel ? (zh ? capLabel.zh : t(capLabel.en)) : cap; out.push({ key: `cap:${cap}`, label: zh ? `${label} · 能力工具` : t("{{label}} · Capability Tools", { label }), hint: zh ? "该能力的专属工具,仅在此能力运行时挂载。" : t("Tools specific to this capability; mounted only when it runs."), tools: list, }); } return out; }, [tools, language, t]); const filteredSections = useMemo(() => { if (!sections) return null; const needle = query.trim().toLocaleLowerCase(); if (!needle) return sections; return sections .map((section) => ({ ...section, tools: section.tools.filter((tool) => { const hints = localizedHints(tool); const alternateHints = tool.hints[hintLanguage === "zh" ? "en" : "zh"] ?? tool.hints.en; const searchableText = [ tool.name, tool.description, ...tool.aliases, hints.short_description, hints.when_to_use, hints.input_format, hints.guideline, hints.note, ...hints.aliases.flatMap((alias) => [ alias.name, alias.description, alias.phase, ]), alternateHints.short_description, alternateHints.when_to_use, alternateHints.input_format, alternateHints.guideline, alternateHints.note, ...tool.parameters.flatMap((parameter) => [ parameter.name, parameter.type, parameter.description, ...(parameter.enum ?? []), ]), ] .filter(Boolean) .join(" ") .toLocaleLowerCase(); return searchableText.includes(needle); }), })) .filter((section) => section.tools.length > 0); }, [hintLanguage, localizedHints, query, sections]); const toggleExpanded = (name: string) => { setExpanded((prev) => { const next = new Set(prev); if (next.has(name)) next.delete(name); else next.add(name); return next; }); }; return (
setQuery(event.target.value)} placeholder={t("Search tools")} aria-label={t("Search tools")} className="w-full rounded-xl border border-[var(--border)] bg-[var(--card)] py-2.5 pl-10 pr-10 text-[13px] text-[var(--foreground)] outline-none transition-colors placeholder:text-[var(--muted-foreground)]/60 focus:border-[var(--ring)] focus:ring-2 focus:ring-[var(--ring)]/20" spellCheck={false} /> {query && ( )}
{error && (
{t("Failed to load tools")}: {error}
)} {!tools && !error && (
{t("Loading...")}
)} {filteredSections && query.trim() && filteredSections.length === 0 && (
{t("No tools match “{{query}}”.", { query: query.trim() })}
)} {filteredSections && filteredSections.length > 0 && (
{filteredSections.map((section) => { const list = section.tools; if (list.length === 0) return null; return (

{section.label}

{section.hint}

{list.length}
{list.map((tool, idx) => { const isOpen = expanded.has(tool.name); const hints = localizedHints(tool); const isPending = pending.has(tool.name); const isComingSoon = !!tool.coming_soon; const isAvailable = tool.available !== false; const availabilityCopy = !isAvailable ? toolAvailabilityCopy( tool.unavailable_reason, language === "zh" ? "zh" : "en", ) : null; const availability = availabilityCopy && language !== "zh" ? { ...availabilityCopy, badge: t(availabilityCopy.badge), detail: t(availabilityCopy.detail), } : availabilityCopy; const isEnabled = toolEffectiveEnabled( tool.toggleable ? enabled.has(tool.name) : true, isAvailable, isComingSoon, ); return (
0 ? "border-t border-[var(--border)]/50" : undefined } >
{isComingSoon ? ( { /* locked */ }} label={ language === "zh" ? "敬请期待" : t("Coming soon") } /> ) : !isAvailable ? ( { /* runtime unavailable */ }} label={ availability?.badge ?? t("Not configured") } /> ) : tool.toggleable ? ( handleToggleEnabled(tool.name)} label={t(isEnabled ? "On" : "Off")} /> ) : ( {t("Always on")} )}
{isOpen && (
{hints.when_to_use && ( )} {hints.input_format && ( )} {hints.guideline && ( )} {hints.note && ( )} {tool.parameters.length > 0 && (
{t("Parameters")}
    {tool.parameters.map((p) => (
  • {p.name} {p.type} {p.required ? "" : ` · ${t("optional")}`} {p.description && ( — {p.description} )}
  • ))}
)}
)}
); })}
); })}
)}
); } function ToolToggle({ checked, disabled, onChange, label, }: { checked: boolean; disabled: boolean; onChange: () => void; label: string; }) { return ( ); } function Field({ label, body, mono, }: { label: string; body: string; mono?: boolean; }) { return (
{label}

{body}

); }