@cmach_socket/opencode-qq-bot 2.0.2 → 2.1.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 +1 -1
- package/package.json +20 -19
- package/src/config/initial.ts +20 -5
- package/src/config/live.ts +23 -2
- package/src/config/registry.ts +54 -0
- package/src/index.ts +2 -0
- package/src/panel/api.ts +81 -0
- package/src/panel/documents.ts +17 -14
- package/src/panel/server.ts +4 -0
- package/src/panel/tools.ts +32 -0
- package/src/panel/user-md.ts +218 -0
- package/src/safe-object.ts +5 -0
- package/src/session/manager.ts +25 -5
- package/src/user/store.ts +11 -1
- package/web/dist/assets/index-BkGFsghB.js +9 -0
- package/web/dist/assets/index-DxbYpvI7.css +1 -0
- package/web/dist/index.html +2 -2
- package/web/dist/assets/index-C5lRsdbz.css +0 -1
- package/web/dist/assets/index-siSfDOnC.js +0 -8
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { readdir, readFile, stat } from "node:fs/promises"
|
|
2
|
+
import { join } from "node:path"
|
|
3
|
+
import { allowedTargets, isTargetAllowed } from "../access"
|
|
4
|
+
import { parseTargetKey, targetKey, type ChatTarget } from "../chat"
|
|
5
|
+
import { backupFile, writeTextAtomic } from "../fs/atomic"
|
|
6
|
+
import { knownSecrets } from "../known-secrets"
|
|
7
|
+
import { isPlainObject } from "../safe-object"
|
|
8
|
+
import { maskKnownSecrets } from "../sensitive"
|
|
9
|
+
import { userMdPath, USER_MD_NAME } from "../user/store"
|
|
10
|
+
import type { DocumentProblem } from "./documents"
|
|
11
|
+
import type { PanelRuntime } from "./runtime"
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 面板里的用户档案(USER.md)。
|
|
15
|
+
*
|
|
16
|
+
* 档案按聊天隔离:每个聊天一份,路径由聊天目标算出。
|
|
17
|
+
* 运行时只读它,写它的只有面板:主人在面板里上传的是自己那份文件,
|
|
18
|
+
* 模型与工具依旧不改它。
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/** 档案正文的大小上限:超过就不接受上传。 */
|
|
22
|
+
export const MAX_USER_MD_BYTES = 64 * 1024
|
|
23
|
+
|
|
24
|
+
/** 一个聊天的用户档案在接口里的样子。 */
|
|
25
|
+
export interface UserMdView {
|
|
26
|
+
/** 聊天键,例如 `group:100`。 */
|
|
27
|
+
chat: string
|
|
28
|
+
/** 档案路径。 */
|
|
29
|
+
path: string
|
|
30
|
+
/** 档案是否存在。 */
|
|
31
|
+
exists: boolean
|
|
32
|
+
/** 档案大小(字节)。 */
|
|
33
|
+
bytes: number
|
|
34
|
+
/** 这个聊天当前是否放行。 */
|
|
35
|
+
admitted: boolean
|
|
36
|
+
/** 档案正文;命中已知凭证时已掩码。没放行或正文过大时没有这一项。 */
|
|
37
|
+
content?: string
|
|
38
|
+
/** 正文超过上限,列表里没有正文:手写的大档案不整份读进内存。 */
|
|
39
|
+
oversized?: boolean
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 用户档案列表。 */
|
|
43
|
+
export interface UserMdListView {
|
|
44
|
+
/** 每个聊天一条。 */
|
|
45
|
+
chats: UserMdView[]
|
|
46
|
+
/** 上传时接受的最大字节数,页面按它提前拦下过大的文件。 */
|
|
47
|
+
maxBytes: number
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 上传档案的结果。 */
|
|
51
|
+
export interface UserMdSaveResult {
|
|
52
|
+
/** 写入的聊天键。 */
|
|
53
|
+
chat: string
|
|
54
|
+
/** 写入的路径。 */
|
|
55
|
+
path: string
|
|
56
|
+
/** 写入后的字节数。 */
|
|
57
|
+
bytes: number
|
|
58
|
+
/** 生效时机说明。 */
|
|
59
|
+
note: string
|
|
60
|
+
/** 保存后的档案列表。 */
|
|
61
|
+
chats: UserMdView[]
|
|
62
|
+
/** 上传时接受的最大字节数。 */
|
|
63
|
+
maxBytes: number
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** 档案路径:`<home>/chats/<kind>-<id>/USER.md`。 */
|
|
67
|
+
function pathOf(runtime: PanelRuntime, target: ChatTarget): string {
|
|
68
|
+
return userMdPath(runtime.home.directory, target)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** 下载用的文件名:`group-100-USER.md`。 */
|
|
72
|
+
export function userMdFilename(target: ChatTarget): string {
|
|
73
|
+
return `${target.kind}-${target.id}-${USER_MD_NAME}`
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 读档案原文:下载用。
|
|
78
|
+
*
|
|
79
|
+
* 面板里显示的正文是掩码过的,这里给的是原文——主人下载的是自己写的文件。
|
|
80
|
+
* 文件不在时返回 undefined。
|
|
81
|
+
*/
|
|
82
|
+
export async function readUserMdText(
|
|
83
|
+
runtime: PanelRuntime,
|
|
84
|
+
target: ChatTarget,
|
|
85
|
+
): Promise<string | undefined> {
|
|
86
|
+
return await readFile(pathOf(runtime, target), "utf8").catch(() => undefined)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 磁盘上已经写了档案的聊天。
|
|
91
|
+
*
|
|
92
|
+
* 扫聊天目录即可:目录名就是 `group-<id>` / `private-<id>`。
|
|
93
|
+
* 这些档案原样留着——聊天还没放行时它不生效,等放行了自然接着用。
|
|
94
|
+
*/
|
|
95
|
+
async function chatsWithArchive(runtime: PanelRuntime): Promise<ChatTarget[]> {
|
|
96
|
+
const root = join(runtime.home.directory, "chats")
|
|
97
|
+
let entries: string[]
|
|
98
|
+
try {
|
|
99
|
+
entries = await readdir(root)
|
|
100
|
+
} catch {
|
|
101
|
+
return []
|
|
102
|
+
}
|
|
103
|
+
const targets: ChatTarget[] = []
|
|
104
|
+
for (const entry of entries) {
|
|
105
|
+
const match = /^(group|private)-(\d+)$/.exec(entry)
|
|
106
|
+
if (!match) continue
|
|
107
|
+
const target = parseTargetKey(`${match[1]}:${match[2]}`)
|
|
108
|
+
if (!target) continue
|
|
109
|
+
const info = await stat(pathOf(runtime, target)).catch(() => undefined)
|
|
110
|
+
if (!info?.isFile()) continue
|
|
111
|
+
targets.push(target)
|
|
112
|
+
}
|
|
113
|
+
return targets
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 列出各聊天的档案状态。
|
|
118
|
+
*
|
|
119
|
+
* 名单里是当前放行的聊天,另有磁盘上已经写了档案的聊天:
|
|
120
|
+
* 后者可能还没放行,页面标出来,档案本身不动。
|
|
121
|
+
* 正文只给放行的聊天看:没放行的档案只报元信息。
|
|
122
|
+
*/
|
|
123
|
+
export async function listUserMd(runtime: PanelRuntime): Promise<UserMdListView> {
|
|
124
|
+
const secrets = knownSecrets(runtime.config)
|
|
125
|
+
const targets = new Map<string, ChatTarget>()
|
|
126
|
+
for (const target of allowedTargets(runtime.config.access, runtime.config.ownerQQ)) {
|
|
127
|
+
targets.set(targetKey(target), target)
|
|
128
|
+
}
|
|
129
|
+
for (const target of await chatsWithArchive(runtime)) {
|
|
130
|
+
targets.set(targetKey(target), target)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const chats: UserMdView[] = []
|
|
134
|
+
for (const key of [...targets.keys()].sort()) {
|
|
135
|
+
const target = targets.get(key)
|
|
136
|
+
if (!target) continue
|
|
137
|
+
const path = pathOf(runtime, target)
|
|
138
|
+
const info = await stat(path).catch(() => undefined)
|
|
139
|
+
const exists = info?.isFile() ?? false
|
|
140
|
+
const view: UserMdView = {
|
|
141
|
+
chat: key,
|
|
142
|
+
path,
|
|
143
|
+
exists,
|
|
144
|
+
bytes: info?.size ?? 0,
|
|
145
|
+
admitted: isTargetAllowed(runtime.config.access, target, runtime.config.ownerQQ),
|
|
146
|
+
}
|
|
147
|
+
if (exists && view.admitted) {
|
|
148
|
+
if ((info?.size ?? 0) > MAX_USER_MD_BYTES) {
|
|
149
|
+
// 手写的大档案不整份读进内存:列表只给元信息。
|
|
150
|
+
view.oversized = true
|
|
151
|
+
} else {
|
|
152
|
+
const text = await readFile(path, "utf8").catch(() => undefined)
|
|
153
|
+
if (text !== undefined) view.content = maskKnownSecrets(text, secrets)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
chats.push(view)
|
|
157
|
+
}
|
|
158
|
+
return { chats, maxBytes: MAX_USER_MD_BYTES }
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 上传一个聊天的档案。
|
|
163
|
+
*
|
|
164
|
+
* 整份覆盖:上传的就是全部内容,写之前把旧内容存成 `.bak`。
|
|
165
|
+
* 档案只给放行的聊天写:还没放行的聊天先在准入名单里放行,再上传。
|
|
166
|
+
*/
|
|
167
|
+
export async function saveUserMd(
|
|
168
|
+
runtime: PanelRuntime,
|
|
169
|
+
body: unknown,
|
|
170
|
+
): Promise<{ problems: DocumentProblem[]; result?: UserMdSaveResult }> {
|
|
171
|
+
const problems: DocumentProblem[] = []
|
|
172
|
+
if (!isPlainObject(body)) {
|
|
173
|
+
return { problems: [{ path: "", message: "请求体必须是一个 JSON 对象" }] }
|
|
174
|
+
}
|
|
175
|
+
const target = typeof body.chat === "string" ? parseTargetKey(body.chat) : undefined
|
|
176
|
+
if (!target) {
|
|
177
|
+
problems.push({ path: "chat", message: "聊天键必须是 group:<群号> 或 private:<账号>" })
|
|
178
|
+
}
|
|
179
|
+
const content = typeof body.content === "string" ? body.content : undefined
|
|
180
|
+
if (content === undefined) {
|
|
181
|
+
problems.push({ path: "content", message: "档案内容必须是字符串" })
|
|
182
|
+
}
|
|
183
|
+
if (!target || content === undefined) return { problems }
|
|
184
|
+
|
|
185
|
+
if (content.trim() === "") {
|
|
186
|
+
problems.push({ path: "content", message: "档案内容不能为空" })
|
|
187
|
+
}
|
|
188
|
+
const bytes = Buffer.byteLength(content, "utf8")
|
|
189
|
+
if (bytes > MAX_USER_MD_BYTES) {
|
|
190
|
+
problems.push({
|
|
191
|
+
path: "content",
|
|
192
|
+
message: `档案太大:最多 ${MAX_USER_MD_BYTES / 1024} KiB`,
|
|
193
|
+
})
|
|
194
|
+
}
|
|
195
|
+
if (!isTargetAllowed(runtime.config.access, target, runtime.config.ownerQQ)) {
|
|
196
|
+
problems.push({
|
|
197
|
+
path: "chat",
|
|
198
|
+
message: "这个聊天还没放行:先在准入名单里放行,再上传档案",
|
|
199
|
+
})
|
|
200
|
+
}
|
|
201
|
+
if (problems.length > 0) return { problems }
|
|
202
|
+
|
|
203
|
+
const path = pathOf(runtime, target)
|
|
204
|
+
await backupFile(path)
|
|
205
|
+
await writeTextAtomic(path, content)
|
|
206
|
+
const list = await listUserMd(runtime)
|
|
207
|
+
return {
|
|
208
|
+
problems: [],
|
|
209
|
+
result: {
|
|
210
|
+
chat: targetKey(target),
|
|
211
|
+
path,
|
|
212
|
+
bytes,
|
|
213
|
+
note: "已保存档案:新会话开始时会带上它,进行中的会话可以用 qq_read_user_md 读取。",
|
|
214
|
+
chats: list.chats,
|
|
215
|
+
maxBytes: MAX_USER_MD_BYTES,
|
|
216
|
+
},
|
|
217
|
+
}
|
|
218
|
+
}
|
package/src/safe-object.ts
CHANGED
|
@@ -26,6 +26,11 @@ export function hasKey(target: object, key: string): boolean {
|
|
|
26
26
|
return Object.hasOwn(target, key)
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
/** 值是不是普通对象:数组与 null 都不算。 */
|
|
30
|
+
export function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
31
|
+
return !!value && typeof value === "object" && !Array.isArray(value)
|
|
32
|
+
}
|
|
33
|
+
|
|
29
34
|
/**
|
|
30
35
|
* 把源对象里的键搬进一个没有原型的字典。
|
|
31
36
|
*
|
package/src/session/manager.ts
CHANGED
|
@@ -489,12 +489,23 @@ export class SessionManager {
|
|
|
489
489
|
private readonly canWrite?: () => boolean,
|
|
490
490
|
) {}
|
|
491
491
|
|
|
492
|
-
/**
|
|
493
|
-
|
|
492
|
+
/**
|
|
493
|
+
* 聊天目录是会话的工作目录,创建会话之前必须先落到磁盘上。
|
|
494
|
+
*
|
|
495
|
+
* 目录建过一次就不再重复建:消息热路径上不再有文件系统调用,
|
|
496
|
+
* 只有这个聊天第一次用到时才落一次盘。
|
|
497
|
+
*/
|
|
494
498
|
private ensureChatDirectory(target: ChatTarget): void {
|
|
495
|
-
|
|
499
|
+
const directory = this.locationFor(target).directory
|
|
500
|
+
if (this.ensuredDirectories.has(directory)) return
|
|
501
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 })
|
|
502
|
+
this.ensuredDirectories.add(directory)
|
|
496
503
|
}
|
|
497
504
|
|
|
505
|
+
/** 已经建好的聊天目录。 */
|
|
506
|
+
private readonly ensuredDirectories = new Set<string>()
|
|
507
|
+
|
|
508
|
+
/** 每个聊天的会话各自落在自己的工作目录里。 */
|
|
498
509
|
private locationFor(target: ChatTarget): Location.PublicRef {
|
|
499
510
|
const { directory } = chatWorkspace(this.home.directory, target)
|
|
500
511
|
// 目录由聊天目标拼出来,这里只是把字符串还原成品牌类型。
|
|
@@ -2187,7 +2198,7 @@ export class SessionManager {
|
|
|
2187
2198
|
}
|
|
2188
2199
|
}
|
|
2189
2200
|
|
|
2190
|
-
const
|
|
2201
|
+
const createOptions = {
|
|
2191
2202
|
title: targetSessionTitle(target),
|
|
2192
2203
|
location: this.locationFor(target),
|
|
2193
2204
|
...(agent ? { agent } : {}),
|
|
@@ -2196,7 +2207,16 @@ export class SessionManager {
|
|
|
2196
2207
|
qqTargetKind: target.kind,
|
|
2197
2208
|
qqTargetId: target.id,
|
|
2198
2209
|
},
|
|
2199
|
-
}
|
|
2210
|
+
}
|
|
2211
|
+
let created: Awaited<ReturnType<typeof this.ctx.session.create>>
|
|
2212
|
+
try {
|
|
2213
|
+
created = await this.ctx.session.create(createOptions)
|
|
2214
|
+
} catch {
|
|
2215
|
+
// 聊天目录可能在运行期间被手工删掉:补建一次再试。
|
|
2216
|
+
this.ensuredDirectories.delete(createOptions.location.directory)
|
|
2217
|
+
this.ensureChatDirectory(target)
|
|
2218
|
+
created = await this.ctx.session.create(createOptions)
|
|
2219
|
+
}
|
|
2200
2220
|
if (!this.bindSession(state, created.id)) {
|
|
2201
2221
|
this.logger.error(`${label} 新建会话 ${created.id} 已被其他目标占用`)
|
|
2202
2222
|
await this.ctx.session.remove({ sessionID: created.id }).catch((error: unknown) => {
|
package/src/user/store.ts
CHANGED
|
@@ -14,6 +14,16 @@ export const USER_PROFILE_NAME = "PROFILE.md"
|
|
|
14
14
|
/** 画像的写入方式:整份覆盖,或追加到末尾。 */
|
|
15
15
|
export type ProfileWriteMode = "replace" | "append"
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* 用户档案的路径:`<home>/chats/<kind>-<id>/USER.md`。
|
|
19
|
+
*
|
|
20
|
+
* 单独成函数:面板上传档案时要算出同一个路径,
|
|
21
|
+
* 路径只在这里定一次,别处不再拼 `chats/<kind>-<id>/USER.md`。
|
|
22
|
+
*/
|
|
23
|
+
export function userMdPath(home: string, target: ChatTarget): string {
|
|
24
|
+
return join(chatWorkspace(home, target).root, USER_MD_NAME)
|
|
25
|
+
}
|
|
26
|
+
|
|
17
27
|
/**
|
|
18
28
|
* 用户档案(USER.md)与用户画像的存取。
|
|
19
29
|
*
|
|
@@ -33,7 +43,7 @@ export class UserStore {
|
|
|
33
43
|
|
|
34
44
|
/** 用户档案的路径:`<home>/chats/<kind>-<id>/USER.md`。 */
|
|
35
45
|
userMdPath(target: ChatTarget): string {
|
|
36
|
-
return
|
|
46
|
+
return userMdPath(this.home, target)
|
|
37
47
|
}
|
|
38
48
|
|
|
39
49
|
/** 用户画像的路径:`<home>/chats/<kind>-<id>/workspace/PROFILE.md`。 */
|