@lynn123411/dsh-kimi-cu 0.3.2 → 0.3.4

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,6 +2,9 @@
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";
7
10
  var MACOS_MCP_LAUNCHER = "/Applications/KimiCU.app/Contents/Resources/kimi-cu-mcp";
@@ -40,6 +43,152 @@ function createStatusRoute(detect) {
40
43
  };
41
44
  }
42
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
+
43
192
  // src/server/resolve.ts
44
193
  function toPlatform(platform) {
45
194
  if (platform === "darwin") return "darwin";
@@ -50,7 +199,7 @@ function installPlan(platform) {
50
199
  if (platform === "darwin") {
51
200
  return {
52
201
  command: MACOS_INSTALL_COMMAND,
53
- steps: ["install", "grant-permissions", "reload-plugin"],
202
+ steps: ["install", "grant-permissions", "recheck"],
54
203
  docsUrl: DOCS_URL,
55
204
  docsUrlEn: DOCS_URL_EN
56
205
  };
@@ -58,7 +207,7 @@ function installPlan(platform) {
58
207
  if (platform === "win32") {
59
208
  return {
60
209
  command: WINDOWS_INSTALL_COMMAND,
61
- steps: ["install", "reload-plugin"],
210
+ steps: ["install", "recheck"],
62
211
  docsUrl: DOCS_URL,
63
212
  docsUrlEn: DOCS_URL_EN
64
213
  };
@@ -444,7 +593,7 @@ function createUpdater(deps) {
444
593
  let release = true;
445
594
  try {
446
595
  emit({ type: "stage", stage: "checking" });
447
- const current = await readCurrent(false);
596
+ const current = await readCurrent(true);
448
597
  if (current.path === null) {
449
598
  emit({ type: "error", kind: "unknown", raw: "KimiCU is not installed" });
450
599
  return;
@@ -484,7 +633,14 @@ function createUpdater(deps) {
484
633
  return;
485
634
  }
486
635
  const after = await readCurrent(true);
487
- 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;
488
644
  let reconnect = "skipped";
489
645
  if (changed && deps.reconnect !== null) {
490
646
  emit({ type: "stage", stage: "restarting" });
@@ -494,7 +650,7 @@ function createUpdater(deps) {
494
650
  reconnect = "failed";
495
651
  }
496
652
  }
497
- 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 });
498
654
  } finally {
499
655
  if (release) running = false;
500
656
  }
@@ -523,9 +679,9 @@ function createUpdateRoute(updater) {
523
679
  }
524
680
  }
525
681
  const encoder = new TextEncoder();
682
+ let closed = false;
526
683
  const stream = new ReadableStream({
527
684
  start(controller) {
528
- let closed = false;
529
685
  const emit = (event) => {
530
686
  if (closed) return;
531
687
  try {
@@ -541,6 +697,9 @@ function createUpdateRoute(updater) {
541
697
  closed = true;
542
698
  controller.close();
543
699
  });
700
+ },
701
+ cancel() {
702
+ closed = true;
544
703
  }
545
704
  });
546
705
  return new Response(stream, {
@@ -614,29 +773,18 @@ function runUpgradeProcess(input) {
614
773
  var name = "dsh-kimi-cu";
615
774
  var inject = ["tools"];
616
775
  var TOOL_NAME = "kimi_cu_status";
617
- var MCP_SERVER_NAME = "kimi-cu";
618
- var COMPUTER_USE_PROVIDER = "kimi-cu";
619
- var MCP_TOOL_TIMEOUT_MS = 3e5;
620
776
  function apply(ctx, options) {
621
777
  const detectBase = options?.detect ?? defaultStatusDetector();
622
- let mcpCtx;
623
- let disposeMcp;
624
- let desktopDenied = false;
778
+ const mcp = createMcpConnection((message) => ctx.logger.warn(message));
625
779
  const detect = async (refresh) => {
626
780
  const status = await detectBase(refresh);
627
- const connection = mcpCtx?.mcpManager.list?.().find((item) => item.name === MCP_SERVER_NAME)?.status;
628
- return { ...status, mcpStatus: connection ?? null };
629
- };
630
- const reconnect = async () => {
631
- const manager = mcpCtx?.mcpManager;
632
- if (manager?.restart === void 0) return false;
633
- await manager.restart(MCP_SERVER_NAME);
634
- return true;
781
+ if (refresh) await mcp.sync(status);
782
+ return { ...status, mcpStatus: mcp.read() };
635
783
  };
636
784
  const updater = createUpdater({
637
785
  platform: toPlatform(process.platform),
638
786
  current: async (refresh) => {
639
- const status = await detect(refresh);
787
+ const status = await detectBase(refresh);
640
788
  return { path: status.path, version: status.version };
641
789
  },
642
790
  fetchManifest: fetchManifestText,
@@ -646,7 +794,7 @@ function apply(ctx, options) {
646
794
  args: input.version === null ? ["upgrade"] : ["upgrade", input.version],
647
795
  onLine: input.onLine
648
796
  }),
649
- reconnect
797
+ reconnect: async () => mcp.restart(await detectBase())
650
798
  });
651
799
  ctx.tools.register(defineTool({
652
800
  name: TOOL_NAME,
@@ -687,54 +835,19 @@ function apply(ctx, options) {
687
835
  });
688
836
  };
689
837
  });
690
- const syncMcp = async () => {
691
- if (desktopDenied || mcpCtx === void 0 || disposeMcp !== void 0) return;
692
- const status = await detect();
693
- if (desktopDenied || mcpCtx === void 0 || disposeMcp !== void 0) return;
694
- if (status.mcp === null) return;
695
- disposeMcp = mcpCtx.mcpManager.register(MCP_SERVER_NAME, {
696
- transport: "stdio",
697
- command: status.mcp.command,
698
- args: status.mcp.args,
699
- toolCallTimeoutMs: MCP_TOOL_TIMEOUT_MS
700
- });
701
- };
702
- ctx.inject(["computerUse"], (cuCtx) => {
703
- const registry = cuCtx.computerUse;
704
- let release;
705
- try {
706
- release = registry.register(COMPUTER_USE_PROVIDER);
707
- } catch (error) {
708
- desktopDenied = true;
709
- disposeMcp?.();
710
- disposeMcp = void 0;
711
- ctx.logger.warn(
712
- "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"
713
- );
714
- return () => {
715
- desktopDenied = false;
716
- void syncMcp().catch(() => {
717
- });
718
- };
719
- }
720
- void syncMcp().catch(() => {
721
- });
722
- return () => {
723
- void Promise.resolve(release?.()).catch(() => {
724
- });
725
- };
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);
726
843
  });
727
- ctx.inject(["mcpManager"], (injectedCtx) => {
728
- mcpCtx = injectedCtx;
729
- void syncMcp().catch(() => {
730
- });
731
- return () => {
732
- mcpCtx = void 0;
733
- disposeMcp?.();
734
- disposeMcp = void 0;
735
- };
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);
736
848
  });
737
- void detect().then((status) => {
849
+ void detectBase().then(async (status) => {
850
+ await mcp.sync(status);
738
851
  if (!status.supported || status.installed) return;
739
852
  ctx.logger.warn(
740
853
  "KimiCU is not installed. Install it with: " + (status.install?.command ?? "")