// SPDX-License-Identifier: AGPL-3.0-only // Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0 import { DropdownMenuItem } from "@/components/ui/dropdown-menu"; import { type LearnedMemory, requestLearnedMemory, } from "@/features/settings/api/learned-memory"; import { Tick02Icon } from "@/lib/tick-icon"; import { toast } from "@/lib/toast"; import { Brain03Icon } from "@hugeicons/core-free-icons"; import { HugeiconsIcon } from "@hugeicons/react"; import { useEffect, useState } from "react"; import { useChatRuntimeStore } from "./stores/chat-runtime-store"; import { refreshContextUsage } from "./utils/refresh-context-usage"; // Off turns "Use memory" off account-wide; on turns it on, plus this chat if Settings disabled it. export function MemoryMenuItem({ threadId }: { threadId?: string | null }) { const incognito = useChatRuntimeStore((state) => state.incognito); const [memory, setMemory] = useState(null); const query = threadId ? new URLSearchParams({ thread_id: threadId }).toString() : ""; useEffect(() => { if (incognito) return; let cancelled = false; // Submenu mounts this on open, so each open re-reads the switch. void requestLearnedMemory(query) .then((result) => { if (!cancelled) setMemory(result); }) .catch(() => {}); return () => { cancelled = true; }; }, [query, incognito]); const on = !incognito && Boolean(memory?.enabled && memory.thread_enabled); return ( { if (!memory) return; // Unsaved chats read thread_enabled as true, so it is never sent for one. const patch = on ? { enabled: false } : { enabled: true, ...(memory.thread_enabled ? {} : { thread_enabled: true }), }; void requestLearnedMemory(query, patch) .then(() => { void refreshContextUsage({ invalidate: true }); }) .catch((error: unknown) => { toast.error("Couldn't change memory", { description: error instanceof Error ? error.message : String(error), }); }); }} > Memory {on ? ( ) : null} ); }