@lih-x-x/kmr 1.0.44 → 1.0.46

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.46") {
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.46"} \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.46");
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) {
@@ -641,42 +664,51 @@ var SessionManager = class {
641
664
  const userDir = path2.join(this.sessionDir, userId);
642
665
  fs2.mkdirSync(userDir, { recursive: true });
643
666
  const agentCmd = this.getAgentCmd();
644
- let sessionExists = false;
645
667
  try {
646
668
  const { stdout } = await execFileAsync("acpx", [agentCmd, "sessions", "list"], {
647
669
  timeout: 1e4,
648
670
  maxBuffer: 1024 * 1024,
649
671
  env: getExecEnv()
650
672
  });
651
- sessionExists = stdout.split("\n").some(
673
+ const hasExisting = stdout.split("\n").some(
652
674
  (line) => !line.includes("[closed]") && line.split(" ")[1]?.trim() === sessionName
653
675
  );
676
+ if (hasExisting) {
677
+ console.log(`[session] \u53D1\u73B0\u6B8B\u7559 session: ${sessionName}\uFF0C\u5148\u5173\u95ED`);
678
+ try {
679
+ await execFileAsync("acpx", [agentCmd, "sessions", "close", sessionName], {
680
+ timeout: 1e4,
681
+ env: getExecEnv()
682
+ });
683
+ } catch {
684
+ }
685
+ }
654
686
  } catch {
655
687
  }
656
- if (sessionExists) {
657
- console.log(`[session] \u590D\u7528\u5DF2\u6709 acpx session: ${sessionName}`);
658
- } else {
659
- console.log(`[session] \u521B\u5EFA\u65B0 session: ${sessionName}`);
660
- try {
661
- await execFileAsync("acpx", [agentCmd, "sessions", "ensure", "--name", sessionName], {
662
- timeout: this.timeout,
663
- maxBuffer: 1024 * 1024,
664
- env: getExecEnv()
665
- });
666
- } catch (err) {
667
- console.error(`[session] \u521B\u5EFA session \u5931\u8D25:`, err.message);
668
- throw new Error(`\u521B\u5EFA\u4F1A\u8BDD\u5931\u8D25: ${err.message}`);
669
- }
670
- try {
671
- await execFileAsync(
672
- "acpx",
673
- ["--approve-all", agentCmd, "-s", sessionName, SESSION_SKILL],
674
- { timeout: this.timeout, maxBuffer: 1024 * 1024, env: getExecEnv() }
675
- );
676
- } catch (err) {
677
- console.error(`[session] \u89D2\u8272\u8BBE\u5B9A\u5931\u8D25:`, err.message);
678
- }
688
+ console.log(`[session] \u521B\u5EFA\u65B0 session: ${sessionName}`);
689
+ const pidsBefore = await this.getAgentPids();
690
+ try {
691
+ await execFileAsync("acpx", [agentCmd, "sessions", "new", "--name", sessionName], {
692
+ timeout: this.timeout,
693
+ maxBuffer: 1024 * 1024,
694
+ env: getExecEnv()
695
+ });
696
+ } catch (err) {
697
+ console.error(`[session] \u521B\u5EFA session \u5931\u8D25:`, err.message);
698
+ throw new Error(`\u521B\u5EFA\u4F1A\u8BDD\u5931\u8D25: ${err.message}`);
699
+ }
700
+ await this.trackNewPids(pidsBefore);
701
+ const pidsBefore2 = await this.getAgentPids();
702
+ try {
703
+ await execFileAsync(
704
+ "acpx",
705
+ ["--approve-all", agentCmd, "-s", sessionName, SESSION_SKILL],
706
+ { timeout: this.timeout, maxBuffer: 1024 * 1024, env: getExecEnv() }
707
+ );
708
+ } catch (err) {
709
+ console.error(`[session] \u89D2\u8272\u8BBE\u5B9A\u5931\u8D25:`, err.message);
679
710
  }
711
+ await this.trackNewPids(pidsBefore2);
680
712
  this.activeSessions.set(userId, {
681
713
  name: sessionName,
682
714
  createdAt: (/* @__PURE__ */ new Date()).toISOString()
@@ -685,14 +717,17 @@ var SessionManager = class {
685
717
  }
686
718
  async sendToSession(sessionName, text) {
687
719
  const agentCmd = this.getAgentCmd();
720
+ const pidsBefore = await this.getAgentPids();
688
721
  try {
689
722
  const { stdout } = await execFileAsync(
690
723
  "acpx",
691
724
  ["--approve-all", agentCmd, "-s", sessionName, text],
692
725
  { timeout: this.timeout, maxBuffer: 1024 * 1024, env: getExecEnv() }
693
726
  );
727
+ await this.trackNewPids(pidsBefore);
694
728
  return this.extractReply(stdout);
695
729
  } catch (err) {
730
+ await this.trackNewPids(pidsBefore);
696
731
  if (err.killed) {
697
732
  throw new Error(`AI \u56DE\u590D\u8D85\u65F6 (${this.timeout}ms)`);
698
733
  }
@@ -754,14 +789,24 @@ var SessionManager = class {
754
789
  this.activeSessions.delete(userId);
755
790
  }
756
791
  /**
757
- * 关闭所有活跃 session,服务退出时调用
792
+ * 关闭所有活跃 session 并杀掉自己启动的进程,服务退出时调用
758
793
  */
759
794
  async closeAll() {
760
795
  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`);
796
+ if (userIds.length > 0) {
797
+ console.log(`[session] \u6B63\u5728\u5173\u95ED ${userIds.length} \u4E2A\u6D3B\u8DC3 session...`);
798
+ await Promise.all(userIds.map((uid) => this.closeSession(uid)));
799
+ }
800
+ if (this.ownedPids.size > 0) {
801
+ console.log(`[session] \u6B63\u5728\u6E05\u7406 ${this.ownedPids.size} \u4E2A agent \u8FDB\u7A0B...`);
802
+ for (const pid of this.ownedPids) {
803
+ try {
804
+ process.kill(pid, "SIGTERM");
805
+ } catch {
806
+ }
807
+ }
808
+ this.ownedPids.clear();
809
+ }
765
810
  }
766
811
  };
767
812
 
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.46",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "bin": {