@cmach_socket/opencode-qq-bot 2.0.2 → 2.2.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.
package/README.md CHANGED
@@ -50,7 +50,7 @@ opencode plugin add @cmach_socket/opencode-qq-bot
50
50
  模板见 [`config.example.json`](config.example.json);全部字段、默认值与取值限制见
51
51
  [docs/guide/configuration.md](docs/guide/configuration.md)。
52
52
 
53
- 文件不存在时插件首次启动会自己生成一份默认配置,改掉 `ownerQQ` 与 `access.allow` 就能用。
53
+ 文件不存在时插件首次启动会自己生成一份最小模板,填上 `ownerQQ`、`napcat.url` 与 `access.allow` 就能用。
54
54
 
55
55
  ## 文档
56
56
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmach_socket/opencode-qq-bot",
3
- "version": "2.0.2",
3
+ "version": "2.2.0",
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",
@@ -34,6 +34,7 @@
34
34
  "format": "prettier --write .",
35
35
  "format:check": "prettier --check .",
36
36
  "test": "vitest run",
37
+ "audit:prod": "bun audit --production --audit-level=high --ignore=GHSA-25mm-r35j-6hgm --ignore=GHSA-mhqm-9wj8-w7r4 --ignore=GHSA-jmj2-9c5q-vw73 --ignore=GHSA-hqgx-p3r3-2cfg --ignore=GHSA-gvrf-jx37-rjv5 --ignore=GHSA-6wwr-g865-f46r --ignore=GHSA-4pf9-5fpx-xmr6 --ignore=GHSA-3wwj-8j6r-q339 --ignore=GHSA-98v3-7xjr-fg83 --ignore=GHSA-wpwg-r92p-fjjh --ignore=GHSA-rgjp-p5j7-w36x --ignore=GHSA-qv2w-fc3h-pm27 --ignore=GHSA-mpg7-vg3p-wc3m --ignore=GHSA-ffhw-4r4q-5cwv --ignore=GHSA-7c32-79xj-j3jp --ignore=GHSA-72fp-85cc-g945",
37
38
  "web:build": "bun run --cwd web build",
38
39
  "web:dev": "bun run --cwd web dev",
39
40
  "web:check": "bun run --cwd web check",
@@ -52,35 +53,35 @@
52
53
  "license": "CC-BY-NC-SA-4.0",
53
54
  "dependencies": {
54
55
  "@bybrave/proper-lockfile2": "5.1.0",
55
- "@hono/node-server": "2.1.7",
56
- "@naplink/naplink": "1.2.0",
57
- "@opencode/plugin": "2.2.5",
58
- "@opencode/schema": "2.2.5",
56
+ "@hono/node-server": "2.1.11",
57
+ "@naplink/naplink": "1.3.0",
58
+ "@opencode/plugin": "2.4.3",
59
+ "@opencode/schema": "2.4.3",
59
60
  "async-mutex": "0.5.0",
60
61
  "chokidar": "6.0.0",
61
- "flare-redact": "1.6.1",
62
- "hono": "4.13.18",
62
+ "flare-redact": "1.13.1",
63
+ "hono": "4.13.23",
63
64
  "ipaddr.js": "2.5.0",
64
- "remove-markdown": "0.8.0",
65
- "request-filtering-agent": "3.2.1",
65
+ "remove-markdown": "0.8.3",
66
+ "request-filtering-agent": "3.3.1",
66
67
  "winston": "3.19.0",
67
68
  "winston-daily-rotate-file": "5.0.0",
68
69
  "write-file-atomic": "8.0.0",
69
70
  "zod": "4.6.7"
70
71
  },
71
72
  "devDependencies": {
72
- "@sveltejs/vite-plugin-svelte": "7.3.1",
73
- "@testing-library/svelte": "5.2.8",
74
- "@types/bun": "1.4.4",
73
+ "@sveltejs/vite-plugin-svelte": "7.3.2",
74
+ "@testing-library/svelte": "5.4.2",
75
+ "@types/bun": "1.4.5",
75
76
  "@types/write-file-atomic": "4.0.3",
76
- "@vitest/coverage-v8": "5.1.0",
77
- "eslint": "10.14.0",
78
- "jsdom": "^31.0.0",
79
- "prettier": "3.9.10",
80
- "svelte": "5.57.3",
77
+ "@vitest/coverage-v8": "5.1.2",
78
+ "eslint": "10.16.0",
79
+ "jsdom": "^31.0.1",
80
+ "prettier": "3.10.1",
81
+ "svelte": "5.59.2",
81
82
  "typescript": "6.0.3",
82
- "typescript-eslint": "8.72.1",
83
- "vitest": "5.1.0"
83
+ "typescript-eslint": "8.74.0",
84
+ "vitest": "5.1.2"
84
85
  },
85
86
  "overrides": {
86
87
  "cross-spawn": "7.1.2"
package/src/cleanup.ts CHANGED
@@ -11,6 +11,8 @@ export const CleanupOrder = {
11
11
  keepAlive: 20,
12
12
  events: 30,
13
13
  panel: 35,
14
+ /** 撤销共享运行时:要早于会话管理器销毁,其它实例才不会用到死对象。 */
15
+ runtime: 38,
14
16
  manager: 40,
15
17
  persist: 50,
16
18
  souls: 60,
@@ -4,18 +4,33 @@
4
4
  * 配置文件是唯一来源:缺文件时先落一份默认配置,主人改掉需要的那几项即可,
5
5
  * 不然插件只能拒绝启动。
6
6
  */
7
- import { DEFAULT_CONFIG } from "./registry"
8
7
 
9
8
  /** 新装时写进配置文件的 NapCat 地址:NapCat 默认监听的 WebSocket 端口。 */
10
9
  export const INITIAL_NAPCAT_URL = "ws://127.0.0.1:3001"
11
10
 
11
+ /**
12
+ * 配置文件里始终保留的路径。
13
+ *
14
+ * 这几项是主人上手要填的:留在文件里,配置文件自己就说明了该改哪几处。
15
+ */
16
+ export const TEMPLATE_PATHS: readonly string[] = [
17
+ "ownerQQ",
18
+ "napcat.url",
19
+ "access.allow.groups",
20
+ "access.allow.users",
21
+ ]
22
+
12
23
  /**
13
24
  * 生成初始配置内容。
14
25
  *
15
- * 写的是全部默认项加上必填的 `napcat.url` 占位;
16
- * 每次调用都返回新对象,调用方改它不会影响注册表里的默认值。
26
+ * 只写主人要填的几项,其余交给注册表里的默认值:
27
+ * 默认值以后调整,已经装好的机器也得跟着变。
28
+ * `ownerQQ` 写 `null` 表示「还没填」,解析时按未配置处理。
17
29
  */
18
30
  export function initialConfig(): Record<string, unknown> {
19
- const defaults = structuredClone(DEFAULT_CONFIG) as Record<string, unknown>
20
- return { ...defaults, napcat: { url: INITIAL_NAPCAT_URL } }
31
+ return {
32
+ ownerQQ: null,
33
+ napcat: { url: INITIAL_NAPCAT_URL },
34
+ access: { allow: { groups: [], users: [] } },
35
+ }
21
36
  }
@@ -1,8 +1,8 @@
1
1
  import { randomBytes } from "node:crypto"
2
2
  import { applyHotFields, resolveConfig, type ConfigWarning, type QQBotConfig } from "../config"
3
3
  import { CONFIG_FIELDS } from "../config/fields"
4
- import { requiredFieldProblem } from "../config/registry"
5
- import { initialConfig } from "./initial"
4
+ import { pruneDefaultEntries, requiredFieldProblem } from "../config/registry"
5
+ import { initialConfig, TEMPLATE_PATHS } from "./initial"
6
6
  import type { Logger } from "../logger"
7
7
  import { ConfigMissingError, type ConfigPatch, type ConfigStore } from "./store"
8
8
 
@@ -74,11 +74,32 @@ export class ConfigController {
74
74
  */
75
75
  async load(): Promise<QQBotConfig> {
76
76
  this.raw = await this.readOrCreate()
77
+ this.raw = await this.pruneDefaults(this.raw)
77
78
  this.config = this.resolve(this.raw)
78
79
  await this.ensureSecrets()
79
80
  return this.config
80
81
  }
81
82
 
83
+ /**
84
+ * 把配置文件里与默认值相同的项删掉并落盘。
85
+ *
86
+ * 配置文件只留主人改过的值:默认值以后调整,已经装好的机器也跟着变。
87
+ * 写不进去只告警:这一份配置照常用,只是默认值暂时传不过来。
88
+ */
89
+ private async pruneDefaults(raw: Record<string, unknown>): Promise<Record<string, unknown>> {
90
+ const pruned = pruneDefaultEntries(raw, TEMPLATE_PATHS)
91
+ if (JSON.stringify(pruned) === JSON.stringify(raw)) return raw
92
+ try {
93
+ await this.options.store.write(pruned)
94
+ } catch (error) {
95
+ this.warn(
96
+ `配置文件精简失败,继续沿用原文件:${error instanceof Error ? error.message : String(error)}`,
97
+ )
98
+ return raw
99
+ }
100
+ return pruned
101
+ }
102
+
82
103
  /**
83
104
  * 读配置文件;文件不存在时先生成一份默认配置。
84
105
  *
@@ -462,6 +462,60 @@ export function warnUnknownFileKeys(raw: Record<string, unknown>, warn: ConfigWa
462
462
  walk(raw, "")
463
463
  }
464
464
 
465
+ /**
466
+ * 删掉配置文件里与默认值相同的项。
467
+ *
468
+ * 配置文件只保留主人改过的值:默认值以后调整,已经装好的机器也跟着变。
469
+ * `keep` 里的路径原样留下,首次启动生成的模板就靠它们提示主人该填哪几项。
470
+ */
471
+ export function pruneDefaultEntries(
472
+ raw: Record<string, unknown>,
473
+ keep: readonly string[] = [],
474
+ ): Record<string, unknown> {
475
+ const kept = new Set(keep)
476
+ const result = structuredClone(raw) as Record<string, unknown>
477
+ for (const field of CONFIG_FIELDS) {
478
+ if (field.default === undefined) continue
479
+ if (kept.has(field.path)) continue
480
+ const value = getConfigValue(result, field.path)
481
+ if (value === undefined) continue
482
+ if (!sameValue(value, field.default)) continue
483
+ deletePath(result, field.path)
484
+ }
485
+ return result
486
+ }
487
+
488
+ /** 值是否与默认值相同:数组逐项比较,其余按引用比。 */
489
+ function sameValue(left: unknown, right: unknown): boolean {
490
+ if (left === right) return true
491
+ if (!Array.isArray(left) || !Array.isArray(right)) return false
492
+ return left.length === right.length && left.every((item, index) => sameValue(item, right[index]))
493
+ }
494
+
495
+ /** 删掉一个路径,顺手清掉因此变空的上层对象。 */
496
+ function deletePath(target: Record<string, unknown>, path: string): void {
497
+ const segments = path.split(".")
498
+ const parents: Array<{ parent: Record<string, unknown>; key: string }> = []
499
+ let current = target
500
+ for (const segment of segments.slice(0, -1)) {
501
+ const child = current[segment]
502
+ if (!child || typeof child !== "object" || Array.isArray(child)) return
503
+ parents.push({ parent: current, key: segment })
504
+ current = child as Record<string, unknown>
505
+ }
506
+ const leaf = segments[segments.length - 1]
507
+ if (leaf === undefined) return
508
+ delete current[leaf]
509
+ for (let index = parents.length - 1; index >= 0; index -= 1) {
510
+ const entry = parents[index]
511
+ if (!entry) continue
512
+ const child = entry.parent[entry.key]
513
+ if (!child || typeof child !== "object" || Array.isArray(child)) break
514
+ if (Object.keys(child).length > 0) break
515
+ delete entry.parent[entry.key]
516
+ }
517
+ }
518
+
465
519
  /** 注册表里声明为热更新的字段。 */
466
520
  export const HOT_FIELDS: readonly ConfigField[] = CONFIG_FIELDS.filter((field) => field.hot)
467
521