"use client"; import { PenLineIcon, PlusIcon, SearchIcon, Trash2Icon, Heart, BookOpen, Map, Zap, Target, ShieldAlert } from "lucide-react"; import { useMemo, useState } from "react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Switch } from "@/components/ui/switch"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Textarea } from "@/components/ui/textarea"; import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"; import { useI18n } from "@/core/i18n/hooks"; import { searchMemory } from "@/core/memory/api"; import { useAgents } from "@/core/agents/hooks"; import { useAddMemoryEntry, useDeleteMemoryEntry, useMemory, useMemoryConfig, usePatchMemoryConfig, useReplaceMemoryEntry, } from "@/core/memory/hooks"; import type { MemoryFact, MemorySearchMode, MemoryTarget, MemoryV2Config, } from "@/core/memory/types"; import { desensitize } from "@/lib/desensitize"; import { SettingsSection } from "./settings-section"; /** 一条条目的编辑态:新增时 oldText 为 null,编辑时为原文。 */ type EntryEditState = { target: MemoryTarget; oldText: string | null; content: string; }; export function MemorySettingsPage() { const { t } = useI18n(); const m = t.settings.memory; // 先读配置拿到 version,再决定渲染哪套 UI const { configResponse } = useMemoryConfig(); const memoryVersion = configResponse?.version ?? "v2"; // 工作笔记按专家隔离 —— selectedAgent 控制查看/编辑哪个专家的 MEMORY.md。 // 用户画像 (USER.md) 是 per-user 的,不受 selectedAgent 影响。 // 专家列表来自 agents 注册表 (非内建的才是用户的专家);default 是每个 // 用户都有的内置助手,始终作为下拉的第一个选项。 const { agents: allAgents } = useAgents(); const experts = useMemo( () => allAgents.filter((a) => !a.builtin), [allAgents], ); const [selectedAgent, setSelectedAgent] = useState("default"); const { memory, isLoading, error } = useMemory(selectedAgent); const addEntry = useAddMemoryEntry(selectedAgent); const replaceEntry = useReplaceMemoryEntry(selectedAgent); const deleteEntry = useDeleteMemoryEntry(selectedAgent); const [editState, setEditState] = useState(null); const [entryToDelete, setEntryToDelete] = useState<{ target: MemoryTarget; content: string; } | null>(null); function openAddDialog(target: MemoryTarget) { setEditState({ target, oldText: null, content: "" }); } function openEditDialog(target: MemoryTarget, content: string) { setEditState({ target, oldText: content, content }); } async function handleSaveEntry() { if (!editState) return; const content = editState.content.trim(); if (!content) { toast.error(m.entryValidationContent); return; } try { if (editState.oldText === null) { await addEntry.mutateAsync({ target: editState.target, content }); } else { await replaceEntry.mutateAsync({ target: editState.target, oldText: editState.oldText, content, }); } toast.success(m.entrySaveSuccess); setEditState(null); } catch (err) { toast.error(err instanceof Error ? err.message : String(err)); } } async function handleDeleteEntry() { if (!entryToDelete) return; try { await deleteEntry.mutateAsync({ target: entryToDelete.target, oldText: entryToDelete.content, }); toast.success(m.deleteEntrySuccess); setEntryToDelete(null); } catch (err) { toast.error(err instanceof Error ? err.message : String(err)); } } const isEntryPending = addEntry.isPending || replaceEntry.isPending; function renderEntrySection( target: MemoryTarget, sectionTitle: string, sectionDesc: string, entries: string[], usage: string, headerExtra?: React.ReactNode, ) { return (

{sectionTitle}

{sectionDesc}

{headerExtra} {m.usageLabel}: {usage}
{entries.length === 0 ? (
{m.entryEmpty}
) : (
{entries.map((entry, index) => (

{desensitize(entry)}

))}
)}
); } return ( <> {isLoading ? (
{t.common.loading}
) : error ? (
{m.loadError}: {error.message}
) : !memory ? (
{m.entryEmpty}
) : memoryVersion === "v1" ? ( // V1 模式:只读展示 facts 卡片列表,支持按 agent 切换
{m.workNotesAgentLabel}:
) : ( // V2 模式:USER.md / MEMORY.md 条目编辑
{renderEntrySection( "user", m.userProfileTitle, m.userProfileDesc, memory.user_entries, memory.user_usage, )} {renderEntrySection( "memory", m.workNotesTitle, m.workNotesDesc, memory.memory_entries, memory.memory_usage,
{m.workNotesAgentLabel}:
, )}
)}
{memoryVersion !== "v1" && ( <>
)} {/* 新增 / 编辑条目对话框 */} { if (!open) setEditState(null); }} > {editState?.oldText === null ? m.addEntryTitle : m.editEntryTitle}