247 lines
8.8 KiB
TypeScript
247 lines
8.8 KiB
TypeScript
"use client";
|
||
|
||
import {
|
||
ArrowDownIcon,
|
||
ArrowUpIcon,
|
||
BotIcon,
|
||
PlusIcon,
|
||
Trash2Icon,
|
||
} from "lucide-react";
|
||
import { useMemo, useState } from "react";
|
||
import { toast } from "sonner";
|
||
|
||
import { Button } from "@/components/ui/button";
|
||
import {
|
||
Command,
|
||
CommandEmpty,
|
||
CommandGroup,
|
||
CommandInput,
|
||
CommandItem,
|
||
CommandList,
|
||
} from "@/components/ui/command";
|
||
import {
|
||
Dialog,
|
||
DialogContent,
|
||
DialogDescription,
|
||
DialogHeader,
|
||
DialogTitle,
|
||
} from "@/components/ui/dialog";
|
||
import { Tooltip } from "@/components/workspace/tooltip";
|
||
import {
|
||
useAgentSelector,
|
||
useAgents,
|
||
useReorderFeaturedAgents,
|
||
useSetAgentFeatured,
|
||
} from "@/core/agents";
|
||
import type { Agent } from "@/core/agents";
|
||
import { cn } from "@/lib/utils";
|
||
|
||
interface AgentManageDialogProps {
|
||
open: boolean;
|
||
onOpenChange: (open: boolean) => void;
|
||
}
|
||
|
||
/**
|
||
* Admin-only homepage agent picker management.
|
||
*
|
||
* Single list (no tabs, no per-user buckets): admin curates exactly what every
|
||
* user sees in the new-chat selector. Up/down to reorder, ✕ to remove, + to
|
||
* add a published agent. All actions hit the server immediately and react to
|
||
* TanStack Query invalidation, so the selector dropdown stays in lockstep.
|
||
*/
|
||
export function AgentManageDialog({
|
||
open,
|
||
onOpenChange,
|
||
}: AgentManageDialogProps) {
|
||
const { agents, isLoading } = useAgentSelector();
|
||
const { agents: allAgents } = useAgents();
|
||
const setFeaturedMutation = useSetAgentFeatured();
|
||
const reorderMutation = useReorderFeaturedAgents();
|
||
const [pickerOpen, setPickerOpen] = useState(false);
|
||
|
||
const candidates = useMemo(() => {
|
||
const existing = new Set(agents.map((a) => a.id));
|
||
// Private-square agents are hidden from the homepage selector even if
|
||
// they're published — the backend ``list_featured`` filter would drop
|
||
// them again on the way out, so offering them here is just confusing.
|
||
return allAgents.filter(
|
||
(a) => a.published && !a.is_private && !existing.has(a.id),
|
||
);
|
||
}, [agents, allAgents]);
|
||
|
||
const busy = setFeaturedMutation.isPending || reorderMutation.isPending;
|
||
|
||
const move = async (index: number, direction: -1 | 1) => {
|
||
const target = index + direction;
|
||
if (target < 0 || target >= agents.length) return;
|
||
const next = agents.map((a) => a.id);
|
||
[next[index], next[target]] = [next[target]!, next[index]!];
|
||
try {
|
||
await reorderMutation.mutateAsync(next);
|
||
} catch (err) {
|
||
toast.error(err instanceof Error ? err.message : String(err));
|
||
}
|
||
};
|
||
|
||
const remove = async (agentId: string) => {
|
||
try {
|
||
await setFeaturedMutation.mutateAsync({ id: agentId, order: null });
|
||
} catch (err) {
|
||
toast.error(err instanceof Error ? err.message : String(err));
|
||
}
|
||
};
|
||
|
||
const add = async (agent: Agent) => {
|
||
if (!agent.published) {
|
||
toast.error("只有已发布的智能体可以加入选择器");
|
||
return;
|
||
}
|
||
const maxOrder = agents.reduce(
|
||
(acc, a) => Math.max(acc, a.featured_order ?? 0),
|
||
-1,
|
||
);
|
||
try {
|
||
await setFeaturedMutation.mutateAsync({
|
||
id: agent.id,
|
||
order: maxOrder + 1,
|
||
});
|
||
setPickerOpen(false);
|
||
} catch (err) {
|
||
toast.error(err instanceof Error ? err.message : String(err));
|
||
}
|
||
};
|
||
|
||
return (
|
||
<>
|
||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||
<DialogContent className="sm:max-w-xl">
|
||
<DialogHeader>
|
||
<DialogTitle>管理 Agent 列表</DialogTitle>
|
||
<DialogDescription>
|
||
配置首页对话框里展示的 agent。所有人会看到同一份列表,按下方顺序展示。
|
||
</DialogDescription>
|
||
</DialogHeader>
|
||
|
||
<div className="space-y-3">
|
||
{isLoading ? (
|
||
<div className="rounded-md border p-6 text-center text-sm text-muted-foreground">
|
||
加载中…
|
||
</div>
|
||
) : agents.length === 0 ? (
|
||
<div className="rounded-md border border-dashed p-6 text-center text-sm text-muted-foreground">
|
||
还没有配置任何 agent。点击下方“添加 Agent”开始。
|
||
</div>
|
||
) : (
|
||
<div className="max-h-[50vh] divide-y overflow-y-auto rounded-md border">
|
||
{agents.map((agent, index) => (
|
||
<div
|
||
key={agent.id}
|
||
className="flex min-w-0 items-start gap-2 px-3 py-2 hover:bg-accent/30"
|
||
>
|
||
<span className="text-muted-foreground w-6 shrink-0 text-center text-xs tabular-nums">
|
||
{index + 1}
|
||
</span>
|
||
<BotIcon className="text-muted-foreground mt-0.5 size-4 shrink-0" />
|
||
<div className="flex min-w-0 flex-1 flex-col">
|
||
<span className="truncate text-sm">{agent.name}</span>
|
||
{agent.description && (
|
||
<span className="line-clamp-2 whitespace-normal break-words text-[11px] leading-snug text-muted-foreground">
|
||
{agent.description}
|
||
</span>
|
||
)}
|
||
</div>
|
||
<div className="flex shrink-0 items-center gap-0.5">
|
||
<Tooltip content="上移">
|
||
<Button
|
||
size="icon"
|
||
variant="ghost"
|
||
disabled={busy || index === 0}
|
||
onClick={() => void move(index, -1)}
|
||
className="size-7"
|
||
>
|
||
<ArrowUpIcon className="size-3.5" />
|
||
</Button>
|
||
</Tooltip>
|
||
<Tooltip content="下移">
|
||
<Button
|
||
size="icon"
|
||
variant="ghost"
|
||
disabled={busy || index === agents.length - 1}
|
||
onClick={() => void move(index, 1)}
|
||
className="size-7"
|
||
>
|
||
<ArrowDownIcon className="size-3.5" />
|
||
</Button>
|
||
</Tooltip>
|
||
<Tooltip content="从选择器移除">
|
||
<Button
|
||
size="icon"
|
||
variant="ghost"
|
||
disabled={busy}
|
||
onClick={() => void remove(agent.id)}
|
||
className="text-destructive hover:text-destructive size-7"
|
||
>
|
||
<Trash2Icon className="size-3.5" />
|
||
</Button>
|
||
</Tooltip>
|
||
</div>
|
||
</div>
|
||
))}
|
||
</div>
|
||
)}
|
||
|
||
<Button
|
||
type="button"
|
||
variant="outline"
|
||
disabled={busy}
|
||
onClick={() => setPickerOpen(true)}
|
||
className="w-full"
|
||
>
|
||
<PlusIcon className="mr-1 size-3.5" />
|
||
添加 Agent
|
||
</Button>
|
||
</div>
|
||
</DialogContent>
|
||
</Dialog>
|
||
|
||
<Dialog open={pickerOpen} onOpenChange={setPickerOpen}>
|
||
<DialogContent className="overflow-hidden p-0 sm:max-w-md">
|
||
<DialogTitle className="sr-only">添加 Agent</DialogTitle>
|
||
<Command className={cn("**:data-[slot=command-input-wrapper]:h-auto")}>
|
||
<CommandInput placeholder="搜索可添加的 agent…" className="h-auto py-3.5" />
|
||
<CommandList>
|
||
<CommandEmpty>
|
||
{candidates.length === 0
|
||
? "没有更多已发布的 agent 可添加。"
|
||
: "没有找到匹配的 agent。"}
|
||
</CommandEmpty>
|
||
{candidates.length > 0 && (
|
||
<CommandGroup>
|
||
{candidates.map((agent) => (
|
||
<CommandItem
|
||
key={agent.id}
|
||
value={`${agent.name} ${agent.description ?? ""}`}
|
||
onSelect={() => void add(agent)}
|
||
className="flex w-full min-w-0 items-start overflow-hidden"
|
||
>
|
||
<BotIcon className="text-muted-foreground mr-2 mt-0.5 size-4 shrink-0" />
|
||
<div className="flex min-w-0 flex-1 flex-col">
|
||
<span className="truncate text-sm">{agent.name}</span>
|
||
{agent.description && (
|
||
<span className="line-clamp-2 whitespace-normal break-words text-[10px] leading-snug text-muted-foreground">
|
||
{agent.description}
|
||
</span>
|
||
)}
|
||
</div>
|
||
</CommandItem>
|
||
))}
|
||
</CommandGroup>
|
||
)}
|
||
</CommandList>
|
||
</Command>
|
||
</DialogContent>
|
||
</Dialog>
|
||
</>
|
||
);
|
||
}
|