@ktkid/kmh-subagent-team 0.1.0 → 0.1.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.
@@ -116,6 +116,10 @@ export async function runAcpTurn(req, open = openStdioProcess) {
116
116
  return cancelledResult(activeSessionId, textParts, reasoningParts, tools);
117
117
  }
118
118
  if (initResp === undefined) {
119
+ const spawnErr = await getSpawnFailure(proc);
120
+ if (spawnErr !== undefined) {
121
+ return failResult("spawn", `无法启动进程 ${req.command}: ${spawnErr.message}`, activeSessionId, textParts, reasoningParts, tools);
122
+ }
119
123
  return failResult("initialize", `ACP 握手未响应。${formatStderr(stderrLines)}`, activeSessionId, textParts, reasoningParts, tools);
120
124
  }
121
125
  const initErr = rpcErrorMessage(initResp);
@@ -325,3 +329,15 @@ function formatStderr(lines) {
325
329
  return "";
326
330
  return `stderr: ${lines.slice(-5).join(" | ")}`;
327
331
  }
332
+ async function getSpawnFailure(proc) {
333
+ const immediate = proc.getSpawnError?.();
334
+ if (immediate !== undefined)
335
+ return immediate;
336
+ return await Promise.race([
337
+ proc.exited.then(() => undefined, (err) => (err instanceof Error ? err : new Error(String(err)))),
338
+ new Promise((resolve) => {
339
+ const timer = setTimeout(() => resolve(undefined), 50);
340
+ timer.unref?.();
341
+ }),
342
+ ]);
343
+ }
@@ -5,8 +5,15 @@ export interface StdioProcess {
5
5
  closeStdin(): void;
6
6
  kill(signal?: NodeJS.Signals): void;
7
7
  readonly exited: Promise<number | null>;
8
+ /** 可选返回异步 spawn 失败原因(如 ENOENT)。 */
9
+ getSpawnError?(): Error | undefined;
8
10
  }
9
11
  export type StdioOpener = (command: string, args: readonly string[], env: NodeJS.ProcessEnv, cwd: string) => StdioProcess;
12
+ /**
13
+ * 为 GUI 桌面端(如 macOS Electron 仅有 /usr/bin:/bin:/usr/sbin:/sbin)补齐常见 CLI 安装目录。
14
+ * 保留已有 PATH 顺序在最前,缺失目录按兜底顺序追加在后。
15
+ */
16
+ export declare function buildAugmentedPath(currentPath: string | undefined, homeDir?: string): string;
10
17
  /** 继承宿主环境并清除可能干扰本地订阅登录的密钥/配置覆盖,再叠加成员显式 env。 */
11
18
  export declare function buildControlledEnv(baseEnv: NodeJS.ProcessEnv, overrides?: Readonly<Record<string, string>>): NodeJS.ProcessEnv;
12
19
  /** 启动本机 stdio 子进程并返回逐行异步迭代器。 */
@@ -1,5 +1,7 @@
1
1
  /** 受控 stdio 子进程管理:清理敏感覆盖变量、按行读写 NDJSON、优雅退出与超时强杀。 */
2
2
  import { spawn } from "node:child_process";
3
+ import { homedir } from "node:os";
4
+ import path from "node:path";
3
5
  const SENSITIVE_ENV_KEY = /(API_KEY|_TOKEN|SECRET|PASSWORD|CREDENTIAL|AUTH_PROVIDER|OIDC_)/i;
4
6
  const DROPPED_OVERRIDE_KEYS = new Set([
5
7
  "GROK_CONFIG",
@@ -9,6 +11,40 @@ const DROPPED_OVERRIDE_KEYS = new Set([
9
11
  "GROK_OIDC_ISSUER",
10
12
  "GROK_OIDC_CLIENT_ID",
11
13
  ]);
14
+ /**
15
+ * 为 GUI 桌面端(如 macOS Electron 仅有 /usr/bin:/bin:/usr/sbin:/sbin)补齐常见 CLI 安装目录。
16
+ * 保留已有 PATH 顺序在最前,缺失目录按兜底顺序追加在后。
17
+ */
18
+ export function buildAugmentedPath(currentPath, homeDir = homedir()) {
19
+ const existing = (currentPath ?? "")
20
+ .split(path.delimiter)
21
+ .map((s) => s.trim())
22
+ .filter((s) => s.length > 0);
23
+ if (process.platform === "win32") {
24
+ return existing.join(path.delimiter);
25
+ }
26
+ const fallbacks = [
27
+ path.join(homeDir, ".grok", "bin"),
28
+ path.join(homeDir, ".local", "bin"),
29
+ path.join(homeDir, "bin"),
30
+ "/opt/homebrew/bin",
31
+ "/opt/homebrew/sbin",
32
+ "/usr/local/bin",
33
+ "/usr/bin",
34
+ "/bin",
35
+ "/usr/sbin",
36
+ "/sbin",
37
+ ];
38
+ const seen = new Set(existing);
39
+ const merged = [...existing];
40
+ for (const dir of fallbacks) {
41
+ if (!seen.has(dir)) {
42
+ seen.add(dir);
43
+ merged.push(dir);
44
+ }
45
+ }
46
+ return merged.join(path.delimiter);
47
+ }
12
48
  /** 继承宿主环境并清除可能干扰本地订阅登录的密钥/配置覆盖,再叠加成员显式 env。 */
13
49
  export function buildControlledEnv(baseEnv, overrides = {}) {
14
50
  const result = {};
@@ -19,6 +55,7 @@ export function buildControlledEnv(baseEnv, overrides = {}) {
19
55
  continue;
20
56
  result[key] = value;
21
57
  }
58
+ result["PATH"] = buildAugmentedPath(result["PATH"], result["HOME"] ?? baseEnv["HOME"] ?? homedir());
22
59
  for (const [key, value] of Object.entries(overrides)) {
23
60
  result[key] = value;
24
61
  }
@@ -31,8 +68,12 @@ export function openStdioProcess(command, args, env, cwd) {
31
68
  env,
32
69
  stdio: ["pipe", "pipe", "pipe"],
33
70
  });
71
+ let spawnError;
34
72
  const exited = new Promise((resolve, reject) => {
35
- child.once("error", reject);
73
+ child.once("error", (err) => {
74
+ spawnError = err;
75
+ reject(err);
76
+ });
36
77
  child.once("exit", (code) => resolve(code));
37
78
  });
38
79
  // 防止未提前 await exited 时出现 unhandled rejection。
@@ -56,6 +97,9 @@ export function openStdioProcess(command, args, env, cwd) {
56
97
  }
57
98
  },
58
99
  exited,
100
+ getSpawnError() {
101
+ return spawnError;
102
+ },
59
103
  };
60
104
  }
61
105
  /** 优雅关闭子进程:先关闭 stdin,等待宽限时间后依次发 SIGTERM / SIGKILL。 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ktkid/kmh-subagent-team",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "面向所有 DSH 会话的外部 ACP 团队插件(内置 agy-acp 与 grok-acp,支持异步派发、续聊与审批透传)",