feat!: intents 只按大类订阅、群消息全量模式与事件流优化

破坏性变更:intents.toml 订阅粒度收敛到 Intents 大类(分组)——
配置从「[分组] 表内逐事件开关」改为「分组名 = true/false」。网关只接受
分组位掩码,组内事件本就无法单独订阅,此前的逐事件开关仅是「组内任一
为 true 即订阅整组」的装饰。旧格式不再兼容,与拼错的分组名、非布尔值
一样在启动时直接抛 ValueError,而非静默少订阅。

新增:
- GROUP_MESSAGE_CREATE 事件接收(群消息全量模式,与 GROUP_AT_MESSAGE_CREATE
  共用 GroupAtMessage);GroupAtMessage 补齐 message_type/message_scene/
  attachments/mentions/ark_data/msg_elements 字段

优化:
- 形参注入缓存化:合并缓存 handler 签名与类型标注解析(此前每条事件都重跑
  inspect.signature/get_type_hints),services 命中改为每条事件实时判定,
  保证「先注册 handler、后登记组件」的既有装配顺序不受影响
- EventQueue.put_event/put_reply 去掉多余的 return await
- model.NoneType 联合成员顺序调整(None 置于末尾)

文档同步 GUIDE/API/README/CHANGELOG 与示例;测试 76 全绿,ruff/pyright 干净。
This commit is contained in:
snowy-yang committed 2026-09-20 21:39:25 +08:00
1 parent 3237558128
commit 71cbfdaf5d
16 files changed
+204 -116

No files matched your search

+26
View File
@@ -99,6 +99,32 @@ async def test_unregistered_annotation_falls_back_to_raw_dict():
assert received == [{"session_id": "s1"}]
@pytest.mark.asyncio
async def test_service_registered_after_handler_still_injects():
"""回归:形参解析被缓存(签名+标注),但 services 命中必须在每条事件时判定。
先注册 handler、后登记组件是 run_loop 里的真实时序(用户 handler 在
main() 之前注册);若缓存把"已解析好的实参"也存下来,这里会永远拿不到
实例。同一 handler 连发两条也一并覆盖缓存复用路径。
"""
emitter = EventEmitter()
class Store:
pass
store = Store()
got: list[Store] = []
@emitter.on("READY")
async def handler(st: Store):
got.append(st)
emitter.services[Store] = store # 登记晚于 handler 注册
await emitter.emit({"op": Opcode.DISPATCH, "t": "READY", "d": {}})
await emitter.emit({"op": Opcode.DISPATCH, "t": "READY", "d": {}})
assert got == [store, store]
@pytest.mark.asyncio
async def test_scalar_d_falls_back_to_raw_value():
"""回归:op=9 INVALID_SESSION 的 d 是布尔值,DI 回退必须传原始 d。
+23 -7
View File
@@ -23,20 +23,36 @@ def test_missing_file_returns_zero(tmp_path):
def test_enabled_group_sets_bit(tmp_path):
path = write(tmp_path, "[GROUP_AND_C2C_EVENT]\nC2C_MESSAGE_CREATE = true\n")
path = write(tmp_path, "GROUP_AND_C2C_EVENT = true\n")
assert get_intents(path) & (1 << 25)
def test_only_enabled_groups_are_subscribed(tmp_path):
path = write(tmp_path, "GUILDS = true\nINTERACTION = false\nAUDIO_ACTION = true\n")
assert get_intents(path) == (1 << 0) | (1 << 29)
def test_all_disabled_returns_zero(tmp_path):
path = write(tmp_path, "[GUILDS]\nGUILD_CREATE = false\n")
path = write(tmp_path, "GUILDS = false\nFORUMS_EVENT = false\n")
assert get_intents(path) == 0
def test_unknown_group_is_skipped(tmp_path):
path = write(
tmp_path, "[NOT_A_GROUP]\nFOO = true\n\n[GUILDS]\nGUILD_CREATE = true\n"
)
assert get_intents(path) == (1 << 0)
def test_unknown_group_raises(tmp_path):
path = write(tmp_path, "NOT_A_GROUP = true\n")
with pytest.raises(ValueError, match="NOT_A_GROUP"):
get_intents(path)
def test_non_bool_value_raises(tmp_path):
path = write(tmp_path, 'GUILDS = "yes"\n')
with pytest.raises(ValueError, match="true/false"):
get_intents(path)
def test_legacy_per_event_switches_are_rejected(tmp_path):
path = write(tmp_path, "[GROUP_AND_C2C_EVENT]\nC2C_MESSAGE_CREATE = true\n")
with pytest.raises(ValueError, match="组内事件开关"):
get_intents(path)
def test_config_load_defaults():
+47
View File
@@ -28,6 +28,53 @@ def test_parse_group_at_message():
assert msg.author is not None and msg.author.member_openid == "M1"
def test_parse_group_full_message():
msg = parse_event(
"GROUP_MESSAGE_CREATE",
{
"author": {"member_openid": "M2"},
"content": "大家好",
"group_openid": "G1",
"id": "msgid-2",
"timestamp": "2026-01-01",
"message_type": 0,
"message_scene": {"unknown": False},
"attachments": [{"content_type": 1, "url": "https://x/img.png"}],
"mentions": [{"id": "U1"}],
"msg_elements": [{"type": 1, "text_element": {"content": "大家好"}}],
},
)
assert isinstance(msg, GroupAtMessage)
assert msg.group_openid == "G1"
assert msg.user_openid == "M2"
assert msg.message_type == 0
assert msg.message_scene == {"unknown": False}
assert msg.attachments == [{"content_type": 1, "url": "https://x/img.png"}]
assert msg.mentions == [{"id": "U1"}]
assert msg.msg_elements == [{"type": 1, "text_element": {"content": "大家好"}}]
async def test_group_full_message_reaches_typed_handler():
queue = EventQueue()
emitter = EventEmitter(queue)
received: list[GroupAtMessage] = []
@emitter.on("GROUP_MESSAGE_CREATE")
async def handler(msg: GroupAtMessage):
received.append(msg)
await emitter.emit(
{
"op": Opcode.DISPATCH,
"t": "GROUP_MESSAGE_CREATE",
"d": {"content": "hi", "group_openid": "G1"},
}
)
assert len(received) == 1
assert received[0].content == "hi"
assert received[0].group_openid == "G1"
def test_parse_c2c_message():
msg = parse_event(
"C2C_MESSAGE_CREATE",