"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 ( <> 管理 Agent 列表 配置首页对话框里展示的 agent。所有人会看到同一份列表,按下方顺序展示。
{isLoading ? (
加载中…
) : agents.length === 0 ? (
还没有配置任何 agent。点击下方“添加 Agent”开始。
) : (
{agents.map((agent, index) => (
{index + 1}
{agent.name} {agent.description && ( {agent.description} )}
))}
)}
添加 Agent {candidates.length === 0 ? "没有更多已发布的 agent 可添加。" : "没有找到匹配的 agent。"} {candidates.length > 0 && ( {candidates.map((agent) => ( void add(agent)} className="flex w-full min-w-0 items-start overflow-hidden" >
{agent.name} {agent.description && ( {agent.description} )}
))}
)}
); }