Open-source, local-first AI agent for real work.
TypeScript agent-toolsai-agentbyokcoding-agentcross-platformdesktop-appdeveloper-toolselectronharnessknowledge-basellmwikilocal-firstmcpplugin-systemprivacy-firstskillstypescriptvettaworkflow-automation
import { Button } from "@vetta-org/ui/button";
import { cn } from "@vetta-org/ui/utils";
import { type JSX, type ReactNode, useId, useState } from "react";
import { formatFileSize } from "../knowledge/types";
import { SegmentedControl } from "../shared/SegmentedControl";
import { SettingHeading, type SettingSectionMeta } from "./SettingChrome";
import { StorageBar, type StorageBarSegment } from "./StorageBar";
import { StorageExplorer, type StorageExplorerLabels, type StorageExplorerProps } from "./StorageExplorer";
export type StorageTabView = "overview" | "content";
export type StorageShareKindView = "yours" | "cleanable";
export interface StorageShareItemView {
readonly id: string;
readonly label: string;
readonly bytes: number;
readonly onOpen?: () => void;
}
export interface StorageShareView {
readonly kind: StorageShareKindView;
readonly label: string;
readonly bytes: number;
readonly items: readonly StorageShareItemView[];
}
export interface StorageCleanActionView {
/** 与「可以清理」分段里的条目同 id,占用条与清单靠它互相高亮。 */
readonly id: string;
readonly title: string;
readonly description: string;
/** 现在就能清理的体积;0 时不可勾选。 */
readonly bytes: number;
/** 有内容但正在使用,现在清不了。 */
readonly blocked: boolean;
readonly selected: boolean;
}
export interface StorageSettingsViewLabels extends StorageExplorerLabels {
readonly title: string;
readonly description: string;
readonly total: string;
readonly cleaning: string;
readonly clean: (size: string) => string;
readonly cleanEmpty: string;
readonly inUse: string;
readonly nothingToClean: string;
/** 勾选后的预告:清理后还占多少。 */
readonly afterClean: (size: string) => string;
readonly selectHint: string;
readonly assistTitle: string;
readonly assistDescription: string;
readonly assistAction: string;
readonly tabs: Readonly<Record<StorageTabView, string>>;
readonly sections: { readonly overview: string; readonly clean: string; readonly largest: string };
}
export interface StorageSettingsViewProps {
readonly labels: StorageSettingsViewLabels;
readonly tab: StorageTabView;
readonly onTabChange: (tab: StorageTabView) => void;
readonly overviewSection: SettingSectionMeta;
readonly cleanSection: SettingSectionMeta;
readonly largestSection: SettingSectionMeta;
/** null 表示首次扫描还没有结果。capacityBytes 是占用条整条代表的体积,清理后大于 totalBytes。 */
readonly overview: {
readonly totalBytes: number;
readonly capacityBytes: number;
readonly shares: readonly StorageShareView[];
} | null;
readonly cleanActions: readonly StorageCleanActionView[];
/** 需要 Vetta 判断才能清理的体积说明;没有时不显示。 */
readonly assistPotential: string | null;
readonly explorer: StorageExplorerProps["explorer"];
readonly scanning: boolean;
readonly cleaning: boolean;
/** 上一次清理的结果,显示在清理按钮旁。 */
readonly cleanResult: string | null;
readonly error: string | null;
readonly onRescan: () => void;
readonly onToggleClean: (id: string, selected: boolean) => void;
readonly onClean: () => void;
readonly onAssist: () => void;
/** path 相对数据目录。 */
readonly onReveal: (path: string) => void;
}
/** 你的内容每类一个色相,可以清理单独一个暖色。 */
const CONTENT_COLORS: Record<string, string> = {
conversations: "var(--context-segment-1)",
projects: "var(--context-segment-2)",
knowledge: "var(--context-segment-3)",
};
const CLEANABLE_COLOR = "var(--context-segment-5)";
interface OverviewPart extends StorageBarSegment {
readonly label: string;
readonly onSelect?: () => void;
}
/** 你的内容按类别分段,可以清理的合成一段。 */
function toParts(shares: readonly StorageShareView[], goTo: (target: "clean" | "content") => void): OverviewPart[] {
return shares.flatMap((share): OverviewPart[] => {
if (share.kind === "yours") {
return share.items.map((item) => ({
id: item.id,
label: item.label,
bytes: item.bytes,
color: CONTENT_COLORS[item.id] ?? "var(--muted-foreground)",
onSelect: item.onOpen ?? (() => goTo("content")),
}));
}
return [
{
id: share.kind,
label: share.label,
bytes: share.bytes,
color: CLEANABLE_COLOR,
hatched: true,
onSelect: () => goTo("clean"),
},
];
});
}
export function StorageSettingsView(props: StorageSettingsViewProps): JSX.Element {
const { labels, tab, largestSection } = props;
return (
<div className="mx-auto w-full max-w-[680px] px-8 pt-2 pb-16">
<h1 className="mb-1.5 text-[20px] font-bold text-foreground">{labels.title}</h1>
<p className="text-[13px] text-muted-foreground">{labels.description}</p>
<SegmentedControl
className="mt-5"
items={[
{ key: "overview", label: labels.tabs.overview },
{ key: "content", label: labels.tabs.content },
]}
value={tab}
onChange={props.onTabChange}
/>
{props.error && (
<p role="alert" className="mt-4 text-[12px] text-destructive">
{props.error}
</p>
)}
<div key={tab} className="animate-in fade-in-0 duration-200 motion-reduce:animate-none">
{tab === "overview" ? (
<OverviewTab {...props} />
) : (
<section
id={largestSection.id}
aria-label={labels.sections.largest}
data-setting-section-id={largestSection.id}
data-setting-section-highlight-target={largestSection.id}
className="mt-8"
>
<StorageExplorer
labels={labels}
explorer={props.explorer}
scanning={props.scanning}
cleaning={props.cleaning}
onRescan={props.onRescan}
onReveal={props.onReveal}
/>
</section>
)}
</div>
</div>
);
}
function OverviewTab(props: StorageSettingsViewProps): JSX.Element {
const { labels } = props;
return (
<>
<StorageOverview {...props} />
<CleanSection {...props} />
<section className="mt-10 flex items-center gap-8">
<div className="min-w-0 flex-1">
<h2 className="text-[15px] font-semibold text-foreground">{labels.assistTitle}</h2>
<p className="mt-1.5 text-[12px] leading-relaxed text-muted-foreground">{labels.assistDescription}</p>
{props.assistPotential && <p className="mt-1.5 text-[12px] text-primary">{props.assistPotential}</p>}
</div>
<Button variant="field" onClick={props.onAssist}>
<span aria-hidden="true" className="icon-[solar--magic-stick-3-linear] h-3.5 w-3.5" />
{labels.assistAction}
</Button>
</section>
</>
);
}
function StorageOverview({
labels,
overview,
overviewSection,
cleanSection,
scanning,
cleaning,
onRescan,
onTabChange,
}: StorageSettingsViewProps): JSX.Element {
const [activeId, setActiveId] = useState<string | null>(null);
const parts = toParts(overview?.shares ?? [], (target) => {
if (target === "content") onTabChange("content");
else document.getElementById(cleanSection.id)?.scrollIntoView({ behavior: "smooth", block: "start" });
});
const active = parts.find((part) => part.id === activeId);
const total = overview?.totalBytes ?? 0;
const refreshLabel = scanning ? labels.scanning : labels.rescan;
return (
<section
id={overviewSection.id}
aria-label={labels.sections.overview}
data-setting-section-id={overviewSection.id}
data-setting-section-highlight-target={overviewSection.id}
className="mt-4 rounded-xl bg-muted-foreground/7 px-4 pt-3.5 pb-2.5"
>
<div className="flex items-start justify-between gap-4">
<div className="min-w-0">
<div className="truncate text-[12px] leading-4 text-muted-foreground">
{active?.label ?? labels.total}
{active && total > 0 && ` · ${formatShare(active.bytes / total)}`}
</div>
<div className="mt-0.5 text-[22px] leading-7 font-semibold tracking-tight text-foreground">
{overview ? formatFileSize(active?.bytes ?? total) : <span className="text-muted-foreground">—</span>}
</div>
</div>
<Button
variant="ghost"
size="icon-sm"
className="-mt-1 -mr-1.5"
title={refreshLabel}
aria-label={refreshLabel}
disabled={scanning || cleaning}
onClick={onRescan}
>
<span aria-hidden="true" className={cn("icon-[solar--refresh-linear] h-4 w-4", scanning && "animate-spin")} />
</Button>
</div>
<div className="mt-2.5">
<StorageBar
segments={parts}
capacityBytes={overview?.capacityBytes ?? 0}
activeId={activeId}
onActiveChange={setActiveId}
/>
</div>
{/* 首次扫描时先留出一行,结果出来后卡片不会变高。 */}
<ul className="-mx-2 mt-1.5 flex min-h-7 flex-wrap justify-between" onMouseLeave={() => setActiveId(null)}>
{parts
.filter((part) => part.bytes > 0)
.map((part) => (
<li key={part.id}>
<Button
variant="ghost"
className={cn("h-7 gap-1.5 px-2 text-[12px] font-normal", activeId === part.id && "bg-accent/50")}
onMouseEnter={() => setActiveId(part.id)}
onFocus={() => setActiveId(part.id)}
onBlur={() => setActiveId(null)}
onClick={part.onSelect}
>
<span aria-hidden="true" className="h-2 w-2 shrink-0 rounded-full" style={{ background: part.color }} />
<span className="text-muted-foreground">{part.label}</span>
<span className="text-foreground tabular-nums">{formatFileSize(part.bytes)}</span>
</Button>
</li>
))}
</ul>
</section>
);
}
const formatShare = (share: number): string => (share < 0.01 ? "<1%" : `${Math.round(share * 100)}%`);
/** 一排可勾选的色块;说明不常驻,指到或聚焦哪块就在标题下读哪块。 */
function CleanSection({
labels,
overview,
cleanSection,
cleanActions,
cleanResult,
scanning,
cleaning,
onToggleClean,
onClean,
}: StorageSettingsViewProps): JSX.Element {
const [activeId, setActiveId] = useState<string | null>(null);
// 首次扫描前先按完整清单占位,有结果后只留真的有内容的条目。
const actions = overview ? cleanActions.filter((item) => item.bytes > 0 || item.blocked) : cleanActions;
const active = actions.find((action) => action.id === activeId);
const cleanableBytes = actions.reduce((sum, action) => sum + action.bytes, 0);
const selectedBytes = actions.reduce((sum, action) => sum + (action.selected ? action.bytes : 0), 0);
const empty = actions.length === 0;
let status: ReactNode = empty ? labels.nothingToClean : labels.selectHint;
if (selectedBytes > 0) status = labels.afterClean(formatFileSize((overview?.totalBytes ?? 0) - selectedBytes));
if (cleanResult) {
status = (
<span className="flex items-center gap-1.5 text-foreground">
<span aria-hidden="true" className="icon-[solar--check-circle-linear] h-4 w-4 shrink-0 text-emerald-400" />
{cleanResult}
</span>
);
}
return (
<section
className="mt-10"
data-setting-section-id={cleanSection.id}
data-setting-section-highlight-target={cleanSection.id}
>
<div className="flex items-start justify-between gap-6">
<div className="min-w-0 flex-1">
<SettingHeading section={cleanSection} title={labels.sections.clean} />
{/* 留足两行,说明变长时下面的色块不会跳动。 */}
<div className={cn("mt-1.5 text-[12px] leading-[18px] text-muted-foreground tabular-nums", !empty && "min-h-9")}>
<div role="status" className={cn(active && "sr-only")}>
{status}
</div>
{active && <div aria-hidden="true">{active.description}</div>}
</div>
</div>
{!empty && (
<Button
variant="primary"
disabled={!overview || scanning || cleaning || selectedBytes === 0}
onClick={onClean}
>
{cleaning ? labels.cleaning : selectedBytes > 0 ? labels.clean(formatFileSize(selectedBytes)) : labels.cleanEmpty}
</Button>
)}
</div>
{!empty && (
<div
className="mt-2 grid gap-1"
style={{ gridTemplateColumns: `repeat(${actions.length}, minmax(0, 1fr))` }}
onMouseLeave={() => setActiveId(null)}
>
{actions.map((action) => (
<CleanTile
key={action.id}
action={action}
share={cleanableBytes > 0 ? action.bytes / cleanableBytes : 0}
disabled={cleaning || action.bytes === 0}
emptyLabel={overview ? labels.inUse : "—"}
onActiveChange={(on) => setActiveId(on ? action.id : null)}
onToggle={(selected) => onToggleClean(action.id, selected)}
/>
))}
</div>
)}
</section>
);
}
function CleanTile({
action,
share,
disabled,
emptyLabel,
onActiveChange,
onToggle,
}: {
readonly action: StorageCleanActionView;
/** 占全部可清理体积的比例,决定底部色条多宽。 */
readonly share: number;
readonly disabled: boolean;
readonly emptyLabel: string;
readonly onActiveChange: (active: boolean) => void;
readonly onToggle: (selected: boolean) => void;
}): JSX.Element {
const descriptionId = useId();
const available = action.bytes > 0;
const checked = action.selected && available;
const [amount, unit] = available ? formatFileSize(action.bytes).split(" ") : [];
return (
<label
className={cn(
"relative overflow-hidden rounded-lg px-3 pt-2.5 pb-4 transition-colors duration-200 has-focus-visible:ring-2 has-focus-visible:ring-ring",
disabled ? "cursor-default" : "cursor-pointer",
)}
style={{
backgroundColor: checked
? `color-mix(in srgb, ${CLEANABLE_COLOR} 12%, transparent)`
: "color-mix(in srgb, var(--muted-foreground) 8%, transparent)",
}}
onMouseEnter={() => onActiveChange(true)}
>
<input
type="checkbox"
className="sr-only"
aria-label={action.title}
aria-describedby={descriptionId}
checked={checked}
disabled={disabled}
onFocus={() => onActiveChange(true)}
onBlur={() => onActiveChange(false)}
onChange={(event) => onToggle(event.target.checked)}
/>
<span id={descriptionId} className="sr-only">
{action.description}
</span>
<span
aria-hidden="true"
className={cn(
"absolute bottom-0 left-0 h-1 origin-left transition-transform duration-300 ease-out motion-reduce:transition-none",
!checked && "scale-x-0",
)}
style={{
width: `${share * 100}%`,
backgroundColor: CLEANABLE_COLOR,
}}
/>
<span className="flex items-center justify-between gap-2">
<span className={cn("truncate text-[12px]", checked ? "text-foreground" : "text-muted-foreground")}>
{action.title}
</span>
{available && (
<span
aria-hidden="true"
className={cn(
"flex h-4 w-4 shrink-0 items-center justify-center rounded-[5px] transition-colors duration-200",
checked ? "text-background" : "bg-muted-foreground/20 text-transparent",
)}
style={checked ? { backgroundColor: CLEANABLE_COLOR } : undefined}
>
<span className="icon-[mdi--check] h-3 w-3" />
</span>
)}
</span>
<span className="mt-1.5 flex h-6 items-baseline gap-1">
{available ? (
<>
<span
className={cn(
"text-[20px] leading-6 font-semibold tracking-tight tabular-nums",
checked ? "text-foreground" : "text-muted-foreground",
)}
>
{amount}
</span>
<span className="text-[12px] text-muted-foreground">{unit}</span>
</>
) : (
<span className="text-[13px] leading-6 text-muted-foreground">{emptyLabel}</span>
)}
</span>
</label>
);
}