MCPcopy
hub / github.com/ModelEngine-Group/nexent / useMemory

Function useMemory

frontend/hooks/useMemory.ts:24–485  ·  view source on GitHub ↗
({ visible, currentUserId, currentTenantId, message }: UseMemoryOptions)

Source from the content-addressed store, hash-verified

22import log from "@/lib/logger";
23
24export function useMemory({ visible, currentUserId, currentTenantId, message }: UseMemoryOptions) {
25 const { t } = useTranslation()
26 /* ----------------------- Basic Settings State ----------------------- */
27 const [memoryEnabled, setMemoryEnabledState] = useState<boolean>(true)
28 const [shareOption, setShareOptionState] = useState<"always" | "ask" | "never">("always")
29
30 /* ------------------------- Original Logic State ------------------------- */
31 // Group disabled state (only effective for Agent shared, user Agent tabs)
32 const [disabledGroups, setDisabledGroups] = useState<Record<string, boolean>>({})
33
34 const disableAgentIdSet = useRef<Set<string>>(new Set())
35 const disableUserAgentIdSet = useRef<Set<string>>(new Set())
36
37 const [openKey, setOpenKey] = useState<string>()
38
39 // Currently active Tab
40 const [activeTabKey, setActiveTabKey] = useState<string>("base")
41
42 // Pagination state
43 const [pageMap, setPageMap] = useState<Record<string, number>>({ agentShared: 1, userAgent: 1 })
44
45 /* ------------------------------ Data Groups ------------------------------ */
46 const [tenantSharedGroup, setTenantSharedGroup] = useState<MemoryGroup>({ title: "", key: "tenant", items: [] })
47 const [agentSharedGroups, setAgentSharedGroups] = useState<MemoryGroup[]>([])
48 const [userPersonalGroup, setUserPersonalGroup] = useState<MemoryGroup>({ title: "", key: "user-personal", items: [] })
49 const [userAgentGroups, setUserAgentGroups] = useState<MemoryGroup[]>([])
50
51 /* ------------------------------ New Memory State ------------------------------ */
52 const [addingMemoryKey, setAddingMemoryKey] = useState<string | null>(null)
53 const [newMemoryContent, setNewMemoryContent] = useState<string>("")
54 const [isAddingMemory, setIsAddingMemory] = useState<boolean>(false)
55
56 /* --------------------------- Initialization Loading --------------------------- */
57 useEffect(() => {
58 if (!visible) return
59
60 // 1. Load configuration
61 loadMemoryConfig().then((cfg) => {
62 setMemoryEnabledState(cfg.memoryEnabled)
63 setShareOptionState(cfg.shareOption)
64 disableAgentIdSet.current = new Set(cfg.disableAgentIds)
65 disableUserAgentIdSet.current = new Set(cfg.disableUserAgentIds)
66 }).catch((e) => {
67 log.error("Failed to load memory config:", e)
68 message.error(t('useMemory.loadConfigError'))
69 })
70 }, [visible, message])
71
72 /* --------------------------- Load Group Data --------------------------- */
73 useEffect(() => {
74 if (!visible || !memoryEnabled) return
75
76 const loadGroupsForActiveTab = async () => {
77 try {
78 if (activeTabKey === "tenant") {
79 const tenantGrp = await fetchTenantSharedGroup()
80 setTenantSharedGroup(tenantGrp)
81 } else if (activeTabKey === "agentShared") {

Callers 1

MemoryContentFunction · 0.90

Calls 15

loadMemoryConfigFunction · 0.90
addDisabledAgentIdFunction · 0.90
addDisabledUserAgentIdFunction · 0.90
removeDisabledAgentIdFunction · 0.90
addMemoryFunction · 0.90
clearMemoryFunction · 0.90
deleteMemoryFunction · 0.90
setMemorySwitchFunction · 0.90
setMemoryAgentShareFunction · 0.90
loadGroupsForActiveTabFunction · 0.85
delayFunction · 0.85

Tested by

no test coverage detected