@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
package/src/panel/server.ts
CHANGED
|
@@ -48,6 +48,10 @@ export class PanelServer {
|
|
|
48
48
|
private listening: { host: string; port: number } | undefined
|
|
49
49
|
/** 端口被占用时的重试策略。 */
|
|
50
50
|
private readonly retry: { times: number; intervalMs: number }
|
|
51
|
+
/** 正在应用配置:期间来的改动排队,不并行处理。 */
|
|
52
|
+
private applying = false
|
|
53
|
+
/** 排队中的改动:应用期间又来了新改动就再跑一轮。 */
|
|
54
|
+
private queued = false
|
|
51
55
|
|
|
52
56
|
constructor(private readonly options: PanelServerOptions) {
|
|
53
57
|
this.token = options.runtime.config.panel.token
|
|
@@ -68,7 +72,9 @@ export class PanelServer {
|
|
|
68
72
|
*/
|
|
69
73
|
async start(): Promise<void> {
|
|
70
74
|
this.unsubscribe ??= this.options.controller.onChange(() => {
|
|
71
|
-
void this.reconcile()
|
|
75
|
+
void this.reconcile().catch((error: unknown) => {
|
|
76
|
+
this.options.logger.error("应用面板配置失败", error)
|
|
77
|
+
})
|
|
72
78
|
})
|
|
73
79
|
await this.reconcile()
|
|
74
80
|
}
|
|
@@ -80,8 +86,29 @@ export class PanelServer {
|
|
|
80
86
|
await this.closeServer()
|
|
81
87
|
}
|
|
82
88
|
|
|
83
|
-
/**
|
|
89
|
+
/**
|
|
90
|
+
* 配置变了:令牌、是否启用、地址或端口都要跟着调整。
|
|
91
|
+
*
|
|
92
|
+
* 一次只能应用一轮:应用期间又来的改动排队,等这一轮结束再跑。
|
|
93
|
+
*/
|
|
84
94
|
private async reconcile(): Promise<void> {
|
|
95
|
+
if (this.applying) {
|
|
96
|
+
this.queued = true
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
this.applying = true
|
|
100
|
+
try {
|
|
101
|
+
do {
|
|
102
|
+
this.queued = false
|
|
103
|
+
await this.apply()
|
|
104
|
+
} while (this.queued)
|
|
105
|
+
} finally {
|
|
106
|
+
this.applying = false
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** 应用一轮配置。 */
|
|
111
|
+
private async apply(): Promise<void> {
|
|
85
112
|
const config = this.options.runtime.config
|
|
86
113
|
// 令牌先换:面板关着的时候换了令牌,启用后也要认新的。
|
|
87
114
|
if (config.panel.token !== this.token) {
|
|
@@ -89,56 +116,70 @@ export class PanelServer {
|
|
|
89
116
|
this.auth.rotate(config.panel.token)
|
|
90
117
|
this.options.logger.info("面板令牌已更新,已签发的会话全部作废")
|
|
91
118
|
}
|
|
119
|
+
// 令牌被吊销:旧令牌与已登录会话立刻作废,服务也不再对外。
|
|
120
|
+
if (config.panel.tokenRevoked) {
|
|
121
|
+
this.auth.clear()
|
|
122
|
+
await this.closeServer()
|
|
123
|
+
return
|
|
124
|
+
}
|
|
92
125
|
if (!config.panel.enabled) {
|
|
93
126
|
await this.closeServer()
|
|
94
127
|
return
|
|
95
128
|
}
|
|
129
|
+
// 没有令牌就登不进去:等配置控制器补一个令牌,补完会再通知一次。
|
|
130
|
+
if (!config.panel.token) {
|
|
131
|
+
await this.closeServer()
|
|
132
|
+
return
|
|
133
|
+
}
|
|
96
134
|
const host = config.panel.host
|
|
97
135
|
const port = config.panel.port
|
|
98
136
|
if (this.listening && this.listening.host === host && this.listening.port === port) return
|
|
99
|
-
await this.
|
|
100
|
-
await this.listen()
|
|
137
|
+
await this.serve(host, port)
|
|
101
138
|
}
|
|
102
139
|
|
|
103
|
-
/**
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
server.close(() => resolve())
|
|
111
|
-
// SSE 的连接不会自己断,close 会一直等;强制掐掉,免得卸载卡住。
|
|
112
|
-
if ("closeAllConnections" in server) server.closeAllConnections()
|
|
113
|
-
})
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/** 真正开始监听。 */
|
|
117
|
-
private async listen(): Promise<void> {
|
|
118
|
-
const config = this.options.runtime.config
|
|
119
|
-
const host = config.panel.host
|
|
120
|
-
const port = config.panel.port
|
|
121
|
-
const staticDir = panelStaticDir()
|
|
140
|
+
/**
|
|
141
|
+
* 换到新的地址与端口。
|
|
142
|
+
*
|
|
143
|
+
* 端口变了先绑新的,成功了再关旧的:绑不上时旧服务照常可用。
|
|
144
|
+
* 端口没变(只换了主机名)得先关旧的,同一个端口绑不了两次。
|
|
145
|
+
*/
|
|
146
|
+
private async serve(host: string, port: number): Promise<void> {
|
|
122
147
|
const app = createPanelApp({
|
|
123
148
|
auth: this.auth,
|
|
124
149
|
controller: this.options.controller,
|
|
125
150
|
runtime: this.options.runtime,
|
|
126
151
|
models: this.options.models,
|
|
127
|
-
...(existsSync(
|
|
152
|
+
...(existsSync(panelStaticDir()) ? { staticDir: panelStaticDir() } : {}),
|
|
128
153
|
})
|
|
154
|
+
const samePort = this.listening?.port === port
|
|
155
|
+
if (samePort) await this.closeServer()
|
|
129
156
|
const server = await this.bind(app, host, port)
|
|
130
157
|
server.on("error", (error: Error) => {
|
|
131
158
|
this.options.logger.error("状态面板服务出错", error)
|
|
132
159
|
})
|
|
133
160
|
server.unref?.()
|
|
161
|
+
if (!samePort) await this.closeServer()
|
|
134
162
|
this.server = server
|
|
135
163
|
this.listening = { host, port }
|
|
136
164
|
this.options.logger.info(`状态面板已启动:http://${host}:${port}`)
|
|
137
|
-
if (!existsSync(
|
|
165
|
+
if (!existsSync(panelStaticDir())) {
|
|
138
166
|
this.options.logger.warn("面板前端产物不存在,只有接口可用", "请执行 bun run web:build")
|
|
139
167
|
}
|
|
140
168
|
}
|
|
141
169
|
|
|
170
|
+
/** 关掉 HTTP 服务。 */
|
|
171
|
+
private async closeServer(): Promise<void> {
|
|
172
|
+
const server = this.server
|
|
173
|
+
this.server = undefined
|
|
174
|
+
this.listening = undefined
|
|
175
|
+
if (!server) return
|
|
176
|
+
await new Promise<void>((resolve) => {
|
|
177
|
+
server.close(() => resolve())
|
|
178
|
+
// SSE 的连接不会自己断,close 会一直等;强制掐掉,免得卸载卡住。
|
|
179
|
+
if ("closeAllConnections" in server) server.closeAllConnections()
|
|
180
|
+
})
|
|
181
|
+
}
|
|
182
|
+
|
|
142
183
|
/**
|
|
143
184
|
* 监听端口。
|
|
144
185
|
*
|
package/src/panel/status.ts
CHANGED
|
@@ -57,6 +57,8 @@ export interface StatusSnapshot {
|
|
|
57
57
|
wake?: string
|
|
58
58
|
/** 是否允许主动说话。 */
|
|
59
59
|
proactive?: boolean
|
|
60
|
+
/** 是否正在收集消息(窗口还没到点)。 */
|
|
61
|
+
collecting?: boolean
|
|
60
62
|
}>
|
|
61
63
|
/** 出站限流用量。 */
|
|
62
64
|
rateLimit: Array<{
|
|
@@ -119,6 +121,8 @@ interface SessionRow {
|
|
|
119
121
|
wake?: string
|
|
120
122
|
/** 是否允许主动说话。 */
|
|
121
123
|
proactive?: boolean
|
|
124
|
+
/** 是否正在收集消息(窗口还没到点)。 */
|
|
125
|
+
collecting?: boolean
|
|
122
126
|
}
|
|
123
127
|
|
|
124
128
|
/**
|
|
@@ -194,7 +198,10 @@ async function collectSessions(runtime: PanelRuntime): Promise<SessionRow[]> {
|
|
|
194
198
|
buffered: item.buffered,
|
|
195
199
|
pending: item.pending,
|
|
196
200
|
...(override?.wake ? { wake: override.wake } : {}),
|
|
197
|
-
...(override?.proactive !== undefined
|
|
201
|
+
...(override?.["proactive.enabled"] !== undefined
|
|
202
|
+
? { proactive: override["proactive.enabled"] }
|
|
203
|
+
: {}),
|
|
204
|
+
...(item.collectUntil !== undefined ? { collecting: true } : {}),
|
|
198
205
|
})
|
|
199
206
|
}
|
|
200
207
|
return result
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 原型安全的小工具。
|
|
3
|
+
*
|
|
4
|
+
* 配置里的键名(配置档名、聊天键)都是用户可控的:直接拿它们去索引普通对象,
|
|
5
|
+
* `__proto__`、`constructor` 这类名字会命中 `Object.prototype` 上的属性,
|
|
6
|
+
* 于是「这个档存在吗」这类判断永远为真,运行时再拿原型当配置档用就崩了。
|
|
7
|
+
*
|
|
8
|
+
* 这里统一两件事:映射一律用没有原型的字典,查找一律用自有属性判断。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** 不能用作键名的属性名:它们会落到原型上。 */
|
|
12
|
+
const RESERVED_KEYS = new Set(["__proto__", "constructor", "prototype"])
|
|
13
|
+
|
|
14
|
+
/** 建一个没有原型属性的字典。 */
|
|
15
|
+
export function safeMap<T = unknown>(): Record<string, T> {
|
|
16
|
+
return Object.create(null) as Record<string, T>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** 键名是否安全:不能是原型上的属性名。 */
|
|
20
|
+
export function isSafeKey(name: string): boolean {
|
|
21
|
+
return !RESERVED_KEYS.has(name)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** 对象里是否真的有这个自有键。 */
|
|
25
|
+
export function hasKey(target: object, key: string): boolean {
|
|
26
|
+
return Object.hasOwn(target, key)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 把源对象里的键搬进一个没有原型的字典。
|
|
31
|
+
*
|
|
32
|
+
* 键名是用户给的:直接 `target[key] = value` 会踩到 `__proto__` 的 setter,
|
|
33
|
+
* 键会变成原型上的属性,序列化时还会丢掉。
|
|
34
|
+
*/
|
|
35
|
+
export function copyToSafeMap<T = unknown>(
|
|
36
|
+
source: Record<string, unknown>,
|
|
37
|
+
target: Record<string, T> = safeMap<T>(),
|
|
38
|
+
): Record<string, T> {
|
|
39
|
+
for (const [key, value] of Object.entries(source)) {
|
|
40
|
+
Object.defineProperty(target, key, {
|
|
41
|
+
value: value as T,
|
|
42
|
+
enumerable: true,
|
|
43
|
+
writable: true,
|
|
44
|
+
configurable: true,
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
return target
|
|
48
|
+
}
|
package/src/sensitive.ts
CHANGED
|
@@ -266,6 +266,8 @@ export interface SensitiveCheckerOptions {
|
|
|
266
266
|
knownSecrets?: string[]
|
|
267
267
|
/** 是否启用检查,默认启用。 */
|
|
268
268
|
enabled?: boolean
|
|
269
|
+
/** 要排除的检测器:某些场景下某一类不该拦。 */
|
|
270
|
+
exclude?: readonly string[]
|
|
269
271
|
}
|
|
270
272
|
|
|
271
273
|
/**
|
|
@@ -290,7 +292,10 @@ export class SensitiveChecker {
|
|
|
290
292
|
)
|
|
291
293
|
const known = knownSecretDetector(this.knownSecrets)
|
|
292
294
|
this.custom = known ? [known, SENSITIVE_PATH_DETECTOR] : [SENSITIVE_PATH_DETECTOR]
|
|
293
|
-
|
|
295
|
+
const excluded = new Set(options.exclude ?? [])
|
|
296
|
+
this.only = [...SECRET_DETECTORS, ...this.custom.map((detector) => detector.id)].filter(
|
|
297
|
+
(id) => !excluded.has(id),
|
|
298
|
+
)
|
|
294
299
|
}
|
|
295
300
|
|
|
296
301
|
/** 打开或关闭检查:开关是热字段,改完立即生效。 */
|
|
@@ -327,7 +332,31 @@ export class SensitiveChecker {
|
|
|
327
332
|
}
|
|
328
333
|
}
|
|
329
334
|
|
|
330
|
-
/**
|
|
335
|
+
/** 掩码时已知凭证的最短长度:再短就容易撞上普通文本。 */
|
|
336
|
+
const MIN_MASKED_SECRET_LENGTH = 4
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* 把已知凭证明文换成占位符。
|
|
340
|
+
*
|
|
341
|
+
* 只认环境里已知的那些明文(面板令牌、NapCat 令牌、环境变量里的密钥),
|
|
342
|
+
* 不跑通用检测器:面板要回显的是配置内容,路径与地址不该被误伤。
|
|
343
|
+
*
|
|
344
|
+
* 这里不再按通用的 12 字符下限过滤:已知凭证就是凭证,用户自己设的短令牌
|
|
345
|
+
* 同样不能原样回显。
|
|
346
|
+
*/
|
|
347
|
+
export function maskKnownSecrets(text: string, secrets: readonly string[]): string {
|
|
348
|
+
let masked = text
|
|
349
|
+
for (const secret of secrets) {
|
|
350
|
+
if (secret.length < MIN_MASKED_SECRET_LENGTH) continue
|
|
351
|
+
if (!masked.includes(secret)) continue
|
|
352
|
+
masked = masked.split(secret).join("[REDACTED]")
|
|
353
|
+
}
|
|
354
|
+
return masked
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* 日志打码的选项。
|
|
359
|
+
*/
|
|
331
360
|
export interface MaskOptions {
|
|
332
361
|
/** 已知的凭证明文:环境变量里的密钥、NapCat 访问令牌等。 */
|
|
333
362
|
knownSecrets?: string[]
|
package/src/session/find.ts
CHANGED
|
@@ -24,12 +24,13 @@ export interface FindOptions {
|
|
|
24
24
|
* 在指定目录下按谓词查找会话。
|
|
25
25
|
*
|
|
26
26
|
* 列表按创建时间倒序、翻页上限固定,避免会话很多时把整个列表拉一遍。
|
|
27
|
+
* 不传目录时查的是全部目录:老版本把会话建在项目目录里,只按工作目录找会漏掉。
|
|
27
28
|
*
|
|
28
29
|
* @returns 命中的会话;找不到或查询失败时返回空数组。
|
|
29
30
|
*/
|
|
30
31
|
export async function findSessions(
|
|
31
32
|
ctx: Plugin.Context,
|
|
32
|
-
directory: NonNullable<Parameters<Plugin.Context["session"]["list"]>[0]>["directory"],
|
|
33
|
+
directory: NonNullable<Parameters<Plugin.Context["session"]["list"]>[0]>["directory"] | undefined,
|
|
33
34
|
match: (session: ListedSession) => boolean,
|
|
34
35
|
logger: Logger,
|
|
35
36
|
failure: string,
|
|
@@ -43,7 +44,7 @@ export async function findSessions(
|
|
|
43
44
|
try {
|
|
44
45
|
for (let page = 0; page < maxPages && found.length < limit; page += 1) {
|
|
45
46
|
const result = await ctx.session.list({
|
|
46
|
-
directory,
|
|
47
|
+
...(directory ? { directory } : {}),
|
|
47
48
|
limit: pageSize,
|
|
48
49
|
order: "desc",
|
|
49
50
|
...(cursor ? { cursor } : {}),
|