@lih-x-x/kmr 1.0.44 → 1.0.45

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/dist/cli.js CHANGED
@@ -6,9 +6,9 @@ async function checkUpdate() {
6
6
  try {
7
7
  const res = await fetch(`https://registry.npmjs.org/${"@lih-x-x/kmr"}/latest`, { signal: AbortSignal.timeout(3e3) });
8
8
  const data = await res.json();
9
- if (data.version && data.version !== "1.0.44") {
9
+ if (data.version && data.version !== "1.0.45") {
10
10
  console.log(`
11
- \u2B06\uFE0F \u65B0\u7248\u672C\u53EF\u7528: ${"1.0.44"} \u2192 ${data.version}`);
11
+ \u2B06\uFE0F \u65B0\u7248\u672C\u53EF\u7528: ${"1.0.45"} \u2192 ${data.version}`);
12
12
  console.log(` \u8FD0\u884C npm install -g ${"@lih-x-x/kmr"} \u66F4\u65B0
13
13
  `);
14
14
  }
@@ -56,7 +56,7 @@ KMR\uFF08Key Meetings Record\uFF09\u2014 \u4F1A\u8BAE\u6316\u6398\u673A
56
56
  kmr --help \u663E\u793A\u5E2E\u52A9
57
57
  `);
58
58
  } else if (command === "--version" || command === "-v") {
59
- console.log("1.0.44");
59
+ console.log("1.0.45");
60
60
  } else if (command === "list") {
61
61
  const { loadConfig } = await import("./config-L2SVVMAR.js");
62
62
  const { JsonStore } = await import("./jsonStore-AL73KEUG.js");
package/dist/index.js CHANGED
@@ -620,6 +620,7 @@ var SessionManager = class {
620
620
  }
621
621
  timeout;
622
622
  activeSessions = /* @__PURE__ */ new Map();
623
+ ownedPids = /* @__PURE__ */ new Set();
623
624
  sessionDir;
624
625
  async handleMessage(userId, text) {
625
626
  const sessionName = await this.ensureSession(userId);
@@ -632,6 +633,28 @@ var SessionManager = class {
632
633
  const provider = getAgentProvider();
633
634
  return provider === "codex" ? "codex" : provider;
634
635
  }
636
+ /**
637
+ * 获取当前所有 claude-agent-acp 进程的 PID
638
+ */
639
+ async getAgentPids() {
640
+ try {
641
+ const { stdout } = await execFileAsync("pgrep", ["-f", "claude-agent-acp"], { timeout: 5e3 });
642
+ return new Set(stdout.trim().split("\n").filter(Boolean).map(Number));
643
+ } catch {
644
+ return /* @__PURE__ */ new Set();
645
+ }
646
+ }
647
+ /**
648
+ * 记录操作前后新增的 PID
649
+ */
650
+ async trackNewPids(before) {
651
+ const after = await this.getAgentPids();
652
+ for (const pid of after) {
653
+ if (!before.has(pid)) {
654
+ this.ownedPids.add(pid);
655
+ }
656
+ }
657
+ }
635
658
  async ensureSession(userId) {
636
659
  const existing = this.activeSessions.get(userId);
637
660
  if (existing) {
@@ -657,6 +680,7 @@ var SessionManager = class {
657
680
  console.log(`[session] \u590D\u7528\u5DF2\u6709 acpx session: ${sessionName}`);
658
681
  } else {
659
682
  console.log(`[session] \u521B\u5EFA\u65B0 session: ${sessionName}`);
683
+ const pidsBefore = await this.getAgentPids();
660
684
  try {
661
685
  await execFileAsync("acpx", [agentCmd, "sessions", "ensure", "--name", sessionName], {
662
686
  timeout: this.timeout,
@@ -667,6 +691,8 @@ var SessionManager = class {
667
691
  console.error(`[session] \u521B\u5EFA session \u5931\u8D25:`, err.message);
668
692
  throw new Error(`\u521B\u5EFA\u4F1A\u8BDD\u5931\u8D25: ${err.message}`);
669
693
  }
694
+ await this.trackNewPids(pidsBefore);
695
+ const pidsBefore2 = await this.getAgentPids();
670
696
  try {
671
697
  await execFileAsync(
672
698
  "acpx",
@@ -676,6 +702,7 @@ var SessionManager = class {
676
702
  } catch (err) {
677
703
  console.error(`[session] \u89D2\u8272\u8BBE\u5B9A\u5931\u8D25:`, err.message);
678
704
  }
705
+ await this.trackNewPids(pidsBefore2);
679
706
  }
680
707
  this.activeSessions.set(userId, {
681
708
  name: sessionName,
@@ -685,14 +712,17 @@ var SessionManager = class {
685
712
  }
686
713
  async sendToSession(sessionName, text) {
687
714
  const agentCmd = this.getAgentCmd();
715
+ const pidsBefore = await this.getAgentPids();
688
716
  try {
689
717
  const { stdout } = await execFileAsync(
690
718
  "acpx",
691
719
  ["--approve-all", agentCmd, "-s", sessionName, text],
692
720
  { timeout: this.timeout, maxBuffer: 1024 * 1024, env: getExecEnv() }
693
721
  );
722
+ await this.trackNewPids(pidsBefore);
694
723
  return this.extractReply(stdout);
695
724
  } catch (err) {
725
+ await this.trackNewPids(pidsBefore);
696
726
  if (err.killed) {
697
727
  throw new Error(`AI \u56DE\u590D\u8D85\u65F6 (${this.timeout}ms)`);
698
728
  }
@@ -754,14 +784,24 @@ var SessionManager = class {
754
784
  this.activeSessions.delete(userId);
755
785
  }
756
786
  /**
757
- * 关闭所有活跃 session,服务退出时调用
787
+ * 关闭所有活跃 session 并杀掉自己启动的进程,服务退出时调用
758
788
  */
759
789
  async closeAll() {
760
790
  const userIds = [...this.activeSessions.keys()];
761
- if (userIds.length === 0) return;
762
- console.log(`[session] \u6B63\u5728\u5173\u95ED ${userIds.length} \u4E2A\u6D3B\u8DC3 session...`);
763
- await Promise.all(userIds.map((uid) => this.closeSession(uid)));
764
- console.log(`[session] \u6240\u6709 session \u5DF2\u5173\u95ED`);
791
+ if (userIds.length > 0) {
792
+ console.log(`[session] \u6B63\u5728\u5173\u95ED ${userIds.length} \u4E2A\u6D3B\u8DC3 session...`);
793
+ await Promise.all(userIds.map((uid) => this.closeSession(uid)));
794
+ }
795
+ if (this.ownedPids.size > 0) {
796
+ console.log(`[session] \u6B63\u5728\u6E05\u7406 ${this.ownedPids.size} \u4E2A agent \u8FDB\u7A0B...`);
797
+ for (const pid of this.ownedPids) {
798
+ try {
799
+ process.kill(pid, "SIGTERM");
800
+ } catch {
801
+ }
802
+ }
803
+ this.ownedPids.clear();
804
+ }
765
805
  }
766
806
  };
767
807
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lih-x-x/kmr",
3
- "version": "1.0.44",
3
+ "version": "1.0.45",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "bin": {