450 lines
16 KiB
TypeScript
450 lines
16 KiB
TypeScript
"use client";
|
||
|
||
import { ArrowDownWideNarrow, Loader2, MessageSquareIcon, SearchIcon, UserIcon } from "lucide-react";
|
||
import { useMemo, useState } from "react";
|
||
|
||
import { Badge } from "@/components/ui/badge";
|
||
import { Input } from "@/components/ui/input";
|
||
import { ScrollArea } from "@/components/ui/scroll-area";
|
||
import {
|
||
Select,
|
||
SelectContent,
|
||
SelectItem,
|
||
SelectTrigger,
|
||
SelectValue,
|
||
} from "@/components/ui/select";
|
||
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
||
import type { AdminMessage } from "@/core/admin/api";
|
||
import {
|
||
useUserMemory,
|
||
useUserThreadMessages,
|
||
useUserThreads,
|
||
} from "@/core/admin/hooks";
|
||
import type { UserListItem } from "@/core/auth/api";
|
||
import { useAllUsers } from "@/core/auth/hooks";
|
||
import { cn } from "@/lib/utils";
|
||
|
||
import {
|
||
getEntries,
|
||
MemoryEntryList,
|
||
V1BucketView,
|
||
} from "./admin-memory-page";
|
||
|
||
// ── Helpers (shared with AdminUserActivityPage) ─────────────────────────────
|
||
|
||
function extractText(content: unknown): string {
|
||
if (content == null) return "";
|
||
if (typeof content === "string") return content;
|
||
if (Array.isArray(content)) {
|
||
return content
|
||
.map((block) => {
|
||
if (typeof block === "string") return block;
|
||
if (block && typeof block === "object") {
|
||
const text = (block as Record<string, unknown>).text;
|
||
if (typeof text === "string") return text;
|
||
}
|
||
return "";
|
||
})
|
||
.join("");
|
||
}
|
||
if (typeof content === "object") {
|
||
const inner = (content as Record<string, unknown>).content;
|
||
if (inner !== undefined) return extractText(inner);
|
||
}
|
||
return "";
|
||
}
|
||
|
||
function roleOf(message: AdminMessage): "user" | "assistant" | null {
|
||
const type = (message.type ?? "").toLowerCase();
|
||
if (type === "human") return "user";
|
||
if (type === "ai" || type === "aimessagechunk") return "assistant";
|
||
return null;
|
||
}
|
||
|
||
function fmtTime(iso: string): string {
|
||
if (!iso) return "";
|
||
const d = new Date(iso);
|
||
return Number.isNaN(d.getTime()) ? iso : d.toLocaleString();
|
||
}
|
||
|
||
// ── UserInfoPanel ───────────────────────────────────────────────────────────
|
||
|
||
function UserInfoPanel({ user }: { user: UserListItem }) {
|
||
return (
|
||
<div className="p-6">
|
||
<dl className="grid grid-cols-[max-content_1fr] gap-x-8 gap-y-3 text-sm">
|
||
<dt className="text-muted-foreground font-medium">邮箱</dt>
|
||
<dd className="break-all">{user.email}</dd>
|
||
<dt className="text-muted-foreground font-medium">角色</dt>
|
||
<dd>
|
||
{user.system_role === "admin" ? (
|
||
<Badge variant="default">管理员</Badge>
|
||
) : (
|
||
<Badge variant="secondary">普通用户</Badge>
|
||
)}
|
||
</dd>
|
||
<dt className="text-muted-foreground font-medium">问答次数</dt>
|
||
<dd className="tabular-nums">{user.qa_count}</dd>
|
||
<dt className="text-muted-foreground font-medium">Skills</dt>
|
||
<dd className="tabular-nums">{user.skill_count}</dd>
|
||
<dt className="text-muted-foreground font-medium">Agents</dt>
|
||
<dd className="tabular-nums">{user.agent_count}</dd>
|
||
<dt className="text-muted-foreground font-medium">User ID</dt>
|
||
<dd className="font-mono text-xs text-muted-foreground break-all">{user.id}</dd>
|
||
</dl>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
// ── MessageRow / MemoryBucketView (audit sub-components) ────────────────────
|
||
|
||
function MessageRow({ message }: { message: AdminMessage }) {
|
||
const role = roleOf(message);
|
||
if (!role) return null;
|
||
const text = extractText(message.content).trim();
|
||
if (!text) return null;
|
||
return (
|
||
<div
|
||
className={cn(
|
||
"flex flex-col gap-1",
|
||
role === "user" ? "items-start" : "items-end",
|
||
)}
|
||
>
|
||
<span className="text-muted-foreground text-xs">
|
||
{role === "user" ? "用户提问" : "AI 回答"}
|
||
</span>
|
||
<div
|
||
className={cn(
|
||
"max-w-[85%] whitespace-pre-wrap rounded-lg border px-3 py-2 text-sm",
|
||
role === "user" ? "bg-muted" : "bg-primary/5 border-primary/20",
|
||
)}
|
||
>
|
||
{text}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
// ── UserAuditPanel ──────────────────────────────────────────────────────────
|
||
|
||
function UserAuditPanel({ userId }: { userId: string }) {
|
||
const [threadId, setThreadId] = useState<string | null>(null);
|
||
|
||
const { data: threads, isLoading: threadsLoading } = useUserThreads(userId);
|
||
const { data: messages, isLoading: messagesLoading } = useUserThreadMessages(
|
||
userId,
|
||
threadId,
|
||
);
|
||
|
||
return (
|
||
<div className="flex min-h-0 flex-1">
|
||
{/* Thread list */}
|
||
<div className="flex w-60 shrink-0 flex-col border-r">
|
||
<div className="text-muted-foreground border-b px-3 py-2 text-xs font-medium">
|
||
会话列表{threads ? `(${threads.length})` : ""}
|
||
</div>
|
||
<ScrollArea className="min-h-0 flex-1">
|
||
{threadsLoading && (
|
||
<div className="text-muted-foreground flex items-center justify-center gap-2 py-8 text-sm">
|
||
<Loader2 className="size-4 animate-spin" /> 加载中
|
||
</div>
|
||
)}
|
||
{!threadsLoading && (threads?.length ?? 0) === 0 && (
|
||
<div className="text-muted-foreground py-8 text-center text-sm">
|
||
该用户暂无会话
|
||
</div>
|
||
)}
|
||
<div className="flex flex-col p-1">
|
||
{(threads ?? []).map((thread) => (
|
||
<button
|
||
key={thread.thread_id}
|
||
type="button"
|
||
onClick={() => setThreadId(thread.thread_id)}
|
||
className={cn(
|
||
"flex flex-col gap-0.5 rounded-md px-2 py-2 text-left",
|
||
thread.thread_id === threadId ? "bg-accent" : "hover:bg-muted",
|
||
)}
|
||
>
|
||
<span className="truncate text-sm">
|
||
{thread.display_name || "未命名会话"}
|
||
</span>
|
||
<span className="text-muted-foreground text-xs">
|
||
{fmtTime(thread.updated_at)}
|
||
</span>
|
||
</button>
|
||
))}
|
||
</div>
|
||
</ScrollArea>
|
||
</div>
|
||
|
||
{/* Messages */}
|
||
<ScrollArea className="min-h-0 flex-1">
|
||
<div className="p-4">
|
||
{!threadId && (
|
||
<div className="text-muted-foreground flex flex-col items-center gap-2 py-16 text-sm dark:text-[#AACEF8]">
|
||
<MessageSquareIcon className="size-6" />
|
||
请从左侧选择一个会话
|
||
</div>
|
||
)}
|
||
{threadId && messagesLoading && (
|
||
<div className="text-muted-foreground flex items-center justify-center gap-2 py-16 text-sm">
|
||
<Loader2 className="size-4 animate-spin" /> 加载中
|
||
</div>
|
||
)}
|
||
{threadId && !messagesLoading && (
|
||
<div className="flex flex-col gap-3">
|
||
{(messages ?? []).map((m, i) => (
|
||
<MessageRow key={m.id ?? i} message={m} />
|
||
))}
|
||
{(messages ?? []).every((m) => !roleOf(m)) && (
|
||
<div className="text-muted-foreground py-12 text-center text-sm">
|
||
该会话暂无问答记录
|
||
</div>
|
||
)}
|
||
</div>
|
||
)}
|
||
</div>
|
||
</ScrollArea>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
// ── AgentMemoryPanel ─────────────────────────────────────────────────────────
|
||
|
||
function bucketLabel(name: string, displayName?: string): string {
|
||
if (name === "default") return "默认智能体";
|
||
return displayName || name;
|
||
}
|
||
|
||
function AgentMemoryPanel({ userId }: { userId: string }) {
|
||
const { data: memory, isLoading } = useUserMemory(userId);
|
||
// 默认选中"默认助手"("default" 对应全局记忆文件)
|
||
const [selectedAgent, setSelectedAgent] = useState<string>("default");
|
||
|
||
if (isLoading) {
|
||
return (
|
||
<div className="text-muted-foreground flex flex-1 items-center justify-center gap-2 text-sm">
|
||
<Loader2 className="size-4 animate-spin" /> 加载中…
|
||
</div>
|
||
);
|
||
}
|
||
|
||
const isV1 = memory?.version === "v1";
|
||
|
||
// 非 global、非 default 的其他智能体 bucket
|
||
const otherBuckets = (memory?.buckets ?? []).filter(
|
||
(b) => b.name !== "global" && b.name !== "default",
|
||
);
|
||
|
||
// 当前选中的 bucket
|
||
const currentBucket = (memory?.buckets ?? []).find(
|
||
(b) => b.name === selectedAgent,
|
||
);
|
||
|
||
// "default" 对应 agent=null(全局记忆文件),与 _resolve_agent_name 的哨兵逻辑一致
|
||
const bucketAgentParam = selectedAgent === "default" ? null : selectedAgent;
|
||
|
||
return (
|
||
<div className="flex min-h-0 flex-1 flex-col">
|
||
<ScrollArea className="min-h-0 flex-1">
|
||
<div className="space-y-4 p-4">
|
||
{/* 智能体下拉选择器 */}
|
||
<div className="flex items-center gap-2">
|
||
<span className="text-muted-foreground text-sm shrink-0">智能体:</span>
|
||
<Select value={selectedAgent} onValueChange={setSelectedAgent}>
|
||
<SelectTrigger className="h-8 w-48" size="sm">
|
||
<SelectValue />
|
||
</SelectTrigger>
|
||
<SelectContent position="popper" side="bottom" sideOffset={4}>
|
||
{/* 默认助手始终显示,对应全局记忆文件 */}
|
||
<SelectItem value="default">默认助手</SelectItem>
|
||
{otherBuckets.map((b) => (
|
||
<SelectItem key={b.name} value={b.name}>
|
||
{bucketLabel(b.name, b.display_name)}
|
||
</SelectItem>
|
||
))}
|
||
</SelectContent>
|
||
</Select>
|
||
{!isV1 && currentBucket && (
|
||
<span className="text-muted-foreground text-xs">
|
||
{getEntries(currentBucket.data).length} 条
|
||
</span>
|
||
)}
|
||
</div>
|
||
|
||
{/* 当前智能体记忆内容 */}
|
||
{!currentBucket ? (
|
||
<div className="text-muted-foreground rounded-md border border-dashed p-4 text-center text-sm">
|
||
暂无记忆数据
|
||
</div>
|
||
) : isV1 ? (
|
||
<V1BucketView
|
||
data={currentBucket.data}
|
||
agent={bucketAgentParam}
|
||
userId={userId}
|
||
/>
|
||
) : (
|
||
<MemoryEntryList
|
||
entries={getEntries(currentBucket.data)}
|
||
userId={userId}
|
||
agent={bucketAgentParam}
|
||
/>
|
||
)}
|
||
</div>
|
||
</ScrollArea>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
// ── AdminUserHub (main) ─────────────────────────────────────────────────────
|
||
|
||
export function AdminUserHub() {
|
||
const { data: users, isLoading: usersLoading } = useAllUsers(true);
|
||
const [selectedUserId, setSelectedUserId] = useState<string | null>(null);
|
||
const [userQuery, setUserQuery] = useState("");
|
||
// 活动审计 放到前面 → 默认进入审计标签页。
|
||
const [activeTab, setActiveTab] = useState<"audit" | "info" | "memory">("audit");
|
||
// 是否按问答次数(降序)排序用户列表。
|
||
const [sortByQa, setSortByQa] = useState(false);
|
||
|
||
const filteredUsers = useMemo(() => {
|
||
const needle = userQuery.trim().toLowerCase();
|
||
let list = users ?? [];
|
||
if (needle) {
|
||
list = list.filter((u) => u.email.toLowerCase().includes(needle));
|
||
}
|
||
if (sortByQa) {
|
||
// Copy before sorting so we never mutate the query cache array.
|
||
list = [...list].sort((a, b) => (b.qa_count ?? 0) - (a.qa_count ?? 0));
|
||
}
|
||
return list;
|
||
}, [users, userQuery, sortByQa]);
|
||
|
||
const selectedUser = users?.find((u) => u.id === selectedUserId) ?? null;
|
||
|
||
function selectUser(id: string) {
|
||
setSelectedUserId(id);
|
||
setActiveTab("audit");
|
||
}
|
||
|
||
return (
|
||
<div className="flex size-full flex-col">
|
||
<div className="border-b px-6 py-4">
|
||
<h1 className="text-xl font-semibold">Admin 用户中心</h1>
|
||
<p className="text-muted-foreground mt-0.5 text-sm">
|
||
管理用户信息、审计历史问答、编辑用户记忆。
|
||
</p>
|
||
</div>
|
||
|
||
<div className="flex min-h-0 flex-1">
|
||
{/* Left: user list */}
|
||
<div className="flex w-64 shrink-0 flex-col border-r">
|
||
<div className="relative p-2">
|
||
<SearchIcon className="text-muted-foreground absolute left-4 top-1/2 size-4 -translate-y-1/2" />
|
||
<Input
|
||
value={userQuery}
|
||
onChange={(e) => setUserQuery(e.target.value)}
|
||
placeholder="搜索用户邮箱"
|
||
className="pl-8"
|
||
/>
|
||
</div>
|
||
<button
|
||
type="button"
|
||
onClick={() => setSortByQa((v) => !v)}
|
||
className={cn(
|
||
"mx-2 mb-1 flex items-center justify-center gap-1 rounded-md border px-2 py-1 text-xs",
|
||
sortByQa
|
||
? "border-primary/40 bg-primary/5 text-primary"
|
||
: "text-muted-foreground hover:bg-muted",
|
||
)}
|
||
title="按问答次数从高到低排序"
|
||
>
|
||
<ArrowDownWideNarrow className="size-3.5" />
|
||
{sortByQa ? "按问答次数排序中" : "按问答次数排序"}
|
||
</button>
|
||
<ScrollArea className="min-h-0 flex-1">
|
||
{usersLoading && (
|
||
<div className="text-muted-foreground flex items-center justify-center gap-2 py-8 text-sm">
|
||
<Loader2 className="size-4 animate-spin" /> 加载中
|
||
</div>
|
||
)}
|
||
{!usersLoading && filteredUsers.length === 0 && (
|
||
<div className="text-muted-foreground py-8 text-center text-sm">
|
||
无用户
|
||
</div>
|
||
)}
|
||
<div className="flex flex-col p-1">
|
||
{filteredUsers.map((u) => (
|
||
<button
|
||
key={u.id}
|
||
type="button"
|
||
onClick={() => selectUser(u.id)}
|
||
className={cn(
|
||
"flex items-center gap-2 rounded-md px-2 py-2 text-left text-sm",
|
||
u.id === selectedUserId
|
||
? "bg-accent font-medium"
|
||
: "hover:bg-muted",
|
||
)}
|
||
>
|
||
<UserIcon className="text-muted-foreground size-4 shrink-0" />
|
||
<span className="flex-1 truncate">{u.email}</span>
|
||
{u.system_role === "admin" && (
|
||
<Badge variant="outline" className="shrink-0 text-xs">
|
||
管理员
|
||
</Badge>
|
||
)}
|
||
<span
|
||
className="text-muted-foreground shrink-0 tabular-nums text-xs"
|
||
title="问答次数"
|
||
>
|
||
{u.qa_count ?? 0}
|
||
</span>
|
||
</button>
|
||
))}
|
||
</div>
|
||
</ScrollArea>
|
||
</div>
|
||
|
||
{/* Right: tabs */}
|
||
{!selectedUserId ? (
|
||
<div className="text-muted-foreground flex flex-1 items-center justify-center text-sm">
|
||
从左侧选择一个用户
|
||
</div>
|
||
) : (
|
||
<Tabs
|
||
value={activeTab}
|
||
onValueChange={(v) => setActiveTab(v as typeof activeTab)}
|
||
className="flex min-w-0 flex-1 flex-col overflow-hidden"
|
||
>
|
||
<div className="border-b px-4 pt-3 shrink-0">
|
||
<TabsList variant="line">
|
||
<TabsTrigger value="audit">活动审计</TabsTrigger>
|
||
<TabsTrigger value="info">用户信息</TabsTrigger>
|
||
<TabsTrigger value="memory">记忆管理</TabsTrigger>
|
||
</TabsList>
|
||
</div>
|
||
|
||
<TabsContent value="info" className="flex-1 overflow-auto p-0">
|
||
{selectedUser && <UserInfoPanel user={selectedUser} />}
|
||
</TabsContent>
|
||
|
||
<TabsContent
|
||
value="audit"
|
||
className="flex min-h-0 flex-1 flex-col p-0"
|
||
>
|
||
<UserAuditPanel key={selectedUserId} userId={selectedUserId} />
|
||
</TabsContent>
|
||
|
||
<TabsContent
|
||
value="memory"
|
||
className="flex min-h-0 flex-1 flex-col p-0"
|
||
>
|
||
<AgentMemoryPanel key={selectedUserId} userId={selectedUserId} />
|
||
</TabsContent>
|
||
</Tabs>
|
||
)}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|