Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.
Python agent-orchestrationaiai-agentschatopsdiscordlangchainmcpmicrosoft-teamsmulti-agentopen-sourceopenaiself-hostedslack
Address review: file posts follow the conversation as of their first file
Simon Flacke65cfb668a06
diff --git a/core/switch_core/db/stores/message_store.py b/core/switch_core/db/stores/message_store.pyindex d0cd58f55..935fc5d84 100644--- a/core/switch_core/db/stores/message_store.py+++ b/core/switch_core/db/stores/message_store.py@@ -2,6 +2,7 @@ from collections.abc import Collectionfrom datetime import datetimefrom sqlalchemy import (+ ColumnElement,bindparam,func,or_,@@ -328,8 +329,10 @@ class MessageStore:*,thread_root_event_id: str,limit: int,+ before_seq: int | None = None,) -> list[Message]:- """A thread's newest messages, its root included, newest first."""+ """A thread's newest messages, its root included, newest first; only+ those written before `before_seq` when it is given."""result = await session.execute(select(Message).where(@@ -338,6 +341,7 @@ class MessageStore:Message.thread_root_event_id == thread_root_event_id,Message.transport_event_id == thread_root_event_id,),+ *_before(before_seq),).order_by(Message.seq.desc()).limit(limit)@@ -345,15 +349,22 @@ class MessageStore:return list(result.scalars().all())async def list_top_level_newest_first(- self, session: AsyncSession, room_id: str, *, limit: int+ self,+ session: AsyncSession,+ room_id: str,+ *,+ limit: int,+ before_seq: int | None = None,) -> list[Message]:- """The room's newest top-level messages, newest first. Replies inside- threads are left out."""+ """The room's newest top-level messages, newest first; only those+ written before `before_seq` when it is given. Replies inside threads+ are left out."""result = await session.execute(select(Message).where(Message.room_id == room_id,Message.thread_root_event_id.is_(None),+ *_before(before_seq),).order_by(Message.seq.desc()).limit(limit)@@ -434,3 +445,8 @@ _ROOM_HEAD = RawStatement(Message.room_id == bindparam("room_id")))+++def _before(seq: int | None) -> list[ColumnElement[bool]]:+ """The condition keeping rows written before `seq`; none when not given."""+ return [] if seq is None else [Message.seq < seq]diff --git a/core/switch_core/delivery/conversation.py b/core/switch_core/delivery/conversation.pyindex bb26b53e3..ec456ca77 100644--- a/core/switch_core/delivery/conversation.py+++ b/core/switch_core/delivery/conversation.py@@ -168,6 +168,10 @@ class ConversationMessage:content: Mapping[str, object]thread_root_id: str | Nonesent_at: datetime+ # Set for a message whose rows were written a while before it is decided+ # (a multi-file post): the rules then read only rows written before it,+ # not what was written in the room while it waited.+ before_seq: int | None = Nonedef may_follow(@@ -364,6 +368,7 @@ class RoomConversation:self._room_id,thread_root_event_id=thread_root_id,limit=THREAD_LOOKBACK + 1,+ before_seq=self._message.before_seq,)self._thread_rows = self._before_message(rows, THREAD_LOOKBACK)return self._thread_rows@@ -371,7 +376,10 @@ class RoomConversation:async def _channel(self, session: AsyncSession) -> list[Message]:if self._channel_rows is None:rows = await self._rules.message_store.list_top_level_newest_first(- session, self._room_id, limit=CHANNEL_LOOKBACK + 1+ session,+ self._room_id,+ limit=CHANNEL_LOOKBACK + 1,+ before_seq=self._message.before_seq,)self._channel_rows = self._before_message(rows, CHANNEL_LOOKBACK)return self._channel_rowsdiff --git a/core/switch_core/delivery/recipient.py b/core/switch_core/delivery/recipient.pyindex ef2034be6..3721cd9c4 100644--- a/core/switch_core/delivery/recipient.py+++ b/core/switch_core/delivery/recipient.py@@ -865,14 +865,14 @@ class AgentRecipient:The message is anchored on the first file (index 0) when it arrived,so its id and time are the ones the sender was given, whichever file- completed it. `lead` is the file the rest is taken from when the first- is missing, and whose thread and sender name the message carries: the- file that completed the post, or for a post given up on, the first of- its files written. A post given up on (`complete` False) says in its+ completed it, and its thread is the message's (`post_anchor`). `lead`+ is the file the rest is taken from when the first is missing, and+ whose sender name the message carries: the file that completed the+ post, or for a post given up on, the first of its files written. A post given up on (`complete` False) says in itsbody how many files arrived."""meta = self._meta- anchor = first_file(files) or lead+ anchor = post_anchor(files, lead)gate = _GateOutcome(addressed=False, refusal=None)followed = Falseif named:@@ -889,7 +889,8 @@ class AgentRecipient:"Media received without sender name, using the sender id: %s",sender_name,)- thread_id = lead.thread_root_id+ # The thread the rules routed the post by (`_post_as_message`).+ thread_id = anchor.thread_root_idgroup = parse_attachment_group(lead.content)total = group[2] if group is not None else 1@@ -1936,6 +1937,21 @@ def first_file(parts: list[InboundMedia]) -> InboundMedia | None:return next((part for part in parts if file_index(part) == 0), None)+def post_anchor(parts: list[InboundMedia], lead: InboundMedia) -> InboundMedia:+ """The file a post's text, thread and time are taken from: its first+ file, or `lead` when the first is missing."""+ return first_file(parts) or lead+++def caption_of(file: InboundMedia) -> str:+ """The text the sender wrote with a file, or empty when there is none.++ Without a caption a file's body is its filename (`media_content`); with+ one, the body is the caption and the filename is carried apart.+ """+ return (file.body or "") if "filename" in file.content else ""++def command_event(row: InboundCustomEvent) -> CommandEvent:"""A command as the row it was written in reads, with the ids the rowalone knows filled in.diff --git a/core/switch_core/delivery/router.py b/core/switch_core/delivery/router.pyindex 5c0c6f252..63f42eb35 100644--- a/core/switch_core/delivery/router.py+++ b/core/switch_core/delivery/router.py@@ -112,9 +112,10 @@ from switch_core.delivery.recipient import (Delivery,RecipientServices,RoomMember,+ caption_of,command_event,file_index,- first_file,+ post_anchor,)from switch_core.logging_context import log_contextfrom switch_core.observability.catalogue import ROUTING_FAILURES@@ -1172,7 +1173,16 @@ class MessageRouter:# Not stamped until written (`_Unwritten`); the database stamps it# with its own clock a moment from now.conversation = self._conversation_of(- reads, recipients, event, sent_at=datetime.now(UTC)+ reads,+ recipients,+ ConversationMessage(+ event_id=event.event_id,+ sender=event.sender,+ body=event.body or "",+ content=event.content,+ thread_root_id=event.thread_root_id,+ sent_at=datetime.now(UTC),+ ),)if conversation is None:return _Plan()@@ -1192,23 +1202,13 @@ class MessageRouter:self,reads: RoomReads,recipients: list[AgentRecipient],- event: InboundMessage,- *,- sent_at: datetime,+ message: ConversationMessage,) -> RoomConversation | None:- """`event`'s room as the follow-the-conversation rules read it, for a- message (or a post's first file) that names nobody; None when+ """`message`'s room as the follow-the-conversation rules read it, for+ a message or a post of files that names nobody; None when`may_follow` settles that no rule can apply, without a read."""rules = self._conversationmeta = reads.meta- message = ConversationMessage(- event_id=event.event_id,- sender=event.sender,- body=event.body or "",- content=event.content,- thread_root_id=event.thread_root_id,- sent_at=sent_at,- )if not recipients or not may_follow(rules.settings, channel_type=meta.channel_type, message=message):@@ -1267,8 +1267,7 @@ class MessageRouter:lead=event,complete=True,key=key,- # Decided as it is written, as a text message is.- sent_at=datetime.now(UTC),+ live=event,),group=step,)@@ -1283,21 +1282,29 @@ class MessageRouter:lead: InboundMedia,complete: bool,key: str,- sent_at: datetime,+ live: InboundMedia | None,) -> list[tuple[AgentRecipient, Delivery]]:"""Each agent's delivery of a post of one or more files.When no file names any agent in the room, the post is the room'sconversation, and following the conversation may still hand it toone, as it does a text message (`_follow_the_conversation`): the rules- read the first file, whose text and thread are the post's, and the- agent is given the post with all its files (CHOO-4067).+ read the post as its anchor (`post_anchor`) has it, its caption and+ thread, and the agent is given the post with all its files+ (CHOO-4067).++ `live` is the file being written now, when one is: the post is+ decided as that file is written. A post given up on by the sweep+ (`_decide_due`) has none."""# Whether any file names the agent: the text and its mentions ride on# the first file alone, so every file is asked, not only the lead.- # An agent this cannot be worked out for misses the post.+ # An agent this cannot be worked out for misses the post, and as it+ # may have been named by alias or role, nor does the post follow the+ # conversation to anyone else.named: dict[str, bool] = {}+ undecided = Falsefor recipient in recipients:async def name(recipient: AgentRecipient = recipient) -> Delivery:@@ -1307,15 +1314,17 @@ class MessageRouter:return _NAMES_NOBODYnamed_here = await _decided(recipient, lead, name)- if named_here is not NOTHING:+ if named_here is NOTHING:+ undecided = True+ else:named[recipient.agent.id] = named_here.namedrecipients = [r for r in recipients if r.agent.id in named]conversation = (None- if any(named.values())+ if undecided or any(named.values())else self._conversation_of(- reads, recipients, first_file(files) or lead, sent_at=sent_at+ reads, recipients, _post_as_message(files, lead=lead, live=live)))deliveries = []@@ -1778,7 +1787,6 @@ class MessageRouter:if member.client_id != senderand int(member.joined_at.timestamp() * 1000) <= began]- first = first_file(files) or leadplan = _Plan(deliveries=await self._decide_post(session,@@ -1788,10 +1796,7 @@ class MessageRouter:lead=lead,complete=complete,key=group_id,- # When its first file was sent: the post is decided only now,- # its wait over, and how long ago that was is not a gap in- # the conversation.- sent_at=datetime.fromtimestamp(first.timestamp / 1000, UTC),+ live=None,))stored = await self._store_own_events(session, reads, plan.deliveries, lead)@@ -1924,6 +1929,36 @@ def _unwritten(message: Message) -> _Unwritten:_NAMES_NOBODY = Delivery(own_event=None, after=None)+def _post_as_message(+ files: list[InboundMedia], *, lead: InboundMedia, live: InboundMedia | None+) -> ConversationMessage:+ """A post of files as the follow-the-conversation rules read it: its+ anchor's caption (empty without one, never the filename standing in for+ it) and thread, the same thread the post is delivered in.++ A post is dated, and the room read, as of its anchor, however it came to+ be decided: when the anchor is the file being written now, as that is+ written; otherwise at the anchor's own time and from the rows before it,+ so neither the wait for the rest of the post nor what was written in the+ room meanwhile counts against it.+ """+ anchor = post_anchor(files, lead)+ if live is not None and anchor.event_id == live.event_id:+ sent_at, before_seq = datetime.now(UTC), None+ else:+ sent_at = datetime.fromtimestamp(anchor.timestamp / 1000, UTC)+ before_seq = anchor.seq+ return ConversationMessage(+ event_id=anchor.event_id,+ sender=anchor.sender,+ body=caption_of(anchor),+ content=anchor.content,+ thread_root_id=anchor.thread_root_id,+ sent_at=sent_at,+ before_seq=before_seq,+ )++# The time on a row not written yet, which nothing deciding it reads._NOT_STAMPED = datetime.fromtimestamp(0, UTC)diff --git a/core/tests/switch_core/db/stores/test_message_store.py b/core/tests/switch_core/db/stores/test_message_store.pyindex a7baeaaf6..40c2c3687 100644--- a/core/tests/switch_core/db/stores/test_message_store.py+++ b/core/tests/switch_core/db/stores/test_message_store.py@@ -499,6 +499,38 @@ class TestConversationHistory:assert [r.transport_event_id for r in rows] == ["$c", "$b"]+ async def test_both_read_only_what_came_before_a_row_when_asked(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ store = MessageStore()+ async with session_factory() as session:+ room = await _make_room(session, "alpha")+ await store.create(session, _message(room.id, "$root"), [])+ await store.create(+ session, _message(room.id, "$r1", thread_root_event_id="$root"), []+ )+ await store.create(session, _message(room.id, "$top"), [])+ await store.create(+ session, _message(room.id, "$r2", thread_root_event_id="$root"), []+ )+ await store.create(session, _message(room.id, "$later"), [])+ await session.commit()+ [top] = await store.list_by_transport_event_ids(session, room.id, ["$top"])++ thread = await store.list_thread_newest_first(+ session,+ room.id,+ thread_root_event_id="$root",+ limit=10,+ before_seq=top.seq,+ )+ channel = await store.list_top_level_newest_first(+ session, room.id, limit=10, before_seq=top.seq+ )++ assert [r.transport_event_id for r in thread] == ["$r1", "$root"]+ assert [r.transport_event_id for r in channel] == ["$root"]+class TestForeignKeyBehaviour:async def test_deleting_the_room_cascades_its_messages(diff --git a/core/tests/switch_core/delivery/test_conversation_journeys.py b/core/tests/switch_core/delivery/test_conversation_journeys.pyindex 2f0ba905f..e0bcf592b 100644--- a/core/tests/switch_core/delivery/test_conversation_journeys.py+++ b/core/tests/switch_core/delivery/test_conversation_journeys.py@@ -45,6 +45,7 @@ from switch_core.db.stores.room_role_store import RoomRoleStorefrom switch_core.db.stores.room_store import RoomStorefrom switch_core.delivery import router as router_modulefrom switch_core.delivery.conversation import ConversationSettings+from switch_core.delivery.recipient import AgentRecipientfrom switch_core.transport import RoomReffrom tests.routing import Process, process_onfrom tests.switch_core.delivery.world import World@@ -562,6 +563,92 @@ class TestAttachments:)assert count == 1+ async def test_a_post_given_up_on_reads_only_what_came_before_it(+ self, room: Journey, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ # Another agent speaks in the thread while the post waits for files+ # that never come: written after the post began, it does not count.+ monkeypatch.setattr(router_module, "ATTACHMENT_GROUP_TIMEOUT_SECONDS", 60.0)+ answer = await room.post("helper", "Send me the screenshots", at=0)+ await room.send_file(+ "alice",+ "one.png",+ at=10,+ thread=answer,+ group={"id": "grp-3", "index": 0, "total": 3},+ )+ # Written after the first file. At the same moment: a member who+ # joined after a post began is not given it, and this world's members+ # joined moments ago, so the file is not moved back past them.+ await room.post("reviewer", "I can look too", at=10, thread=answer)++ await room.after_the_wait()++ assert await room.reached("grp-3") == {"helper"}++ async def test_a_post_is_delivered_in_the_thread_it_was_routed_by(+ self, room: Journey+ ) -> None:+ # Only the first file carries the thread, and the post completes on+ # a later one.+ answer = await room.post("helper", "Send me the screenshots", at=0)+ for index, name in enumerate(("one.png", "two.png")):+ await room.send_file(+ "alice",+ name,+ at=10,+ thread=answer if index == 0 else None,+ group={"id": "grp-4", "index": index, "total": 2},+ )++ assert await room.reached("grp-4") == {"helper"}+ payload = (await room.payloads("grp-4"))["helper"]+ assert payload.thread_id == answer++ async def test_a_slow_post_is_timed_from_its_first_file(+ self, room: Journey+ ) -> None:+ # The first file is inside the window after the agent's answer; the+ # last lands outside it. The post is as old as its first file.+ await room.say("alice", "@helper can you read screenshots?", at=0)+ await room.post("helper", "Yes, send them", at=5)+ await room.send_file(+ "alice", "one.png", at=300, group={"id": "grp-5", "index": 0, "total": 2}+ )+ await room.send_file(+ "alice", "two.png", at=330, group={"id": "grp-5", "index": 1, "total": 2}+ )++ assert await room.reached("grp-5") == {"helper"}++ async def test_a_filename_is_not_read_as_text(self, room: Journey) -> None:+ # With no caption the file's body is its filename, which looks like+ # a mention here. It is no text of the sender's.+ answer = await room.post("helper", "Which report?", at=0)++ told = await room.send_file("alice", "@2024-q3.csv", at=10, thread=answer)++ assert await room.reached(told) == {"helper"}++ async def test_a_post_not_known_to_name_nobody_does_not_follow(+ self, room: Journey, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ # Whether the post names reviewer cannot be worked out. It may have+ # (by alias or role), so it is not handed to the thread's owner.+ answer = await room.post("helper", "Which log?", at=0)+ addressed_by = AgentRecipient.addressed_by++ async def failing(self: AgentRecipient, session: Any, event: Any) -> bool:+ if self.agent.name == room.world.agents["reviewer"].name:+ raise RuntimeError("cannot tell")+ return await addressed_by(self, session, event)++ monkeypatch.setattr(AgentRecipient, "addressed_by", failing)++ told = await room.send_file("alice", "app.log", at=10, thread=answer)++ assert await room.reached(told) == set()+class TestWithTheFlagOff:async def test_nothing_changes(self, world: World) -> None: