diff --git a/semif_agent/gateway/simplex.py b/semif_agent/gateway/simplex.py index a019baf..58bfe17 100644 --- a/semif_agent/gateway/simplex.py +++ b/semif_agent/gateway/simplex.py @@ -186,14 +186,25 @@ class SimplexAdapter(GatewayAdapter): async def _maybe_accept(self, ws, resp: dict) -> None: if not self.auto_accept: return - contact_id = (resp.get("contactRequest") or {}).get("contactId") - if contact_id is None: + req = resp.get("contactRequest") or {} + # v7 names the field contactRequestId_ (the trailing underscore is the + # generated-API optional marker) and /_accept takes the *request* id; + # older events exposed contactId. Fall back so both shapes work. + req_id = req.get("contactRequestId") + if req_id is None: + req_id = req.get("contactId_") or req.get("contactId") + if req_id is None: return self._corr += 1 await ws.send( - json.dumps({"corrId": f"sf-{self._corr}", "cmd": f"/_accept {contact_id}"}) + json.dumps({"corrId": f"sf-{self._corr}", "cmd": f"/_accept {req_id}"}) + ) + contact_id = req.get("contactId_") or req.get("contactId") + self._event( + "gateway_accepted", + contact_request_id=str(req_id), + contact_id=str(contact_id) if contact_id is not None else "", ) - self._event("gateway_accepted", contact_id=str(contact_id)) def _schedule_flush(self, message: InboundMessage, on_inbound) -> None: chat = message.chat_id diff --git a/tests/test_gateway.py b/tests/test_gateway.py index 6dd2c71..175b2f4 100644 --- a/tests/test_gateway.py +++ b/tests/test_gateway.py @@ -115,6 +115,44 @@ def test_send_command_is_structured_not_shortcut(): assert command != "/_send @4 hello" +# ---- adapter: contact request acceptance ---- + +class _FakeWS: + def __init__(self): + self.sent = [] + + async def send(self, data): + self.sent.append(json.loads(data)["cmd"]) + + +def _contact_request_event(req): + return json.dumps( + {"resp": {"type": "receivedContactRequest", "contactRequest": req}} + ) + + +def test_accept_uses_v7_contact_request_id(): + # v7 exposes contactRequestId (and contactId_); /_accept takes the request id. + adapter = SimplexAdapter({"auto_accept": True}) + ws = _FakeWS() + asyncio.run(adapter._consume(_contact_request_event({"contactRequestId": 7, "contactId_": 9}), lambda m: None, ws)) + assert ws.sent == ["/_accept 7"] + + +def test_accept_falls_back_to_contact_id_for_legacy_events(): + adapter = SimplexAdapter({"auto_accept": True}) + ws = _FakeWS() + asyncio.run(adapter._consume(_contact_request_event({"contactId": 4}), lambda m: None, ws)) + assert ws.sent == ["/_accept 4"] + + +def test_accept_disabled_sends_nothing(): + adapter = SimplexAdapter({"auto_accept": False}) + ws = _FakeWS() + asyncio.run(adapter._consume(_contact_request_event({"contactRequestId": 7, "contactId_": 9}), lambda m: None, ws)) + assert ws.sent == [] + + # ---- adapter: batching ---- def test_batching_concatenates_rapid_messages():