@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.
Files changed (39) hide show
  1. package/NOTICE +48 -15
  2. package/README.md +130 -398
  3. package/config.example.json +12 -0
  4. package/package.json +11 -1
  5. package/src/cleanup.ts +1 -0
  6. package/src/config/fields.ts +780 -0
  7. package/src/config/live.ts +189 -0
  8. package/src/config/registry.ts +654 -0
  9. package/src/config/store.ts +175 -0
  10. package/src/config/types.ts +253 -0
  11. package/src/config.ts +77 -1023
  12. package/src/diary/scheduler.ts +39 -1
  13. package/src/emoji/collection.ts +289 -0
  14. package/src/emoji/favorite-index.ts +40 -3
  15. package/src/emoji/favorites.ts +7 -1
  16. package/src/home.ts +11 -16
  17. package/src/index.ts +117 -25
  18. package/src/logger.ts +104 -1
  19. package/src/naplink/client.ts +16 -0
  20. package/src/panel/api.ts +345 -0
  21. package/src/panel/auth.ts +65 -0
  22. package/src/panel/models.ts +56 -0
  23. package/src/panel/runtime.ts +51 -0
  24. package/src/panel/server.ts +220 -0
  25. package/src/panel/status.ts +201 -0
  26. package/src/rate-limit.ts +46 -0
  27. package/src/sensitive.ts +6 -1
  28. package/src/session/manager.ts +4 -3
  29. package/src/tools/qq.ts +83 -10
  30. package/web/dist/assets/index-7eMQEM7-.css +1 -0
  31. package/web/dist/assets/index-DCkgUY7y.js +15 -0
  32. package/web/dist/assets/inter-cyrillic-ext-wght-normal-BOeWTOD4.woff2 +0 -0
  33. package/web/dist/assets/inter-cyrillic-wght-normal-DqGufNeO.woff2 +0 -0
  34. package/web/dist/assets/inter-greek-ext-wght-normal-DlzME5K_.woff2 +0 -0
  35. package/web/dist/assets/inter-greek-wght-normal-CkhJZR-_.woff2 +0 -0
  36. package/web/dist/assets/inter-latin-ext-wght-normal-DO1Apj_S.woff2 +0 -0
  37. package/web/dist/assets/inter-latin-wght-normal-Dx4kXJAl.woff2 +0 -0
  38. package/web/dist/assets/inter-vietnamese-wght-normal-CBcvBZtf.woff2 +0 -0
  39. package/web/dist/index.html +13 -0
@@ -0,0 +1,189 @@
1
+ import { randomBytes } from "node:crypto"
2
+ import { applyHotFields, resolveConfig, type ConfigWarning, type QQBotConfig } from "../config"
3
+ import { CONFIG_FIELDS } from "../config/fields"
4
+ import { requiredFieldProblem } from "../config/registry"
5
+ import type { Logger } from "../logger"
6
+ import type { ConfigPatch, ConfigStore } from "./store"
7
+
8
+ /**
9
+ * 生效中的配置:文件内容、内存配置与热更新都归它管。
10
+ *
11
+ * 配置的唯一来源是 `config.json`:外部改了文件会被监听到并热更新,
12
+ * 面板保存也走同一条路径,落盘的就是权威值。
13
+ */
14
+
15
+ /** 配置变化的回调。 */
16
+ export type ConfigListener = (config: QQBotConfig, raw: Record<string, unknown>) => void
17
+
18
+ /** 配置控制器的依赖。 */
19
+ export interface ConfigControllerOptions {
20
+ /** 配置文件读写器。 */
21
+ store: ConfigStore
22
+ /** `opencode.json` 里的 agent。 */
23
+ agent?: string
24
+ /** 告警回调。 */
25
+ warn?: ConfigWarning
26
+ /** 日志器;没有单独的告警回调时把告警写进日志。 */
27
+ logger?: Logger
28
+ }
29
+
30
+ /** 保存后的结果。 */
31
+ export interface ConfigUpdateResult {
32
+ /** 更新后的生效配置。 */
33
+ config: QQBotConfig
34
+ /** 这次改动里需要重启才能生效的字段路径。 */
35
+ restartRequired: string[]
36
+ }
37
+
38
+ export class ConfigController {
39
+ /** 配置文件里的原始内容。 */
40
+ private raw: Record<string, unknown> = {}
41
+ /** 生效中的配置对象:热更新只改它的叶子值,对象身份保持不变。 */
42
+ private config: QQBotConfig | undefined
43
+ private readonly listeners = new Set<ConfigListener>()
44
+ private stopWatching: (() => void) | undefined
45
+
46
+ constructor(private readonly options: ConfigControllerOptions) {}
47
+
48
+ /** 输出一条配置告警:优先用注入的回调,否则写日志。 */
49
+ private warn(message: string): void {
50
+ if (this.options.warn) {
51
+ this.options.warn(message)
52
+ return
53
+ }
54
+ this.options.logger?.warn(message)
55
+ }
56
+
57
+ /** 生效中的配置;尚未加载时是 undefined。 */
58
+ get current(): QQBotConfig | undefined {
59
+ return this.config
60
+ }
61
+
62
+ /** 配置文件里的原始内容。 */
63
+ get file(): Record<string, unknown> {
64
+ return this.raw
65
+ }
66
+
67
+ /**
68
+ * 读取配置文件并解析成生效配置。
69
+ *
70
+ * @throws 配置文件不存在或内容非法时抛出:配置来源只有一个,缺文件不能静默按默认值跑。
71
+ */
72
+ async load(): Promise<QQBotConfig> {
73
+ this.raw = await this.options.store.read()
74
+ this.config = this.resolve(this.raw)
75
+ await this.ensureSecrets()
76
+ return this.config
77
+ }
78
+
79
+ /**
80
+ * 补齐缺失的随机凭据。
81
+ *
82
+ * 面板开着却没有令牌时生成一个并写回配置文件:令牌要留在文件里,
83
+ * 重启后不变,面板也才有得登录。写不进去只告警,不影响机器人本身。
84
+ */
85
+ private async ensureSecrets(): Promise<void> {
86
+ const live = this.config
87
+ if (!live?.panel.enabled) return
88
+ if (live.panel.token) {
89
+ if (live.panel.token.length < 16) this.warn("面板访问令牌太短,建议至少 16 个字符")
90
+ return
91
+ }
92
+ const token = randomBytes(32).toString("hex")
93
+ try {
94
+ await this.applyPatch({ "panel.token": token })
95
+ this.warn("面板访问令牌已生成,请查看 config.json 里的 panel.token")
96
+ } catch (error) {
97
+ this.warn(`面板访问令牌生成失败:${error instanceof Error ? error.message : String(error)}`)
98
+ }
99
+ }
100
+
101
+ /** 解析文件内容;非法值告警并回退默认值。 */
102
+ private resolve(raw: Record<string, unknown>): QQBotConfig {
103
+ return resolveConfig(raw, this.options.agent, (message) => this.warn(message))
104
+ }
105
+
106
+ /**
107
+ * 应用稀疏补丁并落盘。
108
+ *
109
+ * 只改动补丁里出现的路径;值为 `null` 表示删掉这个键,回到默认值。
110
+ * 落盘的是权威值,下次启动仍然生效。
111
+ *
112
+ * @throws 补丁要删掉必填项,或改完之后配置解析不出来时抛出;此时文件保持原样。
113
+ */
114
+ async applyPatch(patch: ConfigPatch): Promise<ConfigUpdateResult> {
115
+ const live = this.config
116
+ if (!live) throw new Error("配置尚未加载")
117
+ for (const [path, value] of Object.entries(patch)) {
118
+ const problem = requiredFieldProblem(path, value)
119
+ if (problem) throw new Error(problem)
120
+ }
121
+ const raw = await this.options.store.patch(patch)
122
+ // 先解析再落盘:配置装不起来时不能把文件改坏,旧配置继续用着。
123
+ const next = this.resolve(raw)
124
+ await this.options.store.write(raw)
125
+ this.raw = raw
126
+ applyHotFields(live, next)
127
+ this.config = live
128
+ this.notify()
129
+ // 面板刚被打开时补一个令牌:补完会再通知一次,订阅者这时看到的配置是完整的。
130
+ await this.ensureSecrets()
131
+ return {
132
+ config: live,
133
+ restartRequired: Object.keys(patch).filter((path) => !isHotField(path)),
134
+ }
135
+ }
136
+
137
+ /**
138
+ * 监听配置文件的外部改动。
139
+ *
140
+ * 改动合法就热更新并通知;不合法只告警,继续用上一份配置。
141
+ */
142
+ async startWatching(): Promise<void> {
143
+ if (this.stopWatching) return
144
+ this.stopWatching = await this.options.store.watch((raw) => {
145
+ if (JSON.stringify(raw) === JSON.stringify(this.raw)) return
146
+ const live = this.config
147
+ if (!live) return
148
+ let next: QQBotConfig
149
+ try {
150
+ next = this.resolve(raw)
151
+ } catch (error) {
152
+ this.warn(
153
+ `配置文件改动后无法解析,继续沿用上一份配置:${error instanceof Error ? error.message : String(error)}`,
154
+ )
155
+ return
156
+ }
157
+ this.raw = raw
158
+ applyHotFields(live, next)
159
+ this.config = live
160
+ this.notify()
161
+ // 面板刚被打开时补一个令牌:补完会再通知一次,订阅者这时看到的配置是完整的。
162
+ void this.ensureSecrets()
163
+ })
164
+ }
165
+
166
+ /** 停止监听。 */
167
+ stop(): void {
168
+ this.stopWatching?.()
169
+ this.stopWatching = undefined
170
+ }
171
+
172
+ /** 订阅配置变化。 */
173
+ onChange(listener: ConfigListener): () => void {
174
+ this.listeners.add(listener)
175
+ return () => this.listeners.delete(listener)
176
+ }
177
+
178
+ /** 通知订阅者。 */
179
+ private notify(): void {
180
+ const live = this.config
181
+ if (!live) return
182
+ for (const listener of this.listeners) listener(live, this.raw)
183
+ }
184
+ }
185
+
186
+ /** 字段是不是热更新:热字段保存后立即生效,不必重启。 */
187
+ export function isHotField(path: string): boolean {
188
+ return CONFIG_FIELDS.find((field) => field.path === path)?.hot === true
189
+ }