@lynn123411/dsh-kimi-cu 0.3.1 → 0.3.3

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
@@ -2,8 +2,12 @@
2
2
  import { defineTool } from "@deepseek-ai/dsh-tools";
3
3
 
4
4
  // src/shared/status.ts
5
+ function isMcpConnecting(state) {
6
+ return state === "connecting" || state === "reconnecting";
7
+ }
5
8
  var STATUS_ROUTE_PATH = "/api/dsh-kimi-cu/status";
6
9
  var MACOS_BINARY = "/Applications/KimiCU.app/Contents/MacOS/kimi-cu";
10
+ var MACOS_MCP_LAUNCHER = "/Applications/KimiCU.app/Contents/Resources/kimi-cu-mcp";
7
11
  var WINDOWS_EXE_NAME = "kimi-cu.exe";
8
12
  var WINDOWS_DIR_NAME = "Kimi Computer Use";
9
13
  var WINDOWS_LEGACY_DIR_NAME = "KimiCU";
@@ -39,6 +43,152 @@ function createStatusRoute(detect) {
39
43
  };
40
44
  }
41
45
 
46
+ // src/server/mcp.ts
47
+ var MCP_SERVER_NAME = "kimi-cu";
48
+ var MCP_TOOL_TIMEOUT_MS = 3e5;
49
+ var MCP_CONNECT_TIMEOUT_MS = 35e3;
50
+ function kimiView(views) {
51
+ return views.find(({ name: name2 }) => name2 === MCP_SERVER_NAME);
52
+ }
53
+ function statusOf(view) {
54
+ const { state, reason, lastError, result } = view.status;
55
+ const detail = lastError ?? result?.cleanupError ?? reason;
56
+ return { state, ...detail === void 0 ? {} : { lastError: detail } };
57
+ }
58
+ function isTerminal(state) {
59
+ return state !== "connected" && !isMcpConnecting(state);
60
+ }
61
+ function createMcpConnection(warn) {
62
+ let current;
63
+ let failure = null;
64
+ let lastDetection;
65
+ let manager;
66
+ let registry;
67
+ let tail = Promise.resolve();
68
+ const enqueue = (work) => {
69
+ const next = tail.then(work);
70
+ tail = next.then(() => {
71
+ }, () => {
72
+ });
73
+ return next;
74
+ };
75
+ const read = () => {
76
+ const view = current === void 0 ? void 0 : kimiView(current.manager.list());
77
+ return view === void 0 ? failure : statusOf(view);
78
+ };
79
+ const disconnect = async (registration) => {
80
+ current = void 0;
81
+ registration.observe.abort();
82
+ registration.withdraw();
83
+ if (registration.release === void 0) return;
84
+ const signal = new AbortController();
85
+ for await (const views of registration.manager.watch(signal.signal)) {
86
+ const view = kimiView(views);
87
+ if (view === void 0 || view.configured) break;
88
+ if (view.status.reason === "cleanup") {
89
+ failure = statusOf(view);
90
+ return;
91
+ }
92
+ }
93
+ await registration.release();
94
+ };
95
+ const report = (state, error) => {
96
+ const lastError = error instanceof Error ? error.message : String(error);
97
+ if (failure?.state !== state || failure.lastError !== lastError) warn("KimiCU: " + lastError);
98
+ failure = { state, lastError };
99
+ };
100
+ const observe = async (registration) => {
101
+ for await (const views of registration.manager.watch(registration.observe.signal)) {
102
+ const view = kimiView(views);
103
+ if (view !== void 0 && !isTerminal(view.status.state)) continue;
104
+ await enqueue(async () => {
105
+ if (current !== registration) return;
106
+ const latest = kimiView(registration.manager.list());
107
+ if (latest !== void 0 && !isTerminal(latest.status.state)) return;
108
+ failure = latest === void 0 ? null : statusOf(latest);
109
+ await disconnect(registration);
110
+ });
111
+ return;
112
+ }
113
+ };
114
+ const reconcile = async () => {
115
+ const status = lastDetection;
116
+ const command = status?.supported && status.installed && status.doctor.ok && !status.notes.includes("mcp-command-missing") ? status.mcp : null;
117
+ 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
+ await disconnect(current);
120
+ }
121
+ if (command === null || manager === void 0) {
122
+ failure = null;
123
+ return;
124
+ }
125
+ if (registry?.providerName !== void 0) {
126
+ report("conflict", registry.providerName);
127
+ return;
128
+ }
129
+ const activeRegistry = registry;
130
+ const activeManager = manager;
131
+ let release;
132
+ try {
133
+ release = activeRegistry?.register(MCP_SERVER_NAME);
134
+ } catch (error) {
135
+ report("conflict", error);
136
+ return;
137
+ }
138
+ try {
139
+ const withdraw = activeManager.register(MCP_SERVER_NAME, {
140
+ transport: "stdio",
141
+ command: command.command,
142
+ args: command.args,
143
+ toolCallTimeoutMs: MCP_TOOL_TIMEOUT_MS
144
+ });
145
+ const registration = { manager: activeManager, registry: activeRegistry, command, withdraw, release, observe: new AbortController() };
146
+ current = registration;
147
+ failure = null;
148
+ void observe(registration).catch((error) => warn("KimiCU MCP: " + String(error)));
149
+ } catch (error) {
150
+ await release?.();
151
+ report("registration-failed", error);
152
+ }
153
+ };
154
+ return {
155
+ setManager(value) {
156
+ manager = value;
157
+ return enqueue(reconcile);
158
+ },
159
+ setRegistry(value) {
160
+ registry = value;
161
+ return enqueue(reconcile);
162
+ },
163
+ sync(status) {
164
+ return enqueue(async () => {
165
+ lastDetection = status;
166
+ await reconcile();
167
+ });
168
+ },
169
+ read,
170
+ restart(status) {
171
+ return enqueue(async () => {
172
+ const previous = current;
173
+ lastDetection = status;
174
+ await reconcile();
175
+ const registration = current;
176
+ if (registration !== void 0 && registration === previous) await registration.manager.restart(MCP_SERVER_NAME);
177
+ return registration;
178
+ }).then(async (registration) => {
179
+ if (registration === void 0) return false;
180
+ const signal = AbortSignal.any([registration.observe.signal, AbortSignal.timeout(MCP_CONNECT_TIMEOUT_MS)]);
181
+ for await (const views of registration.manager.watch(signal)) {
182
+ const state = kimiView(views)?.status.state;
183
+ if (state === "connected") return current === registration;
184
+ if (state === void 0 || isTerminal(state)) return false;
185
+ }
186
+ return false;
187
+ });
188
+ }
189
+ };
190
+ }
191
+
42
192
  // src/server/resolve.ts
43
193
  function toPlatform(platform) {
44
194
  if (platform === "darwin") return "darwin";
@@ -49,7 +199,7 @@ function installPlan(platform) {
49
199
  if (platform === "darwin") {
50
200
  return {
51
201
  command: MACOS_INSTALL_COMMAND,
52
- steps: ["install", "grant-permissions", "reload-plugin"],
202
+ steps: ["install", "grant-permissions", "recheck"],
53
203
  docsUrl: DOCS_URL,
54
204
  docsUrlEn: DOCS_URL_EN
55
205
  };
@@ -57,7 +207,7 @@ function installPlan(platform) {
57
207
  if (platform === "win32") {
58
208
  return {
59
209
  command: WINDOWS_INSTALL_COMMAND,
60
- steps: ["install", "reload-plugin"],
210
+ steps: ["install", "recheck"],
61
211
  docsUrl: DOCS_URL,
62
212
  docsUrlEn: DOCS_URL_EN
63
213
  };
@@ -79,9 +229,9 @@ function resolveInstall(input) {
79
229
  supported: true,
80
230
  installed,
81
231
  path: installed ? MACOS_BINARY : null,
82
- mcp: { command: MACOS_BINARY, args: ["mcp", "-s", "user"] },
232
+ mcp: { command: MACOS_MCP_LAUNCHER, args: ["mcp", "-s", "user", "--surface", "codeact"] },
83
233
  install: installPlan("darwin"),
84
- notes: []
234
+ notes: installed && !input.exists(MACOS_MCP_LAUNCHER) ? ["mcp-command-missing"] : []
85
235
  };
86
236
  }
87
237
  if (platform === "win32") {
@@ -144,7 +294,18 @@ function parseDoctor(platform, raw) {
144
294
  const lines = raw.split("\n").map((line) => line.trim());
145
295
  if (platform === "darwin") {
146
296
  const checks = [];
297
+ const hasServiceSection = lines.some((line) => /^Service \(authoritative/i.test(line));
298
+ let inServiceSection = !hasServiceSection;
147
299
  for (const line of lines) {
300
+ if (/^Service \(authoritative/i.test(line)) {
301
+ inServiceSection = true;
302
+ continue;
303
+ }
304
+ if (/^This process \(diagnostic/i.test(line)) {
305
+ inServiceSection = false;
306
+ continue;
307
+ }
308
+ if (!inServiceSection) continue;
148
309
  const mark = line.slice(0, 1);
149
310
  if (mark !== "\u2705" && mark !== "\u274C") continue;
150
311
  const label = line.slice(1).trim();
@@ -432,7 +593,7 @@ function createUpdater(deps) {
432
593
  let release = true;
433
594
  try {
434
595
  emit({ type: "stage", stage: "checking" });
435
- const current = await readCurrent(false);
596
+ const current = await readCurrent(true);
436
597
  if (current.path === null) {
437
598
  emit({ type: "error", kind: "unknown", raw: "KimiCU is not installed" });
438
599
  return;
@@ -472,7 +633,14 @@ function createUpdater(deps) {
472
633
  return;
473
634
  }
474
635
  const after = await readCurrent(true);
475
- const changed = after.version !== null && after.version !== current.version;
636
+ const target = inspected.latest;
637
+ const targetOrder = after.version !== null && target !== null ? compareVersions(after.version, target) : null;
638
+ const previousOrder = after.version !== null && current.version !== null ? compareVersions(after.version, current.version) : null;
639
+ if (after.path === null || after.version === null || previousOrder === -1 || target !== null && after.version !== target && (targetOrder === null || targetOrder < 0)) {
640
+ emit({ type: "error", kind: "unconfirmed", raw: outcome.raw });
641
+ return;
642
+ }
643
+ const changed = after.version !== current.version;
476
644
  let reconnect = "skipped";
477
645
  if (changed && deps.reconnect !== null) {
478
646
  emit({ type: "stage", stage: "restarting" });
@@ -482,7 +650,7 @@ function createUpdater(deps) {
482
650
  reconnect = "failed";
483
651
  }
484
652
  }
485
- emit({ type: "done", upToDate: false, version: after.version, reconnect, raw: outcome.raw });
653
+ emit({ type: "done", upToDate: !changed, version: after.version, reconnect, raw: outcome.raw });
486
654
  } finally {
487
655
  if (release) running = false;
488
656
  }
@@ -511,9 +679,9 @@ function createUpdateRoute(updater) {
511
679
  }
512
680
  }
513
681
  const encoder = new TextEncoder();
682
+ let closed = false;
514
683
  const stream = new ReadableStream({
515
684
  start(controller) {
516
- let closed = false;
517
685
  const emit = (event) => {
518
686
  if (closed) return;
519
687
  try {
@@ -529,6 +697,9 @@ function createUpdateRoute(updater) {
529
697
  closed = true;
530
698
  controller.close();
531
699
  });
700
+ },
701
+ cancel() {
702
+ closed = true;
532
703
  }
533
704
  });
534
705
  return new Response(stream, {
@@ -602,24 +773,18 @@ function runUpgradeProcess(input) {
602
773
  var name = "dsh-kimi-cu";
603
774
  var inject = ["tools"];
604
775
  var TOOL_NAME = "kimi_cu_status";
605
- var MCP_SERVER_NAME = "kimi-cu";
606
- var COMPUTER_USE_PROVIDER = "kimi-cu";
607
- var MCP_TOOL_TIMEOUT_MS = 3e5;
608
776
  function apply(ctx, options) {
609
- const detect = options?.detect ?? defaultStatusDetector();
610
- let mcpCtx;
611
- let disposeMcp;
612
- let desktopDenied = false;
613
- const reconnect = async () => {
614
- const manager = mcpCtx?.mcpManager;
615
- if (manager?.restart === void 0) return false;
616
- await manager.restart(MCP_SERVER_NAME);
617
- return true;
777
+ const detectBase = options?.detect ?? defaultStatusDetector();
778
+ const mcp = createMcpConnection((message) => ctx.logger.warn(message));
779
+ const detect = async (refresh) => {
780
+ const status = await detectBase(refresh);
781
+ if (refresh) await mcp.sync(status);
782
+ return { ...status, mcpStatus: mcp.read() };
618
783
  };
619
784
  const updater = createUpdater({
620
785
  platform: toPlatform(process.platform),
621
786
  current: async (refresh) => {
622
- const status = await detect(refresh);
787
+ const status = await detectBase(refresh);
623
788
  return { path: status.path, version: status.version };
624
789
  },
625
790
  fetchManifest: fetchManifestText,
@@ -629,7 +794,7 @@ function apply(ctx, options) {
629
794
  args: input.version === null ? ["upgrade"] : ["upgrade", input.version],
630
795
  onLine: input.onLine
631
796
  }),
632
- reconnect
797
+ reconnect: async () => mcp.restart(await detectBase())
633
798
  });
634
799
  ctx.tools.register(defineTool({
635
800
  name: TOOL_NAME,
@@ -670,54 +835,19 @@ function apply(ctx, options) {
670
835
  });
671
836
  };
672
837
  });
673
- const syncMcp = async () => {
674
- if (desktopDenied || mcpCtx === void 0 || disposeMcp !== void 0) return;
675
- const status = await detect();
676
- if (desktopDenied || mcpCtx === void 0 || disposeMcp !== void 0) return;
677
- if (status.mcp === null) return;
678
- disposeMcp = mcpCtx.mcpManager.register(MCP_SERVER_NAME, {
679
- transport: "stdio",
680
- command: status.mcp.command,
681
- args: status.mcp.args,
682
- toolCallTimeoutMs: MCP_TOOL_TIMEOUT_MS
683
- });
684
- };
685
- ctx.inject(["computerUse"], (cuCtx) => {
686
- const registry = cuCtx.computerUse;
687
- let release;
688
- try {
689
- release = registry.register(COMPUTER_USE_PROVIDER);
690
- } catch (error) {
691
- desktopDenied = true;
692
- disposeMcp?.();
693
- disposeMcp = void 0;
694
- ctx.logger.warn(
695
- "KimiCU \u6CA1\u6709\u5360\u7528\u684C\u9762\u64CD\u4F5C\u69FD\u4F4D\uFF08" + (error instanceof Error ? error.message : String(error)) + "\uFF09\uFF1B\u5DF2\u505C\u7528 mcp__kimi-cu__* \u5DE5\u5177\uFF0C\u907F\u514D\u4E24\u5957\u540E\u7AEF\u540C\u65F6\u63A7\u5236\u684C\u9762\u3002"
696
- );
697
- return () => {
698
- desktopDenied = false;
699
- void syncMcp().catch(() => {
700
- });
701
- };
702
- }
703
- void syncMcp().catch(() => {
704
- });
705
- return () => {
706
- void Promise.resolve(release?.()).catch(() => {
707
- });
708
- };
838
+ const logFailure = (error) => ctx.logger.warn("KimiCU MCP: " + String(error));
839
+ ctx.inject(["computerUse"], (injected) => {
840
+ const registry = injected.computerUse;
841
+ void mcp.setRegistry(registry).catch(logFailure);
842
+ return () => mcp.setRegistry(void 0).catch(logFailure);
709
843
  });
710
- ctx.inject(["mcpManager"], (injectedCtx) => {
711
- mcpCtx = injectedCtx;
712
- void syncMcp().catch(() => {
713
- });
714
- return () => {
715
- mcpCtx = void 0;
716
- disposeMcp?.();
717
- disposeMcp = void 0;
718
- };
844
+ ctx.inject(["mcpManager"], (injected) => {
845
+ const manager = injected.mcpManager;
846
+ void mcp.setManager(manager).catch(logFailure);
847
+ return () => mcp.setManager(void 0).catch(logFailure);
719
848
  });
720
- void detect().then((status) => {
849
+ void detectBase().then(async (status) => {
850
+ await mcp.sync(status);
721
851
  if (!status.supported || status.installed) return;
722
852
  ctx.logger.warn(
723
853
  "KimiCU is not installed. Install it with: " + (status.install?.command ?? "")