forked from muyouzhi6/astrbot_plugin_context_aware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
3494 lines (3021 loc) · 141 KB
/
Copy pathmain.py
File metadata and controls
3494 lines (3021 loc) · 141 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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
AstrBot 上下文场景记忆增强插件 v3.4.1 (Context Scene Memory)
为 LLM 提供结构化的群聊场景描述,增强其对对话情境的理解能力。
重点解决:主动回复时 Bot 误以为别人在问自己的问题。
核心功能:
- 触发类型检测: 被@、被回复、唤醒词、主动搭话、戳一戳
- 对话对象推断: 谁在和谁说话(关键功能)
- 对话流分析: 最近的对话结构
- Bot 状态追踪: 上次发言时间和内容
- 图像转述: 将群友发送的图片转为文字描述(可选)
设计原则:
- 只做加法,不修改框架原有信息
- 可完全替代框架内置 LTM 的群聊记录功能
- 轻量高效,图像转述为可选功能
v3.4.1 分叉版更新:
- [FIX] 只有 AstrBot Image 组件才会进入最近图片上下文;纯文本中的 [图片] 不再被误认成真实图片
- [SAFE] 对纯文本图片占位增加明确标记,禁止模型据此描述、分析、搜索或声称看到了图片
v3.4.0 分叉版更新:
- [NEW] 新增默认关闭的 reply_direction_hint,为可靠的引用回复临时注入当前发言人、引用来源和 Bot 原始回复对象说明
- [SAFE] QQ 官方 Bot、缺失引用发送者 ID 或引用 Bot 回复无法唯一定位时均不做不安全归因
- [CLEAN] 仅在当前 ProviderRequest 副本中清理旧内部场景标记,并剥离模型误回显的内部标记
v3.3.1 分叉版更新:
- [FIX] 统一发送者的 speaker 身份键,并为 Bot 回复对象、@ 对象和推断对话对象补充稳定身份标签
- [FIX] 同名用户场景下,模型可区分 Bot 的上一句具体回复给谁,避免把该回复或其后续上下文串给另一位同名用户
- [API] get_recent_messages() 增加 speaker_id、talking_to_id、talking_to_speaker
v3.3.0 分叉版更新:
- [FIX] 为每位群成员注入稳定平台 ID(QQ 平台即 QQ 号),避免同名或改名成员的历史发言串人
- [NEW] 新增可配置的用户发言归因保护,覆盖当前消息、对话流、图片、语音和历史摘要
- [CONFIG] 新增 speaker_identity_mode、speaker_attribution_guard、speaker_attribution_template
v3.2.4 分叉版更新:
- [FIX] 在记录消息前识别 `/reset` 和 `/new`,避免清空命令本身进入插件上下文
- [FIX] 兼容 astrbot_plugin_cmdmask 的命令别名,按解析后的真实命令清理上下文
v3.2.3 分叉版更新:
- [FIX] 兼容平台传入的 base64 data URI 图片:转述时改用临时本地文件路径,
避免部分 Provider 将超长 data URI 当作文件名而报错
- [NEW] 新增 strict_mode:可在主动/未知触发时禁止低置信度的“正在和 Bot 说话”推断
- [FIX] 图像转述失败会缓存空结果,避免对同一图片持续重试
- [FIX] 修正规则4的插话保护时间基准,按当前消息判断近期对话
v3.2.2 分叉版更新:
- [NEW] 适配 astrbot_plugin_dynamic_card_plus,自动读取基础名字、当前群名片和近期名片作为 Bot 文本别名
- [FIX] 回复对象昵称命中动态名片时,按“回复 Bot”处理,减少把动态名片误判成另一个人的情况
- [CONFIG] 新增 dynamic_card_plus_compat、dynamic_card_plus_identity_hint、dynamic_card_plus_identity_template 等配置项
v3.2.1 分叉版更新:
- [SYNC] 同步上游 v3.1.6:语音转写独立上下文窗口、图片上下文、GIF 过滤、内置 LTM 警告和消息去重
- [KEEP] 保留分叉版插件标识、注入标记、动态群名片身份提示、结构化消息记录和临时场景注入
v3.1.6 上游更新:
- [FIX] 语音转写独立上下文窗口,避免高频群聊把未回复语音挤出最近对话流
v3.1.5 更新:
- [FIX] 兼容 Gemini_STT 语音转写上下文,记录为普通群聊消息
- [FIX] 按消息 ID 幂等写入,避免 LLM 请求兜底记录和消息 handler 重复记录同一条语音
v3.1.4 更新:
- [FIX] 最近图片上下文支持过滤 GIF,避免不支持 image/gif 的模型在后续请求中报错
- [CONFIG] 新增 show_recent_images_allow_gif 开关,默认不将 GIF 加入 <recent_images>
v3.1.3 更新:
- [FIX] 图片上下文记录只扩展到图片概要,避免纯表情/@/引用等占位消息污染对话流
- [CHANGE] 最低 AstrBot 版本提高到 4.24.0,确保临时注入不会写入会话历史
v3.1.2 更新:
- [FIX] 唤醒词判定改为显式匹配 wake_prefix,避免把异常触发误判成 wake_word
- [FIX] 多个 @ 对象时保留完整对话目标,避免只显示第一个人
v3.1.1 更新:
- [FIX] 修复 SessionState 字段重复定义问题
- [FIX] 修复超时配置代码(300s)与schema(600s)不一致
- [FIX] 修复 Bot 消息 ID 使用时间戳可能冲突,改用 uuid
v3.0.0 更新 (重大重构):
- [CRITICAL] 修复并发竞态: SessionManager 添加异步锁 + deque 替代 list
- [HIGH] 图像转述优化: 并发限流(Semaphore) + 超时控制 + URL缓存
- [HIGH] 修复封装破坏: SceneAnalyzer 添加 bot_id 只读属性
- [HIGH] 消除魔法字符串: 集中定义 ExtraKeys 常量类
- [HIGH] 安全注入场景: 防止重复注入 + 兼容处理
- [HIGH] 对话推断增强: 关键锚点分离 + 推断原因追踪
- [MEDIUM] 配置工具方法: _cfg_int/_cfg_bool/_cfg_list
- [MEDIUM] 回复特征词可配置化
- [MEDIUM] 增强可观测性: 推断规则日志
- [LOW] 修复时间戳精度: 使用 uuid
v2.5.1 更新:
- 新增戳一戳触发类型(TRIGGER_POKE)
- 支持 poke_to_llm 插件的 _poke_trigger 标记
- 戳一戳时正确显示戳一戳用户信息
Author: Huli3(fork 自 木有知)
Version: 3.4.1
"""
from __future__ import annotations
import asyncio
import base64
import hashlib
import os
import re
import tempfile
import time
import uuid
from collections import OrderedDict, deque
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Final, final
from astrbot import logger
from astrbot.api import star
from astrbot.api.event import AstrMessageEvent, filter
from astrbot.api.message_components import At, AtAll, Image, Plain, Reply
from astrbot.api.provider import LLMResponse, Provider, ProviderRequest
from astrbot.core.agent.message import TextPart
if TYPE_CHECKING:
from astrbot.core.config import AstrBotConfig
# ============================================================================
# Extra Keys - 消除魔法字符串
# ============================================================================
@final
class ExtraKeys:
"""框架 extra 字段键名常量,集中管理避免魔法字符串"""
POKE_TRIGGER: Final[str] = "_poke_trigger"
POKE_SENDER_ID: Final[str] = "_poke_sender_id"
POKE_SENDER_NAME: Final[str] = "_poke_sender_name"
ACTIVE_TRIGGER: Final[str] = "_active_trigger"
ACTIVE_REPLY_TRIGGERED: Final[str] = "active_reply_triggered"
CURRENT_MESSAGE_RECORD: Final[str] = "_scene_memory_current_message_record"
GEMINI_STT_TRANSCRIPT: Final[str] = "_gemini_stt_transcript"
GEMINI_STT_RAW_TEXT: Final[str] = "_gemini_stt_raw_text"
GEMINI_STT_CACHE_ONLY: Final[str] = "_gemini_stt_cache_only"
GEMINI_STT_SHOULD_REPLY: Final[str] = "_gemini_stt_should_reply"
GEMINI_STT_REPLY_REASON: Final[str] = "_gemini_stt_reply_reason"
# AstrBot 会话清理标记:同时兼容新旧版本和第三方命令钩子
SESSION_CLEAN_GROUP: Final[str] = "_clean_group_context_session"
SESSION_CLEAN_LEGACY: Final[str] = "_clean_ltm_session"
# astrbot_plugin_cmdmask 用这些 extra 保存别名解析后的真实命令
CMDMASK_APPLIED: Final[str] = "__astrbot_plugin_cmdmask:applied"
CMDMASK_TARGET: Final[str] = "__astrbot_plugin_cmdmask:target"
# 场景注入标记,防止重复注入
SCENE_INJECTED_MARKER: Final[str] = "<!-- scene_memory_fork_v321 -->"
# ============================================================================
# Constants
# ============================================================================
# 触发类型常量
TRIGGER_PRIVATE: Final = "private_chat"
TRIGGER_AT: Final = "at_bot"
TRIGGER_AT_ALL: Final = "at_all"
TRIGGER_REPLY: Final = "reply_to_bot"
TRIGGER_WAKE: Final = "wake_word"
TRIGGER_MENTION: Final = "mention"
TRIGGER_ACTIVE: Final = "active"
TRIGGER_POKE: Final = "poke"
TRIGGER_UNKNOWN: Final = "unknown"
DYNAMIC_CARD_PLUS_PLUGIN_ID: Final[str] = "astrbot_plugin_dynamic_card_plus"
# 触发类型中文名(用于日志)
TRIGGER_NAMES: Final = {
TRIGGER_PRIVATE: "私聊",
TRIGGER_AT: "@Bot",
TRIGGER_AT_ALL: "@全体",
TRIGGER_REPLY: "回复Bot",
TRIGGER_WAKE: "唤醒词",
TRIGGER_MENTION: "提及Bot",
TRIGGER_ACTIVE: "主动触发",
TRIGGER_POKE: "戳一戳",
TRIGGER_UNKNOWN: "未知",
}
# 回复特征词(用于判断是否在回复 Bot)- 可通过配置覆盖
DEFAULT_REPLY_STARTERS: Final = frozenset({
"好的", "好", "嗯", "是的", "对", "谢谢", "感谢", "收到",
"明白", "知道了", "了解", "可以", "行", "没问题",
"ok", "OK", "Ok", "好滴", "好哒", "好嘞", "okok",
})
# 单张 data URI 图片的解码上限,避免异常消息占用过多内存和临时磁盘空间。
IMAGE_CAPTION_DATA_URI_MAX_BYTES: Final = 50 * 1024 * 1024
_DATA_URI_IMAGE_SUFFIXES: Final[dict[str, str]] = {
"image/jpeg": ".jpg",
"image/jpg": ".jpg",
"image/png": ".png",
"image/gif": ".gif",
"image/webp": ".webp",
"image/bmp": ".bmp",
"image/x-icon": ".ico",
"image/vnd.microsoft.icon": ".ico",
}
# ============================================================================
# Inference Reasons - 推断原因追踪
# ============================================================================
@final
class InferenceReason:
"""对话对象推断原因常量"""
RULE_1_AT_BOT: Final[str] = "rule_1_at_bot" # 明确 @Bot
RULE_1B_TEXT_MENTION_BOT: Final[str] = "rule_1b_text_mention_bot" # 文本点名 Bot
RULE_2_AT_OTHER: Final[str] = "rule_2_at_other" # @其他人
RULE_3_REPLY: Final[str] = "rule_3_reply" # 引用回复
RULE_4_BOT_REPLIED: Final[str] = "rule_4_bot_replied" # Bot 刚回复过此人
RULE_4B_BOT_INTERRUPTED: Final[str] = "rule_4b_bot_interrupted" # Bot 插话导致误判,回退给上一位对话者
RULE_5_ABA_PATTERN: Final[str] = "rule_5_aba_pattern" # A-B-A 对话模式
RULE_6_QUICK_FOLLOW: Final[str] = "rule_6_quick_follow" # 快速连续对话
DEFAULT_GROUP: Final[str] = "default_group" # 默认群聊
# LLM 看到的用户身份标签模式。平台 ID 在 QQ/OneBot 场景中即为 QQ 号。
SPEAKER_IDENTITY_PLATFORM_ID: Final[str] = "platform_id"
SPEAKER_IDENTITY_MASKED: Final[str] = "masked"
SPEAKER_IDENTITY_NAME_ONLY: Final[str] = "name_only"
_VALID_SPEAKER_IDENTITY_MODES: Final[frozenset[str]] = frozenset({
SPEAKER_IDENTITY_PLATFORM_ID,
SPEAKER_IDENTITY_MASKED,
SPEAKER_IDENTITY_NAME_ONLY,
})
DEFAULT_SPEAKER_ATTRIBUTION_TEMPLATE: Final[str] = (
"本轮当前用户的唯一身份标签是 {current_speaker}。只有当前消息,以及 speaker 身份标签"
"完全等于 {current_speaker} 的历史内容,才能归属给这位用户。历史摘要、历史对话、"
"图片和语音内容只属于其中各自标明的身份标签;除非身份标签完全一致,严禁把其他用户"
"说过的话、做过的事、偏好或观点归因给当前用户。没有身份标签的历史仅可作为背景,"
"不可作为当前用户曾经说过或做过某事的证据。"
)
REPLY_DIRECTION_INJECTED_MARKER: Final[str] = (
"<!-- scene_memory_reply_direction_v340 -->"
)
DEFAULT_REPLY_DIRECTION_HINT_TEMPLATE: Final[str] = (
"这是引用回复的临时指向说明:当前发言人是 {current_speaker};"
"被引用消息的发送者是 {quoted_speaker}。{quoted_bot_reply_target_note}"
"仅按这些身份标签解释引用关系;除非身份标签一致,否则不要把被引用消息、"
"Bot 回复对象或其历史经历归属给当前发言人。"
)
_QQ_OFFICIAL_PLATFORM_NAMES: Final[frozenset[str]] = frozenset({
"qq_official",
"qqofficial",
})
_INTERNAL_SCENE_BLOCK_RE: Final[re.Pattern[str]] = re.compile(
r"<!--\s*scene_memory_fork_v[\w.-]+\s*-->\s*"
r"<conversation_scene(?:\s[^>]*)?>.*?</conversation_scene\s*>",
re.IGNORECASE | re.DOTALL,
)
_INTERNAL_REPLY_DIRECTION_BLOCK_RE: Final[re.Pattern[str]] = re.compile(
r"<!--\s*scene_memory_reply_direction_v[\w.-]+\s*-->\s*"
r"<reply_direction(?:\s[^>]*)?>.*?</reply_direction\s*>",
re.IGNORECASE | re.DOTALL,
)
_INTERNAL_SCENE_MARKER_RE: Final[re.Pattern[str]] = re.compile(
r"<!--\s*scene_memory_(?:fork|reply_direction)_v[\w.-]+\s*-->",
re.IGNORECASE,
)
# ============================================================================
# Data Structures
# ============================================================================
@dataclass(slots=True)
class MessageRecord:
"""轻量级消息记录"""
msg_id: str
sender_id: str
sender_name: str
content: str
timestamp: float # Unix timestamp
is_bot: bool = False
at_bot: bool = False
at_all: bool = False
reply_to_id: str | None = None
reply_to_name: str = ""
reply_to_message_id: str = ""
reply_to_content: str = ""
reply_to_timestamp: float = 0.0
talking_to: str = "group"
talking_to_name: str = "群聊"
at_targets: list[tuple[str, str]] = field(default_factory=list)
message_outline: str = ""
has_image: bool = False
image_count: int = 0
has_gif: bool = False
gif_count: int = 0
def _is_reliable_reply_sender_id(value: Any) -> bool:
"""引用组件的发送者 ID 只有非空、非占位值时才可用于身份归因。"""
normalized = _clean_one_line(value).strip().casefold()
return bool(normalized and normalized not in {"0", "none", "null", "unknown", "undefined"})
def _apply_reply_reference(msg: MessageRecord, comp: Reply) -> None:
"""从 Reply 组件保存可用于本轮引用指向分析的稳定元数据。"""
reply_sender_id = _clean_one_line(getattr(comp, "sender_id", "")).strip()
if _is_reliable_reply_sender_id(reply_sender_id):
msg.reply_to_id = reply_sender_id
reply_name = _clean_one_line(getattr(comp, "sender_nickname", "")).strip()
if reply_name:
msg.reply_to_name = reply_name
reply_message_id = _clean_one_line(getattr(comp, "id", "")).strip()
if reply_message_id and reply_message_id != "0":
msg.reply_to_message_id = reply_message_id
reply_content = _clean_one_line(getattr(comp, "message_str", "")).strip()
if reply_content:
msg.reply_to_content = reply_content[:500]
try:
reply_timestamp = float(getattr(comp, "time", 0) or 0)
except (TypeError, ValueError):
reply_timestamp = 0.0
if reply_timestamp > 0:
msg.reply_to_timestamp = reply_timestamp
def _normalize_at_target(
bot_id: str,
target_id: str,
target_name: str | None,
) -> tuple[str, str]:
"""统一 @ 目标的表示,Bot 使用稳定标识避免后续判断分裂。"""
normalized_id = str(target_id or "").strip()
if normalized_id == bot_id:
return "bot", "你"
normalized_name = str(target_name or normalized_id).strip() or normalized_id
return normalized_id, normalized_name
def _unique_targets(targets: list[tuple[str, str]]) -> list[tuple[str, str]]:
"""按 target_id 去重,保留首次出现顺序。"""
seen: set[str] = set()
normalized: list[tuple[str, str]] = []
for target_id, target_name in targets:
normalized_id = str(target_id or "").strip()
if not normalized_id or normalized_id in seen:
continue
seen.add(normalized_id)
normalized_name = str(target_name or normalized_id).strip() or normalized_id
normalized.append((normalized_id, normalized_name))
return normalized
def _format_name_list(names: list[str]) -> str:
"""把多个名字拼成更自然的中文列举。"""
if not names:
return ""
if len(names) == 1:
return names[0]
if len(names) == 2:
return f"{names[0]}和{names[1]}"
return f"{'、'.join(names[:-1])}和{names[-1]}"
def _clean_one_line(value: Any) -> str:
"""压缩消息概要为单行文本,避免注入上下文时破坏结构。"""
text = "" if value is None else str(value)
return " ".join(text.replace("\r", " ").replace("\n", " ").split())
def _normalize_speaker_identity_mode(value: Any) -> str:
"""归一化身份标签模式,非法配置回退到原始平台 ID。"""
mode = _clean_one_line(value).casefold()
if mode in _VALID_SPEAKER_IDENTITY_MODES:
return mode
return SPEAKER_IDENTITY_PLATFORM_ID
def _identity_key_from_values(sender_id: Any, sender_name: Any, mode: str) -> str:
"""按配置从平台 ID 和昵称生成可直接比较的身份键。"""
mode = _normalize_speaker_identity_mode(mode)
normalized_id = _clean_one_line(sender_id).strip()
normalized_name = _clean_one_line(sender_name).strip() or "未知用户"
if mode == SPEAKER_IDENTITY_NAME_ONLY:
return f"name:{normalized_name}"
if not normalized_id:
return f"name:{normalized_name}"
if mode == SPEAKER_IDENTITY_MASKED:
digest = hashlib.sha256(normalized_id.encode("utf-8")).hexdigest()[:12]
return f"user:{digest}"
return f"user:{normalized_id}"
def _speaker_identity_key(msg: MessageRecord, mode: str) -> str:
"""生成供 LLM 归因的稳定身份键,不依赖容易变化的群昵称。"""
if msg.is_bot:
return "bot:self"
return _identity_key_from_values(msg.sender_id, msg.sender_name, mode)
def _speaker_identity_label(msg: MessageRecord, mode: str) -> str:
"""把昵称与稳定身份键一起呈现,兼顾可读性和精确归因。"""
if msg.is_bot:
return "你 [bot:self]"
sender_name = _clean_one_line(msg.sender_name).strip() or "未知用户"
return f"{sender_name} [{_speaker_identity_key(msg, mode)}]"
def _unique_speaker_labels(
messages: list[MessageRecord],
mode: str,
) -> list[str]:
"""按稳定身份键去重并保留出现顺序,避免同名成员被合并。"""
labels: list[str] = []
seen: set[str] = set()
for msg in messages:
if msg.is_bot:
continue
key = _speaker_identity_key(msg, mode)
if key in seen:
continue
seen.add(key)
labels.append(_speaker_identity_label(msg, mode))
return labels
def _format_speaker_attribution(template: Any, current_speaker: str) -> str:
"""渲染当前请求的归因规则,保留一个安全且可配置的占位符。"""
text = _clean_one_line(template).strip() or DEFAULT_SPEAKER_ATTRIBUTION_TEMPLATE
return text.replace("{current_speaker}", current_speaker)
def _append_unique_text(items: list[str], value: Any, *, key_seen: set[str] | None = None) -> None:
text = _clean_one_line(value).strip()
if not text:
return
key = text.casefold()
seen = key_seen if key_seen is not None else {item.casefold() for item in items}
if key in seen:
return
items.append(text)
seen.add(key)
def _is_useful_dynamic_card_alias(value: str, *, min_length: int = 2) -> bool:
text = _clean_one_line(value).strip(" \t\r\n-_/||·•,,::[]【】()()")
if len(text) < min_length or len(text) > 80:
return False
if not re.search(r"[\w\u4e00-\u9fff]", text, re.UNICODE):
return False
return text.casefold() not in {
"cpu",
"mem",
"memory",
"ram",
"time",
"status",
"sen",
"sen值",
"内存",
"时间",
"状态",
}
def _dynamic_card_alias_candidates(value: Any, *, min_length: int = 2) -> list[str]:
"""Extract stable bot aliases from a dynamic group card."""
text = _clean_one_line(value).strip()
if not text:
return []
aliases: list[str] = []
seen: set[str] = set()
def add(alias: Any) -> None:
alias_text = _clean_one_line(alias).strip(" \t\r\n-_/||·•,,::[]【】()()")
if _is_useful_dynamic_card_alias(alias_text, min_length=min_length):
_append_unique_text(aliases, alias_text, key_seen=seen)
add(text)
metric_pattern = re.compile(
r"(?i)(cpu|mem|memory|ram|sen\s*值|sen值|内存|时间|状态|负载)\s*[::]?\s*[\d.%%::-]*"
)
metric_match = metric_pattern.search(text)
if metric_match and metric_match.start() > 0:
add(text[: metric_match.start()])
stripped = metric_pattern.sub(" ", text)
stripped = re.sub(r"\b\d{1,2}:\d{2}(?::\d{2})?\b", " ", stripped)
stripped = re.sub(r"\b\d{4}[-/]\d{1,2}[-/]\d{1,2}\b", " ", stripped)
stripped = re.sub(r"\s+", " ", stripped).strip()
if stripped != text:
add(stripped)
first_token = re.split(r"[\s||/·•]+", text, maxsplit=1)[0]
if first_token != text:
add(first_token)
return aliases
def _event_message_outline(event: AstrMessageEvent) -> str:
"""优先使用 AstrBot 消息概要,以保留图片/语音等非文本消息占位。"""
transcript = _event_voice_transcript(event)
if transcript:
return transcript
outline = ""
try:
outline = event.get_message_outline()
except Exception:
outline = ""
if not outline:
try:
outline = event.get_message_str()
except Exception:
outline = ""
if not outline:
outline = str(getattr(event.message_obj, "message_str", "") or event.message_str or "")
return _clean_one_line(outline)
def _event_voice_transcript(event: AstrMessageEvent) -> str:
"""读取 Gemini_STT 导出的语音转写,作为群聊上下文普通消息记录。"""
getter = getattr(event, "get_extra", None)
if not callable(getter):
return ""
try:
transcript = getter(ExtraKeys.GEMINI_STT_TRANSCRIPT, "") or getter(
ExtraKeys.GEMINI_STT_RAW_TEXT, ""
)
except Exception:
return ""
transcript = _clean_one_line(transcript)
if not transcript:
return ""
return f"[语音转写] {transcript}"
def _looks_like_voice_transcript(text: str) -> bool:
return _clean_one_line(text).startswith("[语音转写]")
def _looks_like_image_outline(text: str) -> bool:
"""识别平台概要中的图片占位,仅用于判断无组件事件是否值得记录。"""
lowered = text.lower()
return any(token in lowered for token in ("[图片", "图片", "照片", "[image", "image", "photo"))
_IMAGE_PLACEHOLDER_TEXT_RE: Final[re.Pattern[str]] = re.compile(
r"\[\s*(?:图片|image)(?:\s*[::][^\]]*)?\]",
re.IGNORECASE,
)
def _contains_image_placeholder_text(value: Any) -> bool:
"""识别消息正文里的字面图片占位,不能将其当作媒体组件。"""
return bool(_IMAGE_PLACEHOLDER_TEXT_RE.search(_clean_one_line(value)))
_GIF_BASE64_PREFIXES: Final[tuple[str, str]] = ("R0lGODlh", "R0lGODdh")
def _image_ref_looks_like_gif(image_ref: str) -> bool:
"""尽量在投递给视觉模型前识别 GIF,避免不支持 image/gif 的模型报错。"""
ref = (image_ref or "").strip()
if not ref:
return False
lowered = ref.lower()
if "image/gif" in lowered:
return True
ref_without_query = lowered.split("?", 1)[0].split("#", 1)[0]
if ref_without_query.endswith(".gif"):
return True
local_path = ref
if lowered.startswith("file:///"):
local_path = ref[8:]
elif lowered.startswith("file://"):
local_path = ref[7:]
if "://" not in local_path and not lowered.startswith(("data:", "base64:")):
try:
with open(local_path, "rb") as f:
return f.read(6) in (b"GIF87a", b"GIF89a")
except OSError:
pass
payload = ref
lowered_payload = payload.lower()
if lowered_payload.startswith("base64://"):
payload = payload[len("base64://"):]
elif lowered_payload.startswith("base64:"):
payload = payload[len("base64:"):]
if payload.lower().startswith("data:") and "," in payload:
payload = payload.split(",", 1)[1]
payload = payload.lstrip()
return payload.startswith(_GIF_BASE64_PREFIXES)
def _explicit_addressees(
msg: MessageRecord,
*,
bot_label: str = "你",
) -> list[tuple[str, str]]:
"""提取消息里显式出现的 @ 目标。"""
targets = _unique_targets(msg.at_targets)
if not targets:
return []
return [
(target_id, bot_label if target_id == "bot" else target_name)
for target_id, target_name in targets
]
def _other_explicit_target_names(msg: MessageRecord) -> list[str]:
"""获取除 Bot 外其他被显式点名的对象名称。"""
return [
target_name
for target_id, target_name in _explicit_addressees(msg)
if target_id != "bot"
]
def _other_explicit_target_labels(msg: MessageRecord, mode: str) -> list[str]:
"""获取除 Bot 外其他被显式点名对象的稳定身份标签。"""
return [
_addressee_identity_label(
target_id,
target_name,
mode,
bot_label="你",
group_label="群聊",
)
for target_id, target_name in _explicit_addressees(msg)
if target_id != "bot"
]
def _raw_bot_target_names(msg: MessageRecord) -> list[str]:
"""获取消息里原始出现过的 Bot 名称/群名片,用于动态改名场景提示。"""
names: list[str] = []
for target_id, target_name in _unique_targets(msg.at_targets):
if target_id != "bot":
continue
normalized_name = str(target_name or "").strip()
if normalized_name and normalized_name not in names:
names.append(normalized_name)
return names
def _describe_addressee(
msg: MessageRecord,
*,
bot_label: str = "你(Bot)",
group_label: str = "群聊",
multi_target_bot_label: str = "你",
) -> str:
"""根据显式目标和推断结果生成更贴近真实群聊的对话对象描述。"""
explicit_targets = _explicit_addressees(msg, bot_label=multi_target_bot_label)
if len(explicit_targets) > 1:
return _format_name_list([target_name for _, target_name in explicit_targets])
if msg.talking_to == "bot":
return bot_label
if msg.talking_to == "group":
return group_label
if explicit_targets:
return explicit_targets[0][1]
return msg.talking_to_name or msg.talking_to
def _addressee_identity_label(
target_id: Any,
target_name: Any,
mode: str,
*,
bot_label: str,
group_label: str,
) -> str:
"""为消息接收对象附上与发送者相同的稳定身份标签。"""
normalized_id = _clean_one_line(target_id).strip()
normalized_name = _clean_one_line(target_name).strip() or "未知用户"
if normalized_id == "bot":
return f"{bot_label} [bot:self]"
if normalized_id == "group":
return f"{group_label} [group:all]"
identity_key = _identity_key_from_values(
normalized_id,
normalized_name,
mode,
)
return f"{normalized_name} [{identity_key}]"
def _describe_addressee_with_identity(
msg: MessageRecord,
*,
mode: str,
bot_label: str = "你(Bot)",
group_label: str = "群聊",
multi_target_bot_label: str = "你",
) -> str:
"""生成可由模型精确比对的接收对象描述,保留多 @ 场景。"""
explicit_targets = _explicit_addressees(msg, bot_label=multi_target_bot_label)
if len(explicit_targets) > 1:
labels = [
_addressee_identity_label(
target_id,
target_name,
mode,
bot_label=multi_target_bot_label,
group_label=group_label,
)
for target_id, target_name in explicit_targets
]
return _format_name_list(labels)
if msg.talking_to == "bot":
return _addressee_identity_label(
"bot",
bot_label,
mode,
bot_label=bot_label,
group_label=group_label,
)
if msg.talking_to == "group":
return _addressee_identity_label(
"group",
group_label,
mode,
bot_label=bot_label,
group_label=group_label,
)
if explicit_targets:
target_id, target_name = explicit_targets[0]
return _addressee_identity_label(
target_id,
target_name,
mode,
bot_label=bot_label,
group_label=group_label,
)
return _addressee_identity_label(
msg.talking_to,
msg.talking_to_name,
mode,
bot_label=bot_label,
group_label=group_label,
)
@dataclass(slots=True)
class SessionState:
"""会话状态 - 每个群聊/私聊一个
v3.0.0: 使用 deque 替代 list,避免手动裁剪的非原子操作
v3.1.1: 修复字段重复定义问题
"""
messages: deque[MessageRecord] = field(default_factory=lambda: deque(maxlen=50))
bot_last_spoke_at: float = 0.0
bot_last_content: str = ""
bot_last_replied_to: str = "" # Bot 上次回复的对象 ID
bot_last_replied_to_name: str = "" # Bot 上次回复的对象名称
# 关键锚点分离,不随消息淘汰
last_user_interaction: dict[str, float] = field(default_factory=dict) # user_id -> timestamp
# 会话摘要(用于上下文压缩)
summary: str = ""
summary_updated_at: float = 0.0
summary_message_count: int = 0
compressing: bool = False
@dataclass
class PluginStats:
"""插件统计信息"""
messages_recorded: int = 0
scenes_injected: int = 0
bot_responses_recorded: int = 0
trigger_counts: dict[str, int] = field(default_factory=dict)
def record_trigger(self, trigger_type: str) -> None:
self.trigger_counts[trigger_type] = self.trigger_counts.get(trigger_type, 0) + 1
@dataclass(slots=True)
class SessionSnapshot:
"""会话快照(避免在锁外直接读写 SessionState 导致竞态)"""
messages: list[MessageRecord]
bot_last_spoke_at: float
bot_last_content: str
bot_last_replied_to: str
bot_last_replied_to_name: str
summary: str
summary_updated_at: float
summary_message_count: int
# ============================================================================
# Session Manager (LRU Cache)
# ============================================================================
class SessionManager:
"""会话管理器 - 带 LRU 淘汰机制和异步锁保护
v3.0.0 重构:
- 添加 asyncio.Lock 防止并发竞态
- 使用 deque 自动裁剪,避免非原子操作
- 淘汰会话时同时清理关联的锁
v3.0.1 增强:
- 添加缓存级别锁保护 LRU 的 move_to_end/popitem
- 废弃同步写方法的直接使用(保留向后兼容但加警告)
并发模型说明:
- _cache_lock: 保护 _sessions (OrderedDict) 和 _locks (dict) 的结构性修改
- 每会话锁: 保护单个会话的 messages/state 修改
- 所有写操作应使用 async 版本
"""
__slots__ = ("_sessions", "_locks", "_max_messages", "_max_sessions", "_cache_lock")
def __init__(self, max_messages: int = 50, max_sessions: int = 100) -> None:
self._sessions: OrderedDict[str, SessionState] = OrderedDict()
self._locks: dict[str, asyncio.Lock] = {}
self._cache_lock = asyncio.Lock() # 缓存级别锁,保护 LRU 操作
self._max_messages = max(10, max_messages)
self._max_sessions = max(10, max_sessions)
def _has_message_id(self, state: SessionState, msg_id: str) -> bool:
normalized = str(msg_id or "").strip()
if not normalized:
return False
return any(existing.msg_id == normalized for existing in state.messages)
def _get_lock(self, session_id: str) -> asyncio.Lock:
"""获取会话锁(惰性创建,使用 setdefault 保证原子性)"""
# setdefault 是原子操作,避免竞态条件
return self._locks.setdefault(session_id, asyncio.Lock())
async def _get_or_create_session(self, session_id: str) -> SessionState:
"""获取或创建会话状态(异步,带缓存锁保护)
这是并发安全的核心方法,保护 LRU 的 move_to_end 和 popitem。
"""
async with self._cache_lock:
if session_id in self._sessions:
self._sessions.move_to_end(session_id)
return self._sessions[session_id]
while len(self._sessions) >= self._max_sessions:
evicted_id, _ = self._sessions.popitem(last=False)
# 清理关联的锁
self._locks.pop(evicted_id, None)
# 创建新会话时设置 deque 的 maxlen
state = SessionState()
state.messages = deque(maxlen=self._max_messages)
self._sessions[session_id] = state
return state
def get(self, session_id: str) -> SessionState:
"""获取或创建会话状态(同步方法,用于读取)
警告:此方法在并发场景下可能存在竞态。
推荐在异步上下文中使用 _get_or_create_session()。
"""
if session_id in self._sessions:
self._sessions.move_to_end(session_id)
return self._sessions[session_id]
while len(self._sessions) >= self._max_sessions:
evicted_id, _ = self._sessions.popitem(last=False)
self._locks.pop(evicted_id, None)
state = SessionState()
state.messages = deque(maxlen=self._max_messages)
self._sessions[session_id] = state
return state
async def add_message_async(self, session_id: str, msg: MessageRecord) -> bool:
"""异步添加消息到会话(推荐使用,完全并发安全)"""
async with self._get_lock(session_id):
state = await self._get_or_create_session(session_id)
if self._has_message_id(state, msg.msg_id):
return False
state.messages.append(msg)
if not msg.is_bot:
state.last_user_interaction[msg.sender_id] = msg.timestamp
return True
async def get_snapshot_async(self, session_id: str) -> SessionSnapshot:
"""获取会话快照(带会话锁)"""
async with self._get_lock(session_id):
state = await self._get_or_create_session(session_id)
return SessionSnapshot(
messages=list(state.messages),
bot_last_spoke_at=state.bot_last_spoke_at,
bot_last_content=state.bot_last_content,
bot_last_replied_to=state.bot_last_replied_to,
bot_last_replied_to_name=state.bot_last_replied_to_name,
summary=state.summary,
summary_updated_at=state.summary_updated_at,
summary_message_count=state.summary_message_count,
)
async def mark_compressing_async(self, session_id: str) -> bool:
"""尝试标记会话正在压缩(避免并发重复压缩)。成功返回 True。"""
async with self._get_lock(session_id):
state = await self._get_or_create_session(session_id)
if state.compressing:
return False
state.compressing = True
return True
async def clear_compressing_async(self, session_id: str) -> None:
async with self._get_lock(session_id):
if session_id in self._sessions:
self._sessions[session_id].compressing = False
async def set_summary_and_trim_async(
self,
session_id: str,
*,
summary: str,
keep_recent: int,
summarized_count: int,
updated_at: float,
) -> None:
"""设置摘要并裁剪历史(带会话锁)"""
keep_recent = max(5, keep_recent)
async with self._get_lock(session_id):
state = await self._get_or_create_session(session_id)
msgs = list(state.messages)
recent = msgs[-keep_recent:] if msgs else []
state.messages = deque(recent, maxlen=state.messages.maxlen)
state.summary = summary
state.summary_updated_at = updated_at
state.summary_message_count = max(state.summary_message_count, summarized_count)
state.compressing = False
async def remove_session_async(self, session_id: str) -> int:
"""移除整个会话(用于 reset/new/switch 等清空场景)"""
async with self._cache_lock:
state = self._sessions.pop(session_id, None)
self._locks.pop(session_id, None)
if not state:
return 0
return len(state.messages)
def add_message(self, session_id: str, msg: MessageRecord) -> bool:
"""同步添加消息(向后兼容,但不推荐在并发场景使用)
注意:此方法不提供完整的并发保护,仅用于向后兼容。
"""
state = self.get(session_id)
if self._has_message_id(state, msg.msg_id):
return False
state.messages.append(msg)
if not msg.is_bot:
state.last_user_interaction[msg.sender_id] = msg.timestamp
return True
async def record_bot_response_async(
self,
session_id: str,
content: str,
ts: float,
replied_to_id: str = "",
replied_to_name: str = "",
) -> None:
"""异步记录 Bot 回复(推荐使用,完全并发安全)"""
async with self._get_lock(session_id):
state = await self._get_or_create_session(session_id)
state.bot_last_spoke_at = ts
state.bot_last_content = content[:100] if content else ""