-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.py
More file actions
753 lines (685 loc) · 30.6 KB
/
Copy pathplugin.py
File metadata and controls
753 lines (685 loc) · 30.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
from __future__ import annotations
import asyncio
import base64
import hashlib
import logging
import random
import re
import sys
import tempfile
import time
import uuid
from typing import Any, ClassVar, cast
from maibot_sdk import MaiBotPlugin, MessageGateway, PluginConfigBase, Tool
from .config import WemaiPluginSettings
from .constants import WEMAI_GATEWAY_NAME
from .runtime import WemaiWsServer
logger = logging.getLogger("wemai_adapter")
class WemaiAdapterPlugin(MaiBotPlugin):
config_model: ClassVar[type[PluginConfigBase] | None] = WemaiPluginSettings
def __init__(self) -> None:
super().__init__()
self._ws_server: WemaiWsServer | None = None
self._pending_requests: dict[str, asyncio.Future] = {}
self._resp_lock = asyncio.Lock()
self._pending_outbound: list[dict[str, Any]] = []
self._hub_task: asyncio.Task | None = None
async def on_load(self) -> None:
logger.info("on_load 被调用, enabled=%s", self._is_enabled())
if not hasattr(self, "_FRIEND_SEEN"):
self._FRIEND_SEEN: set[str] = set()
try:
await self._restart_server_if_needed()
except Exception as e:
logger.error("插件启动异常(已捕获,插件继续运行): %s", e)
def _is_enabled(self) -> bool:
try:
return self._load_settings().plugin.enabled
except Exception:
return False
async def on_unload(self) -> None:
self._stop_hub_tick()
await self._stop_server()
async def on_config_update(self, scope: str, config_data: dict[str, Any], version: str) -> None:
if scope != "self":
return
old_settings = self._load_settings()
old_enabled = old_settings.plugin.enabled
old_host = old_settings.ws_server.host
old_port = old_settings.ws_server.port
self.set_plugin_config(config_data)
new_settings = self._load_settings()
conn_changed = (
old_enabled != new_settings.plugin.enabled
or old_host != new_settings.ws_server.host
or old_port != new_settings.ws_server.port
)
if conn_changed:
try:
await self._restart_server_if_needed()
except Exception as e:
logger.error("重启 WS 服务器失败: %s", e)
else:
logger.debug("连接配置未变化,跳过服务器重启")
try:
await asyncio.wait_for(self._push_config_to_client(), timeout=5.0)
except (asyncio.TimeoutError, Exception) as e:
logger.debug("推送配置到客户端失败: %s", e)
@MessageGateway(
name=WEMAI_GATEWAY_NAME,
route_type="duplex",
platform="wechat",
protocol="wemai",
description="WeMai 微信双工消息网关",
)
async def handle_wemai_gateway(
self,
message: dict[str, Any],
route: dict[str, Any] | None = None,
metadata: dict[str, Any] | None = None,
) -> dict[str, Any]:
logger.info("handle_wemai_gateway 被调用, message_id=%s route=%s", message.get("message_id", ""), route)
outbound = {
"type": "outbound",
"message_id": message.get("message_id", ""),
"receiver": "",
"segments": [],
}
mi = message.get("message_info", {})
additional = mi.get("additional_config") or {}
outbound["receiver"] = (
additional.get("platform_io_target_user_id")
or additional.get("target_user_id")
or ""
)
if not outbound["receiver"]:
group_info = mi.get("group_info") or {}
user_info = mi.get("user_info") or {}
outbound["receiver"] = (
group_info.get("group_name")
or user_info.get("user_nickname")
or ""
)
raw_msg = message.get("raw_message", [])
if raw_msg:
segments, at_members = self._build_segments_from_raw(raw_msg)
else:
seg = message.get("message_segment", {})
texts = self._extract_text(seg)
segments = [{"type": "text", "data": t} for t in texts if t]
at_members = []
outbound["segments"] = segments
outbound["at_members"] = at_members
if outbound["receiver"] and segments:
ok = await self._send_outbound(outbound)
return {
"success": ok,
"external_message_id": outbound.get("message_id"),
}
return {"success": True}
@staticmethod
def _build_segments_from_raw(raw: list[Any]) -> tuple[list[dict], list[str]]:
result: list[dict] = []
at_members: list[str] = []
emoji_map = {
"laugh": "「呲牙」", "smile": "「微笑」", "cry": "「流泪」", "angry": "「发怒」",
"surprised": "「惊讶」", "fear": "「恐惧」", "cool": "「酷」", "sad": "「难过」",
"shy": "「害羞」", "sleepy": "「困」", "love": "「爱心」", "ok": "「OK」",
"clap": "「鼓掌」", "think": "「思考」", "wave": "「挥手」", "strong": "「强」",
"weak": "「弱」", "rose": "「玫瑰」", "heart": "「爱心」", "broken_heart": "「心碎」",
"cake": "「蛋糕」", "coffee": "「咖啡」", "beer": "「啤酒」",
}
for seg in raw:
if not isinstance(seg, dict):
continue
stype = seg.get("type", "")
sdata = seg.get("data", "")
if stype == "text":
if isinstance(sdata, str):
result.append({"type": "text", "data": sdata})
elif stype == "image":
# 图片:binary_data_base64 → 客户端剪贴板粘贴
image_b64 = seg.get("binary_data_base64", "")
if image_b64:
result.append({"type": "image", "data": image_b64})
elif stype == "video":
# 视频段暂时不回传
pass
elif stype == "emoji":
emoji_b64 = seg.get("binary_data_base64", "")
if emoji_b64:
result.append({"type": "image", "data": emoji_b64})
elif isinstance(sdata, str):
text = emoji_map.get(sdata, f"「{sdata}」")
if text:
result.append({"type": "text", "data": text})
elif isinstance(sdata, dict):
name = sdata.get("emoji_name") or sdata.get("name") or ""
text = emoji_map.get(name, f"「{name}」") if name else ""
if text:
result.append({"type": "text", "data": text})
elif stype == "at":
if isinstance(sdata, str):
at_members.append(sdata)
result.append({"type": "text", "data": f"@{sdata} "})
elif isinstance(sdata, dict):
name = (
sdata.get("user_nickname")
or sdata.get("name")
or sdata.get("target_user_nickname")
or sdata.get("target_user_id")
or ""
)
if name:
at_members.append(name)
result.append({"type": "text", "data": f"@{name} "})
elif stype == "seglist" and isinstance(sdata, (list, tuple)):
sub_segs, sub_ats = WemaiAdapterPlugin._build_segments_from_raw(list(sdata))
result.extend(sub_segs)
at_members.extend(sub_ats)
for seg in result:
if seg.get("type") != "text":
continue
text = seg.get("data", "")
for token in re.split(r"[\s((]+", text):
if token.startswith("@") and len(token) > 1:
name = token[1:].rstrip("))")
if name and name not in at_members:
at_members.append(name)
return result, at_members
def _extract_text(self, seg: Any, collector: list[str] | None = None) -> list[str]:
if collector is None:
collector = []
if isinstance(seg, dict):
stype = seg.get("type", "")
sdata = seg.get("data", "")
if stype == "text" and isinstance(sdata, str):
collector.append(sdata)
elif stype == "seglist" and isinstance(sdata, (list, tuple)):
for s in sdata:
self._extract_text(s, collector)
return collector
async def _handle_client_inbound(self, data: dict[str, Any]) -> None:
msg_type = data.get("type", "")
if msg_type == "sync_config":
await self._push_config_to_client()
return
if msg_type == "moment_response":
req_id = data.get("request_id", "")
if req_id:
async with self._resp_lock:
future = self._pending_requests.pop(req_id, None)
if future and not future.done():
future.set_result(data)
return
if msg_type == "friend_request":
await self._handle_friend_request(data)
return
if msg_type != "inbound":
return
# ── 解析 wxid 优先字段 ──
chat_wxid = data.get("chat", "")
chat_name = data.get("chat_name", chat_wxid)
sender_wxid = data.get("sender", "")
sender_name = data.get("sender_name", sender_wxid)
content = data.get("content", "")
is_group = data.get("is_group", False)
sub_type = data.get("msg_type", "text")
media_path = data.get("media_path", "")
media_base64 = data.get("media_base64", "")
media_ext = data.get("media_ext", ".png")
media_url = data.get("media_url", "")
server_id = data.get("server_id", "")
if not sender_wxid or not content:
return
logger.info("收到入站消息: [%s/%s] %s/%s: %s (%s)",
chat_wxid, chat_name, sender_wxid, sender_name, content[:120], sub_type)
settings = self._load_settings()
if settings.chat.enable_chat_list_filter:
if is_group:
if settings.chat.group_list and not (
chat_wxid in settings.chat.group_list
or chat_name in settings.chat.group_list
):
logger.info("群聊 %s/%s 不在白名单中,已过滤", chat_wxid, chat_name)
return
else:
if settings.chat.private_list and not (
sender_wxid in settings.chat.private_list
or sender_name in settings.chat.private_list
or chat_wxid in settings.chat.private_list
or chat_name in settings.chat.private_list
):
logger.info("私聊 %s/%s 不在白名单中,已过滤", sender_wxid, sender_name)
return
if media_base64:
try:
raw = base64.b64decode(media_base64)
ext = media_ext if media_ext.startswith(".") else "." + media_ext
tmp = tempfile.NamedTemporaryFile(suffix=ext, delete=False)
tmp.write(raw)
tmp.close()
media_path = tmp.name
except Exception as e:
logger.warning("保存媒体文件失败: %s", e)
msg_id = hashlib.md5(
f"{chat_wxid}|{sender_wxid}|{content}|{time.time()}".encode()
).hexdigest()
group_info_val = None
if is_group:
group_info_val = {
"platform": "wechat",
"group_id": chat_wxid,
"group_name": chat_name,
}
if sub_type == "emoji":
seg_data: list[dict] = [{"type": "image", "data": media_url or media_path or content}]
elif sub_type == "image":
seg_data = [{"type": "image", "data": media_url or media_path or content}]
elif sub_type == "video":
seg_data = [{"type": "video", "data": media_url or media_path or content}]
else:
seg_data = [{"type": "text", "data": content}]
# raw_message: 文本段 + 图片/表情段 + 链接卡片段
raw_msg: list[dict] = [{"type": "text", "data": content}]
if media_base64 and sub_type in ("emoji", "image"):
try:
raw_binary = base64.b64decode(media_base64)
image_hash = hashlib.sha256(raw_binary).hexdigest()
except Exception:
image_hash = ""
raw_msg.append({
"type": "image",
"data": "",
"hash": image_hash,
"binary_data_base64": media_base64,
})
elif media_url and sub_type in ("emoji", "image", "video"):
raw_msg.append({
"type": sub_type,
"data": media_url,
})
appmsg_url = data.get("appmsg_url", "")
appmsg_title = data.get("appmsg_title", "")
appmsg_description = data.get("appmsg_description", "")
appmsg_app_name = data.get("appmsg_app_name", "")
if appmsg_url or appmsg_title:
parts: list[str] = []
if appmsg_title:
parts.append(f"标题: {appmsg_title}")
if appmsg_description:
parts.append(f"摘要: {appmsg_description}")
if appmsg_url:
parts.append(f"链接: {appmsg_url}")
if appmsg_app_name:
parts.append(f"来源: {appmsg_app_name}")
raw_msg.append({"type": "text", "data": "[分享链接]\n" + "\n".join(parts)})
message_dict = {
"message_id": msg_id,
"platform": "wechat",
"message_info": {
"platform": "wechat",
"message_id": msg_id,
"time": time.time(),
"user_info": {
"platform": "wechat",
"user_id": sender_name,
"user_nickname": sender_name,
},
"group_info": group_info_val,
"additional_config": {
**({"platform_io_target_group_id": chat_wxid} if is_group else {"platform_io_target_user_id": sender_name}),
},
},
"message_segment": {
"type": "seglist",
"data": seg_data,
},
"raw_message": raw_msg,
}
try:
accepted = await self.ctx.gateway.route_message(
gateway_name=WEMAI_GATEWAY_NAME,
message=message_dict,
)
except RuntimeError as e:
logger.error("入站路由失败(RuntimeError): %s", e)
accepted = False
except Exception as e:
logger.error("入站路由异常: %s", e)
accepted = False
if accepted:
logger.info("入站已注入: [%s/%s] %s/%s: %s",
chat_wxid, chat_name, sender_wxid, sender_name, content[:60])
else:
logger.warning("入站被拒绝: [%s] %s", chat_wxid, sender_wxid)
async def _handle_friend_request(self, data: dict[str, Any]) -> None:
content = data.get("content", "")
details = data.get("details", "")
if content in self._FRIEND_SEEN:
logger.debug("好友请求已处理过,跳过: %s", content)
return
self._FRIEND_SEEN.add(content)
if len(self._FRIEND_SEEN) > 500:
self._FRIEND_SEEN.clear()
logger.info("收到好友请求: %s %s", content, details)
action_hint = (
f"\n你可做以下操作:\n"
f"1. 自行决定 → 使用 hub_approve_friend 或 hub_dismiss_friend,无需回复此会话\n"
f"2. 需要询问管理员 → 直接回复此会话"
)
msg = f"收到好友请求: {content} ({details}){action_hint}"
await self._inject_to_hub("系统", f"friend:{content}", msg)
async def _push_config_to_client(self) -> None:
settings = self._load_settings()
payload = {
"type": "config_update",
"enable_filter": settings.chat.enable_chat_list_filter,
"group_list": settings.chat.group_list,
"private_list": settings.chat.private_list,
"admin": settings.plugin.admin,
"data_source": settings.data_source.mode,
"weflow_base_url": settings.data_source.weflow_base_url,
"weflow_api_token": settings.data_source.weflow_api_token,
"weflow_poll_interval": settings.data_source.weflow_poll_interval,
"send_delay": settings.client.send_delay,
"close_weixin": settings.client.close_weixin,
"include_muted": settings.client.include_muted,
"excluded": settings.client.excluded,
}
await self._send_outbound(payload)
async def _send_outbound(self, data: dict[str, Any]) -> bool:
ws = self._ws_server
if ws is not None:
ok = await ws.send_outbound(data)
if ok:
return True
self._pending_outbound.append(data)
return True
async def _drain_pending_outbound(self) -> None:
if not self._pending_outbound:
return
ws = self._ws_server
if ws is None:
return
batch = list(self._pending_outbound)
self._pending_outbound.clear()
for data in batch:
await ws.send_outbound(data)
if batch:
logger.info("已发送 %d 条排队出站消息", len(batch))
async def _send_request(self, req_type: str, params: dict, timeout: float = 15.0) -> dict:
req_id = str(uuid.uuid4())[:8]
future: asyncio.Future = asyncio.get_event_loop().create_future()
async with self._resp_lock:
self._pending_requests[req_id] = future
await self._send_outbound({"type": req_type, "request_id": req_id, **params})
try:
return await asyncio.wait_for(future, timeout=timeout)
except asyncio.TimeoutError:
async with self._resp_lock:
self._pending_requests.pop(req_id, None)
return {"error": "timeout", "success": False}
@Tool(
name="read_wechat_moments",
description="读取微信朋友圈的最新动态,返回最近发布的朋友圈内容列表。每次最多读取10条。",
parameters={
"type": "object",
"properties": {
"limit": {"type": "integer", "description": "读取条数,最多10条", "default": 5}
},
},
)
async def tool_read_moments(self, limit: int = 5, **kwargs: Any) -> dict:
result = await self._send_request("moment_read", {"limit": min(limit, 10)})
moments = result.get("moments", [])
count = result.get("count", len(moments))
return {"success": True, "count": count, "moments": moments[:limit]}
@Tool(
name="post_wechat_moment",
description="发布一条微信朋友圈,可以带文字内容。发布成功后返回发布结果。",
parameters={
"type": "object",
"properties": {
"text": {"type": "string", "description": "朋友圈的文字内容"}
},
"required": ["text"],
},
)
async def tool_post_moment(self, text: str, **kwargs: Any) -> dict:
result = await self._send_request("moment_post", {"text": text})
ok = result.get("success", False)
return {"success": ok, "text": text, "message": "朋友圈已发布" if ok else "发布失败"}
# ─── 微信系统中枢 ──────────────────────────────
def _start_hub_tick(self) -> None:
self._stop_hub_tick()
self._hub_task = asyncio.create_task(self._hub_tick_loop())
def _stop_hub_tick(self) -> None:
if self._hub_task is not None:
self._hub_task.cancel()
self._hub_task = None
async def _hub_tick_loop(self) -> None:
try:
while True:
await asyncio.sleep(random.randint(180, 600))
await self._inject_to_hub("系统", "tick", "定时检查时间")
except asyncio.CancelledError:
pass
async def _inject_to_hub(self, sender: str, content: str, plain: str = "") -> None:
admins = self._load_settings().plugin.admin
if not admins:
return
for admin in admins:
msg_id = hashlib.md5(f"sys|{admin}|{sender}|{content}|{time.time()}".encode()).hexdigest()
msg = {
"message_id": msg_id,
"platform": "wechat",
"message_info": {
"platform": "wechat",
"message_id": msg_id,
"time": time.time(),
"user_info": {"platform": "wechat", "user_id": admin, "user_nickname": admin},
"group_info": None,
"additional_config": {"platform_io_target_user_id": admin},
},
"message_segment": {"type": "seglist", "data": [{"type": "text", "data": f"[系统消息] {content}"}]},
"raw_message": [{"type": "text", "data": f"[系统消息] {plain or content}"}],
}
ok = await self.ctx.gateway.route_message(gateway_name=WEMAI_GATEWAY_NAME, message=msg)
if ok:
logger.info("系统消息已注入: [%s] %s", admin, content[:40])
async def _inject_to_session(self, chat_name: str, sender: str, content: str, plain: str = "", group_info: dict | None = ...) -> bool:
if group_info is ...:
group_info = {"platform": "wechat", "group_id": chat_name, "group_name": chat_name}
prefix = "hub" if group_info is None else "cross"
msg_id = hashlib.md5(f"{prefix}|{chat_name}|{sender}|{content}|{time.time()}".encode()).hexdigest()
msg = {
"message_id": msg_id,
"platform": "wechat",
"message_info": {
"platform": "wechat",
"message_id": msg_id,
"time": time.time(),
"user_info": {"platform": "wechat", "user_id": sender, "user_nickname": sender},
"group_info": group_info,
},
"message_segment": {"type": "seglist", "data": [{"type": "text", "data": content}]},
"raw_message": [{"type": "text", "data": plain or content}],
}
ok = await self.ctx.gateway.route_message(gateway_name=WEMAI_GATEWAY_NAME, message=msg)
if ok:
logger.info("消息已注入: [%s] %s: %s", chat_name, sender, content[:40])
return ok
@Tool(
name="hub_send_notification",
description="向管理员发送一条系统通知。当需要报告任务结果、提醒注意或通知系统状态时使用。",
parameters={
"type": "object",
"properties": {
"title": {"type": "string", "description": "通知标题"},
"content": {"type": "string", "description": "通知内容"},
},
"required": ["content"],
},
)
async def tool_hub_send_notification(self, title: str = "", content: str = "", **kwargs: Any) -> dict:
if not content:
return {"success": False, "error": "缺少通知内容"}
text = f"系统通知: {title} {content}".strip()
asyncio.create_task(self._inject_to_hub("系统", f"notice:{title}", f"管理员通知: {content[:40]}"))
logger.info("管理员通知: %s", text[:60])
return {"success": True, "message": f"通知已发送: {text[:40]}"}
@Tool(
name="hub_check_chat_status",
description="检查当前所有监控聊天的状态摘要。适合定期巡检,查看各聊天活跃度和待处理事项。",
)
async def tool_hub_check_chat_status(self, **kwargs: Any) -> dict:
return {
"success": True,
"message": "当前所有聊天流运行正常,等待进一步指令。",
}
@Tool(
name="hub_delayed_task",
description="延迟执行一个任务。在指定分钟后向管理员发送提醒。",
parameters={
"type": "object",
"properties": {
"task_desc": {"type": "string", "description": "任务描述"},
"delay_minutes": {"type": "integer", "description": "延迟分钟数", "default": 5},
},
"required": ["task_desc"],
},
)
async def tool_hub_delayed_task(self, task_desc: str = "", delay_minutes: int = 5, **kwargs: Any) -> dict:
if not task_desc:
return {"success": False, "error": "缺少任务描述"}
asyncio.create_task(self._hub_delayed_reminder(task_desc, delay_minutes))
logger.info("延迟任务: %s (%d分钟后)", task_desc[:40], delay_minutes)
return {"success": True, "message": f"已安排任务「{task_desc[:30]}」,{delay_minutes} 分钟后提醒"}
async def _hub_delayed_reminder(self, task_desc: str, delay_minutes: int) -> None:
try:
await asyncio.sleep(delay_minutes * 60)
await self._inject_to_hub("系统", "reminder", f"提醒:{task_desc}")
except asyncio.CancelledError:
pass
@Tool(
name="hub_tell",
description="向指定会话发送一条消息。需要向其他会话发送消息时使用。",
parameters={
"type": "object",
"properties": {
"target": {"type": "string", "description": "目标会话名称"},
"message": {"type": "string", "description": "消息内容"},
},
"required": ["target", "message"],
},
)
async def tool_hub_tell(self, target: str = "", message: str = "", **kwargs: Any) -> dict:
if not target or not message:
return {"success": False, "error": "缺少目标或消息"}
ok = await self._inject_to_session(target, "系统", message)
return {"success": ok, "message": f"已向 {target} 发送消息"}
@Tool(
name="hub_approve_friend",
description="批准一个好友请求。确定可添加对方为好友时直接调用,无需询问管理员。参数: friend_name=对方昵称或验证消息中的名字。",
parameters={
"type": "object",
"properties": {
"friend_name": {"type": "string", "description": "要批准的好友昵称或验证消息中的名字"},
},
"required": ["friend_name"],
},
)
async def tool_hub_approve_friend(self, friend_name: str = "", **kwargs: Any) -> dict:
if not friend_name:
return {"success": False, "error": "缺少好友名称"}
await self._send_outbound({
"type": "friend_approve",
"friend_name": friend_name,
})
logger.info("好友批准指令已发送: %s", friend_name)
return {"success": True, "message": f"已通知客户端批准 {friend_name} 的好友申请"}
@Tool(
name="hub_dismiss_friend",
description="忽略/取消一个好友请求。不添加对方为好友,直接清除通知。确定无需添加时直接调用,无需询问管理员。参数: friend_name=对方昵称或验证消息中的名字。",
parameters={
"type": "object",
"properties": {
"friend_name": {"type": "string", "description": "要忽略的好友昵称或验证消息中的名字"},
},
"required": ["friend_name"],
},
)
async def tool_hub_dismiss_friend(self, friend_name: str = "", **kwargs: Any) -> dict:
if not friend_name:
return {"success": False, "error": "缺少好友名称"}
await self._send_outbound({
"type": "friend_dismiss",
"friend_name": friend_name,
})
logger.info("好友忽略指令已发送: %s", friend_name)
return {"success": True, "message": f"已通知客户端忽略 {friend_name} 的好友申请"}
async def _restart_server_if_needed(self) -> None:
await self._stop_server()
settings = self._load_settings()
if not settings.should_connect() or not settings.validate_runtime_config():
return
self._ensure_server(settings)
if self._ws_server is None:
return
self._ws_server.set_inbound_handler(self._handle_client_inbound)
try:
await self._ws_server.start()
except OSError as e:
logger.error("WS 服务器启动失败(端口被占用?): %s", e)
self._ws_server = None
return
except Exception as e:
logger.error("WS 服务器启动异常: %s", e)
self._ws_server = None
return
await self._drain_pending_outbound()
for retry in range(5):
try:
ok = await self.ctx.gateway.update_state(
gateway_name=WEMAI_GATEWAY_NAME,
ready=True,
platform="wechat",
metadata={"server": settings.ws_server.build_ws_url()},
)
if ok:
logger.info("消息网关状态已就绪")
break
logger.warning("update_state 返回 False (第%d次)", retry + 1)
except RuntimeError as e:
logger.warning("update_state 失败 (第%d次): %s", retry + 1, e)
await asyncio.sleep(0.5)
else:
logger.warning("消息网关状态未就绪,入站消息将无法注入 Host")
logger.info(
"WeMai 适配器已启动: %s",
settings.ws_server.build_ws_url(),
)
async def _stop_server(self) -> None:
if self._ws_server is not None:
await self._ws_server.stop()
self._ws_server = None
try:
await self.ctx.gateway.update_state(
gateway_name=WEMAI_GATEWAY_NAME,
ready=False,
)
except Exception:
pass
def _ensure_server(self, settings: WemaiPluginSettings | None = None) -> None:
if self._ws_server is None:
if settings is None:
settings = self._load_settings()
self._ws_server = WemaiWsServer(
host=settings.ws_server.host,
port=settings.ws_server.port,
)
def _load_settings(self) -> WemaiPluginSettings:
return cast(WemaiPluginSettings, self.config)
def create_plugin() -> WemaiAdapterPlugin:
return WemaiAdapterPlugin()