@cmach_socket/opencode-qq-bot 0.5.1 → 0.6.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/src/config.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Model } from "@opencode/schema"
2
2
  import { z } from "zod"
3
+ import type { AccessConfig } from "./access"
3
4
  import type { LogLevel } from "./logger"
4
5
 
5
6
  /**
@@ -15,6 +16,8 @@ const TRUTHY = new Set(["1", "true", "yes", "on"])
15
16
  const BOOLEAN_SPELLINGS = ["1", "0", "true", "false", "yes", "no", "on", "off"] as const
16
17
  /** 合法的日志级别。 */
17
18
  const LOG_LEVELS: readonly string[] = ["debug", "info", "warn", "error"]
19
+ /** 可订阅的事件类别。 */
20
+ export const EVENT_CATEGORIES: readonly string[] = ["message", "notice"]
18
21
 
19
22
  export interface NapcatConfig {
20
23
  /** NapCat 正向 WebSocket 地址,必填。 */
@@ -34,6 +37,46 @@ export interface ProactiveConfig {
34
37
  probabilityPerHour: number
35
38
  /** 心跳间隔(秒)。 */
36
39
  tickSeconds: number
40
+ /** 只在这些群里主动说话;为空表示不限制。 */
41
+ allowGroups: number[]
42
+ /** 只在这些私聊里主动说话;为空表示不限制。 */
43
+ allowUsers: number[]
44
+ }
45
+
46
+ /** 唤醒判定配置:全局的触发条件。 */
47
+ export interface WakeConfig {
48
+ /** 全局唤醒前缀,命中即必回。 */
49
+ prefixes: string[]
50
+ /** 是否忽略 @全体成员。 */
51
+ ignoreAtAll: boolean
52
+ /** 回复机器人自己的消息是否必回。 */
53
+ replyToSelf: boolean
54
+ /** 群里第一条 @ 的是别人时是否不唤醒机器人。 */
55
+ ignoreAtOthers: boolean
56
+ }
57
+
58
+ /** 回复节奏配置:怎么拆段、怎么发。 */
59
+ export interface ReplyConfig {
60
+ /** 分段之间的最小间隔(毫秒)。 */
61
+ minDelayMs: number
62
+ /** 分段之间的最大间隔(毫秒),0 表示沿用 `sendDelayMs`。 */
63
+ maxDelayMs: number
64
+ /** 单条消息超过该字数就拆成多段发送。 */
65
+ segmentChars: number
66
+ /** 单条回复超过该字数就改用合并转发,0 表示关闭。 */
67
+ forwardThresholdChars: number
68
+ }
69
+
70
+ /** 调用链追踪配置。 */
71
+ export interface TraceConfig {
72
+ /** 是否记录调用链追踪。 */
73
+ enabled: boolean
74
+ /** 追踪文件目录,空表示与日志同目录。 */
75
+ dir: string
76
+ /** 追踪文件总容量上限(MB)。 */
77
+ maxSizeMb: number
78
+ /** 追踪文件保留天数。 */
79
+ retentionDays: number
37
80
  }
38
81
 
39
82
  export interface ReminderConfig {
@@ -62,6 +105,10 @@ export interface RateLimitConfig {
62
105
  maxPerHour: number
63
106
  /** 相邻两次发送之间的最小间隔(毫秒),0 表示不限制。 */
64
107
  minGapMs: number
108
+ /** 触顶时是否等窗口结束再发;关闭时直接拒绝这次发送。 */
109
+ stall: boolean
110
+ /** 单次等待窗口结束的上限(毫秒)。 */
111
+ stallMaxWaitMs: number
65
112
  }
66
113
 
67
114
  export interface SensitiveConfig {
@@ -81,6 +128,16 @@ export interface LogConfig {
81
128
  maxFiles: number
82
129
  }
83
130
 
131
+ /** 事件订阅配置。 */
132
+ export interface EventsConfig {
133
+ /** 订阅哪些事件类别:`message` / `notice`。 */
134
+ subscribe: string[]
135
+ /** notice 类别里具体关心哪些通知;留空表示全收。 */
136
+ notices: string[]
137
+ /** 通知是否注入会话上下文。 */
138
+ inject: boolean
139
+ }
140
+
84
141
  /** 媒体配置。 */
85
142
  export interface MediaConfig {
86
143
  /** 额外允许发送媒体的目录。 */
@@ -95,10 +152,16 @@ export interface ModelRef {
95
152
 
96
153
  export interface QQBotConfig {
97
154
  napcat: NapcatConfig
98
- /** 参与 AI 对话的群号。 */
99
- groups: number[]
100
- /** 参与 AI 对话的私聊 QQ 号。 */
101
- users: number[]
155
+ /** 准入配置:放行名单、拒绝名单与名单为空时的行为。 */
156
+ access: AccessConfig
157
+ /** 机器人所有者 QQ 号:可用管理命令,并拥有全部工具权限。 */
158
+ ownerQQ?: number
159
+ /** 管理员 QQ 号:拥有全部工具权限。 */
160
+ admins: number[]
161
+ /** 工具配置文件路径,空表示当前工作目录下的 `tool-profiles.json`。 */
162
+ toolProfilesPath?: string
163
+ /** 会话行为覆盖文件路径,空表示当前工作目录下的 `chat-overrides.json`。 */
164
+ chatOverridesPath?: string
102
165
  /** 角色设定文件路径,默认使用插件包内的 souls 目录。 */
103
166
  soulPath?: string
104
167
  /** souls 目录里的角色设定文件名,默认 default.txt。 */
@@ -116,19 +179,29 @@ export interface QQBotConfig {
116
179
  /** 重启后是否复用上一次的会话。 */
117
180
  reuseSession: boolean
118
181
  proactive: ProactiveConfig
182
+ wake: WakeConfig
183
+ reply: ReplyConfig
184
+ trace: TraceConfig
119
185
  reminder: ReminderConfig
120
186
  keepAlive: KeepAliveConfig
121
187
  history: HistoryConfig
122
188
  rateLimit: RateLimitConfig
123
189
  sensitive: SensitiveConfig
190
+ events: EventsConfig
124
191
  log: LogConfig
125
192
  media: MediaConfig
126
193
  }
127
194
 
128
195
  /** 除 NapCat 连接信息外的全部默认值。 */
129
196
  export const DEFAULT_CONFIG: Omit<QQBotConfig, "napcat"> = {
130
- groups: [],
131
- users: [],
197
+ access: {
198
+ allow: { groups: [], users: [] },
199
+ deny: { groups: [], users: [] },
200
+ allowAllWhenEmpty: false,
201
+ },
202
+ admins: [],
203
+ toolProfilesPath: "",
204
+ chatOverridesPath: "",
132
205
  sendDelayMs: 1000,
133
206
  warnLength: 500,
134
207
  reuseSession: false,
@@ -138,21 +211,42 @@ export const DEFAULT_CONFIG: Omit<QQBotConfig, "napcat"> = {
138
211
  startProbability: 0.01,
139
212
  probabilityPerHour: 0.05,
140
213
  tickSeconds: 3600,
214
+ allowGroups: [],
215
+ allowUsers: [],
141
216
  },
217
+ wake: { prefixes: ["/"], ignoreAtAll: true, replyToSelf: true, ignoreAtOthers: true },
218
+ reply: { minDelayMs: 0, maxDelayMs: 0, segmentChars: 150, forwardThresholdChars: 1500 },
219
+ trace: { enabled: true, dir: "", maxSizeMb: 80, retentionDays: 7 },
142
220
  reminder: { enabled: true, max: 2 },
143
221
  keepAlive: { enabled: true, seconds: 20 * 60 },
144
222
  history: { count: 20 },
145
- rateLimit: { maxPerMinute: 8, maxPerHour: 60, minGapMs: 1500 },
223
+ rateLimit: {
224
+ maxPerMinute: 8,
225
+ maxPerHour: 60,
226
+ minGapMs: 1500,
227
+ stall: false,
228
+ stallMaxWaitMs: 60_000,
229
+ },
146
230
  sensitive: { enabled: true },
231
+ events: { subscribe: ["message"], notices: [], inject: true },
147
232
  log: { level: "info", dir: "", maxSize: "5m", maxFiles: 5 },
148
233
  media: { allowedDirs: [] },
149
234
  }
150
235
 
236
+ /** 迁移类告警前缀:启动时用更高等级输出,避免升级用户静默失联。 */
237
+ export const MIGRATION_WARNING_PREFIX = "【迁移】"
238
+
239
+ /** 移除的环境变量与替代项。 */
240
+ const RENAMED_ENV_VARS: Record<string, string> = {
241
+ QQ_TARGET_GROUPS: "QQ_ALLOW_GROUPS",
242
+ QQ_TARGET_USERS: "QQ_ALLOW_USERS",
243
+ }
244
+
151
245
  /** 配置告警回调。 */
152
246
  export type ConfigWarning = (message: string) => void
153
247
 
154
248
  /** 解析 "provider/model" 或 "provider/model/variant",复用 SDK 的模型引用解析。 */
155
- function parseModelRef(value: unknown): ModelRef | undefined {
249
+ export function parseModelRef(value: unknown): ModelRef | undefined {
156
250
  const raw = typeof value === "string" ? value.trim() : ""
157
251
  if (!raw) return undefined
158
252
  const [providerID, id, variant, ...rest] = raw.split("/").filter(Boolean)
@@ -329,28 +423,38 @@ function createConfigSchema(
329
423
  return raw as LogLevel
330
424
  })
331
425
 
332
- const groups = z
333
- .unknown()
334
- .optional()
335
- .transform((value) => {
336
- if (value === undefined || value === null) return [...defaults.groups]
337
- const parsed = toIds(value, warn, label("groups"), "群号")
338
- if (!parsed) {
339
- if (present(value)) warn(`${label("groups")} 解析不出群号:${String(value)}`)
340
- return [...defaults.groups]
341
- }
342
- return parsed
343
- })
426
+ /** 编号名单:数组或逗号分隔字符串。 */
427
+ const idList = (path: string, fallback: readonly number[], noun: string) =>
428
+ z
429
+ .unknown()
430
+ .optional()
431
+ .transform((value) => {
432
+ if (value === undefined || value === null) return [...fallback]
433
+ const parsed = toIds(value, warn, label(path), noun)
434
+ if (!parsed) {
435
+ if (present(value)) warn(`${label(path)} 解析不出${noun}:${String(value)}`)
436
+ return [...fallback]
437
+ }
438
+ return parsed
439
+ })
440
+
441
+ const admins = idList("admins", defaults.admins, "QQ 号")
344
442
 
345
- const users = z
443
+ const ownerQQ = z
346
444
  .unknown()
347
445
  .optional()
348
446
  .transform((value) => {
349
- if (value === undefined || value === null) return [...defaults.users]
350
- const parsed = toIds(value, warn, label("users"), "QQ 号")
351
- if (!parsed) {
352
- if (present(value)) warn(`${label("users")} 解析不出 QQ 号:${String(value)}`)
353
- return [...defaults.users]
447
+ if (!present(value)) return defaults.ownerQQ
448
+ // 只接受数字或非空数字字符串:布尔值经 Number() 会变成 0/1。
449
+ const parsed =
450
+ typeof value === "number"
451
+ ? value
452
+ : typeof value === "string" && value.trim() !== ""
453
+ ? Number(value.trim())
454
+ : Number.NaN
455
+ if (!Number.isInteger(parsed) || parsed <= 0) {
456
+ warn(`${label("ownerQQ")} 不是合法的 QQ 号:${String(value)}`)
457
+ return defaults.ownerQQ
354
458
  }
355
459
  return parsed
356
460
  })
@@ -368,6 +472,34 @@ function createConfigSchema(
368
472
  return parsed
369
473
  })
370
474
 
475
+ /** 字符串列表:数组或逗号分隔字符串。 */
476
+ const stringList = (path: string, fallback: readonly string[]) =>
477
+ z
478
+ .unknown()
479
+ .optional()
480
+ .transform((value) => {
481
+ if (value === undefined || value === null) return [...fallback]
482
+ const source = Array.isArray(value)
483
+ ? value
484
+ : typeof value === "string"
485
+ ? value.split(",")
486
+ : undefined
487
+ if (!source) {
488
+ if (present(value)) {
489
+ warn(`${label(path)} 必须是字符串或字符串数组:${String(value)}`)
490
+ }
491
+ return [...fallback]
492
+ }
493
+ const items: string[] = []
494
+ for (const entry of source) {
495
+ const raw = typeof entry === "string" ? entry.trim() : ""
496
+ if (!raw) continue
497
+ if (items.includes(raw)) continue
498
+ items.push(raw)
499
+ }
500
+ return items
501
+ })
502
+
371
503
  /** 允许发送媒体的目录:数组或逗号分隔字符串。 */
372
504
  const mediaDirs = z
373
505
  .unknown()
@@ -403,8 +535,44 @@ function createConfigSchema(
403
535
  })
404
536
  .prefault({})
405
537
  .catch({ url: napcat.url, token: napcat.token }),
406
- groups,
407
- users,
538
+ access: z
539
+ .object({
540
+ allow: z
541
+ .object({
542
+ groups: idList("access.allow.groups", defaults.access.allow.groups, "群号").default([
543
+ ...defaults.access.allow.groups,
544
+ ]),
545
+ users: idList("access.allow.users", defaults.access.allow.users, "QQ 号").default([
546
+ ...defaults.access.allow.users,
547
+ ]),
548
+ })
549
+ .prefault({})
550
+ .catch({
551
+ groups: [...defaults.access.allow.groups],
552
+ users: [...defaults.access.allow.users],
553
+ }),
554
+ deny: z
555
+ .object({
556
+ groups: idList("access.deny.groups", defaults.access.deny.groups, "群号").default([
557
+ ...defaults.access.deny.groups,
558
+ ]),
559
+ users: idList("access.deny.users", defaults.access.deny.users, "QQ 号").default([
560
+ ...defaults.access.deny.users,
561
+ ]),
562
+ })
563
+ .prefault({})
564
+ .catch({
565
+ groups: [...defaults.access.deny.groups],
566
+ users: [...defaults.access.deny.users],
567
+ }),
568
+ allowAllWhenEmpty: boolean("access.allowAllWhenEmpty", defaults.access.allowAllWhenEmpty),
569
+ })
570
+ .prefault({})
571
+ .catch(defaults.access),
572
+ admins,
573
+ ownerQQ,
574
+ toolProfilesPath: text("toolProfilesPath", defaults.toolProfilesPath),
575
+ chatOverridesPath: text("chatOverridesPath", defaults.chatOverridesPath),
408
576
  soulPath: text("soulPath", defaults.soulPath),
409
577
  soulName: text("soulName", defaults.soulName),
410
578
  home: text("home", defaults.home),
@@ -429,9 +597,60 @@ function createConfigSchema(
429
597
  defaults.proactive.probabilityPerHour,
430
598
  ),
431
599
  tickSeconds: positive("proactive.tickSeconds", defaults.proactive.tickSeconds),
600
+ allowGroups: idList(
601
+ "proactive.allowGroups",
602
+ defaults.proactive.allowGroups,
603
+ "群号",
604
+ ).default([...defaults.proactive.allowGroups]),
605
+ allowUsers: idList("proactive.allowUsers", defaults.proactive.allowUsers, "QQ 号").default([
606
+ ...defaults.proactive.allowUsers,
607
+ ]),
432
608
  })
433
609
  .prefault({})
434
610
  .catch(defaults.proactive),
611
+ wake: z
612
+ .object({
613
+ prefixes: stringList("wake.prefixes", defaults.wake.prefixes).default([
614
+ ...defaults.wake.prefixes,
615
+ ]),
616
+ ignoreAtAll: boolean("wake.ignoreAtAll", defaults.wake.ignoreAtAll),
617
+ replyToSelf: boolean("wake.replyToSelf", defaults.wake.replyToSelf),
618
+ ignoreAtOthers: boolean("wake.ignoreAtOthers", defaults.wake.ignoreAtOthers),
619
+ })
620
+ .prefault({})
621
+ .catch(defaults.wake),
622
+ reply: z
623
+ .object({
624
+ minDelayMs: nonNegative("reply.minDelayMs", defaults.reply.minDelayMs),
625
+ maxDelayMs: nonNegative("reply.maxDelayMs", defaults.reply.maxDelayMs),
626
+ segmentChars: integer("reply.segmentChars", defaults.reply.segmentChars, 1),
627
+ forwardThresholdChars: nonNegative(
628
+ "reply.forwardThresholdChars",
629
+ defaults.reply.forwardThresholdChars,
630
+ ),
631
+ })
632
+ .prefault({})
633
+ .catch(defaults.reply),
634
+ trace: z
635
+ .object({
636
+ enabled: boolean("trace.enabled", defaults.trace.enabled),
637
+ dir: z
638
+ .unknown()
639
+ .optional()
640
+ .transform((value) => {
641
+ if (value === undefined || value === null) return defaults.trace.dir
642
+ if (typeof value !== "string") {
643
+ warn(`${label("trace.dir")} 必须是字符串:${String(value)}`)
644
+ return defaults.trace.dir
645
+ }
646
+ // 空目录是有意义的:表示与日志同目录。
647
+ return value.trim()
648
+ }),
649
+ maxSizeMb: positive("trace.maxSizeMb", defaults.trace.maxSizeMb),
650
+ retentionDays: integer("trace.retentionDays", defaults.trace.retentionDays, 1),
651
+ })
652
+ .prefault({})
653
+ .catch(defaults.trace),
435
654
  reminder: z
436
655
  .object({
437
656
  enabled: boolean("reminder.enabled", defaults.reminder.enabled),
@@ -457,6 +676,8 @@ function createConfigSchema(
457
676
  maxPerMinute: nonNegative("rateLimit.maxPerMinute", defaults.rateLimit.maxPerMinute),
458
677
  maxPerHour: nonNegative("rateLimit.maxPerHour", defaults.rateLimit.maxPerHour),
459
678
  minGapMs: nonNegative("rateLimit.minGapMs", defaults.rateLimit.minGapMs),
679
+ stall: boolean("rateLimit.stall", defaults.rateLimit.stall),
680
+ stallMaxWaitMs: nonNegative("rateLimit.stallMaxWaitMs", defaults.rateLimit.stallMaxWaitMs),
460
681
  })
461
682
  .prefault({})
462
683
  .catch(defaults.rateLimit),
@@ -466,6 +687,17 @@ function createConfigSchema(
466
687
  })
467
688
  .prefault({})
468
689
  .catch(defaults.sensitive),
690
+ events: z
691
+ .object({
692
+ subscribe: stringList("events.subscribe", defaults.events.subscribe),
693
+ notices: stringList("events.notices", defaults.events.notices),
694
+ inject: boolean("events.inject", defaults.events.inject),
695
+ })
696
+ .prefault({
697
+ subscribe: [...defaults.events.subscribe],
698
+ notices: [...defaults.events.notices],
699
+ })
700
+ .catch(defaults.events),
469
701
  log: z
470
702
  .object({
471
703
  level: logLevel("log.level", defaults.log.level),
@@ -499,8 +731,15 @@ function createConfigSchema(
499
731
  export const ENV_VARS = {
500
732
  "napcat.url": "NAPCAT_WS_URL",
501
733
  "napcat.token": "NAPCAT_WS_TOKEN",
502
- groups: "QQ_TARGET_GROUPS",
503
- users: "QQ_TARGET_USERS",
734
+ "access.allow.groups": "QQ_ALLOW_GROUPS",
735
+ "access.allow.users": "QQ_ALLOW_USERS",
736
+ "access.deny.groups": "QQ_DENY_GROUPS",
737
+ "access.deny.users": "QQ_DENY_USERS",
738
+ "access.allowAllWhenEmpty": "QQ_ALLOW_ALL_WHEN_EMPTY",
739
+ ownerQQ: "QQ_OWNER_QQ",
740
+ admins: "QQ_ADMINS",
741
+ toolProfilesPath: "QQ_TOOL_PROFILES",
742
+ chatOverridesPath: "QQ_CHAT_OVERRIDES",
504
743
  soulPath: "QQ_BOT_SOUL_PATH",
505
744
  soulName: "QQ_BOT_SOUL_NAME",
506
745
  home: "QQ_BOT_HOME",
@@ -514,6 +753,20 @@ export const ENV_VARS = {
514
753
  "proactive.startProbability": "QQ_PROACTIVE_P_START",
515
754
  "proactive.probabilityPerHour": "QQ_PROACTIVE_P_SLOPE_PER_HOUR",
516
755
  "proactive.tickSeconds": "QQ_PROACTIVE_TICK_SECONDS",
756
+ "proactive.allowGroups": "QQ_PROACTIVE_ALLOW_GROUPS",
757
+ "proactive.allowUsers": "QQ_PROACTIVE_ALLOW_USERS",
758
+ "wake.prefixes": "QQ_WAKE_PREFIXES",
759
+ "wake.ignoreAtAll": "QQ_WAKE_IGNORE_AT_ALL",
760
+ "wake.replyToSelf": "QQ_WAKE_REPLY_TO_SELF",
761
+ "wake.ignoreAtOthers": "QQ_WAKE_IGNORE_AT_OTHERS",
762
+ "reply.minDelayMs": "QQ_REPLY_MIN_DELAY_MS",
763
+ "reply.maxDelayMs": "QQ_REPLY_MAX_DELAY_MS",
764
+ "reply.segmentChars": "QQ_REPLY_SEGMENT_CHARS",
765
+ "reply.forwardThresholdChars": "QQ_REPLY_FORWARD_THRESHOLD_CHARS",
766
+ "trace.enabled": "QQ_TRACE_ENABLED",
767
+ "trace.dir": "QQ_TRACE_DIR",
768
+ "trace.maxSizeMb": "QQ_TRACE_MAX_SIZE_MB",
769
+ "trace.retentionDays": "QQ_TRACE_RETENTION_DAYS",
517
770
  "reminder.enabled": "QQ_REMINDER_ENABLED",
518
771
  "reminder.max": "QQ_REMINDER_MAX",
519
772
  "keepAlive.enabled": "QQ_KEEP_ALIVE_ENABLED",
@@ -522,7 +775,12 @@ export const ENV_VARS = {
522
775
  "rateLimit.maxPerMinute": "QQ_RATE_MAX_PER_MINUTE",
523
776
  "rateLimit.maxPerHour": "QQ_RATE_MAX_PER_HOUR",
524
777
  "rateLimit.minGapMs": "QQ_RATE_MIN_GAP_MS",
778
+ "rateLimit.stall": "QQ_RATE_STALL",
779
+ "rateLimit.stallMaxWaitMs": "QQ_RATE_STALL_MAX_WAIT_MS",
525
780
  "sensitive.enabled": "QQ_SENSITIVE_ENABLED",
781
+ "events.subscribe": "QQ_EVENT_SUBSCRIBE",
782
+ "events.notices": "QQ_EVENT_NOTICES",
783
+ "events.inject": "QQ_EVENT_INJECT",
526
784
  "log.level": "QQ_LOG_LEVEL",
527
785
  "log.dir": "QQ_LOG_DIR",
528
786
  "log.maxSize": "QQ_LOG_MAX_SIZE",
@@ -532,8 +790,11 @@ export const ENV_VARS = {
532
790
 
533
791
  const OPTION_KEYS = new Set([
534
792
  "napcat",
535
- "groups",
536
- "users",
793
+ "access",
794
+ "ownerQQ",
795
+ "admins",
796
+ "toolProfilesPath",
797
+ "chatOverridesPath",
537
798
  "soulPath",
538
799
  "soulName",
539
800
  "home",
@@ -543,47 +804,77 @@ const OPTION_KEYS = new Set([
543
804
  "warnLength",
544
805
  "reuseSession",
545
806
  "proactive",
807
+ "wake",
808
+ "reply",
809
+ "trace",
546
810
  "reminder",
547
811
  "keepAlive",
548
812
  "history",
549
813
  "rateLimit",
550
814
  "sensitive",
815
+ "events",
551
816
  "log",
552
817
  "media",
553
818
  ])
554
819
 
555
820
  const NESTED_OPTION_KEYS: Record<string, ReadonlySet<string>> = {
556
821
  napcat: new Set(["url", "token"]),
822
+ access: new Set(["allow", "deny", "allowAllWhenEmpty"]),
823
+ "access.allow": new Set(["groups", "users"]),
824
+ "access.deny": new Set(["groups", "users"]),
557
825
  proactive: new Set([
558
826
  "enabled",
559
827
  "silenceThresholdSeconds",
560
828
  "startProbability",
561
829
  "probabilityPerHour",
562
830
  "tickSeconds",
831
+ "allowGroups",
832
+ "allowUsers",
563
833
  ]),
834
+ wake: new Set(["prefixes", "ignoreAtAll", "replyToSelf", "ignoreAtOthers"]),
835
+ reply: new Set(["minDelayMs", "maxDelayMs", "segmentChars", "forwardThresholdChars"]),
836
+ trace: new Set(["enabled", "dir", "maxSizeMb", "retentionDays"]),
564
837
  reminder: new Set(["enabled", "max"]),
565
838
  keepAlive: new Set(["enabled", "seconds"]),
566
839
  history: new Set(["count"]),
567
- rateLimit: new Set(["maxPerMinute", "maxPerHour", "minGapMs"]),
840
+ rateLimit: new Set(["maxPerMinute", "maxPerHour", "minGapMs", "stall", "stallMaxWaitMs"]),
568
841
  sensitive: new Set(["enabled"]),
842
+ events: new Set(["subscribe", "notices", "inject"]),
569
843
  log: new Set(["level", "dir", "maxSize", "maxFiles"]),
570
844
  media: new Set(["allowedDirs"]),
571
845
  }
572
846
 
573
- /** 报告被 schema 静默剥离的未知配置项。 */
847
+ /** 改过名的配置项:给出迁移提示而不是笼统的“未知配置项”。 */
848
+ const RENAMED_OPTIONS: Record<string, string> = {
849
+ groups: "access.allow.groups",
850
+ users: "access.allow.users",
851
+ }
852
+
853
+ /**
854
+ * 报告被 schema 静默剥离的未知配置项。
855
+ *
856
+ * 按层级逐层检查:`access.allow.groups` 这类嵌套路径也能报出来。
857
+ */
574
858
  function warnUnknownKeys(options: Record<string, unknown>, warn: ConfigWarning): void {
575
- for (const key of Object.keys(options)) {
576
- if (!OPTION_KEYS.has(key)) {
577
- warn(`插件选项 未知配置项:${key}`)
578
- continue
579
- }
580
- const allowed = NESTED_OPTION_KEYS[key]
581
- const value = options[key]
582
- if (!allowed || !value || typeof value !== "object" || Array.isArray(value)) continue
583
- for (const nested of Object.keys(value)) {
584
- if (!allowed.has(nested)) warn(`插件选项 ${key}.${nested} 未知配置项`)
859
+ const walk = (value: Record<string, unknown>, path: string): void => {
860
+ const allowed = path === "" ? OPTION_KEYS : NESTED_OPTION_KEYS[path]
861
+ for (const key of Object.keys(value)) {
862
+ const childPath = path ? `${path}.${key}` : key
863
+ if (allowed && !allowed.has(key)) {
864
+ const renamed = RENAMED_OPTIONS[childPath]
865
+ warn(
866
+ renamed
867
+ ? `${MIGRATION_WARNING_PREFIX} 插件选项 ${childPath} 已移除,请改用 ${renamed}`
868
+ : `插件选项 未知配置项:${childPath}`,
869
+ )
870
+ continue
871
+ }
872
+ const child = value[key]
873
+ if (!child || typeof child !== "object" || Array.isArray(child)) continue
874
+ walk(child as Record<string, unknown>, childPath)
585
875
  }
586
876
  }
877
+ walk(options, "")
587
878
  }
588
879
 
589
880
  function envLabel(path: string): string {
@@ -631,6 +922,32 @@ export function resolveConfig(
631
922
  const optionsSchema = createConfigSchema(base, base.napcat, (path) => `插件选项 ${path}`, warn)
632
923
  const parsed: RawConfig = optionsSchema.parse(options)
633
924
 
925
+ // 只保留认识的事件类别:写错的类别直接告警,不静默失效。
926
+ const subscribe: string[] = []
927
+ for (const category of parsed.events.subscribe) {
928
+ if (!EVENT_CATEGORIES.includes(category)) {
929
+ warn(`events.subscribe 忽略未知类别:${category}`)
930
+ continue
931
+ }
932
+ if (!subscribe.includes(category)) subscribe.push(category)
933
+ }
934
+
935
+ // 旧的名单环境变量已经移除:给出醒目的迁移提示,避免升级后机器人静默失联。
936
+ for (const [name, replacement] of Object.entries(RENAMED_ENV_VARS)) {
937
+ if (env[name] !== undefined) {
938
+ warn(
939
+ `${MIGRATION_WARNING_PREFIX} 环境变量 ${name} 已移除,请改用 ${replacement};未迁移时机器人不会响应任何消息`,
940
+ )
941
+ }
942
+ }
943
+
944
+ // owner 必须能私聊到机器人,否则管理命令无处触发:拒绝名单里的 owner 豁免。
945
+ const denyUsers = [...parsed.access.deny.users]
946
+ if (parsed.ownerQQ !== undefined && denyUsers.includes(parsed.ownerQQ)) {
947
+ warn(`ownerQQ ${parsed.ownerQQ} 在 access.deny.users 里,已豁免:owner 的私聊必须可用`)
948
+ denyUsers.splice(denyUsers.indexOf(parsed.ownerQQ), 1)
949
+ }
950
+
634
951
  // 先输出告警,避免 URL 缺失时把已经发现的问题一起吞掉。
635
952
  for (const warning of warnings) onWarning(warning)
636
953
 
@@ -643,14 +960,36 @@ export function resolveConfig(
643
960
 
644
961
  return {
645
962
  ...parsed,
646
- groups: [...parsed.groups],
647
- users: [...parsed.users],
648
- proactive: { ...parsed.proactive },
963
+ access: {
964
+ allow: {
965
+ groups: [...parsed.access.allow.groups],
966
+ users: [...parsed.access.allow.users],
967
+ },
968
+ deny: {
969
+ groups: [...parsed.access.deny.groups],
970
+ users: denyUsers,
971
+ },
972
+ allowAllWhenEmpty: parsed.access.allowAllWhenEmpty,
973
+ },
974
+ admins: [...parsed.admins],
975
+ proactive: {
976
+ ...parsed.proactive,
977
+ allowGroups: [...parsed.proactive.allowGroups],
978
+ allowUsers: [...parsed.proactive.allowUsers],
979
+ },
980
+ wake: { ...parsed.wake, prefixes: [...parsed.wake.prefixes] },
981
+ reply: { ...parsed.reply },
982
+ trace: { ...parsed.trace },
649
983
  reminder: { ...parsed.reminder },
650
984
  keepAlive: { ...parsed.keepAlive },
651
985
  history: { ...parsed.history },
652
986
  rateLimit: { ...parsed.rateLimit },
653
987
  sensitive: { ...parsed.sensitive },
988
+ events: {
989
+ subscribe,
990
+ notices: [...parsed.events.notices],
991
+ inject: parsed.events.inject,
992
+ },
654
993
  log: { ...parsed.log },
655
994
  media: { allowedDirs: [...parsed.media.allowedDirs] },
656
995
  napcat: { url, token: parsed.napcat.token },