bridges: daemon-backed message history + true unread

The in-memory inbox is a receive buffer, not the daemon's read state, so
the bridge could not see messages that arrived while it was down. Query
/_get chats <userId> count=<n> with a ChatListQuery unread filter
(chatStats{unreadCount, minUnreadItemId} + chatItem.meta.itemStatus
rcvNew/rcvRead) and /_get chat @<id> count=<n> over the existing
corrId->Future path, and expose GET /unread and GET /history?contact=<id>.
Both are read-only (v7 has no mark-read command) and degrade like
/address: 503 not connected, 502 lookup failure, 400 without a contact.
The live /inbox buffer is unchanged.
This commit is contained in:
Denton Social committed 2026-09-30 12:10:34 -05:00
1 parent 4a4fe850e0
commit e93db6ec09
6 files changed
+564 -17

No files matched your search

+60
View File
@@ -43,6 +43,17 @@ class SimplexBridge(BridgeService):
"GET /inbox/next?contact=<id> -> 200 {\"message\": {...}|null} — pop "
"the oldest unread message (optionally from one contact); null means "
"none buffered. The bridge owns the read cursor",
"GET /unread -> 200 {\"chats\": [{\"contact_id\", \"display_name\", "
"\"unread_count\", \"min_unread_item_id\", \"unread\", \"messages\": "
"[{item_id, text, status, unread, direction, sent_at}]}]} — the "
"daemon's persistent unread chats (survives bridge restarts), not "
"just what arrived while connected. Read-only; 503 when the daemon "
"is not connected, 502 when the lookup fails",
"GET /history?contact=<id>&count=<n> -> 200 {\"contact_id\", "
"\"messages\": [{item_id, text, status, unread, direction, "
"sent_at}]} — recent message history for one contact (count default "
"20); status is rcvNew (unread) or rcvRead. Read-only; 503/502 as "
"/unread, 400 without a contact",
"GET /address -> 200 {\"short_link\", \"full_link\", \"created\"} — "
"the agent's contact link; creates it on first call. 503 when the "
"daemon is not connected, 502 when the lookup fails",
@@ -139,6 +150,12 @@ class SimplexBridge(BridgeService):
if path == "/inbox/next":
contact = (query.get("contact") or [None])[0]
return 200, {"message": self.inbox.pop(contact)}
if path == "/unread":
return self._unread(query)
if path == "/history":
contact = (query.get("contact") or [None])[0]
count = (query.get("count") or [None])[0]
return self._history(contact, count)
if path == "/address":
return self._address()
return 404, {"error": "not found"}
@@ -176,6 +193,49 @@ class SimplexBridge(BridgeService):
self._event("contacts_refreshed", count=len(contacts))
return self.inbox.contacts()
def _unread(self, query: dict) -> tuple[int, dict]:
"""The daemon's persistent unread chats (not the live receive buffer).
Read-only: there is no mark-read command in v7, so this never consumes.
Best-effort like `/address` — 503 when the daemon is not connected, 502
when the lookup fails, never a fabricated chat.
"""
count = self._count((query.get("count") or [None])[0])
try:
chats = self.daemon.request_chats(unread_only=True, count=count)
except RuntimeError as exc:
return 503, {"error": f"unread lookup is not available: {exc}"[:300]}
except Exception as exc: # TimeoutError, daemon error
return 502, {"error": f"unread lookup failed: {exc}"[:300]}
return 200, {"chats": chats}
def _history(self, contact: str | None, count: str | None) -> tuple[int, dict]:
"""Recent message history for one contact, straight from the daemon.
Read-only. A missing contact is a client error; a disconnected or
failing daemon degrades like `/unread`.
"""
contact = str(contact or "").strip()
if not contact:
return 400, {"error": "contact is required"}
try:
messages = self.daemon.request_chat_history(
contact, count=self._count(count)
)
except RuntimeError as exc:
return 503, {"error": f"history lookup is not available: {exc}"[:300]}
except Exception as exc: # TimeoutError, daemon error
return 502, {"error": f"history lookup failed: {exc}"[:300]}
return 200, {"contact_id": contact, "messages": messages}
@staticmethod
def _count(raw: str | None, default: int = 20, maximum: int = 100) -> int:
try:
value = int(raw) if raw is not None else default
except (TypeError, ValueError):
value = default
return max(1, min(value, maximum))
def _address(self) -> tuple[int, dict]:
try:
link = self.daemon.request_address()
+163
View File
@@ -76,6 +76,86 @@ def contact_link(resp: dict) -> dict:
return {}
def _text_from_content(content: dict) -> str | None:
"""Extract the text of a text message from a `CIContent` union, or None.
Handles the same two shapes as `parse_direct_text_item`: the API form
(`{"type": "rcvMsgContent", "msgContent": {...}}`) and the DB/Aeson form
(`{"rcvMsgContent": {"msgContent": {...}}}`), for both received and sent
messages. Non-text content (files, calls, events) yields None.
"""
for key in ("rcvMsgContent", "sndMsgContent"):
if content.get("type") == key:
msg_content = content.get("msgContent") or {}
elif isinstance(content.get(key), dict):
msg_content = content[key].get("msgContent") or {}
else:
continue
if msg_content.get("type") == "text":
return str(msg_content.get("text", "")).strip()
return None
def parse_chat_item(item: dict) -> dict | None:
"""Normalize one `ChatItem` into a history entry, or None.
Returns `{item_id, text, status, unread, direction, sent_at}`. `status` is
the raw `meta.itemStatus.type` (`rcvNew` / `rcvRead` / `snd*`), and `unread`
is True only for an unread received message. Items without an id are
dropped rather than fabricated.
"""
meta = item.get("meta") or {}
item_id = meta.get("itemId")
if item_id is None:
return None
text = _text_from_content(item.get("content") or {})
if not text:
return None
status = (meta.get("itemStatus") or {}).get("type") or ""
return {
"item_id": str(item_id),
"text": text,
"status": status,
"unread": status == "rcvNew",
"direction": (item.get("chatDir") or {}).get("type") or "",
"sent_at": meta.get("itemTs") or "",
}
def parse_chat(achat: dict) -> dict | None:
"""Normalize one `AChat` into a chat summary, or None for a non-direct chat.
Returns `{contact_id, display_name, unread_count, min_unread_item_id,
unread, messages}` from `chatInfo` + `chatStats` + `chatItems`. A group or
local chat (no direct contact) yields None.
"""
info = achat.get("chatInfo") or {}
if info.get("type") != "direct":
return None
entry = contact_entry(info.get("contact") or {})
if entry is None:
return None
stats = achat.get("chatStats") or {}
try:
unread_count = int(stats.get("unreadCount") or 0)
except (TypeError, ValueError):
unread_count = 0
messages: list[dict] = []
for item in achat.get("chatItems") or []:
parsed = parse_chat_item(item)
if parsed is not None:
messages.append(parsed)
min_unread = stats.get("minUnreadItemId")
return {
"contact_id": entry["id"],
"display_name": entry["display_name"],
"unread_count": unread_count,
"min_unread_item_id": str(min_unread) if min_unread is not None else "",
"unread": bool(stats.get("unreadChat")) or unread_count > 0,
"messages": messages,
}
def parse_direct_text_item(item: dict) -> InboundDict | None:
"""Normalize one `newChatItems` entry into a message dict, or None.
@@ -396,6 +476,89 @@ class SimplexDaemon:
future.cancel()
raise TimeoutError("simplex contacts request timed out") from exc
# ---- chats / history ----
async def chats(
self, unread_only: bool = True, count: int = 20, timeout: float = 20.0
) -> list[dict]:
"""Chat previews from the daemon, optionally only those with unread.
`/_get chats <userId> count=<n> {"type":"filters","favorite":false,
"unread":<bool>}` returns `apiChats`. This reads the daemon's persistent
state, unlike the bridge's in-memory receive buffer. A `chatCmdError`
or malformed reply yields an empty list rather than fabricated chats.
"""
user_id = await self.active_user(timeout)
query = {"type": "filters", "favorite": False, "unread": bool(unread_only)}
command = f"/_get chats {user_id} count={max(1, int(count))} {json.dumps(query)}"
resp = await self._roundtrip(command, timeout)
if resp.get("type") != "apiChats":
return []
chats: list[dict] = []
for achat in resp.get("chats") or []:
parsed = parse_chat(achat)
if parsed is not None:
chats.append(parsed)
return chats
async def chat_history(
self, contact_id: str, count: int = 20, timeout: float = 20.0
) -> list[dict]:
"""Recent messages of one direct chat, newest-aware, from the daemon.
`/_get chat @<contactId> count=<n>` returns `apiChat`; each message
carries `meta.itemStatus` (`rcvNew`/`rcvRead`), so callers can tell
unread from read. A malformed reply yields an empty list.
"""
chat_ref = str(contact_id).strip().lstrip("@")
command = f"/_get chat @{chat_ref} count={max(1, int(count))}"
resp = await self._roundtrip(command, timeout)
if resp.get("type") != "apiChat":
return []
chat = resp.get("chat")
parsed = parse_chat(chat) if isinstance(chat, dict) else None
return parsed["messages"] if parsed is not None else []
def request_chats(
self, unread_only: bool = True, count: int = 20, timeout: float = 20.0
) -> list[dict]:
"""Thread-safe chat-preview lookup for callers off the event loop.
Raises `RuntimeError` when no live connection exists and `TimeoutError`
when the daemon does not answer in time — never a fabricated list.
"""
loop = self._loop
if loop is None or not loop.is_running():
raise RuntimeError("simplex gateway is not connected")
future = asyncio.run_coroutine_threadsafe(
self.chats(unread_only=unread_only, count=count, timeout=timeout), loop
)
try:
return future.result(timeout=timeout + 5.0)
except concurrent.futures.TimeoutError as exc:
future.cancel()
raise TimeoutError("simplex chats request timed out") from exc
def request_chat_history(
self, contact_id: str, count: int = 20, timeout: float = 20.0
) -> list[dict]:
"""Thread-safe per-chat history lookup for callers off the event loop.
Raises `RuntimeError` when no live connection exists and `TimeoutError`
when the daemon does not answer in time — never a fabricated list.
"""
loop = self._loop
if loop is None or not loop.is_running():
raise RuntimeError("simplex gateway is not connected")
future = asyncio.run_coroutine_threadsafe(
self.chat_history(contact_id, count=count, timeout=timeout), loop
)
try:
return future.result(timeout=timeout + 5.0)
except concurrent.futures.TimeoutError as exc:
future.cancel()
raise TimeoutError("simplex chat history request timed out") from exc
# ---- inbound ----
async def _consume(self, raw: Any) -> None: