154 lines
5.4 KiB
TypeScript
154 lines
5.4 KiB
TypeScript
"use client";
|
|
|
|
import { InboxIcon, InfoIcon, XIcon } from "lucide-react";
|
|
import { useMemo } from "react";
|
|
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { Button } from "@/components/ui/button";
|
|
import type { Skill } from "@/core/skills";
|
|
import { desensitize } from "@/lib/desensitize";
|
|
import { cn } from "@/lib/utils";
|
|
|
|
/** Chinese display label with English-name fallback. */
|
|
export function skillDisplayLabel(skill: Skill) {
|
|
return skill.name_zh?.trim() ? skill.name_zh : desensitize(skill.name);
|
|
}
|
|
|
|
/** First character of the display label — the letter avatar on skill cards. */
|
|
export function skillInitial(skill: Skill) {
|
|
const label = skill.name_zh?.trim() || skill.name;
|
|
return label.trim().charAt(0).toUpperCase() || "?";
|
|
}
|
|
|
|
/** Human label for the skill's provenance (内置 / 用户上传 / 对话沉淀). */
|
|
export function skillSourceLabel(skill: Skill): string | null {
|
|
if (skill.builtin) return "内置";
|
|
if (skill.source === "upload") return "用户上传";
|
|
if (skill.source === "agent") return "对话沉淀";
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* The agent's enabled-skill list shown in the detail panel: one card per
|
|
* whitelisted skill name with a letter avatar, Chinese name, category/source
|
|
* badges and the short description. In editing mode each card gains a remove
|
|
* button. Skill names with no metadata in the pool (e.g. a skill that was
|
|
* deleted after being whitelisted) still render as a degraded card so the
|
|
* whitelist stays visible and removable.
|
|
*/
|
|
export function EnabledSkillList({
|
|
skillNames,
|
|
skills,
|
|
editing,
|
|
onRemove,
|
|
onViewDetail,
|
|
isLoading,
|
|
}: {
|
|
/** The agent's persisted whitelist — `agent.skills` (English names). */
|
|
skillNames: string[];
|
|
/** Full skill pool used to resolve metadata for each name. */
|
|
skills: Skill[];
|
|
editing?: boolean;
|
|
onRemove?: (name: string) => void;
|
|
onViewDetail?: (name: string) => void;
|
|
isLoading?: boolean;
|
|
}) {
|
|
const byName = useMemo(
|
|
() => new Map(skills.map((s) => [s.name, s])),
|
|
[skills],
|
|
);
|
|
|
|
if (isLoading && skillNames.length > 0) {
|
|
return (
|
|
<div className="text-muted-foreground rounded-lg border border-dashed px-3 py-4 text-center text-xs">
|
|
技能加载中…
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (skillNames.length === 0) {
|
|
return (
|
|
<div className="text-muted-foreground flex flex-col items-center gap-1.5 rounded-lg border border-dashed px-3 py-5 text-xs">
|
|
<InboxIcon className="size-5 opacity-50" />
|
|
<span>尚未启用任何技能</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-2">
|
|
{skillNames.map((name) => {
|
|
const skill = byName.get(name);
|
|
return (
|
|
<div
|
|
key={name}
|
|
className={cn(
|
|
"group/skill bg-background/60 flex items-start gap-2.5 rounded-xl border p-2.5 transition-colors",
|
|
onViewDetail && "hover:border-primary/40 hover:bg-accent/40",
|
|
)}
|
|
>
|
|
<div className="bg-primary/10 text-primary flex size-8 shrink-0 items-center justify-center rounded-lg text-sm font-semibold">
|
|
{skill ? skillInitial(skill) : "?"}
|
|
</div>
|
|
<div className="min-w-0 flex-1">
|
|
<div className="flex min-w-0 items-center gap-1.5">
|
|
<span
|
|
className="min-w-0 truncate text-sm font-medium"
|
|
title={skill ? skillDisplayLabel(skill) : desensitize(name)}
|
|
>
|
|
{skill ? skillDisplayLabel(skill) : desensitize(name)}
|
|
</span>
|
|
{skill?.category?.trim() && (
|
|
<Badge variant="secondary" className="shrink-0 text-[10px]">
|
|
{skill.category}
|
|
</Badge>
|
|
)}
|
|
{skill && skillSourceLabel(skill) && (
|
|
<Badge variant="outline" className="shrink-0 text-[10px]">
|
|
{skillSourceLabel(skill)}
|
|
</Badge>
|
|
)}
|
|
</div>
|
|
<p className="text-muted-foreground mt-0.5 line-clamp-2 text-xs leading-relaxed">
|
|
{skill
|
|
? skill.description?.trim()
|
|
? desensitize(skill.description)
|
|
: "暂无描述"
|
|
: "该技能已不存在或不可用,可将其移除。"}
|
|
</p>
|
|
</div>
|
|
<div className="flex shrink-0 items-center gap-0.5">
|
|
{skill && onViewDetail && (
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
className="text-muted-foreground size-7"
|
|
onClick={() => onViewDetail(name)}
|
|
title="查看技能详情"
|
|
aria-label="查看技能详情"
|
|
>
|
|
<InfoIcon className="size-3.5" />
|
|
</Button>
|
|
)}
|
|
{editing && onRemove && (
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
className="text-muted-foreground hover:text-destructive size-7"
|
|
onClick={() => onRemove(name)}
|
|
title="移除技能"
|
|
aria-label="移除技能"
|
|
>
|
|
<XIcon className="size-3.5" />
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
);
|
|
}
|