Skip to content

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

feat(chats): group_chats, a fleet-wide flag for group chats

raullopez-sandboxaf38dfb0a9ca
diff --git a/core/switch_core/chats/service.py b/core/switch_core/chats/service.py
index 06e39a359..0f399a294 100644
--- a/core/switch_core/chats/service.py
+++ b/core/switch_core/chats/service.py
@@ -173,11 +173,13 @@ class ChatService:
id_server_name: str,
media_max_bytes: int,
agent_chats_enabled: bool,
+ group_chats_enabled: bool,
connections: AgentConnectionRegistry,
) -> None:
self.session_factory = session_factory
self._connections = connections
self.agent_chats_enabled = agent_chats_enabled
+ self.group_chats_enabled = group_chats_enabled
self.listener = listener
self.activity_listener = activity_listener
self._activity_store = AgentSessionActivityStore()
@@ -265,6 +267,14 @@ class ChatService:
if self._signals is not None:
self._signals.memberships_changed(tenant_id, user_ids, room_ids)
+ def _require_group_chats(self) -> None:
+ """Refuse a group-only request while `group_chats` is off, so a fleet
+ that turned it off (or never on) says so rather than half-serving it."""
+ if not self.group_chats_enabled:
+ raise ChatError(
+ 409, "GROUP_CHATS_OFF", "Group chats are not turned on for this server."
+ )
+
def _lock(self, *key: str) -> asyncio.Lock:
lock = self._locks.get(key)
if lock is None:
@@ -922,6 +932,7 @@ class ChatService:
) -> Room:
"""Manager only, and only an agent the manager may use in this room.
Already in is success. A second agent makes a direct chat a group."""
+ self._require_group_chats()
async with tenant_session(self.session_factory, tenant_id) as session:
room = await self._room(session, tenant_id, room_id)
await self._require_manager(session, tenant_id, actor, room)
@@ -969,6 +980,7 @@ class ChatService:
) -> None:
"""Manager only, with no check on using the agent, so an unwanted
agent can always be taken out. Already gone is success."""
+ self._require_group_chats()
async with tenant_session(self.session_factory, tenant_id) as session:
room = await self._room(session, tenant_id, room_id)
await self._require_manager(session, tenant_id, actor, room)
@@ -1152,6 +1164,7 @@ class ChatService:
"""The ids of the agents the user could add: to a new chat when
`room_id` is None, else to that room, which they must manage, less
the agents already in it. The rule `_require_may_use_agent` applies."""
+ self._require_group_chats()
target_room_id, group_id, present = "", None, set[str]()
if room_id is not None:
room = await self._room(session, tenant_id, room_id)
@@ -1204,6 +1217,8 @@ class ChatService:
"""
member_user_ids = sorted(set(member_user_ids) - {user.id})
direct = kind != "group" and len(agent_ids) == 1 and not member_user_ids
+ if member_user_ids or kind == "group" or not agent_ids:
+ self._require_group_chats()
if greeting is not None:
if len(agent_ids) != 1 or kind == "group":
raise ChatError(
@@ -1452,6 +1467,8 @@ class ChatService:
"""
mentions = sorted(set(mention_agent_ids))
people = sorted(set(mention_user_ids))
+ if people:
+ self._require_group_chats()
request: dict[str, object] = {
"roomId": room_id,
"body": body,
diff --git a/core/switch_core/config.py b/core/switch_core/config.py
index f12a55c33..f13c527af 100644
--- a/core/switch_core/config.py
+++ b/core/switch_core/config.py
@@ -18,6 +18,7 @@ from switch_core.feature_flags import (
AGENT_CHATS,
AGENT_MANAGEMENT,
FOLLOW_THE_CONVERSATION,
+ GROUP_CHATS,
HOSTED_AGENTS,
KNOWN_FEATURE_FLAGS,
SERVICE_CONNECTIONS,
@@ -1308,6 +1309,10 @@ class SwitchConfig(BaseSettings):
def agent_chats_enabled(self) -> bool:
return self.feature_flags[AGENT_CHATS]
+ @property
+ def group_chats_enabled(self) -> bool:
+ return self.feature_flags[GROUP_CHATS]
+
@model_validator(mode="after")
def _validate_outbound_allowed_private_hosts(self) -> "SwitchConfig":
OutboundPolicy.parse(self.outbound_allowed_private_hosts)
diff --git a/core/switch_core/feature_flags.py b/core/switch_core/feature_flags.py
index b77e94689..81ea0cfb0 100644
--- a/core/switch_core/feature_flags.py
+++ b/core/switch_core/feature_flags.py
@@ -63,6 +63,15 @@ AGENT_CHATS = "agent_chats"
# on, so a server without the template is never asked to make one.
SWITCH_BUDDY = "switch_buddy"
+# Group chats in Switch Console: chats with people and with no or several
+# agents, adding and removing a chat's agents, naming people in a message, and
+# the agents a person may add. Turn it on only once every switch-core process
+# runs a release that has it: the guarantee is server-first rollout, then the
+# flag. An upgraded process refuses those requests while it is off, but a
+# process still on an older release ignores the fields it does not know, so
+# turning it on before the rollout finishes is not protected.
+GROUP_CHATS = "group_chats"
+
# Every flag the server recognises.
KNOWN_FEATURE_FLAGS: frozenset[str] = frozenset(
{
@@ -74,5 +83,6 @@ KNOWN_FEATURE_FLAGS: frozenset[str] = frozenset(
FOLLOW_THE_CONVERSATION,
AGENT_CHATS,
SWITCH_BUDDY,
+ GROUP_CHATS,
}
)
diff --git a/core/switch_core/main.py b/core/switch_core/main.py
index 69c7b2585..48e26df8c 100644
--- a/core/switch_core/main.py
+++ b/core/switch_core/main.py
@@ -1157,6 +1157,7 @@ async def run(config: SwitchConfig, process_id: str) -> None:
id_server_name=config.id_server_name,
media_max_bytes=config.agent_media_max_bytes,
agent_chats_enabled=config.agent_chats_enabled,
+ group_chats_enabled=config.group_chats_enabled,
connections=connections,
)
# Changes to people's chats made in another process wake the Console
diff --git a/core/tests/switch_core/gateway/conftest.py b/core/tests/switch_core/gateway/conftest.py
index e83d3fa69..6e82f0389 100644
--- a/core/tests/switch_core/gateway/conftest.py
+++ b/core/tests/switch_core/gateway/conftest.py
@@ -258,6 +258,7 @@ def _chat_service(
id_server_name="switch.test",
media_max_bytes=1024,
agent_chats_enabled=True,
+ group_chats_enabled=True,
connections=connections,
)
room_service.on_room_agents_changed(service.sync_room)
diff --git a/core/tests/switch_core/gateway/test_chats_groups.py b/core/tests/switch_core/gateway/test_chats_groups.py
index b2bd8735e..ffccc9894 100644
--- a/core/tests/switch_core/gateway/test_chats_groups.py
+++ b/core/tests/switch_core/gateway/test_chats_groups.py
@@ -1054,3 +1054,46 @@ async def test_a_change_to_one_chat_re_summarises_only_that_chat(
await events.stream.aclose() # type: ignore[attr-defined]
# The add answers with one summary; each of the two streams sends one.
assert summaries == [room_ids[0]] * 3
+
+
+async def test_group_only_requests_are_refused_while_group_chats_is_off(
+ chats: _Harness,
+) -> None:
+ agent = await chats.agent("helper", owner=chats.alice)
+ other = await chats.agent("other", owner=chats.alice)
+ room_id = await chats.new_chat(chats.alice, agent, "c1")
+ chats.service.group_chats_enabled = False
+
+ refusals = [
+ await chats.client.post(
+ "/chats",
+ json={
+ "agentId": agent.id,
+ "memberUserIds": [chats.bob.id],
+ "requestId": "g1",
+ },
+ headers=chats.as_user(chats.alice),
+ ),
+ await chats.client.post(
+ "/chats",
+ json={"agentIds": [], "requestId": "g2"},
+ headers=chats.as_user(chats.alice),
+ ),
+ await _add_agent(chats, chats.alice.id, room_id, other),
+ await chats.client.delete(
+ f"/chats/{room_id}/agents/{agent.id}", headers=chats.as_user(chats.alice)
+ ),
+ await chats.send(
+ chats.alice, room_id, "s1", "@alice", mentionUserIds=[chats.alice.id]
+ ),
+ await _candidates(chats, chats.alice, room_id),
+ ]
+ assert [(r.status_code, _code(r)) for r in refusals] == [
+ (409, "GROUP_CHATS_OFF")
+ ] * len(refusals)
+ # What was there before group chats still works.
+ assert (await chats.send(chats.alice, room_id, "s2", "hi")).status_code == 200
+ assert (await chats.invite(chats.alice, room_id, chats.bob)).status_code == 200
+
+ chats.service.group_chats_enabled = True
+ assert (await _add_agent(chats, chats.alice.id, room_id, other)).status_code == 200