@wu529778790/open-im 1.10.9-beta.6 → 1.10.9-beta.7

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.
@@ -58,15 +58,40 @@ function workDirToProjectPath(workDir) {
58
58
  return workDir.replace(/\//g, '-');
59
59
  }
60
60
  /**
61
- * 检查 CLI 进程是否正在使用某个 session(通过 ps 检测)
61
+ * 检查 CLI 进程是否**正在活跃使用**某个 session
62
+ *
63
+ * 仅当同时满足以下两个条件时返回 true:
64
+ * 1. 存在包含该 sessionId 的 claude 进程(通过 ps 检测)
65
+ * 2. session 文件在最近 30 秒内被修改过(说明 CLI 正在处理消息)
66
+ *
67
+ * 如果 CLI 只是挂在终端等待用户输入,进程仍在但文件长时间未变,
68
+ * 此时安全允许 open-im 接管 session(无缝切换)。
69
+ *
62
70
  * 注意:仅支持 macOS/Linux,Windows 上会静默返回 false
63
71
  */
64
- function isCliSessionActive(sessionId) {
72
+ function isCliSessionActive(sessionId, sessionFilePath) {
65
73
  try {
66
74
  // macOS/Linux: 用 ps 搜索包含该 sessionId 的 claude 进程
67
75
  // -F 固定字符串匹配,避免正则意外;-- 防止 sessionId 被误认为 flag
68
76
  const result = execSync(`ps -axo pid,command 2>/dev/null | grep -v grep | grep "claude" | grep -F -- "${sessionId}" || true`, { encoding: 'utf-8', timeout: 3000 });
69
- return result.trim().length > 0;
77
+ if (result.trim().length === 0)
78
+ return false;
79
+ // 进程存在,但可能只是 idle 在终端等输入。检查文件 mtime:
80
+ // 如果 session 文件超过 30 秒未修改,说明 CLI 没在活跃处理。
81
+ if (sessionFilePath) {
82
+ try {
83
+ const stat = statSync(sessionFilePath);
84
+ const ageMs = Date.now() - stat.mtimeMs;
85
+ if (ageMs > 30_000) {
86
+ log.info(`CLI process found for ${sessionId} but session file idle for ${Math.round(ageMs / 1000)}s, treating as inactive`);
87
+ return false;
88
+ }
89
+ }
90
+ catch {
91
+ // stat 失败时保守地认为活跃
92
+ }
93
+ }
94
+ return true;
70
95
  }
71
96
  catch {
72
97
  return false;
@@ -357,7 +382,7 @@ async function getOrCreateSession(sessionId, workDir, model, permissionMode, onS
357
382
  const latest = findLatestClaudeSession(workDir);
358
383
  if (latest) {
359
384
  // 安全检查:如果 CLI 正在使用该 session,不能接管
360
- if (isCliSessionActive(latest.sessionId)) {
385
+ if (isCliSessionActive(latest.sessionId, latest.filePath)) {
361
386
  log.info(`CLI is actively using session ${latest.sessionId}, skipping auto-resume`);
362
387
  }
363
388
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wu529778790/open-im",
3
- "version": "1.10.9-beta.6",
3
+ "version": "1.10.9-beta.7",
4
4
  "description": "Multi-platform IM bridge for AI CLI tools (Claude, Codex, CodeBuddy)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",