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(home): count only the caller's own unread topics in sidebar badges (#22896)

Arvin Xu3c0762a05753
diff --git a/packages/database/src/repositories/home/__tests__/index.test.ts b/packages/database/src/repositories/home/__tests__/index.test.ts
index 35f0d9c1430..2acede4bef5 100644
--- a/packages/database/src/repositories/home/__tests__/index.test.ts
+++ b/packages/database/src/repositories/home/__tests__/index.test.ts
@@ -22,6 +22,7 @@ beforeEach(async () => {
});
afterEach(async () => {
+ await clientDB.delete(Schema.workspaces);
await clientDB.delete(Schema.users);
});
@@ -431,6 +432,57 @@ describe('HomeRepository', () => {
expect(group!.unreadCount).toBe(1);
});
+ it("should not count teammates' unread topics in workspace sidebar badges", async () => {
+ const workspaceId = 'ws-unread-badge';
+ const agentId = 'ws-shared-agent';
+ const groupId = 'ws-shared-group';
+
+ await clientDB.transaction(async (tx) => {
+ await tx.insert(Schema.workspaces).values({
+ id: workspaceId,
+ name: 'Unread Badge Workspace',
+ primaryOwnerId: userId,
+ slug: workspaceId,
+ });
+ await tx.insert(Schema.agents).values({
+ id: agentId,
+ title: 'Shared Agent',
+ userId,
+ virtual: false,
+ workspaceId,
+ });
+ await tx.insert(Schema.chatGroups).values({
+ id: groupId,
+ title: 'Shared Group',
+ userId,
+ workspaceId,
+ });
+ await tx.insert(Schema.topics).values([
+ { agentId, id: 'ws-my-agent-unread', status: 'unread', userId, workspaceId },
+ {
+ agentId,
+ id: 'ws-teammate-agent-unread',
+ status: 'unread',
+ userId: otherUserId,
+ workspaceId,
+ },
+ { groupId, id: 'ws-my-group-unread', status: 'unread', userId, workspaceId },
+ {
+ groupId,
+ id: 'ws-teammate-group-unread',
+ status: 'unread',
+ userId: otherUserId,
+ workspaceId,
+ },
+ ]);
+ });
+
+ const result = await new HomeRepository(clientDB, userId, workspaceId).getSidebarAgentList();
+
+ expect(result.ungrouped.find((i) => i.id === agentId)?.unreadCount).toBe(1);
+ expect(result.ungrouped.find((i) => i.id === groupId)?.unreadCount).toBe(1);
+ });
+
describe('agents.pinned is the only sidebar pin source', () => {
it('should ignore sessions.pinned when agents.pinned is null', async () => {
// Simulate legacy data: agents.pinned is null, but sessions.pinned is true
diff --git a/packages/database/src/repositories/home/index.ts b/packages/database/src/repositories/home/index.ts
index 1839b11d78f..c4e5272e6da 100644
--- a/packages/database/src/repositories/home/index.ts
+++ b/packages/database/src/repositories/home/index.ts
@@ -246,6 +246,8 @@ export class HomeRepository {
groupUnread: Map<string, number>;
}> {
const isUnread = eq(topics.status, 'unread');
+ // A workspace scope matches every member's topics; the badge is the caller's own unread.
+ const isMine = eq(topics.userId, this.userId);
const isMainSidebarTopic = or(
isNull(topics.trigger),
not(inArray(topics.trigger, HOME_UNREAD_EXCLUDE_TRIGGERS)),
@@ -261,6 +263,7 @@ export class HomeRepository {
// Agent-share visitor topics keep the creator's userId — never bump
// the creator's own unread badge for a visitor's conversation.
notShareVisitorTopic(),
+ isMine,
isUnread,
isMainSidebarTopic,
sql`${topics.agentId} is not null`,
@@ -274,6 +277,7 @@ export class HomeRepository {
and(
buildWorkspaceWhere(this.scope, topics),
notShareVisitorTopic(),
+ isMine,
isUnread,
isMainSidebarTopic,
sql`${topics.groupId} is not null`,