@linjz/opencode-qqbot 0.3.4 → 0.3.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/README.md +74 -37
- package/bin/openqq-setup.mjs +50 -50
- package/bin/openqq.js +47 -47
- package/dist/opencode-qqbot-plugin.js +10 -1
- package/dist/openqq-bot.js +360 -69
- package/package.json +69 -59
- package/src/_FOLDER.md +19 -19
- package/src/bridge.ts +934 -796
- package/src/commands.ts +554 -514
- package/src/config.ts +121 -121
- package/src/core/bot-launcher.ts +146 -134
- package/src/core/card-approval.ts +146 -146
- package/src/core/context-buffer.ts +78 -78
- package/src/core/delivery.ts +284 -284
- package/src/core/discovery.ts +151 -151
- package/src/core/error-rescue.ts +33 -0
- package/src/core/file-log.ts +45 -45
- package/src/core/form-bridge.ts +304 -304
- package/src/core/inflight-journal.ts +212 -151
- package/src/core/instance-lock.ts +122 -122
- package/src/core/linkwatch.ts +195 -195
- package/src/core/message-queue.ts +80 -0
- package/src/core/mirror.ts +173 -173
- package/src/core/permission.ts +301 -301
- package/src/core/reply-limiter.ts +106 -106
- package/src/core/send-media.ts +90 -90
- package/src/core/util.ts +8 -8
- package/src/index.ts +162 -162
- package/src/opencode/_FOLDER.md +11 -11
- package/src/opencode/client.ts +387 -386
- package/src/opencode/eventstream.ts +138 -138
- package/src/opencode/history.ts +143 -143
- package/src/opencode/mirror.ts +65 -65
- package/src/opencode/sessions.ts +158 -158
- package/src/plugin-entry.ts +112 -112
- package/src/qq/_FOLDER.md +13 -13
- package/src/qq/api.ts +539 -539
- package/src/qq/gateway.ts +492 -492
- package/src/qq/media.ts +219 -204
- package/src/qq/sender.ts +300 -300
- package/src/qq/types.ts +154 -152
- package/tsconfig.json +24 -24
package/src/opencode/mirror.ts
CHANGED
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
// @input: ./client (SessionMessage), ./history (redactSecrets)
|
|
2
|
-
// @output: buildDigest
|
|
3
|
-
// @pos: opencode层 - 摘要镜像(桌面轮次完成 → QQ 摘要卡)
|
|
4
|
-
//
|
|
5
|
-
// GET /session/{id}/message 实测倒序(新→旧):最后一条 user 消息至末尾 = 最近一轮。
|
|
6
|
-
// 无 assistant 正文的轮次(纯工具/中断)返回 null 不镜像,避免噪音(E1 债务清单)。
|
|
7
|
-
|
|
8
|
-
import type { SessionMessage } from "./client.js"
|
|
9
|
-
import { redactSecrets } from "./history.js"
|
|
10
|
-
|
|
11
|
-
const USER_CLIP = 160
|
|
12
|
-
|
|
13
|
-
// E2(2026-10-29,用户要求"摘要回全文"):答案截断上限改为运行时可配——
|
|
14
|
-
// 默认 0=不截断回全文(投递层 splitMessage 自动分条送达);设
|
|
15
|
-
// QQ_DIGEST_ANSWER_MAX_CHARS>0 可恢复旧的截断模式。用户提问行(USER_CLIP)
|
|
16
|
-
// 保持截断不变(问题行只是定位上下文,全文意义不大)。
|
|
17
|
-
function answerClipLimit(): number {
|
|
18
|
-
const n = Number(process.env.QQ_DIGEST_ANSWER_MAX_CHARS ?? "0")
|
|
19
|
-
return Number.isFinite(n) && n > 0 ? Math.floor(n) : 0
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// 构建最近一轮问答的摘要卡;返回 null = 无可镜像正文。
|
|
23
|
-
export function buildDigest(messages: SessionMessage[]): string | null {
|
|
24
|
-
const ordered = [...messages].reverse()
|
|
25
|
-
|
|
26
|
-
let lastUserIdx = -1
|
|
27
|
-
for (let i = ordered.length - 1; i >= 0; i--) {
|
|
28
|
-
if (ordered[i].type === "user") {
|
|
29
|
-
lastUserIdx = i
|
|
30
|
-
break
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
if (lastUserIdx < 0) return null
|
|
34
|
-
|
|
35
|
-
const userText = ((ordered[lastUserIdx] as { text?: string }).text ?? "").trim()
|
|
36
|
-
|
|
37
|
-
const answerParts: string[] = []
|
|
38
|
-
for (let i = lastUserIdx + 1; i < ordered.length; i++) {
|
|
39
|
-
const m = ordered[i]
|
|
40
|
-
if (m.type === "assistant") {
|
|
41
|
-
for (const p of m.content ?? []) {
|
|
42
|
-
if (p.type === "text" && p.text?.trim()) answerParts.push(p.text.trim())
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
const answer = answerParts.join("\n").trim()
|
|
47
|
-
if (!answer) return null
|
|
48
|
-
|
|
49
|
-
const lines = ["📋 桌面问答摘要"]
|
|
50
|
-
if (userText) lines.push(`🧑 ${clip(userText, USER_CLIP)}`)
|
|
51
|
-
const clipLimit = answerClipLimit()
|
|
52
|
-
if (clipLimit > 0) {
|
|
53
|
-
lines.push(`🤖 ${clip(answer, clipLimit)}`)
|
|
54
|
-
if (answer.length > clipLimit) lines.push(`(答案共 ${answer.length} 字,回 /history 看更多)`)
|
|
55
|
-
} else {
|
|
56
|
-
// 全文模式:保留原文换行/排版(clip 的空白折叠只用于截断模式)
|
|
57
|
-
lines.push(`🤖 ${answer}`)
|
|
58
|
-
}
|
|
59
|
-
return redactSecrets(lines.join("\n"))
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function clip(value: string, max: number): string {
|
|
63
|
-
const s = value.replace(/\s+/g, " ").trim()
|
|
64
|
-
return s.length > max ? `${s.slice(0, max)}…` : s
|
|
65
|
-
}
|
|
1
|
+
// @input: ./client (SessionMessage), ./history (redactSecrets)
|
|
2
|
+
// @output: buildDigest
|
|
3
|
+
// @pos: opencode层 - 摘要镜像(桌面轮次完成 → QQ 摘要卡)
|
|
4
|
+
//
|
|
5
|
+
// GET /session/{id}/message 实测倒序(新→旧):最后一条 user 消息至末尾 = 最近一轮。
|
|
6
|
+
// 无 assistant 正文的轮次(纯工具/中断)返回 null 不镜像,避免噪音(E1 债务清单)。
|
|
7
|
+
|
|
8
|
+
import type { SessionMessage } from "./client.js"
|
|
9
|
+
import { redactSecrets } from "./history.js"
|
|
10
|
+
|
|
11
|
+
const USER_CLIP = 160
|
|
12
|
+
|
|
13
|
+
// E2(2026-10-29,用户要求"摘要回全文"):答案截断上限改为运行时可配——
|
|
14
|
+
// 默认 0=不截断回全文(投递层 splitMessage 自动分条送达);设
|
|
15
|
+
// QQ_DIGEST_ANSWER_MAX_CHARS>0 可恢复旧的截断模式。用户提问行(USER_CLIP)
|
|
16
|
+
// 保持截断不变(问题行只是定位上下文,全文意义不大)。
|
|
17
|
+
function answerClipLimit(): number {
|
|
18
|
+
const n = Number(process.env.QQ_DIGEST_ANSWER_MAX_CHARS ?? "0")
|
|
19
|
+
return Number.isFinite(n) && n > 0 ? Math.floor(n) : 0
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// 构建最近一轮问答的摘要卡;返回 null = 无可镜像正文。
|
|
23
|
+
export function buildDigest(messages: SessionMessage[]): string | null {
|
|
24
|
+
const ordered = [...messages].reverse()
|
|
25
|
+
|
|
26
|
+
let lastUserIdx = -1
|
|
27
|
+
for (let i = ordered.length - 1; i >= 0; i--) {
|
|
28
|
+
if (ordered[i].type === "user") {
|
|
29
|
+
lastUserIdx = i
|
|
30
|
+
break
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (lastUserIdx < 0) return null
|
|
34
|
+
|
|
35
|
+
const userText = ((ordered[lastUserIdx] as { text?: string }).text ?? "").trim()
|
|
36
|
+
|
|
37
|
+
const answerParts: string[] = []
|
|
38
|
+
for (let i = lastUserIdx + 1; i < ordered.length; i++) {
|
|
39
|
+
const m = ordered[i]
|
|
40
|
+
if (m.type === "assistant") {
|
|
41
|
+
for (const p of m.content ?? []) {
|
|
42
|
+
if (p.type === "text" && p.text?.trim()) answerParts.push(p.text.trim())
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const answer = answerParts.join("\n").trim()
|
|
47
|
+
if (!answer) return null
|
|
48
|
+
|
|
49
|
+
const lines = ["📋 桌面问答摘要"]
|
|
50
|
+
if (userText) lines.push(`🧑 ${clip(userText, USER_CLIP)}`)
|
|
51
|
+
const clipLimit = answerClipLimit()
|
|
52
|
+
if (clipLimit > 0) {
|
|
53
|
+
lines.push(`🤖 ${clip(answer, clipLimit)}`)
|
|
54
|
+
if (answer.length > clipLimit) lines.push(`(答案共 ${answer.length} 字,回 /history 看更多)`)
|
|
55
|
+
} else {
|
|
56
|
+
// 全文模式:保留原文换行/排版(clip 的空白折叠只用于截断模式)
|
|
57
|
+
lines.push(`🤖 ${answer}`)
|
|
58
|
+
}
|
|
59
|
+
return redactSecrets(lines.join("\n"))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function clip(value: string, max: number): string {
|
|
63
|
+
const s = value.replace(/\s+/g, " ").trim()
|
|
64
|
+
return s.length > max ? `${s.slice(0, max)}…` : s
|
|
65
|
+
}
|
package/src/opencode/sessions.ts
CHANGED
|
@@ -1,158 +1,158 @@
|
|
|
1
|
-
// @input: ./client (OpencodeClient)
|
|
2
|
-
// @output: SessionManager, UserSession
|
|
3
|
-
// @pos: opencode层 - QQ用户<->OpenCode Session 映射管理(持久化到 ~/.openqq/sessions.json)
|
|
4
|
-
import { existsSync, readFileSync, mkdirSync, writeFileSync } from "fs"
|
|
5
|
-
import { join } from "path"
|
|
6
|
-
import { homedir } from "os"
|
|
7
|
-
import type { OpencodeClient } from "./client.js"
|
|
8
|
-
|
|
9
|
-
interface UserSession {
|
|
10
|
-
sessionId: string
|
|
11
|
-
title?: string
|
|
12
|
-
modelId?: string
|
|
13
|
-
providerId?: string
|
|
14
|
-
agentId?: string
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const STORE_DIR = join(homedir(), ".openqq")
|
|
18
|
-
const STORE_FILE = join(STORE_DIR, "sessions.json")
|
|
19
|
-
|
|
20
|
-
export class SessionManager {
|
|
21
|
-
private sessions = new Map<string, UserSession>()
|
|
22
|
-
private client: OpencodeClient
|
|
23
|
-
|
|
24
|
-
constructor(client: OpencodeClient) {
|
|
25
|
-
this.client = client
|
|
26
|
-
this.loadSessions()
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// 持久化映射:bot 重启(升级/故障)不再丢失 QQ↔会话绑定
|
|
30
|
-
//(2026-10-13 实测教训:重启清空内存映射 → 用户 QQ 消息落到全新会话,AI 失忆从零探索)。
|
|
31
|
-
private loadSessions(): void {
|
|
32
|
-
try {
|
|
33
|
-
if (!existsSync(STORE_FILE)) return
|
|
34
|
-
// 手工编辑或 PowerShell Out-File 常带 BOM,JSON.parse 会炸——先剥掉
|
|
35
|
-
const raw = readFileSync(STORE_FILE, "utf8").replace(/^\uFEFF/, "")
|
|
36
|
-
const parsed = JSON.parse(raw) as Record<string, UserSession>
|
|
37
|
-
for (const [userId, session] of Object.entries(parsed)) {
|
|
38
|
-
if (session?.sessionId) this.sessions.set(userId, session)
|
|
39
|
-
}
|
|
40
|
-
if (this.sessions.size > 0) {
|
|
41
|
-
console.log(`[sessions] 已从磁盘恢复 ${this.sessions.size} 个用户会话映射`)
|
|
42
|
-
}
|
|
43
|
-
} catch (error) {
|
|
44
|
-
console.error("[sessions] 会话映射加载失败(忽略,按全新处理):", error)
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
private saveSessions(): void {
|
|
49
|
-
try {
|
|
50
|
-
if (!existsSync(STORE_DIR)) mkdirSync(STORE_DIR, { recursive: true })
|
|
51
|
-
writeFileSync(STORE_FILE, JSON.stringify(Object.fromEntries(this.sessions), null, 2), "utf8")
|
|
52
|
-
} catch (error) {
|
|
53
|
-
console.error("[sessions] 会话映射持久化失败:", error)
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
async getOrCreate(userId: string): Promise<UserSession> {
|
|
58
|
-
const existing = this.sessions.get(userId)
|
|
59
|
-
if (existing) return existing
|
|
60
|
-
|
|
61
|
-
const result = await this.client.session.create({})
|
|
62
|
-
const session: UserSession = {
|
|
63
|
-
sessionId: result.data!.id,
|
|
64
|
-
title: result.data!.title,
|
|
65
|
-
}
|
|
66
|
-
this.sessions.set(userId, session)
|
|
67
|
-
this.saveSessions()
|
|
68
|
-
return session
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async createNew(userId: string): Promise<UserSession> {
|
|
72
|
-
const result = await this.client.session.create({})
|
|
73
|
-
const session: UserSession = {
|
|
74
|
-
sessionId: result.data!.id,
|
|
75
|
-
title: result.data!.title,
|
|
76
|
-
}
|
|
77
|
-
this.sessions.set(userId, session)
|
|
78
|
-
this.saveSessions()
|
|
79
|
-
return session
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
switchSession(userId: string, sessionId: string, title?: string): void {
|
|
83
|
-
const prev = this.sessions.get(userId)
|
|
84
|
-
const changed = prev?.sessionId !== sessionId
|
|
85
|
-
this.sessions.set(userId, {
|
|
86
|
-
...prev,
|
|
87
|
-
sessionId,
|
|
88
|
-
title,
|
|
89
|
-
})
|
|
90
|
-
this.saveSessions()
|
|
91
|
-
// 切到别的会话时,把本用户记住的模型/Agent 重放到目标会话
|
|
92
|
-
//(v2 模型是 session 级设置,不跟随 prompt)。
|
|
93
|
-
if (changed) {
|
|
94
|
-
void this.applySettings(userId)
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
getSession(userId: string): UserSession | undefined {
|
|
99
|
-
return this.sessions.get(userId)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// 枚举全部映射(摘要镜像订阅、会话→用户反查用)
|
|
103
|
-
all(): Array<{ userId: string; sessionId: string }> {
|
|
104
|
-
return [...this.sessions.entries()].map(([userId, s]) => ({ userId, sessionId: s.sessionId }))
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// v2: 模型/agent 是 session 级(/prompt body 会被剥离),必须调 session 端点。
|
|
108
|
-
// 先服务端成功再落本地,避免 /model 显示成功但实际没生效。
|
|
109
|
-
async setModel(userId: string, providerId: string, modelId: string): Promise<void> {
|
|
110
|
-
const s = this.sessions.get(userId)
|
|
111
|
-
if (!s) return
|
|
112
|
-
await this.client.session.setModel({
|
|
113
|
-
path: { id: s.sessionId },
|
|
114
|
-
body: { model: { providerID: providerId, id: modelId } },
|
|
115
|
-
})
|
|
116
|
-
s.providerId = providerId
|
|
117
|
-
s.modelId = modelId
|
|
118
|
-
this.saveSessions()
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
async setAgent(userId: string, agentId: string): Promise<void> {
|
|
122
|
-
const s = this.sessions.get(userId)
|
|
123
|
-
if (!s) return
|
|
124
|
-
await this.client.session.setAgent({ path: { id: s.sessionId }, body: { agent: agentId } })
|
|
125
|
-
s.agentId = agentId
|
|
126
|
-
this.saveSessions()
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
private async applySettings(userId: string): Promise<void> {
|
|
130
|
-
const s = this.sessions.get(userId)
|
|
131
|
-
if (!s) return
|
|
132
|
-
try {
|
|
133
|
-
if (s.providerId && s.modelId) {
|
|
134
|
-
await this.client.session.setModel({
|
|
135
|
-
path: { id: s.sessionId },
|
|
136
|
-
body: { model: { providerID: s.providerId, id: s.modelId } },
|
|
137
|
-
})
|
|
138
|
-
}
|
|
139
|
-
if (s.agentId) {
|
|
140
|
-
await this.client.session.setAgent({ path: { id: s.sessionId }, body: { agent: s.agentId } })
|
|
141
|
-
}
|
|
142
|
-
} catch (error) {
|
|
143
|
-
// 切会话是主操作,设置重放失败只记日志(目标会话用其自身/默认设置)
|
|
144
|
-
console.error("[sessions] 重放模型/Agent 设置失败:", error)
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
getModel(userId: string): { providerId?: string; modelId?: string } {
|
|
149
|
-
const s = this.sessions.get(userId)
|
|
150
|
-
return { providerId: s?.providerId, modelId: s?.modelId }
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
getAgent(userId: string): string | undefined {
|
|
154
|
-
return this.sessions.get(userId)?.agentId
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export type { UserSession }
|
|
1
|
+
// @input: ./client (OpencodeClient)
|
|
2
|
+
// @output: SessionManager, UserSession
|
|
3
|
+
// @pos: opencode层 - QQ用户<->OpenCode Session 映射管理(持久化到 ~/.openqq/sessions.json)
|
|
4
|
+
import { existsSync, readFileSync, mkdirSync, writeFileSync } from "fs"
|
|
5
|
+
import { join } from "path"
|
|
6
|
+
import { homedir } from "os"
|
|
7
|
+
import type { OpencodeClient } from "./client.js"
|
|
8
|
+
|
|
9
|
+
interface UserSession {
|
|
10
|
+
sessionId: string
|
|
11
|
+
title?: string
|
|
12
|
+
modelId?: string
|
|
13
|
+
providerId?: string
|
|
14
|
+
agentId?: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const STORE_DIR = join(homedir(), ".openqq")
|
|
18
|
+
const STORE_FILE = join(STORE_DIR, "sessions.json")
|
|
19
|
+
|
|
20
|
+
export class SessionManager {
|
|
21
|
+
private sessions = new Map<string, UserSession>()
|
|
22
|
+
private client: OpencodeClient
|
|
23
|
+
|
|
24
|
+
constructor(client: OpencodeClient) {
|
|
25
|
+
this.client = client
|
|
26
|
+
this.loadSessions()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// 持久化映射:bot 重启(升级/故障)不再丢失 QQ↔会话绑定
|
|
30
|
+
//(2026-10-13 实测教训:重启清空内存映射 → 用户 QQ 消息落到全新会话,AI 失忆从零探索)。
|
|
31
|
+
private loadSessions(): void {
|
|
32
|
+
try {
|
|
33
|
+
if (!existsSync(STORE_FILE)) return
|
|
34
|
+
// 手工编辑或 PowerShell Out-File 常带 BOM,JSON.parse 会炸——先剥掉
|
|
35
|
+
const raw = readFileSync(STORE_FILE, "utf8").replace(/^\uFEFF/, "")
|
|
36
|
+
const parsed = JSON.parse(raw) as Record<string, UserSession>
|
|
37
|
+
for (const [userId, session] of Object.entries(parsed)) {
|
|
38
|
+
if (session?.sessionId) this.sessions.set(userId, session)
|
|
39
|
+
}
|
|
40
|
+
if (this.sessions.size > 0) {
|
|
41
|
+
console.log(`[sessions] 已从磁盘恢复 ${this.sessions.size} 个用户会话映射`)
|
|
42
|
+
}
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.error("[sessions] 会话映射加载失败(忽略,按全新处理):", error)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private saveSessions(): void {
|
|
49
|
+
try {
|
|
50
|
+
if (!existsSync(STORE_DIR)) mkdirSync(STORE_DIR, { recursive: true })
|
|
51
|
+
writeFileSync(STORE_FILE, JSON.stringify(Object.fromEntries(this.sessions), null, 2), "utf8")
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error("[sessions] 会话映射持久化失败:", error)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async getOrCreate(userId: string): Promise<UserSession> {
|
|
58
|
+
const existing = this.sessions.get(userId)
|
|
59
|
+
if (existing) return existing
|
|
60
|
+
|
|
61
|
+
const result = await this.client.session.create({})
|
|
62
|
+
const session: UserSession = {
|
|
63
|
+
sessionId: result.data!.id,
|
|
64
|
+
title: result.data!.title,
|
|
65
|
+
}
|
|
66
|
+
this.sessions.set(userId, session)
|
|
67
|
+
this.saveSessions()
|
|
68
|
+
return session
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async createNew(userId: string): Promise<UserSession> {
|
|
72
|
+
const result = await this.client.session.create({})
|
|
73
|
+
const session: UserSession = {
|
|
74
|
+
sessionId: result.data!.id,
|
|
75
|
+
title: result.data!.title,
|
|
76
|
+
}
|
|
77
|
+
this.sessions.set(userId, session)
|
|
78
|
+
this.saveSessions()
|
|
79
|
+
return session
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
switchSession(userId: string, sessionId: string, title?: string): void {
|
|
83
|
+
const prev = this.sessions.get(userId)
|
|
84
|
+
const changed = prev?.sessionId !== sessionId
|
|
85
|
+
this.sessions.set(userId, {
|
|
86
|
+
...prev,
|
|
87
|
+
sessionId,
|
|
88
|
+
title,
|
|
89
|
+
})
|
|
90
|
+
this.saveSessions()
|
|
91
|
+
// 切到别的会话时,把本用户记住的模型/Agent 重放到目标会话
|
|
92
|
+
//(v2 模型是 session 级设置,不跟随 prompt)。
|
|
93
|
+
if (changed) {
|
|
94
|
+
void this.applySettings(userId)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
getSession(userId: string): UserSession | undefined {
|
|
99
|
+
return this.sessions.get(userId)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 枚举全部映射(摘要镜像订阅、会话→用户反查用)
|
|
103
|
+
all(): Array<{ userId: string; sessionId: string }> {
|
|
104
|
+
return [...this.sessions.entries()].map(([userId, s]) => ({ userId, sessionId: s.sessionId }))
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// v2: 模型/agent 是 session 级(/prompt body 会被剥离),必须调 session 端点。
|
|
108
|
+
// 先服务端成功再落本地,避免 /model 显示成功但实际没生效。
|
|
109
|
+
async setModel(userId: string, providerId: string, modelId: string): Promise<void> {
|
|
110
|
+
const s = this.sessions.get(userId)
|
|
111
|
+
if (!s) return
|
|
112
|
+
await this.client.session.setModel({
|
|
113
|
+
path: { id: s.sessionId },
|
|
114
|
+
body: { model: { providerID: providerId, id: modelId } },
|
|
115
|
+
})
|
|
116
|
+
s.providerId = providerId
|
|
117
|
+
s.modelId = modelId
|
|
118
|
+
this.saveSessions()
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async setAgent(userId: string, agentId: string): Promise<void> {
|
|
122
|
+
const s = this.sessions.get(userId)
|
|
123
|
+
if (!s) return
|
|
124
|
+
await this.client.session.setAgent({ path: { id: s.sessionId }, body: { agent: agentId } })
|
|
125
|
+
s.agentId = agentId
|
|
126
|
+
this.saveSessions()
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private async applySettings(userId: string): Promise<void> {
|
|
130
|
+
const s = this.sessions.get(userId)
|
|
131
|
+
if (!s) return
|
|
132
|
+
try {
|
|
133
|
+
if (s.providerId && s.modelId) {
|
|
134
|
+
await this.client.session.setModel({
|
|
135
|
+
path: { id: s.sessionId },
|
|
136
|
+
body: { model: { providerID: s.providerId, id: s.modelId } },
|
|
137
|
+
})
|
|
138
|
+
}
|
|
139
|
+
if (s.agentId) {
|
|
140
|
+
await this.client.session.setAgent({ path: { id: s.sessionId }, body: { agent: s.agentId } })
|
|
141
|
+
}
|
|
142
|
+
} catch (error) {
|
|
143
|
+
// 切会话是主操作,设置重放失败只记日志(目标会话用其自身/默认设置)
|
|
144
|
+
console.error("[sessions] 重放模型/Agent 设置失败:", error)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
getModel(userId: string): { providerId?: string; modelId?: string } {
|
|
149
|
+
const s = this.sessions.get(userId)
|
|
150
|
+
return { providerId: s?.providerId, modelId: s?.modelId }
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
getAgent(userId: string): string | undefined {
|
|
154
|
+
return this.sessions.get(userId)?.agentId
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export type { UserSession }
|