🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix: delete persona without purging memories (#20576)
Neko4f2b63922456
diff --git a/apps/server/src/routers/lambda/__tests__/userMemory.test.ts b/apps/server/src/routers/lambda/__tests__/userMemory.test.tsindex 043effad8a4..2554bd54fa9 100644--- a/apps/server/src/routers/lambda/__tests__/userMemory.test.ts+++ b/apps/server/src/routers/lambda/__tests__/userMemory.test.ts@@ -415,3 +415,28 @@ describe('userMemoryRouter persona versions', () => {},);});++describe('userMemoryRouter.deletePersona', () => {+ beforeEach(() => vi.clearAllMocks());++ it('deletes only the caller persona without purging memories or resetting topics', async () => {+ mockDeletePersona.mockResolvedValue(undefined);+ await expect(createCaller().deletePersona()).resolves.toEqual({ success: true });+ expect(mockDeletePersona).toHaveBeenCalledOnce();+ expect(mockDeleteAll).not.toHaveBeenCalled();+ expect(mockResetMemoryExtractStatus).not.toHaveBeenCalled();+ });++ it('rejects workspace scope before deleting personal data', async () => {+ await expect(+ createCaller({ workspaceId: 'workspace-1' }).deletePersona(),+ ).rejects.toMatchObject({ code: 'FORBIDDEN' });+ expect(mockDeletePersona).not.toHaveBeenCalled();+ });++ it('propagates a failed deletion', async () => {+ mockDeletePersona.mockRejectedValueOnce(new Error('delete failed'));+ await expect(createCaller().deletePersona()).rejects.toThrow('delete failed');+ expect(mockDeleteAll).not.toHaveBeenCalled();+ });+});diff --git a/apps/server/src/routers/lambda/userMemory.ts b/apps/server/src/routers/lambda/userMemory.tsindex 344ab3e7e86..0d37e36d5f0 100644--- a/apps/server/src/routers/lambda/userMemory.ts+++ b/apps/server/src/routers/lambda/userMemory.ts@@ -61,7 +61,7 @@ const personalUserMemoryProcedure = userMemoryProcedure.use(async ({ ctx, next }if (ctx.workspaceId) {throw new TRPCError({code: 'FORBIDDEN',- message: 'Persona versions are available only in personal scope',+ message: 'Persona is available only in personal scope',});}return next();@@ -118,6 +118,16 @@ export const userMemoryRouter = router({return ctx.activityModel.delete(input.id);}),+ /**+ * Delete the caller's persona without changing their memory entries.+ * Triggering workflow: PersonaHeader confirmation -> memoryCRUDService.deletePersona+ * -> userMemory.deletePersona -> UserPersonaModel.deletePersona.+ */+ deletePersona: personalUserMemoryWriteProcedure.mutation(async ({ ctx }) => {+ await ctx.personaModel.deletePersona();+ return { success: true };+ }),+deleteAll: userMemoryWriteProcedure.mutation(async ({ ctx }) => {await ctx.userMemoryModel.deleteAll();await ctx.personaModel.deletePersona();diff --git a/locales/en-US/memory.json b/locales/en-US/memory.jsonindex b5f97b16156..4eee723e612 100644--- a/locales/en-US/memory.json+++ b/locales/en-US/memory.json@@ -75,6 +75,11 @@"identity.roleCloud.expand": "Show More","identity.view.list": "List","identity.view.timeline": "Timeline",+ "persona.delete.action": "Delete persona",+ "persona.delete.confirm": "This deletes your persona and its version history. Your memory entries will be kept. Future memory analysis may create a new persona.",+ "persona.delete.error": "Failed to delete persona. Please try again.",+ "persona.delete.success": "Persona deleted. Your memories have been kept.",+ "persona.delete.title": "Delete persona?","preference.actions.delete": "Delete","preference.actions.edit": "Edit","preference.conclusionDirectives": "Conclusion Directives",diff --git a/locales/zh-CN/memory.json b/locales/zh-CN/memory.jsonindex 90c90f2e423..a20c2a3793e 100644--- a/locales/zh-CN/memory.json+++ b/locales/zh-CN/memory.json@@ -75,6 +75,11 @@"identity.roleCloud.expand": "显示更多","identity.view.list": "列表","identity.view.timeline": "时间轴",+ "persona.delete.action": "删除画像",+ "persona.delete.confirm": "这将删除个人画像及其版本历史,保留所有记忆条目。后续记忆分析可能会重新生成画像。",+ "persona.delete.error": "删除个人画像失败,请重试。",+ "persona.delete.success": "个人画像已删除,记忆条目已保留。",+ "persona.delete.title": "删除个人画像?","preference.actions.delete": "删除","preference.actions.edit": "编辑","preference.conclusionDirectives": "结论指令",diff --git a/packages/locales/src/default/memory.ts b/packages/locales/src/default/memory.tsindex 2b0f8843d0d..f0b64930f7e 100644--- a/packages/locales/src/default/memory.ts+++ b/packages/locales/src/default/memory.ts@@ -82,6 +82,12 @@ export default {'identity.roleCloud.expand': 'Show More','identity.view.list': 'List','identity.view.timeline': 'Timeline',+ 'persona.delete.action': 'Delete persona',+ 'persona.delete.title': 'Delete persona?',+ 'persona.delete.confirm':+ 'This deletes your persona and its version history. Your memory entries will be kept. Future memory analysis may create a new persona.',+ 'persona.delete.success': 'Persona deleted. Your memories have been kept.',+ 'persona.delete.error': 'Failed to delete persona. Please try again.','preference.actions.delete': 'Delete','preference.actions.edit': 'Edit','preference.conclusionDirectives': 'Conclusion Directives',diff --git a/src/routes/(main)/memory/(home)/features/Persona/PersonaHeader.tsx b/src/routes/(main)/memory/(home)/features/Persona/PersonaHeader.tsxindex 80d75cca3d7..f77ec9bf0e7 100644--- a/src/routes/(main)/memory/(home)/features/Persona/PersonaHeader.tsx+++ b/src/routes/(main)/memory/(home)/features/Persona/PersonaHeader.tsx@@ -1,5 +1,11 @@-import { Text } from '@lobehub/ui/base-ui';+import { Flexbox } from '@lobehub/ui';+import { Button, confirmModal, Text, toast } from '@lobehub/ui/base-ui';import { createStaticStyles } from 'antd-style';+import { useState } from 'react';+import { useTranslation } from 'react-i18next';++import { useActiveWorkspaceId } from '@/business/client/hooks/useActiveWorkspaceId';+import { useUserMemoryStore } from '@/store/userMemory';const styles = createStaticStyles(({ css, cssVar }) => ({title: css`@@ -11,10 +17,43 @@ const styles = createStaticStyles(({ css, cssVar }) => ({}));const PersonaHeader = () => {+ const { t } = useTranslation(['memory', 'common']);+ const workspaceId = useActiveWorkspaceId();+ const deletePersona = useUserMemoryStore((s) => s.deletePersona);+ const [loading, setLoading] = useState(false);++ const confirmDelete = () =>+ confirmModal({+ title: t('persona.delete.title'),+ content: t('persona.delete.confirm'),+ cancelText: t('cancel', { ns: 'common' }),+ okText: t('delete', { ns: 'common' }),+ okButtonProps: { danger: true },+ onOk: async () => {+ setLoading(true);+ try {+ await deletePersona();+ toast.success(t('persona.delete.success'));+ } catch (error) {+ toast.error(t('persona.delete.error'));+ throw error;+ } finally {+ setLoading(false);+ }+ },+ });+return (- <Text as={'h1'} className={styles.title}>- Persona- </Text>+ <Flexbox horizontal align={'center'} gap={16} justify={'space-between'}>+ <Text as={'h1'} className={styles.title}>+ Persona+ </Text>+ {!workspaceId && (+ <Button danger loading={loading} size={'small'} onClick={confirmDelete}>+ {t('persona.delete.action')}+ </Button>+ )}+ </Flexbox>);};diff --git a/src/services/userMemory/crud.ts b/src/services/userMemory/crud.tsindex cfae46edae0..f778727dff6 100644--- a/src/services/userMemory/crud.ts+++ b/src/services/userMemory/crud.ts@@ -5,6 +5,10 @@ import { lambdaClient } from '@/libs/trpc/client';class MemoryCRUDService {// ============ Identity CRUD ============+ deletePersona = async () => {+ return lambdaClient.userMemory.deletePersona.mutate();+ };+deleteAll = async () => {return lambdaClient.userMemory.deleteAll.mutate();};diff --git a/src/store/userMemory/slices/base/action.test.ts b/src/store/userMemory/slices/base/action.test.tsindex 8fefd272d41..9fb117b9342 100644--- a/src/store/userMemory/slices/base/action.test.ts+++ b/src/store/userMemory/slices/base/action.test.ts@@ -91,3 +91,30 @@ describe('userMemory base actions', () => {});});});++describe('deletePersona', () => {+ const persona = { content: 'My persona', summary: 'Summary' };++ beforeEach(() => {+ useUserMemoryStore.setState({ persona, personaInit: true });+ vi.mocked(mutate).mockImplementation(async (_key: any, update: any) => update());+ });++ it('clears the persona cache while retaining the memory lists', async () => {+ vi.spyOn(memoryCRUDService, 'deletePersona').mockResolvedValue({ success: true });+ await useUserMemoryStore.getState().deletePersona();+ expect(useUserMemoryStore.getState().persona).toBeUndefined();+ expect(useUserMemoryStore.getState().preferences).toEqual([preferenceMemory]);+ expect(useUserMemoryStore.getState().preferencesTotal).toBe(1);+ expect(mutate).toHaveBeenCalledWith(userMemoryKeys.persona(), expect.any(Function), {+ revalidate: false,+ });+ });++ it('retains the persona and memories when deletion fails', async () => {+ vi.spyOn(memoryCRUDService, 'deletePersona').mockRejectedValue(new Error('offline'));+ await expect(useUserMemoryStore.getState().deletePersona()).rejects.toThrow('offline');+ expect(useUserMemoryStore.getState().persona).toEqual(persona);+ expect(useUserMemoryStore.getState().preferences).toEqual([preferenceMemory]);+ });+});diff --git a/src/store/userMemory/slices/base/action.ts b/src/store/userMemory/slices/base/action.tsindex c9525bd2d22..b132b2b6332 100644--- a/src/store/userMemory/slices/base/action.ts+++ b/src/store/userMemory/slices/base/action.ts@@ -51,6 +51,20 @@ export class BaseActionImpl {);};+ /** PersonaHeader confirmation deletes the persona through memoryCRUDService, then clears its cache. */+ deletePersona = async (): Promise<void> => {+ const { memoryCRUDService } = await import('@/services/userMemory');+ await mutate(+ userMemoryKeys.persona(),+ async () => {+ await memoryCRUDService.deletePersona();+ this.#set({ persona: undefined, personaInit: true }, false, n('deletePersona'));+ return null;+ },+ { revalidate: false },+ );+ };+purgeAllMemories = async (): Promise<void> => {const { memoryCRUDService } = await import('@/services/userMemory');