@linxin666/dsh-client-ui-plugin-manager 0.4.3 → 0.4.5
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/README.i18n.yaml +2 -2
- package/README.md +22 -8
- package/README.zh.md +22 -8
- package/cordis.patch.yml +14 -1
- package/lib/client.js +569 -10
- package/lib/client.js.map +1 -1
- package/lib/index.js +393 -22
- package/lib/types/client/PluginListToolbar.d.ts +44 -0
- package/lib/types/client/body-mutations.d.ts +45 -0
- package/lib/types/client/index.d.ts +18 -1
- package/lib/types/client/locales.d.ts +54 -2
- package/lib/types/client/plugin-toolbar-mount.d.ts +47 -0
- package/lib/types/core/protocol.d.ts +13 -0
- package/lib/types/core/updates.d.ts +30 -0
- package/lib/types/core/version.d.ts +4 -1
- package/lib/types/host/gateway.d.ts +107 -3
- package/lib/types/host/restart.d.ts +95 -0
- package/lib/types/host/routes.d.ts +10 -1
- package/lib/types/host/state.d.ts +3 -1
- package/lib/types/index.d.ts +7 -2
- package/package.json +13 -13
package/lib/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
-
import { closeSync, existsSync, openSync, readFileSync, readSync } from "node:fs";
|
|
2
|
+
import { appendFileSync, closeSync, existsSync, mkdirSync, openSync, readFileSync, readSync } from "node:fs";
|
|
3
3
|
import { copyFile, readFile, rename, writeFile } from "node:fs/promises";
|
|
4
|
-
import { basename, dirname, isAbsolute, join, posix, sep, win32 } from "node:path";
|
|
4
|
+
import { basename, dirname, isAbsolute, join, normalize, posix, sep, win32 } from "node:path";
|
|
5
5
|
import { isMap, isScalar, isSeq, parseDocument } from "yaml";
|
|
6
6
|
import { homedir } from "node:os";
|
|
7
7
|
import { isAbsolute as isAbsolute$1, join as join$1 } from "node:path/posix";
|
|
@@ -545,6 +545,10 @@ function resolveDshHome(env = process.env, home = homedir()) {
|
|
|
545
545
|
}
|
|
546
546
|
return j(home, ".dsh");
|
|
547
547
|
}
|
|
548
|
+
/** Resolve the DSH home directory from the live environment. */
|
|
549
|
+
function dshHome() {
|
|
550
|
+
return resolveDshHome();
|
|
551
|
+
}
|
|
548
552
|
//#endregion
|
|
549
553
|
//#region src/host/profile.ts
|
|
550
554
|
/**
|
|
@@ -676,16 +680,18 @@ function launchedFacts(launched, desktopLauncher, env) {
|
|
|
676
680
|
if (name !== "") assertSafeProfileName(name);
|
|
677
681
|
const profileRoot = join(resolveDshHome(env), "profiles");
|
|
678
682
|
const profileDir = dir !== "" ? dir : join(profileRoot, name);
|
|
679
|
-
if (!isAbsolute(profileDir) || profileDir.split(
|
|
680
|
-
|
|
681
|
-
|
|
683
|
+
if (!isAbsolute(profileDir) || profileDir.split(/[\/\\]/).includes("..")) throw new Error(`plugin-manager: published profile directory is not a safe absolute path ${JSON.stringify(launched.dir ?? "")}`);
|
|
684
|
+
const dirBase = profileDir.split(/[\/\\]/).filter(Boolean).pop() ?? basename(profileDir);
|
|
685
|
+
if (name !== "" && name !== dirBase) throw new Error(`plugin-manager: published profile name and directory disagree ${JSON.stringify(name)} vs ${JSON.stringify(profileDir)}`);
|
|
686
|
+
const isPosixLiteral = profileDir.includes("/") && !profileDir.includes("\\");
|
|
687
|
+
const ownPatchPath = isPosixLiteral ? `${profileDir}/cordis.patch.yml` : join(profileDir, "cordis.patch.yml");
|
|
682
688
|
const publishedPatchPath = typeof launched.patchPath === "string" ? launched.patchPath.trim() : "";
|
|
683
|
-
if (publishedPatchPath !== "" && publishedPatchPath !== ownPatchPath) throw new Error(`plugin-manager: published patch path is not the launched profile's own patch file ${JSON.stringify(launched.patchPath)}`);
|
|
689
|
+
if (publishedPatchPath !== "" && publishedPatchPath !== ownPatchPath && normalize(publishedPatchPath) !== normalize(ownPatchPath)) throw new Error(`plugin-manager: published patch path is not the launched profile's own patch file ${JSON.stringify(launched.patchPath)}`);
|
|
684
690
|
return {
|
|
685
|
-
profileName: name !== "" ? name :
|
|
691
|
+
profileName: name !== "" ? name : dirBase,
|
|
686
692
|
profileDir,
|
|
687
693
|
patchPath: ownPatchPath,
|
|
688
|
-
packageJsonPath: join(profileDir, "package.json"),
|
|
694
|
+
packageJsonPath: isPosixLiteral ? `${profileDir}/package.json` : join(profileDir, "package.json"),
|
|
689
695
|
desktop: desktopLauncher
|
|
690
696
|
};
|
|
691
697
|
}
|
|
@@ -805,10 +811,12 @@ function profileExists(profileDir) {
|
|
|
805
811
|
* itself (the UI performing the write), the family settings surface the tab
|
|
806
812
|
* injects into, and the aggregate compat face (disabling it unmounts the
|
|
807
813
|
* whole folded client bundle while host rows keep running — a half-dead
|
|
808
|
-
* page). Covers both the standalone and the aggregate row ids.
|
|
814
|
+
* page). Covers both the standalone and the aggregate row ids. The standalone
|
|
815
|
+
* id is this package's own `cordis.patch.yml` insert id, never the official
|
|
816
|
+
* `ui-plugin-manager` row the DSH web-app bundle already claims.
|
|
809
817
|
*/
|
|
810
818
|
const LOCKED_ENTRY_IDS = /* @__PURE__ */ new Set([
|
|
811
|
-
"ui-plugin-manager",
|
|
819
|
+
"ui-plugin-manager-update-check",
|
|
812
820
|
"web-ui-plugin-manager",
|
|
813
821
|
"ui-web-ui-settings",
|
|
814
822
|
"web-ui-settings",
|
|
@@ -1041,6 +1049,11 @@ function createOutputCapture(maxBytes) {
|
|
|
1041
1049
|
* CLI changed (the conflict ledger). The npm web runtime has no installer
|
|
1042
1050
|
* service, so this gateway is its write path; on runtimes with official
|
|
1043
1051
|
* channels the browser half never calls it.
|
|
1052
|
+
*
|
|
1053
|
+
* One exception: an application-owned profile (a packaged Desktop launch)
|
|
1054
|
+
* cannot be written by the CLI at all — the official launcher refuses it — so
|
|
1055
|
+
* installs, updates and removals there run through the official in-process
|
|
1056
|
+
* plugin manager instead (see {@link NativePluginManager}).
|
|
1044
1057
|
* @module @linxin666/dsh-client-ui-plugin-manager/host
|
|
1045
1058
|
*/
|
|
1046
1059
|
/** Hard deadline for one CLI add (git clones can take minutes). */
|
|
@@ -1298,6 +1311,27 @@ async function detectOfficialChannels(binary, profileName, env = process.env, sp
|
|
|
1298
1311
|
}) !== 0) return false;
|
|
1299
1312
|
return OFFICIAL_INSTALLER_PATTERN.test(output.read());
|
|
1300
1313
|
}
|
|
1314
|
+
/**
|
|
1315
|
+
* The official manager's own failure text for one run, or undefined when its
|
|
1316
|
+
* verdict is not a failure. The manager resolves a failed run instead of
|
|
1317
|
+
* rejecting it, so this is the only place its refusal can be read — the profile
|
|
1318
|
+
* re-read afterwards can only say that nothing moved, never why.
|
|
1319
|
+
* @param outcome - the resolved value of `installBundle` / `removeBundle`.
|
|
1320
|
+
* @returns the failure text to report, or undefined for a run that did not fail.
|
|
1321
|
+
*/
|
|
1322
|
+
function nativeManagerFailure(outcome) {
|
|
1323
|
+
if (typeof outcome !== "object" || outcome === null) return void 0;
|
|
1324
|
+
const verdict = outcome;
|
|
1325
|
+
if (verdict.application === "cancelled") return "本次操作已取消(profile 未改动)";
|
|
1326
|
+
if (verdict.application !== "failed") return void 0;
|
|
1327
|
+
const failure = verdict.error;
|
|
1328
|
+
const diagnostic = typeof failure?.diagnostic === "string" ? failure.diagnostic.trim() : "";
|
|
1329
|
+
if (diagnostic !== "") return diagnostic;
|
|
1330
|
+
const incompatible = failure?.incompatible;
|
|
1331
|
+
if (Array.isArray(incompatible) && incompatible.length > 0) return incompatible.map((item) => `${item.name ?? "未知包"}@${item.version ?? "未知版本"} 与 dsh ${item.runtimeVersion ?? "当前版本"} 不兼容`).join(";");
|
|
1332
|
+
const code = failure?.code;
|
|
1333
|
+
return typeof code === "string" && code !== "" ? `官方插件管理器拒绝执行(${code})` : "官方插件管理器报告失败";
|
|
1334
|
+
}
|
|
1301
1335
|
/** The gateway: serializes CLI operations through one job table and one mutation queue. */
|
|
1302
1336
|
var CliGateway = class {
|
|
1303
1337
|
facts;
|
|
@@ -1360,6 +1394,16 @@ var CliGateway = class {
|
|
|
1360
1394
|
if (this.facts.desktop !== true) return void 0;
|
|
1361
1395
|
return this.deps.nativeManager?.();
|
|
1362
1396
|
}
|
|
1397
|
+
/**
|
|
1398
|
+
* Whether this gateway will write through the official in-process manager
|
|
1399
|
+
* instead of the CLI (an application-owned profile, see
|
|
1400
|
+
* {@link nativeManager}). The HTTP layer reads it so its CLI-availability
|
|
1401
|
+
* guard never rejects a job the CLI is not going to run.
|
|
1402
|
+
* @returns true when the native writer serves installs, updates and removals.
|
|
1403
|
+
*/
|
|
1404
|
+
usesNativeWriter() {
|
|
1405
|
+
return this.nativeManager() !== void 0;
|
|
1406
|
+
}
|
|
1363
1407
|
/** Run one CLI command to completion and return the bounded output. */
|
|
1364
1408
|
async runCli(binary, args, timeoutMs) {
|
|
1365
1409
|
const output = createOutputCapture(MAX_OUTPUT_BYTES);
|
|
@@ -1537,7 +1581,12 @@ var CliGateway = class {
|
|
|
1537
1581
|
spawnCli(binary, args) {
|
|
1538
1582
|
return (this.deps.spawnImpl ?? spawnDsh)(binary, args, this.env);
|
|
1539
1583
|
}
|
|
1540
|
-
/**
|
|
1584
|
+
/**
|
|
1585
|
+
* Start an install; the caller polls {@link status}. An application-owned
|
|
1586
|
+
* profile runs it through the official in-process manager instead of the CLI
|
|
1587
|
+
* (see {@link nativeManager}), exactly like {@link update}; the job table,
|
|
1588
|
+
* polling contract and profile verification are identical either way.
|
|
1589
|
+
*/
|
|
1541
1590
|
install(spec) {
|
|
1542
1591
|
const job = {
|
|
1543
1592
|
id: `job-${++this.counter}`,
|
|
@@ -1553,6 +1602,11 @@ var CliGateway = class {
|
|
|
1553
1602
|
this.retainFinished(job.id);
|
|
1554
1603
|
return { jobId: job.id };
|
|
1555
1604
|
}
|
|
1605
|
+
const native = this.nativeManager();
|
|
1606
|
+
if (native !== void 0) {
|
|
1607
|
+
this.enqueueNativeInstall(job, native);
|
|
1608
|
+
return { jobId: job.id };
|
|
1609
|
+
}
|
|
1556
1610
|
this.enqueue(() => this.run(job, [
|
|
1557
1611
|
"plugin",
|
|
1558
1612
|
"--profile",
|
|
@@ -1600,6 +1654,62 @@ var CliGateway = class {
|
|
|
1600
1654
|
], ADD_TIMEOUT_MS));
|
|
1601
1655
|
return { jobId: job.id };
|
|
1602
1656
|
}
|
|
1657
|
+
/** Start an install through the official in-process manager. */
|
|
1658
|
+
enqueueNativeInstall(job, native) {
|
|
1659
|
+
this.enqueue(async () => {
|
|
1660
|
+
try {
|
|
1661
|
+
await this.runNativeInstall(job, native);
|
|
1662
|
+
} catch (error) {
|
|
1663
|
+
job.phase = "error";
|
|
1664
|
+
job.error = `plugin-manager: 官方插件管理器安装失败:${error instanceof Error ? error.message : String(error)}`;
|
|
1665
|
+
}
|
|
1666
|
+
this.retainFinished(job.id);
|
|
1667
|
+
});
|
|
1668
|
+
}
|
|
1669
|
+
/**
|
|
1670
|
+
* Run one install through the official in-process manager (an
|
|
1671
|
+
* application-owned profile, where the CLI refuses to write). The manager
|
|
1672
|
+
* resolves the registry, runs pnpm with the launcher's bundled toolchain and
|
|
1673
|
+
* applies the bundle, then this reads the profile the same way the CLI path
|
|
1674
|
+
* does: the install is only `done` once the profile carries a dependency it
|
|
1675
|
+
* did not carry before, so a green manager call that added nothing is still
|
|
1676
|
+
* reported as a failure. Its resolved verdict is read first
|
|
1677
|
+
* ({@link nativeManagerFailure}): the manager folds a refused run into that
|
|
1678
|
+
* value instead of rejecting, and only the verdict names the reason. The
|
|
1679
|
+
* CLI-specific guards are deliberately absent — the official manager
|
|
1680
|
+
* validates and applies the bundle itself, exactly as it does for the
|
|
1681
|
+
* official Plugins page.
|
|
1682
|
+
* @param job - the install job being settled.
|
|
1683
|
+
* @param native - the official manager.
|
|
1684
|
+
*/
|
|
1685
|
+
async runNativeInstall(job, native) {
|
|
1686
|
+
const before = await this.capture();
|
|
1687
|
+
const refusal = nativeManagerFailure(await native.installBundle(job.spec, {
|
|
1688
|
+
enabled: true,
|
|
1689
|
+
requestId: job.id
|
|
1690
|
+
}));
|
|
1691
|
+
if (refusal !== void 0) {
|
|
1692
|
+
job.phase = "error";
|
|
1693
|
+
job.error = `plugin-manager: 官方插件管理器安装失败:${refusal}`;
|
|
1694
|
+
return;
|
|
1695
|
+
}
|
|
1696
|
+
const after = await this.capture();
|
|
1697
|
+
const name = this.newDependency(before, after);
|
|
1698
|
+
if (name === void 0) {
|
|
1699
|
+
job.phase = "error";
|
|
1700
|
+
job.error = "plugin-manager: 官方插件管理器报告成功,但 profile 未新增任何依赖(安装未生效)";
|
|
1701
|
+
return;
|
|
1702
|
+
}
|
|
1703
|
+
const manifest = await readProfileManifest(this.facts.packageJsonPath);
|
|
1704
|
+
job.plugin = await buildPluginRow(this.facts, name, manifest.dependencies[name] ?? job.spec, after.layer.rows);
|
|
1705
|
+
job.conflicts = significantChanges(diffLayer(before.layer, after.layer)).map((change) => ({
|
|
1706
|
+
id: change.id,
|
|
1707
|
+
name: change.id,
|
|
1708
|
+
from: change.from,
|
|
1709
|
+
to: change.to
|
|
1710
|
+
}));
|
|
1711
|
+
job.phase = "done";
|
|
1712
|
+
}
|
|
1603
1713
|
/** Start an in-place update through the official in-process manager. */
|
|
1604
1714
|
enqueueNativeUpdate(job, native) {
|
|
1605
1715
|
this.enqueue(async () => {
|
|
@@ -1619,6 +1729,10 @@ var CliGateway = class {
|
|
|
1619
1729
|
* this reads the profile the same way the CLI path does: the dependency must
|
|
1620
1730
|
* still be there and the installed version must be the one the route resolved,
|
|
1621
1731
|
* so a green manager call that changed nothing is still reported as a failure.
|
|
1732
|
+
* Its resolved verdict is read first ({@link nativeManagerFailure}): a refused
|
|
1733
|
+
* run (pnpm exited non-zero, an incompatible version, a cancellation) resolves
|
|
1734
|
+
* with `application: 'failed'`, and only that verdict carries the reason — the
|
|
1735
|
+
* profile re-read can only show that the version did not move.
|
|
1622
1736
|
* @param job - the update job being settled.
|
|
1623
1737
|
* @param native - the official manager.
|
|
1624
1738
|
*/
|
|
@@ -1631,10 +1745,15 @@ var CliGateway = class {
|
|
|
1631
1745
|
return;
|
|
1632
1746
|
}
|
|
1633
1747
|
const before = await this.capture();
|
|
1634
|
-
await native.installBundle(job.spec, {
|
|
1748
|
+
const refusal = nativeManagerFailure(await native.installBundle(job.spec, {
|
|
1635
1749
|
enabled: true,
|
|
1636
1750
|
requestId: job.id
|
|
1637
|
-
});
|
|
1751
|
+
}));
|
|
1752
|
+
if (refusal !== void 0) {
|
|
1753
|
+
job.phase = "error";
|
|
1754
|
+
job.error = `plugin-manager: 官方插件管理器更新失败:${refusal}`;
|
|
1755
|
+
return;
|
|
1756
|
+
}
|
|
1638
1757
|
const after = await this.capture();
|
|
1639
1758
|
if (!after.dependencies.includes(targetId)) {
|
|
1640
1759
|
job.phase = "error";
|
|
@@ -1690,7 +1809,11 @@ var CliGateway = class {
|
|
|
1690
1809
|
});
|
|
1691
1810
|
return { jobId: job.id };
|
|
1692
1811
|
}
|
|
1693
|
-
/**
|
|
1812
|
+
/**
|
|
1813
|
+
* Start a removal; the caller polls {@link status}. An application-owned
|
|
1814
|
+
* profile removes through the official in-process manager, the same writer
|
|
1815
|
+
* the install and update paths use there (the CLI refuses that profile).
|
|
1816
|
+
*/
|
|
1694
1817
|
remove(id) {
|
|
1695
1818
|
const job = {
|
|
1696
1819
|
id: `job-${++this.counter}`,
|
|
@@ -1706,6 +1829,11 @@ var CliGateway = class {
|
|
|
1706
1829
|
this.retainFinished(job.id);
|
|
1707
1830
|
return { jobId: job.id };
|
|
1708
1831
|
}
|
|
1832
|
+
const native = this.nativeManager();
|
|
1833
|
+
if (native !== void 0) {
|
|
1834
|
+
this.enqueueNativeRemove(job, native);
|
|
1835
|
+
return { jobId: job.id };
|
|
1836
|
+
}
|
|
1709
1837
|
this.enqueue(() => this.run(job, [
|
|
1710
1838
|
"plugin",
|
|
1711
1839
|
"--profile",
|
|
@@ -1715,6 +1843,51 @@ var CliGateway = class {
|
|
|
1715
1843
|
], REMOVE_TIMEOUT_MS));
|
|
1716
1844
|
return { jobId: job.id };
|
|
1717
1845
|
}
|
|
1846
|
+
/** Start a removal through the official in-process manager. */
|
|
1847
|
+
enqueueNativeRemove(job, native) {
|
|
1848
|
+
this.enqueue(async () => {
|
|
1849
|
+
try {
|
|
1850
|
+
await this.runNativeRemove(job, native);
|
|
1851
|
+
} catch (error) {
|
|
1852
|
+
job.phase = "error";
|
|
1853
|
+
job.error = `plugin-manager: 官方插件管理器卸载失败:${error instanceof Error ? error.message : String(error)}`;
|
|
1854
|
+
}
|
|
1855
|
+
this.retainFinished(job.id);
|
|
1856
|
+
});
|
|
1857
|
+
}
|
|
1858
|
+
/**
|
|
1859
|
+
* Run one removal through the official in-process manager (an
|
|
1860
|
+
* application-owned profile, where the CLI refuses to write). The removal is
|
|
1861
|
+
* only `done` once a dependency the profile carried before is gone, exactly
|
|
1862
|
+
* the verification the CLI path performs. Its resolved verdict is read first
|
|
1863
|
+
* ({@link nativeManagerFailure}): a refused removal resolves with
|
|
1864
|
+
* `application: 'failed'` rather than rejecting.
|
|
1865
|
+
* @param job - the removal job being settled.
|
|
1866
|
+
* @param native - the official manager.
|
|
1867
|
+
*/
|
|
1868
|
+
async runNativeRemove(job, native) {
|
|
1869
|
+
const before = await this.capture();
|
|
1870
|
+
const refusal = nativeManagerFailure(await native.removeBundle(job.spec));
|
|
1871
|
+
if (refusal !== void 0) {
|
|
1872
|
+
job.phase = "error";
|
|
1873
|
+
job.error = `plugin-manager: 官方插件管理器卸载失败:${refusal}`;
|
|
1874
|
+
return;
|
|
1875
|
+
}
|
|
1876
|
+
const after = await this.capture();
|
|
1877
|
+
if (before.dependencies.find((candidate) => !after.dependencies.includes(candidate)) === void 0) {
|
|
1878
|
+
job.phase = "error";
|
|
1879
|
+
job.error = "plugin-manager: 官方插件管理器报告成功,但依赖仍在 profile 中(卸载未生效)";
|
|
1880
|
+
return;
|
|
1881
|
+
}
|
|
1882
|
+
job.plugin = await this.rowFor("remove", job.spec, before, after);
|
|
1883
|
+
job.conflicts = significantChanges(diffLayer(before.layer, after.layer)).map((change) => ({
|
|
1884
|
+
id: change.id,
|
|
1885
|
+
name: change.id,
|
|
1886
|
+
from: change.from,
|
|
1887
|
+
to: change.to
|
|
1888
|
+
}));
|
|
1889
|
+
job.phase = "done";
|
|
1890
|
+
}
|
|
1718
1891
|
/**
|
|
1719
1892
|
* Read one job's current state (a shallow copy). Finished jobs beyond the
|
|
1720
1893
|
* ring cap are evicted and read as not-found, the same as an id that never
|
|
@@ -2254,7 +2427,9 @@ function meetsMinimumDsh(host, minimum) {
|
|
|
2254
2427
|
}
|
|
2255
2428
|
/**
|
|
2256
2429
|
* Read the declared DSH minimum from a published registry manifest:
|
|
2257
|
-
* `dsh.engines.dsh` first, top-level `engines.dsh`
|
|
2430
|
+
* `dsh.engines.dsh` first, top-level `engines.dsh` next, and
|
|
2431
|
+
* `peerDependencies["@deepseek-ai/dsh"]` last, which is where a package that
|
|
2432
|
+
* only declares the host as a peer still gets gated at boot.
|
|
2258
2433
|
* Defensive against malformed or untrusted metadata: anything that is not a
|
|
2259
2434
|
* non-empty string reads as absent.
|
|
2260
2435
|
* @param manifest - the decoded registry version manifest.
|
|
@@ -2263,7 +2438,8 @@ function meetsMinimumDsh(host, minimum) {
|
|
|
2263
2438
|
function dshRequirementOf(manifest) {
|
|
2264
2439
|
const dshEngines = manifest.dsh?.engines;
|
|
2265
2440
|
const engines = manifest.engines?.dsh;
|
|
2266
|
-
const
|
|
2441
|
+
const peers = manifest.peerDependencies?.["@deepseek-ai/dsh"];
|
|
2442
|
+
const value = dshEngines?.dsh ?? engines ?? peers;
|
|
2267
2443
|
return typeof value === "string" && value.trim() !== "" ? value.trim() : void 0;
|
|
2268
2444
|
}
|
|
2269
2445
|
//#endregion
|
|
@@ -2312,6 +2488,131 @@ function targetSpecForLegacy(sourceSpec, familyVersion) {
|
|
|
2312
2488
|
if (familyVersion === "") return void 0;
|
|
2313
2489
|
return `${CURRENT_AGGREGATE}@${familyVersion}`;
|
|
2314
2490
|
}
|
|
2491
|
+
/** How long the helper waits for this process to disappear before giving up on the wait. */
|
|
2492
|
+
const RESTART_WAIT_MS = 3e4;
|
|
2493
|
+
/** Exit/interpreter arguments a relaunch must not repeat (they re-bind fixed ports). */
|
|
2494
|
+
function isReusableExecArg(argument) {
|
|
2495
|
+
return !/^--(?:inspect|inspect-brk|inspect-port|debug|debug-brk)(?:=|$)/.test(argument);
|
|
2496
|
+
}
|
|
2497
|
+
/**
|
|
2498
|
+
* Decide how a restart request is carried out.
|
|
2499
|
+
* @param facts - the running process's launch facts.
|
|
2500
|
+
* @returns the planned mode, with the relaunch command when one is used.
|
|
2501
|
+
*/
|
|
2502
|
+
function planRestart(facts) {
|
|
2503
|
+
if (facts.desktop === true || facts.env.ELECTRON_RUN_AS_NODE === "1") return { mode: "shell" };
|
|
2504
|
+
if (!facts.interactive) return { mode: "manual" };
|
|
2505
|
+
const script = facts.argv[0];
|
|
2506
|
+
if (script === void 0 || script === "") return { mode: "manual" };
|
|
2507
|
+
return {
|
|
2508
|
+
mode: "relaunch",
|
|
2509
|
+
command: {
|
|
2510
|
+
execPath: facts.execPath,
|
|
2511
|
+
argv: [
|
|
2512
|
+
...(facts.execArgv ?? []).filter(isReusableExecArg),
|
|
2513
|
+
script,
|
|
2514
|
+
...facts.argv.slice(1)
|
|
2515
|
+
],
|
|
2516
|
+
cwd: facts.cwd,
|
|
2517
|
+
env: facts.env
|
|
2518
|
+
}
|
|
2519
|
+
};
|
|
2520
|
+
}
|
|
2521
|
+
/** Absolute path of the relaunch journal this plugin appends to. */
|
|
2522
|
+
function restartLogPath(home = dshHome()) {
|
|
2523
|
+
return join(home, "logs", "plugin-manager-restart.log");
|
|
2524
|
+
}
|
|
2525
|
+
/**
|
|
2526
|
+
* The detached helper. It is passed as `node -e <source> <job-json>`, so it
|
|
2527
|
+
* must stay dependency-free and single-file; the job carries no environment
|
|
2528
|
+
* (the child inherits this helper's own).
|
|
2529
|
+
*/
|
|
2530
|
+
const RELAUNCH_HELPER = `
|
|
2531
|
+
const fs = require('node:fs');
|
|
2532
|
+
const cp = require('node:child_process');
|
|
2533
|
+
const job = JSON.parse(process.argv[1]);
|
|
2534
|
+
fs.mkdirSync(job.logDir, { recursive: true });
|
|
2535
|
+
const log = (line) => { try { fs.appendFileSync(job.logPath, new Date().toISOString() + ' ' + line + '\\n'); } catch {} };
|
|
2536
|
+
const fd = fs.openSync(job.logPath, 'a');
|
|
2537
|
+
const alive = (pid) => { try { process.kill(pid, 0); return true; } catch { return false; } };
|
|
2538
|
+
const launch = (attempt) => {
|
|
2539
|
+
if (attempt > 0) log('relaunch attempt ' + (attempt + 1));
|
|
2540
|
+
const child = cp.spawn(job.execPath, job.argv, { cwd: job.cwd, env: process.env, detached: true, stdio: ['ignore', fd, fd] });
|
|
2541
|
+
child.unref();
|
|
2542
|
+
let code = null;
|
|
2543
|
+
child.on('exit', (value) => { code = value; });
|
|
2544
|
+
setTimeout(() => {
|
|
2545
|
+
if (code === null) { log('relaunch started (pid ' + child.pid + ')'); fs.closeSync(fd); return; }
|
|
2546
|
+
if (attempt < 4) { log('relaunch exited with ' + code + '; retrying'); launch(attempt + 1); return; }
|
|
2547
|
+
log('relaunch gave up after exit ' + code);
|
|
2548
|
+
fs.closeSync(fd);
|
|
2549
|
+
}, 3000);
|
|
2550
|
+
};
|
|
2551
|
+
const waitFor = (deadline) => {
|
|
2552
|
+
if (!alive(job.parentPid) || Date.now() >= deadline) { setTimeout(() => launch(0), 300); return; }
|
|
2553
|
+
setTimeout(() => waitFor(deadline), 200);
|
|
2554
|
+
};
|
|
2555
|
+
waitFor(Date.now() + job.waitMs);
|
|
2556
|
+
`;
|
|
2557
|
+
/**
|
|
2558
|
+
* Start the detached relaunch helper.
|
|
2559
|
+
* @param command - the command line the replacement runs.
|
|
2560
|
+
* @param logPath - journal the helper (and the replacement's output) appends to.
|
|
2561
|
+
* @returns true when a helper process was actually created.
|
|
2562
|
+
*/
|
|
2563
|
+
function startRelaunchHelper(command, logPath = restartLogPath()) {
|
|
2564
|
+
const job = {
|
|
2565
|
+
parentPid: process.pid,
|
|
2566
|
+
execPath: command.execPath,
|
|
2567
|
+
argv: command.argv,
|
|
2568
|
+
cwd: command.cwd,
|
|
2569
|
+
logDir: dirname(logPath),
|
|
2570
|
+
logPath,
|
|
2571
|
+
waitMs: RESTART_WAIT_MS
|
|
2572
|
+
};
|
|
2573
|
+
try {
|
|
2574
|
+
mkdirSync(dirname(logPath), { recursive: true });
|
|
2575
|
+
const helper = spawn(command.execPath, [
|
|
2576
|
+
"-e",
|
|
2577
|
+
RELAUNCH_HELPER,
|
|
2578
|
+
JSON.stringify(job)
|
|
2579
|
+
], {
|
|
2580
|
+
cwd: command.cwd,
|
|
2581
|
+
env: command.env,
|
|
2582
|
+
detached: true,
|
|
2583
|
+
stdio: "ignore"
|
|
2584
|
+
});
|
|
2585
|
+
helper.unref();
|
|
2586
|
+
return helper.pid !== void 0;
|
|
2587
|
+
} catch (error) {
|
|
2588
|
+
try {
|
|
2589
|
+
appendFileSync(logPath, `${(/* @__PURE__ */ new Date()).toISOString()} helper failed: ${String(error)}\n`);
|
|
2590
|
+
} catch {}
|
|
2591
|
+
return false;
|
|
2592
|
+
}
|
|
2593
|
+
}
|
|
2594
|
+
/**
|
|
2595
|
+
* Carry out one restart plan.
|
|
2596
|
+
* @param plan - the verdict from {@link planRestart}.
|
|
2597
|
+
* @param runtime - effect seams; the defaults spawn the helper and exit.
|
|
2598
|
+
* @returns the mode actually used: 'relaunch' degrades to 'manual' when the
|
|
2599
|
+
* helper could not be started, so a process that cannot be replaced stays up.
|
|
2600
|
+
*/
|
|
2601
|
+
function performRestart(plan, runtime = {}) {
|
|
2602
|
+
if (plan.mode === "manual") return "manual";
|
|
2603
|
+
const startHelper = runtime.startHelper ?? ((command) => startRelaunchHelper(command));
|
|
2604
|
+
const schedule = runtime.schedule ?? ((run, delayMs) => {
|
|
2605
|
+
setTimeout(run, delayMs);
|
|
2606
|
+
});
|
|
2607
|
+
const exit = runtime.exit ?? (() => {
|
|
2608
|
+
process.exit(0);
|
|
2609
|
+
});
|
|
2610
|
+
if (plan.mode === "relaunch") {
|
|
2611
|
+
if (plan.command === void 0 || !startHelper(plan.command)) return "manual";
|
|
2612
|
+
}
|
|
2613
|
+
schedule(exit, 400);
|
|
2614
|
+
return plan.mode;
|
|
2615
|
+
}
|
|
2315
2616
|
//#endregion
|
|
2316
2617
|
//#region src/host/routes.ts
|
|
2317
2618
|
/** Route prefix the browser half mirrors. */
|
|
@@ -2399,6 +2700,20 @@ function isDirectRegistrySpec(spec) {
|
|
|
2399
2700
|
return !/^(?:link:|file:|git:|github:|git\+|https?:\/\/|npm:|workspace:|catalog:)/.test(spec);
|
|
2400
2701
|
}
|
|
2401
2702
|
/**
|
|
2703
|
+
* The package name of a spec that names no version, or undefined when the
|
|
2704
|
+
* caller already chose one.
|
|
2705
|
+
*
|
|
2706
|
+
* A scoped name's `@` opens the scope, so only a `@` after the scope counts as
|
|
2707
|
+
* a version separator; every other separator form (`name@1.2.3`, `name@next`,
|
|
2708
|
+
* `name@^1`) yields undefined and is passed through untouched.
|
|
2709
|
+
* @param spec - the install spec the caller supplied.
|
|
2710
|
+
* @returns the bare package name when the spec is unversioned.
|
|
2711
|
+
*/
|
|
2712
|
+
function barePackageName(spec) {
|
|
2713
|
+
if (spec === "") return void 0;
|
|
2714
|
+
return (spec.startsWith("@") ? spec.indexOf("@", 1) : spec.indexOf("@")) < 0 ? spec : void 0;
|
|
2715
|
+
}
|
|
2716
|
+
/**
|
|
2402
2717
|
* Build the gateway routes.
|
|
2403
2718
|
* @param deps - profile facts, the CLI gateway, and seams.
|
|
2404
2719
|
* @returns the web-server routes to register.
|
|
@@ -2480,11 +2795,25 @@ function makeGatewayRoutes(deps) {
|
|
|
2480
2795
|
writeJson(res, 400, { error: unsafeSpec });
|
|
2481
2796
|
return;
|
|
2482
2797
|
}
|
|
2483
|
-
if (!deps.cliAvailable()) {
|
|
2798
|
+
if (!deps.cliAvailable() && !gateway.usesNativeWriter()) {
|
|
2484
2799
|
writeJson(res, 500, { error: "plugin-manager: dsh CLI not found on PATH" });
|
|
2485
2800
|
return;
|
|
2486
2801
|
}
|
|
2487
|
-
|
|
2802
|
+
let installSpec = spec.trim();
|
|
2803
|
+
const name = barePackageName(installSpec);
|
|
2804
|
+
if (name !== void 0 && isDirectRegistrySpec(installSpec)) {
|
|
2805
|
+
const manifest = await fetchManifest(name).catch(() => void 0);
|
|
2806
|
+
if (manifest?.version !== void 0 && manifest.version !== "") {
|
|
2807
|
+
const pinned = `${name}@${manifest.version}`;
|
|
2808
|
+
const unsafePinned = unsafeSpecReason(pinned);
|
|
2809
|
+
if (unsafePinned !== void 0) {
|
|
2810
|
+
writeJson(res, 400, { error: unsafePinned });
|
|
2811
|
+
return;
|
|
2812
|
+
}
|
|
2813
|
+
installSpec = pinned;
|
|
2814
|
+
}
|
|
2815
|
+
}
|
|
2816
|
+
writeJson(res, 200, gateway.install(installSpec));
|
|
2488
2817
|
};
|
|
2489
2818
|
const updateHandler = async (req, res) => {
|
|
2490
2819
|
const id = (await readJsonBody(req, {
|
|
@@ -2501,7 +2830,7 @@ function makeGatewayRoutes(deps) {
|
|
|
2501
2830
|
writeJson(res, 400, { error: unsafe });
|
|
2502
2831
|
return;
|
|
2503
2832
|
}
|
|
2504
|
-
if (!deps.cliAvailable()) {
|
|
2833
|
+
if (!deps.cliAvailable() && !gateway.usesNativeWriter()) {
|
|
2505
2834
|
writeJson(res, 500, { error: "plugin-manager: dsh CLI not found on PATH" });
|
|
2506
2835
|
return;
|
|
2507
2836
|
}
|
|
@@ -2598,7 +2927,7 @@ function makeGatewayRoutes(deps) {
|
|
|
2598
2927
|
writeJson(res, 400, { error: unsafeId });
|
|
2599
2928
|
return;
|
|
2600
2929
|
}
|
|
2601
|
-
if (!deps.cliAvailable()) {
|
|
2930
|
+
if (!deps.cliAvailable() && !gateway.usesNativeWriter()) {
|
|
2602
2931
|
writeJson(res, 500, { error: "plugin-manager: dsh CLI not found on PATH" });
|
|
2603
2932
|
return;
|
|
2604
2933
|
}
|
|
@@ -2722,6 +3051,38 @@ function makeGatewayRoutes(deps) {
|
|
|
2722
3051
|
}
|
|
2723
3052
|
writeJson(res, 200, { updates });
|
|
2724
3053
|
};
|
|
3054
|
+
/**
|
|
3055
|
+
* The running process's own launch facts, read once per request: the desktop
|
|
3056
|
+
* verdict comes from the profile facts (packaged Desktop launcher), the rest
|
|
3057
|
+
* from this process.
|
|
3058
|
+
*/
|
|
3059
|
+
const liveRestartFacts = () => ({
|
|
3060
|
+
desktop: facts.desktop,
|
|
3061
|
+
env: process.env,
|
|
3062
|
+
execPath: process.execPath,
|
|
3063
|
+
argv: process.argv.slice(1),
|
|
3064
|
+
execArgv: process.execArgv,
|
|
3065
|
+
cwd: process.cwd(),
|
|
3066
|
+
interactive: process.stdin.isTTY === true || process.stdout.isTTY === true
|
|
3067
|
+
});
|
|
3068
|
+
/**
|
|
3069
|
+
* Restart the host so an applied plugin update is loaded. The route answers
|
|
3070
|
+
* with the mode it actually used (see host/restart.ts): 'relaunch' when this
|
|
3071
|
+
* process re-executes itself, 'shell' when the packaged Desktop shell owns
|
|
3072
|
+
* the process tree and runs its own restart, 'manual' when neither is safe.
|
|
3073
|
+
*/
|
|
3074
|
+
const restartHandler = async (req, res) => {
|
|
3075
|
+
const plan = planRestart((deps.restartFacts ?? liveRestartFacts)());
|
|
3076
|
+
if (req.method === "GET" || req.method === "HEAD") {
|
|
3077
|
+
writeJson(res, 200, { restart: { mode: plan.mode } });
|
|
3078
|
+
return;
|
|
3079
|
+
}
|
|
3080
|
+
if (req.method !== "POST") {
|
|
3081
|
+
writeJson(res, 405, { error: "plugin-manager: restart needs GET (plan) or POST (execute)" });
|
|
3082
|
+
return;
|
|
3083
|
+
}
|
|
3084
|
+
writeJson(res, 202, { restart: { mode: performRestart(plan, deps.restartRuntime ?? {}) } });
|
|
3085
|
+
};
|
|
2725
3086
|
return [
|
|
2726
3087
|
{
|
|
2727
3088
|
kind: "exact",
|
|
@@ -2767,13 +3128,23 @@ function makeGatewayRoutes(deps) {
|
|
|
2767
3128
|
kind: "exact",
|
|
2768
3129
|
path: `${GATEWAY_PREFIX}/check-updates`,
|
|
2769
3130
|
handler: guard(checkUpdatesHandler)
|
|
3131
|
+
},
|
|
3132
|
+
{
|
|
3133
|
+
kind: "exact",
|
|
3134
|
+
path: `${GATEWAY_PREFIX}/restart`,
|
|
3135
|
+
handler: guard(restartHandler)
|
|
2770
3136
|
}
|
|
2771
3137
|
];
|
|
2772
3138
|
}
|
|
2773
3139
|
//#endregion
|
|
2774
3140
|
//#region src/index.ts
|
|
2775
|
-
/**
|
|
2776
|
-
|
|
3141
|
+
/**
|
|
3142
|
+
* Stable cordis plugin name (matches this package's own cordis.patch.yml
|
|
3143
|
+
* insert id). It must not repeat an id another bundle already claims: the
|
|
3144
|
+
* loader keeps one entry per id, so a shared id would also give two host
|
|
3145
|
+
* halves one cordis plugin name.
|
|
3146
|
+
*/
|
|
3147
|
+
const name = "ui-plugin-manager-update-check";
|
|
2777
3148
|
/** Services the gateway needs — the web server seam. */
|
|
2778
3149
|
const inject = ["webServer"];
|
|
2779
3150
|
/** Apply the host half (once per process). */
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The list-level toolbar: check every installed third-party plugin for a newer
|
|
3
|
+
* registry release, apply them in one run, then restart DSH to load them.
|
|
4
|
+
*
|
|
5
|
+
* It exists because the official Plugins page compares one package at a time
|
|
6
|
+
* (its `plugins.detail.section` contributions render on a bundle's page), while
|
|
7
|
+
* the answer a user wants when they open the page is "is anything of mine out
|
|
8
|
+
* of date, and can I just fix it". It is mounted beside the page's "Installed"
|
|
9
|
+
* heading (see plugin-toolbar-mount.tsx) because that heading is the page's own
|
|
10
|
+
* chrome and the page declares no seat next to it.
|
|
11
|
+
*
|
|
12
|
+
* Policy lives in core/updates.ts (third-party only, compatibility-gated) and
|
|
13
|
+
* the operations come from the injected face, so this component is pure
|
|
14
|
+
* rendering plus the sequence of calls: nothing here touches the DOM beyond its
|
|
15
|
+
* own subtree.
|
|
16
|
+
* @module @linxin666/dsh-client-ui-plugin-manager/client
|
|
17
|
+
*/
|
|
18
|
+
import type { PropsLocale } from '@deepseek-ai/dsh-client-ui-slots';
|
|
19
|
+
import type { PluginUpdateItem, RestartMode } from '../core/protocol.ts';
|
|
20
|
+
/** The operations the toolbar drives (the package's dual-channel face). */
|
|
21
|
+
export interface PluginListToolbarInjected {
|
|
22
|
+
/** Whether this browser has loopback authority to use the host routes. */
|
|
23
|
+
isLoopback: boolean;
|
|
24
|
+
/** Compare every installed plugin against the version its source serves. */
|
|
25
|
+
checkUpdates: () => Promise<PluginUpdateItem[]>;
|
|
26
|
+
/** Re-install one plugin from its recorded source. */
|
|
27
|
+
update: (id: string) => Promise<unknown>;
|
|
28
|
+
/**
|
|
29
|
+
* Read how a restart would be carried out, without carrying it out: the
|
|
30
|
+
* confirmation must name the same consequence the host will produce (an
|
|
31
|
+
* in-place relaunch, the packaged Desktop shell's own recovery dialog, or a
|
|
32
|
+
* manual restart), and a plan read has no side effects.
|
|
33
|
+
*/
|
|
34
|
+
restartPlan: () => Promise<RestartMode>;
|
|
35
|
+
/** Restart the host so an applied update is loaded. */
|
|
36
|
+
restart: () => Promise<RestartMode>;
|
|
37
|
+
}
|
|
38
|
+
/** Full component props. */
|
|
39
|
+
export type PluginListToolbarProps = PropsLocale<'settings.pluginManager'> & PluginListToolbarInjected;
|
|
40
|
+
/**
|
|
41
|
+
* The toolbar itself. All state is local: the page around it owns the plugin
|
|
42
|
+
* list, and a restart tears this component down anyway.
|
|
43
|
+
*/
|
|
44
|
+
export declare function PluginListToolbar(props: PluginListToolbarProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One `document.body` MutationObserver per page, shared by every family
|
|
3
|
+
* plugin bundle.
|
|
4
|
+
*
|
|
5
|
+
* Family plugins that inject at the DOM level must notice when the shell
|
|
6
|
+
* re-renders around their injected node: the aggregate shell's column shims
|
|
7
|
+
* and any plugin surface still extending the shell from outside the official
|
|
8
|
+
* slot seats. Each consumer used to install its OWN `MutationObserver` on
|
|
9
|
+
* `document.body` with
|
|
10
|
+
* `{ childList: true, subtree: true }`, so a page carrying N family plugins
|
|
11
|
+
* paid N native observers and N callback invocations for EVERY mutation batch
|
|
12
|
+
* the app produced — chat token streaming alone produces many per second, and
|
|
13
|
+
* the count grows with the number of installed plugins.
|
|
14
|
+
*
|
|
15
|
+
* This module keeps exactly ONE body observer per page. The registry lives on
|
|
16
|
+
* `globalThis` under a `Symbol.for` key, so the per-package generated copies
|
|
17
|
+
* of this file (scripts/sync-shared.mjs) and separately bundled plugins all
|
|
18
|
+
* reach the same hub at runtime instead of one hub per module instance.
|
|
19
|
+
* Record subscribers receive the mutations accumulated since the last flush.
|
|
20
|
+
* Consumers that only re-check their DOM use subscribeBodyInvalidations:
|
|
21
|
+
* when all subscribers use that path, the hub retains no mutation records
|
|
22
|
+
* (or detached subtrees) while a background page's animation frames pause.
|
|
23
|
+
* Both paths run at most once per frame. The last subscriber disconnects the
|
|
24
|
+
* observer, cancels the pending frame and releases its records.
|
|
25
|
+
*
|
|
26
|
+
* Failure policy: without a DOM or `MutationObserver` the subscription is a
|
|
27
|
+
* no-op disposer (the same silence the per-consumer observers had), and when
|
|
28
|
+
* `requestAnimationFrame` is unavailable the flush runs synchronously so a
|
|
29
|
+
* subscriber is never silently dropped. A throwing subscriber cannot stop the
|
|
30
|
+
* others.
|
|
31
|
+
*/
|
|
32
|
+
/**
|
|
33
|
+
* Subscribe to a coalesced DOM re-check without retaining mutation records.
|
|
34
|
+
* The marked wrapper also works with an older hub, which delivers records
|
|
35
|
+
* that it simply ignores until a page reload picks up the updated hub.
|
|
36
|
+
*/
|
|
37
|
+
export declare function subscribeBodyInvalidations(subscriber: () => void): () => void;
|
|
38
|
+
/**
|
|
39
|
+
* Subscribe to body-level childList mutations.
|
|
40
|
+
* @param subscriber - called at most once per animation frame with the records
|
|
41
|
+
* collected since the previous flush; must be safe to run repeatedly.
|
|
42
|
+
* @returns the disposer removing this subscriber (and the observer when it was
|
|
43
|
+
* the last one).
|
|
44
|
+
*/
|
|
45
|
+
export declare function subscribeBodyMutations(subscriber: (records: MutationRecord[]) => void): () => void;
|