Files
gabby fc0beb5798 simplex: mark the reported message read so next_message advances
The bridge only read the daemon's unread state (/_get chats, /_get chat);
nothing wrote the read state, so a persistent-unread message was re-reported
on every call and simplex.next_message never advanced.

v7.0.3 does expose /_read chat @<id> and /_read chat items @<id> <ids>
(the earlier "v7 has no mark-read command" note was wrong). Added:

- SimplexDaemon.mark_read / request_mark_read plus read_command /
  parse_read_response; parse_direct_text_item now carries item_id.
- SimpleX bridge POST /read {"contact","item_ids"} marks exactly the listed
  items read on the bridge's own daemon, reporting ok/error honestly.
- The live inbox entry keeps item_id so /inbox/next hands back the
  consumable id.
- simplex.next_message consumes exactly the item it reported (a failed
  consume is surfaced as an honest note, never a fabricated read failure).

Precise per-item reading leaves the rest of the chat unread.
2026-10-09 11:16:21 +00:00

90 lines
3.4 KiB
Python

"""A bounded, thread-safe FIFO of buffered inbound messages + known contacts.
The bridge owns the read cursor so a read skill stays stateless. Generic over
transports: entries are normalized message dicts (`contact_id`, `display_name`,
`text`, `item_id`), which is exactly what `semif_agent.simplex_ws` yields.
"""
from __future__ import annotations
import threading
import time
import uuid
from collections import deque
class MessagingInbox:
def __init__(self, max_size: int = 100):
self._lock = threading.Lock()
self._items: deque[dict] = deque()
self._max = max(1, int(max_size))
self._contacts: dict[str, dict] = {}
def record(self, message: dict) -> dict:
contact_id = str(message.get("contact_id") or "")
display_name = message.get("display_name") or ""
entry = {
"id": uuid.uuid4().hex[:12],
"contact_id": contact_id,
"display_name": display_name,
"text": message.get("text") or "",
# The transport's per-item id (SimpleX `itemId`), when known: lets a
# consumer mark exactly this message read on the daemon.
"item_id": str(message.get("item_id") or ""),
"received_at": time.time(),
}
with self._lock:
self._remember(contact_id, message)
self._items.append(entry)
while len(self._items) > self._max:
self._items.popleft()
return entry
def _remember(self, contact_id: str, contact: dict) -> None:
"""Upsert the cached contact for an id (caller holds the lock)."""
current = self._contacts.setdefault(
contact_id, {"id": contact_id, "display_name": ""}
)
name = contact.get("display_name") or ""
if name:
current["display_name"] = name
# Keep the unique local name and connection health when the source knows
# them; never invent them (an inbound item has no connection state).
for key in ("local_name", "connected", "auth_errors"):
if key in contact and contact[key] not in (None, ""):
current[key] = contact[key]
def peek(self) -> list[dict]:
with self._lock:
return list(self._items)
def pop(self, contact_id: str | None = None) -> dict | None:
with self._lock:
if contact_id is None:
return self._items.popleft() if self._items else None
target = str(contact_id)
for index, entry in enumerate(self._items):
if entry["contact_id"] == target:
del self._items[index]
return entry
return None
def merge_contacts(self, contacts: list[dict]) -> None:
"""Upsert contacts learned from the daemon (not from a message).
A non-empty incoming name overwrites a cached one; an empty name never
clobbers a name already learned from an inbound message. The unique
local name and connection health ride along when the daemon reports
them.
"""
with self._lock:
for contact in contacts:
contact_id = str(contact.get("id") or "")
if not contact_id:
continue
self._remember(contact_id, contact)
def contacts(self) -> list[dict]:
with self._lock:
return [dict(contact) for contact in self._contacts.values()]