@mcviseron/dsh-qq-bridge 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/bot/.env.example +32 -0
- package/bot/bridge.py +339 -0
- package/bot/check_await.py +31 -0
- package/bot/main.py +4335 -0
- package/bot/panel.py +471 -0
- package/bot/probe_group.py +24 -0
- package/bot/requirements.txt +6 -0
- package/bot/run-bot.bat +23 -0
- package/bot/runtime_config.py +185 -0
- package/bot/stop-bot.bat +11 -0
- package/bot/test_bystander.py +48 -0
- package/bot/test_commands.py +149 -0
- package/bot/test_group_alias.py +149 -0
- package/bot/test_group_routing.py +241 -0
- package/bot/test_plan_b.py +153 -0
- package/bot/test_queue_drain.py +114 -0
- package/bot/test_quote.py +240 -0
- package/bot/test_stream_strict.py +84 -0
- package/bot/test_streaming.py +141 -0
- package/bot/test_turnid.py +80 -0
- package/bot/usage.mjs +175 -0
- package/lib/client.js +1004 -372
- package/lib/index.js +1304 -1120
- package/lib/install.js +324 -0
- package/lib/server.js +17 -5
- package/package.json +5 -2
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
"""Offline check of the group routing rules (no gateway, no DSH)."""
|
|
2
|
+
import os as _os, tempfile as _tempfile
|
|
3
|
+
_os.environ.setdefault("DSH_QQBOT_STATE_DIR", _tempfile.mkdtemp(prefix="qqbot-test-"))
|
|
4
|
+
import asyncio
|
|
5
|
+
import dataclasses
|
|
6
|
+
import sys
|
|
7
|
+
import time
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from viseron_qqbotpy import GroupMessage, User
|
|
11
|
+
|
|
12
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
13
|
+
from main import DSHBot, load_config # noqa: E402
|
|
14
|
+
|
|
15
|
+
BOT_MEMBER = "BOTMEMBER0000000000000000000001"
|
|
16
|
+
GROUP = "CCE96DDF6F462E1010E5721BBA1B6CBB"
|
|
17
|
+
OWNER_MEMBER = "35AD3A2431B0901EDD915A29F1AFF7F1"
|
|
18
|
+
MEMBER = "67BC6C00000000000000000000000099"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class StubAPI:
|
|
22
|
+
def __init__(self):
|
|
23
|
+
self.posts = []
|
|
24
|
+
|
|
25
|
+
async def get_group_bot_state(self, group_openid):
|
|
26
|
+
return {"member_openid": BOT_MEMBER, "recv_msg_setting": "all", "allow_proactive_msg": True}
|
|
27
|
+
|
|
28
|
+
async def get_group_member(self, group_openid, member_openid):
|
|
29
|
+
raise RuntimeError("11253")
|
|
30
|
+
|
|
31
|
+
async def post_group_message(self, **payload):
|
|
32
|
+
self.posts.append(payload)
|
|
33
|
+
return {"id": "m1"}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class StubBridge:
|
|
37
|
+
def __init__(self):
|
|
38
|
+
self.calls = []
|
|
39
|
+
|
|
40
|
+
async def inbound(self, **kwargs):
|
|
41
|
+
self.calls.append(kwargs)
|
|
42
|
+
return {}
|
|
43
|
+
|
|
44
|
+
async def pull(self, cursor, wait_ms):
|
|
45
|
+
return {"cursor": 0, "events": []}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def make_bot(owner_bound=True):
|
|
49
|
+
config = dataclasses.replace(load_config(), debounce_s=0.02, debounce_max_s=0.05, ack_delay_s=0)
|
|
50
|
+
bot = DSHBot.__new__(DSHBot)
|
|
51
|
+
bot.config = config
|
|
52
|
+
bot.api = StubAPI()
|
|
53
|
+
bot.pool = {}
|
|
54
|
+
bot._chat_turns = {}
|
|
55
|
+
bot.member_index = {}
|
|
56
|
+
bot.pool_idx = {}
|
|
57
|
+
bot._pending_quotes = {}
|
|
58
|
+
bot.bridge = StubBridge()
|
|
59
|
+
bot.state = type("S", (), {
|
|
60
|
+
"owner_openid": "OWNER",
|
|
61
|
+
"group_owners": {GROUP: OWNER_MEMBER} if owner_bound else {},
|
|
62
|
+
"save": lambda self: None,
|
|
63
|
+
})()
|
|
64
|
+
bot.owner_openid = "OWNER"
|
|
65
|
+
bot.targets = {}
|
|
66
|
+
bot.pending_approvals = {}
|
|
67
|
+
bot.pending = {}
|
|
68
|
+
bot.queued = {}
|
|
69
|
+
bot.group_state = {}
|
|
70
|
+
bot.chime = {}
|
|
71
|
+
bot._seen_ids = {}
|
|
72
|
+
bot._submitted_at = {}
|
|
73
|
+
bot._inflight = {}
|
|
74
|
+
bot._addressed_at = {}
|
|
75
|
+
bot._bot_spoke_at = {}
|
|
76
|
+
bot._last_sent = {}
|
|
77
|
+
bot._last_bot_say = {}
|
|
78
|
+
bot.chatter = {}
|
|
79
|
+
bot.recent_images = {}
|
|
80
|
+
bot._inflight_at = {}
|
|
81
|
+
bot._rate = {}
|
|
82
|
+
# 用例验证的是判断触发与提示词本身:固定 4 条一批,并关掉省钱预筛
|
|
83
|
+
bot.rt = type("RT", (), {
|
|
84
|
+
"get_bool": lambda self, key, default=True: (
|
|
85
|
+
False if key in ("interject_hook_only", "group_at_only") else default
|
|
86
|
+
),
|
|
87
|
+
"get_int": lambda self, key, default=0: 4 if key == "chatter_batch" else default,
|
|
88
|
+
"get_float": lambda self, key, default=0.0: {
|
|
89
|
+
"bot_say_memory_s": 300.0,
|
|
90
|
+
"chatter_max_s": 0.05,
|
|
91
|
+
"chatter_cooldown_s": 0.0,
|
|
92
|
+
"stuck_turn_s": 180.0,
|
|
93
|
+
}.get(key, default),
|
|
94
|
+
"get_str": lambda self, key, default="": default,
|
|
95
|
+
"get": lambda self, key, default=None: default,
|
|
96
|
+
})()
|
|
97
|
+
bot._role_probe_failed_at = 0.0
|
|
98
|
+
bot._msg_seq = 0
|
|
99
|
+
bot._acks = {}
|
|
100
|
+
bot._pull_task = None
|
|
101
|
+
bot._in_quiet_hours = lambda: False
|
|
102
|
+
return bot
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def msg(text, member=MEMBER, mentions=None, attachments=None):
|
|
106
|
+
return GroupMessage(
|
|
107
|
+
id=f"id-{time.time_ns()}",
|
|
108
|
+
group_openid=GROUP,
|
|
109
|
+
content=text,
|
|
110
|
+
author=User(member_openid=member, user_openid="U1"),
|
|
111
|
+
mentions=mentions or [],
|
|
112
|
+
attachments=attachments or [],
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def prompts(bot):
|
|
117
|
+
return [call for call in bot.bridge.calls if not call.get("context_only")]
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
async def case(label, bot, message, expect_prompt):
|
|
121
|
+
await bot.on_group_message_create(message)
|
|
122
|
+
await asyncio.sleep(0.25)
|
|
123
|
+
got = bool(prompts(bot))
|
|
124
|
+
ok = got == expect_prompt
|
|
125
|
+
print(("ok " if ok else "FAIL ") + label + f" (prompt={got}, expected={expect_prompt})")
|
|
126
|
+
return 0 if ok else 1
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
async def main():
|
|
130
|
+
failures = 0
|
|
131
|
+
|
|
132
|
+
# @机器人 才算点名;@别人 不能回
|
|
133
|
+
bot = make_bot()
|
|
134
|
+
self_mention = msg("你在吗", mentions=[User(member_openid=BOT_MEMBER)])
|
|
135
|
+
ok = await bot._mentions_bot(self_mention)
|
|
136
|
+
print(("ok " if ok else "FAIL ") + "@机器人 -> 认作点名")
|
|
137
|
+
if not ok:
|
|
138
|
+
failures += 1
|
|
139
|
+
|
|
140
|
+
bot = make_bot()
|
|
141
|
+
other_mention = msg("老王你看这个", mentions=[User(member_openid="99998888777766665555444433332222", username="老王")])
|
|
142
|
+
ok = not await bot._mentions_bot(other_mention)
|
|
143
|
+
print(("ok " if ok else "FAIL ") + "@别人 -> 不认作点名")
|
|
144
|
+
if not ok:
|
|
145
|
+
failures += 1
|
|
146
|
+
|
|
147
|
+
bot = make_bot()
|
|
148
|
+
ok = not await bot._mentions_bot(msg("没有 mention"))
|
|
149
|
+
print(("ok " if ok else "FAIL ") + "没有 mention -> 不认作点名")
|
|
150
|
+
if not ok:
|
|
151
|
+
failures += 1
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
bot = make_bot()
|
|
155
|
+
failures += await case("窗口外闲聊 -> 只记上下文", bot, msg("今晚谁开黑"), False)
|
|
156
|
+
|
|
157
|
+
bot = make_bot()
|
|
158
|
+
for index in range(3):
|
|
159
|
+
await bot.on_group_message_create(msg(f"闲聊第{index + 1}句"))
|
|
160
|
+
await asyncio.sleep(0.15)
|
|
161
|
+
ok = not prompts(bot)
|
|
162
|
+
print(("ok " if ok else "FAIL ") + f"闲聊未攒够 -> 不插话 (prompts={len(prompts(bot))})")
|
|
163
|
+
if not ok:
|
|
164
|
+
failures += 1
|
|
165
|
+
|
|
166
|
+
await bot.on_group_message_create(msg("闲聊第4句:你们今晚玩啥"))
|
|
167
|
+
await asyncio.sleep(0.3)
|
|
168
|
+
judged = [call for call in prompts(bot) if "群聊插话判断" in str(call.get("text", ""))]
|
|
169
|
+
ok = bool(judged)
|
|
170
|
+
print(("ok " if ok else "FAIL ") + f"攒够 4 条 -> 触发插话判断 (prompts={len(prompts(bot))})")
|
|
171
|
+
if not ok:
|
|
172
|
+
failures += 1
|
|
173
|
+
first_prompt = str(judged[0].get("text", "")) if judged else ""
|
|
174
|
+
ok = "没看到你在群里说过话" in first_prompt and "一直没说话" not in first_prompt
|
|
175
|
+
print(("ok " if ok else "FAIL ") + "没说过话时提示词说「还没说过」")
|
|
176
|
+
if not ok:
|
|
177
|
+
failures += 1
|
|
178
|
+
|
|
179
|
+
await bot.on_group_message_create(msg("闲聊第5句"))
|
|
180
|
+
await asyncio.sleep(0.3)
|
|
181
|
+
ok = len(prompts(bot)) == len(judged)
|
|
182
|
+
print(("ok " if ok else "FAIL ") + "判断刚结束 -> 冷却内不再插话")
|
|
183
|
+
if not ok:
|
|
184
|
+
failures += 1
|
|
185
|
+
|
|
186
|
+
bot = make_bot()
|
|
187
|
+
bot._last_bot_say[f"group:{GROUP}"] = ("我刚说的话", time.time() - 30)
|
|
188
|
+
bot._last_judge_free = True
|
|
189
|
+
for index in range(4):
|
|
190
|
+
await bot.on_group_message_create(msg(f"接话第{index + 1}句"))
|
|
191
|
+
await asyncio.sleep(0.1)
|
|
192
|
+
recent = [call for call in prompts(bot) if "群聊插话判断" in str(call.get("text", ""))]
|
|
193
|
+
ok = bool(recent) and "最近说过" in str(recent[0].get("text", ""))
|
|
194
|
+
print(("ok " if ok else "FAIL ") + "说过话时提示词描述「最近说过」")
|
|
195
|
+
if not ok:
|
|
196
|
+
failures += 1
|
|
197
|
+
|
|
198
|
+
bot = make_bot()
|
|
199
|
+
bot._last_bot_say[f"group:{GROUP}"] = ("七分钟前说的话", time.time() - 400)
|
|
200
|
+
for index in range(4):
|
|
201
|
+
await bot.on_group_message_create(msg(f"又聊第{index + 1}句"))
|
|
202
|
+
await asyncio.sleep(0.1)
|
|
203
|
+
later = [call for call in prompts(bot) if "群聊插话判断" in str(call.get("text", ""))]
|
|
204
|
+
body = str(later[0].get("text", "")) if later else ""
|
|
205
|
+
ok = bool(later) and "安静了" in body and "最近没说话" in body
|
|
206
|
+
print(("ok " if ok else "FAIL ") + "超过记忆窗口 -> 描述为「安静了 X 分钟」")
|
|
207
|
+
if not ok:
|
|
208
|
+
failures += 1
|
|
209
|
+
|
|
210
|
+
bot = make_bot()
|
|
211
|
+
bot_id = User(member_openid=BOT_MEMBER)
|
|
212
|
+
failures += await case("群友 @ 带文字 -> 答", bot, msg("唱首歌", mentions=[bot_id]), True)
|
|
213
|
+
|
|
214
|
+
bot = make_bot()
|
|
215
|
+
failures += await case(
|
|
216
|
+
"群友 @ 只有图 -> 也答", bot, msg("", mentions=[User(member_openid=BOT_MEMBER)], attachments=[{"content_type": "image/png"}]), True
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
bot = make_bot()
|
|
220
|
+
failures += await case("主人窗口外不抢话", bot, msg("我自己部署的", member=OWNER_MEMBER), False)
|
|
221
|
+
|
|
222
|
+
bot = make_bot()
|
|
223
|
+
bot._addressed_at[f"group:{GROUP}"] = time.time()
|
|
224
|
+
failures += await case("主人紧接着说也不答(无 @)", bot, msg("那你觉得呢", member=OWNER_MEMBER), False)
|
|
225
|
+
|
|
226
|
+
bot = make_bot()
|
|
227
|
+
failures += await case("叫名字但没 @ -> 也答(别名视同点名)", bot, msg("大肥鱼你在吗"), True)
|
|
228
|
+
|
|
229
|
+
bot = make_bot()
|
|
230
|
+
await bot.on_group_message_create(msg("/状态"))
|
|
231
|
+
await asyncio.sleep(0.25)
|
|
232
|
+
ok = bool(bot.api.posts) and not prompts(bot)
|
|
233
|
+
print(("ok " if ok else "FAIL ") + f"/命令没 @ -> 本地直接回 (posts={len(bot.api.posts)}, prompts={len(prompts(bot))})")
|
|
234
|
+
if not ok:
|
|
235
|
+
failures += 1
|
|
236
|
+
|
|
237
|
+
print("ALL PASS" if failures == 0 else f"{failures} FAILED")
|
|
238
|
+
return 0 if failures == 0 else 1
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
sys.exit(asyncio.run(main()))
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"""Plan B: independent judge decides, main model gets the verbatim pool."""
|
|
2
|
+
import os as _os, tempfile as _tempfile
|
|
3
|
+
_os.environ.setdefault("DSH_QQBOT_STATE_DIR", _tempfile.mkdtemp(prefix="qqbot-test-"))
|
|
4
|
+
import asyncio
|
|
5
|
+
import dataclasses
|
|
6
|
+
import sys
|
|
7
|
+
import time
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
sys.path.insert(0, str(Path('.').resolve()))
|
|
11
|
+
from main import DSHBot, PooledMessage, load_config # noqa: E402
|
|
12
|
+
|
|
13
|
+
GROUP = "CCE96DDF6F462E1010E5721BBA1B6CBB"
|
|
14
|
+
CONV = f"group:{GROUP}"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class _RT:
|
|
18
|
+
def __init__(self, mode):
|
|
19
|
+
self.mode = mode
|
|
20
|
+
|
|
21
|
+
def get_bool(self, key, default=True):
|
|
22
|
+
if key == "group_at_only":
|
|
23
|
+
return False
|
|
24
|
+
if key == "interject_hook_only":
|
|
25
|
+
return False # 这些用例验证的是判断提示词本身,不走省钱预筛
|
|
26
|
+
return default
|
|
27
|
+
|
|
28
|
+
def get_int(self, key, default=0):
|
|
29
|
+
return 0 if key == "debounce_ms" else default
|
|
30
|
+
|
|
31
|
+
def get_float(self, key, default=0.0):
|
|
32
|
+
return default
|
|
33
|
+
|
|
34
|
+
def get_str(self, key, default=""):
|
|
35
|
+
if key == "group_mode":
|
|
36
|
+
return self.mode
|
|
37
|
+
return default
|
|
38
|
+
|
|
39
|
+
def get(self, key, default=None):
|
|
40
|
+
if key == "group_mode":
|
|
41
|
+
return self.mode
|
|
42
|
+
return default
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class StubAPI:
|
|
46
|
+
def __init__(self):
|
|
47
|
+
self.posts = []
|
|
48
|
+
|
|
49
|
+
async def post_group_message(self, **kw):
|
|
50
|
+
self.posts.append(kw)
|
|
51
|
+
return {"id": "g"}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class StubBridge:
|
|
55
|
+
def __init__(self, verdict):
|
|
56
|
+
self.verdict = verdict
|
|
57
|
+
self.calls = []
|
|
58
|
+
self.judged = []
|
|
59
|
+
|
|
60
|
+
async def judge(self, conv_key, transcript, *, threshold=2, hint=""):
|
|
61
|
+
self.judged.append(transcript)
|
|
62
|
+
self.last_threshold = threshold
|
|
63
|
+
self.last_hint = hint
|
|
64
|
+
return self.verdict
|
|
65
|
+
|
|
66
|
+
async def inbound(self, **kw):
|
|
67
|
+
self.calls.append(kw)
|
|
68
|
+
return {"ok": True}
|
|
69
|
+
|
|
70
|
+
async def pull(self, cursor, wait):
|
|
71
|
+
return {"cursor": 0, "events": []}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class Msg:
|
|
75
|
+
def __init__(self, text, message_id="M1"):
|
|
76
|
+
self.id = message_id
|
|
77
|
+
self.content = text
|
|
78
|
+
self.group_openid = GROUP
|
|
79
|
+
self.attachments = []
|
|
80
|
+
self.mentions = []
|
|
81
|
+
self.raw = {}
|
|
82
|
+
self.author = type("A", (), {"member_openid": "MEMBER1", "user_openid": "U1"})()
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def make_bot(mode, verdict):
|
|
86
|
+
config = dataclasses.replace(load_config(), ack_delay_s=0, debounce_s=0.0, debounce_max_s=0.0)
|
|
87
|
+
bot = DSHBot.__new__(DSHBot)
|
|
88
|
+
bot.config = config
|
|
89
|
+
bot.api = StubAPI()
|
|
90
|
+
bot.bridge = StubBridge(verdict)
|
|
91
|
+
bot.state = type("S", (), {"owner_openid": "OWNER", "group_owners": {}, "save": lambda self: None})()
|
|
92
|
+
bot.owner_openid = "OWNER"
|
|
93
|
+
bot.rt = _RT(mode)
|
|
94
|
+
for name in ("targets", "pending_approvals", "pending", "queued", "recent_images", "group_state", "chime",
|
|
95
|
+
"_seen_ids", "_submitted_at", "_inflight", "_inflight_at", "_rate", "_addressed_at",
|
|
96
|
+
"_bot_spoke_at", "_last_sent", "_last_bot_say", "_streams", "_acks", "_pending_quotes", "pool"):
|
|
97
|
+
setattr(bot, name, {})
|
|
98
|
+
bot.pool_idx = {}
|
|
99
|
+
bot._streamed_turns = {}
|
|
100
|
+
bot._chat_turns = {}
|
|
101
|
+
bot.member_index = {}
|
|
102
|
+
bot._suppress_reply = set()
|
|
103
|
+
bot._urgent_tasks = set()
|
|
104
|
+
bot._pull_task = None
|
|
105
|
+
bot._watchdog_task = None
|
|
106
|
+
bot.chatter = {}
|
|
107
|
+
bot._msg_seq = 0
|
|
108
|
+
bot._in_quiet_hours = lambda: False
|
|
109
|
+
return bot
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def check(name, ok, extra=""):
|
|
113
|
+
print(("ok " if ok else "FAIL ") + name + (f" {extra}" if extra else ""))
|
|
114
|
+
return 0 if ok else 1
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
async def main():
|
|
118
|
+
failures = 0
|
|
119
|
+
|
|
120
|
+
bot = make_bot("B", {"ok": True, "needReply": False, "note": "两个人在互相聊"})
|
|
121
|
+
bot._register(CONV, "group", GROUP, "M1")
|
|
122
|
+
bot.pool["M0"] = PooledMessage(message_id="M0", conv_key=CONV, kind="group", openid=GROUP,
|
|
123
|
+
author="群友A", text="好无聊啊", outbound=False, at=time.time())
|
|
124
|
+
await bot.on_group_message_create(Msg("那我们去玩游戏吧"))
|
|
125
|
+
await asyncio.sleep(0.2)
|
|
126
|
+
failures += check("B 模式:判断为不回 -> 不打扰主模型", not bot.bridge.calls or bot.bridge.calls[-1].get("context_only") is True,
|
|
127
|
+
str(bot.bridge.calls[-1:])[:80])
|
|
128
|
+
failures += check("B 模式:原文进了判断器", bool(bot.bridge.judged) and "好无聊啊" in bot.bridge.judged[-1])
|
|
129
|
+
failures += check("原文带时间戳", bool(bot.bridge.judged) and bot.bridge.judged[-1].startswith("["))
|
|
130
|
+
|
|
131
|
+
bot = make_bot("B", {"ok": True, "needReply": True, "note": "在问机器人"})
|
|
132
|
+
bot._register(CONV, "group", GROUP, "M1")
|
|
133
|
+
bot.pool["M0"] = PooledMessage(message_id="M0", conv_key=CONV, kind="group", openid=GROUP,
|
|
134
|
+
author="群友A", text="好无聊啊", outbound=False, at=time.time())
|
|
135
|
+
await bot.on_group_message_create(Msg("那你觉得呢")) # 没叫名字,才走 Plan B 判断
|
|
136
|
+
await asyncio.sleep(0.2)
|
|
137
|
+
sent = bot.bridge.calls[-1]["text"] if bot.bridge.calls else ""
|
|
138
|
+
failures += check("B 模式:判断为回 -> 带原文交给主模型", "好无聊啊" in sent, sent[:60])
|
|
139
|
+
failures += check("B 模式:附上判断理由", "判断参考" in sent and "在问机器人" in sent)
|
|
140
|
+
|
|
141
|
+
bot = make_bot("A", {"ok": True, "needReply": True, "note": ""})
|
|
142
|
+
bot._register(CONV, "group", GROUP, "M1")
|
|
143
|
+
bot.chatter[CONV] = type("C", (), {"lines": ["群友A: 今晚开黑"], "first_at": time.time(), "last_judge_at": 0.0})()
|
|
144
|
+
await bot._maybe_judge(CONV, force_timer=True)
|
|
145
|
+
sent = bot.bridge.calls[-1]["text"] if bot.bridge.calls else ""
|
|
146
|
+
failures += check("A 模式保持原样(仍是插话判断提示词)", "【群聊插话判断】" in sent and not bot.bridge.judged)
|
|
147
|
+
|
|
148
|
+
print("ALL PASS" if failures == 0 else f"{failures} FAILED")
|
|
149
|
+
return 0 if failures == 0 else 1
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
if __name__ == "__main__":
|
|
153
|
+
sys.exit(asyncio.run(main()))
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"""Offline check: queued messages are drained and the in-flight counter never leaks."""
|
|
2
|
+
import os as _os, tempfile as _tempfile
|
|
3
|
+
_os.environ.setdefault("DSH_QQBOT_STATE_DIR", _tempfile.mkdtemp(prefix="qqbot-test-"))
|
|
4
|
+
import asyncio, dataclasses, sys
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
sys.path.insert(0, str(Path('.').resolve()))
|
|
7
|
+
from main import DSHBot, QueuedPrompt, load_config
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class StubAPI:
|
|
11
|
+
def __init__(self): self.posts = []
|
|
12
|
+
async def post_c2c_message(self, **kw): self.posts.append(kw); return {"id": "m"}
|
|
13
|
+
async def post_group_message(self, **kw): self.posts.append(kw); return {"id": "m"}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class StubBridge:
|
|
17
|
+
def __init__(self): self.calls = []
|
|
18
|
+
async def inbound(self, **kw): self.calls.append(kw); return {"ok": True}
|
|
19
|
+
async def pull(self, cursor, wait): return {"cursor": 0, "events": []}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def make():
|
|
23
|
+
config = dataclasses.replace(load_config(), ack_delay_s=0)
|
|
24
|
+
bot = DSHBot.__new__(DSHBot)
|
|
25
|
+
bot.config = config
|
|
26
|
+
bot.api = StubAPI()
|
|
27
|
+
bot.bridge = StubBridge()
|
|
28
|
+
bot.state = type("S", (), {"owner_openid": "OWNER", "group_owners": {}, "save": lambda self: None})()
|
|
29
|
+
bot.owner_openid = "OWNER"
|
|
30
|
+
bot.targets = {}
|
|
31
|
+
bot.pending_approvals = {}
|
|
32
|
+
bot.pending = {}
|
|
33
|
+
bot.queued = {}
|
|
34
|
+
bot.chatter = {}
|
|
35
|
+
bot.recent_images = {}
|
|
36
|
+
bot.group_state = {}
|
|
37
|
+
bot.chime = {}
|
|
38
|
+
bot._seen_ids = {}
|
|
39
|
+
bot._submitted_at = {}
|
|
40
|
+
bot._inflight = {}
|
|
41
|
+
bot._inflight_at = {}
|
|
42
|
+
bot._rate = {}
|
|
43
|
+
bot._addressed_at = {}
|
|
44
|
+
bot._bot_spoke_at = {}
|
|
45
|
+
bot._last_sent = {}
|
|
46
|
+
bot._last_bot_say = {}
|
|
47
|
+
bot._streams = {}
|
|
48
|
+
bot._suppress_reply = set()
|
|
49
|
+
bot._pending_quotes = {}
|
|
50
|
+
bot._chat_turns = {}
|
|
51
|
+
bot.member_index = {}
|
|
52
|
+
bot._acks = {}
|
|
53
|
+
bot._msg_seq = 0
|
|
54
|
+
bot._pull_task = None
|
|
55
|
+
bot._watchdog_task = None
|
|
56
|
+
bot._urgent_tasks = set()
|
|
57
|
+
bot.rt = None
|
|
58
|
+
bot._in_quiet_hours = lambda: False
|
|
59
|
+
return bot
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
async def main():
|
|
63
|
+
failures = 0
|
|
64
|
+
conv = "c2c:OWNEROPENID000000000000000000"
|
|
65
|
+
|
|
66
|
+
bot = make()
|
|
67
|
+
bot._register(conv, "c2c", conv.split(":", 1)[1], None)
|
|
68
|
+
bot._inflight[conv] = 1
|
|
69
|
+
bot._suppress_reply.add(conv)
|
|
70
|
+
await bot._handle_event({"type": "reply", "convKey": conv, "text": "流式已发过", "tools": []})
|
|
71
|
+
ok = bot._inflight.get(conv, 0) == 0 and conv not in bot._suppress_reply
|
|
72
|
+
print(("ok " if ok else "FAIL ") + f"流式后抑制耐久回复也归还计数 (inflight={bot._inflight.get(conv, 0)})")
|
|
73
|
+
failures += 0 if ok else 1
|
|
74
|
+
|
|
75
|
+
bot = make()
|
|
76
|
+
bot._register(conv, "c2c", conv.split(":", 1)[1], None)
|
|
77
|
+
bot._inflight[conv] = 1
|
|
78
|
+
bot.queued[conv] = QueuedPrompt(parts=["不想动脑", "你怎么死了"], images=[], sender_name="主人", is_owner=True, chime=False, first_at=__import__("time").time())
|
|
79
|
+
await bot._handle_event({"type": "reply", "convKey": conv, "text": "先回答上一句", "tools": []})
|
|
80
|
+
ok2 = bool(bot.bridge.calls) and "不想动脑" in str(bot.bridge.calls[-1])
|
|
81
|
+
print(("ok " if ok2 else "FAIL ") + f"回合结束后把排队消息发出去 (inbound={len(bot.bridge.calls)})")
|
|
82
|
+
failures += 0 if ok2 else 1
|
|
83
|
+
|
|
84
|
+
ok3 = conv not in bot.queued
|
|
85
|
+
print(("ok " if ok3 else "FAIL ") + "队列已清空")
|
|
86
|
+
failures += 0 if ok3 else 1
|
|
87
|
+
|
|
88
|
+
class RTOn:
|
|
89
|
+
def get_bool(self, key, default=True): return key == "persona_enabled"
|
|
90
|
+
def get_int(self, key, default=0): return default
|
|
91
|
+
def get_float(self, key, default=0.0): return default
|
|
92
|
+
def get(self, key, default=None):
|
|
93
|
+
return {"persona_group": "群聊人设喵", "persona_c2c": "私聊人设喵"}.get(key, default)
|
|
94
|
+
|
|
95
|
+
bot = make(); bot.rt = RTOn()
|
|
96
|
+
gconv = "group:GROUPOPENID"
|
|
97
|
+
bot._register(gconv, "group", "GROUPOPENID", None)
|
|
98
|
+
await bot._submit(gconv, "在吗", "群友A", False)
|
|
99
|
+
ok4 = bot.bridge.calls[-1].get("persona") == "群聊人设喵"
|
|
100
|
+
print(("ok " if ok4 else "FAIL ") + f"群聊带群聊人设 (persona={bot.bridge.calls[-1].get('persona')!r})")
|
|
101
|
+
failures += 0 if ok4 else 1
|
|
102
|
+
|
|
103
|
+
cconv = "c2c:OTHEROPENID"
|
|
104
|
+
bot._register(cconv, "c2c", "OTHEROPENID", None)
|
|
105
|
+
await bot._submit(cconv, "在吗", "主人", True)
|
|
106
|
+
ok5 = bot.bridge.calls[-1].get("persona") == "私聊人设喵"
|
|
107
|
+
print(("ok " if ok5 else "FAIL ") + f"私聊带私聊人设 (persona={bot.bridge.calls[-1].get('persona')!r})")
|
|
108
|
+
failures += 0 if ok5 else 1
|
|
109
|
+
|
|
110
|
+
print("ALL PASS" if failures == 0 else f"{failures} FAILED")
|
|
111
|
+
return 0 if failures == 0 else 1
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
sys.exit(asyncio.run(main()))
|