Skip to content

🤯 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.

🐛 fix(thread): drop standalone delete for subagent threads (#21997)

AmAzing-168296f9a978
diff --git a/packages/database/src/models/__tests__/topics/topic.characterization.test.ts b/packages/database/src/models/__tests__/topics/topic.characterization.test.ts
index 21b79baf8a2..00387e2f951 100644
--- a/packages/database/src/models/__tests__/topics/topic.characterization.test.ts
+++ b/packages/database/src/models/__tests__/topics/topic.characterization.test.ts
@@ -1,8 +1,18 @@
+import { ThreadType } from '@lobechat/types';
import { eq } from 'drizzle-orm';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { getTestDB } from '../../../core/getTestDB';
-import { agentOperations, agents, chatGroups, sessions, topics, users } from '../../../schemas';
+import {
+ agentOperations,
+ agents,
+ chatGroups,
+ messages,
+ sessions,
+ threads,
+ topics,
+ users,
+} from '../../../schemas';
import type { LobeChatDatabase } from '../../../type';
import * as topicModule from '../../topic';
import { TopicModel } from '../../topic';
@@ -241,6 +251,35 @@ describe('TopicModel - characterization', () => {
expect(await topicModel.purge([])).toEqual([]);
});
+ it('keeps subagent threads while trashed and purges them with the topic', async () => {
+ await serverDB.insert(threads).values({
+ id: 'subagent-thread',
+ metadata: { sourceToolCallId: 'toolu_1' },
+ topicId: 'agent-topic',
+ type: ThreadType.Isolation,
+ userId,
+ });
+ await serverDB.insert(messages).values({
+ id: 'subagent-message',
+ role: 'assistant',
+ threadId: 'subagent-thread',
+ topicId: 'agent-topic',
+ userId,
+ });
+ const threadIds = async () =>
+ (await serverDB.select({ id: threads.id }).from(threads)).map((row) => row.id);
+ const messageIds = async () =>
+ (await serverDB.select({ id: messages.id }).from(messages)).map((row) => row.id);
+
+ await topicModel.softDelete(['agent-topic'], { deletedAt });
+ expect(await threadIds()).toEqual(['subagent-thread']);
+ expect(await messageIds()).toEqual(['subagent-message']);
+
+ expect(await topicModel.purge(['agent-topic'])).toEqual(['agent-topic']);
+ expect(await threadIds()).toEqual([]);
+ expect(await messageIds()).toEqual([]);
+ });
+
it('batchDeleteIncludingTrashed removes trashed rows too', async () => {
await topicModel.softDelete(['agent-topic'], { deletedAt });
await topicModel.batchDeleteIncludingTrashed(['agent-topic', 'visitor-topic']);
diff --git a/src/features/AgentSidebar/Topic/TopicListContent/ThreadList/ThreadItem/index.tsx b/src/features/AgentSidebar/Topic/TopicListContent/ThreadList/ThreadItem/index.tsx
index 49efca4408a..57e3c72356f 100644
--- a/src/features/AgentSidebar/Topic/TopicListContent/ThreadList/ThreadItem/index.tsx
+++ b/src/features/AgentSidebar/Topic/TopicListContent/ThreadList/ThreadItem/index.tsx
@@ -39,6 +39,7 @@ const ThreadItem = memo<ThreadItemProps>(({ title, id, isSubagent, sourceMessage
);
const dropdownMenu = useThreadItemDropdownMenu({
+ deletable: !isSubagent,
id,
sourceMessageId,
title,
diff --git a/src/features/AgentSidebar/Topic/TopicListContent/ThreadList/ThreadItem/useDropdownMenu.test.tsx b/src/features/AgentSidebar/Topic/TopicListContent/ThreadList/ThreadItem/useDropdownMenu.test.tsx
index 8518cc7970a..353ce1f9476 100644
--- a/src/features/AgentSidebar/Topic/TopicListContent/ThreadList/ThreadItem/useDropdownMenu.test.tsx
+++ b/src/features/AgentSidebar/Topic/TopicListContent/ThreadList/ThreadItem/useDropdownMenu.test.tsx
@@ -52,6 +52,17 @@ describe('useThreadItemDropdownMenu', () => {
expect(getMenuItem(items, 'delete')).toMatchObject({ disabled: true });
});
+ it('offers no delete for a subagent thread', () => {
+ const { result } = renderHook(() =>
+ useThreadItemDropdownMenu({ deletable: false, id: 'thread-1', title: 'Thread' }),
+ );
+ const items = result.current();
+
+ expect(getMenuItem(items, 'delete')).toBeUndefined();
+ expect(getMenuItem(items, 'rename')).toBeDefined();
+ expect(items.at(-1)).not.toMatchObject({ type: 'divider' });
+ });
+
it('renames through a modal, not an inline popover', async () => {
const { result } = renderHook(() =>
useThreadItemDropdownMenu({ id: 'thread-1', title: 'Old title' }),
diff --git a/src/features/AgentSidebar/Topic/TopicListContent/ThreadList/ThreadItem/useDropdownMenu.tsx b/src/features/AgentSidebar/Topic/TopicListContent/ThreadList/ThreadItem/useDropdownMenu.tsx
index 6917637ccd0..ae8ace6572a 100644
--- a/src/features/AgentSidebar/Topic/TopicListContent/ThreadList/ThreadItem/useDropdownMenu.tsx
+++ b/src/features/AgentSidebar/Topic/TopicListContent/ThreadList/ThreadItem/useDropdownMenu.tsx
@@ -9,12 +9,18 @@ import { usePermission } from '@/hooks/usePermission';
import { useChatStore } from '@/store/chat';
interface ThreadItemDropdownMenuProps {
+ /**
+ * Subagent threads are the expanded detail of their parent tool call and are
+ * cleaned up with their topic, so they pass `false` to drop delete.
+ */
+ deletable?: boolean;
id: string;
sourceMessageId?: string;
title: string;
}
export const useThreadItemDropdownMenu = ({
+ deletable = true,
id,
sourceMessageId,
title,
@@ -54,31 +60,36 @@ export const useThreadItemDropdownMenu = ({
},
sfSymbol: 'pencil',
},
- {
- type: 'divider' as const,
- },
- {
- danger: true,
- disabled: !canEditThread,
- icon: <Icon icon={Trash} />,
- key: 'delete',
- label: t('delete', { ns: 'common' }),
- onClick: () => {
- confirmModal({
- cancelText: t('cancel', { ns: 'common' }),
- content: t('actions.confirmRemoveThread'),
- okButtonProps: { danger: true },
- okText: t('delete', { ns: 'common' }),
- onOk: async () => {
- await removeThread(id);
+ ...(deletable
+ ? [
+ {
+ type: 'divider' as const,
},
- title: t('delete', { ns: 'common' }),
- });
- },
- sfSymbol: 'trash',
- },
+ {
+ danger: true,
+ disabled: !canEditThread,
+ icon: <Icon icon={Trash} />,
+ key: 'delete',
+ label: t('delete', { ns: 'common' }),
+ onClick: () => {
+ confirmModal({
+ cancelText: t('cancel', { ns: 'common' }),
+ content: t('actions.confirmRemoveThread'),
+ okButtonProps: { danger: true },
+ okText: t('delete', { ns: 'common' }),
+ onOk: async () => {
+ await removeThread(id);
+ },
+ title: t('delete', { ns: 'common' }),
+ });
+ },
+ sfSymbol: 'trash',
+ },
+ ]
+ : []),
].filter(Boolean) as DropdownItem[];
}, [
+ deletable,
id,
sourceMessageId,
canEditThread,
diff --git a/src/features/Portal/Thread/useMoreMenu.test.ts b/src/features/Portal/Thread/useMoreMenu.test.ts
index df7fb071c1d..83e42aeeb0d 100644
--- a/src/features/Portal/Thread/useMoreMenu.test.ts
+++ b/src/features/Portal/Thread/useMoreMenu.test.ts
@@ -72,6 +72,14 @@ describe('useThreadMoreMenu', () => {
expect(mocks.closeThreadPortal).toHaveBeenCalledOnce();
});
+ it('offers no delete for a subagent thread', () => {
+ mocks.thread = { ...mocks.thread, metadata: { sourceToolCallId: 'toolu_1' } };
+ const { result } = renderHook(() => useThreadMoreMenu());
+
+ expect(result.current!.delete).toBeUndefined();
+ expect(result.current!.rename).toBeDefined();
+ });
+
it('renames the thread from its current title', async () => {
const { result } = renderHook(() => useThreadMoreMenu());
result.current!.rename!();
diff --git a/src/features/Portal/Thread/useMoreMenu.ts b/src/features/Portal/Thread/useMoreMenu.ts
index d415d66827d..5d7b91b2aea 100644
--- a/src/features/Portal/Thread/useMoreMenu.ts
+++ b/src/features/Portal/Thread/useMoreMenu.ts
@@ -3,6 +3,7 @@ import isEqual from 'fast-deep-equal';
import { useTranslation } from 'react-i18next';
import { openRenameModal } from '@/components/RenameModal';
+import { isSubagentThread } from '@/features/AgentSidebar/Topic/TopicListContent/ThreadList/subagent';
import { type PortalMoreMenuConfig } from '@/features/Portal/components/PortalMoreMenu/types';
import { usePermission } from '@/hooks/usePermission';
import { useChatStore } from '@/store/chat';
@@ -49,20 +50,22 @@ export const useThreadMoreMenu = (): PortalMoreMenuConfig | undefined => {
return {
copyId: threadId,
copyLink: shareUrl,
- delete: canEdit
- ? () =>
- confirmModal({
- cancelText: t('cancel', { ns: 'common' }),
- content: t('actions.confirmRemoveThread'),
- okButtonProps: { danger: true },
- okText: t('delete', { ns: 'common' }),
- onOk: async () => {
- await removeThread(threadId);
- closeThreadPortal();
- },
- title: t('delete', { ns: 'common' }),
- })
- : undefined,
+ // Subagent threads are cleaned up with their topic, not one by one.
+ delete:
+ canEdit && !isSubagentThread(thread)
+ ? () =>
+ confirmModal({
+ cancelText: t('cancel', { ns: 'common' }),
+ content: t('actions.confirmRemoveThread'),
+ okButtonProps: { danger: true },
+ okText: t('delete', { ns: 'common' }),
+ onOk: async () => {
+ await removeThread(threadId);
+ closeThreadPortal();
+ },
+ title: t('delete', { ns: 'common' }),
+ })
+ : undefined,
refresh: () =>
Promise.all([
refreshThreads(),
diff --git a/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ThreadList/ThreadItem/index.tsx b/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ThreadList/ThreadItem/index.tsx
index 5d771ca5cd4..34d46e34e04 100644
--- a/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ThreadList/ThreadItem/index.tsx
+++ b/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ThreadList/ThreadItem/index.tsx
@@ -10,6 +10,7 @@ import Actions from './Actions';
import { useThreadItemDropdownMenu } from './useDropdownMenu';
export interface ThreadItemProps {
+ deletable?: boolean;
id: string;
index: number;
isSubagent?: boolean;
@@ -18,7 +19,7 @@ export interface ThreadItemProps {
const SUBAGENT_PADDING_INLINE_START = 32;
-const ThreadItem = memo<ThreadItemProps>(({ title, id, isSubagent }) => {
+const ThreadItem = memo<ThreadItemProps>(({ title, id, isSubagent, deletable }) => {
const activeThreadId = useChatStore((s) => s.activeThreadId);
const { navigateToThread, isInAgentSubRoute } = useThreadNavigation();
@@ -28,6 +29,7 @@ const ThreadItem = memo<ThreadItemProps>(({ title, id, isSubagent }) => {
}, [id, navigateToThread]);
const dropdownMenu = useThreadItemDropdownMenu({
+ deletable,
id,
title,
});
diff --git a/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ThreadList/ThreadItem/useDropdownMenu.test.tsx b/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ThreadList/ThreadItem/useDropdownMenu.test.tsx
index bc87c82d4de..e3f71725aa1 100644
--- a/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ThreadList/ThreadItem/useDropdownMenu.test.tsx
+++ b/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ThreadList/ThreadItem/useDropdownMenu.test.tsx
@@ -52,6 +52,17 @@ describe('group useThreadItemDropdownMenu', () => {
expect(getMenuItem(items, 'delete')).toMatchObject({ disabled: true });
});
+ it('offers no delete for a subagent thread', () => {
+ const { result } = renderHook(() =>
+ useThreadItemDropdownMenu({ deletable: false, id: 'thread-1', title: 'Thread' }),
+ );
+ const items = result.current();
+
+ expect(getMenuItem(items, 'delete')).toBeUndefined();
+ expect(getMenuItem(items, 'rename')).toBeDefined();
+ expect(items.at(-1)).not.toMatchObject({ type: 'divider' });
+ });
+
it('renames through a modal, not an inline popover', async () => {
const { result } = renderHook(() =>
useThreadItemDropdownMenu({ id: 'thread-1', title: 'Old title' }),
diff --git a/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ThreadList/ThreadItem/useDropdownMenu.tsx b/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ThreadList/ThreadItem/useDropdownMenu.tsx
index 1e557545cea..1da9beac91d 100644
--- a/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ThreadList/ThreadItem/useDropdownMenu.tsx
+++ b/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ThreadList/ThreadItem/useDropdownMenu.tsx
@@ -9,11 +9,17 @@ import { usePermission } from '@/hooks/usePermission';
import { useChatStore } from '@/store/chat';
interface ThreadItemDropdownMenuProps {
+ /**
+ * Subagent threads are the expanded detail of their parent tool call and are
+ * cleaned up with their topic, so they pass `false` to drop delete.
+ */
+ deletable?: boolean;
id: string;
title: string;
}
export const useThreadItemDropdownMenu = ({
+ deletable = true,
id,
title,
}: ThreadItemDropdownMenuProps): (() => DropdownItem[]) => {
@@ -40,29 +46,33 @@ export const useThreadItemDropdownMenu = ({
},
sfSymbol: 'pencil',
},
- {
- type: 'divider' as const,
- },
- {
- danger: true,
- disabled: !canEditThread,
- icon: <Icon icon={Trash} />,
- key: 'delete',
- label: t('delete', { ns: 'common' }),
- onClick: () => {
- confirmModal({
- cancelText: t('cancel', { ns: 'common' }),
- content: t('actions.confirmRemoveThread'),
- okButtonProps: { danger: true },
- okText: t('delete', { ns: 'common' }),
- onOk: async () => {
- await removeThread(id);
+ ...(deletable
+ ? [
+ {
+ type: 'divider' as const,
},
- title: t('delete', { ns: 'common' }),
- });
- },
- sfSymbol: 'trash',
- },
+ {
+ danger: true,
+ disabled: !canEditThread,
+ icon: <Icon icon={Trash} />,
+ key: 'delete',
+ label: t('delete', { ns: 'common' }),
+ onClick: () => {
+ confirmModal({
+ cancelText: t('cancel', { ns: 'common' }),
+ content: t('actions.confirmRemoveThread'),
+ okButtonProps: { danger: true },
+ okText: t('delete', { ns: 'common' }),
+ onOk: async () => {
+ await removeThread(id);
+ },
+ title: t('delete', { ns: 'common' }),
+ });
+ },
+ sfSymbol: 'trash',
+ },
+ ]
+ : []),
].filter(Boolean) as DropdownItem[];
- }, [id, canEditThread, removeThread, title, updateThreadTitle, t]);
+ }, [id, canEditThread, deletable, removeThread, title, updateThreadTitle, t]);
};
diff --git a/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ThreadList/index.tsx b/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ThreadList/index.tsx
index eb068e37ab5..d99ee4a86f8 100644
--- a/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ThreadList/index.tsx
+++ b/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ThreadList/index.tsx
@@ -2,6 +2,7 @@ import { ThreadType } from '@lobechat/types';
import { ScrollShadow } from '@lobehub/ui';
import { memo } from 'react';
+import { isSubagentThread } from '@/features/AgentSidebar/Topic/TopicListContent/ThreadList/subagent';
import { useFetchThreads } from '@/hooks/useFetchThreads';
import { useScrollActiveThreadIntoView } from '@/hooks/useScrollActiveThreadIntoView';
import { useChatStore } from '@/store/chat';
@@ -34,6 +35,7 @@ const ThreadList = memo(() => {
>
{threads?.map((item, index) => (
<ThreadItem
+ deletable={!isSubagentThread(item)}
id={item.id}
index={index}
isSubagent={item.type === ThreadType.Isolation}