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): POST /chats takes kind "group" to make a group of one agent

raullopez-sandbox97ba03f47de6
diff --git a/core/switch_core/chats/service.py b/core/switch_core/chats/service.py
index 4780d2bfd..92f0a4a4b 100644
--- a/core/switch_core/chats/service.py
+++ b/core/switch_core/chats/service.py
@@ -1071,13 +1071,15 @@ class ChatService:
name: str | None,
request_id: str,
greeting: str | None,
+ kind: Literal["group"] | None,
) -> Room:
"""A private room with the agents and members, owned by and joined by
the user.
One agent and no other member makes a direct room, which addresses
- the agent without a mention. Anything else is a group (a private
- channel), where a message names the agents it is for. Every agent must
+ the agent without a mention, unless `kind` asks for a group. Anything
+ else is a group (a private channel), where a message names the agents
+ it is for. Every agent must
be one the user may address; every member a member of the workspace.
Retrying with the same request id finishes whatever an earlier attempt
@@ -1089,13 +1091,13 @@ class ChatService:
Only for a direct chat with one of the user's own agents.
"""
member_user_ids = sorted(set(member_user_ids) - {user.id})
- direct = len(agent_ids) == 1 and not member_user_ids
+ direct = kind != "group" and len(agent_ids) == 1 and not member_user_ids
if greeting is not None:
- if len(agent_ids) != 1:
+ if len(agent_ids) != 1 or kind == "group":
raise ChatError(
422,
"GREETING_NEEDS_ONE_AGENT",
- "Only a chat with one agent opens with its greeting.",
+ "Only a direct chat with one agent opens with its greeting.",
)
if not greeting.strip():
raise ChatError(422, "EMPTY_MESSAGE", "The greeting is empty.")
@@ -1114,6 +1116,9 @@ class ChatService:
)
if member_user_ids:
request["memberUserIds"] = member_user_ids
+ # Only when given, so a request without it hashes as it always has.
+ if kind is not None:
+ request["kind"] = kind
payload_hash = _hash(request)
async with self._lock("create", tenant_id, user.id):
async with tenant_session(self.session_factory, tenant_id) as session:
diff --git a/core/switch_core/gateway/chats.py b/core/switch_core/gateway/chats.py
index 46d722247..5929812d4 100644
--- a/core/switch_core/gateway/chats.py
+++ b/core/switch_core/gateway/chats.py
@@ -17,7 +17,7 @@ import logging
import time
from collections.abc import AsyncIterator
from datetime import datetime
-from typing import Annotated, Any
+from typing import Annotated, Any, Literal
from fastapi import (
APIRouter,
@@ -104,6 +104,9 @@ class CreateChatRequest(CamelModel):
# What the agent says as it joins, instead of its usual line; older
# clients send none.
greeting: str | None = None
+ # "group" makes a group even of one agent and nobody else, which would
+ # otherwise be a direct chat; older clients send none.
+ kind: Literal["group"] | None = None
def chosen_agents(self) -> list[str]:
if (self.agent_id is None) == (self.agent_ids is None):
@@ -270,6 +273,7 @@ async def create_chat(
name=req.name,
request_id=req.request_id,
greeting=req.greeting,
+ kind=req.kind,
)
async with tenant_session(service.session_factory, tenant_id) as session:
return ChatEnvelope(
diff --git a/core/tests/switch_core/gateway/test_chats_groups.py b/core/tests/switch_core/gateway/test_chats_groups.py
index 78030e14e..43a497c14 100644
--- a/core/tests/switch_core/gateway/test_chats_groups.py
+++ b/core/tests/switch_core/gateway/test_chats_groups.py
@@ -375,6 +375,58 @@ async def test_one_agent_and_a_person_make_a_group(chats: _Harness) -> None:
assert await _addressed(chats, room_id, agent, "@helper named") is True
+async def test_kind_group_makes_a_group_of_one_agent(chats: _Harness) -> None:
+ agent = await chats.agent("helper", owner=chats.alice)
+ created = await chats.client.post(
+ "/chats",
+ json={"agentId": agent.id, "kind": "group", "requestId": "c1"},
+ headers=chats.as_user(chats.alice),
+ )
+ assert created.status_code == 200, created.text
+ room_id = created.json()["chat"]["roomId"]
+ assert created.json()["chat"]["channelType"] == "channel_private"
+ assert await _channel_type(chats, room_id) == "channel_private"
+ assert room_id in await _listed(chats, chats.alice)
+ assert await _addressed(chats, room_id, agent, "unnamed") is False
+ assert await _addressed(chats, room_id, agent, "@helper named") is True
+
+
+async def test_kind_is_part_of_what_a_retry_must_repeat(chats: _Harness) -> None:
+ agent = await chats.agent("helper", owner=chats.alice)
+
+ async def create(**extra: str) -> httpx.Response:
+ return await chats.client.post(
+ "/chats",
+ json={"agentId": agent.id, "requestId": "c1", **extra},
+ headers=chats.as_user(chats.alice),
+ )
+
+ first = await create(kind="group")
+ assert first.status_code == 200, first.text
+ assert (await create(kind="group")).json()["chat"]["roomId"] == first.json()[
+ "chat"
+ ]["roomId"]
+ other = await create()
+ assert other.status_code == 409
+ assert _code(other) == "REQUEST_REUSED"
+
+
+async def test_a_group_does_not_open_with_a_greeting(chats: _Harness) -> None:
+ agent = await chats.agent("helper", owner=chats.alice)
+ created = await chats.client.post(
+ "/chats",
+ json={
+ "agentId": agent.id,
+ "kind": "group",
+ "greeting": "Hi",
+ "requestId": "c1",
+ },
+ headers=chats.as_user(chats.alice),
+ )
+ assert created.status_code == 422
+ assert _code(created) == "GREETING_NEEDS_ONE_AGENT"
+
+
async def test_a_create_with_people_is_retried_as_one_request(
chats: _Harness,
) -> None: