dsh-mcviseron-plugin 0.0.0-stage → 0.6.2

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.
Files changed (44) hide show
  1. package/CHANGELOG.md +222 -0
  2. package/CONTRIBUTING.md +60 -0
  3. package/LICENSE +202 -0
  4. package/README.en.md +146 -0
  5. package/README.md +287 -2
  6. package/SECURITY.md +38 -0
  7. package/cordis.patch.yml +12 -0
  8. package/docs/automation-task.md +63 -0
  9. package/docs/migration-from-standalone.md +115 -0
  10. package/docs/release.md +85 -0
  11. package/lib/appearance.js +320 -0
  12. package/lib/auto-review-fix.js +453 -0
  13. package/lib/client.js +1266 -0
  14. package/lib/index.js +1701 -0
  15. package/lib/restart.js +226 -0
  16. package/lib/task-guard.js +736 -0
  17. package/package.json +148 -4
  18. package/scripts/repair-profile.mjs +77 -0
  19. package/src/api.ts +5 -0
  20. package/src/appearance/index.ts +103 -0
  21. package/src/auto-review-fix/index.ts +45 -0
  22. package/src/client/AppearanceScheduleRow.tsx +112 -0
  23. package/src/client/ConfigForm.tsx +284 -0
  24. package/src/client/FallbackRestartRow.tsx +23 -0
  25. package/src/client/RestartCard.tsx +108 -0
  26. package/src/client/SettingsSection.tsx +110 -0
  27. package/src/client/bridge.ts +131 -0
  28. package/src/client/driver.ts +166 -0
  29. package/src/client/index.ts +114 -0
  30. package/src/client/locales.ts +193 -0
  31. package/src/client/primitives.d.ts +15 -0
  32. package/src/client/section-presence.ts +33 -0
  33. package/src/client/styles.ts +154 -0
  34. package/src/components/auto-review-fix/index.ts +421 -0
  35. package/src/components/auto-review-fix/translate.ts +235 -0
  36. package/src/components/task-guard/index.ts +1077 -0
  37. package/src/index.ts +71 -0
  38. package/src/loopback.ts +48 -0
  39. package/src/namespace.ts +7 -0
  40. package/src/restart/index.ts +268 -0
  41. package/src/routes.ts +228 -0
  42. package/src/schedule.ts +125 -0
  43. package/src/task-guard/index.ts +52 -0
  44. package/src/volatile.ts +32 -0
package/lib/restart.js ADDED
@@ -0,0 +1,226 @@
1
+ // src/restart/index.ts
2
+ import { execFileSync } from "node:child_process";
3
+ import { appendFileSync, existsSync } from "node:fs";
4
+ import { tmpdir } from "node:os";
5
+ import { basename, dirname, join, resolve } from "node:path";
6
+ import z from "@deepseek-ai/schemastery";
7
+
8
+ // src/loopback.ts
9
+ function isIPv4Loopback(value) {
10
+ const parts = value.split(".");
11
+ return parts.length === 4 && parts[0] === "127" && parts.every((part) => /^\d{1,3}$/.test(part) && Number(part) <= 255);
12
+ }
13
+ function isLoopbackAddress(address) {
14
+ if (address === void 0) return false;
15
+ const normalized = address.toLowerCase();
16
+ if (normalized === "::1") return true;
17
+ if (normalized.startsWith("::ffff:")) return isIPv4Loopback(normalized.slice("::ffff:".length));
18
+ return isIPv4Loopback(normalized);
19
+ }
20
+ function isLoopbackHostname(hostname) {
21
+ if (hostname === "localhost" || hostname === "[::1]") return true;
22
+ return isIPv4Loopback(hostname);
23
+ }
24
+ function isLoopbackRequest(request) {
25
+ if (!isLoopbackAddress(request.socket.remoteAddress)) return false;
26
+ const host = request.headers.host;
27
+ if (typeof host !== "string") return false;
28
+ let hostUrl;
29
+ try {
30
+ hostUrl = new URL("http://" + host);
31
+ } catch {
32
+ return false;
33
+ }
34
+ if (!isLoopbackHostname(hostUrl.hostname)) return false;
35
+ if (request.headers["sec-fetch-site"] === "cross-site") return false;
36
+ const origin = request.headers.origin;
37
+ if (origin === void 0) return true;
38
+ try {
39
+ return new URL(origin).host === hostUrl.host;
40
+ } catch {
41
+ return false;
42
+ }
43
+ }
44
+
45
+ // src/volatile.ts
46
+ function isVolatileLike(value) {
47
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
48
+ if (typeof value.get !== "function") return false;
49
+ return Object.getOwnPropertySymbols(value).length > 0 || Object.keys(value).every((key) => key === "get");
50
+ }
51
+ function toPlain(value) {
52
+ if (isVolatileLike(value)) return toPlain(value.get());
53
+ if (Array.isArray(value)) return value.map((item) => toPlain(item));
54
+ if (value !== null && typeof value === "object") {
55
+ const result = {};
56
+ for (const [key, child] of Object.entries(value)) result[key] = toPlain(child);
57
+ return result;
58
+ }
59
+ return value;
60
+ }
61
+
62
+ // src/restart/index.ts
63
+ var DEFAULT_RESTART_CONFIG = { enabled: true, appPath: "" };
64
+ var Config = z.object({
65
+ enabled: z.boolean().default(DEFAULT_RESTART_CONFIG.enabled).volatile(),
66
+ /** Optional explicit application executable; empty derives it from the Host process. */
67
+ appPath: z.string().default(DEFAULT_RESTART_CONFIG.appPath).volatile()
68
+ }).default(DEFAULT_RESTART_CONFIG);
69
+ var RESTART_API = {
70
+ status: "/api/dsh-mcviseron/restart/status",
71
+ execute: "/api/dsh-mcviseron/restart/execute"
72
+ };
73
+ function resolveRestartTarget(execPath, parentPid, exists, override = "", argv = [], platform = process.platform) {
74
+ if (platform !== "win32") return void 0;
75
+ if (!Number.isSafeInteger(parentPid) || parentPid <= 0) return void 0;
76
+ const configured = typeof override === "string" ? override.trim() : "";
77
+ const candidate = configured !== "" ? configured : deriveDesktopAppPath(execPath, argv, exists);
78
+ if (candidate === "" || !exists(candidate)) return void 0;
79
+ const name = basename(candidate).toLowerCase();
80
+ if (name === "node" || name === "node.exe") return void 0;
81
+ return { mainPid: parentPid, hostPid: process.pid, exePath: candidate };
82
+ }
83
+ function deriveDesktopAppPath(execPath, argv, exists) {
84
+ const isNode = (value) => /^node(\.exe)?$/i.test(basename(value));
85
+ if (!isNode(execPath)) return execPath;
86
+ const argv0 = argv[0] ?? "";
87
+ if (argv0 !== "" && argv0 !== execPath && !isNode(argv0) && exists(argv0)) return argv0;
88
+ const runtimeDir = argv[2] ?? "";
89
+ if (runtimeDir === "") return execPath;
90
+ const candidate = join(resolve(runtimeDir, "../../.."), "DeepSeek Harness.exe");
91
+ return exists(candidate) ? candidate : execPath;
92
+ }
93
+ var RESTART_TASK_NAME = "DSH-MCviseron-Restart";
94
+ var RESTART_DELAY_SECONDS = 6;
95
+ function restartLogPath() {
96
+ return join(tmpdir(), "dsh-mcviseron-restart.log");
97
+ }
98
+ function makeTaskScript(target, taskName, delaySeconds) {
99
+ const quote = (value) => "'" + value.replace(/'/g, "''") + "'";
100
+ return [
101
+ "$user = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name",
102
+ `$action = New-ScheduledTaskAction -Execute ${quote(target.exePath)} -WorkingDirectory ${quote(dirname(target.exePath))}`,
103
+ `$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddSeconds(${delaySeconds})`,
104
+ "$principal = New-ScheduledTaskPrincipal -UserId $user -LogonType Interactive -RunLevel Limited",
105
+ `Register-ScheduledTask -TaskName ${quote(taskName)} -Action $action -Trigger $trigger -Principal $principal -Force | Out-Null`,
106
+ ""
107
+ ].join("\n");
108
+ }
109
+ function launchHelper(target) {
110
+ const logFile = restartLogPath();
111
+ try {
112
+ execFileSync("powershell.exe", [
113
+ "-NoProfile",
114
+ "-NonInteractive",
115
+ "-WindowStyle",
116
+ "Hidden",
117
+ "-Command",
118
+ makeTaskScript(target, RESTART_TASK_NAME, RESTART_DELAY_SECONDS)
119
+ ], { windowsHide: true, timeout: 2e4, encoding: "utf8" });
120
+ appendFileSync(logFile, `${(/* @__PURE__ */ new Date()).toISOString()} host: scheduled ${target.exePath} in ${RESTART_DELAY_SECONDS}s
121
+ `, "utf8");
122
+ } catch (error) {
123
+ const message = error instanceof Error ? error.message : String(error);
124
+ appendFileSync(logFile, `${(/* @__PURE__ */ new Date()).toISOString()} host: task registration failed: ${message}
125
+ `, "utf8");
126
+ throw new Error("cannot schedule the restart task: " + message, { cause: error });
127
+ }
128
+ setTimeout(() => {
129
+ try {
130
+ process.kill(target.mainPid);
131
+ appendFileSync(logFile, `${(/* @__PURE__ */ new Date()).toISOString()} host: kill sent to ${target.mainPid}
132
+ `, "utf8");
133
+ } catch (error) {
134
+ appendFileSync(logFile, `${(/* @__PURE__ */ new Date()).toISOString()} host: kill failed: ${error instanceof Error ? error.message : String(error)}
135
+ `, "utf8");
136
+ }
137
+ }, 700);
138
+ }
139
+ function json(res, status, body) {
140
+ res.writeHead(status, { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" });
141
+ res.end(JSON.stringify(body));
142
+ }
143
+ async function readJsonBody(req) {
144
+ const chunks = [];
145
+ let size = 0;
146
+ for await (const chunk of req) {
147
+ const buffer = chunk;
148
+ size += buffer.length;
149
+ if (size > 8 * 1024) return void 0;
150
+ chunks.push(buffer);
151
+ }
152
+ try {
153
+ return JSON.parse(Buffer.concat(chunks).toString("utf8"));
154
+ } catch {
155
+ return void 0;
156
+ }
157
+ }
158
+ function makeRestartRoutes(deps) {
159
+ let lastLaunch = Number.NEGATIVE_INFINITY;
160
+ const now = deps.now ?? (() => Date.now());
161
+ const status = {
162
+ kind: "exact",
163
+ path: RESTART_API.status,
164
+ handler: async (req, res) => {
165
+ try {
166
+ if (req.method !== "GET" && req.method !== "POST") return json(res, 405, { ok: false, error: "method-not-allowed" });
167
+ if (!isLoopbackRequest(req)) return json(res, 403, { ok: false, error: "forbidden" });
168
+ const target = deps.target();
169
+ json(res, 200, target === void 0 ? { ok: true, available: false, reason: "desktop-app-unavailable" } : { ok: true, available: true, appPath: target.exePath, mainPid: target.mainPid });
170
+ } catch (error) {
171
+ json(res, 500, { ok: false, error: error instanceof Error ? error.message : String(error) });
172
+ }
173
+ }
174
+ };
175
+ const execute = {
176
+ kind: "exact",
177
+ path: RESTART_API.execute,
178
+ handler: async (req, res) => {
179
+ try {
180
+ if (req.method !== "POST") return json(res, 405, { ok: false, error: "method-not-allowed" });
181
+ if (!isLoopbackRequest(req)) return json(res, 403, { ok: false, error: "forbidden" });
182
+ const body = await readJsonBody(req);
183
+ if (body === void 0 || typeof body !== "object" || body === null || body.confirm !== true) {
184
+ return json(res, 400, { ok: false, error: "confirmation-required" });
185
+ }
186
+ const latest = now();
187
+ if (latest - lastLaunch < 5e3) return json(res, 429, { ok: false, error: "restart-already-scheduled" });
188
+ const target = deps.target();
189
+ if (target === void 0) return json(res, 409, { ok: false, error: "desktop-app-unavailable" });
190
+ deps.launch(target);
191
+ lastLaunch = latest;
192
+ json(res, 200, { ok: true, restarting: true, appPath: target.exePath });
193
+ } catch (error) {
194
+ json(res, 500, { ok: false, error: error instanceof Error ? error.message : String(error) });
195
+ }
196
+ }
197
+ };
198
+ return [status, execute];
199
+ }
200
+ var inject = ["webServer"];
201
+ function apply(ctx, config) {
202
+ const plain = toPlain(config) ?? {};
203
+ if (plain.enabled === false) return;
204
+ const appPath = typeof plain.appPath === "string" ? plain.appPath : "";
205
+ const routes = makeRestartRoutes({
206
+ target: () => resolveRestartTarget(process.execPath, process.ppid, existsSync, appPath, process.argv),
207
+ launch: launchHelper
208
+ });
209
+ ctx.effect(() => {
210
+ const disposers = routes.map((route) => ctx.webServer.register(route));
211
+ return () => {
212
+ for (const dispose of disposers) dispose();
213
+ };
214
+ }, "mcviseron-restart: restart routes");
215
+ }
216
+ export {
217
+ Config,
218
+ DEFAULT_RESTART_CONFIG,
219
+ RESTART_API,
220
+ apply,
221
+ deriveDesktopAppPath,
222
+ inject,
223
+ makeRestartRoutes,
224
+ makeTaskScript,
225
+ resolveRestartTarget
226
+ };