Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions bridges/wechat.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,23 @@ def _wx_qr_login(config: dict, bot_type: str = _ILINK_DEFAULT_BOT_TYPE,
def _wx_poll_loop(token: str, base_url: str, config: dict) -> str:
"""Returns "stopped", "auth_error", or raises on unexpected fatal error."""
from tools import _wx_thread_local
from bridges import wechat_smart_reply as _sr
session_ctx = runtime.get_session_ctx(config.get("_session_id", "default"))
run_query_cb = session_ctx.run_query
sync_buf = ""
consecutive_failures = 0

session_ctx.wx_send = lambda uid, txt: _wx_send(uid, txt, config)

# Smart-reply panel store (SQLite-backed; falls back to in-memory) and
# contacts loader, lifecycles bound to the poll loop.
_smart_store = _sr.make_store(
timeout_s=float(config.get("wechat_smart_reply_timeout_s",
_sr.DEFAULT_TIMEOUT_S)),
)
_smart_store.start_janitor()
_smart_contacts = _sr.ContactsStore()

while not _wechat_stop.is_set():
try:
result = _wx_get_updates(base_url, token, sync_buf)
Expand Down Expand Up @@ -346,6 +356,20 @@ def _wx_poll_loop(token: str, base_url: str, config: dict) -> str:
evt.set()
continue

# ── Smart-reply: filehelper input routes panel choice ──────
# Only consume the message if there's an active panel and
# the user is responding to it; otherwise fall through so
# they can still use !jobs / etc. from filehelper.
if _sr.is_filehelper(from_uid):
consumed = _sr.handle_filehelper_message(
text, _smart_store,
send_to_target=lambda uid, txt: _wx_send(uid, txt, config),
send_to_filehelper=lambda txt: _wx_send(_sr._FILEHELPER_UID, txt, config),
)
if consumed:
print(clr(f"\n ✓ smart-reply panel resolved", "dim"))
continue

print(clr(f"\n 📩 WeChat [{from_uid[:8]}]: {text}", "cyan"))

# ── Interactive PTY session ────────────────────────────────
Expand Down Expand Up @@ -560,6 +584,27 @@ def _wx_terminal(cmd, uid, skey):
_pending_evt.set()
continue

# ── Smart-reply: whitelisted contact → draft, don't auto-reply ─
if _sr.is_smart_reply_target(from_uid, config, text=text):
label = (msg.get("from_user_nickname")
or msg.get("from_username")
or from_uid[:8])
triggered = _sr.trigger_smart_reply(
target_uid=from_uid,
target_label=str(label),
message=text,
store=_smart_store,
config=config,
send_to_filehelper=lambda txt: _wx_send(_sr._FILEHELPER_UID, txt, config),
contacts=_smart_contacts,
)
if triggered:
print(clr(f" ↳ smart-reply panel sent to filehelper", "dim"))
continue
# Generation failed → fall through to normal dispatch so
# the user still gets *some* response.
print(clr(f" ⚠ smart-reply candidate generation failed; falling back to auto-reply", "yellow"))

# ── Claude query: create job, queue if busy, else run now ──
job = _jobs.create(text, source="wechat")

Expand Down
Loading
Loading