feat: wecom finish-frame overflow splitting + /status channel state
- _stream_close: finish frame capped at 19KB; overflow chunks delivered as aibot_send_msg markdown (dsh-im verified approach, nothing dropped) - channel_state events recorded on channel objects; wecom WS reconnect failures surface in /status as channels[].state, service "degraded" when any channel failed - runtime _safe_event no longer misroutes chatless keys Tests: 71 passed (2 new: split + total-integrity)
This commit is contained in:
1 parent
23c1adcca4
commit
d8cce95adf
4 files changed
+109
-4
No files matched your search
@@ -303,12 +303,32 @@ class WecomAibotChannel:
|
|||||||
st["last"] = now
|
st["last"] = now
|
||||||
self._reply_stream(chat_id, st, st["buf"][-4000:], finished=False)
|
self._reply_stream(chat_id, st, st["buf"][-4000:], finished=False)
|
||||||
|
|
||||||
|
# aibot_respond_msg single-frame limit is 20480 bytes; leave headroom for
|
||||||
|
# the JSON envelope and stream metadata (matches the wecom-app markdown cap)
|
||||||
|
_STREAM_FINISH_BYTES = 19000
|
||||||
|
|
||||||
def _stream_close(self, chat_id: str, final_text: str) -> None:
|
def _stream_close(self, chat_id: str, final_text: str) -> None:
|
||||||
st = self._stream_key_states(chat_id)
|
st = self._stream_key_states(chat_id)
|
||||||
text = final_text or st["buf"]
|
text = final_text or st["buf"]
|
||||||
self._reply_stream(chat_id, st, text, finished=True)
|
# Overflow guard: the finish frame carries the FULL text and WeCom
|
||||||
|
# rejects frames over ~20KB. dsh-im-verified approach: first chunk
|
||||||
|
# closes the bubble, the remainder goes out as separate markdown
|
||||||
|
# messages instead of being silently dropped by the gateway.
|
||||||
|
chunks = split_message_text(text, self._STREAM_FINISH_BYTES)
|
||||||
|
if chunks:
|
||||||
|
self._reply_stream(chat_id, st, chunks[0], finished=True)
|
||||||
|
overflow = chunks[1:]
|
||||||
|
else:
|
||||||
|
self._reply_stream(chat_id, st, "", finished=True)
|
||||||
|
overflow = []
|
||||||
with self._stream_lock:
|
with self._stream_lock:
|
||||||
self._streams.pop(chat_id, None)
|
self._streams.pop(chat_id, None)
|
||||||
|
if overflow:
|
||||||
|
self._logger.info(
|
||||||
|
f"[wecom] finish frame overflow: {len(overflow)} extra chunk(s) sent as markdown"
|
||||||
|
)
|
||||||
|
for chunk in overflow:
|
||||||
|
self.reply(chat_id, chunk)
|
||||||
|
|
||||||
def _reply_stream(self, chat_id: str, st: dict, text: str, finished: bool) -> None:
|
def _reply_stream(self, chat_id: str, st: dict, text: str, finished: bool) -> None:
|
||||||
# official: reuse the CALLBACK's req_id in headers; stream.id identifies the bubble
|
# official: reuse the CALLBACK's req_id in headers; stream.id identifies the bubble
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ class StatusHTTP:
|
|||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
self.started = time.time()
|
self.started = time.time()
|
||||||
|
self.channels: list = [] # injected by runtime.main() after build_channels
|
||||||
self._server: ThreadingHTTPServer | None = None
|
self._server: ThreadingHTTPServer | None = None
|
||||||
self._thread: threading.Thread | None = None
|
self._thread: threading.Thread | None = None
|
||||||
outer = self
|
outer = self
|
||||||
@@ -46,13 +47,26 @@ class StatusHTTP:
|
|||||||
def _route_get(self, path: str, qs: dict) -> None:
|
def _route_get(self, path: str, qs: dict) -> None:
|
||||||
m = outer.mgr
|
m = outer.mgr
|
||||||
if path in ("/", "/status"):
|
if path in ("/", "/status"):
|
||||||
|
channels = []
|
||||||
|
with m._lock:
|
||||||
|
# channels registered on the runtime are injected here
|
||||||
|
# by main() (see set_channel_registry) so /status can
|
||||||
|
# report per-channel connection state incl. wecom WS
|
||||||
|
for ch in outer.channels:
|
||||||
|
channels.append({
|
||||||
|
"name": getattr(ch, "name", str(ch)),
|
||||||
|
"state": getattr(ch, "connection_state", "unknown"),
|
||||||
|
"error": getattr(ch, "connection_error", "") or None,
|
||||||
|
})
|
||||||
|
degraded = any(c["state"] in ("failed",) for c in channels)
|
||||||
self._json(
|
self._json(
|
||||||
200,
|
200,
|
||||||
{
|
{
|
||||||
"service": "agenthub",
|
"service": "agenthub",
|
||||||
"status": "running",
|
"status": "degraded" if degraded else "running",
|
||||||
"uptime_s": round(time.time() - outer.started, 1),
|
"uptime_s": round(time.time() - outer.started, 1),
|
||||||
"agents": m.list_agents(),
|
"agents": m.list_agents(),
|
||||||
|
"channels": channels,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
elif path == "/healthz":
|
elif path == "/healthz":
|
||||||
|
|||||||
@@ -157,8 +157,14 @@ def _friendly_agent_error(raw: str) -> str:
|
|||||||
|
|
||||||
def _safe_event(ch, key: str, kind: str, payload: dict[str, Any]) -> None:
|
def _safe_event(ch, key: str, kind: str, payload: dict[str, Any]) -> None:
|
||||||
try:
|
try:
|
||||||
chat_id = key.split(":", 1)[1]
|
target, chat_id = key.split(":", 1)
|
||||||
ch.push_event(chat_id, kind, payload)
|
if target == ch.name and chat_id != "__channel__":
|
||||||
|
ch.push_event(chat_id, kind, payload)
|
||||||
|
# channel_state (and other chatless events) are recorded on the
|
||||||
|
# channel object for the /status endpoint, not pushed per-chat
|
||||||
|
elif kind == "channel_state":
|
||||||
|
ch.connection_state = payload.get("state", ch.connection_state)
|
||||||
|
ch.connection_error = payload.get("error", "")
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
log.warning(f"[{ch.name}] push_event {kind} failed: {e}")
|
log.warning(f"[{ch.name}] push_event {kind} failed: {e}")
|
||||||
|
|
||||||
@@ -189,6 +195,7 @@ def main(argv: list[str] | None = None) -> None:
|
|||||||
|
|
||||||
http_cfg = cfg.get("http", {})
|
http_cfg = cfg.get("http", {})
|
||||||
http = StatusHTTP(mgr, host=http_cfg.get("listen_host", "127.0.0.1"), port=int(http_cfg.get("port", 30990)))
|
http = StatusHTTP(mgr, host=http_cfg.get("listen_host", "127.0.0.1"), port=int(http_cfg.get("port", 30990)))
|
||||||
|
http.channels = chans # /status reports per-channel connection state
|
||||||
http.start()
|
http.start()
|
||||||
log.info(f"status http on {http.host}:{http.port}")
|
log.info(f"status http on {http.host}:{http.port}")
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
"""Regressions: finish-frame overflow splitting + /status channel state."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
|
||||||
|
|
||||||
|
from agenthub.channels.wecom import WecomAibotChannel # noqa: E402
|
||||||
|
|
||||||
|
|
||||||
|
class MockWS:
|
||||||
|
def __init__(self):
|
||||||
|
self.sent = []
|
||||||
|
|
||||||
|
def connect(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def recv(self, timeout=1.0):
|
||||||
|
return None
|
||||||
|
|
||||||
|
def send(self, raw):
|
||||||
|
self.sent.append(json.loads(raw))
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def _channel() -> tuple[WecomAibotChannel, MockWS]:
|
||||||
|
ws = MockWS()
|
||||||
|
ch = WecomAibotChannel({"bot_id": "b", "secret": "s"}, ws_factory=lambda cfg: ws)
|
||||||
|
ch._ws = ws
|
||||||
|
return ch, ws
|
||||||
|
|
||||||
|
|
||||||
|
def test_small_final_single_stream_frame():
|
||||||
|
ch, ws = _channel()
|
||||||
|
ch._req_ids["c1"] = "r1"
|
||||||
|
ch._stream_close("c1", "hello")
|
||||||
|
streams = [m for m in ws.sent if m.get("cmd") == "aibot_respond_msg"]
|
||||||
|
assert len(streams) == 1
|
||||||
|
assert streams[0]["body"]["stream"]["finish"] is True
|
||||||
|
assert streams[0]["body"]["stream"]["content"] == "hello"
|
||||||
|
|
||||||
|
|
||||||
|
def test_large_final_splits_into_stream_plus_markdown():
|
||||||
|
ch, ws = _channel()
|
||||||
|
ch._req_ids["c1"] = "r1"
|
||||||
|
big = "字" * 12000 # 3 bytes/char -> 36000 bytes > 19000 limit
|
||||||
|
ch._stream_close("c1", big)
|
||||||
|
streams = [m for m in ws.sent if m.get("cmd") == "aibot_respond_msg"]
|
||||||
|
sends = [m for m in ws.sent if m.get("cmd") == "aibot_send_msg"]
|
||||||
|
assert len(streams) == 1
|
||||||
|
assert streams[0]["body"]["stream"]["finish"] is True
|
||||||
|
# first chunk in the finish frame, remainder as markdown messages
|
||||||
|
assert len(streams[0]["body"]["stream"]["content"].encode()) <= 19000
|
||||||
|
assert len(sends) >= 1
|
||||||
|
total = (streams[0]["body"]["stream"]["content"] + "".join(
|
||||||
|
m["body"]["markdown"]["content"] for m in sends))
|
||||||
|
assert total == big # nothing lost
|
||||||
|
for m in sends:
|
||||||
|
assert len(m["body"]["markdown"]["content"].encode("utf-8")) <= 20000
|
||||||
Reference in new issue
Block a user