@cmach_socket/opencode-qq-bot 0.8.0 → 0.9.2
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/NOTICE +48 -15
- package/README.md +130 -398
- package/config.example.json +12 -0
- package/package.json +11 -1
- package/src/cleanup.ts +1 -0
- package/src/config/fields.ts +780 -0
- package/src/config/live.ts +189 -0
- package/src/config/registry.ts +654 -0
- package/src/config/store.ts +175 -0
- package/src/config/types.ts +253 -0
- package/src/config.ts +77 -1023
- package/src/diary/scheduler.ts +39 -1
- package/src/emoji/collection.ts +289 -0
- package/src/emoji/favorite-index.ts +40 -3
- package/src/emoji/favorites.ts +7 -1
- package/src/home.ts +11 -16
- package/src/index.ts +117 -25
- package/src/logger.ts +104 -1
- package/src/naplink/client.ts +16 -0
- package/src/panel/api.ts +345 -0
- package/src/panel/auth.ts +65 -0
- package/src/panel/models.ts +56 -0
- package/src/panel/runtime.ts +51 -0
- package/src/panel/server.ts +220 -0
- package/src/panel/status.ts +201 -0
- package/src/rate-limit.ts +46 -0
- package/src/sensitive.ts +6 -1
- package/src/session/manager.ts +4 -3
- package/src/tools/qq.ts +83 -10
- package/web/dist/assets/index-7eMQEM7-.css +1 -0
- package/web/dist/assets/index-DCkgUY7y.js +15 -0
- package/web/dist/assets/inter-cyrillic-ext-wght-normal-BOeWTOD4.woff2 +0 -0
- package/web/dist/assets/inter-cyrillic-wght-normal-DqGufNeO.woff2 +0 -0
- package/web/dist/assets/inter-greek-ext-wght-normal-DlzME5K_.woff2 +0 -0
- package/web/dist/assets/inter-greek-wght-normal-CkhJZR-_.woff2 +0 -0
- package/web/dist/assets/inter-latin-ext-wght-normal-DO1Apj_S.woff2 +0 -0
- package/web/dist/assets/inter-latin-wght-normal-Dx4kXJAl.woff2 +0 -0
- package/web/dist/assets/inter-vietnamese-wght-normal-CBcvBZtf.woff2 +0 -0
- package/web/dist/index.html +13 -0
package/src/config.ts
CHANGED
|
@@ -1,1046 +1,98 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import type {
|
|
4
|
-
import type { LogLevel } from "./logger"
|
|
1
|
+
import { createConfigSchema, EVENT_CATEGORIES, warnUnknownFileKeys } from "./config/registry"
|
|
2
|
+
import type { ConfigWarning } from "./config/registry"
|
|
3
|
+
import type { ParsedConfig, QQBotConfig } from "./config/types"
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
|
-
*
|
|
8
|
-
* and built-in defaults into a single validated object.
|
|
6
|
+
* 插件配置:唯一来源是机器人工作目录下的 `config.json`。
|
|
9
7
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
8
|
+
* 文件里没写的字段走默认值;`opencode.json` 里只有 `agent` 一项会被读,
|
|
9
|
+
* 环境变量不再是配置来源。
|
|
12
10
|
*/
|
|
13
11
|
|
|
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
|
-
maxDelayMs: number
|
|
68
|
-
/** 单条消息超过该字数就拆成多段发送。 */
|
|
69
|
-
segmentChars: number
|
|
70
|
-
/** 单条回复超过该字数就改用合并转发,0 表示关闭。 */
|
|
71
|
-
forwardThresholdChars: number
|
|
72
|
-
/** 是否要求模型先说明理由才能发送:默认开启。 */
|
|
73
|
-
requirePermission: boolean
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/** 调用链追踪配置。 */
|
|
77
|
-
export interface TraceConfig {
|
|
78
|
-
/** 是否记录调用链追踪。 */
|
|
79
|
-
enabled: boolean
|
|
80
|
-
/** 追踪文件目录,空表示与日志同目录。 */
|
|
81
|
-
dir: string
|
|
82
|
-
/** 追踪文件总容量上限(MB)。 */
|
|
83
|
-
maxSizeMb: number
|
|
84
|
-
/** 追踪文件保留天数。 */
|
|
85
|
-
retentionDays: number
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export interface ReminderConfig {
|
|
89
|
-
/** 模型一轮结束时没有调用 qq_send_message 时,是否提醒它补一次。 */
|
|
90
|
-
enabled: boolean
|
|
91
|
-
/** 连续提醒次数上限,避免死循环。 */
|
|
92
|
-
max: number
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export interface KeepAliveConfig {
|
|
96
|
-
/** 是否启用保活心跳。 */
|
|
97
|
-
enabled: boolean
|
|
98
|
-
/** 保活心跳间隔(秒),必须小于 OpenCode 的 location 空闲超时。 */
|
|
99
|
-
seconds: number
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export interface HistoryConfig {
|
|
103
|
-
/** 新建会话时补齐的历史消息条数,0 表示关闭。 */
|
|
104
|
-
count: number
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export interface RateLimitConfig {
|
|
108
|
-
/** 每分钟最多发出多少条消息,0 表示不限制。 */
|
|
109
|
-
maxPerMinute: number
|
|
110
|
-
/** 每小时最多发出多少条消息,0 表示不限制。 */
|
|
111
|
-
maxPerHour: number
|
|
112
|
-
/** 相邻两次发送之间的最小间隔(毫秒),0 表示不限制。 */
|
|
113
|
-
minGapMs: number
|
|
114
|
-
/** 触顶时是否等窗口结束再发;关闭时直接拒绝这次发送。 */
|
|
115
|
-
stall: boolean
|
|
116
|
-
/** 单次等待窗口结束的上限(毫秒)。 */
|
|
117
|
-
stallMaxWaitMs: number
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export interface SensitiveConfig {
|
|
121
|
-
/** 是否拦截含敏感信息(凭证、令牌)的出站消息。 */
|
|
122
|
-
enabled: boolean
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/** 日志配置。 */
|
|
126
|
-
export interface LogConfig {
|
|
127
|
-
/** 最低日志级别。 */
|
|
128
|
-
level: LogLevel
|
|
129
|
-
/** 日志目录,空表示用 XDG 状态目录。 */
|
|
130
|
-
dir: string
|
|
131
|
-
/** 单个日志文件的大小上限。 */
|
|
132
|
-
maxSize: string
|
|
133
|
-
/** 保留的历史日志份数。 */
|
|
134
|
-
maxFiles: number
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/** 事件订阅配置。 */
|
|
138
|
-
export interface EventsConfig {
|
|
139
|
-
/** 订阅哪些事件类别:`message` / `notice`。 */
|
|
140
|
-
subscribe: string[]
|
|
141
|
-
/** notice 类别里具体关心哪些通知;留空表示全收。 */
|
|
142
|
-
notices: string[]
|
|
143
|
-
/** 通知是否注入会话上下文。 */
|
|
144
|
-
inject: boolean
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/** 媒体配置。 */
|
|
148
|
-
export interface MediaConfig {
|
|
149
|
-
/** 额外允许发送媒体的目录。 */
|
|
150
|
-
allowedDirs: string[]
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/** 日记的推送方式。 */
|
|
154
|
-
export type DiaryPush = "owner" | "group" | "none"
|
|
155
|
-
|
|
156
|
-
/** 日记配置。 */
|
|
157
|
-
export interface DiaryConfig {
|
|
158
|
-
/** 日记根目录;空表示用机器人工作目录下的 `diary` 子目录。 */
|
|
159
|
-
path: string
|
|
160
|
-
/** 是否启用日记功能。 */
|
|
161
|
-
enabled: boolean
|
|
162
|
-
/** 聊天素材保留天数;只影响素材,不影响日记成品。 */
|
|
163
|
-
retentionDays: number
|
|
164
|
-
/** 每天生成日记的时刻,格式 `HH:mm`,本地时间。 */
|
|
165
|
-
summaryTime: string
|
|
166
|
-
/** 调度轮询间隔(秒)。 */
|
|
167
|
-
tickSeconds: number
|
|
168
|
-
/** 要生成日记的聊天,`group:123` / `private:456`;空表示按准入名单推导。 */
|
|
169
|
-
summaryTargets: string[]
|
|
170
|
-
/** 日记生成后往哪儿推:`owner` 推给所有者,`group` 推回聊天,`none` 不推。 */
|
|
171
|
-
push: DiaryPush
|
|
172
|
-
/** 是否回补历史聊天记录。 */
|
|
173
|
-
backfill: boolean
|
|
174
|
-
/** 回补时最多取多少条历史消息。 */
|
|
175
|
-
backfillMax: number
|
|
176
|
-
/** 单条素材的最大字符数。 */
|
|
177
|
-
maxLineChars: number
|
|
178
|
-
/** 单个聊天单天最多保留多少条素材。 */
|
|
179
|
-
maxLinesPerDay: number
|
|
180
|
-
/** 等待模型总结的超时时间(毫秒)。 */
|
|
181
|
-
timeoutMs: number
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
export interface ModelRef {
|
|
185
|
-
providerID: string
|
|
186
|
-
id: string
|
|
187
|
-
variant?: string
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
export interface QQBotConfig {
|
|
191
|
-
napcat: NapcatConfig
|
|
192
|
-
/** 准入配置:放行名单、拒绝名单与名单为空时的行为。 */
|
|
193
|
-
access: AccessConfig
|
|
194
|
-
/** 机器人所有者 QQ 号:可用管理命令,并拥有全部工具权限。 */
|
|
195
|
-
ownerQQ?: number
|
|
196
|
-
/** 管理员 QQ 号:拥有全部工具权限。 */
|
|
197
|
-
admins: number[]
|
|
198
|
-
/** 工具配置文件路径;相对路径按机器人工作目录解析,空表示工作目录下的 `tool-profiles.json`。 */
|
|
199
|
-
toolProfilesPath?: string
|
|
200
|
-
/** 会话行为覆盖文件路径;相对路径按机器人工作目录解析,空表示工作目录下的 `chat-overrides.json`。 */
|
|
201
|
-
chatOverridesPath?: string
|
|
202
|
-
/** 角色设定文件路径;相对路径按机器人工作目录解析,默认取工作目录里的 souls/。 */
|
|
203
|
-
soulPath?: string
|
|
204
|
-
/** 工作目录 souls 里的角色设定文件名,默认 default.txt。 */
|
|
205
|
-
soulName?: string
|
|
206
|
-
/** 机器人会话统一使用的工作目录,默认使用 XDG 数据目录下的工作区。 */
|
|
207
|
-
home?: string
|
|
208
|
-
/** QQ 会话使用的 OpenCode agent。 */
|
|
209
|
-
agent?: string
|
|
210
|
-
/** QQ 会话使用的模型。 */
|
|
211
|
-
model?: ModelRef
|
|
212
|
-
/** 拆行发送时每条消息之间的间隔(毫秒)。 */
|
|
213
|
-
sendDelayMs: number
|
|
214
|
-
/** 单次发送的内容超过该长度时告警(不截断)。 */
|
|
215
|
-
warnLength: number
|
|
216
|
-
/** 重启后是否复用上一次的会话。 */
|
|
217
|
-
reuseSession: boolean
|
|
218
|
-
proactive: ProactiveConfig
|
|
219
|
-
wake: WakeConfig
|
|
220
|
-
reply: ReplyConfig
|
|
221
|
-
trace: TraceConfig
|
|
222
|
-
reminder: ReminderConfig
|
|
223
|
-
keepAlive: KeepAliveConfig
|
|
224
|
-
history: HistoryConfig
|
|
225
|
-
rateLimit: RateLimitConfig
|
|
226
|
-
sensitive: SensitiveConfig
|
|
227
|
-
events: EventsConfig
|
|
228
|
-
log: LogConfig
|
|
229
|
-
media: MediaConfig
|
|
230
|
-
diary: DiaryConfig
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/** 除 NapCat 连接信息外的全部默认值。 */
|
|
234
|
-
export const DEFAULT_CONFIG: Omit<QQBotConfig, "napcat"> = {
|
|
235
|
-
access: {
|
|
236
|
-
allow: { groups: [], users: [] },
|
|
237
|
-
deny: { groups: [], users: [] },
|
|
238
|
-
allowAllWhenEmpty: false,
|
|
239
|
-
},
|
|
240
|
-
admins: [],
|
|
241
|
-
toolProfilesPath: "",
|
|
242
|
-
chatOverridesPath: "",
|
|
243
|
-
sendDelayMs: 1000,
|
|
244
|
-
warnLength: 500,
|
|
245
|
-
reuseSession: false,
|
|
246
|
-
proactive: {
|
|
247
|
-
enabled: true,
|
|
248
|
-
silenceThresholdSeconds: 3 * 3600,
|
|
249
|
-
startProbability: 0.01,
|
|
250
|
-
probabilityPerHour: 0.05,
|
|
251
|
-
tickSeconds: 3600,
|
|
252
|
-
allowGroups: [],
|
|
253
|
-
allowUsers: [],
|
|
254
|
-
},
|
|
255
|
-
wake: { prefixes: ["/"], ignoreAtAll: true, replyToSelf: true, ignoreAtOthers: true },
|
|
256
|
-
reply: {
|
|
257
|
-
minDelayMs: 0,
|
|
258
|
-
maxDelayMs: 0,
|
|
259
|
-
segmentChars: 150,
|
|
260
|
-
forwardThresholdChars: 1500,
|
|
261
|
-
requirePermission: true,
|
|
262
|
-
},
|
|
263
|
-
trace: { enabled: true, dir: "", maxSizeMb: 80, retentionDays: 7 },
|
|
264
|
-
reminder: { enabled: true, max: 2 },
|
|
265
|
-
keepAlive: { enabled: true, seconds: 20 * 60 },
|
|
266
|
-
history: { count: 20 },
|
|
267
|
-
rateLimit: {
|
|
268
|
-
maxPerMinute: 8,
|
|
269
|
-
maxPerHour: 60,
|
|
270
|
-
minGapMs: 1500,
|
|
271
|
-
stall: false,
|
|
272
|
-
stallMaxWaitMs: 60_000,
|
|
273
|
-
},
|
|
274
|
-
sensitive: { enabled: true },
|
|
275
|
-
events: { subscribe: ["message"], notices: [], inject: true },
|
|
276
|
-
log: { level: "info", dir: "", maxSize: "5m", maxFiles: 5 },
|
|
277
|
-
media: { allowedDirs: [] },
|
|
278
|
-
diary: {
|
|
279
|
-
path: "",
|
|
280
|
-
enabled: true,
|
|
281
|
-
retentionDays: 30,
|
|
282
|
-
summaryTime: "23:30",
|
|
283
|
-
tickSeconds: 3600,
|
|
284
|
-
summaryTargets: [],
|
|
285
|
-
push: "owner",
|
|
286
|
-
backfill: false,
|
|
287
|
-
backfillMax: 1000,
|
|
288
|
-
maxLineChars: 200,
|
|
289
|
-
maxLinesPerDay: 2000,
|
|
290
|
-
timeoutMs: 120_000,
|
|
291
|
-
},
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
/** 迁移类告警前缀:启动时用更高等级输出,避免升级用户静默失联。 */
|
|
295
|
-
export const MIGRATION_WARNING_PREFIX = "【迁移】"
|
|
296
|
-
|
|
297
|
-
/** 移除的环境变量与替代项。 */
|
|
298
|
-
const RENAMED_ENV_VARS: Record<string, string> = {
|
|
299
|
-
QQ_TARGET_GROUPS: "QQ_ALLOW_GROUPS",
|
|
300
|
-
QQ_TARGET_USERS: "QQ_ALLOW_USERS",
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
/** 配置告警回调。 */
|
|
304
|
-
export type ConfigWarning = (message: string) => void
|
|
305
|
-
|
|
306
|
-
/** 解析 "provider/model" 或 "provider/model/variant",复用 SDK 的模型引用解析。 */
|
|
307
|
-
export function parseModelRef(value: unknown): ModelRef | undefined {
|
|
308
|
-
const raw = typeof value === "string" ? value.trim() : ""
|
|
309
|
-
if (!raw) return undefined
|
|
310
|
-
const [providerID, id, variant, ...rest] = raw.split("/").filter(Boolean)
|
|
311
|
-
if (!providerID || !id || rest.length > 0) return undefined
|
|
312
|
-
try {
|
|
313
|
-
const ref = Model.Ref.parse(`${providerID}/${id}${variant ? `#${variant}` : ""}`)
|
|
314
|
-
return {
|
|
315
|
-
providerID: ref.providerID,
|
|
316
|
-
id: ref.id,
|
|
317
|
-
...(ref.variant ? { variant: ref.variant } : {}),
|
|
318
|
-
}
|
|
319
|
-
} catch {
|
|
320
|
-
return undefined
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
/** 判断值是否算“已提供”,用于区分空值与写错的值。 */
|
|
325
|
-
function present(value: unknown): boolean {
|
|
326
|
-
return value !== undefined && value !== null && value !== ""
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
/** 把「数组或逗号分隔字符串」统一成数组;其它类型返回 undefined。 */
|
|
330
|
-
function toSource(value: unknown): unknown[] | undefined {
|
|
331
|
-
if (Array.isArray(value)) return value
|
|
332
|
-
return typeof value === "string" ? value.split(",") : undefined
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
/** 把数字数组或逗号分隔字符串整理成去重后的正整数列表。 */
|
|
336
|
-
function toIds(
|
|
337
|
-
value: unknown,
|
|
338
|
-
warn: ConfigWarning,
|
|
339
|
-
label: string,
|
|
340
|
-
noun: string,
|
|
341
|
-
): number[] | undefined {
|
|
342
|
-
const source = toSource(value)
|
|
343
|
-
if (!source) return undefined
|
|
344
|
-
const ids: number[] = []
|
|
345
|
-
const seen = new Set<number>()
|
|
346
|
-
for (const entry of source) {
|
|
347
|
-
const raw =
|
|
348
|
-
typeof entry === "number" ? String(entry) : typeof entry === "string" ? entry.trim() : ""
|
|
349
|
-
if (!raw) continue
|
|
350
|
-
const parsed = Number(raw)
|
|
351
|
-
if (!Number.isInteger(parsed) || parsed <= 0) {
|
|
352
|
-
warn(`${label} 忽略非法${noun}:${raw}`)
|
|
12
|
+
export type {
|
|
13
|
+
AccessConfig,
|
|
14
|
+
DiaryConfig,
|
|
15
|
+
DiaryPush,
|
|
16
|
+
EventsConfig,
|
|
17
|
+
HistoryConfig,
|
|
18
|
+
KeepAliveConfig,
|
|
19
|
+
LogConfig,
|
|
20
|
+
LogLevel,
|
|
21
|
+
MediaConfig,
|
|
22
|
+
ModelRef,
|
|
23
|
+
NapcatConfig,
|
|
24
|
+
PanelConfig,
|
|
25
|
+
ParsedConfig,
|
|
26
|
+
ProactiveConfig,
|
|
27
|
+
QQBotConfig,
|
|
28
|
+
RateLimitConfig,
|
|
29
|
+
RawConfig,
|
|
30
|
+
ReminderConfig,
|
|
31
|
+
ReplyConfig,
|
|
32
|
+
SensitiveConfig,
|
|
33
|
+
TraceConfig,
|
|
34
|
+
WakeConfig,
|
|
35
|
+
} from "./config/types"
|
|
36
|
+
|
|
37
|
+
export {
|
|
38
|
+
CONFIG_FIELDS,
|
|
39
|
+
CONFIG_GROUPS,
|
|
40
|
+
type ConfigField,
|
|
41
|
+
type ConfigGroup,
|
|
42
|
+
type FieldType,
|
|
43
|
+
} from "./config/fields"
|
|
44
|
+
|
|
45
|
+
export {
|
|
46
|
+
applyHotFields,
|
|
47
|
+
configJsonSchema,
|
|
48
|
+
createConfigSchema,
|
|
49
|
+
createStrictFieldSchema,
|
|
50
|
+
DEFAULT_CONFIG,
|
|
51
|
+
FILE_KEYS,
|
|
52
|
+
getConfigValue,
|
|
53
|
+
HOT_FIELDS,
|
|
54
|
+
isSecretField,
|
|
55
|
+
parseModelRef,
|
|
56
|
+
type ConfigWarning,
|
|
57
|
+
} from "./config/registry"
|
|
58
|
+
|
|
59
|
+
/** 只保留认识的事件类别:写错的类别直接告警,不静默失效。 */
|
|
60
|
+
function filterEventCategories(categories: readonly string[], warn: ConfigWarning): string[] {
|
|
61
|
+
const subscribe: string[] = []
|
|
62
|
+
for (const category of categories) {
|
|
63
|
+
if (!EVENT_CATEGORIES.includes(category)) {
|
|
64
|
+
warn(`events.subscribe 忽略未知类别:${category}`)
|
|
353
65
|
continue
|
|
354
66
|
}
|
|
355
|
-
if (
|
|
356
|
-
seen.add(parsed)
|
|
357
|
-
ids.push(parsed)
|
|
358
|
-
}
|
|
359
|
-
return ids
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
interface PartialNapcatConfig {
|
|
363
|
-
url?: string
|
|
364
|
-
token?: string
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
type RawConfig = Omit<QQBotConfig, "napcat"> & { napcat: PartialNapcatConfig }
|
|
368
|
-
|
|
369
|
-
/**
|
|
370
|
-
* 用同一份字段清单生成配置 schema。
|
|
371
|
-
*
|
|
372
|
-
* `defaults` 是被覆盖层的兜底值,因此环境变量层用 `DEFAULT_CONFIG`,
|
|
373
|
-
* 插件选项层用解析后的环境变量配置,天然实现“选项 > 环境变量 > 默认值”。
|
|
374
|
-
*/
|
|
375
|
-
function createConfigSchema(
|
|
376
|
-
defaults: Omit<QQBotConfig, "napcat">,
|
|
377
|
-
napcat: PartialNapcatConfig,
|
|
378
|
-
label: (path: string) => string,
|
|
379
|
-
warn: ConfigWarning,
|
|
380
|
-
): z.ZodType<RawConfig> {
|
|
381
|
-
const number = (path: string, fallback: number) =>
|
|
382
|
-
z
|
|
383
|
-
.union([z.number(), z.string().trim().regex(NUMBER).transform(Number)])
|
|
384
|
-
.pipe(z.number().finite())
|
|
385
|
-
.catch((ctx) => {
|
|
386
|
-
if (present(ctx.value)) warn(`${label(path)} 不是合法的数字:${String(ctx.value)}`)
|
|
387
|
-
return fallback
|
|
388
|
-
})
|
|
389
|
-
.default(fallback)
|
|
390
|
-
|
|
391
|
-
const constrained = (
|
|
392
|
-
path: string,
|
|
393
|
-
fallback: number,
|
|
394
|
-
check: (value: z.ZodNumber) => z.ZodNumber,
|
|
395
|
-
rule: string,
|
|
396
|
-
) =>
|
|
397
|
-
number(path, fallback)
|
|
398
|
-
.pipe(check(z.number()))
|
|
399
|
-
.catch((ctx) => {
|
|
400
|
-
if (present(ctx.value)) {
|
|
401
|
-
warn(`${label(path)} ${rule},当前为 ${String(ctx.value)},已回退到 ${fallback}`)
|
|
402
|
-
}
|
|
403
|
-
return fallback
|
|
404
|
-
})
|
|
405
|
-
|
|
406
|
-
const positive = (path: string, fallback: number) =>
|
|
407
|
-
constrained(path, fallback, (value) => value.positive(), "必须大于 0")
|
|
408
|
-
|
|
409
|
-
const nonNegative = (path: string, fallback: number) =>
|
|
410
|
-
constrained(path, fallback, (value) => value.min(0), "不能小于 0")
|
|
411
|
-
|
|
412
|
-
const integer = (path: string, fallback: number, min: number) =>
|
|
413
|
-
constrained(path, fallback, (value) => value.int().min(min), `必须是大于等于 ${min} 的整数`)
|
|
414
|
-
|
|
415
|
-
const probability = (path: string, fallback: number) =>
|
|
416
|
-
constrained(path, fallback, (value) => value.min(0).max(1), "必须在 0 到 1 之间")
|
|
417
|
-
|
|
418
|
-
const boolean = (path: string, fallback: boolean) =>
|
|
419
|
-
z
|
|
420
|
-
.union([z.boolean(), z.string().trim().toLowerCase().pipe(z.enum(BOOLEAN_SPELLINGS))])
|
|
421
|
-
.transform((value) => (typeof value === "boolean" ? value : TRUTHY.has(value)))
|
|
422
|
-
.catch((ctx) => {
|
|
423
|
-
if (present(ctx.value)) warn(`${label(path)} 不是合法的布尔值:${String(ctx.value)}`)
|
|
424
|
-
return fallback
|
|
425
|
-
})
|
|
426
|
-
.default(fallback)
|
|
427
|
-
|
|
428
|
-
/**
|
|
429
|
-
* 字符串:空值或非法值都回退到默认值。
|
|
430
|
-
*
|
|
431
|
-
* 返回类型跟随 fallback:给 `undefined` 表示可以不配置(如 napcat.url),
|
|
432
|
-
* 给实串就是必填(如 log.maxSize)。
|
|
433
|
-
*/
|
|
434
|
-
const text = <T extends string | undefined>(path: string, fallback: T) =>
|
|
435
|
-
z
|
|
436
|
-
.unknown()
|
|
437
|
-
.optional()
|
|
438
|
-
.transform((value) => {
|
|
439
|
-
if (!present(value)) return fallback
|
|
440
|
-
if (typeof value !== "string") {
|
|
441
|
-
warn(`${label(path)} 必须是字符串:${String(value)}`)
|
|
442
|
-
return fallback
|
|
443
|
-
}
|
|
444
|
-
const trimmed = value.trim()
|
|
445
|
-
if (!trimmed) {
|
|
446
|
-
warn(`${label(path)} 不能为空,已回退到默认值`)
|
|
447
|
-
return fallback
|
|
448
|
-
}
|
|
449
|
-
return trimmed
|
|
450
|
-
})
|
|
451
|
-
|
|
452
|
-
/** 日志级别:只认 debug/info/warn/error,写错就回退到默认值。 */
|
|
453
|
-
const logLevel = (path: string, fallback: LogLevel) =>
|
|
454
|
-
z
|
|
455
|
-
.unknown()
|
|
456
|
-
.optional()
|
|
457
|
-
.transform((value) => {
|
|
458
|
-
if (!present(value)) return fallback
|
|
459
|
-
const raw = typeof value === "string" ? value.trim().toLowerCase() : ""
|
|
460
|
-
if (!raw) {
|
|
461
|
-
warn(`${label(path)} 不能为空,已回退到默认值`)
|
|
462
|
-
return fallback
|
|
463
|
-
}
|
|
464
|
-
if (!LOG_LEVELS.includes(raw)) {
|
|
465
|
-
warn(
|
|
466
|
-
`${label(path)} 不是合法的日志级别:${String(value)}(可用:${LOG_LEVELS.join("/")})`,
|
|
467
|
-
)
|
|
468
|
-
return fallback
|
|
469
|
-
}
|
|
470
|
-
return raw as LogLevel
|
|
471
|
-
})
|
|
472
|
-
|
|
473
|
-
/** 编号名单:数组或逗号分隔字符串。 */
|
|
474
|
-
const idList = (path: string, fallback: readonly number[], noun: string) =>
|
|
475
|
-
z
|
|
476
|
-
.unknown()
|
|
477
|
-
.optional()
|
|
478
|
-
.transform((value) => {
|
|
479
|
-
if (value === undefined || value === null) return [...fallback]
|
|
480
|
-
const parsed = toIds(value, warn, label(path), noun)
|
|
481
|
-
if (!parsed) {
|
|
482
|
-
if (present(value)) warn(`${label(path)} 解析不出${noun}:${String(value)}`)
|
|
483
|
-
return [...fallback]
|
|
484
|
-
}
|
|
485
|
-
return parsed
|
|
486
|
-
})
|
|
487
|
-
|
|
488
|
-
const admins = idList("admins", defaults.admins, "QQ 号")
|
|
489
|
-
|
|
490
|
-
const ownerQQ = z
|
|
491
|
-
.unknown()
|
|
492
|
-
.optional()
|
|
493
|
-
.transform((value) => {
|
|
494
|
-
if (!present(value)) return defaults.ownerQQ
|
|
495
|
-
// 只接受数字或非空数字字符串:布尔值经 Number() 会变成 0/1。
|
|
496
|
-
const parsed =
|
|
497
|
-
typeof value === "number"
|
|
498
|
-
? value
|
|
499
|
-
: typeof value === "string" && value.trim() !== ""
|
|
500
|
-
? Number(value.trim())
|
|
501
|
-
: Number.NaN
|
|
502
|
-
if (!Number.isInteger(parsed) || parsed <= 0) {
|
|
503
|
-
warn(`${label("ownerQQ")} 不是合法的 QQ 号:${String(value)}`)
|
|
504
|
-
return defaults.ownerQQ
|
|
505
|
-
}
|
|
506
|
-
return parsed
|
|
507
|
-
})
|
|
508
|
-
|
|
509
|
-
const model = z
|
|
510
|
-
.unknown()
|
|
511
|
-
.optional()
|
|
512
|
-
.transform((value) => {
|
|
513
|
-
if (!present(value)) return defaults.model
|
|
514
|
-
const parsed = parseModelRef(value)
|
|
515
|
-
if (!parsed) {
|
|
516
|
-
warn(`${label("model")} 格式无效:${String(value)}(应为 provider/model[/variant])`)
|
|
517
|
-
return defaults.model
|
|
518
|
-
}
|
|
519
|
-
return parsed
|
|
520
|
-
})
|
|
521
|
-
|
|
522
|
-
/** 字符串列表:数组或逗号分隔字符串。 */
|
|
523
|
-
const stringList = (path: string, fallback: readonly string[]) =>
|
|
524
|
-
z
|
|
525
|
-
.unknown()
|
|
526
|
-
.optional()
|
|
527
|
-
.transform((value) => {
|
|
528
|
-
if (value === undefined || value === null) return [...fallback]
|
|
529
|
-
const source = toSource(value)
|
|
530
|
-
if (!source) {
|
|
531
|
-
if (present(value)) {
|
|
532
|
-
warn(`${label(path)} 必须是字符串或字符串数组:${String(value)}`)
|
|
533
|
-
}
|
|
534
|
-
return [...fallback]
|
|
535
|
-
}
|
|
536
|
-
const items: string[] = []
|
|
537
|
-
for (const entry of source) {
|
|
538
|
-
const raw = typeof entry === "string" ? entry.trim() : ""
|
|
539
|
-
if (!raw) continue
|
|
540
|
-
if (items.includes(raw)) continue
|
|
541
|
-
items.push(raw)
|
|
542
|
-
}
|
|
543
|
-
return items
|
|
544
|
-
})
|
|
545
|
-
|
|
546
|
-
/** 允许发送媒体的目录。 */
|
|
547
|
-
const mediaDirs = stringList("media.allowedDirs", defaults.media.allowedDirs)
|
|
548
|
-
|
|
549
|
-
/** 可选目录:空串有意义(由调用方解释为默认位置),非字符串回退到默认值。 */
|
|
550
|
-
const optionalDir = (path: string, fallback: string) =>
|
|
551
|
-
z
|
|
552
|
-
.unknown()
|
|
553
|
-
.optional()
|
|
554
|
-
.transform((value) => {
|
|
555
|
-
if (value === undefined || value === null) return fallback
|
|
556
|
-
if (typeof value !== "string") {
|
|
557
|
-
warn(`${label(path)} 必须是字符串:${String(value)}`)
|
|
558
|
-
return fallback
|
|
559
|
-
}
|
|
560
|
-
return value.trim()
|
|
561
|
-
})
|
|
562
|
-
|
|
563
|
-
/** 时刻:`HH:mm`。 */
|
|
564
|
-
const clock = (path: string, fallback: string) =>
|
|
565
|
-
z
|
|
566
|
-
.unknown()
|
|
567
|
-
.optional()
|
|
568
|
-
.transform((value) => {
|
|
569
|
-
if (!present(value)) return fallback
|
|
570
|
-
if (typeof value !== "string") {
|
|
571
|
-
warn(`${label(path)} 必须是字符串:${String(value)}`)
|
|
572
|
-
return fallback
|
|
573
|
-
}
|
|
574
|
-
const trimmed = value.trim()
|
|
575
|
-
if (!CLOCK.test(trimmed)) {
|
|
576
|
-
warn(`${label(path)} 不是合法的时刻:${trimmed}(应为 HH:mm)`)
|
|
577
|
-
return fallback
|
|
578
|
-
}
|
|
579
|
-
return trimmed
|
|
580
|
-
})
|
|
581
|
-
|
|
582
|
-
/** 日记推送方式。 */
|
|
583
|
-
const diaryPush = (path: string, fallback: DiaryPush) =>
|
|
584
|
-
z
|
|
585
|
-
.unknown()
|
|
586
|
-
.optional()
|
|
587
|
-
.transform((value) => {
|
|
588
|
-
if (!present(value)) return fallback
|
|
589
|
-
const raw = typeof value === "string" ? value.trim().toLowerCase() : ""
|
|
590
|
-
if (!DIARY_PUSH_MODES.includes(raw as DiaryPush)) {
|
|
591
|
-
warn(
|
|
592
|
-
`${label(path)} 不是合法的推送方式:${String(value)}(可用:${DIARY_PUSH_MODES.join("/")})`,
|
|
593
|
-
)
|
|
594
|
-
return fallback
|
|
595
|
-
}
|
|
596
|
-
return raw as DiaryPush
|
|
597
|
-
})
|
|
598
|
-
|
|
599
|
-
return z.object({
|
|
600
|
-
napcat: z
|
|
601
|
-
.object({
|
|
602
|
-
url: text("napcat.url", napcat.url),
|
|
603
|
-
token: text("napcat.token", napcat.token),
|
|
604
|
-
})
|
|
605
|
-
.prefault({})
|
|
606
|
-
.catch({ url: napcat.url, token: napcat.token }),
|
|
607
|
-
access: z
|
|
608
|
-
.object({
|
|
609
|
-
allow: z
|
|
610
|
-
.object({
|
|
611
|
-
groups: idList("access.allow.groups", defaults.access.allow.groups, "群号").default([
|
|
612
|
-
...defaults.access.allow.groups,
|
|
613
|
-
]),
|
|
614
|
-
users: idList("access.allow.users", defaults.access.allow.users, "QQ 号").default([
|
|
615
|
-
...defaults.access.allow.users,
|
|
616
|
-
]),
|
|
617
|
-
})
|
|
618
|
-
.prefault({})
|
|
619
|
-
.catch({
|
|
620
|
-
groups: [...defaults.access.allow.groups],
|
|
621
|
-
users: [...defaults.access.allow.users],
|
|
622
|
-
}),
|
|
623
|
-
deny: z
|
|
624
|
-
.object({
|
|
625
|
-
groups: idList("access.deny.groups", defaults.access.deny.groups, "群号").default([
|
|
626
|
-
...defaults.access.deny.groups,
|
|
627
|
-
]),
|
|
628
|
-
users: idList("access.deny.users", defaults.access.deny.users, "QQ 号").default([
|
|
629
|
-
...defaults.access.deny.users,
|
|
630
|
-
]),
|
|
631
|
-
})
|
|
632
|
-
.prefault({})
|
|
633
|
-
.catch({
|
|
634
|
-
groups: [...defaults.access.deny.groups],
|
|
635
|
-
users: [...defaults.access.deny.users],
|
|
636
|
-
}),
|
|
637
|
-
allowAllWhenEmpty: boolean("access.allowAllWhenEmpty", defaults.access.allowAllWhenEmpty),
|
|
638
|
-
})
|
|
639
|
-
.prefault({})
|
|
640
|
-
.catch(defaults.access),
|
|
641
|
-
admins,
|
|
642
|
-
ownerQQ,
|
|
643
|
-
toolProfilesPath: text("toolProfilesPath", defaults.toolProfilesPath),
|
|
644
|
-
chatOverridesPath: text("chatOverridesPath", defaults.chatOverridesPath),
|
|
645
|
-
soulPath: text("soulPath", defaults.soulPath),
|
|
646
|
-
soulName: text("soulName", defaults.soulName),
|
|
647
|
-
home: text("home", defaults.home),
|
|
648
|
-
agent: text("agent", defaults.agent),
|
|
649
|
-
model,
|
|
650
|
-
sendDelayMs: nonNegative("sendDelayMs", defaults.sendDelayMs),
|
|
651
|
-
warnLength: integer("warnLength", defaults.warnLength, 1),
|
|
652
|
-
reuseSession: boolean("reuseSession", defaults.reuseSession),
|
|
653
|
-
proactive: z
|
|
654
|
-
.object({
|
|
655
|
-
enabled: boolean("proactive.enabled", defaults.proactive.enabled),
|
|
656
|
-
silenceThresholdSeconds: nonNegative(
|
|
657
|
-
"proactive.silenceThresholdSeconds",
|
|
658
|
-
defaults.proactive.silenceThresholdSeconds,
|
|
659
|
-
),
|
|
660
|
-
startProbability: probability(
|
|
661
|
-
"proactive.startProbability",
|
|
662
|
-
defaults.proactive.startProbability,
|
|
663
|
-
),
|
|
664
|
-
probabilityPerHour: probability(
|
|
665
|
-
"proactive.probabilityPerHour",
|
|
666
|
-
defaults.proactive.probabilityPerHour,
|
|
667
|
-
),
|
|
668
|
-
tickSeconds: positive("proactive.tickSeconds", defaults.proactive.tickSeconds),
|
|
669
|
-
allowGroups: idList(
|
|
670
|
-
"proactive.allowGroups",
|
|
671
|
-
defaults.proactive.allowGroups,
|
|
672
|
-
"群号",
|
|
673
|
-
).default([...defaults.proactive.allowGroups]),
|
|
674
|
-
allowUsers: idList("proactive.allowUsers", defaults.proactive.allowUsers, "QQ 号").default([
|
|
675
|
-
...defaults.proactive.allowUsers,
|
|
676
|
-
]),
|
|
677
|
-
})
|
|
678
|
-
.prefault({})
|
|
679
|
-
.catch(defaults.proactive),
|
|
680
|
-
wake: z
|
|
681
|
-
.object({
|
|
682
|
-
prefixes: stringList("wake.prefixes", defaults.wake.prefixes).default([
|
|
683
|
-
...defaults.wake.prefixes,
|
|
684
|
-
]),
|
|
685
|
-
ignoreAtAll: boolean("wake.ignoreAtAll", defaults.wake.ignoreAtAll),
|
|
686
|
-
replyToSelf: boolean("wake.replyToSelf", defaults.wake.replyToSelf),
|
|
687
|
-
ignoreAtOthers: boolean("wake.ignoreAtOthers", defaults.wake.ignoreAtOthers),
|
|
688
|
-
})
|
|
689
|
-
.prefault({})
|
|
690
|
-
.catch(defaults.wake),
|
|
691
|
-
reply: z
|
|
692
|
-
.object({
|
|
693
|
-
minDelayMs: nonNegative("reply.minDelayMs", defaults.reply.minDelayMs),
|
|
694
|
-
maxDelayMs: nonNegative("reply.maxDelayMs", defaults.reply.maxDelayMs),
|
|
695
|
-
segmentChars: integer("reply.segmentChars", defaults.reply.segmentChars, 1),
|
|
696
|
-
forwardThresholdChars: nonNegative(
|
|
697
|
-
"reply.forwardThresholdChars",
|
|
698
|
-
defaults.reply.forwardThresholdChars,
|
|
699
|
-
),
|
|
700
|
-
requirePermission: boolean("reply.requirePermission", defaults.reply.requirePermission),
|
|
701
|
-
})
|
|
702
|
-
.prefault({})
|
|
703
|
-
.catch(defaults.reply),
|
|
704
|
-
trace: z
|
|
705
|
-
.object({
|
|
706
|
-
enabled: boolean("trace.enabled", defaults.trace.enabled),
|
|
707
|
-
dir: optionalDir("trace.dir", defaults.trace.dir),
|
|
708
|
-
maxSizeMb: positive("trace.maxSizeMb", defaults.trace.maxSizeMb),
|
|
709
|
-
retentionDays: integer("trace.retentionDays", defaults.trace.retentionDays, 1),
|
|
710
|
-
})
|
|
711
|
-
.prefault({})
|
|
712
|
-
.catch(defaults.trace),
|
|
713
|
-
reminder: z
|
|
714
|
-
.object({
|
|
715
|
-
enabled: boolean("reminder.enabled", defaults.reminder.enabled),
|
|
716
|
-
max: integer("reminder.max", defaults.reminder.max, 0),
|
|
717
|
-
})
|
|
718
|
-
.prefault({})
|
|
719
|
-
.catch(defaults.reminder),
|
|
720
|
-
keepAlive: z
|
|
721
|
-
.object({
|
|
722
|
-
enabled: boolean("keepAlive.enabled", defaults.keepAlive.enabled),
|
|
723
|
-
seconds: positive("keepAlive.seconds", defaults.keepAlive.seconds),
|
|
724
|
-
})
|
|
725
|
-
.prefault({})
|
|
726
|
-
.catch(defaults.keepAlive),
|
|
727
|
-
history: z
|
|
728
|
-
.object({
|
|
729
|
-
count: nonNegative("history.count", defaults.history.count),
|
|
730
|
-
})
|
|
731
|
-
.prefault({})
|
|
732
|
-
.catch(defaults.history),
|
|
733
|
-
rateLimit: z
|
|
734
|
-
.object({
|
|
735
|
-
maxPerMinute: nonNegative("rateLimit.maxPerMinute", defaults.rateLimit.maxPerMinute),
|
|
736
|
-
maxPerHour: nonNegative("rateLimit.maxPerHour", defaults.rateLimit.maxPerHour),
|
|
737
|
-
minGapMs: nonNegative("rateLimit.minGapMs", defaults.rateLimit.minGapMs),
|
|
738
|
-
stall: boolean("rateLimit.stall", defaults.rateLimit.stall),
|
|
739
|
-
stallMaxWaitMs: nonNegative("rateLimit.stallMaxWaitMs", defaults.rateLimit.stallMaxWaitMs),
|
|
740
|
-
})
|
|
741
|
-
.prefault({})
|
|
742
|
-
.catch(defaults.rateLimit),
|
|
743
|
-
sensitive: z
|
|
744
|
-
.object({
|
|
745
|
-
enabled: boolean("sensitive.enabled", defaults.sensitive.enabled),
|
|
746
|
-
})
|
|
747
|
-
.prefault({})
|
|
748
|
-
.catch(defaults.sensitive),
|
|
749
|
-
events: z
|
|
750
|
-
.object({
|
|
751
|
-
subscribe: stringList("events.subscribe", defaults.events.subscribe),
|
|
752
|
-
notices: stringList("events.notices", defaults.events.notices),
|
|
753
|
-
inject: boolean("events.inject", defaults.events.inject),
|
|
754
|
-
})
|
|
755
|
-
.prefault({
|
|
756
|
-
subscribe: [...defaults.events.subscribe],
|
|
757
|
-
notices: [...defaults.events.notices],
|
|
758
|
-
})
|
|
759
|
-
.catch(defaults.events),
|
|
760
|
-
log: z
|
|
761
|
-
.object({
|
|
762
|
-
level: logLevel("log.level", defaults.log.level),
|
|
763
|
-
dir: optionalDir("log.dir", defaults.log.dir),
|
|
764
|
-
maxSize: text("log.maxSize", defaults.log.maxSize),
|
|
765
|
-
maxFiles: integer("log.maxFiles", defaults.log.maxFiles, 1),
|
|
766
|
-
})
|
|
767
|
-
.prefault({})
|
|
768
|
-
.catch(defaults.log),
|
|
769
|
-
media: z
|
|
770
|
-
.object({
|
|
771
|
-
allowedDirs: mediaDirs,
|
|
772
|
-
})
|
|
773
|
-
.prefault({})
|
|
774
|
-
.catch(defaults.media),
|
|
775
|
-
diary: z
|
|
776
|
-
.object({
|
|
777
|
-
path: optionalDir("diary.path", defaults.diary.path),
|
|
778
|
-
enabled: boolean("diary.enabled", defaults.diary.enabled),
|
|
779
|
-
retentionDays: integer("diary.retentionDays", defaults.diary.retentionDays, 1),
|
|
780
|
-
summaryTime: clock("diary.summaryTime", defaults.diary.summaryTime),
|
|
781
|
-
tickSeconds: positive("diary.tickSeconds", defaults.diary.tickSeconds),
|
|
782
|
-
summaryTargets: stringList("diary.summaryTargets", defaults.diary.summaryTargets).default([
|
|
783
|
-
...defaults.diary.summaryTargets,
|
|
784
|
-
]),
|
|
785
|
-
push: diaryPush("diary.push", defaults.diary.push),
|
|
786
|
-
backfill: boolean("diary.backfill", defaults.diary.backfill),
|
|
787
|
-
backfillMax: positive("diary.backfillMax", defaults.diary.backfillMax),
|
|
788
|
-
maxLineChars: positive("diary.maxLineChars", defaults.diary.maxLineChars),
|
|
789
|
-
maxLinesPerDay: positive("diary.maxLinesPerDay", defaults.diary.maxLinesPerDay),
|
|
790
|
-
timeoutMs: positive("diary.timeoutMs", defaults.diary.timeoutMs),
|
|
791
|
-
})
|
|
792
|
-
.prefault({})
|
|
793
|
-
.catch(defaults.diary),
|
|
794
|
-
})
|
|
795
|
-
}
|
|
796
|
-
|
|
797
|
-
/** 配置项到环境变量名的映射。 */
|
|
798
|
-
export const ENV_VARS = {
|
|
799
|
-
"napcat.url": "NAPCAT_WS_URL",
|
|
800
|
-
"napcat.token": "NAPCAT_WS_TOKEN",
|
|
801
|
-
"access.allow.groups": "QQ_ALLOW_GROUPS",
|
|
802
|
-
"access.allow.users": "QQ_ALLOW_USERS",
|
|
803
|
-
"access.deny.groups": "QQ_DENY_GROUPS",
|
|
804
|
-
"access.deny.users": "QQ_DENY_USERS",
|
|
805
|
-
"access.allowAllWhenEmpty": "QQ_ALLOW_ALL_WHEN_EMPTY",
|
|
806
|
-
ownerQQ: "QQ_OWNER_QQ",
|
|
807
|
-
admins: "QQ_ADMINS",
|
|
808
|
-
toolProfilesPath: "QQ_TOOL_PROFILES",
|
|
809
|
-
chatOverridesPath: "QQ_CHAT_OVERRIDES",
|
|
810
|
-
soulPath: "QQ_BOT_SOUL_PATH",
|
|
811
|
-
soulName: "QQ_BOT_SOUL_NAME",
|
|
812
|
-
home: "QQ_BOT_HOME",
|
|
813
|
-
agent: "QQ_BOT_AGENT",
|
|
814
|
-
model: "QQ_BOT_MODEL",
|
|
815
|
-
sendDelayMs: "QQ_SEND_DELAY_MS",
|
|
816
|
-
warnLength: "QQ_WARN_LENGTH",
|
|
817
|
-
reuseSession: "QQ_REUSE_SESSION",
|
|
818
|
-
"proactive.enabled": "QQ_PROACTIVE_ENABLED",
|
|
819
|
-
"proactive.silenceThresholdSeconds": "QQ_PROACTIVE_SILENCE_SECONDS",
|
|
820
|
-
"proactive.startProbability": "QQ_PROACTIVE_P_START",
|
|
821
|
-
"proactive.probabilityPerHour": "QQ_PROACTIVE_P_SLOPE_PER_HOUR",
|
|
822
|
-
"proactive.tickSeconds": "QQ_PROACTIVE_TICK_SECONDS",
|
|
823
|
-
"proactive.allowGroups": "QQ_PROACTIVE_ALLOW_GROUPS",
|
|
824
|
-
"proactive.allowUsers": "QQ_PROACTIVE_ALLOW_USERS",
|
|
825
|
-
"wake.prefixes": "QQ_WAKE_PREFIXES",
|
|
826
|
-
"wake.ignoreAtAll": "QQ_WAKE_IGNORE_AT_ALL",
|
|
827
|
-
"wake.replyToSelf": "QQ_WAKE_REPLY_TO_SELF",
|
|
828
|
-
"wake.ignoreAtOthers": "QQ_WAKE_IGNORE_AT_OTHERS",
|
|
829
|
-
"reply.minDelayMs": "QQ_REPLY_MIN_DELAY_MS",
|
|
830
|
-
"reply.maxDelayMs": "QQ_REPLY_MAX_DELAY_MS",
|
|
831
|
-
"reply.segmentChars": "QQ_REPLY_SEGMENT_CHARS",
|
|
832
|
-
"reply.forwardThresholdChars": "QQ_REPLY_FORWARD_THRESHOLD_CHARS",
|
|
833
|
-
"reply.requirePermission": "QQ_REPLY_REQUIRE_PERMISSION",
|
|
834
|
-
"trace.enabled": "QQ_TRACE_ENABLED",
|
|
835
|
-
"trace.dir": "QQ_TRACE_DIR",
|
|
836
|
-
"trace.maxSizeMb": "QQ_TRACE_MAX_SIZE_MB",
|
|
837
|
-
"trace.retentionDays": "QQ_TRACE_RETENTION_DAYS",
|
|
838
|
-
"reminder.enabled": "QQ_REMINDER_ENABLED",
|
|
839
|
-
"reminder.max": "QQ_REMINDER_MAX",
|
|
840
|
-
"keepAlive.enabled": "QQ_KEEP_ALIVE_ENABLED",
|
|
841
|
-
"keepAlive.seconds": "QQ_KEEP_ALIVE_SECONDS",
|
|
842
|
-
"history.count": "QQ_HISTORY_COUNT",
|
|
843
|
-
"rateLimit.maxPerMinute": "QQ_RATE_MAX_PER_MINUTE",
|
|
844
|
-
"rateLimit.maxPerHour": "QQ_RATE_MAX_PER_HOUR",
|
|
845
|
-
"rateLimit.minGapMs": "QQ_RATE_MIN_GAP_MS",
|
|
846
|
-
"rateLimit.stall": "QQ_RATE_STALL",
|
|
847
|
-
"rateLimit.stallMaxWaitMs": "QQ_RATE_STALL_MAX_WAIT_MS",
|
|
848
|
-
"sensitive.enabled": "QQ_SENSITIVE_ENABLED",
|
|
849
|
-
"events.subscribe": "QQ_EVENT_SUBSCRIBE",
|
|
850
|
-
"events.notices": "QQ_EVENT_NOTICES",
|
|
851
|
-
"events.inject": "QQ_EVENT_INJECT",
|
|
852
|
-
"log.level": "QQ_LOG_LEVEL",
|
|
853
|
-
"log.dir": "QQ_LOG_DIR",
|
|
854
|
-
"log.maxSize": "QQ_LOG_MAX_SIZE",
|
|
855
|
-
"log.maxFiles": "QQ_LOG_MAX_FILES",
|
|
856
|
-
"media.allowedDirs": "QQ_MEDIA_ALLOWED_DIRS",
|
|
857
|
-
"diary.path": "QQ_DIARY_PATH",
|
|
858
|
-
"diary.enabled": "QQ_DIARY_ENABLED",
|
|
859
|
-
"diary.retentionDays": "QQ_DIARY_RETENTION_DAYS",
|
|
860
|
-
"diary.summaryTime": "QQ_DIARY_SUMMARY_TIME",
|
|
861
|
-
"diary.tickSeconds": "QQ_DIARY_TICK_SECONDS",
|
|
862
|
-
"diary.summaryTargets": "QQ_DIARY_SUMMARY_TARGETS",
|
|
863
|
-
"diary.push": "QQ_DIARY_PUSH",
|
|
864
|
-
"diary.backfill": "QQ_DIARY_BACKFILL",
|
|
865
|
-
"diary.backfillMax": "QQ_DIARY_BACKFILL_MAX",
|
|
866
|
-
"diary.maxLineChars": "QQ_DIARY_MAX_LINE_CHARS",
|
|
867
|
-
"diary.maxLinesPerDay": "QQ_DIARY_MAX_LINES_PER_DAY",
|
|
868
|
-
"diary.timeoutMs": "QQ_DIARY_TIMEOUT_MS",
|
|
869
|
-
} as const
|
|
870
|
-
|
|
871
|
-
const OPTION_KEYS = new Set([
|
|
872
|
-
"napcat",
|
|
873
|
-
"access",
|
|
874
|
-
"ownerQQ",
|
|
875
|
-
"admins",
|
|
876
|
-
"toolProfilesPath",
|
|
877
|
-
"chatOverridesPath",
|
|
878
|
-
"soulPath",
|
|
879
|
-
"soulName",
|
|
880
|
-
"home",
|
|
881
|
-
"agent",
|
|
882
|
-
"model",
|
|
883
|
-
"sendDelayMs",
|
|
884
|
-
"warnLength",
|
|
885
|
-
"reuseSession",
|
|
886
|
-
"proactive",
|
|
887
|
-
"wake",
|
|
888
|
-
"reply",
|
|
889
|
-
"trace",
|
|
890
|
-
"reminder",
|
|
891
|
-
"keepAlive",
|
|
892
|
-
"history",
|
|
893
|
-
"rateLimit",
|
|
894
|
-
"sensitive",
|
|
895
|
-
"events",
|
|
896
|
-
"log",
|
|
897
|
-
"media",
|
|
898
|
-
"diary",
|
|
899
|
-
])
|
|
900
|
-
|
|
901
|
-
const NESTED_OPTION_KEYS: Record<string, ReadonlySet<string>> = {
|
|
902
|
-
napcat: new Set(["url", "token"]),
|
|
903
|
-
access: new Set(["allow", "deny", "allowAllWhenEmpty"]),
|
|
904
|
-
"access.allow": new Set(["groups", "users"]),
|
|
905
|
-
"access.deny": new Set(["groups", "users"]),
|
|
906
|
-
proactive: new Set([
|
|
907
|
-
"enabled",
|
|
908
|
-
"silenceThresholdSeconds",
|
|
909
|
-
"startProbability",
|
|
910
|
-
"probabilityPerHour",
|
|
911
|
-
"tickSeconds",
|
|
912
|
-
"allowGroups",
|
|
913
|
-
"allowUsers",
|
|
914
|
-
]),
|
|
915
|
-
wake: new Set(["prefixes", "ignoreAtAll", "replyToSelf", "ignoreAtOthers"]),
|
|
916
|
-
reply: new Set([
|
|
917
|
-
"minDelayMs",
|
|
918
|
-
"maxDelayMs",
|
|
919
|
-
"segmentChars",
|
|
920
|
-
"forwardThresholdChars",
|
|
921
|
-
"requirePermission",
|
|
922
|
-
]),
|
|
923
|
-
trace: new Set(["enabled", "dir", "maxSizeMb", "retentionDays"]),
|
|
924
|
-
reminder: new Set(["enabled", "max"]),
|
|
925
|
-
keepAlive: new Set(["enabled", "seconds"]),
|
|
926
|
-
history: new Set(["count"]),
|
|
927
|
-
rateLimit: new Set(["maxPerMinute", "maxPerHour", "minGapMs", "stall", "stallMaxWaitMs"]),
|
|
928
|
-
sensitive: new Set(["enabled"]),
|
|
929
|
-
events: new Set(["subscribe", "notices", "inject"]),
|
|
930
|
-
log: new Set(["level", "dir", "maxSize", "maxFiles"]),
|
|
931
|
-
media: new Set(["allowedDirs"]),
|
|
932
|
-
diary: new Set([
|
|
933
|
-
"path",
|
|
934
|
-
"enabled",
|
|
935
|
-
"retentionDays",
|
|
936
|
-
"summaryTime",
|
|
937
|
-
"tickSeconds",
|
|
938
|
-
"summaryTargets",
|
|
939
|
-
"push",
|
|
940
|
-
"backfill",
|
|
941
|
-
"backfillMax",
|
|
942
|
-
"maxLineChars",
|
|
943
|
-
"maxLinesPerDay",
|
|
944
|
-
"timeoutMs",
|
|
945
|
-
]),
|
|
946
|
-
}
|
|
947
|
-
|
|
948
|
-
/** 改过名的配置项:给出迁移提示而不是笼统的“未知配置项”。 */
|
|
949
|
-
const RENAMED_OPTIONS: Record<string, string> = {
|
|
950
|
-
groups: "access.allow.groups",
|
|
951
|
-
users: "access.allow.users",
|
|
952
|
-
}
|
|
953
|
-
|
|
954
|
-
/**
|
|
955
|
-
* 报告被 schema 静默剥离的未知配置项。
|
|
956
|
-
*
|
|
957
|
-
* 按层级逐层检查:`access.allow.groups` 这类嵌套路径也能报出来。
|
|
958
|
-
*/
|
|
959
|
-
function warnUnknownKeys(options: Record<string, unknown>, warn: ConfigWarning): void {
|
|
960
|
-
const walk = (value: Record<string, unknown>, path: string): void => {
|
|
961
|
-
const allowed = path === "" ? OPTION_KEYS : NESTED_OPTION_KEYS[path]
|
|
962
|
-
for (const key of Object.keys(value)) {
|
|
963
|
-
const childPath = path ? `${path}.${key}` : key
|
|
964
|
-
if (allowed && !allowed.has(key)) {
|
|
965
|
-
const renamed = RENAMED_OPTIONS[childPath]
|
|
966
|
-
warn(
|
|
967
|
-
renamed
|
|
968
|
-
? `${MIGRATION_WARNING_PREFIX} 插件选项 ${childPath} 已移除,请改用 ${renamed}`
|
|
969
|
-
: `插件选项 未知配置项:${childPath}`,
|
|
970
|
-
)
|
|
971
|
-
continue
|
|
972
|
-
}
|
|
973
|
-
const child = value[key]
|
|
974
|
-
if (!child || typeof child !== "object" || Array.isArray(child)) continue
|
|
975
|
-
walk(child as Record<string, unknown>, childPath)
|
|
976
|
-
}
|
|
977
|
-
}
|
|
978
|
-
walk(options, "")
|
|
979
|
-
}
|
|
980
|
-
|
|
981
|
-
function envLabel(path: string): string {
|
|
982
|
-
return ENV_VARS[path as keyof typeof ENV_VARS] ?? path
|
|
983
|
-
}
|
|
984
|
-
|
|
985
|
-
function fromEnv(env: Record<string, string | undefined>): Record<string, unknown> {
|
|
986
|
-
const config: Record<string, unknown> = {}
|
|
987
|
-
for (const [path, name] of Object.entries(ENV_VARS)) {
|
|
988
|
-
const value = env[name]
|
|
989
|
-
if (value === undefined) continue
|
|
990
|
-
const keys = path.split(".")
|
|
991
|
-
const leaf = keys.pop()
|
|
992
|
-
if (!leaf) continue
|
|
993
|
-
let target = config
|
|
994
|
-
for (const key of keys) {
|
|
995
|
-
const child = target[key]
|
|
996
|
-
const next = child && typeof child === "object" ? (child as Record<string, unknown>) : {}
|
|
997
|
-
target[key] = next
|
|
998
|
-
target = next
|
|
999
|
-
}
|
|
1000
|
-
target[leaf] = value
|
|
67
|
+
if (!subscribe.includes(category)) subscribe.push(category)
|
|
1001
68
|
}
|
|
1002
|
-
return
|
|
69
|
+
return subscribe
|
|
1003
70
|
}
|
|
1004
71
|
|
|
1005
72
|
/**
|
|
1006
|
-
*
|
|
73
|
+
* 解析配置文件内容。
|
|
1007
74
|
*
|
|
1008
|
-
*
|
|
75
|
+
* 非法值告警并回退默认值,不让一个手写错的值让插件起不来;
|
|
76
|
+
* 配置文件里不认识的键告警但保留在文件里,不静默丢弃。
|
|
1009
77
|
*
|
|
1010
|
-
* @
|
|
78
|
+
* @param raw `config.json` 的原始内容。
|
|
79
|
+
* @param agent `opencode.json` 里的 agent:OpenCode 侧的路由语义,不存在配置文件里。
|
|
80
|
+
* @param onWarning 告警回调。
|
|
81
|
+
* @throws 当没有配置 NapCat WebSocket 地址时抛出,避免带着无效凭证启动。
|
|
1011
82
|
*/
|
|
1012
83
|
export function resolveConfig(
|
|
1013
|
-
|
|
1014
|
-
|
|
84
|
+
raw: Record<string, unknown>,
|
|
85
|
+
agent?: string,
|
|
1015
86
|
onWarning: ConfigWarning = () => {},
|
|
1016
87
|
): QQBotConfig {
|
|
1017
88
|
const warnings: string[] = []
|
|
1018
89
|
const warn: ConfigWarning = (message) => warnings.push(message)
|
|
1019
90
|
|
|
1020
|
-
|
|
1021
|
-
const
|
|
1022
|
-
const
|
|
1023
|
-
const optionsSchema = createConfigSchema(base, base.napcat, (path) => `插件选项 ${path}`, warn)
|
|
1024
|
-
const parsed: RawConfig = optionsSchema.parse(options)
|
|
91
|
+
warnUnknownFileKeys(raw, warn)
|
|
92
|
+
const schema = createConfigSchema(warn)
|
|
93
|
+
const parsed: ParsedConfig = schema.parse(raw)
|
|
1025
94
|
|
|
1026
|
-
|
|
1027
|
-
const subscribe: string[] = []
|
|
1028
|
-
for (const category of parsed.events.subscribe) {
|
|
1029
|
-
if (!EVENT_CATEGORIES.includes(category)) {
|
|
1030
|
-
warn(`events.subscribe 忽略未知类别:${category}`)
|
|
1031
|
-
continue
|
|
1032
|
-
}
|
|
1033
|
-
if (!subscribe.includes(category)) subscribe.push(category)
|
|
1034
|
-
}
|
|
1035
|
-
|
|
1036
|
-
// 旧的名单环境变量已经移除:给出醒目的迁移提示,避免升级后机器人静默失联。
|
|
1037
|
-
for (const [name, replacement] of Object.entries(RENAMED_ENV_VARS)) {
|
|
1038
|
-
if (env[name] !== undefined) {
|
|
1039
|
-
warn(
|
|
1040
|
-
`${MIGRATION_WARNING_PREFIX} 环境变量 ${name} 已移除,请改用 ${replacement};未迁移时机器人不会响应任何消息`,
|
|
1041
|
-
)
|
|
1042
|
-
}
|
|
1043
|
-
}
|
|
95
|
+
const subscribe = filterEventCategories(parsed.events.subscribe, warn)
|
|
1044
96
|
|
|
1045
97
|
// owner 必须能私聊到机器人,否则管理命令无处触发:拒绝名单里的 owner 豁免。
|
|
1046
98
|
const denyUsers = [...parsed.access.deny.users]
|
|
@@ -1055,7 +107,7 @@ export function resolveConfig(
|
|
|
1055
107
|
const url = parsed.napcat.url
|
|
1056
108
|
if (!url) {
|
|
1057
109
|
throw new Error(
|
|
1058
|
-
"未配置 NapCat WebSocket
|
|
110
|
+
"未配置 NapCat WebSocket 地址:请在 config.json 里填写 napcat.url(格式 ws://127.0.0.1:3001)",
|
|
1059
111
|
)
|
|
1060
112
|
}
|
|
1061
113
|
|
|
@@ -1097,6 +149,8 @@ export function resolveConfig(
|
|
|
1097
149
|
...parsed.diary,
|
|
1098
150
|
summaryTargets: [...parsed.diary.summaryTargets],
|
|
1099
151
|
},
|
|
152
|
+
panel: { ...parsed.panel },
|
|
1100
153
|
napcat: { url, token: parsed.napcat.token },
|
|
154
|
+
...(agent ? { agent } : {}),
|
|
1101
155
|
}
|
|
1102
156
|
}
|