feat: route WeChat messages through SessionRouter for per-user context
Previously wechat_webhook.py sent each WeChat message as a stateless API call to Hermes (no X-Hermes-Session-Id), so Mohe had zero memory of previous messages. Now uses SessionRouter with SessionBridge (same as XMPP path), which: - Maintains per-user sessions via opencode.db - Injects recent conversation context automatically - Uses the same model/LLM pipeline as XMPP messages
This commit is contained in:
@@ -4,6 +4,13 @@
|
|||||||
import os, sys, json, logging, threading, queue, time
|
import os, sys, json, logging, threading, queue, time
|
||||||
from http.server import HTTPServer, BaseHTTPRequestHandler
|
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||||||
|
|
||||||
|
# ── MoFin session routing (shared with XMPP) ──
|
||||||
|
_SCRIPTS_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "scripts")
|
||||||
|
if _SCRIPTS_DIR not in sys.path:
|
||||||
|
sys.path.insert(0, _SCRIPTS_DIR)
|
||||||
|
from chat_bridge import SessionBridge
|
||||||
|
from session_router import SessionRouter
|
||||||
|
|
||||||
HERMES_API = "http://192.168.1.246:8646/v1/chat/completions"
|
HERMES_API = "http://192.168.1.246:8646/v1/chat/completions"
|
||||||
HERMES_KEY = "hermes123"
|
HERMES_KEY = "hermes123"
|
||||||
PORT = 5804
|
PORT = 5804
|
||||||
@@ -23,6 +30,12 @@ logging.basicConfig(
|
|||||||
)
|
)
|
||||||
log = logging.getLogger("wc-webhook")
|
log = logging.getLogger("wc-webhook")
|
||||||
|
|
||||||
|
# ── Session router (per-user session context via opencode.db) ──
|
||||||
|
_router = SessionRouter(
|
||||||
|
bridge=SessionBridge(session_id="wechat-mohe"),
|
||||||
|
default_session="wechat-mohe",
|
||||||
|
)
|
||||||
|
|
||||||
# ── 消息队列(串行处理,防止并发打爆 Hermes)──────────────
|
# ── 消息队列(串行处理,防止并发打爆 Hermes)──────────────
|
||||||
_msg_queue = queue.Queue()
|
_msg_queue = queue.Queue()
|
||||||
|
|
||||||
@@ -42,29 +55,15 @@ def _hermes_worker():
|
|||||||
|
|
||||||
|
|
||||||
def _do_forward(sender, sender_id, text):
|
def _do_forward(sender, sender_id, text):
|
||||||
"""实际调用 Hermes API 并回复微信。"""
|
"""通过 SessionRouter 发送消息(自动带上下文 + 会话管理)。"""
|
||||||
payload = json.dumps({
|
|
||||||
"model": "nova-4",
|
|
||||||
"messages": [
|
|
||||||
{"role": "user", "content": f"[微信消息] 来自 {sender}({sender_id}): {text}"}
|
|
||||||
]
|
|
||||||
}).encode()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import urllib.request as ureq
|
reply = _router.route("wechat", sender_id, text)
|
||||||
handler = ureq.ProxyHandler({})
|
log.info(f"Router OK, reply: {reply[:60] if reply else '(empty)'}")
|
||||||
opener = ureq.build_opener(handler)
|
|
||||||
req = ureq.Request(HERMES_API, data=payload,
|
|
||||||
headers={"Content-Type": "application/json", "Authorization": f"Bearer {HERMES_KEY}"})
|
|
||||||
resp = opener.open(req, timeout=600)
|
|
||||||
resp_data = json.loads(resp.read())
|
|
||||||
reply = resp_data.get('choices', [{}])[0].get('message', {}).get('content', '')
|
|
||||||
log.info(f"Hermes OK, reply: {reply[:60]}")
|
|
||||||
|
|
||||||
if reply and sender:
|
if reply and sender:
|
||||||
WebhookHandler._send_wechat_static(sender, reply)
|
WebhookHandler._send_wechat_static(sender, reply)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error(f"Hermes error: {e}")
|
log.error(f"Router error: {e}")
|
||||||
|
|
||||||
|
|
||||||
# 启动 worker 线程
|
# 启动 worker 线程
|
||||||
|
|||||||
Reference in New Issue
Block a user