@wu529778790/open-im 1.9.3-beta.13 → 1.9.3-beta.15
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.
|
@@ -18,11 +18,15 @@ const activeSessions = new Map();
|
|
|
18
18
|
const activeStreams = new Set();
|
|
19
19
|
// 空闲会话清理:跟踪最后使用时间,定期清除超时会话
|
|
20
20
|
const sessionLastUsed = new Map();
|
|
21
|
+
// 跟踪正在执行任务的 session ID,防止空闲清理误杀运行中的长任务
|
|
22
|
+
const runningSessions = new Set();
|
|
21
23
|
const SESSION_IDLE_TTL_MS = 30 * 60 * 1000; // 30 分钟未使用则清理
|
|
22
24
|
const CLEANUP_INTERVAL_MS = 5 * 60 * 1000; // 每 5 分钟检查一次
|
|
23
25
|
const cleanupInterval = setInterval(() => {
|
|
24
26
|
const now = Date.now();
|
|
25
27
|
for (const [id, lastUsed] of sessionLastUsed) {
|
|
28
|
+
if (runningSessions.has(id))
|
|
29
|
+
continue; // 跳过正在运行任务的 session
|
|
26
30
|
if (now - lastUsed > SESSION_IDLE_TTL_MS) {
|
|
27
31
|
const session = activeSessions.get(id);
|
|
28
32
|
if (session) {
|
|
@@ -48,6 +52,8 @@ function lazyCleanupIdleSessions() {
|
|
|
48
52
|
return;
|
|
49
53
|
const now = Date.now();
|
|
50
54
|
for (const [id, lastUsed] of sessionLastUsed) {
|
|
55
|
+
if (runningSessions.has(id))
|
|
56
|
+
continue; // 跳过正在运行任务的 session
|
|
51
57
|
if (now - lastUsed > SESSION_IDLE_TTL_MS) {
|
|
52
58
|
const s = activeSessions.get(id);
|
|
53
59
|
if (s) {
|
|
@@ -220,6 +226,7 @@ export class ClaudeSDKAdapter {
|
|
|
220
226
|
? 'plan'
|
|
221
227
|
: 'default';
|
|
222
228
|
const runSession = async () => {
|
|
229
|
+
let trackedRunningId; // 用于 finally 中清理 runningSessions
|
|
223
230
|
try {
|
|
224
231
|
// 检查环境变量
|
|
225
232
|
const hasApiKey = !!process.env.ANTHROPIC_API_KEY;
|
|
@@ -234,6 +241,8 @@ export class ClaudeSDKAdapter {
|
|
|
234
241
|
if (returnedId.startsWith('pending-')) {
|
|
235
242
|
pendingTempId = returnedId;
|
|
236
243
|
}
|
|
244
|
+
runningSessions.add(returnedId);
|
|
245
|
+
trackedRunningId = returnedId;
|
|
237
246
|
// 发送用户消息
|
|
238
247
|
await session.send(prompt);
|
|
239
248
|
// 获取响应流
|
|
@@ -263,6 +272,11 @@ export class ClaudeSDKAdapter {
|
|
|
263
272
|
sessionLastUsed.set(newSessionId, Date.now());
|
|
264
273
|
if (idToClean)
|
|
265
274
|
sessionLastUsed.delete(idToClean);
|
|
275
|
+
// 更新 runningSessions:移除旧 ID,添加新 ID
|
|
276
|
+
if (idToClean)
|
|
277
|
+
runningSessions.delete(idToClean);
|
|
278
|
+
runningSessions.add(newSessionId);
|
|
279
|
+
trackedRunningId = newSessionId;
|
|
266
280
|
actualSessionId = newSessionId;
|
|
267
281
|
log.info(`[V2] Got actual sessionId: ${newSessionId}`);
|
|
268
282
|
callbacks.onSessionId?.(newSessionId);
|
|
@@ -412,6 +426,16 @@ export class ClaudeSDKAdapter {
|
|
|
412
426
|
}
|
|
413
427
|
callbacks.onError(msg);
|
|
414
428
|
}
|
|
429
|
+
finally {
|
|
430
|
+
// 无论成功、失败还是 abort,都从运行中集合移除
|
|
431
|
+
if (trackedRunningId) {
|
|
432
|
+
runningSessions.delete(trackedRunningId);
|
|
433
|
+
}
|
|
434
|
+
// 也清理 actualSessionId(可能在 init 后更新了)
|
|
435
|
+
if (actualSessionId && actualSessionId !== trackedRunningId) {
|
|
436
|
+
runningSessions.delete(actualSessionId);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
415
439
|
};
|
|
416
440
|
// 启动会话(不等待),catch 兜底防止 unhandledRejection 导致用户请求挂起
|
|
417
441
|
runSession().catch((err) => {
|