@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
|
@@ -0,0 +1,654 @@
|
|
|
1
|
+
import { Model } from "@opencode/schema"
|
|
2
|
+
import { z } from "zod"
|
|
3
|
+
import { CONFIG_FIELDS, type ConfigField } from "./fields"
|
|
4
|
+
import type {
|
|
5
|
+
AccessConfig,
|
|
6
|
+
DiaryPush,
|
|
7
|
+
LogLevel,
|
|
8
|
+
ModelRef,
|
|
9
|
+
ParsedConfig,
|
|
10
|
+
QQBotConfig,
|
|
11
|
+
} from "./types"
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 由字段注册表生成配置产物:默认值、zod 校验、配置文件键集合与 JSON Schema。
|
|
15
|
+
*
|
|
16
|
+
* 注册表是唯一事实源:新增一个配置项只需要改 `fields.ts`。
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const NUMBER = /^-?(?:\d+\.?\d*|\.\d+)$/
|
|
20
|
+
const TRUTHY = new Set(["1", "true", "yes", "on"])
|
|
21
|
+
const BOOLEAN_SPELLINGS = ["1", "0", "true", "false", "yes", "no", "on", "off"] as const
|
|
22
|
+
/** 合法的时刻格式:`HH:mm`。 */
|
|
23
|
+
const CLOCK = /^([01]?\d|2[0-3]):([0-5]\d)$/
|
|
24
|
+
|
|
25
|
+
/** 配置告警回调。 */
|
|
26
|
+
export type ConfigWarning = (message: string) => void
|
|
27
|
+
|
|
28
|
+
/** 判断值是否算“已提供”,用于区分空值与写错的值。 */
|
|
29
|
+
function present(value: unknown): boolean {
|
|
30
|
+
return value !== undefined && value !== null && value !== ""
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 把「数组或逗号分隔字符串」统一成数组;其它类型返回 undefined。 */
|
|
34
|
+
function toSource(value: unknown): unknown[] | undefined {
|
|
35
|
+
if (Array.isArray(value)) return value
|
|
36
|
+
return typeof value === "string" ? value.split(",") : undefined
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 把数字数组或逗号分隔字符串整理成去重后的正整数列表。 */
|
|
40
|
+
function toIds(
|
|
41
|
+
value: unknown,
|
|
42
|
+
warn: ConfigWarning,
|
|
43
|
+
label: string,
|
|
44
|
+
noun: string,
|
|
45
|
+
): number[] | undefined {
|
|
46
|
+
const source = toSource(value)
|
|
47
|
+
if (!source) return undefined
|
|
48
|
+
const ids: number[] = []
|
|
49
|
+
const seen = new Set<number>()
|
|
50
|
+
for (const entry of source) {
|
|
51
|
+
const raw =
|
|
52
|
+
typeof entry === "number" ? String(entry) : typeof entry === "string" ? entry.trim() : ""
|
|
53
|
+
if (!raw) continue
|
|
54
|
+
const parsed = Number(raw)
|
|
55
|
+
if (!Number.isInteger(parsed) || parsed <= 0) {
|
|
56
|
+
warn(`${label} 忽略非法${noun}:${raw}`)
|
|
57
|
+
continue
|
|
58
|
+
}
|
|
59
|
+
if (seen.has(parsed)) continue
|
|
60
|
+
seen.add(parsed)
|
|
61
|
+
ids.push(parsed)
|
|
62
|
+
}
|
|
63
|
+
return ids
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** 解析 "provider/model" 或 "provider/model/variant",复用 SDK 的模型引用解析。 */
|
|
67
|
+
export function parseModelRef(value: unknown): ModelRef | undefined {
|
|
68
|
+
const raw = typeof value === "string" ? value.trim() : ""
|
|
69
|
+
if (!raw) return undefined
|
|
70
|
+
const [providerID, id, variant, ...rest] = raw.split("/").filter(Boolean)
|
|
71
|
+
if (!providerID || !id || rest.length > 0) return undefined
|
|
72
|
+
try {
|
|
73
|
+
const ref = Model.Ref.parse(`${providerID}/${id}${variant ? `#${variant}` : ""}`)
|
|
74
|
+
return {
|
|
75
|
+
providerID: ref.providerID,
|
|
76
|
+
id: ref.id,
|
|
77
|
+
...(ref.variant ? { variant: ref.variant } : {}),
|
|
78
|
+
}
|
|
79
|
+
} catch {
|
|
80
|
+
return undefined
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 深拷贝一份默认值:catch 分支不能把默认对象本身交出去。 */
|
|
85
|
+
function clone<T>(value: T): T {
|
|
86
|
+
if (Array.isArray(value)) return value.map((entry) => clone(entry)) as unknown as T
|
|
87
|
+
if (value && typeof value === "object") {
|
|
88
|
+
const result: Record<string, unknown> = {}
|
|
89
|
+
for (const [key, entry] of Object.entries(value as Record<string, unknown>)) {
|
|
90
|
+
result[key] = clone(entry)
|
|
91
|
+
}
|
|
92
|
+
return result as T
|
|
93
|
+
}
|
|
94
|
+
return value
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** 字段树:一个节点要么是叶子字段,要么是下一层对象。 */
|
|
98
|
+
interface SchemaNode {
|
|
99
|
+
/** 子节点,键是路径的一段。 */
|
|
100
|
+
children: Map<string, SchemaNode>
|
|
101
|
+
/** 叶子字段;对象节点为 undefined。 */
|
|
102
|
+
field?: ConfigField
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** 由注册表长出字段树。 */
|
|
106
|
+
function buildTree(): SchemaNode {
|
|
107
|
+
const root: SchemaNode = { children: new Map() }
|
|
108
|
+
for (const field of CONFIG_FIELDS) {
|
|
109
|
+
let node = root
|
|
110
|
+
for (const segment of field.path.split(".")) {
|
|
111
|
+
let child = node.children.get(segment)
|
|
112
|
+
if (!child) {
|
|
113
|
+
child = { children: new Map() }
|
|
114
|
+
node.children.set(segment, child)
|
|
115
|
+
}
|
|
116
|
+
node = child
|
|
117
|
+
}
|
|
118
|
+
node.field = field
|
|
119
|
+
}
|
|
120
|
+
return root
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const TREE = buildTree()
|
|
124
|
+
|
|
125
|
+
/** 取某个路径在对象里的值。 */
|
|
126
|
+
export function getConfigValue(target: unknown, path: string): unknown {
|
|
127
|
+
let current: unknown = target
|
|
128
|
+
for (const segment of path.split(".")) {
|
|
129
|
+
if (!current || typeof current !== "object") return undefined
|
|
130
|
+
current = (current as Record<string, unknown>)[segment]
|
|
131
|
+
}
|
|
132
|
+
return current
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** 把值写进对象的指定路径;中间对象必须已经存在。 */
|
|
136
|
+
function setPath(target: Record<string, unknown>, path: string, value: unknown): void {
|
|
137
|
+
const segments = path.split(".")
|
|
138
|
+
let current: Record<string, unknown> = target
|
|
139
|
+
for (const segment of segments.slice(0, -1)) {
|
|
140
|
+
const child = current[segment]
|
|
141
|
+
if (!child || typeof child !== "object") return
|
|
142
|
+
current = child as Record<string, unknown>
|
|
143
|
+
}
|
|
144
|
+
const leaf = segments[segments.length - 1]
|
|
145
|
+
if (leaf === undefined) return
|
|
146
|
+
current[leaf] = value
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** 编号名单里“群号”与“QQ 号”的叫法。 */
|
|
150
|
+
function idNoun(path: string): string {
|
|
151
|
+
return path.toLowerCase().includes("group") ? "群号" : "QQ 号"
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** 数值字段的取值约束:范围、整数。 */
|
|
155
|
+
function numberConstraint(field: ConfigField): { check: z.ZodNumber; rule: string } {
|
|
156
|
+
let check = z.number()
|
|
157
|
+
const parts: string[] = []
|
|
158
|
+
if (field.step === 1) {
|
|
159
|
+
check = check.int()
|
|
160
|
+
parts.push("必须是整数")
|
|
161
|
+
}
|
|
162
|
+
if (field.min !== undefined) {
|
|
163
|
+
if (field.exclusiveMin) {
|
|
164
|
+
check = field.min === 0 ? check.positive() : check.gt(field.min)
|
|
165
|
+
parts.push(`必须大于 ${field.min}`)
|
|
166
|
+
} else {
|
|
167
|
+
check = check.min(field.min)
|
|
168
|
+
parts.push(`不能小于 ${field.min}`)
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (field.max !== undefined) {
|
|
172
|
+
check = check.max(field.max)
|
|
173
|
+
parts.push(`不能大于 ${field.max}`)
|
|
174
|
+
}
|
|
175
|
+
return { check, rule: parts.join(",") }
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** 单个字段的 zod 校验:非法值告警并回退到默认值。 */
|
|
179
|
+
function leafSchema(field: ConfigField, warn: ConfigWarning): z.ZodTypeAny {
|
|
180
|
+
const path = field.path
|
|
181
|
+
const fallback = field.default
|
|
182
|
+
switch (field.type) {
|
|
183
|
+
case "boolean": {
|
|
184
|
+
const value = fallback as boolean
|
|
185
|
+
return z
|
|
186
|
+
.union([z.boolean(), z.string().trim().toLowerCase().pipe(z.enum(BOOLEAN_SPELLINGS))])
|
|
187
|
+
.transform((entry) => (typeof entry === "boolean" ? entry : TRUTHY.has(entry)))
|
|
188
|
+
.catch((ctx) => {
|
|
189
|
+
if (present(ctx.value)) warn(`${path} 不是合法的布尔值:${String(ctx.value)}`)
|
|
190
|
+
return value
|
|
191
|
+
})
|
|
192
|
+
.default(value)
|
|
193
|
+
}
|
|
194
|
+
case "number": {
|
|
195
|
+
// 可选的单个编号:允许数字或数字字符串,非法值回退到未配置。
|
|
196
|
+
if (fallback === undefined) {
|
|
197
|
+
return z
|
|
198
|
+
.unknown()
|
|
199
|
+
.optional()
|
|
200
|
+
.transform((entry) => {
|
|
201
|
+
if (!present(entry)) return undefined
|
|
202
|
+
const parsed =
|
|
203
|
+
typeof entry === "number"
|
|
204
|
+
? entry
|
|
205
|
+
: typeof entry === "string" && entry.trim() !== ""
|
|
206
|
+
? Number(entry.trim())
|
|
207
|
+
: Number.NaN
|
|
208
|
+
if (!Number.isInteger(parsed) || parsed <= 0) {
|
|
209
|
+
warn(`${path} 不是合法的 QQ 号:${String(entry)}`)
|
|
210
|
+
return undefined
|
|
211
|
+
}
|
|
212
|
+
return parsed
|
|
213
|
+
})
|
|
214
|
+
}
|
|
215
|
+
const value = fallback as number
|
|
216
|
+
const { check, rule } = numberConstraint(field)
|
|
217
|
+
return z
|
|
218
|
+
.union([z.number(), z.string().trim().regex(NUMBER).transform(Number)])
|
|
219
|
+
.pipe(z.number().finite())
|
|
220
|
+
.catch((ctx) => {
|
|
221
|
+
if (present(ctx.value)) warn(`${path} 不是合法的数字:${String(ctx.value)}`)
|
|
222
|
+
return value
|
|
223
|
+
})
|
|
224
|
+
.default(value)
|
|
225
|
+
.pipe(check)
|
|
226
|
+
.catch((ctx) => {
|
|
227
|
+
if (present(ctx.value)) {
|
|
228
|
+
warn(`${path} ${rule},当前为 ${String(ctx.value)},已回退到 ${value}`)
|
|
229
|
+
}
|
|
230
|
+
return value
|
|
231
|
+
})
|
|
232
|
+
}
|
|
233
|
+
case "enum": {
|
|
234
|
+
const options = field.options ?? []
|
|
235
|
+
return z
|
|
236
|
+
.unknown()
|
|
237
|
+
.optional()
|
|
238
|
+
.transform((value) => {
|
|
239
|
+
if (!present(value)) return fallback
|
|
240
|
+
const raw = typeof value === "string" ? value.trim().toLowerCase() : ""
|
|
241
|
+
if (!raw) {
|
|
242
|
+
warn(`${path} 不能为空,已回退到默认值`)
|
|
243
|
+
return fallback
|
|
244
|
+
}
|
|
245
|
+
if (!options.includes(raw)) {
|
|
246
|
+
warn(`${path} 不是合法的取值:${String(value)}(可用:${options.join("/")})`)
|
|
247
|
+
return fallback
|
|
248
|
+
}
|
|
249
|
+
return raw
|
|
250
|
+
})
|
|
251
|
+
}
|
|
252
|
+
case "numberList": {
|
|
253
|
+
const list = Array.isArray(fallback) ? (fallback as number[]) : []
|
|
254
|
+
return z
|
|
255
|
+
.unknown()
|
|
256
|
+
.optional()
|
|
257
|
+
.transform((entry) => {
|
|
258
|
+
if (entry === undefined || entry === null) return [...list]
|
|
259
|
+
const parsed = toIds(entry, warn, path, idNoun(path))
|
|
260
|
+
if (!parsed) {
|
|
261
|
+
if (present(entry)) warn(`${path} 解析不出${idNoun(path)}:${String(entry)}`)
|
|
262
|
+
return [...list]
|
|
263
|
+
}
|
|
264
|
+
return parsed
|
|
265
|
+
})
|
|
266
|
+
}
|
|
267
|
+
case "stringList": {
|
|
268
|
+
const list = Array.isArray(fallback) ? (fallback as string[]) : []
|
|
269
|
+
return z
|
|
270
|
+
.unknown()
|
|
271
|
+
.optional()
|
|
272
|
+
.transform((value) => {
|
|
273
|
+
if (value === undefined || value === null) return [...list]
|
|
274
|
+
const source = toSource(value)
|
|
275
|
+
if (!source) {
|
|
276
|
+
if (present(value)) warn(`${path} 必须是字符串或字符串数组:${String(value)}`)
|
|
277
|
+
return [...list]
|
|
278
|
+
}
|
|
279
|
+
const items: string[] = []
|
|
280
|
+
for (const entry of source) {
|
|
281
|
+
const raw = typeof entry === "string" ? entry.trim() : ""
|
|
282
|
+
if (!raw) continue
|
|
283
|
+
if (items.includes(raw)) continue
|
|
284
|
+
items.push(raw)
|
|
285
|
+
}
|
|
286
|
+
return items
|
|
287
|
+
})
|
|
288
|
+
}
|
|
289
|
+
case "clock":
|
|
290
|
+
return z
|
|
291
|
+
.unknown()
|
|
292
|
+
.optional()
|
|
293
|
+
.transform((value) => {
|
|
294
|
+
if (!present(value)) return fallback
|
|
295
|
+
if (typeof value !== "string") {
|
|
296
|
+
warn(`${path} 必须是字符串:${String(value)}`)
|
|
297
|
+
return fallback
|
|
298
|
+
}
|
|
299
|
+
const trimmed = value.trim()
|
|
300
|
+
if (!CLOCK.test(trimmed)) {
|
|
301
|
+
warn(`${path} 不是合法的时刻:${trimmed}(应为 HH:mm)`)
|
|
302
|
+
return fallback
|
|
303
|
+
}
|
|
304
|
+
return trimmed
|
|
305
|
+
})
|
|
306
|
+
case "modelRef":
|
|
307
|
+
return z
|
|
308
|
+
.unknown()
|
|
309
|
+
.optional()
|
|
310
|
+
.transform((value) => {
|
|
311
|
+
if (!present(value)) return fallback
|
|
312
|
+
const parsed = parseModelRef(value)
|
|
313
|
+
if (!parsed) {
|
|
314
|
+
warn(`${path} 格式无效:${String(value)}(应为 provider/model[/variant])`)
|
|
315
|
+
return fallback
|
|
316
|
+
}
|
|
317
|
+
return parsed
|
|
318
|
+
})
|
|
319
|
+
case "path":
|
|
320
|
+
// 路径允许留空(空表示默认位置),非字符串回退到默认值。
|
|
321
|
+
return z
|
|
322
|
+
.unknown()
|
|
323
|
+
.optional()
|
|
324
|
+
.transform((value) => {
|
|
325
|
+
if (value === undefined || value === null) return fallback
|
|
326
|
+
if (typeof value !== "string") {
|
|
327
|
+
warn(`${path} 必须是字符串:${String(value)}`)
|
|
328
|
+
return fallback
|
|
329
|
+
}
|
|
330
|
+
return value.trim()
|
|
331
|
+
})
|
|
332
|
+
case "text":
|
|
333
|
+
case "secret":
|
|
334
|
+
return z
|
|
335
|
+
.unknown()
|
|
336
|
+
.optional()
|
|
337
|
+
.transform((value) => {
|
|
338
|
+
if (!present(value)) return fallback
|
|
339
|
+
if (typeof value !== "string") {
|
|
340
|
+
warn(`${path} 必须是字符串:${String(value)}`)
|
|
341
|
+
return fallback
|
|
342
|
+
}
|
|
343
|
+
const trimmed = value.trim()
|
|
344
|
+
if (!trimmed) {
|
|
345
|
+
warn(`${path} 不能为空,已回退到默认值`)
|
|
346
|
+
return fallback
|
|
347
|
+
}
|
|
348
|
+
return trimmed
|
|
349
|
+
})
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** 对象节点的默认值:只包含这一层注册表声明过的字段。 */
|
|
354
|
+
function nodeDefaults(node: SchemaNode): Record<string, unknown> {
|
|
355
|
+
const result: Record<string, unknown> = {}
|
|
356
|
+
for (const [key, child] of node.children) {
|
|
357
|
+
if (child.field) {
|
|
358
|
+
if (child.field.default === undefined) continue
|
|
359
|
+
result[key] = clone(child.field.default)
|
|
360
|
+
continue
|
|
361
|
+
}
|
|
362
|
+
result[key] = nodeDefaults(child)
|
|
363
|
+
}
|
|
364
|
+
return result
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** 递归生成 zod schema。 */
|
|
368
|
+
function nodeSchema(node: SchemaNode, warn: ConfigWarning): z.ZodTypeAny {
|
|
369
|
+
if (node.field) return leafSchema(node.field, warn)
|
|
370
|
+
const shape: Record<string, z.ZodTypeAny> = {}
|
|
371
|
+
for (const [key, child] of node.children) {
|
|
372
|
+
shape[key] = nodeSchema(child, warn)
|
|
373
|
+
}
|
|
374
|
+
return z.object(shape).prefault({}).catch(nodeDefaults(node))
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* 用字段注册表生成配置校验 schema。
|
|
379
|
+
*
|
|
380
|
+
* 语义与旧版一致:非法值告警并回退到默认值,不让一个手写错的值让插件起不来。
|
|
381
|
+
*/
|
|
382
|
+
export function createConfigSchema(warn: ConfigWarning): z.ZodType<ParsedConfig> {
|
|
383
|
+
return nodeSchema(TREE, warn) as unknown as z.ZodType<ParsedConfig>
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** 除 NapCat 连接信息外的全部默认值。 */
|
|
387
|
+
export const DEFAULT_CONFIG: Omit<QQBotConfig, "napcat"> = nodeDefaults(TREE) as Omit<
|
|
388
|
+
QQBotConfig,
|
|
389
|
+
"napcat"
|
|
390
|
+
>
|
|
391
|
+
|
|
392
|
+
/** NapCat 连接信息的默认值(URL 必填,因此没有默认值)。 */
|
|
393
|
+
export const DEFAULT_NAPCAT: { url?: string; token?: string } = {}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* 配置文件里允许出现的键:路径前缀 → 该层的合法子键。
|
|
397
|
+
*
|
|
398
|
+
* 键集合由注册表生成:注册表里没有的键就是未知键,读文件时告警但保留原样。
|
|
399
|
+
*/
|
|
400
|
+
export const FILE_KEYS: ReadonlyMap<string, ReadonlySet<string>> = (() => {
|
|
401
|
+
const keys = new Map<string, Set<string>>()
|
|
402
|
+
for (const field of CONFIG_FIELDS) {
|
|
403
|
+
const segments = field.path.split(".")
|
|
404
|
+
for (let index = 0; index < segments.length; index += 1) {
|
|
405
|
+
const prefix = segments.slice(0, index).join(".")
|
|
406
|
+
const key = segments[index]
|
|
407
|
+
if (key === undefined) continue
|
|
408
|
+
const allowed = keys.get(prefix) ?? new Set<string>()
|
|
409
|
+
allowed.add(key)
|
|
410
|
+
keys.set(prefix, allowed)
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return keys
|
|
414
|
+
})()
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* 报告配置文件里被 schema 忽略的未知键。
|
|
418
|
+
*
|
|
419
|
+
* 按层级逐层检查:`access.allow.groups` 这类嵌套路径也能报出来。
|
|
420
|
+
*/
|
|
421
|
+
export function warnUnknownFileKeys(raw: Record<string, unknown>, warn: ConfigWarning): void {
|
|
422
|
+
const walk = (value: Record<string, unknown>, path: string): void => {
|
|
423
|
+
const allowed = FILE_KEYS.get(path)
|
|
424
|
+
for (const key of Object.keys(value)) {
|
|
425
|
+
const childPath = path ? `${path}.${key}` : key
|
|
426
|
+
if (allowed && !allowed.has(key)) {
|
|
427
|
+
warn(`配置文件 未知配置项:${childPath}`)
|
|
428
|
+
continue
|
|
429
|
+
}
|
|
430
|
+
const child = value[key]
|
|
431
|
+
if (!child || typeof child !== "object" || Array.isArray(child)) continue
|
|
432
|
+
walk(child as Record<string, unknown>, childPath)
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
walk(raw, "")
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/** 注册表里声明为热更新的字段。 */
|
|
439
|
+
export const HOT_FIELDS: readonly ConfigField[] = CONFIG_FIELDS.filter((field) => field.hot)
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* 把新配置里的热字段写进正在生效的配置对象。
|
|
443
|
+
*
|
|
444
|
+
* 只改叶子值,中间对象保持同一个引用:运行中的模块早就抓住了这些对象,
|
|
445
|
+
* 换掉对象它们就看不到新值了。
|
|
446
|
+
*/
|
|
447
|
+
export function applyHotFields(live: QQBotConfig, next: QQBotConfig): void {
|
|
448
|
+
for (const field of HOT_FIELDS) {
|
|
449
|
+
const value = getConfigValue(next, field.path)
|
|
450
|
+
setPath(live as unknown as Record<string, unknown>, field.path, clone(value))
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* 单个字段的严格校验 schema。
|
|
456
|
+
*
|
|
457
|
+
* 与解析用的 schema 不同:这里不会把非法值回退成默认值,而是直接报错,
|
|
458
|
+
* 面板保存时才能把「哪个字段写错了」告诉用户。
|
|
459
|
+
*/
|
|
460
|
+
export function createStrictFieldSchema(field: ConfigField): z.ZodType {
|
|
461
|
+
const strictId = z
|
|
462
|
+
.union([z.number(), z.string().trim().regex(/^\d+$/).transform(Number)])
|
|
463
|
+
.pipe(z.number().int().positive())
|
|
464
|
+
const strictIdFromText = z
|
|
465
|
+
.string()
|
|
466
|
+
.regex(/^\d+$/)
|
|
467
|
+
.transform((value) => Number(value))
|
|
468
|
+
.pipe(z.number().int().positive())
|
|
469
|
+
switch (field.type) {
|
|
470
|
+
case "boolean":
|
|
471
|
+
return z
|
|
472
|
+
.union([z.boolean(), z.string().trim().toLowerCase().pipe(z.enum(BOOLEAN_SPELLINGS))])
|
|
473
|
+
.transform((value) => (typeof value === "boolean" ? value : TRUTHY.has(value)))
|
|
474
|
+
case "number": {
|
|
475
|
+
const { check } = numberConstraint(field)
|
|
476
|
+
if (field.default === undefined) return strictId
|
|
477
|
+
return z
|
|
478
|
+
.union([z.number(), z.string().trim().regex(NUMBER).transform(Number)])
|
|
479
|
+
.pipe(z.number().finite())
|
|
480
|
+
.pipe(check)
|
|
481
|
+
}
|
|
482
|
+
case "enum": {
|
|
483
|
+
const options = field.options ?? []
|
|
484
|
+
return z
|
|
485
|
+
.string()
|
|
486
|
+
.trim()
|
|
487
|
+
.toLowerCase()
|
|
488
|
+
.refine((value) => options.includes(value), {
|
|
489
|
+
message: `必须是 ${options.join("/")} 之一`,
|
|
490
|
+
})
|
|
491
|
+
}
|
|
492
|
+
case "numberList":
|
|
493
|
+
return z.union([
|
|
494
|
+
z.array(strictId),
|
|
495
|
+
z
|
|
496
|
+
.string()
|
|
497
|
+
.transform((value) =>
|
|
498
|
+
value
|
|
499
|
+
.split(",")
|
|
500
|
+
.map((entry) => entry.trim())
|
|
501
|
+
.filter((entry) => entry.length > 0),
|
|
502
|
+
)
|
|
503
|
+
.pipe(z.array(strictIdFromText)),
|
|
504
|
+
])
|
|
505
|
+
case "stringList":
|
|
506
|
+
return z
|
|
507
|
+
.union([
|
|
508
|
+
z.array(z.string()),
|
|
509
|
+
z.string().transform((value) =>
|
|
510
|
+
value
|
|
511
|
+
.split(",")
|
|
512
|
+
.map((entry) => entry.trim())
|
|
513
|
+
.filter((entry) => entry.length > 0),
|
|
514
|
+
),
|
|
515
|
+
])
|
|
516
|
+
.pipe(z.array(z.string().min(1)))
|
|
517
|
+
case "clock":
|
|
518
|
+
return z.string().trim().regex(CLOCK, "必须是 HH:mm 格式的时刻")
|
|
519
|
+
case "modelRef":
|
|
520
|
+
return z
|
|
521
|
+
.string()
|
|
522
|
+
.trim()
|
|
523
|
+
.refine((value) => parseModelRef(value) !== undefined, {
|
|
524
|
+
message: "格式应为 provider/model[/variant]",
|
|
525
|
+
})
|
|
526
|
+
case "path":
|
|
527
|
+
return z.string().trim()
|
|
528
|
+
case "text":
|
|
529
|
+
case "secret":
|
|
530
|
+
return z.string().trim().min(1, "不能为空")
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/** JSON Schema 里每个字段的取值规则。 */
|
|
535
|
+
function jsonSchemaFor(field: ConfigField): Record<string, unknown> {
|
|
536
|
+
const base: Record<string, unknown> = {}
|
|
537
|
+
switch (field.type) {
|
|
538
|
+
case "boolean":
|
|
539
|
+
base.type = "boolean"
|
|
540
|
+
break
|
|
541
|
+
case "number":
|
|
542
|
+
base.type = "number"
|
|
543
|
+
if (field.min !== undefined) {
|
|
544
|
+
if (field.exclusiveMin) {
|
|
545
|
+
base.exclusiveMinimum = field.min
|
|
546
|
+
if (field.min === 0) base.exclusiveMinimum = 0
|
|
547
|
+
} else {
|
|
548
|
+
base.minimum = field.min
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
if (field.max !== undefined) base.maximum = field.max
|
|
552
|
+
if (field.step === 1) base.type = "integer"
|
|
553
|
+
break
|
|
554
|
+
case "enum":
|
|
555
|
+
base.type = "string"
|
|
556
|
+
base.enum = [...(field.options ?? [])]
|
|
557
|
+
break
|
|
558
|
+
case "numberList":
|
|
559
|
+
base.type = "array"
|
|
560
|
+
base.items = { type: "integer", minimum: 1 }
|
|
561
|
+
break
|
|
562
|
+
case "stringList":
|
|
563
|
+
base.type = "array"
|
|
564
|
+
base.items = { type: "string" }
|
|
565
|
+
break
|
|
566
|
+
case "clock":
|
|
567
|
+
base.type = "string"
|
|
568
|
+
base.pattern = "^([01]?\\d|2[0-3]):([0-5]\\d)$"
|
|
569
|
+
break
|
|
570
|
+
case "modelRef":
|
|
571
|
+
base.type = "string"
|
|
572
|
+
break
|
|
573
|
+
default:
|
|
574
|
+
base.type = "string"
|
|
575
|
+
break
|
|
576
|
+
}
|
|
577
|
+
base["x-label"] = field.label
|
|
578
|
+
if (field.description) base["x-description"] = field.description
|
|
579
|
+
if (field.required && base.type === "string") base.minLength = 1
|
|
580
|
+
base["x-ui"] = {
|
|
581
|
+
type: field.type,
|
|
582
|
+
group: field.group,
|
|
583
|
+
hot: field.hot === true,
|
|
584
|
+
secret: field.secret === true,
|
|
585
|
+
...(field.options ? { options: [...field.options] } : {}),
|
|
586
|
+
...(field.min !== undefined ? { min: field.min } : {}),
|
|
587
|
+
...(field.max !== undefined ? { max: field.max } : {}),
|
|
588
|
+
...(field.step !== undefined ? { step: field.step } : {}),
|
|
589
|
+
}
|
|
590
|
+
return base
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/** 递归生成 JSON Schema。 */
|
|
594
|
+
function jsonSchemaNode(node: SchemaNode): Record<string, unknown> {
|
|
595
|
+
const properties: Record<string, unknown> = {}
|
|
596
|
+
const required: string[] = []
|
|
597
|
+
for (const [key, child] of node.children) {
|
|
598
|
+
if (child.field) {
|
|
599
|
+
properties[key] = jsonSchemaFor(child.field)
|
|
600
|
+
if (child.field.required) required.push(key)
|
|
601
|
+
continue
|
|
602
|
+
}
|
|
603
|
+
properties[key] = jsonSchemaNode(child)
|
|
604
|
+
}
|
|
605
|
+
return required.length > 0
|
|
606
|
+
? { type: "object", properties, required }
|
|
607
|
+
: { type: "object", properties }
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* 生成 JSON Schema:前端用它做校验,规则只有一份。
|
|
612
|
+
*
|
|
613
|
+
* 面板用 ajv 直接校验这份 schema,不在前端另写一套 min/max/enum 判断。
|
|
614
|
+
*/
|
|
615
|
+
export function configJsonSchema(): Record<string, unknown> {
|
|
616
|
+
return {
|
|
617
|
+
$schema: "http://json-schema.org/draft-07/schema#",
|
|
618
|
+
title: "opencode-qq-bot 配置",
|
|
619
|
+
...jsonSchemaNode(TREE),
|
|
620
|
+
additionalProperties: true,
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/** 判定配置里某个字段是否敏感。 */
|
|
625
|
+
export function isSecretField(path: string): boolean {
|
|
626
|
+
return CONFIG_FIELDS.find((field) => field.path === path)?.secret === true
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* 必填项能不能被删掉。
|
|
631
|
+
*
|
|
632
|
+
* 值为 `null` 表示删键;必填项删掉之后配置文件就装不起来,
|
|
633
|
+
* 所以这里直接给出原因,由调用方拒绝这次改动。
|
|
634
|
+
*
|
|
635
|
+
* @returns 出问题的说明;没问题时返回 undefined。
|
|
636
|
+
*/
|
|
637
|
+
export function requiredFieldProblem(path: string, value: unknown): string | undefined {
|
|
638
|
+
if (value !== null) return undefined
|
|
639
|
+
const field = CONFIG_FIELDS.find((entry) => entry.path === path)
|
|
640
|
+
if (!field?.required) return undefined
|
|
641
|
+
return `${field.label}(${path})必填,不能删除`
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/** 日志级别与日记推送方式的取值集合,供外部复用。 */
|
|
645
|
+
export const LOG_LEVELS: readonly LogLevel[] = ["debug", "info", "warn", "error"]
|
|
646
|
+
export const DIARY_PUSH_MODES: readonly DiaryPush[] = ["owner", "group", "none"]
|
|
647
|
+
export const EVENT_CATEGORIES: readonly string[] = ["message", "notice"]
|
|
648
|
+
|
|
649
|
+
/** 准入配置的默认值。 */
|
|
650
|
+
export const DEFAULT_ACCESS: AccessConfig = {
|
|
651
|
+
allow: { groups: [], users: [] },
|
|
652
|
+
deny: { groups: [], users: [] },
|
|
653
|
+
allowAllWhenEmpty: false,
|
|
654
|
+
}
|