@cmach_socket/opencode-qq-bot 0.9.0 → 0.10.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/NOTICE +0 -1
- package/README.md +152 -494
- package/chat-overrides.example.json +3 -3
- package/config.example.json +12 -0
- package/package.json +7 -2
- package/src/chat.ts +5 -0
- package/src/commands.ts +88 -1
- package/src/config/fields.ts +48 -5
- package/src/config/live.ts +61 -3
- package/src/config/store.ts +6 -11
- package/src/config/types.ts +24 -0
- package/src/config.ts +3 -0
- package/src/diary/command.ts +13 -12
- package/src/diary/format.ts +17 -0
- package/src/diary/scheduler.ts +1 -1
- package/src/diary/summary.ts +6 -5
- package/src/emoji/collection.ts +321 -0
- package/src/emoji/favorite-index.ts +40 -3
- package/src/emoji/favorites.ts +7 -1
- package/src/fs/atomic.ts +82 -0
- package/src/home.ts +6 -2
- package/src/index.ts +41 -36
- package/src/keepalive.ts +60 -2
- package/src/known-secrets.ts +20 -0
- package/src/logger.ts +2 -1
- package/src/overrides.ts +209 -47
- package/src/panel/api.ts +61 -7
- package/src/panel/documents.ts +621 -0
- package/src/panel/server.ts +66 -25
- package/src/panel/status.ts +8 -1
- package/src/safe-object.ts +48 -0
- package/src/sensitive.ts +31 -2
- package/src/session/find.ts +3 -2
- package/src/session/manager.ts +646 -43
- package/src/session/proactive.ts +3 -1
- package/src/session/soul-guard.ts +162 -0
- package/src/session/soul.ts +78 -13
- package/src/state-lock.ts +74 -0
- package/src/tools/policy.ts +65 -24
- package/src/tools/qq.ts +103 -22
- package/web/dist/assets/index-C5lRsdbz.css +1 -0
- package/web/dist/assets/index-YqorZD0I.js +8 -0
- package/web/dist/index.html +2 -2
- package/web/dist/assets/index-7eMQEM7-.css +0 -1
- package/web/dist/assets/index-DCkgUY7y.js +0 -15
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"chats": {
|
|
3
|
-
"group:123456789": { "wake": "mention" },
|
|
4
|
-
"group:987654321": { "wake": "prefix", "
|
|
5
|
-
"private:135792468": { "proactive": false }
|
|
3
|
+
"group:123456789": { "wake": "mention", "collect.groupMs": 30000 },
|
|
4
|
+
"group:987654321": { "wake": "prefix", "wake.prefixes": ["bot,"] },
|
|
5
|
+
"private:135792468": { "proactive.enabled": false, "collect.privateMs": 5000 }
|
|
6
6
|
}
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cmach_socket/opencode-qq-bot",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.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",
|
|
@@ -23,13 +23,14 @@
|
|
|
23
23
|
"src",
|
|
24
24
|
"souls",
|
|
25
25
|
"web/dist",
|
|
26
|
+
"config.example.json",
|
|
26
27
|
"tool-profiles.example.json",
|
|
27
28
|
"chat-overrides.example.json",
|
|
28
29
|
"NOTICE",
|
|
29
30
|
"README.md"
|
|
30
31
|
],
|
|
31
32
|
"scripts": {
|
|
32
|
-
"typecheck": "tsc --noEmit",
|
|
33
|
+
"typecheck": "tsc --noEmit && tsc -p tests/web/tsconfig.json",
|
|
33
34
|
"lint": "eslint .",
|
|
34
35
|
"format": "prettier --write .",
|
|
35
36
|
"format:check": "prettier --check .",
|
|
@@ -69,11 +70,15 @@
|
|
|
69
70
|
"zod": "4.6.7"
|
|
70
71
|
},
|
|
71
72
|
"devDependencies": {
|
|
73
|
+
"@sveltejs/vite-plugin-svelte": "7.3.1",
|
|
74
|
+
"@testing-library/svelte": "5.2.8",
|
|
72
75
|
"@types/bun": "1.4.4",
|
|
73
76
|
"@types/write-file-atomic": "4.0.3",
|
|
74
77
|
"@vitest/coverage-v8": "5.1.0",
|
|
75
78
|
"eslint": "10.14.0",
|
|
79
|
+
"jsdom": "^31.0.0",
|
|
76
80
|
"prettier": "3.9.10",
|
|
81
|
+
"svelte": "5.57.3",
|
|
77
82
|
"typescript": "6.0.3",
|
|
78
83
|
"typescript-eslint": "8.72.1",
|
|
79
84
|
"vitest": "5.1.0"
|
package/src/chat.ts
CHANGED
|
@@ -11,6 +11,11 @@ export function targetLabel(target: ChatTarget): string {
|
|
|
11
11
|
return target.kind === "group" ? `群 ${target.id}` : `私聊 ${target.id}`
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
/** 机器人给目标建的会话标题;老版本只有群聊会话,标题就是这一种。 */
|
|
15
|
+
export function targetSessionTitle(target: ChatTarget): string {
|
|
16
|
+
return target.kind === "group" ? `QQ 群 ${target.id}` : `QQ 私聊 ${target.id}`
|
|
17
|
+
}
|
|
18
|
+
|
|
14
19
|
/** 解析持久化键,仅接受 `group:<id>` 与 `private:<id>`。 */
|
|
15
20
|
export function parseTargetKey(key: string): ChatTarget | undefined {
|
|
16
21
|
const separator = key.indexOf(":")
|
package/src/commands.ts
CHANGED
|
@@ -105,6 +105,7 @@ function sessionLine(manager: SessionManager, target: ChatTarget): string {
|
|
|
105
105
|
parts.push(entry.busy ? "执行中" : "空闲")
|
|
106
106
|
if (entry.buffered > 0) parts.push(`缓存 ${entry.buffered} 条`)
|
|
107
107
|
if (entry.pending) parts.push("有待重试批次")
|
|
108
|
+
if (entry.collectUntil !== undefined) parts.push("收集消息中")
|
|
108
109
|
return `${targetLabel(target)}:${parts.join(",")}`
|
|
109
110
|
}
|
|
110
111
|
|
|
@@ -137,7 +138,9 @@ async function runStatus(context: CommandContext): Promise<string> {
|
|
|
137
138
|
const lines: string[] = []
|
|
138
139
|
for (const target of targets) {
|
|
139
140
|
const override = await overrides.for(target)
|
|
140
|
-
lines.push(
|
|
141
|
+
lines.push(
|
|
142
|
+
`${sessionLine(manager, target)},${describeOverride(override, target, config.collect)}`,
|
|
143
|
+
)
|
|
141
144
|
}
|
|
142
145
|
return ["QQ 机器人状态", ...lines].join("\n")
|
|
143
146
|
}
|
|
@@ -172,6 +175,72 @@ async function runReset(context: CommandContext, args: string[]): Promise<string
|
|
|
172
175
|
return lines.join("\n")
|
|
173
176
|
}
|
|
174
177
|
|
|
178
|
+
/** `/stop` 的单条回执:说清打断结果与丢掉的排队消息。 */
|
|
179
|
+
function stopLines(
|
|
180
|
+
target: ChatTarget,
|
|
181
|
+
result: { interrupted: boolean; dropped: number; droppedPending: boolean },
|
|
182
|
+
): string[] {
|
|
183
|
+
const idle = !result.interrupted && result.dropped === 0 && !result.droppedPending
|
|
184
|
+
const lines = [`${targetLabel(target)} ${idle ? "没有正在执行的回合" : "已停止"}`]
|
|
185
|
+
if (!idle) lines.push(result.interrupted ? "原回合已打断" : "原回合没有在执行")
|
|
186
|
+
if (result.dropped > 0) lines.push(`丢弃 ${result.dropped} 条排队消息`)
|
|
187
|
+
if (result.droppedPending) lines.push("丢弃一批待重试的消息")
|
|
188
|
+
return lines
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async function runStop(context: CommandContext, args: string[]): Promise<string> {
|
|
192
|
+
const { config, manager } = context
|
|
193
|
+
const requested = args[0]
|
|
194
|
+
if (requested) {
|
|
195
|
+
const target = parseChatArg(requested)
|
|
196
|
+
if (!target) return `无法识别聊天:${requested}(示例:/stop group:123)`
|
|
197
|
+
if (!isTargetAllowed(config.access, target, config.ownerQQ)) {
|
|
198
|
+
return `${targetLabel(target)} 不在放行名单里,无法停止`
|
|
199
|
+
}
|
|
200
|
+
return stopLines(target, await manager.stopTarget(target)).join("\n")
|
|
201
|
+
}
|
|
202
|
+
// 不带参数时停掉所有正在执行的回合:这是紧急刹车,不用再问一遍。
|
|
203
|
+
// 队列里还压着消息的聊天也算在内:上游多半还在跑,只是不在本插件的轮次里。
|
|
204
|
+
const busy = manager
|
|
205
|
+
.snapshot()
|
|
206
|
+
.filter((item) => item.busy || item.buffered > 0 || item.pending)
|
|
207
|
+
.map((item) => item.target)
|
|
208
|
+
.filter((target) => isTargetAllowed(config.access, target, config.ownerQQ))
|
|
209
|
+
if (busy.length === 0) return "当前没有正在执行的回合"
|
|
210
|
+
const lines: string[] = []
|
|
211
|
+
for (const target of busy) {
|
|
212
|
+
lines.push(...stopLines(target, await manager.stopTarget(target)))
|
|
213
|
+
}
|
|
214
|
+
return lines.join("\n")
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async function runCompact(context: CommandContext, args: string[]): Promise<string> {
|
|
218
|
+
const { config, manager } = context
|
|
219
|
+
const requested = args[0]
|
|
220
|
+
if (!requested) {
|
|
221
|
+
// 压缩要额外跑一次模型,不带参数时只列清单,由人来挑。
|
|
222
|
+
const entries = manager
|
|
223
|
+
.snapshot()
|
|
224
|
+
.filter((item) => item.sessionID !== undefined)
|
|
225
|
+
.filter((item) => isTargetAllowed(config.access, item.target, config.ownerQQ))
|
|
226
|
+
if (entries.length === 0) return "还没有可压缩的会话"
|
|
227
|
+
return [
|
|
228
|
+
"要压缩哪个聊天?把目标加在命令后面:",
|
|
229
|
+
...entries.map((item) => `/compact ${targetKey(item.target)} — 会话 ${item.sessionID}`),
|
|
230
|
+
].join("\n")
|
|
231
|
+
}
|
|
232
|
+
const target = parseChatArg(requested)
|
|
233
|
+
if (!target) return `无法识别聊天:${requested}(示例:/compact group:123)`
|
|
234
|
+
if (!isTargetAllowed(config.access, target, config.ownerQQ)) {
|
|
235
|
+
return `${targetLabel(target)} 不在放行名单里,无法压缩`
|
|
236
|
+
}
|
|
237
|
+
const result = await manager.compactTarget(target)
|
|
238
|
+
if (result.compacted) return `${targetLabel(target)} 已提交压缩请求,上游会压缩这段上下文`
|
|
239
|
+
if (result.reason === "no-session") return `${targetLabel(target)} 还没有会话,无法压缩`
|
|
240
|
+
if (result.reason === "busy") return `${targetLabel(target)} 正在执行,先 /stop 再压缩`
|
|
241
|
+
return `${targetLabel(target)} 压缩失败,详见日志`
|
|
242
|
+
}
|
|
243
|
+
|
|
175
244
|
async function runTools(context: CommandContext, args: string[]): Promise<string> {
|
|
176
245
|
const { config, policy, toolNames } = context
|
|
177
246
|
const requested = args[0]
|
|
@@ -220,6 +289,24 @@ export const COMMANDS: readonly CommandDefinition[] = [
|
|
|
220
289
|
usage: ["/reset — 列出所有聊天", "/reset group:123 — 重置指定聊天(也支持 private:123)"],
|
|
221
290
|
run: runReset,
|
|
222
291
|
},
|
|
292
|
+
{
|
|
293
|
+
name: "stop",
|
|
294
|
+
description: "打断执行中的回合并丢掉排队消息,保留会话与历史",
|
|
295
|
+
usage: [
|
|
296
|
+
"/stop — 打断所有正在执行的回合",
|
|
297
|
+
"/stop group:123 — 打断指定聊天的回合(也支持 private:123)",
|
|
298
|
+
],
|
|
299
|
+
run: runStop,
|
|
300
|
+
},
|
|
301
|
+
{
|
|
302
|
+
name: "compact",
|
|
303
|
+
description: "让上游压缩会话上下文;不带参数时列出可压缩的聊天",
|
|
304
|
+
usage: [
|
|
305
|
+
"/compact — 列出可压缩的聊天",
|
|
306
|
+
"/compact group:123 — 压缩指定聊天的上下文(也支持 private:123)",
|
|
307
|
+
],
|
|
308
|
+
run: runCompact,
|
|
309
|
+
},
|
|
223
310
|
{
|
|
224
311
|
name: "tools",
|
|
225
312
|
description: "查看聊天使用的工具配置档",
|
package/src/config/fields.ts
CHANGED
|
@@ -65,6 +65,7 @@ export const CONFIG_GROUPS: readonly ConfigGroup[] = [
|
|
|
65
65
|
{ id: "access", label: "准入" },
|
|
66
66
|
{ id: "proactive", label: "主动说话" },
|
|
67
67
|
{ id: "wake", label: "唤醒" },
|
|
68
|
+
{ id: "collect", label: "消息收集" },
|
|
68
69
|
{ id: "reply", label: "回复" },
|
|
69
70
|
{ id: "rateLimit", label: "出站限流" },
|
|
70
71
|
{ id: "sensitive", label: "敏感拦截" },
|
|
@@ -136,7 +137,7 @@ export const CONFIG_FIELDS: readonly ConfigField[] = [
|
|
|
136
137
|
type: "boolean",
|
|
137
138
|
label: "复用上一次的会话",
|
|
138
139
|
description: "完全没有绑定时,按会话标记找回上一次的会话",
|
|
139
|
-
default:
|
|
140
|
+
default: true,
|
|
140
141
|
hot: true,
|
|
141
142
|
},
|
|
142
143
|
{
|
|
@@ -171,6 +172,14 @@ export const CONFIG_FIELDS: readonly ConfigField[] = [
|
|
|
171
172
|
description: "工作目录 souls 里的角色设定文件名",
|
|
172
173
|
default: undefined,
|
|
173
174
|
},
|
|
175
|
+
{
|
|
176
|
+
path: "soul.allowedPaths",
|
|
177
|
+
group: "general",
|
|
178
|
+
type: "stringList",
|
|
179
|
+
label: "角色设定允许目录",
|
|
180
|
+
description: "除工作目录外,还允许哪些目录里的角色设定文件生效;相对路径按工作目录解析",
|
|
181
|
+
default: [],
|
|
182
|
+
},
|
|
174
183
|
|
|
175
184
|
// NapCat
|
|
176
185
|
{
|
|
@@ -292,7 +301,7 @@ export const CONFIG_FIELDS: readonly ConfigField[] = [
|
|
|
292
301
|
group: "proactive",
|
|
293
302
|
type: "numberList",
|
|
294
303
|
label: "主动说话的群号",
|
|
295
|
-
description: "
|
|
304
|
+
description: "留空表示一个都不主动说话",
|
|
296
305
|
default: [],
|
|
297
306
|
},
|
|
298
307
|
{
|
|
@@ -300,7 +309,7 @@ export const CONFIG_FIELDS: readonly ConfigField[] = [
|
|
|
300
309
|
group: "proactive",
|
|
301
310
|
type: "numberList",
|
|
302
311
|
label: "主动说话的私聊 QQ 号",
|
|
303
|
-
description: "
|
|
312
|
+
description: "留空表示一个都不主动说话",
|
|
304
313
|
default: [],
|
|
305
314
|
},
|
|
306
315
|
|
|
@@ -342,6 +351,30 @@ export const CONFIG_FIELDS: readonly ConfigField[] = [
|
|
|
342
351
|
hot: true,
|
|
343
352
|
},
|
|
344
353
|
|
|
354
|
+
// 消息收集
|
|
355
|
+
{
|
|
356
|
+
path: "collect.privateMs",
|
|
357
|
+
group: "collect",
|
|
358
|
+
type: "number",
|
|
359
|
+
label: "私聊收集窗口(毫秒)",
|
|
360
|
+
description: "私聊收到消息后先攒一会儿再一起回复,0 表示立即回复",
|
|
361
|
+
default: 10_000,
|
|
362
|
+
min: 0,
|
|
363
|
+
step: 100,
|
|
364
|
+
hot: true,
|
|
365
|
+
},
|
|
366
|
+
{
|
|
367
|
+
path: "collect.groupMs",
|
|
368
|
+
group: "collect",
|
|
369
|
+
type: "number",
|
|
370
|
+
label: "群聊收集窗口(毫秒)",
|
|
371
|
+
description: "群聊收到消息后先攒一会儿再一起回复,0 表示立即回复",
|
|
372
|
+
default: 60_000,
|
|
373
|
+
min: 0,
|
|
374
|
+
step: 100,
|
|
375
|
+
hot: true,
|
|
376
|
+
},
|
|
377
|
+
|
|
345
378
|
// 回复
|
|
346
379
|
{
|
|
347
380
|
path: "reply.minDelayMs",
|
|
@@ -742,8 +775,8 @@ export const CONFIG_FIELDS: readonly ConfigField[] = [
|
|
|
742
775
|
group: "panel",
|
|
743
776
|
type: "boolean",
|
|
744
777
|
label: "启用状态面板",
|
|
745
|
-
description: "面板自带 HTTP
|
|
746
|
-
default:
|
|
778
|
+
description: "面板自带 HTTP 服务,默认开启",
|
|
779
|
+
default: true,
|
|
747
780
|
hot: true,
|
|
748
781
|
},
|
|
749
782
|
{
|
|
@@ -777,4 +810,14 @@ export const CONFIG_FIELDS: readonly ConfigField[] = [
|
|
|
777
810
|
secret: true,
|
|
778
811
|
hot: true,
|
|
779
812
|
},
|
|
813
|
+
{
|
|
814
|
+
path: "panel.tokenRevoked",
|
|
815
|
+
group: "panel",
|
|
816
|
+
type: "boolean",
|
|
817
|
+
label: "吊销访问令牌",
|
|
818
|
+
description:
|
|
819
|
+
"吊销后旧令牌与已登录会话立即作废,面板不再自动补新令牌;恢复只需在配置文件里把它改回 false",
|
|
820
|
+
default: false,
|
|
821
|
+
hot: true,
|
|
822
|
+
},
|
|
780
823
|
]
|
package/src/config/live.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto"
|
|
1
2
|
import { applyHotFields, resolveConfig, type ConfigWarning, type QQBotConfig } from "../config"
|
|
2
3
|
import { CONFIG_FIELDS } from "../config/fields"
|
|
3
4
|
import { requiredFieldProblem } from "../config/registry"
|
|
@@ -71,9 +72,61 @@ export class ConfigController {
|
|
|
71
72
|
async load(): Promise<QQBotConfig> {
|
|
72
73
|
this.raw = await this.options.store.read()
|
|
73
74
|
this.config = this.resolve(this.raw)
|
|
75
|
+
await this.ensureSecrets()
|
|
74
76
|
return this.config
|
|
75
77
|
}
|
|
76
78
|
|
|
79
|
+
/**
|
|
80
|
+
* 补齐缺失的随机凭据。
|
|
81
|
+
*
|
|
82
|
+
* 面板开着却没有令牌时生成一个并写回配置文件:令牌要留在文件里,
|
|
83
|
+
* 重启后不变,面板也才有得登录。写不进去只告警,不影响机器人本身。
|
|
84
|
+
*
|
|
85
|
+
* 令牌被吊销时不再补:吊销的语义就是「这个令牌永远不能用」,
|
|
86
|
+
* 补一个新的只会让人以为已经吊销成功。
|
|
87
|
+
*/
|
|
88
|
+
private async ensureSecrets(): Promise<void> {
|
|
89
|
+
const live = this.config
|
|
90
|
+
if (!live?.panel.enabled) return
|
|
91
|
+
if (live.panel.tokenRevoked) {
|
|
92
|
+
// 文件里的旧令牌一并删掉:留着就还有被捡回来的机会。
|
|
93
|
+
if (!live.panel.token) return
|
|
94
|
+
try {
|
|
95
|
+
await this.applyPatch({ "panel.token": null })
|
|
96
|
+
this.warn("面板访问令牌已吊销,面板不再对外服务")
|
|
97
|
+
} catch (error) {
|
|
98
|
+
this.warn(`面板访问令牌吊销失败:${error instanceof Error ? error.message : String(error)}`)
|
|
99
|
+
}
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
if (live.panel.token) {
|
|
103
|
+
if (live.panel.token.length < 16) this.warn("面板访问令牌太短,建议至少 16 个字符")
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
const token = randomBytes(32).toString("hex")
|
|
107
|
+
try {
|
|
108
|
+
await this.applyPatch({ "panel.token": token })
|
|
109
|
+
this.warn("面板访问令牌已生成,请查看 config.json 里的 panel.token")
|
|
110
|
+
} catch (error) {
|
|
111
|
+
this.warn(`面板访问令牌生成失败:${error instanceof Error ? error.message : String(error)}`)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 规整补丁:令牌与吊销状态保持一致。
|
|
117
|
+
*
|
|
118
|
+
* 写入新令牌就是恢复使用:顺带把吊销标记清掉,
|
|
119
|
+
* 否则会留下「令牌有了但面板还是关着」这种看不懂的状态。
|
|
120
|
+
* 清空令牌不当作吊销:吊销由 `panel.tokenRevoked` 单独表达,
|
|
121
|
+
* 这样「重置令牌」和「吊销面板」是两件事。
|
|
122
|
+
*/
|
|
123
|
+
private normalizePatch(patch: ConfigPatch): ConfigPatch {
|
|
124
|
+
if (!("panel.token" in patch)) return patch
|
|
125
|
+
const value = patch["panel.token"]
|
|
126
|
+
if (typeof value !== "string" || !value.trim()) return patch
|
|
127
|
+
return { ...patch, "panel.tokenRevoked": false }
|
|
128
|
+
}
|
|
129
|
+
|
|
77
130
|
/** 解析文件内容;非法值告警并回退默认值。 */
|
|
78
131
|
private resolve(raw: Record<string, unknown>): QQBotConfig {
|
|
79
132
|
return resolveConfig(raw, this.options.agent, (message) => this.warn(message))
|
|
@@ -90,11 +143,12 @@ export class ConfigController {
|
|
|
90
143
|
async applyPatch(patch: ConfigPatch): Promise<ConfigUpdateResult> {
|
|
91
144
|
const live = this.config
|
|
92
145
|
if (!live) throw new Error("配置尚未加载")
|
|
93
|
-
|
|
146
|
+
const normalized = this.normalizePatch(patch)
|
|
147
|
+
for (const [path, value] of Object.entries(normalized)) {
|
|
94
148
|
const problem = requiredFieldProblem(path, value)
|
|
95
149
|
if (problem) throw new Error(problem)
|
|
96
150
|
}
|
|
97
|
-
const raw = await this.options.store.patch(
|
|
151
|
+
const raw = await this.options.store.patch(normalized)
|
|
98
152
|
// 先解析再落盘:配置装不起来时不能把文件改坏,旧配置继续用着。
|
|
99
153
|
const next = this.resolve(raw)
|
|
100
154
|
await this.options.store.write(raw)
|
|
@@ -102,9 +156,11 @@ export class ConfigController {
|
|
|
102
156
|
applyHotFields(live, next)
|
|
103
157
|
this.config = live
|
|
104
158
|
this.notify()
|
|
159
|
+
// 面板刚被打开时补一个令牌:补完会再通知一次,订阅者这时看到的配置是完整的。
|
|
160
|
+
await this.ensureSecrets()
|
|
105
161
|
return {
|
|
106
162
|
config: live,
|
|
107
|
-
restartRequired: Object.keys(
|
|
163
|
+
restartRequired: Object.keys(normalized).filter((path) => !isHotField(path)),
|
|
108
164
|
}
|
|
109
165
|
}
|
|
110
166
|
|
|
@@ -132,6 +188,8 @@ export class ConfigController {
|
|
|
132
188
|
applyHotFields(live, next)
|
|
133
189
|
this.config = live
|
|
134
190
|
this.notify()
|
|
191
|
+
// 面板刚被打开时补一个令牌:补完会再通知一次,订阅者这时看到的配置是完整的。
|
|
192
|
+
void this.ensureSecrets()
|
|
135
193
|
})
|
|
136
194
|
}
|
|
137
195
|
|
package/src/config/store.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { dirname } from "node:path"
|
|
1
|
+
import { readFile } from "node:fs/promises"
|
|
3
2
|
import chokidar, { type FSWatcher } from "chokidar"
|
|
4
|
-
import
|
|
3
|
+
import { backupFile, DIR_MODE, FILE_MODE, writeJsonAtomic } from "../fs/atomic"
|
|
5
4
|
import type { Logger } from "../logger"
|
|
6
5
|
|
|
7
6
|
/**
|
|
@@ -35,9 +34,9 @@ export const CONFIG_FILE_NAME = "config.json"
|
|
|
35
34
|
/** 配置文件的备份名。 */
|
|
36
35
|
export const CONFIG_BACKUP_NAME = "config.json.bak"
|
|
37
36
|
/** 配置文件权限:只有本人可读写。 */
|
|
38
|
-
export const CONFIG_FILE_MODE =
|
|
37
|
+
export const CONFIG_FILE_MODE = FILE_MODE
|
|
39
38
|
/** 配置目录权限:只有本人可进入。 */
|
|
40
|
-
export const CONFIG_DIR_MODE =
|
|
39
|
+
export const CONFIG_DIR_MODE = DIR_MODE
|
|
41
40
|
/** 外部改写配置文件后,去抖再通知的等待时间(毫秒)。 */
|
|
42
41
|
const WATCH_DEBOUNCE_MS = 150
|
|
43
42
|
|
|
@@ -93,19 +92,15 @@ export class ConfigStore {
|
|
|
93
92
|
* 原子写 + `0600` 权限;写之前把旧内容存成 `.bak`。
|
|
94
93
|
*/
|
|
95
94
|
async write(raw: Record<string, unknown>): Promise<void> {
|
|
96
|
-
await mkdir(dirname(this.path), { recursive: true, mode: CONFIG_DIR_MODE })
|
|
97
95
|
await this.backup()
|
|
98
|
-
|
|
99
|
-
await writeFileAtomic(this.path, text, { mode: CONFIG_FILE_MODE, fsync: true })
|
|
96
|
+
await writeJsonAtomic(this.path, raw)
|
|
100
97
|
}
|
|
101
98
|
|
|
102
99
|
/** 把当前内容存成备份;没有旧文件时跳过。 */
|
|
103
100
|
private async backup(): Promise<void> {
|
|
104
101
|
try {
|
|
105
|
-
|
|
106
|
-
await writeFileAtomic(this.backupPath, text, { mode: CONFIG_FILE_MODE })
|
|
102
|
+
await backupFile(this.path)
|
|
107
103
|
} catch (error) {
|
|
108
|
-
if ((error as NodeJS.ErrnoException).code === "ENOENT") return
|
|
109
104
|
this.logger?.warn("备份配置文件失败", error)
|
|
110
105
|
}
|
|
111
106
|
}
|
package/src/config/types.ts
CHANGED
|
@@ -41,6 +41,14 @@ export interface WakeConfig {
|
|
|
41
41
|
ignoreAtOthers: boolean
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
/** 消息收集窗口配置:收到消息后先攒一会儿,再合并成一轮一起回复。 */
|
|
45
|
+
export interface CollectConfig {
|
|
46
|
+
/** 私聊的收集窗口(毫秒),0 表示收到消息立即回复。 */
|
|
47
|
+
privateMs: number
|
|
48
|
+
/** 群聊的收集窗口(毫秒),0 表示收到消息立即回复。 */
|
|
49
|
+
groupMs: number
|
|
50
|
+
}
|
|
51
|
+
|
|
44
52
|
/** 回复节奏配置:怎么拆段、怎么发。 */
|
|
45
53
|
export interface ReplyConfig {
|
|
46
54
|
/** 分段之间的最小间隔(毫秒)。 */
|
|
@@ -173,6 +181,8 @@ export interface PanelConfig {
|
|
|
173
181
|
port: number
|
|
174
182
|
/** 访问令牌;留空时自动生成并写回配置文件。 */
|
|
175
183
|
token: string
|
|
184
|
+
/** 是否已吊销访问令牌:吊销后不再自动补令牌,面板也不再对外服务。 */
|
|
185
|
+
tokenRevoked: boolean
|
|
176
186
|
}
|
|
177
187
|
|
|
178
188
|
export interface ModelRef {
|
|
@@ -200,6 +210,8 @@ export interface QQBotConfig {
|
|
|
200
210
|
soulPath?: string
|
|
201
211
|
/** 工作目录 souls 里的角色设定文件名,默认 default.txt。 */
|
|
202
212
|
soulName?: string
|
|
213
|
+
/** 角色设定的边界:允许哪些目录里的文件能进提示词。 */
|
|
214
|
+
soul: SoulConfig
|
|
203
215
|
/** QQ 会话使用的 OpenCode agent。 */
|
|
204
216
|
agent?: string
|
|
205
217
|
/** QQ 会话使用的模型。 */
|
|
@@ -212,6 +224,7 @@ export interface QQBotConfig {
|
|
|
212
224
|
reuseSession: boolean
|
|
213
225
|
proactive: ProactiveConfig
|
|
214
226
|
wake: WakeConfig
|
|
227
|
+
collect: CollectConfig
|
|
215
228
|
reply: ReplyConfig
|
|
216
229
|
trace: TraceConfig
|
|
217
230
|
reminder: ReminderConfig
|
|
@@ -226,6 +239,17 @@ export interface QQBotConfig {
|
|
|
226
239
|
panel: PanelConfig
|
|
227
240
|
}
|
|
228
241
|
|
|
242
|
+
/** 角色设定的边界配置。 */
|
|
243
|
+
export interface SoulConfig {
|
|
244
|
+
/**
|
|
245
|
+
* 允许的角色设定根目录;相对路径按机器人工作目录解析。
|
|
246
|
+
*
|
|
247
|
+
* 默认只有工作目录:角色设定的内容会随每一轮发给模型服务商,
|
|
248
|
+
* 越出允许根的文件不会被加载。
|
|
249
|
+
*/
|
|
250
|
+
allowedPaths: string[]
|
|
251
|
+
}
|
|
252
|
+
|
|
229
253
|
/** 准入配置。 */
|
|
230
254
|
export interface AccessConfig {
|
|
231
255
|
/** 放行名单。 */
|
package/src/config.ts
CHANGED
|
@@ -11,6 +11,7 @@ import type { ParsedConfig, QQBotConfig } from "./config/types"
|
|
|
11
11
|
|
|
12
12
|
export type {
|
|
13
13
|
AccessConfig,
|
|
14
|
+
CollectConfig,
|
|
14
15
|
DiaryConfig,
|
|
15
16
|
DiaryPush,
|
|
16
17
|
EventsConfig,
|
|
@@ -30,6 +31,7 @@ export type {
|
|
|
30
31
|
ReminderConfig,
|
|
31
32
|
ReplyConfig,
|
|
32
33
|
SensitiveConfig,
|
|
34
|
+
SoulConfig,
|
|
33
35
|
TraceConfig,
|
|
34
36
|
WakeConfig,
|
|
35
37
|
} from "./config/types"
|
|
@@ -145,6 +147,7 @@ export function resolveConfig(
|
|
|
145
147
|
},
|
|
146
148
|
log: { ...parsed.log },
|
|
147
149
|
media: { allowedDirs: [...parsed.media.allowedDirs] },
|
|
150
|
+
soul: { allowedPaths: [...parsed.soul.allowedPaths] },
|
|
148
151
|
diary: {
|
|
149
152
|
...parsed.diary,
|
|
150
153
|
summaryTargets: [...parsed.diary.summaryTargets],
|
package/src/diary/command.ts
CHANGED
|
@@ -8,7 +8,7 @@ import type { SendRateLimiter } from "../rate-limit"
|
|
|
8
8
|
import type { SensitiveChecker } from "../sensitive"
|
|
9
9
|
import { createPlainSender, sendFileMessage } from "../tools/qq"
|
|
10
10
|
import type { DiaryCollector } from "./collector"
|
|
11
|
-
import { dateKey, isDateKey } from "./format"
|
|
11
|
+
import { dateKey, chatLabel, isDateKey } from "./format"
|
|
12
12
|
import { diaryScopeChat, isDiaryScopeChat } from "./scope"
|
|
13
13
|
import { readDiaryHead, type DiaryStore } from "./store"
|
|
14
14
|
import type { DiarySummarizer } from "./summary"
|
|
@@ -109,7 +109,7 @@ export function formatDiaryList(
|
|
|
109
109
|
days: number,
|
|
110
110
|
entries: readonly { date: string; title: string }[],
|
|
111
111
|
): string {
|
|
112
|
-
const head = `日记(${chat},最近 ${days} 篇)`
|
|
112
|
+
const head = `日记(${chatLabel(chat)},最近 ${days} 篇)`
|
|
113
113
|
if (entries.length === 0) return `${head}\n还没有日记。`
|
|
114
114
|
return [head, ...entries.map((entry) => `${entry.date} ${entry.title}`)].join("\n")
|
|
115
115
|
}
|
|
@@ -136,12 +136,12 @@ export function createDiaryRunner(deps: DiaryRunnerDeps): DiaryCommandRunner {
|
|
|
136
136
|
body = await readDiaryHead(file)
|
|
137
137
|
} catch (error) {
|
|
138
138
|
logger.error(`${targetLabel(target)} 的日记文件读取失败,放弃发送`, error)
|
|
139
|
-
await report(target,
|
|
139
|
+
await report(target, `${targetLabel(target)} 的日记文件读取失败,没有发送。`)
|
|
140
140
|
return
|
|
141
141
|
}
|
|
142
142
|
if (!sensitive.check(body).allowed) {
|
|
143
143
|
logger.warn(`${targetLabel(target)} 的日记正文包含敏感信息,已拦截`)
|
|
144
|
-
await report(target,
|
|
144
|
+
await report(target, `${targetLabel(target)} 的日记正文包含敏感信息,已拦截。`)
|
|
145
145
|
return
|
|
146
146
|
}
|
|
147
147
|
await sendFileMessage(client, target, file, {
|
|
@@ -164,7 +164,7 @@ export function createDiaryRunner(deps: DiaryRunnerDeps): DiaryCommandRunner {
|
|
|
164
164
|
body: content,
|
|
165
165
|
source: "manual",
|
|
166
166
|
})
|
|
167
|
-
await report(target, `已写入 ${date} 的日记:${entry.title}`)
|
|
167
|
+
await report(target, `已写入 ${chatLabel(targetKey(target))} ${date} 的日记:${entry.title}`)
|
|
168
168
|
}
|
|
169
169
|
|
|
170
170
|
const runList = async (target: ChatTarget, args: DiaryCommandArgs): Promise<void> => {
|
|
@@ -181,10 +181,10 @@ export function createDiaryRunner(deps: DiaryRunnerDeps): DiaryCommandRunner {
|
|
|
181
181
|
}
|
|
182
182
|
const entry = await store.read(targetKey(target), date)
|
|
183
183
|
if (!entry) {
|
|
184
|
-
await report(target, `没有找到 ${date} 的日记。`)
|
|
184
|
+
await report(target, `没有找到 ${chatLabel(targetKey(target))} ${date} 的日记。`)
|
|
185
185
|
return
|
|
186
186
|
}
|
|
187
|
-
await report(target, `${date} 的日记`)
|
|
187
|
+
await report(target, `${chatLabel(targetKey(target))} ${date} 的日记`)
|
|
188
188
|
await sendDiaryFile(target, date).catch((error: unknown) => {
|
|
189
189
|
logger.error(`${targetLabel(target)} 发送日记文件失败`, error)
|
|
190
190
|
return report(
|
|
@@ -196,12 +196,13 @@ export function createDiaryRunner(deps: DiaryRunnerDeps): DiaryCommandRunner {
|
|
|
196
196
|
|
|
197
197
|
const runAuto = async (target: ChatTarget, args: DiaryCommandArgs): Promise<void> => {
|
|
198
198
|
const date = args.date ?? dateKey()
|
|
199
|
+
const label = chatLabel(targetKey(target))
|
|
199
200
|
if (!isDateKey(date)) {
|
|
200
201
|
await report(target, "要生成哪天的日记?用法:/diary auto [日期],日期用 YYYY-MM-DD")
|
|
201
202
|
return
|
|
202
203
|
}
|
|
203
204
|
if (!(await collector.has(targetKey(target), date))) {
|
|
204
|
-
await report(target, `${date} 没有聊天素材,跳过总结。`)
|
|
205
|
+
await report(target, `${label} ${date} 没有聊天素材,跳过总结。`)
|
|
205
206
|
return
|
|
206
207
|
}
|
|
207
208
|
if (running) {
|
|
@@ -211,13 +212,13 @@ export function createDiaryRunner(deps: DiaryRunnerDeps): DiaryCommandRunner {
|
|
|
211
212
|
running = true
|
|
212
213
|
// 总结可能要跑很久,放到后台执行,结果用 QQ 消息回。
|
|
213
214
|
void (async () => {
|
|
214
|
-
await report(target, `开始生成 ${date} 的日记…`)
|
|
215
|
+
await report(target, `开始生成 ${label} ${date} 的日记…`)
|
|
215
216
|
const title = await summarizer.summarize(targetKey(target), date)
|
|
216
217
|
if (!title) {
|
|
217
|
-
await report(target, `${date} 的日记总结失败,素材保留待下次重试。`)
|
|
218
|
+
await report(target, `${label} ${date} 的日记总结失败,素材保留待下次重试。`)
|
|
218
219
|
return
|
|
219
220
|
}
|
|
220
|
-
await report(target, `${date} 的日记已生成:${title}`)
|
|
221
|
+
await report(target, `${label} ${date} 的日记已生成:${title}`)
|
|
221
222
|
await sendDiaryFile(target, date).catch((error: unknown) => {
|
|
222
223
|
logger.error(`${targetLabel(target)} 发送日记文件失败`, error)
|
|
223
224
|
})
|
|
@@ -226,7 +227,7 @@ export function createDiaryRunner(deps: DiaryRunnerDeps): DiaryCommandRunner {
|
|
|
226
227
|
logger.error("日记总结任务失败", error)
|
|
227
228
|
return report(
|
|
228
229
|
target,
|
|
229
|
-
|
|
230
|
+
`${label} 日记总结失败:${error instanceof Error ? error.message : String(error)}`,
|
|
230
231
|
)
|
|
231
232
|
})
|
|
232
233
|
.finally(() => {
|
package/src/diary/format.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { parseTargetKey, targetLabel } from "../chat"
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* 日记的文件格式:日期键、聊天目录名与 front matter。
|
|
3
5
|
*
|
|
@@ -14,6 +16,8 @@ export interface DiaryMeta {
|
|
|
14
16
|
date: string
|
|
15
17
|
/** 聊天键,形如 `group:123`。 */
|
|
16
18
|
chat: string
|
|
19
|
+
/** 聊天可读标识,形如 `群 123`;旧文件里可能没有。 */
|
|
20
|
+
chat_label?: string
|
|
17
21
|
/** 来源。 */
|
|
18
22
|
source: DiarySource
|
|
19
23
|
/** 首次写入时间(ISO 字符串)。 */
|
|
@@ -87,6 +91,17 @@ export function fallbackTitle(date: string): string {
|
|
|
87
91
|
return `${date} 的日记`
|
|
88
92
|
}
|
|
89
93
|
|
|
94
|
+
/**
|
|
95
|
+
* 聊天键的可读名称。
|
|
96
|
+
*
|
|
97
|
+
* 日记的正文、列表与发送说明都要让人一眼看出是群聊还是私聊,
|
|
98
|
+
* 所以统一用「群 123」「私聊 456」,解析不了时原样返回。
|
|
99
|
+
*/
|
|
100
|
+
export function chatLabel(chat: string): string {
|
|
101
|
+
const target = parseTargetKey(chat)
|
|
102
|
+
return target ? targetLabel(target) : chat
|
|
103
|
+
}
|
|
104
|
+
|
|
90
105
|
/**
|
|
91
106
|
* 序列化一篇日记。
|
|
92
107
|
*
|
|
@@ -97,6 +112,7 @@ export function serializeDiary(entry: DiaryEntry): string {
|
|
|
97
112
|
"---",
|
|
98
113
|
`date: ${entry.date}`,
|
|
99
114
|
`chat: ${entry.chat}`,
|
|
115
|
+
`chat_label: ${chatLabel(entry.chat)}`,
|
|
100
116
|
`source: ${entry.source}`,
|
|
101
117
|
`created: ${entry.created}`,
|
|
102
118
|
`updated: ${entry.updated}`,
|
|
@@ -147,6 +163,7 @@ export function parseDiary(content: string): ParsedDiary | undefined {
|
|
|
147
163
|
meta: {
|
|
148
164
|
date,
|
|
149
165
|
chat: meta.chat ?? "",
|
|
166
|
+
...(meta.chat_label ? { chat_label: meta.chat_label } : {}),
|
|
150
167
|
source,
|
|
151
168
|
created: meta.created ?? "",
|
|
152
169
|
updated: meta.updated ?? "",
|