@cmach_socket/opencode-qq-bot 0.8.0 → 0.9.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.
@@ -0,0 +1,161 @@
1
+ import { applyHotFields, resolveConfig, type ConfigWarning, type QQBotConfig } from "../config"
2
+ import { CONFIG_FIELDS } from "../config/fields"
3
+ import { requiredFieldProblem } from "../config/registry"
4
+ import type { Logger } from "../logger"
5
+ import type { ConfigPatch, ConfigStore } from "./store"
6
+
7
+ /**
8
+ * 生效中的配置:文件内容、内存配置与热更新都归它管。
9
+ *
10
+ * 配置的唯一来源是 `config.json`:外部改了文件会被监听到并热更新,
11
+ * 面板保存也走同一条路径,落盘的就是权威值。
12
+ */
13
+
14
+ /** 配置变化的回调。 */
15
+ export type ConfigListener = (config: QQBotConfig, raw: Record<string, unknown>) => void
16
+
17
+ /** 配置控制器的依赖。 */
18
+ export interface ConfigControllerOptions {
19
+ /** 配置文件读写器。 */
20
+ store: ConfigStore
21
+ /** `opencode.json` 里的 agent。 */
22
+ agent?: string
23
+ /** 告警回调。 */
24
+ warn?: ConfigWarning
25
+ /** 日志器;没有单独的告警回调时把告警写进日志。 */
26
+ logger?: Logger
27
+ }
28
+
29
+ /** 保存后的结果。 */
30
+ export interface ConfigUpdateResult {
31
+ /** 更新后的生效配置。 */
32
+ config: QQBotConfig
33
+ /** 这次改动里需要重启才能生效的字段路径。 */
34
+ restartRequired: string[]
35
+ }
36
+
37
+ export class ConfigController {
38
+ /** 配置文件里的原始内容。 */
39
+ private raw: Record<string, unknown> = {}
40
+ /** 生效中的配置对象:热更新只改它的叶子值,对象身份保持不变。 */
41
+ private config: QQBotConfig | undefined
42
+ private readonly listeners = new Set<ConfigListener>()
43
+ private stopWatching: (() => void) | undefined
44
+
45
+ constructor(private readonly options: ConfigControllerOptions) {}
46
+
47
+ /** 输出一条配置告警:优先用注入的回调,否则写日志。 */
48
+ private warn(message: string): void {
49
+ if (this.options.warn) {
50
+ this.options.warn(message)
51
+ return
52
+ }
53
+ this.options.logger?.warn(message)
54
+ }
55
+
56
+ /** 生效中的配置;尚未加载时是 undefined。 */
57
+ get current(): QQBotConfig | undefined {
58
+ return this.config
59
+ }
60
+
61
+ /** 配置文件里的原始内容。 */
62
+ get file(): Record<string, unknown> {
63
+ return this.raw
64
+ }
65
+
66
+ /**
67
+ * 读取配置文件并解析成生效配置。
68
+ *
69
+ * @throws 配置文件不存在或内容非法时抛出:配置来源只有一个,缺文件不能静默按默认值跑。
70
+ */
71
+ async load(): Promise<QQBotConfig> {
72
+ this.raw = await this.options.store.read()
73
+ this.config = this.resolve(this.raw)
74
+ return this.config
75
+ }
76
+
77
+ /** 解析文件内容;非法值告警并回退默认值。 */
78
+ private resolve(raw: Record<string, unknown>): QQBotConfig {
79
+ return resolveConfig(raw, this.options.agent, (message) => this.warn(message))
80
+ }
81
+
82
+ /**
83
+ * 应用稀疏补丁并落盘。
84
+ *
85
+ * 只改动补丁里出现的路径;值为 `null` 表示删掉这个键,回到默认值。
86
+ * 落盘的是权威值,下次启动仍然生效。
87
+ *
88
+ * @throws 补丁要删掉必填项,或改完之后配置解析不出来时抛出;此时文件保持原样。
89
+ */
90
+ async applyPatch(patch: ConfigPatch): Promise<ConfigUpdateResult> {
91
+ const live = this.config
92
+ if (!live) throw new Error("配置尚未加载")
93
+ for (const [path, value] of Object.entries(patch)) {
94
+ const problem = requiredFieldProblem(path, value)
95
+ if (problem) throw new Error(problem)
96
+ }
97
+ const raw = await this.options.store.patch(patch)
98
+ // 先解析再落盘:配置装不起来时不能把文件改坏,旧配置继续用着。
99
+ const next = this.resolve(raw)
100
+ await this.options.store.write(raw)
101
+ this.raw = raw
102
+ applyHotFields(live, next)
103
+ this.config = live
104
+ this.notify()
105
+ return {
106
+ config: live,
107
+ restartRequired: Object.keys(patch).filter((path) => !isHotField(path)),
108
+ }
109
+ }
110
+
111
+ /**
112
+ * 监听配置文件的外部改动。
113
+ *
114
+ * 改动合法就热更新并通知;不合法只告警,继续用上一份配置。
115
+ */
116
+ async startWatching(): Promise<void> {
117
+ if (this.stopWatching) return
118
+ this.stopWatching = await this.options.store.watch((raw) => {
119
+ if (JSON.stringify(raw) === JSON.stringify(this.raw)) return
120
+ const live = this.config
121
+ if (!live) return
122
+ let next: QQBotConfig
123
+ try {
124
+ next = this.resolve(raw)
125
+ } catch (error) {
126
+ this.warn(
127
+ `配置文件改动后无法解析,继续沿用上一份配置:${error instanceof Error ? error.message : String(error)}`,
128
+ )
129
+ return
130
+ }
131
+ this.raw = raw
132
+ applyHotFields(live, next)
133
+ this.config = live
134
+ this.notify()
135
+ })
136
+ }
137
+
138
+ /** 停止监听。 */
139
+ stop(): void {
140
+ this.stopWatching?.()
141
+ this.stopWatching = undefined
142
+ }
143
+
144
+ /** 订阅配置变化。 */
145
+ onChange(listener: ConfigListener): () => void {
146
+ this.listeners.add(listener)
147
+ return () => this.listeners.delete(listener)
148
+ }
149
+
150
+ /** 通知订阅者。 */
151
+ private notify(): void {
152
+ const live = this.config
153
+ if (!live) return
154
+ for (const listener of this.listeners) listener(live, this.raw)
155
+ }
156
+ }
157
+
158
+ /** 字段是不是热更新:热字段保存后立即生效,不必重启。 */
159
+ export function isHotField(path: string): boolean {
160
+ return CONFIG_FIELDS.find((field) => field.path === path)?.hot === true
161
+ }