fix: WeChat session context via X-Hermes-Session-Id (revert SessionRouter)
SessionRouter approach failed on Linux because chat_bridge.py has
hardcoded Windows paths for provider config and opencode.db.
Instead, use the same pattern as xmpp_agent_core.py: direct Hermes API
call with X-Hermes-Session-Id=wechat-{sender_id} for per-user sessions.
This gives each WeChat user persistent context in Hermes without
depending on cross-platform path issues in chat_bridge.py.
This commit is contained in:
@@ -1,16 +1,9 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""WeChat webhook receiver v2 - receives messages from docker-wechatbot-webhook."""
|
"""WeChat webhook receiver v3 — per-user session context via Hermes session ID."""
|
||||||
|
|
||||||
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
|
||||||
@@ -30,12 +23,6 @@ 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()
|
||||||
|
|
||||||
@@ -55,15 +42,34 @@ def _hermes_worker():
|
|||||||
|
|
||||||
|
|
||||||
def _do_forward(sender, sender_id, text):
|
def _do_forward(sender, sender_id, text):
|
||||||
"""通过 SessionRouter 发送消息(自动带上下文 + 会话管理)。"""
|
"""调用 Hermes API 并回复微信(带 X-Hermes-Session-Id 实现 per-user 上下文)。"""
|
||||||
|
session_id = f"wechat-{sender_id}"
|
||||||
|
payload = json.dumps({
|
||||||
|
"model": "nova-4",
|
||||||
|
"messages": [
|
||||||
|
{"role": "user", "content": f"[微信消息] 来自 {sender}({sender_id}): {text}"}
|
||||||
|
]
|
||||||
|
}).encode()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
reply = _router.route("wechat", sender_id, text)
|
import urllib.request as ureq
|
||||||
log.info(f"Router OK, reply: {reply[:60] if reply else '(empty)'}")
|
handler = ureq.ProxyHandler({})
|
||||||
|
opener = ureq.build_opener(handler)
|
||||||
|
req = ureq.Request(HERMES_API, data=payload,
|
||||||
|
headers={
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Authorization": f"Bearer {HERMES_KEY}",
|
||||||
|
"X-Hermes-Session-Id": session_id,
|
||||||
|
})
|
||||||
|
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 [{sender[:8]}], 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"Router error: {e}")
|
log.error(f"Hermes error [{sender[:8]}]: {e}")
|
||||||
|
|
||||||
|
|
||||||
# 启动 worker 线程
|
# 启动 worker 线程
|
||||||
|
|||||||
Reference in New Issue
Block a user