"use client"; import { useState } from "react"; import { memoryApi } from "@/lib/api"; import type { MemoryType } from "@/types/domain"; import type { Memory } from "@/types/api"; import { MEMORY_TYPE_LABEL, MEMORY_SOURCE_LABEL } from "@/lib/constants/labels"; const TYPE_OPTIONS: MemoryType[] = [ "identity", "user", "episodic", "semantic", "procedural", "relational", "emotional", "world", "reflection", "evolution", ]; /** * 息壤枢记忆治理列表(可增删改)。 * 治理者可补录记忆(admin 校正/手动沉淀)、inline 编辑、软删除。 * 复用 §10 memoryApi.createMemory/updateMemory/deleteMemory。 */ export function MemoryAdminList({ initial }: { initial: Memory[] }) { const [memories, setMemories] = useState(initial); const [editingId, setEditingId] = useState(null); const [draft, setDraft] = useState({ title: "", content: "" }); const [busyId, setBusyId] = useState(null); const [error, setError] = useState(null); const [showAdd, setShowAdd] = useState(false); const [newMem, setNewMem] = useState<{ type: MemoryType; title: string; content: string; }>({ type: "user", title: "", content: "", }); function startEdit(m: Memory) { setEditingId(m.id); setDraft({ title: m.title ?? "", content: m.content }); setError(null); } async function save(id: string) { setBusyId(id); setError(null); try { const updated = await memoryApi.updateMemory(id, draft); setMemories((prev) => prev.map((m) => (m.id === id ? { ...m, ...updated } : m)), ); setEditingId(null); } catch (e) { setError(e instanceof Error ? e.message : "保存失败,请重试。"); } finally { setBusyId(null); } } async function remove(id: string) { setBusyId(id); setError(null); try { await memoryApi.deleteMemory(id); setMemories((prev) => prev.filter((m) => m.id !== id)); } catch (e) { setError(e instanceof Error ? e.message : "删除失败,请重试。"); } finally { setBusyId(null); } } async function add() { if (!newMem.content.trim()) { setError("请填写记忆内容。"); return; } setBusyId("__new"); setError(null); try { const created = await memoryApi.createMemory({ type: newMem.type, title: newMem.title.trim() || undefined, content: newMem.content.trim(), }); setMemories((prev) => [created, ...prev]); setNewMem({ type: "user", title: "", content: "" }); setShowAdd(false); } catch (e) { setError(e instanceof Error ? e.message : "新增失败,请重试。"); } finally { setBusyId(null); } } return ( <> {showAdd ? (
setNewMem((s) => ({ ...s, title: e.target.value })) } placeholder="标题(可选)" className="min-w-0 flex-1 rounded-lg border border-border-glass bg-glass px-2 py-1 text-sm text-fg" />