Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(chats): removing a person updates everyone left in the chat

raullopez-sandbox2567694a96f0
diff --git a/core/switch_core/chats/service.py b/core/switch_core/chats/service.py
index 0d4266031..8c3141d21 100644
--- a/core/switch_core/chats/service.py
+++ b/core/switch_core/chats/service.py
@@ -870,7 +870,9 @@ class ChatService:
room.transport_room_id, client.transport_user_id
)
await self._make_lasting(tenant_id, target_user_id, room.id)
- self._memberships_changed(tenant_id, [target_user_id])
+ async with tenant_session(self.session_factory, tenant_id) as session:
+ member_ids = [member.id for member, _ in await self.members(session, room)]
+ self._memberships_changed(tenant_id, [target_user_id, *member_ids])
async def add_agent(
self, tenant_id: str, actor: User, room_id: str, agent_id: str
diff --git a/core/switch_core/chats/views.py b/core/switch_core/chats/views.py
index 01862ed48..33ba45738 100644
--- a/core/switch_core/chats/views.py
+++ b/core/switch_core/chats/views.py
@@ -19,6 +19,7 @@ from switch_core.db.models import (
Agent,
AgentDefinition,
Client,
+ ClientRoom,
CollaborationBridge,
Message,
Room,
@@ -87,6 +88,8 @@ class ChatSummary(CamelModel):
can_manage: bool
# The viewer owns one of the agents, which keeps them in the room.
owns_agent: bool
+ # People in the room, so a membership change re-sends the summary.
+ member_count: int
last_message: ChatLastMessage | None
@@ -166,6 +169,12 @@ async def chat_summary(
).scalars()
}
bridge = await _bridge(session, room)
+ member_count = await session.scalar(
+ select(func.count())
+ .select_from(ClientRoom)
+ .join(Client, Client.id == ClientRoom.client_id)
+ .where(ClientRoom.room_id == room.id, Client.type == MEMBER_CLIENT_TYPE)
+ )
channel_name: str | None = None
if bridge is not None and room.external_channel_id is not None:
prefix = f"{bridge.display_name}: "
@@ -203,6 +212,7 @@ async def chat_summary(
],
can_manage=can_manage,
owns_agent=any(agent.owner_id == viewer_id for agent in agents),
+ member_count=member_count or 0,
last_message=(
ChatLastMessage(
seq=last.seq,
diff --git a/core/tests/switch_core/gateway/test_chats_groups.py b/core/tests/switch_core/gateway/test_chats_groups.py
index f7ec2ccce..0e6a1c7e7 100644
--- a/core/tests/switch_core/gateway/test_chats_groups.py
+++ b/core/tests/switch_core/gateway/test_chats_groups.py
@@ -20,6 +20,7 @@ from tests.switch_core.gateway.test_chats_routes import (
_left_chat,
_listed,
_reads,
+ _stream,
)
@@ -263,6 +264,33 @@ async def test_a_bridged_direct_room_is_never_converted(chats: _Harness) -> None
assert await _channel_type(chats, room_id) == "direct"
+async def test_removing_a_person_updates_the_others_summary(chats: _Harness) -> None:
+ created = await chats.client.post(
+ "/chats",
+ json={
+ "agentIds": [],
+ "memberUserIds": [chats.bob.id, chats.admin.id],
+ "requestId": "c1",
+ },
+ headers=chats.as_user(chats.alice),
+ )
+ room_id = created.json()["chat"]["roomId"]
+ assert created.json()["chat"]["memberCount"] == 3
+
+ events = _stream(chats, chats.bob, {}, recheck=60.0)
+ name, summary = await events.next()
+ assert (name, summary["memberCount"]) == ("chat", 3)
+ assert (await events.next())[0] == "ready"
+
+ removed = await chats.client.delete(
+ f"/chats/{room_id}/members/{chats.admin.id}", headers=chats.as_user(chats.alice)
+ )
+ assert removed.status_code == 204
+ name, summary = await events.next()
+ assert (name, summary["roomId"], summary["memberCount"]) == ("chat", room_id, 2)
+ await events.stream.aclose() # type: ignore[attr-defined]
+
+
# ── Creating with people ─────────────────────────────────────────────────────