"use client"; import { Check, Loader2, Pencil, RefreshCw, Trash2, X } from "lucide-react"; import { AnimatePresence, motion } from "motion/react"; import { useState } from "react"; import { Input } from "@/shared/ui/input"; import { cn } from "@/lib/utils"; import type { AgentChatSessionSummary } from "../api/client"; import { agentLayoutTransition, agentPanelItemVariants, agentPanelListVariants } from "./agent-motion"; type AgentHistoryPanelProps = { activeSessionId?: string | null; loading: boolean; loadingSessionId: string | null; sessions: AgentChatSessionSummary[]; onRefresh?: () => Promise | void; onSelectSession: (sessionId: string) => void; onRenameSession?: (sessionId: string, title: string) => Promise | void; onDeleteSession?: (sessionId: string) => Promise | void; }; export function AgentHistoryPanel({ activeSessionId, loading, loadingSessionId, sessions, onRefresh, onSelectSession, onRenameSession, onDeleteSession }: AgentHistoryPanelProps) { const [editingSessionId, setEditingSessionId] = useState(null); const [draftTitle, setDraftTitle] = useState(""); const [savingSessionId, setSavingSessionId] = useState(null); const [deletingSessionId, setDeletingSessionId] = useState(null); const [confirmingDeleteSessionId, setConfirmingDeleteSessionId] = useState(null); const beginRename = (session: AgentChatSessionSummary) => { setEditingSessionId(session.id); setDraftTitle(session.title); }; const cancelRename = () => { setEditingSessionId(null); setDraftTitle(""); }; const saveRename = (session: AgentChatSessionSummary) => { const normalizedTitle = draftTitle.trim(); if (!normalizedTitle || normalizedTitle === session.title) { cancelRename(); return; } setSavingSessionId(session.id); void Promise.resolve(onRenameSession?.(session.id, normalizedTitle)) .then(cancelRename) .finally(() => setSavingSessionId(null)); }; const confirmDelete = (sessionId: string) => { setDeletingSessionId(sessionId); void Promise.resolve(onDeleteSession?.(sessionId)) .then(() => setConfirmingDeleteSessionId(null)) .finally(() => setDeletingSessionId(null)); }; return (

历史记录

{sessions.length ? `${sessions.length} 个会话` : "暂无历史会话"}

{loading && sessions.length === 0 ? ( ) : sessions.length > 0 ? ( sessions.map((session) => { const active = session.id === activeSessionId; const itemLoading = loadingSessionId === session.id; const editing = editingSessionId === session.id; const saving = savingSessionId === session.id; const deleting = deletingSessionId === session.id; const confirmingDelete = confirmingDeleteSessionId === session.id; return ( {confirmingDelete ? ( 删除“{session.title}”? 删除后无法从历史记录中恢复 ) : editing ? ( { event.preventDefault(); saveRename(session); }} > setDraftTitle(event.currentTarget.value)} onKeyDown={(event) => { if (event.key === "Escape") { cancelRename(); } }} /> ) : ( onSelectSession(session.id)} > {session.title} {formatSessionUpdatedAt(session.updatedAt)} )} {session.runStatus === "running" || session.isStreaming ? ( ) : null} {itemLoading || saving || deleting ? ( ); }) ) : ( 暂无历史记录 )}
); } function formatSessionUpdatedAt(value: number) { return new Intl.DateTimeFormat("zh-CN", { month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit" }).format(value); }