-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.py
More file actions
616 lines (545 loc) · 23.3 KB
/
Copy pathplugin.py
File metadata and controls
616 lines (545 loc) · 23.3 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
import threading
import os
import base64
from typing import Any, List, ClassVar
import socketserver
from concurrent.futures import Future
from datetime import datetime
import hashlib
from maibot_sdk import MaiBotPlugin, MessageGateway, Field, PluginConfigBase
import asyncio
import time
from plugins.QQPilotAdapter.GroupChatManager import GroupChatManager, buildText
from plugins.QQPilotAdapter.isEmoji import base64ToImage, isEmoji
# from plugins.QQPilotAdapter.plugin import gateway, groupChatManager, prin
import asyncio
import http.server
import json
import re
import time
from typing import List
from uuid import uuid4
from colorama import Fore
class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b'QQPilot Plugin For MaiBot')
def _send_json(self, status_code: int, data: dict):
"""统一发送 JSON 响应"""
body = json.dumps(data, ensure_ascii=False).encode('utf-8')
self.send_response(status_code)
self.send_header('Content-Type', 'application/json; charset=utf-8')
self.send_header('Content-Length', str(len(body)))
self.end_headers()
self.wfile.write(body)
def do_POST(self):
global print
print=gateway.ctx.logger.info
warn=gateway.ctx.logger.warning
error=gateway.ctx.logger.error
"""处理 /v1/chat/completions POST 请求"""
if self.path != '/v1/chat/completions':
self._send_json(404, {"error": f"Not Found: {self.path}"})
return
# 读取并解析请求体
content_length = int(self.headers.get('Content-Length', 0))
try:
raw_body = self.rfile.read(content_length)
body = json.loads(raw_body.decode('utf-8'))
except Exception as e:
self._send_json(400, {"error": f"Invalid JSON: {str(e)}"})
return
# 调试输出
try:
print("\n" + "=" * 70)
print("收到请求")
print("=" * 70)
print("Headers:")
for k, v in self.headers.items():
print(f" {k}: {v}")
print("\nRequest Body (JSON):")
rbody=json.dumps(body, indent=2, ensure_ascii=False)
print(rbody[:50]+('...' if len(rbody)>=50 else '') )
print("=" * 70)
except:
pass
PATTERN = re.compile(
r'\[\s*time\s*\]\s*(.*?)\s*\[\s*username\s*\]\s*(.*?)\s*\[\s*content\s*\]\s*(.*?)\s*(?=\[\s*time\s*\]|$)',
re.DOTALL
)
if not body or 'model' not in body:
self._send_json(400, {"error": "Missing 'model' in request body"})
return
assert(isinstance(body,dict))
ctr:List=body.get('messages') # type: ignore
reply_text = "......"
try:
if gateway._delete_image_in_next_request and gateway._last_saved_images:
deleted_count = 0
for img_path in gateway._last_saved_images:
try:
os.remove(img_path)
deleted_count += 1
print(f"🗑️ 删除上一次回复的图片: {img_path}")
except Exception as e:
print(f"⚠️ 删除图片失败: {img_path}, 错误: {e}")
gateway._last_saved_images.clear()
print(f"✅ 已删除 {deleted_count} 张图片")
chatlist=[]
messages_to_send=[]
all_usernames = set()
containsNewMassage=False
for i in ctr:
if i['role'] != 'user':
continue
content_field = i["content"]
text_content = ""
image_urls = []
emoji_urls = []
if isinstance(content_field, list):
for item in content_field:
if isinstance(item, dict):
item_type = item.get("type", "")
if item_type == "text":
text_content = item.get("text", "")
elif item_type == "image_url":
image_url = item.get("image_url", {})
if isinstance(image_url, dict):
url = image_url.get("url", "")
if url.startswith("data:image"):
# 解析 base64 图片数据
base64_data = url.split(",", 1)[1]
try:
# 使用 isEmoji 模块判断是否为表情包
img = base64ToImage(base64_data)
if isEmoji(img):
emoji_urls.append(url)
else:
image_urls.append(url)
except Exception as e:
# 解析失败时默认当作普通图片处理
image_urls.append(url)
elif isinstance(content_field, str):
text_content = content_field
match = PATTERN.search(text_content)
if match:
timestamp_str = match.group(1).strip()
username = match.group(2).strip()
content = match.group(3).strip()
print(f"✅ time: {timestamp_str}")
print(f"✅ username: {username}")
print(f"✅ content: {content}")
print(f"✅ images: {len(image_urls)}, emojis: {len(emoji_urls)}")
chatlist.append([timestamp_str, username, content])
all_usernames.add(username)
if not groupChatManager.message_exists(buildText(timestamp_str,username, content)):
messages_to_send.append({
"timestamp": timestamp_str,
"username": username,
"content": content,
"images": image_urls,
"emojis": emoji_urls
})
containsNewMassage=True
else:
warn(f"⚠️ 消息已存在,跳过: {username}:{content[:30]}...")
group_id, group_name =groupChatManager.classify_group(chatlist)
print(f'GroupID:{group_id},groupName={group_name}')
if not containsNewMassage:
response = {
"id": f"chatcmpl-{uuid4().hex[:8]}",
"object": "chat.completion",
"created": int(time.time()),
"model": body["model"],
"system_fingerprint": "fp_mai_bot",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": ""
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens":0
}
}
self._send_json(500, response)
return
with gateway._reply_lock:
gateway._replies = []
is_private_chat = len(all_usernames) == 1
print(f"🔍 检测到 {len(all_usernames)} 个不同用户(包括已存在的消息),私聊模式: {is_private_chat}")
for msg in messages_to_send:
timestamp_str = msg["timestamp"]
username = msg["username"]
content = msg["content"]
images = msg.get("images", [])
emojis = msg.get("emojis", [])
print(f"✅ time: {timestamp_str}")
print(f"✅ username: {username}")
print(f"✅ content: {content}")
print(f"✅ images: {len(images)}, emojis: {len(emojis)}")
resultDict={}
resultDict['message_id']=f"qqpilot-{uuid4().hex[:16]}"
resultDict['user_id']=f"qqpilot_user_{hash(username) & 0xFFFFFFFFFFFFFFFF}"
resultDict['nickname']=username
resultDict['message']=content
resultDict['timestamp']=timestamp_str
resultDict['images']=images
resultDict['emojis']=emojis
if is_private_chat:
resultDict['groupID'] = ""
resultDict['groupName'] = ""
print(f"📩 私聊消息,不包含群信息")
else:
resultDict['groupID']=group_id
resultDict['groupName']=group_name
asyncio.run_coroutine_threadsafe(gateway.handle_inbound(resultDict), gateway._loop)
new_messages = [[m["timestamp"], m["username"], m["content"]] for m in messages_to_send]
groupChatManager.add_messages_to_group(group_id, new_messages)
try:
total_timeout = 180
idle_timeout = 30
last_count = -1
wait_time = 0
total_wait_time = 0
while total_wait_time < total_timeout:
with gateway._reply_lock:
current_count = len(gateway._replies)
if current_count > last_count:
last_count = current_count
wait_time = 0
print(f"🔄 收到新回复,当前共 {current_count} 条")
else:
wait_time += 0.5
total_wait_time += 0.5
time.sleep(0.5)
with gateway._reply_lock:
if len(gateway._replies) > 0 and wait_time >= idle_timeout:
print(f"⏰ 连续 {idle_timeout} 秒无新回复,结束等待")
break
with gateway._reply_lock:
if gateway._replies:
reply_text = "[[NEXT]]".join(gateway._replies)
print(f"✅ 收到 {len(gateway._replies)} 条回复,总等待时间: {total_wait_time:.1f}秒")
print(f"✅ 回复内容: {reply_text}")
else:
reply_text = ""
except Exception as e:
warn(f"⚠️ 获取回复失败: {e}")
reply_text = ""
except Exception as e:
print(e)
response = {
"id": f"chatcmpl-{uuid4().hex[:8]}",
"object": "chat.completion",
"created": int(time.time()),
"model": body["model"],
"system_fingerprint": "fp_mai_bot",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": reply_text
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": body.get("max_tokens", 2048),
"completion_tokens": len(reply_text),
"total_tokens": body.get("max_tokens", 2048) + len(reply_text)
}
}
self._send_json(200, response)
ACCOUNT_ID="2134542354565646"
GATEWAY_NAME="qqpilot_gateway2"
PLATFORM="QQPilot"
httpd=None
print=None
warn=None
error=None
groupChatManager=GroupChatManager()
class QQPilotPluginOptions(PluginConfigBase):
"""插件级配置。"""
__ui_label__ = "插件设置"
__ui_order__ = 0
enabled: bool = Field(
default=True,
description="是否启用 QQPilot 适配器。",
json_schema_extra={
"label": "启用适配器",
"order": 0,
},
)
config_version: str = Field(
default="2.0.0",
description="当前配置结构版本。",
json_schema_extra={
"disabled": True,
"hidden": True,
"label": "配置版本",
"order": 99,
},
)
class QQPilotImageOptions(PluginConfigBase):
"""图片相关配置。"""
__ui_label__ = "图片设置"
__ui_order__ = 1
ImagePath: str = Field(
default="images",
description="图片保存路径。",
json_schema_extra={
"label": "图片保存路径",
"order": 0,
},
)
deleteImageInTheNextRequest: bool = Field(
default=False,
description="是否在下一次请求时删除上一次回复生成的图片。",
json_schema_extra={
"label": "下一次请求时删除图片",
"order": 1,
},
)
class Gateway(MaiBotPlugin):
config_model: ClassVar[type[PluginConfigBase] | None] = None
def __init__(self) -> None:
super().__init__()
self._image_path = "images"
self._delete_image_in_next_request = False
self._last_saved_images: List[str] = []
async def on_load(self) -> None:
# 上报网关就绪状态
await self.ctx.gateway.update_state(
gateway_name=GATEWAY_NAME,
ready=True,
platform=PLATFORM,
account_id=ACCOUNT_ID,
scope="primary",
metadata={"protocol": "qqpilot"},
)
self._loop = asyncio.get_running_loop()
self._replies: List[str] = []
self._reply_lock = threading.Lock()
self._last_reply_time = 0
self.server_thread = threading.Thread(target=self.run_server, args=(7749,), daemon=True)
self.server_thread.start()
self.ctx.logger.info("就绪!")
self.ctx.logger.info(f"{Fore.LIGHTYELLOW_EX}在设置的服务器那一栏填写http://localhost:7749/v1")
async def on_unload(self) -> None:
# 上报网关离线
await self.ctx.gateway.update_state(
gateway_name="qqpilot_gateway2",
ready=False,
)
self.httpd.shutdown()
self.server_thread.join()
self.ctx.logger.info(f"{Fore.CYAN}离线。")
async def __del__(self):
await self.on_unload()
def run_server(self,port=7749):
"""在子线程中启动服务器"""
self.httpd:socketserver.TCPServer = socketserver.TCPServer(("", port), Handler)
# self.ctx.logger.info(f"Server started at http://localhost:{port}/")
self.httpd.serve_forever()
async def on_config_update(self, scope: str, config_data: dict, version: str) -> None:
if "image" in config_data:
if "ImagePath" in config_data["image"]:
self._image_path = config_data["image"]["ImagePath"]
self.ctx.logger.info(f'图片保存路径已更新: {self._image_path}')
if "deleteImageInTheNextRequest" in config_data["image"]:
self._delete_image_in_next_request = config_data["image"]["deleteImageInTheNextRequest"]
self.ctx.logger.info(f'下一次请求删除图片: {self._delete_image_in_next_request}')
def Print(self,content):
self.ctx.logger.info(content)
@MessageGateway(
route_type="duplex",
name=GATEWAY_NAME,
platform=PLATFORM,
protocol="qqpilot",
account_id=ACCOUNT_ID,
scope="primary",
)
async def send_to_platform(
self,
message: dict[str, Any],
route: dict[str, Any] | None = None,
metadata: dict[str, Any] | None = None,
**kwargs: Any,
) -> dict[str, Any]:
"""出站:将 Host 消息转发到外部平台。"""
# 调试日志:查看完整消息结构
raw_message = message.get("raw_message", [])
print(f"\n📤 send_to_platform 被调用")
print(f"📤 raw_message 段数: {len(raw_message)}")
for i, segment in enumerate(raw_message):
seg_type = segment.get("type", "")
if seg_type == "text":
data = segment.get("data", "")
text_content = ""
if isinstance(data, str):
text_content = data
elif isinstance(data, dict):
text_content = data.get("text", "")
print(f"📤 段 {i}: type={seg_type}, 长度={len(text_content)}, 内容前50字='{text_content[:50]}...'")
else:
print(f"📤 段 {i}: type={seg_type}")
reply_text = ""
for segment in raw_message:
segment_type = segment.get("type", "")
if segment_type == "text":
data = segment.get("data", {})
if isinstance(data, str):
reply_text += data
elif isinstance(data, dict):
reply_text += data.get("text", "")
elif segment_type in ("emoji", "image"):
base64_data = segment.get("binary_data_base64", "")
if base64_data:
self._save_image(base64_data)
print(f"📤 合并后回复文本长度: {len(reply_text)}")
if reply_text:
with self._reply_lock:
self._replies.append(reply_text)
self._last_reply_time = time.time()
return {"success": True, "external_message_id": 1}
def _save_image(self, base64_data: str) -> None:
"""将 base64 图片数据保存到配置指定的路径。"""
image_path = self._image_path
if not os.path.isabs(image_path):
images_dir = os.path.join(os.path.dirname(__file__), image_path)
else:
images_dir = image_path
os.makedirs(images_dir, exist_ok=True)
try:
image_data = base64.b64decode(base64_data)
timestamp = int(time.time() * 1000)
filename = f"{timestamp}_{uuid4().hex[:8]}.png"
filepath = os.path.join(images_dir, filename)
with open(filepath, "wb") as f:
f.write(image_data)
self._last_saved_images.append(filepath)
self.ctx.logger.info(f"图片已保存: {filepath}")
except Exception as e:
self.ctx.logger.error(f"保存图片失败: {e}")
async def handle_inbound(self, payload: dict[str, Any]) -> None:
"""入站:将外部平台消息注入 Host。"""
msg_id = payload["message_id"]
content = payload["message"]
# 解析时间戳
timestamp_str = payload.get("timestamp", "")
if timestamp_str:
try:
timestamp_str = timestamp_str.strip()
if "月" in timestamp_str and "日" in timestamp_str:
dt = datetime.strptime(timestamp_str, "%m月-%d日 %H:%M:%S")
current_year = datetime.now().year
dt = dt.replace(year=current_year)
timestamp_seconds = dt.timestamp()
elif "+08:00" in timestamp_str:
dt = datetime.fromisoformat(timestamp_str.replace("+08:00", "+0800"))
timestamp_seconds = dt.timestamp()
else:
dt = datetime.fromisoformat(timestamp_str)
timestamp_seconds = dt.timestamp()
except:
timestamp_seconds = time.time()
else:
timestamp_seconds = time.time()
# 构建 raw_message 列表,包含文本和多媒体内容
raw_message = [{"type": "text", "data": {"text": content}}]
# 处理普通图片(非表情包)
image_urls = payload.get("images", [])
for image_url in image_urls:
if image_url.startswith("data:image"):
try:
base64_data = image_url.split(",", 1)[1]
binary_data = base64_data.encode("utf-8")
image_hash = hashlib.sha256(binary_data).hexdigest()
raw_message.append({
"type": "image",
"data": "",
"hash": image_hash,
"binary_data_base64": base64_data,
})
except:
pass
# 处理表情包图片
emoji_urls = payload.get("emojis", [])
for emoji_url in emoji_urls:
if emoji_url.startswith("data:image"):
try:
base64_data = emoji_url.split(",", 1)[1]
binary_data = base64_data.encode("utf-8")
emoji_hash = hashlib.sha256(binary_data).hexdigest()
raw_message.append({
"type": "emoji",
"data": "",
"hash": emoji_hash,
"binary_data_base64": base64_data,
})
except:
pass
# 判断消息类型标志
is_emoji = len(emoji_urls) > 0
is_picture = len(image_urls) > 0
# 构建消息信息,根据是否为私聊决定是否包含群信息
message_info = {
"platform": PLATFORM,
"message_id": msg_id,
"time": int(timestamp_seconds),
"user_info": {
"user_id": payload["user_id"],
"user_nickname": payload["nickname"],
},
"additional_config": {},
}
group_id = payload.get("groupID", "")
group_name = payload.get("groupName", "")
if group_id and group_name:
message_info["group_info"] = {
"group_id": group_id,
"group_name": group_name,
}
accepted = await self.ctx.gateway.route_message(
gateway_name=GATEWAY_NAME,
message={
"message_id": msg_id,
"timestamp": str(float(timestamp_seconds)),
"platform": PLATFORM,
"message_info": message_info,
"raw_message": raw_message,
"processed_plain_text": content,
"display_message": content,
"is_mentioned": False,
"is_at": False,
"is_emoji": is_emoji,
"is_picture": is_picture,
"is_command": content.startswith("/"),
"is_notify": False,
"session_id": "",
},
route_metadata={
"self_id": ACCOUNT_ID,
"connection_id": "primary",
},
external_message_id=msg_id,
dedupe_key=msg_id,
)
if not accepted:
self.ctx.logger.warning(
"Host 未接收入站消息: %s", msg_id
)
gateway=Gateway()
def create_plugin():
global gateway
return gateway