@cmach_socket/opencode-qq-bot 0.6.4 → 0.6.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmach_socket/opencode-qq-bot",
3
- "version": "0.6.4",
3
+ "version": "0.6.6",
4
4
  "description": "OpenCode V2 plugin bridging a QQ group chat (NapCat via NapLink) to an OpenCode session.",
5
5
  "repository": {
6
6
  "type": "git",
package/src/cleanup.ts ADDED
@@ -0,0 +1,74 @@
1
+ import { runUnload, type UnloadLogger, type UnloadStep } from "./unload"
2
+
3
+ /**
4
+ * 卸载步骤的执行顺序。
5
+ *
6
+ * 数字小的先执行。交接在途状态必须最先做,接管方才能拿到上一轮的状态;
7
+ * 断开 NapCat 必须早于释放机器级锁,避免新实例接管时出现两条连接。
8
+ */
9
+ export const CleanupOrder = {
10
+ handover: 10,
11
+ keepAlive: 20,
12
+ events: 30,
13
+ manager: 40,
14
+ persist: 50,
15
+ souls: 60,
16
+ tools: 70,
17
+ tui: 80,
18
+ labeler: 90,
19
+ guards: 100,
20
+ hooks: 110,
21
+ napcat: 120,
22
+ lock: 130,
23
+ lease: 140,
24
+ logs: 150,
25
+ } as const
26
+
27
+ /**
28
+ * 卸载步骤的收集器。
29
+ *
30
+ * 插件初始化要花一小段时间,这期间新实例可能已经加载并要求旧实例卸载。
31
+ * 清理函数必须那时就已经可用,所以每建好一样资源就登记一步,
32
+ * 而不是等初始化结束再拼一个固定的步骤列表。
33
+ */
34
+ export class Cleanup {
35
+ private readonly steps: Array<UnloadStep & { order: number }> = []
36
+ private started = false
37
+
38
+ /**
39
+ * 登记一步清理。
40
+ *
41
+ * @param name 步骤名称,失败时写进日志。
42
+ * @param run 步骤动作。
43
+ * @param order 执行顺序,数字小的先执行。
44
+ */
45
+ add(name: string, run: () => Promise<void> | void, order: number): void {
46
+ // 清理已经跑完又登记了新步骤:这一步当场释放,别把资源留在原地。
47
+ if (this.started) {
48
+ void Promise.resolve()
49
+ .then(run)
50
+ .catch(() => {})
51
+ return
52
+ }
53
+ this.steps.push({ name, run, order })
54
+ }
55
+
56
+ /** 是否已经执行过。 */
57
+ get done(): boolean {
58
+ return this.started
59
+ }
60
+
61
+ /**
62
+ * 执行全部清理。
63
+ *
64
+ * 每一步单独兜底:一步失败不能挡住后面的步骤。
65
+ *
66
+ * @param logger 日志器,用来记录失败。
67
+ */
68
+ async run(logger: UnloadLogger): Promise<void> {
69
+ if (this.started) return
70
+ this.started = true
71
+ const steps = [...this.steps].sort((left, right) => left.order - right.order)
72
+ await runUnload(steps, logger)
73
+ }
74
+ }