@lynn123411/dsh-kimi-cu 0.3.7 → 0.3.9

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/lib/index.js CHANGED
@@ -113,12 +113,15 @@ function createMcpConnection(warn) {
113
113
  };
114
114
  const reconcile = async () => {
115
115
  const status = lastDetection;
116
- const command = status?.supported && status.installed && status.doctor.ok && !status.notes.includes("mcp-command-missing") ? status.mcp : null;
116
+ const command = status?.supported && status.installed ? status.mcp : null;
117
+ const outcome = status?.doctor.outcome;
117
118
  if (current !== void 0) {
118
- if (command !== null && current.manager === manager && current.registry === registry && current.command.command === command.command && JSON.stringify(current.command.args) === JSON.stringify(command.args)) return;
119
+ const sameProviders = current.manager === manager && current.registry === registry;
120
+ const sameCommand = command !== null && current.command.command === command.command && current.command.args.length === command.args.length && current.command.args.every((arg, index) => arg === command.args[index]);
121
+ if (sameProviders && (status?.notes.includes("detection-failed") || sameCommand && (outcome === "passed" || outcome === "unknown"))) return;
119
122
  await disconnect(current);
120
123
  }
121
- if (command === null || manager === void 0) {
124
+ if (outcome !== "passed" || command === null || manager === void 0) {
122
125
  failure = null;
123
126
  return;
124
127
  }
@@ -173,7 +176,10 @@ function createMcpConnection(warn) {
173
176
  lastDetection = status;
174
177
  await reconcile();
175
178
  const registration = current;
176
- if (registration !== void 0 && registration === previous) await registration.manager.restart(MCP_SERVER_NAME);
179
+ if (registration !== void 0 && registration === previous) {
180
+ if (status.doctor.outcome !== "passed") return void 0;
181
+ await registration.manager.restart(MCP_SERVER_NAME);
182
+ }
177
183
  return registration;
178
184
  }).then(async (registration) => {
179
185
  if (registration === void 0) return false;
@@ -214,52 +220,51 @@ function installPlan(platform) {
214
220
  }
215
221
  return null;
216
222
  }
217
- function windowsPrimary(env) {
218
- return (env.LOCALAPPDATA || "") + "\\" + WINDOWS_DIR_NAME + "\\" + WINDOWS_EXE_NAME;
219
- }
220
- function windowsMcpCommand(env) {
221
- return env.KIMI_CU_WINDOWS_EXE || "" || windowsPrimary(env);
222
- }
223
223
  function resolveInstall(input) {
224
224
  const platform = toPlatform(input.platform);
225
225
  if (platform === "darwin") {
226
226
  const installed = input.exists(MACOS_BINARY);
227
+ const launcher = installed && input.exists(MACOS_MCP_LAUNCHER);
227
228
  return {
228
229
  platform,
229
230
  supported: true,
230
231
  installed,
231
232
  path: installed ? MACOS_BINARY : null,
232
- mcp: { command: MACOS_MCP_LAUNCHER, args: ["mcp", "-s", "user", "--surface", "codeact"] },
233
+ mcp: launcher ? { command: MACOS_MCP_LAUNCHER, args: ["mcp", "-s", "user", "--surface", "codeact"] } : null,
233
234
  install: installPlan("darwin"),
234
- notes: installed && !input.exists(MACOS_MCP_LAUNCHER) ? ["mcp-command-missing"] : []
235
+ notes: installed && !launcher ? ["mcp-command-missing"] : []
235
236
  };
236
237
  }
237
238
  if (platform === "win32") {
238
- const base = input.env.LOCALAPPDATA || "";
239
- const primary = windowsPrimary(input.env);
240
- const legacy = base + "\\" + WINDOWS_LEGACY_DIR_NAME + "\\" + WINDOWS_EXE_NAME;
241
- const override = input.env.KIMI_CU_WINDOWS_EXE || "";
242
- const command = windowsMcpCommand(input.env);
239
+ const base = input.env.LOCALAPPDATA || null;
240
+ const primary = base === null ? null : base + "\\" + WINDOWS_DIR_NAME + "\\" + WINDOWS_EXE_NAME;
241
+ const legacy = base === null ? null : base + "\\" + WINDOWS_LEGACY_DIR_NAME + "\\" + WINDOWS_EXE_NAME;
242
+ const override = input.env.KIMI_CU_WINDOWS_EXE || null;
243
+ const notes = [];
243
244
  let path = null;
244
- if (override !== "") {
245
- path = input.exists(override) ? override : null;
246
- } else if (primary !== "" && input.exists(primary)) {
247
- path = primary;
248
- } else if (legacy !== "" && input.exists(legacy)) {
249
- path = legacy;
245
+ let command = null;
246
+ if (override !== null && input.exists(override)) {
247
+ path = command = override;
248
+ } else {
249
+ if (override !== null) notes.push("override-missing");
250
+ if (base === null) notes.push("localappdata-missing");
251
+ if (primary !== null && input.exists(primary)) {
252
+ path = primary;
253
+ if (override === null) command = primary;
254
+ } else if (legacy !== null && input.exists(legacy)) {
255
+ path = legacy;
256
+ notes.push("legacy-install");
257
+ if (override === null) notes.push("mcp-command-missing");
258
+ }
250
259
  }
251
- const notes = [];
252
- if (path !== null && path === legacy) notes.push("legacy-install");
253
- if (override !== "" && path === null) notes.push("override-missing");
254
- if (path !== null && command !== "" && !input.exists(command)) notes.push("mcp-command-missing");
255
260
  const install = installPlan("win32");
256
- if (install !== null && notes.includes("legacy-install")) install.steps = [...install.steps, "upgrade"];
261
+ if (install !== null && notes.includes("legacy-install")) install.steps = ["upgrade", "recheck"];
257
262
  return {
258
263
  platform,
259
264
  supported: true,
260
265
  installed: path !== null,
261
266
  path,
262
- mcp: command === "" ? null : { command, args: ["mcp"] },
267
+ mcp: command === null ? null : { command, args: ["mcp"] },
263
268
  install,
264
269
  notes
265
270
  };
@@ -276,7 +281,7 @@ function resolveInstall(input) {
276
281
  }
277
282
 
278
283
  // src/server/status.ts
279
- import { existsSync, readFileSync } from "node:fs";
284
+ import { readFileSync, statSync } from "node:fs";
280
285
  import { dirname, join } from "node:path";
281
286
 
282
287
  // src/server/doctor.ts
@@ -290,49 +295,70 @@ var MACOS_CHECK_IDS = {
290
295
  "bg-input (window-location spi)": "bg-input",
291
296
  "signed keyboard": "signed-keyboard"
292
297
  };
298
+ var MACOS_PERMISSIONS = [
299
+ { id: "accessibility", label: "Accessibility (AX)" },
300
+ { id: "screen-recording", label: "Screen Recording" }
301
+ ];
302
+ var MACOS_CONTROL_CHANNEL = { id: "runtime-control", label: "Runtime control channel" };
303
+ function parseMacosCheck(line, index) {
304
+ const mark = line.slice(0, 1);
305
+ if (mark !== "\u2705" && mark !== "\u274C") return null;
306
+ const label = line.slice(1).trim();
307
+ if (label === "") return null;
308
+ const lower = label.toLowerCase();
309
+ const id = /^runtime control channel(?:$|:)/.test(lower) ? MACOS_CONTROL_CHANNEL.id : MACOS_CHECK_IDS[lower] ?? "check-" + String(index + 1);
310
+ return { id, label, ok: mark === "\u2705" };
311
+ }
312
+ function doctorOutcome(checks) {
313
+ if (checks.some((check) => check.ok === false)) return "failed";
314
+ return checks.length > 0 && checks.every((check) => check.ok === true) ? "passed" : "unknown";
315
+ }
293
316
  function parseDoctor(platform, raw) {
294
317
  const lines = raw.split("\n").map((line) => line.trim());
295
318
  if (platform === "darwin") {
296
319
  const checks = [];
297
- const hasServiceSection = lines.some((line) => /^Service \(authoritative/i.test(line));
298
- let inServiceSection = !hasServiceSection;
320
+ const diagnosticChecks = [];
321
+ const isServiceHeading = (line) => /^Service \(authoritative/i.test(line);
322
+ const isProcessHeading = (line) => /^This process \(diagnostic/i.test(line);
323
+ const scoped = lines.some((line) => isServiceHeading(line) || isProcessHeading(line));
324
+ let section = scoped ? void 0 : checks;
299
325
  for (const line of lines) {
300
- if (/^Service \(authoritative/i.test(line)) {
301
- inServiceSection = true;
326
+ if (isServiceHeading(line)) {
327
+ section = checks;
302
328
  continue;
303
329
  }
304
- if (/^This process \(diagnostic/i.test(line)) {
305
- inServiceSection = false;
330
+ if (isProcessHeading(line)) {
331
+ section = diagnosticChecks;
306
332
  continue;
307
333
  }
308
- if (!inServiceSection) continue;
309
- const mark = line.slice(0, 1);
310
- if (mark !== "\u2705" && mark !== "\u274C") continue;
311
- const label = line.slice(1).trim();
312
- if (label === "") continue;
313
- const id = MACOS_CHECK_IDS[label.toLowerCase()] ?? "check-" + String(checks.length + 1);
314
- checks.push({ id, label, ok: mark === "\u2705" });
334
+ if (section === void 0) continue;
335
+ const check = parseMacosCheck(line, section.length);
336
+ if (check !== null) section.push(check);
337
+ }
338
+ const required = scoped ? [...MACOS_PERMISSIONS, MACOS_CONTROL_CHANNEL] : MACOS_PERMISSIONS;
339
+ for (const check of required) {
340
+ if (!checks.some(({ id }) => id === check.id)) checks.push({ ...check, ok: null });
315
341
  }
316
- return { ok: checks.length > 0 && checks.every((check) => check.ok), checks, version: null };
342
+ const outcome = doctorOutcome(checks);
343
+ return { outcome, ok: outcome === "passed", checks, diagnosticChecks, version: null };
317
344
  }
318
345
  if (platform === "win32") {
319
346
  let version = null;
320
- let mcp = null;
347
+ const mcpValues = /* @__PURE__ */ new Set();
321
348
  for (const line of lines) {
322
349
  if (line.startsWith("version=")) version = line.slice("version=".length).trim() || null;
323
- else if (line.startsWith("mcp=")) mcp = line.slice("mcp=".length).trim();
350
+ else if (line.startsWith("mcp=")) mcpValues.add(line.slice("mcp=".length).trim());
324
351
  }
325
- return {
326
- ok: mcp === "true",
327
- checks: [{ id: "mcp", label: "MCP", ok: mcp === "true" }],
328
- version
329
- };
352
+ const mcp = mcpValues.size === 1 ? mcpValues.values().next().value : void 0;
353
+ const checks = [{ id: "mcp", label: "MCP", ok: mcp === "true" ? true : mcp === "false" ? false : null }];
354
+ const outcome = doctorOutcome(checks);
355
+ return { outcome, ok: outcome === "passed", checks, diagnosticChecks: [], version };
330
356
  }
331
- return { ok: false, checks: [], version: null };
357
+ return { outcome: "not-run", ok: false, checks: [], diagnosticChecks: [], version: null };
332
358
  }
333
359
  async function runDoctor(platform, binaryPath) {
334
360
  if (platform === "other" || binaryPath === "") {
335
- return { ran: false, ok: false, checks: [], version: null, raw: "", error: "unsupported platform" };
361
+ return { ...parseDoctor("other", ""), ran: false, raw: "" };
336
362
  }
337
363
  try {
338
364
  const { stdout, stderr } = await execFileAsync(binaryPath, ["doctor"], {
@@ -340,14 +366,19 @@ async function runDoctor(platform, binaryPath) {
340
366
  windowsHide: true,
341
367
  encoding: "utf8"
342
368
  });
343
- const raw = String(stdout ?? "") + String(stderr ?? "");
369
+ const raw = [stdout, stderr].filter(Boolean).join("\n");
344
370
  return { ran: true, ...parseDoctor(platform, raw), raw };
345
371
  } catch (error) {
346
372
  const record = error;
347
- const raw = String(record.stdout ?? "") + String(record.stderr ?? "");
373
+ const raw = [record.stdout, record.stderr].filter(Boolean).join("\n");
374
+ const parsed = parseDoctor(platform, raw);
375
+ const exited = typeof record.code === "number" && !record.killed && record.signal == null;
376
+ const outcome = exited && parsed.outcome === "failed" ? "failed" : "unknown";
348
377
  return {
349
- ran: true,
350
- ...parseDoctor(platform, raw),
378
+ ...parsed,
379
+ ran: exited || record.killed === true || record.signal != null || raw !== "",
380
+ outcome,
381
+ ok: false,
351
382
  raw,
352
383
  error: error instanceof Error ? error.message : String(error)
353
384
  };
@@ -355,6 +386,17 @@ async function runDoctor(platform, binaryPath) {
355
386
  }
356
387
 
357
388
  // src/server/status.ts
389
+ function doctorUnavailable(error) {
390
+ return {
391
+ ran: false,
392
+ outcome: error === void 0 ? "not-run" : "unknown",
393
+ ok: false,
394
+ checks: [],
395
+ diagnosticChecks: [],
396
+ raw: "",
397
+ ...error === void 0 ? {} : { error: error instanceof Error ? error.message : String(error) }
398
+ };
399
+ }
358
400
  function parseMacosVersion(plist) {
359
401
  const key = plist.indexOf("CFBundleShortVersionString");
360
402
  if (key === -1) return null;
@@ -385,13 +427,7 @@ function createStatusDetector(deps) {
385
427
  installed: false,
386
428
  path: null,
387
429
  version: null,
388
- doctor: {
389
- ran: false,
390
- ok: false,
391
- checks: [],
392
- raw: "",
393
- error: error instanceof Error ? error.message : String(error)
394
- },
430
+ doctor: doctorUnavailable(error),
395
431
  mcp: null,
396
432
  install: installPlan(platform),
397
433
  notes: ["detection-failed"],
@@ -409,26 +445,14 @@ function createStatusDetector(deps) {
409
445
  version = null;
410
446
  }
411
447
  }
412
- let doctor = { ran: false, ok: false, checks: [], raw: "" };
448
+ let doctor = doctorUnavailable();
413
449
  if (resolved.supported && resolved.path !== null) {
414
450
  try {
415
- const run = await deps.runDoctor(resolved.platform, resolved.path);
416
- doctor = {
417
- ran: run.ran,
418
- ok: run.ok,
419
- checks: run.checks,
420
- raw: run.raw,
421
- ...run.error === void 0 ? {} : { error: run.error }
422
- };
423
- version = version ?? run.version;
451
+ const { version: doctorVersion, ...report } = await deps.runDoctor(resolved.platform, resolved.path);
452
+ doctor = report;
453
+ version = version ?? doctorVersion;
424
454
  } catch (error) {
425
- doctor = {
426
- ran: true,
427
- ok: false,
428
- checks: [],
429
- raw: "",
430
- error: error instanceof Error ? error.message : String(error)
431
- };
455
+ doctor = doctorUnavailable(error);
432
456
  }
433
457
  }
434
458
  return {
@@ -448,9 +472,9 @@ function createStatusDetector(deps) {
448
472
  }
449
473
  };
450
474
  return async (refresh = false) => {
451
- const at = now();
452
- if (!refresh && cached !== void 0 && at - cached.at < ttl) return cached.value;
475
+ if (refresh === "after-upgrade" && inflight !== void 0) await inflight;
453
476
  if (inflight !== void 0) return inflight;
477
+ if (!refresh && cached !== void 0 && now() - cached.at < ttl) return cached.value;
454
478
  inflight = build().then((value) => {
455
479
  cached = { at: now(), value };
456
480
  return value;
@@ -464,7 +488,7 @@ function defaultStatusDetector() {
464
488
  return createStatusDetector({
465
489
  platform: process.platform,
466
490
  env: process.env,
467
- exists: existsSync,
491
+ exists: (path) => statSync(path, { throwIfNoEntry: false })?.isFile() ?? false,
468
492
  readVersion: (platform, path) => platform === "darwin" ? readMacosVersion(path) : null,
469
493
  runDoctor
470
494
  });
@@ -539,9 +563,8 @@ var FAILURE_RULES = [
539
563
  function classifyFailure(raw, timedOut) {
540
564
  if (timedOut) return "timeout";
541
565
  const errors = raw.split("\n").map((line) => line.trim()).filter((line) => line !== "" && !line.startsWith("==>") && !line.startsWith("\u2713")).join("\n");
542
- const text = errors === "" ? raw : errors;
543
566
  for (const [pattern, kind] of FAILURE_RULES) {
544
- if (pattern.test(text)) return kind;
567
+ if (pattern.test(errors)) return kind;
545
568
  }
546
569
  return "unknown";
547
570
  }
@@ -633,7 +656,7 @@ function createUpdater(deps) {
633
656
  const exited = await outcome.whenExited;
634
657
  outcome = { ...outcome, code: exited?.code ?? null, raw: exited?.raw ?? outcome.raw, timedOut: false };
635
658
  }
636
- const after = await readCurrent(true);
659
+ const after = await readCurrent("after-upgrade");
637
660
  if (outcome.code !== 0) {
638
661
  if (after.path !== null && after.version !== null && after.version !== current.version && deps.reconnect !== null) {
639
662
  emit({ type: "stage", stage: "restarting" });
@@ -805,7 +828,6 @@ function apply(ctx, options) {
805
828
  fetchManifest: fetchManifestText,
806
829
  runUpgrade: (input) => runUpgradeProcess({
807
830
  binaryPath: input.binaryPath,
808
- // Windows 的 upgrade 必须带目标版本;macOS 交给 KimiCU 自己判断。
809
831
  args: input.version === null ? ["upgrade"] : ["upgrade", input.version],
810
832
  onLine: input.onLine
811
833
  }),
@@ -817,7 +839,7 @@ function apply(ctx, options) {
817
839
  parameters: {
818
840
  refresh: {
819
841
  type: "boolean",
820
- description: "Re-run detection instead of using the cached result."
842
+ description: "Re-run detection instead of using the cached result and reconcile the MCP connection."
821
843
  }
822
844
  },
823
845
  output: {
@@ -863,12 +885,17 @@ function apply(ctx, options) {
863
885
  });
864
886
  void detectBase().then(async (status) => {
865
887
  await mcp.sync(status);
866
- if (!status.supported || status.installed) return;
867
- ctx.logger.warn(
868
- "KimiCU is not installed. Install it with: " + (status.install?.command ?? "")
869
- );
870
- }).catch(() => {
871
- });
888
+ if (!status.supported) return;
889
+ if (status.notes.includes("detection-failed")) {
890
+ ctx.logger.warn("KimiCU detection failed: " + (status.doctor.error ?? "unknown error"));
891
+ } else if (status.notes.includes("override-missing")) {
892
+ ctx.logger.warn("KIMI_CU_WINDOWS_EXE does not point to an installed KimiCU executable.");
893
+ } else if (status.notes.includes("localappdata-missing")) {
894
+ ctx.logger.warn("KimiCU cannot check the default Windows install path because LOCALAPPDATA is missing.");
895
+ } else if (!status.installed) {
896
+ ctx.logger.warn("KimiCU is not installed. Install it with: " + (status.install?.command ?? ""));
897
+ }
898
+ }).catch(logFailure);
872
899
  }
873
900
  export {
874
901
  apply,