feat(wecom): official aibot long-connection protocol + real WS transport
- wecom_ws.py: AibotWSAdapter (asyncio websockets bridged to channel threads):
connect wss://openws.work.weixin.qq.com, aibot_subscribe auth, 30s ping
heartbeat, reader->queue, graceful shutdown
- wecom.py switched to official frames:
* inbound aibot_msg_callback / aibot_event_callback (enter_chat -> welcome)
* reply: aibot_respond_msg stream {id, finish, content} reusing the
callback's req_id; after finish, proactive aibot_send_msg markdown
* image inbound: {url, aeskey} fetch + AES-256-CBC (IV=key[:16], PKCS7)
* mixed (text+image) and voice (transcribed) message types
- tests updated to official frame shapes; 43 passed
This commit is contained in:
1 parent
62773ab2e5
commit
bbe36be192
6 files changed
+439
-42
No files matched your search
+12
-10
@@ -72,32 +72,34 @@ def test_wecom_dispatch_text_and_permission():
|
||||
ch, ws = _make_wecom()
|
||||
got = []
|
||||
ch.on_message(got.append)
|
||||
ws.incoming.append(
|
||||
json.dumps({"frame_id": "f1", "body": {"msgtype": "text", "from": {"userid": "u1"}, "chatid": "c1", "text": {"content": "hello\u200b"}}})
|
||||
)
|
||||
ch._dispatch(json.loads(ws.incoming[0]), ws)
|
||||
frame = {"cmd": "aibot_msg_callback", "headers": {"req_id": "r1"},
|
||||
"body": {"msgid": "m1", "chatid": "c1", "chattype": "single", "msgtype": "text",
|
||||
"from": {"userid": "u1"}, "text": {"content": "hello\u200b"}}}
|
||||
ch._dispatch(frame, ws)
|
||||
assert len(got) == 1 and got[0].text == "hello" and got[0].chat_id == "c1"
|
||||
assert ch._req_ids["c1"] == "r1" # captured for the streaming reply
|
||||
|
||||
|
||||
def test_wecom_allowed_users_drops_others():
|
||||
ch, ws = _make_wecom()
|
||||
got = []
|
||||
ch.on_message(got.append)
|
||||
ch._dispatch({"frame_id": "f2", "body": {"msgtype": "text", "from": {"userid": "u2"}, "chatid": "c", "text": {"content": "x"}}}, ws)
|
||||
ch._dispatch({"cmd": "aibot_msg_callback", "headers": {"req_id": "r2"}, "body": {"msgtype": "text", "from": {"userid": "u2"}, "chatid": "c", "text": {"content": "x"}}}, ws)
|
||||
assert got == []
|
||||
|
||||
|
||||
def test_wecom_streaming_delta_coalesce_and_final():
|
||||
ch, ws = _make_wecom()
|
||||
ch._ws = ws # pretend connected (stream sends go through the socket)
|
||||
ch._req_ids["c1"] = "req-9" # as set by an inbound callback
|
||||
ch.push_event("c1", "delta", {"text": "hello "})
|
||||
ch.push_event("c1", "delta", {"text": "world"})
|
||||
# force-time independent: final flushes buffer
|
||||
ch.push_event("c1", "final", {"text": "hello world"})
|
||||
replies = [m for m in ws.sent if m["type"] == "reply_stream"]
|
||||
assert replies and replies[-1]["finished"] is True
|
||||
assert replies[-1]["content"] == "hello world"
|
||||
assert len({m["req_id"] for m in replies}) == 1 # same bubble
|
||||
replies = [m for m in ws.sent if m["cmd"] == "aibot_respond_msg"]
|
||||
assert replies and replies[-1]["body"]["stream"]["finish"] is True
|
||||
assert replies[-1]["body"]["stream"]["content"] == "hello world"
|
||||
assert replies[-1]["headers"]["req_id"] == "req-9" # must reuse callback req_id
|
||||
assert len({m["body"]["stream"]["id"] for m in replies}) == 1 # same bubble
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@@ -69,7 +69,7 @@ def test_e2e_wecom_aibot_text_loop():
|
||||
hub.set_event_sink(lambda key, kind, payload: ch.push_event(key.split(":", 1)[1], kind, payload))
|
||||
# inbound frame from user u1 in chat c1
|
||||
ch._dispatch(
|
||||
{"frame_id": "f1", "body": {"msgtype": "text", "from": {"userid": "u1"}, "chatid": "c1", "text": {"content": "帮我看看登录 bug"}}},
|
||||
{"cmd": "aibot_msg_callback", "headers": {"req_id": "req-1"}, "body": {"msgid": "m1", "chatid": "c1", "chattype": "single", "msgtype": "text", "from": {"userid": "u1"}, "text": {"content": "帮我看看登录 bug"}}},
|
||||
ws,
|
||||
)
|
||||
# wait for the turn to finish and events to flow
|
||||
@@ -77,26 +77,29 @@ def test_e2e_wecom_aibot_text_loop():
|
||||
deadline = time.time() + 10
|
||||
while time.time() < deadline:
|
||||
frames = ws.sent + getattr(ch, "_outbox", [])
|
||||
if any('"finished": true' in s for s in frames):
|
||||
if any('"finish": true' in s for s in frames):
|
||||
break
|
||||
time.sleep(0.05)
|
||||
assert any('"finished": true' in s for s in frames), f"no final frame; frames={frames}"
|
||||
assert any('"finish": true' in s for s in frames), f"no final frame; frames={frames}"
|
||||
assert any("chunk" in s for s in frames), f"no delta frames; frames={frames}"
|
||||
assert any("aibot_respond_msg" in s for s in frames)
|
||||
b = hub.get_binding("wecom", "c1")
|
||||
assert b is not None and b.agent == "mock"
|
||||
hub.stop()
|
||||
|
||||
|
||||
def test_e2e_wecom_aibot_image_inbound():
|
||||
import base64
|
||||
hub = make_hub()
|
||||
ws = MockWS()
|
||||
ch = WecomAibotChannel({"bot_id": "b", "secret": "s"}, ws_factory=lambda cfg: ws)
|
||||
got = queue.Queue()
|
||||
ch.on_message(got.put)
|
||||
png = base64.b64encode(b"\x89PNG fake").decode()
|
||||
# aibot image frames carry {url, aeskey}; stub the fetch+decrypt (real path needs 微信 CDN)
|
||||
ch._download_aibot_image = lambda img: b"\x89PNG fake" if img.get("url") else None
|
||||
ch._dispatch(
|
||||
{"frame_id": "f2", "body": {"msgtype": "image", "from": {"userid": "u1"}, "chatid": "c1", "image": {"base64": png}}},
|
||||
{"cmd": "aibot_msg_callback", "headers": {"req_id": "r3"},
|
||||
"body": {"msgid": "m2", "chatid": "c1", "chattype": "single", "msgtype": "image",
|
||||
"from": {"userid": "u1"}, "image": {"url": "https://cdn/x.png", "aeskey": "k"}}},
|
||||
ws,
|
||||
)
|
||||
m = got.get(timeout=5)
|
||||
|
||||
Reference in new issue
Block a user