@linjz/opencode-qqbot 0.0.0-stage → 0.3.1
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/LICENSE +21 -0
- package/README.md +134 -3
- package/bin/openqq.js +2 -0
- package/package.json +42 -4
- package/src/_FOLDER.md +19 -0
- package/src/bridge.ts +731 -0
- package/src/commands.ts +514 -0
- package/src/config.ts +121 -0
- package/src/core/card-approval.ts +146 -0
- package/src/core/context-buffer.ts +52 -0
- package/src/core/delivery.ts +263 -0
- package/src/core/discovery.ts +151 -0
- package/src/core/form-bridge.ts +304 -0
- package/src/core/linkwatch.ts +195 -0
- package/src/core/mirror.ts +173 -0
- package/src/core/permission.ts +301 -0
- package/src/core/reply-limiter.ts +106 -0
- package/src/core/send-media.ts +90 -0
- package/src/core/util.ts +8 -0
- package/src/index.ts +134 -0
- package/src/opencode/_FOLDER.md +11 -0
- package/src/opencode/client.ts +386 -0
- package/src/opencode/eventstream.ts +138 -0
- package/src/opencode/history.ts +143 -0
- package/src/opencode/mirror.ts +65 -0
- package/src/opencode/sessions.ts +158 -0
- package/src/qq/_FOLDER.md +13 -0
- package/src/qq/api.ts +519 -0
- package/src/qq/gateway.ts +486 -0
- package/src/qq/media.ts +204 -0
- package/src/qq/sender.ts +300 -0
- package/src/qq/types.ts +146 -0
- package/tsconfig.json +24 -0
package/src/bridge.ts
ADDED
|
@@ -0,0 +1,731 @@
|
|
|
1
|
+
// @input: ./config, ./qq/* (types, api, sender), ./opencode/* (client, sessions), ./commands, ./core/*
|
|
2
|
+
// @output: createBridge, isAllowedUser, milestoneTargets, loadWatchedMap, saveWatchedMap,
|
|
3
|
+
// (O7b 再导出)parsePermissionDecision, formatPermissionPrompt
|
|
4
|
+
// @pos: 根层 - 核心桥接: QQ 消息 -> OpenCode -> QQ 回复
|
|
5
|
+
import { existsSync, readFileSync, mkdirSync, writeFileSync } from "fs"
|
|
6
|
+
import { join } from "path"
|
|
7
|
+
import { homedir } from "os"
|
|
8
|
+
import type { Config } from "./config.js"
|
|
9
|
+
import type { MessageContext } from "./qq/types.js"
|
|
10
|
+
import { getAccessToken, sendC2CInputNotify, sendC2CMessage, sendGroupMessage } from "./qq/api.js"
|
|
11
|
+
import { prepareQQAttachments, augmentPromptWithAttachments, purgeOldQQImages } from "./qq/media.js"
|
|
12
|
+
import type { OpencodeClient } from "./opencode/client.js"
|
|
13
|
+
import { collectReplyText } from "./opencode/client.js"
|
|
14
|
+
import { createEventStream, type V2Event } from "./opencode/eventstream.js"
|
|
15
|
+
import { summarizeInput, redactSecrets } from "./opencode/history.js"
|
|
16
|
+
import { SessionManager } from "./opencode/sessions.js"
|
|
17
|
+
import {
|
|
18
|
+
buildHelpText,
|
|
19
|
+
handleCommand,
|
|
20
|
+
handlePendingSelection,
|
|
21
|
+
isCommand,
|
|
22
|
+
type CommandContext,
|
|
23
|
+
type MirrorApi,
|
|
24
|
+
type PendingSelection,
|
|
25
|
+
type WatchApi,
|
|
26
|
+
} from "./commands.js"
|
|
27
|
+
import { createMirror } from "./core/mirror.js"
|
|
28
|
+
import {
|
|
29
|
+
createPermissionGate,
|
|
30
|
+
parsePermissionDecision,
|
|
31
|
+
formatPermissionPrompt,
|
|
32
|
+
DECISION_LABELS,
|
|
33
|
+
} from "./core/permission.js"
|
|
34
|
+
import { truncate } from "./core/util.js"
|
|
35
|
+
import { createDelivery } from "./core/delivery.js"
|
|
36
|
+
import { handleApprovalInteraction, sendApprovalCard } from "./core/card-approval.js"
|
|
37
|
+
import { createFormBridge, renderFormPrompt } from "./core/form-bridge.js"
|
|
38
|
+
import { recordMessage, resolveQuote, scopeKeyOf } from "./core/context-buffer.js"
|
|
39
|
+
import { extractSendFileMarkers, sendLocalFile } from "./core/send-media.js"
|
|
40
|
+
import type { InteractionEvent } from "./qq/types.js"
|
|
41
|
+
// O7b:解析/格式化保持经 bridge 再导出——单测与既有调用方导入路径不变
|
|
42
|
+
export { parsePermissionDecision, formatPermissionPrompt } from "./core/permission.js"
|
|
43
|
+
|
|
44
|
+
// E 闲置回收纯函数(导出供测试,2026-10-29):返回应回收的闲置会话 id——
|
|
45
|
+
// 超过 idleMs 无活动且未被 /watch 显式盯住的会话。
|
|
46
|
+
export function evictableIdleSessions(
|
|
47
|
+
lastActivity: ReadonlyMap<string, number>,
|
|
48
|
+
watched: ReadonlySet<string>,
|
|
49
|
+
now: number,
|
|
50
|
+
idleMs: number,
|
|
51
|
+
): string[] {
|
|
52
|
+
const out: string[] = []
|
|
53
|
+
for (const [sid, at] of lastActivity) {
|
|
54
|
+
if (watched.has(sid)) continue
|
|
55
|
+
if (now - at > idleMs) out.push(sid)
|
|
56
|
+
}
|
|
57
|
+
return out
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// H2 停滞提醒决策(导出供测试,2026-10-30):bgWait 零进展时提醒用户处置桌面弹窗。
|
|
61
|
+
// 首次:startedAt 后 firstMs;之后:上次进展/提醒后 intervalMs;最多 maxNudges 次。
|
|
62
|
+
export function stallNudgeDecision(
|
|
63
|
+
startedAt: number,
|
|
64
|
+
lastProgressAt: number,
|
|
65
|
+
nudges: number,
|
|
66
|
+
now: number,
|
|
67
|
+
firstMs: number,
|
|
68
|
+
intervalMs: number,
|
|
69
|
+
maxNudges: number,
|
|
70
|
+
): "nudge" | "wait" {
|
|
71
|
+
if (nudges >= maxNudges) return "wait"
|
|
72
|
+
const base = nudges === 0 ? startedAt : lastProgressAt
|
|
73
|
+
const need = nudges === 0 ? firstMs : intervalMs
|
|
74
|
+
return now - base >= need ? "nudge" : "wait"
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
interface Bridge {
|
|
78
|
+
handleMessage: (ctx: MessageContext) => Promise<void>
|
|
79
|
+
// HC2(2026-10-26):暴露事件流句柄,index 侧链路自愈巡检需对其热换基
|
|
80
|
+
eventStream: ReturnType<typeof createEventStream>
|
|
81
|
+
// A 项(2026-10-29):卡片按钮互动回调(index 侧挂到 QQ 网关 onInteraction)
|
|
82
|
+
handleInteraction: (event: InteractionEvent) => void
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function createBridge(
|
|
86
|
+
config: Config,
|
|
87
|
+
client: OpencodeClient,
|
|
88
|
+
sessions: SessionManager,
|
|
89
|
+
): Bridge {
|
|
90
|
+
const busyUsers = new Set<string>()
|
|
91
|
+
// Persist greeted users to disk so restarting the bot doesn't re-send the
|
|
92
|
+
// full command list to every user on their next message.
|
|
93
|
+
const greeted = loadGreetedSet()
|
|
94
|
+
const pendingSelections = new Map<string, PendingSelection>()
|
|
95
|
+
|
|
96
|
+
// ===== 实时观察 / 回合过程推送 =====
|
|
97
|
+
// 单条 SSE 连接按 sessionID 分流,多订阅者共享(同会话同 userId Set 去重不双发)。
|
|
98
|
+
// 里程碑投递 = /watch 专属(2026-10-14 拍板);sessionSubs 只管 SSE 活性与去重。
|
|
99
|
+
const eventStream = createEventStream(client.baseUrl)
|
|
100
|
+
const sessionSubs = new Map<string, Set<string>>() // sessionID -> userIds
|
|
101
|
+
const sessionUnsubs = new Map<string, () => void>() // sessionID -> SSE 退订
|
|
102
|
+
const activeWatches = new Map<string, string>() // userId -> sessionID(/watch 显式订阅)
|
|
103
|
+
const lastCtx = new Map<string, MessageContext>() // userId -> 最近消息 ctx(推送通道选择依据)
|
|
104
|
+
const bgWaits = new Map<string, { cancel: () => void }>() // 会话 -> 后台等待
|
|
105
|
+
const callCache = new Map<string, string>() // callID -> 命令摘要(⚠️ 上下文用)
|
|
106
|
+
|
|
107
|
+
// E(2026-10-29 同步升级,吸收 dsh session/idle-evictor):闲置会话回收——
|
|
108
|
+
// 长期无活动的会话退订 SSE,降 opencode 服务端事件扇出;/watch 显式盯着的
|
|
109
|
+
// 会话豁免;用户再来消息时 subscribeSession 会重新挂上。env QQ_SESSION_IDLE_HOURS。
|
|
110
|
+
const sessionLastActivity = new Map<string, number>()
|
|
111
|
+
const IDLE_EVICT_MS = (() => {
|
|
112
|
+
const h = Number(process.env.QQ_SESSION_IDLE_HOURS ?? "6")
|
|
113
|
+
return Number.isFinite(h) && h > 0 ? Math.floor(h * 3600_000) : 6 * 3600_000
|
|
114
|
+
})()
|
|
115
|
+
setInterval(() => {
|
|
116
|
+
const now = Date.now()
|
|
117
|
+
for (const sid of evictableIdleSessions(sessionLastActivity, new Set(activeWatches.values()), now, IDLE_EVICT_MS)) {
|
|
118
|
+
sessionUnsubs.get(sid)?.()
|
|
119
|
+
sessionUnsubs.delete(sid)
|
|
120
|
+
sessionSubs.delete(sid)
|
|
121
|
+
sessionLastActivity.delete(sid)
|
|
122
|
+
console.log(`[bridge] 闲置会话已回收(${Math.round(IDLE_EVICT_MS / 3600_000)}h 无活动): ${sid}`)
|
|
123
|
+
}
|
|
124
|
+
}, 30 * 60 * 1000)
|
|
125
|
+
|
|
126
|
+
function subscribeSession(sessionID: string, userId: string): void {
|
|
127
|
+
sessionLastActivity.set(sessionID, Date.now())
|
|
128
|
+
if (!sessionSubs.has(sessionID)) {
|
|
129
|
+
sessionSubs.set(sessionID, new Set())
|
|
130
|
+
sessionUnsubs.set(
|
|
131
|
+
sessionID,
|
|
132
|
+
eventStream.subscribe(sessionID, (ev) => onSessionEvent(sessionID, ev)),
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
sessionSubs.get(sessionID)!.add(userId)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function unsubscribeSession(sessionID: string, userId: string): void {
|
|
139
|
+
const set = sessionSubs.get(sessionID)
|
|
140
|
+
if (!set) return
|
|
141
|
+
set.delete(userId)
|
|
142
|
+
if (set.size === 0) {
|
|
143
|
+
sessionSubs.delete(sessionID)
|
|
144
|
+
sessionUnsubs.get(sessionID)?.()
|
|
145
|
+
sessionUnsubs.delete(sessionID)
|
|
146
|
+
sessionLastActivity.delete(sessionID)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function onSessionEvent(sessionID: string, ev: V2Event): void {
|
|
151
|
+
sessionLastActivity.set(sessionID, Date.now())
|
|
152
|
+
const line = formatMilestone(ev, callCache)
|
|
153
|
+
if (!line) return
|
|
154
|
+
// 2026-10-14 用户拍板:里程碑只投 /watch 显式订阅者——自身回合回显与
|
|
155
|
+
// 会话订阅表的其他来源一律不再隐式携带(镜像只发📋摘要卡)。
|
|
156
|
+
for (const userId of milestoneTargets(activeWatches, sessionID)) enqueuePush(userId, line)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// ===== 摘要镜像域已随 O7d 收进 core/mirror.ts(订阅/在途标记/解锁/摘要投递)=====
|
|
160
|
+
|
|
161
|
+
// ===== 后台等待:前台超时后不放弃,任务完成时回传结果 =====
|
|
162
|
+
// SSE execution.succeeded 即时触发 + 5 分钟轮询兜底(防 SSE 断连漏事件),
|
|
163
|
+
// 上限 24 小时。用户在同一会话发起新回合时被取代(避免同一结果投递两次)。
|
|
164
|
+
const BG_WAIT_MAX_MS = 24 * 60 * 60 * 1000
|
|
165
|
+
const BG_POLL_MS = 5 * 60 * 1000
|
|
166
|
+
|
|
167
|
+
// H2(2026-10-30,昨晚卡死事故复盘):桌面审批/选择弹窗对 bot 无通道(V2 无
|
|
168
|
+
// question API,C 项已证伪)——共享会话会被"桌面弹窗挂起"传导性卡死,且静默。
|
|
169
|
+
// bgWait 期间检测"零进展停滞",主动提醒用户:去桌面端处理弹窗,或用 /stop 终止。
|
|
170
|
+
const STALL_FIRST_NUDGE_MS = 30 * 60 * 1000
|
|
171
|
+
const STALL_NUDGE_INTERVAL_MS = 30 * 60 * 1000
|
|
172
|
+
const STALL_NUDGE_MAX = 3
|
|
173
|
+
|
|
174
|
+
function startBackgroundWait(sessionId: string, ctx: MessageContext, baseline: number): void {
|
|
175
|
+
let cancelled = false
|
|
176
|
+
let finished = false
|
|
177
|
+
const startedAt = Date.now()
|
|
178
|
+
let lastProgressAt = startedAt
|
|
179
|
+
let nudges = 0
|
|
180
|
+
let lastMarker = ""
|
|
181
|
+
|
|
182
|
+
const cleanup = (): void => {
|
|
183
|
+
cancelled = true
|
|
184
|
+
unsub()
|
|
185
|
+
clearInterval(pollTimer)
|
|
186
|
+
clearTimeout(giveupTimer)
|
|
187
|
+
bgWaits.delete(sessionId)
|
|
188
|
+
// 后台等待结束(完成/被新回合取代)→ 延迟解锁摘要镜像
|
|
189
|
+
mirror.scheduleUnlock(sessionId)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async function finish(kind: "done" | "giveup"): Promise<void> {
|
|
193
|
+
if (cancelled || finished) return
|
|
194
|
+
finished = true
|
|
195
|
+
cleanup()
|
|
196
|
+
if (kind === "giveup") {
|
|
197
|
+
await safeDeliver(ctx, "⚠️ 后台任务 24 小时仍未完成,停止自动跟进;结果可随时用 /history 查询")
|
|
198
|
+
return
|
|
199
|
+
}
|
|
200
|
+
// SSE 完成事件后给最终消息落库留 2 秒余量,避免读到半截
|
|
201
|
+
await new Promise((r) => setTimeout(r, 2000))
|
|
202
|
+
try {
|
|
203
|
+
const listJson = await client.session.messages({ path: { id: sessionId } })
|
|
204
|
+
const text = collectReplyText(listJson?.data ?? [], baseline)
|
|
205
|
+
await safeDeliver(
|
|
206
|
+
ctx,
|
|
207
|
+
text
|
|
208
|
+
? `📤 后台任务完成,结果如下:\n\n${text}`
|
|
209
|
+
: "📤 后台任务完成(无文本产出),详情可用 /history 查看",
|
|
210
|
+
)
|
|
211
|
+
console.log(`[bridge] 会话 ${sessionId} 后台等待已交付`)
|
|
212
|
+
} catch (error) {
|
|
213
|
+
console.error("[bridge] 后台完成结果获取失败:", error)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async function tick(): Promise<void> {
|
|
218
|
+
if (cancelled || finished) return
|
|
219
|
+
try {
|
|
220
|
+
const listJson = await client.session.messages({ path: { id: sessionId } })
|
|
221
|
+
const msgs = listJson?.data ?? []
|
|
222
|
+
|
|
223
|
+
// H2 停滞检测:消息数或最新 created 变化=有进展;零进展超窗则提醒用户
|
|
224
|
+
let maxCreated = 0
|
|
225
|
+
for (const m of msgs) {
|
|
226
|
+
const created = m.time?.created ?? 0
|
|
227
|
+
if (created > maxCreated) maxCreated = created
|
|
228
|
+
}
|
|
229
|
+
const marker = `${msgs.length}:${maxCreated}`
|
|
230
|
+
const now = Date.now()
|
|
231
|
+
if (marker !== lastMarker) {
|
|
232
|
+
lastMarker = marker
|
|
233
|
+
lastProgressAt = now
|
|
234
|
+
} else if (
|
|
235
|
+
stallNudgeDecision(startedAt, lastProgressAt, nudges, now, STALL_FIRST_NUDGE_MS, STALL_NUDGE_INTERVAL_MS, STALL_NUDGE_MAX) === "nudge"
|
|
236
|
+
) {
|
|
237
|
+
nudges += 1
|
|
238
|
+
lastProgressAt = now
|
|
239
|
+
console.log(`[bridge] 后台任务停滞提醒 #${nudges} session=${sessionId}`)
|
|
240
|
+
void safeDeliver(
|
|
241
|
+
ctx,
|
|
242
|
+
"⚠️ 后台任务已停滞超过 30 分钟——很可能在桌面端等待确认(审批/选择弹窗无法同步到 QQ)。请到桌面端查看处理;或回复 /stop 终止该任务后重发。",
|
|
243
|
+
).catch(() => {})
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (msgs.some((x) => x.type === "idle" && (x.time?.created ?? 0) >= baseline)) {
|
|
247
|
+
await finish("done")
|
|
248
|
+
}
|
|
249
|
+
} catch (error) {
|
|
250
|
+
console.error("[bridge] 后台等待轮询失败:", error)
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const unsub = eventStream.subscribe(sessionId, (ev) => {
|
|
255
|
+
if (ev.type === "session.execution.succeeded") void finish("done")
|
|
256
|
+
})
|
|
257
|
+
const pollTimer = setInterval(() => void tick(), BG_POLL_MS)
|
|
258
|
+
const giveupTimer = setTimeout(() => void finish("giveup"), BG_WAIT_MAX_MS)
|
|
259
|
+
bgWaits.set(sessionId, { cancel: cleanup })
|
|
260
|
+
console.log(`[bridge] 会话 ${sessionId} 转入后台等待(最长 ${BG_WAIT_MAX_MS / 3_600_000} 小时)`)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const watchApi: WatchApi = {
|
|
264
|
+
start(userId, sessionID, ctx) {
|
|
265
|
+
const prev = activeWatches.get(userId)
|
|
266
|
+
if (prev && prev !== sessionID) unsubscribeSession(prev, userId)
|
|
267
|
+
activeWatches.set(userId, sessionID)
|
|
268
|
+
lastCtx.set(userId, ctx)
|
|
269
|
+
subscribeSession(sessionID, userId)
|
|
270
|
+
saveWatchedMap(activeWatches) // R2-P1-3:落盘,重启恢复
|
|
271
|
+
return [
|
|
272
|
+
`👁 已开启观察:${sessionID}`,
|
|
273
|
+
"该会话的工具调用/执行完成会实时推送摘要,/watch off 停止。",
|
|
274
|
+
"(≥2 秒节流合并;QQ 主动消息有日配额,被动窗口内不受限)",
|
|
275
|
+
].join("\n")
|
|
276
|
+
},
|
|
277
|
+
stop(userId) {
|
|
278
|
+
const sid = activeWatches.get(userId)
|
|
279
|
+
if (!sid) return "当前没有开启观察"
|
|
280
|
+
activeWatches.delete(userId)
|
|
281
|
+
unsubscribeSession(sid, userId)
|
|
282
|
+
saveWatchedMap(activeWatches) // R2-P1-3:落盘,重启不复活
|
|
283
|
+
return `已停止观察:${sid}`
|
|
284
|
+
},
|
|
285
|
+
status(userId) {
|
|
286
|
+
const sid = activeWatches.get(userId)
|
|
287
|
+
return sid ? `👁 观察中:${sid}` : "未开启观察"
|
|
288
|
+
},
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// O7c:投递域门面(统一投递/降级/配额/节流队列/被动回复全在 core/delivery.ts)
|
|
292
|
+
const { enqueuePush, safeDeliver, sendReply } = createDelivery(config, lastCtx)
|
|
293
|
+
|
|
294
|
+
// O7b:审批域门面(挂起槽/等待/裁决/跨重启恢复全在 core/permission.ts)
|
|
295
|
+
const perm = createPermissionGate({
|
|
296
|
+
client,
|
|
297
|
+
sendReply,
|
|
298
|
+
safeDeliver,
|
|
299
|
+
// A 项:审批卡片发送(失败自动降级文字菜单,permission 域内处理)
|
|
300
|
+
sendApprovalPrompt: (ctx, request, mainSessionId) =>
|
|
301
|
+
sendApprovalCard({ appId: config.qq.appId, clientSecret: config.qq.clientSecret }, ctx, request, mainSessionId),
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
// O7d:摘要镜像域门面(订阅/回合标记/延迟解锁/摘要投递全在 core/mirror.ts)
|
|
305
|
+
const mirror = createMirror({ sessions, eventStream, client, lastCtx, safeDeliver, bgWaits, busyUsers })
|
|
306
|
+
|
|
307
|
+
// L 项(2026-10-30):表单桥(选择弹窗 A/B/C/D 的 QQ 同步与作答回传)。
|
|
308
|
+
// 与审批共用"每用户单一交互槽"心智:表单挂起时优先消费用户回复。
|
|
309
|
+
// 按钮卡片(弹窗验证前置):选项直点直交,失败自动降级文字编号作答。
|
|
310
|
+
const formBridge = createFormBridge({
|
|
311
|
+
client,
|
|
312
|
+
sendReply,
|
|
313
|
+
safeDeliver,
|
|
314
|
+
sendFormCard: async (ctx, form, keyboard) => {
|
|
315
|
+
try {
|
|
316
|
+
const token = await getAccessToken(config.qq.appId, config.qq.clientSecret)
|
|
317
|
+
const text = renderFormPrompt(form)
|
|
318
|
+
if (ctx.type === "group" && ctx.groupId) {
|
|
319
|
+
await sendGroupMessage(token, ctx.groupId, text, ctx.msgId || undefined, undefined, true, keyboard)
|
|
320
|
+
} else {
|
|
321
|
+
await sendC2CMessage(token, ctx.userId, text, ctx.msgId || undefined, undefined, true, keyboard)
|
|
322
|
+
}
|
|
323
|
+
return true
|
|
324
|
+
} catch (error) {
|
|
325
|
+
console.error("[bridge] 表单卡片发送失败,降级文字:", error instanceof Error ? error.message : error)
|
|
326
|
+
return false
|
|
327
|
+
}
|
|
328
|
+
},
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
// L 项:常驻交互监听——轮询全局待答权限 + 表单,反查 QQ 用户后推送。
|
|
332
|
+
// 覆盖"桌面端回合发起的交互"(昨晚事故盲区):bot 空闲时也能把弹窗送到 QQ。
|
|
333
|
+
const INTERACTION_WATCH_MS = (() => {
|
|
334
|
+
const n = Number(process.env.QQ_INTERACTION_WATCH_SECONDS ?? "12")
|
|
335
|
+
return Number.isFinite(n) && n >= 0 ? Math.floor(n * 1000) : 12_000
|
|
336
|
+
})()
|
|
337
|
+
|
|
338
|
+
function usersOfSession(sessionID: string): string[] {
|
|
339
|
+
return sessions
|
|
340
|
+
.all()
|
|
341
|
+
.filter((e) => e.sessionId === sessionID)
|
|
342
|
+
.map((e) => e.userId)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function ctxForUser(userId: string): MessageContext {
|
|
346
|
+
return (
|
|
347
|
+
lastCtx.get(userId) ?? { type: "c2c", userId, msgId: "", content: "", timestamp: String(Date.now()) }
|
|
348
|
+
)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async function interactionTick(): Promise<void> {
|
|
352
|
+
try {
|
|
353
|
+
const [permRes] = await Promise.all([client.permission.list()])
|
|
354
|
+
for (const req of permRes?.data ?? []) {
|
|
355
|
+
for (const uid of usersOfSession(req.sessionID)) {
|
|
356
|
+
if (perm.hasPending(uid)) continue // 单一槽:在挂起(含同 id)即跳过
|
|
357
|
+
perm.pushExternal(uid, ctxForUser(uid), req, req.sessionID)
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
// 表单按会话列表(GET /api/form 是 location 维度,bot cwd 不在会话项目会漏)
|
|
361
|
+
for (const entry of sessions.all()) {
|
|
362
|
+
const forms = (await client.form.list({ sessionID: entry.sessionId }).catch(() => undefined))?.data ?? []
|
|
363
|
+
for (const form of forms) {
|
|
364
|
+
// 会话级列表返回 Form.Info(无 state);带 state 时只推 pending
|
|
365
|
+
const state = (form as { state?: { status?: string } }).state
|
|
366
|
+
if (state && state.status !== "pending") continue
|
|
367
|
+
formBridge.pushForm(entry.userId, ctxForUser(entry.userId), form)
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
} catch (error) {
|
|
371
|
+
console.error("[interaction-watch] 轮询失败:", error instanceof Error ? error.message : error)
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if (INTERACTION_WATCH_MS > 0) {
|
|
376
|
+
setInterval(() => void interactionTick(), INTERACTION_WATCH_MS)
|
|
377
|
+
console.log(`[interaction-watch] 常驻交互监听已启动(每 ${INTERACTION_WATCH_MS / 1000}s 轮询权限+表单)`)
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const mirrorApi: MirrorApi = mirror.api
|
|
381
|
+
|
|
382
|
+
const commandContext: CommandContext = {
|
|
383
|
+
config,
|
|
384
|
+
client,
|
|
385
|
+
sessions,
|
|
386
|
+
getAccessToken: () => getAccessToken(config.qq.appId, config.qq.clientSecret),
|
|
387
|
+
pendingSelections,
|
|
388
|
+
watch: watchApi,
|
|
389
|
+
mirror: mirrorApi,
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// R2 修复批次启动项(2026-10-14)
|
|
393
|
+
if (config.allowedUsers.length === 0) {
|
|
394
|
+
console.warn("[security] ALLOWED_USERS 为空 → 所有消息将被拒绝(fail-closed);请在 ~/.openqq/.env 配置白名单")
|
|
395
|
+
}
|
|
396
|
+
// R2-P1-3:恢复持久化的 /watch 订阅(里程碑已改 /watch 专属,重启不得静默丢)
|
|
397
|
+
{
|
|
398
|
+
const saved = loadWatchedMap()
|
|
399
|
+
const known = new Set(sessions.all().map((x) => x.sessionId))
|
|
400
|
+
let pruned = false
|
|
401
|
+
for (const [uid, sid] of Object.entries(saved)) {
|
|
402
|
+
if (!known.has(sid)) {
|
|
403
|
+
pruned = true
|
|
404
|
+
continue
|
|
405
|
+
}
|
|
406
|
+
activeWatches.set(uid, sid)
|
|
407
|
+
subscribeSession(sid, uid)
|
|
408
|
+
console.log(`[watch] 恢复订阅 ${uid.slice(0, 8)}… → ${sid}`)
|
|
409
|
+
}
|
|
410
|
+
if (pruned) saveWatchedMap(activeWatches)
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// O2(P2-4a):恢复重启前挂起的审批(逻辑随 O7b 收进 core/permission.ts)
|
|
414
|
+
perm.restore()
|
|
415
|
+
|
|
416
|
+
// 摘要镜像:启动时对全部映射会话挂常驻订阅
|
|
417
|
+
mirror.ensureSubs()
|
|
418
|
+
// 图片落盘:清理 7 天前的 QQ 图片(原生附件接入)
|
|
419
|
+
purgeOldQQImages()
|
|
420
|
+
|
|
421
|
+
const handleMessage = async (ctx: MessageContext): Promise<void> => {
|
|
422
|
+
try {
|
|
423
|
+
if (!isAllowedUser(ctx.userId, config.allowedUsers)) {
|
|
424
|
+
await sendReply(ctx, "你不在允许使用的名单里")
|
|
425
|
+
return
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const content = ctx.content.trim()
|
|
429
|
+
// 纯图消息(无文字但有附件)同样放行;真空消息才丢
|
|
430
|
+
if (!content && !ctx.attachments?.length) {
|
|
431
|
+
return
|
|
432
|
+
}
|
|
433
|
+
// 记录最近消息上下文:观察/过程推送的通道选择(被动优先)
|
|
434
|
+
lastCtx.set(ctx.userId, ctx)
|
|
435
|
+
|
|
436
|
+
if (!greeted.has(ctx.userId)) {
|
|
437
|
+
greeted.add(ctx.userId)
|
|
438
|
+
saveGreetedSet(greeted)
|
|
439
|
+
await sendReply(ctx, buildHelpText())
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
// 权限审批决策优先于命令/普通消息:原 prompt 在 promptAndWait 里
|
|
443
|
+
// 挂起等待该 promise(busy 检查也在其后,不会被"处理中"拒绝)。
|
|
444
|
+
const decision = parsePermissionDecision(content)
|
|
445
|
+
if (decision && perm.hasPending(ctx.userId)) {
|
|
446
|
+
// 债务D2:不在这里发"已允许"——等 client 的 reply POST 成功回调
|
|
447
|
+
//(onPermissionReplied)再确认;404(桌面已批)走 onPermissionConflict。
|
|
448
|
+
perm.resolve(ctx.userId, decision)
|
|
449
|
+
return
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// L 项:表单作答消费(选择弹窗 A/B/C/D)——有挂起表单时优先于命令/普通消息。
|
|
453
|
+
// 文字"允许"与表单答案的歧义:parsePermissionDecision 先行的同时要求存在
|
|
454
|
+
// 待审权限才会消费;表单槽独立判断,互不串线。
|
|
455
|
+
if (formBridge.tryResolveText(ctx.userId, content)) {
|
|
456
|
+
return
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
if (isCommand(content)) {
|
|
460
|
+
const reply = await handleCommand(ctx, commandContext)
|
|
461
|
+
await sendReply(ctx, reply)
|
|
462
|
+
return
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
const pendingReply = await maybeHandlePendingSelection(ctx, commandContext)
|
|
466
|
+
if (pendingReply !== null) {
|
|
467
|
+
await sendReply(ctx, pendingReply)
|
|
468
|
+
return
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// 审批待决时,普通消息不进新 prompt(会话正阻塞在审批上),重发审批菜单
|
|
472
|
+
const pendingPerm = perm.getPending(ctx.userId)
|
|
473
|
+
if (pendingPerm) {
|
|
474
|
+
await sendReply(ctx, formatPermissionPrompt(pendingPerm.request, pendingPerm.mainSessionId))
|
|
475
|
+
return
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
if (busyUsers.has(ctx.userId)) {
|
|
479
|
+
await sendReply(ctx, "上一条消息还在处理中,请稍候再试")
|
|
480
|
+
return
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
busyUsers.add(ctx.userId)
|
|
484
|
+
|
|
485
|
+
// B3(2026-10-29 同步升级):C2C typing 指示——长回合处理中给用户
|
|
486
|
+
// "对方正在输入"体感(官方 input_notify,仅 C2C;失败静默不阻塞主流程)
|
|
487
|
+
if (ctx.type === "c2c") {
|
|
488
|
+
void getAccessToken(config.qq.appId, config.qq.clientSecret)
|
|
489
|
+
.then((token) => sendC2CInputNotify(token, ctx.userId, ctx.msgId, 60))
|
|
490
|
+
.catch(() => {})
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
let sessionId = ""
|
|
494
|
+
let turnStart = 0
|
|
495
|
+
try {
|
|
496
|
+
const session = await sessions.getOrCreate(ctx.userId)
|
|
497
|
+
sessionId = session.sessionId
|
|
498
|
+
sessionLastActivity.set(sessionId, Date.now())
|
|
499
|
+
|
|
500
|
+
// 缺口 1(引用上下文,吸收自官方 quote-ref 中间件):本条消息的引用解析
|
|
501
|
+
// 先于本条登记(顺序不可反:本条入缓存后才能被后续消息引用)
|
|
502
|
+
if (ctx.referencedMsgId) {
|
|
503
|
+
const quoted = resolveQuote(scopeKeyOf(ctx), ctx.referencedMsgId)
|
|
504
|
+
if (quoted) ctx.quoteText = quoted
|
|
505
|
+
}
|
|
506
|
+
recordMessage(scopeKeyOf(ctx), ctx.msgId, ctx.content)
|
|
507
|
+
// 新回合取代旧的后台等待(steer 合并运行时只投递一次合并结果)
|
|
508
|
+
bgWaits.get(sessionId)?.cancel()
|
|
509
|
+
// 会话订阅通道(SSE 活性 + 同会话同用户 Set 去重不双发);里程碑投递
|
|
510
|
+
// 已改 /watch 专属(2026-10-14),此订阅只保通道不再隐式携带推送
|
|
511
|
+
subscribeSession(sessionId, ctx.userId)
|
|
512
|
+
// 摘要镜像:新映射会话补挂订阅;QQ 轮次在途标记(防完成后重复镜像)
|
|
513
|
+
mirror.beginTurn(sessionId)
|
|
514
|
+
turnStart = Date.now()
|
|
515
|
+
// 原生附件接入(2026-10-14):QQ 图片下载落盘 → prompt files;
|
|
516
|
+
// 下载失败/类型不支持转成可见文字备注,绝不静默丢
|
|
517
|
+
const prepared = await prepareQQAttachments(ctx.attachments, () =>
|
|
518
|
+
getAccessToken(config.qq.appId, config.qq.clientSecret),
|
|
519
|
+
)
|
|
520
|
+
const { text: promptBody, files } = augmentPromptWithAttachments(content, prepared)
|
|
521
|
+
// 缺口 1:引用上文注入——让 AI 明确知道用户引用了哪条消息
|
|
522
|
+
const promptText = ctx.quoteText ? `> 引用上文:${ctx.quoteText}\n\n${promptBody}` : promptBody
|
|
523
|
+
const replyText = await client.promptAndWait(sessionId, promptText, {
|
|
524
|
+
files: files.length ? files : undefined,
|
|
525
|
+
onPermission: (req) => perm.waitForDecision(ctx, req, sessionId),
|
|
526
|
+
onPermissionReplied: (d) => {
|
|
527
|
+
void safeDeliver(ctx, `已${DECISION_LABELS[d]},AI 将继续执行…`)
|
|
528
|
+
},
|
|
529
|
+
onPermissionConflict: () => {
|
|
530
|
+
void safeDeliver(ctx, "⚠️ 该审批已在桌面端处理,AI 继续执行…")
|
|
531
|
+
},
|
|
532
|
+
})
|
|
533
|
+
|
|
534
|
+
if (replyText.trim()) {
|
|
535
|
+
// 缺口 4:AI 主动发文件——QQ_SEND_FILE 标记行拦截发送文件,剩余文本照送
|
|
536
|
+
const extracted = extractSendFileMarkers(replyText)
|
|
537
|
+
for (const p of extracted.paths) {
|
|
538
|
+
try {
|
|
539
|
+
const done = await sendLocalFile(ctx, p, {
|
|
540
|
+
getToken: () => getAccessToken(config.qq.appId, config.qq.clientSecret),
|
|
541
|
+
})
|
|
542
|
+
await safeDeliver(ctx, `📎 ${done}`)
|
|
543
|
+
} catch (error) {
|
|
544
|
+
await safeDeliver(ctx, `⚠️ 发送文件失败(${p}):${error instanceof Error ? error.message : error}`)
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
// 超长回合可能已超被动窗口:deliver 被动失败自动降级主动,答案不丢
|
|
548
|
+
if (extracted.text) await safeDeliver(ctx, extracted.text)
|
|
549
|
+
}
|
|
550
|
+
} catch (error) {
|
|
551
|
+
if (sessionId && turnStart > 0 && error instanceof Error && error.message.includes("响应超时")) {
|
|
552
|
+
// 前台时限到 ≠ 放弃:任务继续跑,转入后台等待(SSE + 5 分钟轮询兜底,
|
|
553
|
+
// 最长 24 小时),完成时经 deliver 被动/主动回传 —— 12 小时长任务的答案也不丢。
|
|
554
|
+
await safeDeliver(
|
|
555
|
+
ctx,
|
|
556
|
+
"⏳ 回合超过前台时限,已转后台等待:任务继续执行,完成即回传结果(最长 24 小时)。期间可用 /history 查看进展。",
|
|
557
|
+
)
|
|
558
|
+
startBackgroundWait(sessionId, ctx, turnStart)
|
|
559
|
+
} else {
|
|
560
|
+
await safeDeliver(ctx, `处理失败:${toErrorMessage(error)}`)
|
|
561
|
+
}
|
|
562
|
+
} finally {
|
|
563
|
+
if (sessionId) unsubscribeSession(sessionId, ctx.userId) // /watch 若仍挂着则保留订阅
|
|
564
|
+
// 镜像解锁:转后台的任务由 bgWait 完成时解锁,其余延迟 5 秒(SSE 事件宽限)
|
|
565
|
+
if (sessionId && !bgWaits.has(sessionId)) mirror.scheduleUnlock(sessionId)
|
|
566
|
+
busyUsers.delete(ctx.userId)
|
|
567
|
+
}
|
|
568
|
+
} catch (error) {
|
|
569
|
+
console.error("[bridge] handleMessage failed:", error)
|
|
570
|
+
try {
|
|
571
|
+
await sendReply(ctx, `处理消息失败:${toErrorMessage(error)}`)
|
|
572
|
+
} catch (replyError) {
|
|
573
|
+
console.error("[bridge] failed to send error reply:", replyError)
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
return {
|
|
579
|
+
handleMessage,
|
|
580
|
+
eventStream,
|
|
581
|
+
handleInteraction: (event) => {
|
|
582
|
+
if (handleApprovalInteraction(event, perm)) return
|
|
583
|
+
void formBridge.handleButton(event)
|
|
584
|
+
},
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
async function maybeHandlePendingSelection(
|
|
589
|
+
ctx: MessageContext,
|
|
590
|
+
commandContext: CommandContext,
|
|
591
|
+
): Promise<string | null> {
|
|
592
|
+
const pending = commandContext.pendingSelections.get(ctx.userId)
|
|
593
|
+
if (!pending) {
|
|
594
|
+
return null
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
if (pending.expiresAt <= Date.now()) {
|
|
598
|
+
commandContext.pendingSelections.delete(ctx.userId)
|
|
599
|
+
return null
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
if (!/^\d+$/.test(ctx.content.trim())) {
|
|
603
|
+
commandContext.pendingSelections.delete(ctx.userId)
|
|
604
|
+
return null
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
return handlePendingSelection(ctx.userId, Number(ctx.content.trim()), commandContext)
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// R2-P1-1(2026-10-14):空名单 fail-closed——配置缺失时宁可全拒,不静默
|
|
611
|
+
// 全开(首轮 P0-2 只闭了配置层,本次闭结构层)。
|
|
612
|
+
export function isAllowedUser(userId: string, allowedUsers: string[]): boolean {
|
|
613
|
+
if (allowedUsers.length === 0) return false
|
|
614
|
+
return allowedUsers.includes(userId)
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
// R2-P1-3(2026-10-14):/watch 持久化——里程碑已改 /watch 专属,重启静默丢
|
|
618
|
+
// 订阅不可接受;与 greeted.json 同款风格(损坏即空、文件名参数化供单测)。
|
|
619
|
+
const WATCH_FILE = join(homedir(), ".openqq", "watch.json")
|
|
620
|
+
|
|
621
|
+
export function loadWatchedMap(file: string = WATCH_FILE): Record<string, string> {
|
|
622
|
+
try {
|
|
623
|
+
if (existsSync(file)) {
|
|
624
|
+
const raw = readFileSync(file, "utf-8")
|
|
625
|
+
const obj = JSON.parse(raw)
|
|
626
|
+
if (obj && typeof obj === "object" && !Array.isArray(obj)) {
|
|
627
|
+
const out: Record<string, string> = {}
|
|
628
|
+
for (const [k, v] of Object.entries(obj)) if (typeof v === "string" && v) out[k] = v
|
|
629
|
+
return out
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
} catch {
|
|
633
|
+
// 损坏即空
|
|
634
|
+
}
|
|
635
|
+
return {}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
export function saveWatchedMap(m: ReadonlyMap<string, string>, file: string = WATCH_FILE): void {
|
|
639
|
+
try {
|
|
640
|
+
writeFileSync(file, JSON.stringify(Object.fromEntries(m), null, 2))
|
|
641
|
+
} catch (error) {
|
|
642
|
+
console.warn("[watch] 持久化失败:", error instanceof Error ? error.message : error)
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
const GREETED_FILE = join(homedir(), ".openqq", "greeted.json")
|
|
647
|
+
|
|
648
|
+
function loadGreetedSet(): Set<string> {
|
|
649
|
+
try {
|
|
650
|
+
if (existsSync(GREETED_FILE)) {
|
|
651
|
+
const raw = readFileSync(GREETED_FILE, "utf-8")
|
|
652
|
+
const arr = JSON.parse(raw)
|
|
653
|
+
if (Array.isArray(arr)) {
|
|
654
|
+
return new Set(arr.filter((x): x is string => typeof x === "string"))
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
} catch {
|
|
658
|
+
// fall through to empty set
|
|
659
|
+
}
|
|
660
|
+
return new Set<string>()
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
function saveGreetedSet(set: Set<string>): void {
|
|
664
|
+
try {
|
|
665
|
+
mkdirSync(join(homedir(), ".openqq"), { recursive: true })
|
|
666
|
+
writeFileSync(GREETED_FILE, JSON.stringify([...set]))
|
|
667
|
+
} catch (error) {
|
|
668
|
+
console.error("[bridge] 保存 greeted 失败:", error)
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
// 里程碑映射(事件载荷实测 2026-10-13):
|
|
673
|
+
// tool.called.data.input{command,...} → 动作可见
|
|
674
|
+
// tool.success.data.metadata.exit → 非零退出码才提示(成功静默,避免刷屏)
|
|
675
|
+
// execution.succeeded/failed → 轮次收尾
|
|
676
|
+
// 纯函数(导出供测试;生产路径经 onSessionEvent → enqueuePush)。
|
|
677
|
+
// calls: 可选的 callID→命令摘要缓存——tool.success 的载荷可能不含 input,
|
|
678
|
+
// 失败行靠缓存补上命令上下文(仅"上条命令 exit N"无从对号入座,用户误以为大量报错)。
|
|
679
|
+
export function formatMilestone(ev: V2Event, calls?: Map<string, string>): string | null {
|
|
680
|
+
const d = (ev.data ?? {}) as Record<string, any>
|
|
681
|
+
switch (ev.type) {
|
|
682
|
+
case "session.tool.called": {
|
|
683
|
+
const line = truncate(redactSecrets(summarizeInput(d.input)), 140)
|
|
684
|
+
if (d.id && calls) {
|
|
685
|
+
calls.set(String(d.id), line)
|
|
686
|
+
if (calls.size > 100) {
|
|
687
|
+
const oldest = calls.keys().next().value
|
|
688
|
+
if (oldest !== undefined) calls.delete(oldest)
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
// 2026-10-14 用户反馈:纯文件类工具(read/edit/write/grep…)推送裸路径没有
|
|
692
|
+
// 意义——只保留动作类里程碑(input.command 的 shell 命令,或 input 即命令串);
|
|
693
|
+
// 文件类静默,过程内容由摘要镜像 /history 承载。⚠️失败行不受影响(走 calls 缓存)。
|
|
694
|
+
const cmd = typeof d.input === "string" ? d.input : d.input?.command
|
|
695
|
+
if (!(typeof cmd === "string" && cmd.trim())) return null
|
|
696
|
+
return `🔧 ${line}`
|
|
697
|
+
}
|
|
698
|
+
case "session.tool.success": {
|
|
699
|
+
const exit = d.metadata?.exit
|
|
700
|
+
if (typeof exit !== "number" || exit === 0) return null
|
|
701
|
+
const what =
|
|
702
|
+
(d.id ? calls?.get(String(d.id)) : undefined) ??
|
|
703
|
+
(d.input ? truncate(redactSecrets(summarizeInput(d.input)), 80) : "")
|
|
704
|
+
return what ? `⚠️ exit ${exit}:${what}` : `⚠️ exit ${exit}`
|
|
705
|
+
}
|
|
706
|
+
case "session.execution.succeeded":
|
|
707
|
+
return "✅ 本轮完成"
|
|
708
|
+
case "session.execution.failed":
|
|
709
|
+
return "❌ 执行失败"
|
|
710
|
+
default:
|
|
711
|
+
return null
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
// 里程碑投递目标(纯函数,供单测):只认 /watch 显式订阅(activeWatches)。
|
|
716
|
+
// 2026-10-14 拍板:推不推完全由用户显式控制;sessionSubs(自身回合/镜像等
|
|
717
|
+
// 来源)不再隐式携带里程碑,避免"没开 /watch 却在收"的噪声。
|
|
718
|
+
export function milestoneTargets(
|
|
719
|
+
activeWatches: ReadonlyMap<string, string>,
|
|
720
|
+
sessionID: string,
|
|
721
|
+
): string[] {
|
|
722
|
+
const out: string[] = []
|
|
723
|
+
for (const [userId, sid] of activeWatches) {
|
|
724
|
+
if (sid === sessionID) out.push(userId)
|
|
725
|
+
}
|
|
726
|
+
return out
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
function toErrorMessage(error: unknown): string {
|
|
730
|
+
return error instanceof Error ? error.message : String(error)
|
|
731
|
+
}
|