feat: support manual add and edit for memory facts (#1538)

* feat: support manual add and edit for memory facts

* fix: restore memory updater save helper

* fix: address memory fact review feedback

* fix: remove duplicate memory fact edit action

* docs: simplify memory fact review setup

* docs: relax memory review startup instructions

* fix: clear rebase marker in memory settings page

* fix: address memory fact review and format issues

* fix: address memory fact review feedback

* refactor: make memory fact updates explicit patch semantics

---------

Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
This commit is contained in:
Admire
2026-03-29 23:53:23 +08:00
committed by GitHub
parent cdb2a3a017
commit fc7de7fffe
15 changed files with 977 additions and 52 deletions
@@ -1,8 +1,8 @@
"use client";
import { Trash2Icon } from "lucide-react";
import { PenLineIcon, PlusIcon, Trash2Icon } from "lucide-react";
import Link from "next/link";
import { useDeferredValue, useState } from "react";
import { useDeferredValue, useId, useState } from "react";
import { toast } from "sonner";
import { Streamdown } from "streamdown";
@@ -16,14 +16,21 @@ import {
DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
import { useI18n } from "@/core/i18n/hooks";
import {
useClearMemory,
useCreateMemoryFact,
useDeleteMemoryFact,
useMemory,
useUpdateMemoryFact,
} from "@/core/memory/hooks";
import type { UserMemory } from "@/core/memory/types";
import type {
MemoryFactInput,
MemoryFactPatchInput,
UserMemory,
} from "@/core/memory/types";
import { streamdownPlugins } from "@/core/streamdown/plugins";
import { pathOfThread } from "@/core/threads/utils";
import { formatTimeAgo } from "@/core/utils/datetime";
@@ -44,6 +51,18 @@ type MemorySectionGroup = {
sections: MemorySection[];
};
type FactFormState = {
content: string;
category: string;
confidence: string;
};
const DEFAULT_FACT_FORM_STATE: FactFormState = {
content: "",
category: "context",
confidence: "0.8",
};
function confidenceToLevelKey(confidence: unknown): {
key: "veryHigh" | "high" | "normal" | "unknown";
value?: number;
@@ -191,13 +210,24 @@ export function MemorySettingsPage() {
const { t } = useI18n();
const { memory, isLoading, error } = useMemory();
const clearMemory = useClearMemory();
const createMemoryFact = useCreateMemoryFact();
const deleteMemoryFact = useDeleteMemoryFact();
const updateMemoryFact = useUpdateMemoryFact();
const [clearDialogOpen, setClearDialogOpen] = useState(false);
const [factToDelete, setFactToDelete] = useState<MemoryFact | null>(null);
const [factToEdit, setFactToEdit] = useState<MemoryFact | null>(null);
const [factEditorOpen, setFactEditorOpen] = useState(false);
const [factForm, setFactForm] = useState<FactFormState>(
DEFAULT_FACT_FORM_STATE,
);
const [query, setQuery] = useState("");
const [filter, setFilter] = useState<MemoryViewFilter>("all");
const deferredQuery = useDeferredValue(query);
const normalizedQuery = deferredQuery.trim().toLowerCase();
const factContentInputId = useId();
const factCategoryInputId = useId();
const factConfidenceInputId = useId();
const factConfidenceHintId = useId();
const clearAllLabel = t.settings.memory.clearAll ?? "Clear all memory";
const clearAllConfirmTitle =
@@ -214,10 +244,23 @@ export function MemorySettingsPage() {
"This fact will be removed from memory immediately. This action cannot be undone.";
const factDeleteSuccess =
t.settings.memory.factDeleteSuccess ?? "Fact deleted";
const addFactLabel = t.settings.memory.addFact;
const addFactTitle = t.settings.memory.addFactTitle;
const editFactTitle = t.settings.memory.editFactTitle;
const addFactSuccess = t.settings.memory.addFactSuccess;
const editFactSuccess = t.settings.memory.editFactSuccess;
const factContentLabel = t.settings.memory.factContentLabel;
const factCategoryLabel = t.settings.memory.factCategoryLabel;
const factConfidenceLabel = t.settings.memory.factConfidenceLabel;
const factContentPlaceholder = t.settings.memory.factContentPlaceholder;
const factCategoryPlaceholder = t.settings.memory.factCategoryPlaceholder;
const factConfidenceHint = t.settings.memory.factConfidenceHint;
const factSave = t.settings.memory.factSave;
const factValidationContent = t.settings.memory.factValidationContent;
const factValidationConfidence = t.settings.memory.factValidationConfidence;
const manualFactSource = t.settings.memory.manualFactSource;
const noFacts = t.settings.memory.noFacts ?? "No saved facts yet.";
const summaryReadOnly =
t.settings.memory.summaryReadOnly ??
"Summary sections are read-only for now. You can currently clear all memory or delete individual facts.";
const summaryReadOnly = t.settings.memory.summaryReadOnly;
const memoryFullyEmpty =
t.settings.memory.memoryFullyEmpty ?? "No memory saved yet.";
const factPreviewLabel =
@@ -287,6 +330,68 @@ export function MemorySettingsPage() {
}
}
function openCreateFactDialog() {
setFactToEdit(null);
setFactForm(DEFAULT_FACT_FORM_STATE);
setFactEditorOpen(true);
}
function openEditFactDialog(fact: MemoryFact) {
setFactToEdit(fact);
setFactForm({
content: fact.content,
category: fact.category,
confidence: String(fact.confidence),
});
setFactEditorOpen(true);
}
async function handleSaveFact() {
const trimmedContent = factForm.content.trim();
if (!trimmedContent) {
toast.error(factValidationContent);
return;
}
const confidence = Number(factForm.confidence);
if (!Number.isFinite(confidence) || confidence < 0 || confidence > 1) {
toast.error(factValidationConfidence);
return;
}
const input: MemoryFactInput = {
content: trimmedContent,
category: factForm.category.trim() || "context",
confidence,
};
try {
if (factToEdit) {
const patchInput: MemoryFactPatchInput = {
content: input.content,
category: input.category,
confidence: input.confidence,
};
await updateMemoryFact.mutateAsync({
factId: factToEdit.id,
input: patchInput,
});
toast.success(editFactSuccess);
} else {
await createMemoryFact.mutateAsync(input);
toast.success(addFactSuccess);
}
setFactEditorOpen(false);
setFactToEdit(null);
setFactForm(DEFAULT_FACT_FORM_STATE);
} catch (err) {
toast.error(err instanceof Error ? err.message : String(err));
}
}
const isFactFormPending =
createMemoryFact.isPending || updateMemoryFact.isPending;
return (
<>
<SettingsSection
@@ -335,13 +440,19 @@ export function MemorySettingsPage() {
</ToggleGroup>
</div>
<Button
variant="destructive"
onClick={() => setClearDialogOpen(true)}
disabled={clearMemory.isPending}
>
{clearMemory.isPending ? t.common.loading : clearAllLabel}
</Button>
<div className="flex items-center gap-2">
<Button variant="outline" onClick={openCreateFactDialog}>
<PlusIcon className="mr-2 h-4 w-4" />
{addFactLabel}
</Button>
<Button
variant="destructive"
onClick={() => setClearDialogOpen(true)}
disabled={clearMemory.isPending}
>
{clearMemory.isPending ? t.common.loading : clearAllLabel}
</Button>
</div>
</div>
{!hasMatchingVisibleContent && normalizedQuery ? (
@@ -412,25 +523,45 @@ export function MemorySettingsPage() {
<p className="text-sm break-words">
{fact.content}
</p>
<Link
href={pathOfThread(fact.source)}
className="text-primary text-sm underline-offset-4 hover:underline"
>
{t.settings.memory.markdown.table.view}
</Link>
{fact.source === "manual" ? (
<span className="text-muted-foreground text-sm">
{manualFactSource}
</span>
) : (
<Link
href={pathOfThread(fact.source)}
className="text-primary text-sm underline-offset-4 hover:underline"
>
{t.settings.memory.markdown.table.view}
</Link>
)}
</div>
<Button
variant="ghost"
size="icon"
className="text-destructive hover:text-destructive shrink-0"
onClick={() => setFactToDelete(fact)}
disabled={deleteMemoryFact.isPending}
title={t.common.delete}
aria-label={t.common.delete}
>
<Trash2Icon className="h-4 w-4" />
</Button>
<div className="flex shrink-0 items-center gap-1 self-start sm:ml-3">
<Button
variant="ghost"
size="icon"
className="shrink-0"
onClick={() => openEditFactDialog(fact)}
disabled={deleteMemoryFact.isPending}
title={t.common.edit}
aria-label={t.common.edit}
>
<PenLineIcon className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="icon"
className="text-destructive hover:text-destructive shrink-0"
onClick={() => setFactToDelete(fact)}
disabled={deleteMemoryFact.isPending}
title={t.common.delete}
aria-label={t.common.delete}
>
<Trash2Icon className="h-4 w-4" />
</Button>
</div>
</div>
);
})}
@@ -467,6 +598,118 @@ export function MemorySettingsPage() {
</DialogContent>
</Dialog>
<Dialog
open={factEditorOpen}
onOpenChange={(open) => {
setFactEditorOpen(open);
if (!open) {
setFactToEdit(null);
setFactForm(DEFAULT_FACT_FORM_STATE);
}
}}
>
<DialogContent>
<DialogHeader>
<DialogTitle>
{factToEdit ? editFactTitle : addFactTitle}
</DialogTitle>
</DialogHeader>
<div className="space-y-4">
<div className="space-y-2">
<label
className="text-sm font-medium"
htmlFor={factContentInputId}
>
{factContentLabel}
</label>
<Textarea
id={factContentInputId}
value={factForm.content}
onChange={(event) =>
setFactForm((current) => ({
...current,
content: event.target.value,
}))
}
placeholder={factContentPlaceholder}
rows={4}
/>
</div>
<div className="grid gap-4 sm:grid-cols-2">
<div className="space-y-2">
<label
className="text-sm font-medium"
htmlFor={factCategoryInputId}
>
{factCategoryLabel}
</label>
<Input
id={factCategoryInputId}
value={factForm.category}
onChange={(event) =>
setFactForm((current) => ({
...current,
category: event.target.value,
}))
}
placeholder={factCategoryPlaceholder}
/>
</div>
<div className="space-y-2">
<label
className="text-sm font-medium"
htmlFor={factConfidenceInputId}
>
{factConfidenceLabel}
</label>
<Input
id={factConfidenceInputId}
aria-describedby={factConfidenceHintId}
type="number"
min="0"
max="1"
step="0.01"
value={factForm.confidence}
onChange={(event) =>
setFactForm((current) => ({
...current,
confidence: event.target.value,
}))
}
/>
<div
className="text-muted-foreground text-xs"
id={factConfidenceHintId}
>
{factConfidenceHint}
</div>
</div>
</div>
</div>
<DialogFooter>
<Button
variant="outline"
onClick={() => {
setFactEditorOpen(false);
setFactToEdit(null);
setFactForm(DEFAULT_FACT_FORM_STATE);
}}
disabled={isFactFormPending}
>
{t.common.cancel}
</Button>
<Button
onClick={() => void handleSaveFact()}
disabled={isFactFormPending}
>
{isFactFormPending ? t.common.loading : factSave}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
<Dialog
open={factToDelete !== null}
onOpenChange={(open) => {
+17 -1
View File
@@ -22,6 +22,7 @@ export const enUS: Translations = {
home: "Home",
settings: "Settings",
delete: "Delete",
edit: "Edit",
rename: "Rename",
share: "Share",
openInNewWindow: "Open in new window",
@@ -312,6 +313,11 @@ export const enUS: Translations = {
"DeerFlow automatically learns from your conversations in the background. These memories help DeerFlow understand you better and deliver a more personalized experience.",
empty: "No memory data to display.",
rawJson: "Raw JSON",
addFact: "Add fact",
addFactTitle: "Add memory fact",
editFactTitle: "Edit memory fact",
addFactSuccess: "Fact created",
editFactSuccess: "Fact updated",
clearAll: "Clear all memory",
clearAllConfirmTitle: "Clear all memory?",
clearAllConfirmDescription:
@@ -321,9 +327,19 @@ export const enUS: Translations = {
factDeleteConfirmDescription:
"This fact will be removed from memory immediately. This action cannot be undone.",
factDeleteSuccess: "Fact deleted",
factContentLabel: "Content",
factCategoryLabel: "Category",
factConfidenceLabel: "Confidence",
factContentPlaceholder: "Describe the memory fact you want to save",
factCategoryPlaceholder: "context",
factConfidenceHint: "Use a number between 0 and 1.",
factSave: "Save fact",
factValidationContent: "Fact content cannot be empty.",
factValidationConfidence: "Confidence must be a number between 0 and 1.",
manualFactSource: "Manual",
noFacts: "No saved facts yet.",
summaryReadOnly:
"Summary sections are read-only for now. You can currently clear all memory or delete individual facts.",
"Summary sections are read-only for now. You can currently add, edit, or delete individual facts, or clear all memory.",
memoryFullyEmpty: "No memory saved yet.",
factPreviewLabel: "Fact to delete",
searchPlaceholder: "Search memory",
+16
View File
@@ -11,6 +11,7 @@ export interface Translations {
home: string;
settings: string;
delete: string;
edit: string;
rename: string;
share: string;
openInNewWindow: string;
@@ -247,6 +248,11 @@ export interface Translations {
description: string;
empty: string;
rawJson: string;
addFact: string;
addFactTitle: string;
editFactTitle: string;
addFactSuccess: string;
editFactSuccess: string;
clearAll: string;
clearAllConfirmTitle: string;
clearAllConfirmDescription: string;
@@ -254,6 +260,16 @@ export interface Translations {
factDeleteConfirmTitle: string;
factDeleteConfirmDescription: string;
factDeleteSuccess: string;
factContentLabel: string;
factCategoryLabel: string;
factConfidenceLabel: string;
factContentPlaceholder: string;
factCategoryPlaceholder: string;
factConfidenceHint: string;
factSave: string;
factValidationContent: string;
factValidationConfidence: string;
manualFactSource: string;
noFacts: string;
summaryReadOnly: string;
memoryFullyEmpty: string;
+17 -1
View File
@@ -22,6 +22,7 @@ export const zhCN: Translations = {
home: "首页",
settings: "设置",
delete: "删除",
edit: "编辑",
rename: "重命名",
share: "分享",
openInNewWindow: "在新窗口打开",
@@ -298,6 +299,11 @@ export const zhCN: Translations = {
"DeerFlow 会在后台不断从你的对话中自动学习。这些记忆能帮助 DeerFlow 更好地理解你,并提供更个性化的体验。",
empty: "暂无可展示的记忆数据。",
rawJson: "原始 JSON",
addFact: "添加事实",
addFactTitle: "添加记忆事实",
editFactTitle: "编辑记忆事实",
addFactSuccess: "事实已创建",
editFactSuccess: "事实已更新",
clearAll: "清空全部记忆",
clearAllConfirmTitle: "要清空全部记忆吗?",
clearAllConfirmDescription:
@@ -307,9 +313,19 @@ export const zhCN: Translations = {
factDeleteConfirmDescription:
"这条事实会立即从记忆中删除。此操作无法撤销。",
factDeleteSuccess: "事实已删除",
factContentLabel: "内容",
factCategoryLabel: "类别",
factConfidenceLabel: "置信度",
factContentPlaceholder: "描述你想保存的记忆事实",
factCategoryPlaceholder: "context",
factConfidenceHint: "请输入 0 到 1 之间的数字。",
factSave: "保存事实",
factValidationContent: "事实内容不能为空。",
factValidationConfidence: "置信度必须是 0 到 1 之间的数字。",
manualFactSource: "手动添加",
noFacts: "还没有保存的事实。",
summaryReadOnly:
"摘要分区当前仍为只读。现在你可以清空全部记忆或删除单条事实。",
"摘要分区当前仍为只读。你可以在下方添加、编辑或删除事实,或清空全部记忆。",
memoryFullyEmpty: "还没有保存任何记忆。",
factPreviewLabel: "即将删除的事实",
searchPlaceholder: "搜索记忆",
+35 -1
View File
@@ -1,6 +1,10 @@
import { getBackendBaseURL } from "../config";
import type { UserMemory } from "./types";
import type {
MemoryFactInput,
MemoryFactPatchInput,
UserMemory,
} from "./types";
async function readMemoryResponse(
response: Response,
@@ -39,3 +43,33 @@ export async function deleteMemoryFact(factId: string): Promise<UserMemory> {
);
return readMemoryResponse(response, "Failed to delete memory fact");
}
export async function createMemoryFact(
input: MemoryFactInput,
): Promise<UserMemory> {
const response = await fetch(`${getBackendBaseURL()}/api/memory/facts`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(input),
});
return readMemoryResponse(response, "Failed to create memory fact");
}
export async function updateMemoryFact(
factId: string,
input: MemoryFactPatchInput,
): Promise<UserMemory> {
const response = await fetch(
`${getBackendBaseURL()}/api/memory/facts/${encodeURIComponent(factId)}`,
{
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(input),
},
);
return readMemoryResponse(response, "Failed to update memory fact");
}
+40 -2
View File
@@ -1,7 +1,17 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { clearMemory, deleteMemoryFact, loadMemory } from "./api";
import type { UserMemory } from "./types";
import {
clearMemory,
createMemoryFact,
deleteMemoryFact,
loadMemory,
updateMemoryFact,
} from "./api";
import type {
MemoryFactInput,
MemoryFactPatchInput,
UserMemory,
} from "./types";
export function useMemory() {
const { data, isLoading, error } = useQuery({
@@ -32,3 +42,31 @@ export function useDeleteMemoryFact() {
},
});
}
export function useCreateMemoryFact() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (input: MemoryFactInput) => createMemoryFact(input),
onSuccess: (memory) => {
queryClient.setQueryData<UserMemory>(["memory"], memory);
},
});
}
export function useUpdateMemoryFact() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({
factId,
input,
}: {
factId: string;
input: MemoryFactPatchInput;
}) => updateMemoryFact(factId, input),
onSuccess: (memory) => {
queryClient.setQueryData<UserMemory>(["memory"], memory);
},
});
}
+22 -8
View File
@@ -1,3 +1,24 @@
export interface MemoryFact {
id: string;
content: string;
category: string;
confidence: number;
createdAt: string;
source: string;
}
export interface MemoryFactInput {
content: string;
category: string;
confidence: number;
}
export interface MemoryFactPatchInput {
content?: string;
category?: string;
confidence?: number;
}
export interface UserMemory {
version: string;
lastUpdated: string;
@@ -29,12 +50,5 @@ export interface UserMemory {
updatedAt: string;
};
};
facts: {
id: string;
content: string;
category: string;
confidence: number;
createdAt: string;
source: string;
}[];
facts: MemoryFact[];
}