master 的 Memory V2 重构删除了 memory/updater.py,admin_users.py 仍从中导入 get_memory_data 导致启动 ImportError。改用 BuiltinFileProvider 读取 V2 的 USER.md / 各 agent MEMORY.md;前端 MemoryBucketView 同步改为渲染 V2 条目列表。 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
315 lines
12 KiB
TypeScript
315 lines
12 KiB
TypeScript
"use client";
|
||
|
||
import { 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 { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
||
import { useUserMemory, useUserThreadMessages, useUserThreads } from "@/core/admin/hooks";
|
||
import type { AdminMemoryBucket, AdminMessage } from "@/core/admin/api";
|
||
import { useAllUsers } from "@/core/auth/hooks";
|
||
import { cn } from "@/lib/utils";
|
||
|
||
/** Flatten a langchain message dump (or raw string / block list) into text. */
|
||
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; // tool results and system messages are hidden from the Q&A view
|
||
}
|
||
|
||
function fmtTime(iso: string): string {
|
||
if (!iso) return "";
|
||
const d = new Date(iso);
|
||
return Number.isNaN(d.getTime()) ? iso : d.toLocaleString();
|
||
}
|
||
|
||
/** A single Q&A bubble. */
|
||
function MessageRow({ message }: { message: AdminMessage }) {
|
||
const role = roleOf(message);
|
||
if (!role) return null;
|
||
const text = extractText(message.content).trim();
|
||
if (!text) return null; // skip empty AI turns (e.g. tool-call-only responses)
|
||
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>
|
||
);
|
||
}
|
||
|
||
/** Render one Memory V2 bucket: a flat list of USER.md / MEMORY.md entries. */
|
||
function MemoryBucketView({ bucket }: { bucket: AdminMemoryBucket }) {
|
||
const raw = (bucket.data as { entries?: unknown }).entries;
|
||
const entries = Array.isArray(raw)
|
||
? raw.filter(
|
||
(e): e is string => typeof e === "string" && e.trim().length > 0,
|
||
)
|
||
: [];
|
||
|
||
return (
|
||
<div className="rounded-md border p-4">
|
||
<div className="mb-3 flex items-center gap-2">
|
||
<Badge variant={bucket.name === "global" ? "default" : "secondary"}>
|
||
{bucket.name === "global" ? "用户画像" : `Agent: ${bucket.name}`}
|
||
</Badge>
|
||
{entries.length > 0 && (
|
||
<span className="text-muted-foreground text-xs">
|
||
{entries.length} 条
|
||
</span>
|
||
)}
|
||
</div>
|
||
{entries.length === 0 ? (
|
||
<p className="text-muted-foreground text-sm">该记忆库暂无内容。</p>
|
||
) : (
|
||
<ul className="flex flex-col gap-1.5">
|
||
{entries.map((entry, i) => (
|
||
<li key={i} className="flex items-start gap-2 text-sm">
|
||
<span className="text-muted-foreground">·</span>
|
||
<span className="flex-1 whitespace-pre-wrap">{entry}</span>
|
||
</li>
|
||
))}
|
||
</ul>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
export default function AdminUserActivityPage() {
|
||
const { data: users, isLoading: usersLoading } = useAllUsers(true);
|
||
const [userId, setUserId] = useState<string | null>(null);
|
||
const [threadId, setThreadId] = useState<string | null>(null);
|
||
const [userQuery, setUserQuery] = useState("");
|
||
const [tab, setTab] = useState<"qa" | "memory">("qa");
|
||
|
||
const { data: threads, isLoading: threadsLoading } = useUserThreads(userId);
|
||
const { data: messages, isLoading: messagesLoading } = useUserThreadMessages(
|
||
userId,
|
||
threadId,
|
||
);
|
||
const { data: memory, isLoading: memoryLoading } = useUserMemory(userId);
|
||
|
||
const filteredUsers = useMemo(() => {
|
||
const needle = userQuery.trim().toLowerCase();
|
||
const list = users ?? [];
|
||
return needle ? list.filter((u) => u.email.toLowerCase().includes(needle)) : list;
|
||
}, [users, userQuery]);
|
||
|
||
const selectedUser = users?.find((u) => u.id === userId) ?? null;
|
||
|
||
const selectUser = (id: string) => {
|
||
setUserId(id);
|
||
setThreadId(null);
|
||
setTab("qa");
|
||
};
|
||
|
||
return (
|
||
<div className="flex size-full flex-col">
|
||
<div className="border-b px-6 py-4">
|
||
<h1 className="text-xl font-semibold">用户活动审计</h1>
|
||
<p className="text-muted-foreground mt-0.5 text-sm">
|
||
查看任意用户的历史问答与记忆(只读)。
|
||
</p>
|
||
</div>
|
||
|
||
<div className="flex min-h-0 flex-1">
|
||
{/* Pane 1: users */}
|
||
<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>
|
||
<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 === userId ? "bg-accent" : "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>
|
||
)}
|
||
</button>
|
||
))}
|
||
</div>
|
||
</ScrollArea>
|
||
</div>
|
||
|
||
{/* Pane 2: threads */}
|
||
<div className="flex w-72 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">
|
||
{!userId && (
|
||
<div className="text-muted-foreground py-8 text-center text-sm">
|
||
请先选择用户
|
||
</div>
|
||
)}
|
||
{userId && threadsLoading && (
|
||
<div className="text-muted-foreground flex items-center justify-center gap-2 py-8 text-sm">
|
||
<Loader2 className="size-4 animate-spin" /> 加载中
|
||
</div>
|
||
)}
|
||
{userId && !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);
|
||
setTab("qa");
|
||
}}
|
||
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>
|
||
|
||
{/* Pane 3: detail */}
|
||
<div className="flex min-w-0 flex-1 flex-col">
|
||
{!userId ? (
|
||
<div className="text-muted-foreground flex flex-1 items-center justify-center text-sm">
|
||
选择一个用户开始审计
|
||
</div>
|
||
) : (
|
||
<>
|
||
<div className="flex items-center justify-between gap-3 border-b px-4 py-2">
|
||
<span className="text-muted-foreground truncate text-sm">
|
||
{selectedUser?.email}
|
||
</span>
|
||
<Tabs value={tab} onValueChange={(v) => setTab(v as "qa" | "memory")}>
|
||
<TabsList>
|
||
<TabsTrigger value="qa">问答记录</TabsTrigger>
|
||
<TabsTrigger value="memory">记忆</TabsTrigger>
|
||
</TabsList>
|
||
</Tabs>
|
||
</div>
|
||
|
||
<ScrollArea className="min-h-0 flex-1">
|
||
{tab === "qa" && (
|
||
<div className="p-4">
|
||
{!threadId && (
|
||
<div className="text-muted-foreground flex flex-col items-center gap-2 py-16 text-sm">
|
||
<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>
|
||
)}
|
||
|
||
{tab === "memory" && (
|
||
<div className="flex flex-col gap-3 p-4">
|
||
{memoryLoading && (
|
||
<div className="text-muted-foreground flex items-center justify-center gap-2 py-16 text-sm">
|
||
<Loader2 className="size-4 animate-spin" /> 加载中
|
||
</div>
|
||
)}
|
||
{!memoryLoading &&
|
||
(memory?.buckets ?? []).map((bucket) => (
|
||
<MemoryBucketView key={bucket.name} bucket={bucket} />
|
||
))}
|
||
{!memoryLoading && (memory?.buckets?.length ?? 0) === 0 && (
|
||
<div className="text-muted-foreground py-12 text-center text-sm">
|
||
该用户暂无记忆数据
|
||
</div>
|
||
)}
|
||
</div>
|
||
)}
|
||
</ScrollArea>
|
||
</>
|
||
)}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|