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: sloved the old removeSessionTopics not work (#11671)

Shinji-Li06d41e5153ec
diff --git a/src/server/routers/lambda/__tests__/integration/topic.integration.test.ts b/src/server/routers/lambda/__tests__/integration/topic.integration.test.ts
index 7cd8dce7048..4dcc1c0362e 100644
--- a/src/server/routers/lambda/__tests__/integration/topic.integration.test.ts
+++ b/src/server/routers/lambda/__tests__/integration/topic.integration.test.ts
@@ -285,6 +285,80 @@ describe('Topic Router Integration Tests', () => {
});
});
+ describe('batchDeleteByAgentId', () => {
+ it('should batch delete topics by agentId (new data)', async () => {
+ const caller = topicRouter.createCaller(createTestContext(userId));
+
+ // Create topics with agentId directly (new data structure)
+ const topicId1 = await caller.createTopic({
+ title: 'Agent Topic 1',
+ agentId: testAgentId,
+ });
+ const topicId2 = await caller.createTopic({
+ title: 'Agent Topic 2',
+ agentId: testAgentId,
+ });
+
+ // Batch delete by agentId
+ await caller.batchDeleteByAgentId({
+ agentId: testAgentId,
+ });
+
+ const remainingTopics = await serverDB.select().from(topics).where(eq(topics.userId, userId));
+
+ expect(remainingTopics).toHaveLength(0);
+ });
+
+ it('should batch delete topics by agentId (legacy sessionId data)', async () => {
+ const caller = topicRouter.createCaller(createTestContext(userId));
+
+ // Create topics with sessionId (legacy data structure)
+ await caller.createTopic({
+ title: 'Legacy Topic 1',
+ sessionId: testSessionId,
+ });
+ await caller.createTopic({
+ title: 'Legacy Topic 2',
+ sessionId: testSessionId,
+ });
+
+ // Batch delete by agentId should also delete legacy topics via sessionId mapping
+ await caller.batchDeleteByAgentId({
+ agentId: testAgentId,
+ });
+
+ const remainingTopics = await serverDB
+ .select()
+ .from(topics)
+ .where(eq(topics.sessionId, testSessionId));
+
+ expect(remainingTopics).toHaveLength(0);
+ });
+
+ it('should batch delete topics by agentId (mixed data)', async () => {
+ const caller = topicRouter.createCaller(createTestContext(userId));
+
+ // Create both new (agentId) and legacy (sessionId) topics
+ await caller.createTopic({
+ title: 'New Agent Topic',
+ agentId: testAgentId,
+ });
+ await caller.createTopic({
+ title: 'Legacy Session Topic',
+ sessionId: testSessionId,
+ });
+
+ // Batch delete by agentId should delete both
+ await caller.batchDeleteByAgentId({
+ agentId: testAgentId,
+ });
+
+ const remainingTopics = await serverDB.select().from(topics).where(eq(topics.userId, userId));
+
+ expect(remainingTopics).toHaveLength(0);
+ });
+ });
+
describe('searchTopics', () => {
it('should search topics using agentId', async () => {
const caller = topicRouter.createCaller(createTestContext(userId));
diff --git a/src/server/routers/lambda/topic.ts b/src/server/routers/lambda/topic.ts
index fc5731e33fe..8a69a0d8a5c 100644
--- a/src/server/routers/lambda/topic.ts
+++ b/src/server/routers/lambda/topic.ts
@@ -76,6 +76,12 @@ export const topicRouter = router({
return ctx.topicModel.batchDelete(input.ids);
}),
+ batchDeleteByAgentId: topicProcedure
+ .input(z.object({ agentId: z.string() }))
+ .mutation(async ({ input, ctx }) => {
+ return ctx.topicModel.batchDeleteByAgentId(input.agentId);
+ }),
+
batchDeleteBySessionId: topicProcedure
.input(
z.object({
diff --git a/src/services/topic/index.ts b/src/services/topic/index.ts
index 9d74e50d224..616e5fccb5f 100644
--- a/src/services/topic/index.ts
+++ b/src/services/topic/index.ts
@@ -109,6 +109,10 @@ export class TopicService {
return lambdaClient.topic.batchDeleteBySessionId.mutate({ id: this.toDbSessionId(sessionId) });
};
+ removeTopicsByAgentId = (agentId: string) => {
+ return lambdaClient.topic.batchDeleteByAgentId.mutate({ agentId });
+ };
+
batchRemoveTopics = (topics: string[]) => {
return lambdaClient.topic.batchDelete.mutate({ ids: topics });
};
diff --git a/src/store/chat/slices/topic/action.test.ts b/src/store/chat/slices/topic/action.test.ts
index 5cd9a4ee4ac..8c923b558fd 100644
--- a/src/store/chat/slices/topic/action.test.ts
+++ b/src/store/chat/slices/topic/action.test.ts
@@ -28,6 +28,7 @@ vi.mock('zustand/traditional');
vi.mock('@/services/topic', () => ({
topicService: {
removeTopics: vi.fn(),
+ removeTopicsByAgentId: vi.fn(),
removeAllTopic: vi.fn(),
removeTopic: vi.fn(),
cloneTopic: vi.fn(),
@@ -570,7 +571,7 @@ describe('topic action', () => {
await result.current.removeSessionTopics();
});
- expect(topicService.removeTopics).toHaveBeenCalledWith(activeAgentId);
+ expect(topicService.removeTopicsByAgentId).toHaveBeenCalledWith(activeAgentId);
expect(refreshTopicSpy).toHaveBeenCalled();
expect(switchTopicSpy).toHaveBeenCalled();
});
diff --git a/src/store/chat/slices/topic/action.ts b/src/store/chat/slices/topic/action.ts
index 8a155135107..b600e2ec2a9 100644
--- a/src/store/chat/slices/topic/action.ts
+++ b/src/store/chat/slices/topic/action.ts
@@ -567,7 +567,7 @@ export const chatTopic: StateCreator<
const { switchTopic, activeAgentId, refreshTopic } = get();
if (!activeAgentId) return;
- await topicService.removeTopics(activeAgentId);
+ await topicService.removeTopicsByAgentId(activeAgentId);
await refreshTopic();
// switch to default topic