@karoy/dsh-wsl-desktop 0.0.0-stage → 0.1.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.
- package/README.md +78 -2
- package/bin/auto.mjs +124 -0
- package/client.js +147 -0
- package/cordis.patch.yml +19 -0
- package/index.js +211 -0
- package/lib/launch.js +45 -0
- package/lib/merge-patch.js +95 -0
- package/lib/setup.js +289 -0
- package/lib/state.js +79 -0
- package/lib/sync.js +207 -0
- package/lib/wsl-remote.cjs +103 -0
- package/package.json +64 -3
package/README.md
CHANGED
|
@@ -1,3 +1,79 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @karoy/dsh-wsl-desktop
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
让 **DSH Desktop 窗口本身**跑在 WSL 里的 DSH Host 上(proxy 模式,保留「应用/编辑」外壳),
|
|
4
|
+
或让主窗口直接加载 WSL Host 页面(direct 模式)。全程只动**副本**,原安装不修改。
|
|
5
|
+
|
|
6
|
+
## 使用
|
|
7
|
+
|
|
8
|
+
安装后(一次),之后**完全自动 + 按需启动**:
|
|
9
|
+
|
|
10
|
+
- **程序启动时**:只做环境准备——
|
|
11
|
+
1. 副本缺失 / 模式变了 / Desktop 升级 → 重建副本(robocopy → 解包 asar → 打 proxy 补丁);
|
|
12
|
+
2. **仅在首次初始化**时把 Windows desktop profile 的插件状态完整镜像到 WSL:
|
|
13
|
+
启用的 → 安装并启用;禁用的 → 安装但保持禁用;配置行(id/name/enabled/config)同步;
|
|
14
|
+
3. **不启动应用**。
|
|
15
|
+
- **点左下角「从WSL打开」**:启动 proxy 版桌面窗口(环境已就绪,秒级)。
|
|
16
|
+
- **副本已在 WSL 模式运行时**:入口变为不可点击的「WSL模式」(依据副本启动时写入的 `.dsh-wsl-running.json` 标记 + 进程存活、24h 内判定,而不是只看进程名)。
|
|
17
|
+
- 之后每次启动都不再同步,**WSL 侧手动启停的插件一直保持**。
|
|
18
|
+
|
|
19
|
+
命令只作兜底:
|
|
20
|
+
|
|
21
|
+
| 命令 | 作用 |
|
|
22
|
+
| --- | --- |
|
|
23
|
+
| `/wsl-desktop-status` | 副本状态、模式、上次准备/启动结果 |
|
|
24
|
+
| `/wsl-desktop-launch` | 立即启动副本 |
|
|
25
|
+
| `/wsl-desktop-sync` | 手动重跑一次完整镜像(启用+禁用+配置) |
|
|
26
|
+
| `/wsl-desktop-setup` | 手动重建副本 |
|
|
27
|
+
| `/wsl-desktop-remove` | 删除副本与独立 Electron profile |
|
|
28
|
+
|
|
29
|
+
## 配置(profile patch 的 `config`,留空按默认)
|
|
30
|
+
|
|
31
|
+
- `mode`: `"proxy"`(默认,保留桌面外壳)|`"direct"`(主窗口直接加载 WSL 页面)
|
|
32
|
+
- `installDir`:可选覆盖;默认 `%LOCALAPPDATA%\DeepSeek Harness WSL Desktop`(不写死盘符)
|
|
33
|
+
- `userDataDirName`(默认 `DeepSeek Harness WSL`)
|
|
34
|
+
- `port` / `distro` / `autoInstall`:WSL Host 参数(默认 3080 / 默认发行版 / 自动装 runtime)
|
|
35
|
+
- `autoRun` / `autoSetup` / `autoLaunch` / `syncWslPlugins` / `syncLinked` / `autoDelayMs`
|
|
36
|
+
(默认 `true / true / false / true / false / 4000`:自动准备、**不自动启动**)
|
|
37
|
+
|
|
38
|
+
## 原理
|
|
39
|
+
|
|
40
|
+
1. `robocopy` 复制整个安装目录到 `installDir`,副本里解包 `app.asar`(原 asar 备份 `.orig`);
|
|
41
|
+
2. 补丁 `lib/main.js`(带 `DSH_WSL_DESKTOP` 标记、可重复):
|
|
42
|
+
- proxy:`host.start()` 后把 `ready.url` 换成 WSL Host 的 token URL;认证后拉取 WSL Host
|
|
43
|
+
的 `/` HTML,解析其 index 注入表(插件图、`rev`、全局配置)替换 `ready.injections`,
|
|
44
|
+
否则桌面壳的 `/plugins/??…&rev=…` 与 WSL rev 对不上会 404;
|
|
45
|
+
- direct:主窗口 `navigateMain` 直接指 WSL URL;
|
|
46
|
+
3. 写入 `lib/wsl-remote.cjs`:WSL 内装 runtime、启动/复用 Host、读出 `dsh web:` URL;
|
|
47
|
+
4. 独立 `DSH_HOME` + 独立 userData,与主应用完全隔离;
|
|
48
|
+
5. helper 通过隐藏 spawn(`CREATE_NO_WINDOW`,无黑框)后台执行。
|
|
49
|
+
|
|
50
|
+
## 首次初始化同步的内容
|
|
51
|
+
|
|
52
|
+
以 Windows desktop profile 为源:
|
|
53
|
+
|
|
54
|
+
| Windows 状态 | WSL 处理 |
|
|
55
|
+
|---|---|
|
|
56
|
+
| 启用的插件 | `dsh plugin add` 安装 + 进 WSL bundles(启用),配置行合并 |
|
|
57
|
+
| 禁用的插件 | 同样安装依赖,但**不进 bundles**,配置行 `enabled: false` |
|
|
58
|
+
| link 插件 | 默认跳过(`syncLinked: true` 时按 `/mnt/<盘>/…` 同步) |
|
|
59
|
+
|
|
60
|
+
同步完成后写 `.dsh-wsl-initialized` 戳;之后 WSL 侧有最终决定权(手动启停不受影响)。
|
|
61
|
+
|
|
62
|
+
## 限制
|
|
63
|
+
|
|
64
|
+
- proxy 模式的工作区选择等目录操作走 WSL Host 的 browse 选择器(外壳的原生目录对话框
|
|
65
|
+
未接到 WSL 侧);「应用/编辑」菜单与窗口外壳保留。
|
|
66
|
+
- 会话、凭据、工作区都是 WSL 自己的;WSL 的 `~/.dsh` 与 Windows 的互不相干。
|
|
67
|
+
- 应用升级会覆盖副本补丁;升级后下次启动自动重建副本(不重同步),可 `/wsl-desktop-sync`。
|
|
68
|
+
|
|
69
|
+
## 发布
|
|
70
|
+
|
|
71
|
+
仓库的 `NPM_TOKEN` 具备直接发布权限:
|
|
72
|
+
|
|
73
|
+
1. 改 `package.json` 的 `version`,提交并 push;
|
|
74
|
+
2. 在 GitHub 创建 **Release**(发布)→ `Publish to npm` 工作流先跑 CI,再执行
|
|
75
|
+
`npm publish --access public`;
|
|
76
|
+
3. 验证:`npm view @karoy/dsh-wsl-desktop version`(当前 `0.1.1`)。
|
|
77
|
+
## 回滚
|
|
78
|
+
|
|
79
|
+
`/wsl-desktop-remove`,或关闭窗口后删除 `installDir` 与 `%APPDATA%\DeepSeek Harness WSL`。
|
package/bin/auto.mjs
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detached helper for the WSL Desktop copy. The Host plugin spawns this script
|
|
3
|
+
* so heavy work (1 GB copy, asar extraction, pnpm installs) never blocks the
|
|
4
|
+
* running DSH window.
|
|
5
|
+
*
|
|
6
|
+
* - First initialization (no `.dsh-wsl-initialized` stamp): build the copy and
|
|
7
|
+
* sync the enabled Windows plugins into the WSL web profile once.
|
|
8
|
+
* - Everything after: the environment is left alone — clicking the footer entry
|
|
9
|
+
* only launches the copy.
|
|
10
|
+
*
|
|
11
|
+
* @module @karoy/dsh-wsl-desktop/auto
|
|
12
|
+
*/
|
|
13
|
+
import fs from 'node:fs'
|
|
14
|
+
import os from 'node:os'
|
|
15
|
+
import path from 'node:path'
|
|
16
|
+
import { resolveConfig, setup } from '../lib/setup.js'
|
|
17
|
+
import { initializedFile, isInitialized, markInitialized, needsBuild, writeStatus } from '../lib/state.js'
|
|
18
|
+
import { spawnCopy } from '../lib/launch.js'
|
|
19
|
+
import { isCopyRunning, removePlugins, restartAndVerify, stopWslHost, syncPlugins } from '../lib/sync.js'
|
|
20
|
+
|
|
21
|
+
const LOCK_PATH = path.join(os.tmpdir(), 'dsh-wsl-desktop.lock')
|
|
22
|
+
|
|
23
|
+
/** Single-flight: only one helper may prepare/launch at a time. */
|
|
24
|
+
function acquireLock(now = Date.now()) {
|
|
25
|
+
try {
|
|
26
|
+
if (fs.existsSync(LOCK_PATH)) {
|
|
27
|
+
const prev = JSON.parse(fs.readFileSync(LOCK_PATH, 'utf8'))
|
|
28
|
+
if (prev && Number.isFinite(prev.pid) && now - (prev.at ?? 0) < 10 * 60 * 1000) {
|
|
29
|
+
try {
|
|
30
|
+
process.kill(prev.pid, 0)
|
|
31
|
+
return false
|
|
32
|
+
} catch {
|
|
33
|
+
/* stale lock */
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
fs.writeFileSync(LOCK_PATH, JSON.stringify({ pid: process.pid, at: now }))
|
|
38
|
+
return true
|
|
39
|
+
} catch {
|
|
40
|
+
return true
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function releaseLock() {
|
|
45
|
+
try {
|
|
46
|
+
if (JSON.parse(fs.readFileSync(LOCK_PATH, 'utf8')).pid === process.pid) fs.unlinkSync(LOCK_PATH)
|
|
47
|
+
} catch {
|
|
48
|
+
/* ignore */
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function readConfig() {
|
|
53
|
+
const source = process.argv[2]
|
|
54
|
+
? fs.readFileSync(process.argv[2], 'utf8').replace(/^\uFEFF/, '')
|
|
55
|
+
: process.env.DSH_WSL_AUTO_CONFIG_FILE
|
|
56
|
+
? fs.readFileSync(process.env.DSH_WSL_AUTO_CONFIG_FILE, 'utf8').replace(/^\uFEFF/, '')
|
|
57
|
+
: (process.env.DSH_WSL_AUTO_CONFIG ?? '{}')
|
|
58
|
+
return resolveConfig(JSON.parse(source))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function needsSetup(installDir) {
|
|
62
|
+
return !fs.existsSync(initializedFile(installDir))
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const config = readConfig()
|
|
66
|
+
const steps = []
|
|
67
|
+
|
|
68
|
+
while (!acquireLock()) {
|
|
69
|
+
const sleep = new Int32Array(new SharedArrayBuffer(4))
|
|
70
|
+
Atomics.wait(sleep, 0, 0, 3000)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
const running = isCopyRunning(config)
|
|
75
|
+
if (running) steps.push('copy already running')
|
|
76
|
+
const firstInit = needsSetup(config.installDir)
|
|
77
|
+
|
|
78
|
+
const rebuild = config.autoSetup !== false && needsBuild(config)
|
|
79
|
+
if (rebuild && running) {
|
|
80
|
+
steps.push('rebuild deferred: copy is running (close it, or use /wsl-desktop-setup)')
|
|
81
|
+
} else if (rebuild) {
|
|
82
|
+
steps.push('build: ' + config.installDir)
|
|
83
|
+
setup(config)
|
|
84
|
+
steps.push('build done')
|
|
85
|
+
} else {
|
|
86
|
+
steps.push('copy up to date')
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (firstInit && !running) {
|
|
90
|
+
steps.push('first-time sync (runs once)')
|
|
91
|
+
const result = syncPlugins(config)
|
|
92
|
+
steps.push('WSL plugins: +' + result.added.length + ' -' + result.removed.length + ' (failed ' + result.failed.length + ', skipped ' + result.skipped.length + ')')
|
|
93
|
+
if (result.added.length > 0 || result.removed.length > 0 || result.bundlesUpdated || result.bundlesDeactivated || result.configsMerged) {
|
|
94
|
+
if (restartAndVerify()) {
|
|
95
|
+
steps.push('WSL Host reloaded with the synced plugins')
|
|
96
|
+
} else {
|
|
97
|
+
removePlugins(result.added)
|
|
98
|
+
stopWslHost()
|
|
99
|
+
steps.push('WSL Host failed to boot with the synced plugins; rolled back ' + String(result.added.length))
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
markInitialized(config.installDir)
|
|
103
|
+
} else if (firstInit && running) {
|
|
104
|
+
steps.push('first-time sync deferred: copy is running')
|
|
105
|
+
} else {
|
|
106
|
+
steps.push('environment already initialized (no sync)')
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (config.autoLaunch !== false && !running) {
|
|
110
|
+
const pid = spawnCopy(config)
|
|
111
|
+
steps.push('launched pid=' + String(pid))
|
|
112
|
+
} else if (config.autoLaunch !== false && running) {
|
|
113
|
+
steps.push('launch skipped: copy is running')
|
|
114
|
+
} else {
|
|
115
|
+
steps.push('copy not started (start it from the button or /wsl-desktop-launch)')
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
writeStatus(config.installDir, { ok: true, steps })
|
|
119
|
+
} catch (error) {
|
|
120
|
+
writeStatus(config.installDir, { ok: false, steps, error: error instanceof Error ? error.message : String(error) })
|
|
121
|
+
process.exitCode = 1
|
|
122
|
+
} finally {
|
|
123
|
+
releaseLock()
|
|
124
|
+
}
|
package/client.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client half: a bottom-left footer entry (above Settings) that asks the Host to
|
|
3
|
+
* build/refresh the WSL Desktop proxy copy and launch it.
|
|
4
|
+
*
|
|
5
|
+
* Once the copy reports it is actually bound to the WSL Host (marker written by
|
|
6
|
+
* its patched main.js), the entry shows the disabled "WSL模式" label. While the
|
|
7
|
+
* copy is still starting it shows "启动中…" and stays disabled.
|
|
8
|
+
*
|
|
9
|
+
* @module @karoy/dsh-wsl-desktop/client
|
|
10
|
+
*/
|
|
11
|
+
window.__ModuleLoader__.load({
|
|
12
|
+
id: '@karoy/dsh-wsl-desktop',
|
|
13
|
+
factory(require) {
|
|
14
|
+
const React = require('react')
|
|
15
|
+
const h = React.createElement
|
|
16
|
+
|
|
17
|
+
const CSS = `
|
|
18
|
+
.dsh-wsl-desktop-footer{display:inline-flex;align-items:center;justify-content:center;gap:8px;height:32px;width:100%;padding:0;
|
|
19
|
+
border:none;border-radius:8px;background:transparent;color:inherit;font:inherit;font-size:12px;line-height:1;
|
|
20
|
+
cursor:pointer;opacity:.85;white-space:nowrap;transition:opacity .12s,background .12s}
|
|
21
|
+
.dsh-wsl-desktop-footer[data-wide=true]{justify-content:flex-start;padding:0 10px}
|
|
22
|
+
.dsh-wsl-desktop-footer:hover{opacity:1;background:color-mix(in srgb,currentColor 10%,transparent)}
|
|
23
|
+
.dsh-wsl-desktop-footer[data-state=mode]{opacity:.6;cursor:default;background:transparent}
|
|
24
|
+
.dsh-wsl-desktop-footer[data-state=starting]{opacity:.6;cursor:default;background:transparent}
|
|
25
|
+
.dsh-wsl-desktop-footer:focus-visible{outline:2px solid color-mix(in srgb,currentColor 45%,transparent);outline-offset:2px}
|
|
26
|
+
.dsh-wsl-desktop-footer:disabled{opacity:.45;cursor:default}
|
|
27
|
+
.dsh-wsl-desktop-footer[data-state=error]{color:#e5534b}
|
|
28
|
+
`
|
|
29
|
+
|
|
30
|
+
function DesktopIcon({ size = 15 }) {
|
|
31
|
+
return h('svg', {
|
|
32
|
+
viewBox: '0 0 16 16', width: size, height: size, fill: 'none', stroke: 'currentColor',
|
|
33
|
+
strokeWidth: 1.4, strokeLinecap: 'round', strokeLinejoin: 'round', 'aria-hidden': true,
|
|
34
|
+
},
|
|
35
|
+
h('rect', { x: 1.6, y: 2.4, width: 12.8, height: 8.6, rx: 1.4 }),
|
|
36
|
+
h('path', { d: 'M5.6 13.6h4.8M8 11v2.6' }),
|
|
37
|
+
h('path', { d: 'M4.4 5.2l1.8 1.5-1.8 1.5M7.8 8.2h2.6' }))
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Host state: `running` = copy process alive, `wslMode` = it proved WSL binding. */
|
|
41
|
+
async function readState() {
|
|
42
|
+
const res = await fetch('/wsl-desktop/state', { headers: { accept: 'application/json' } })
|
|
43
|
+
const data = await res.json().catch(() => null)
|
|
44
|
+
return {
|
|
45
|
+
running: data?.ok === true && data.running === true,
|
|
46
|
+
wslMode: data?.ok === true && data.wslMode === true,
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function openDesktop() {
|
|
51
|
+
const res = await fetch('/wsl-desktop/open', {
|
|
52
|
+
method: 'POST',
|
|
53
|
+
headers: { accept: 'application/json', 'content-type': 'application/json' },
|
|
54
|
+
body: '{}',
|
|
55
|
+
})
|
|
56
|
+
let data
|
|
57
|
+
try {
|
|
58
|
+
data = await res.json()
|
|
59
|
+
} catch {
|
|
60
|
+
data = null
|
|
61
|
+
}
|
|
62
|
+
if (data?.ok !== true) throw new Error(data?.error || `request failed (${res.status})`)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function WslDesktopFooter({ wide }) {
|
|
66
|
+
const [state, setState] = React.useState('idle')
|
|
67
|
+
const [detail, setDetail] = React.useState('')
|
|
68
|
+
const [copy, setCopy] = React.useState({ running: false, wslMode: false })
|
|
69
|
+
|
|
70
|
+
const reveal = () => {
|
|
71
|
+
readState().then(setCopy, () => setCopy({ running: false, wslMode: false }))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
React.useEffect(() => {
|
|
75
|
+
reveal()
|
|
76
|
+
const timer = setInterval(reveal, 10000)
|
|
77
|
+
const onFocus = () => reveal()
|
|
78
|
+
window.addEventListener('focus', onFocus)
|
|
79
|
+
return () => {
|
|
80
|
+
clearInterval(timer)
|
|
81
|
+
window.removeEventListener('focus', onFocus)
|
|
82
|
+
}
|
|
83
|
+
}, [])
|
|
84
|
+
|
|
85
|
+
const busy = state === 'busy' || copy.running
|
|
86
|
+
const onClick = () => {
|
|
87
|
+
if (busy) return
|
|
88
|
+
setState('busy')
|
|
89
|
+
setDetail('')
|
|
90
|
+
openDesktop().then(
|
|
91
|
+
() => {
|
|
92
|
+
setState('idle')
|
|
93
|
+
setTimeout(reveal, 1500)
|
|
94
|
+
},
|
|
95
|
+
(error) => {
|
|
96
|
+
console.warn('[wsl-desktop]', error)
|
|
97
|
+
setDetail(error instanceof Error ? error.message : String(error))
|
|
98
|
+
setState('error')
|
|
99
|
+
setTimeout(() => setState('idle'), 6000)
|
|
100
|
+
},
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const label = copy.wslMode
|
|
105
|
+
? 'WSL模式'
|
|
106
|
+
: copy.running
|
|
107
|
+
? '启动中…'
|
|
108
|
+
: state === 'busy'
|
|
109
|
+
? '准备中'
|
|
110
|
+
: state === 'error'
|
|
111
|
+
? '启动失败'
|
|
112
|
+
: '从WSL打开'
|
|
113
|
+
const title = copy.wslMode
|
|
114
|
+
? '当前 WSL 桌面已在运行(WSL 模式)'
|
|
115
|
+
: copy.running
|
|
116
|
+
? 'WSL 桌面正在启动…'
|
|
117
|
+
: state === 'error'
|
|
118
|
+
? `启动失败:${detail}`
|
|
119
|
+
: '在 WSL 中打开 DSH 桌面窗口(构建/同步 WSL Host 并启动副本)'
|
|
120
|
+
const buttonState = copy.wslMode ? 'mode' : copy.running ? 'starting' : state === 'error' ? 'error' : state
|
|
121
|
+
|
|
122
|
+
return h(React.Fragment, null,
|
|
123
|
+
h('style', null, CSS),
|
|
124
|
+
h('button', {
|
|
125
|
+
type: 'button',
|
|
126
|
+
className: 'dsh-wsl-desktop-footer',
|
|
127
|
+
'data-wide': wide ? 'true' : 'false',
|
|
128
|
+
'data-state': buttonState,
|
|
129
|
+
title,
|
|
130
|
+
'aria-label': title,
|
|
131
|
+
disabled: busy,
|
|
132
|
+
onClick,
|
|
133
|
+
}, h(DesktopIcon, { size: 15 }), wide ? h('span', null, label) : null))
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
inject: ['slots'],
|
|
138
|
+
apply(ctx) {
|
|
139
|
+
ctx.slots.inject('sidebar.footer.action', () => ctx.slots.register({
|
|
140
|
+
name: 'sidebar.footer.action',
|
|
141
|
+
id: 'wsl-desktop',
|
|
142
|
+
order: 4,
|
|
143
|
+
}, WslDesktopFooter))
|
|
144
|
+
},
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
})
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Desktop shell -> WSL Host proxy installer (bundle patch: an operation list).
|
|
2
|
+
# Automatic by default: on profile load it rebuilds the copy when missing or the
|
|
3
|
+
# Desktop version changed, mirrors the enabled Windows plugins into the WSL web
|
|
4
|
+
# profile, then launches the copy. Commands are the manual fallback.
|
|
5
|
+
- insert:
|
|
6
|
+
- id: wsl-desktop
|
|
7
|
+
name: '@karoy/dsh-wsl-desktop'
|
|
8
|
+
config:
|
|
9
|
+
userDataDirName: 'DeepSeek Harness WSL'
|
|
10
|
+
port: 3080
|
|
11
|
+
distro: ''
|
|
12
|
+
autoInstall: true
|
|
13
|
+
mode: proxy
|
|
14
|
+
autoRun: true
|
|
15
|
+
autoSetup: true
|
|
16
|
+
autoLaunch: false
|
|
17
|
+
syncWslPlugins: true
|
|
18
|
+
syncLinked: false
|
|
19
|
+
autoDelayMs: 4000
|
package/index.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host half of the Desktop-proxy installer plugin. By default it runs fully
|
|
3
|
+
* automatically on profile load: a detached helper rebuilds the patched copy
|
|
4
|
+
* when missing or the Desktop version changed, mirrors the enabled Windows
|
|
5
|
+
* plugins into the WSL web profile, then launches the copy. The footer entry and
|
|
6
|
+
* the slash commands are the manual fallback.
|
|
7
|
+
*
|
|
8
|
+
* @module @karoy/dsh-wsl-desktop
|
|
9
|
+
*/
|
|
10
|
+
import { spawn } from 'node:child_process'
|
|
11
|
+
import fs from 'node:fs'
|
|
12
|
+
import os from 'node:os'
|
|
13
|
+
import path from 'node:path'
|
|
14
|
+
import { fileURLToPath } from 'node:url'
|
|
15
|
+
import { remove, resolveConfig, setup, status } from './lib/setup.js'
|
|
16
|
+
import { needsBuild, readStatus } from './lib/state.js'
|
|
17
|
+
import { spawnCopy } from './lib/launch.js'
|
|
18
|
+
import { isCopyRunning as isCopyRunningCached } from './lib/sync.js'
|
|
19
|
+
import { syncPlugins } from './lib/sync.js'
|
|
20
|
+
|
|
21
|
+
export const inject = ['commands', 'webServer']
|
|
22
|
+
|
|
23
|
+
let scheduled = false
|
|
24
|
+
let runningCache = { at: 0, value: false }
|
|
25
|
+
|
|
26
|
+
/** Cached isCopyRunning (PowerShell probe is not free; poll asks every few seconds). */
|
|
27
|
+
/** True when the running copy proved it booted against the WSL Host (marker written by its patched main.js). */
|
|
28
|
+
function copyWslMode(config) {
|
|
29
|
+
try {
|
|
30
|
+
const file = path.join(config.installDir, '.dsh-wsl-running.json')
|
|
31
|
+
const data = JSON.parse(fs.readFileSync(file, 'utf8'))
|
|
32
|
+
if (data === null || data.mode !== 'proxy') return false
|
|
33
|
+
if (Number.isFinite(data.pid)) {
|
|
34
|
+
try { process.kill(data.pid, 0) } catch { return false }
|
|
35
|
+
}
|
|
36
|
+
const at = Date.parse(data.at ?? '')
|
|
37
|
+
if (Number.isFinite(at) && Date.now() - at > 24 * 60 * 60 * 1000) return false
|
|
38
|
+
return true
|
|
39
|
+
} catch {
|
|
40
|
+
return false
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function copyRunning(config) {
|
|
45
|
+
const now = Date.now()
|
|
46
|
+
if (now - runningCache.at < 5000) return runningCache.value
|
|
47
|
+
let value = false
|
|
48
|
+
try { value = isCopyRunningCached(config) } catch { value = false }
|
|
49
|
+
runningCache = { at: now, value }
|
|
50
|
+
return value
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function failure(error) {
|
|
54
|
+
return { kind: 'error', text: error instanceof Error ? error.message : String(error) }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function helperPath() {
|
|
58
|
+
return fileURLToPath(new URL('./bin/auto.mjs', import.meta.url))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Spawn the detached auto-run helper (build/sync/launch) once per Host lifetime. */
|
|
62
|
+
function scheduleAutoRun(config) {
|
|
63
|
+
if (scheduled) return
|
|
64
|
+
scheduled = true
|
|
65
|
+
runHelper(config)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Force a helper run (footer button / manual route). */
|
|
69
|
+
function runHelper(config) {
|
|
70
|
+
// Detached + CREATE_NO_WINDOW: no console flash, survives the Host, and the
|
|
71
|
+
// helper takes its config from a file argument (verified on this machine).
|
|
72
|
+
const dir = path.join(os.tmpdir(), 'dsh-wsl-desktop-auto')
|
|
73
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
74
|
+
const configFile = path.join(dir, 'config.json')
|
|
75
|
+
fs.writeFileSync(configFile, JSON.stringify(config))
|
|
76
|
+
const env = {
|
|
77
|
+
...process.env,
|
|
78
|
+
ELECTRON_RUN_AS_NODE: '1',
|
|
79
|
+
}
|
|
80
|
+
delete env.DSH_WSL_DESKTOP
|
|
81
|
+
const child = spawn(process.execPath, [helperPath(), configFile], {
|
|
82
|
+
detached: true,
|
|
83
|
+
stdio: 'ignore',
|
|
84
|
+
windowsHide: true,
|
|
85
|
+
env,
|
|
86
|
+
})
|
|
87
|
+
if (child.pid !== undefined) child.unref()
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Register automation, the footer route and the manual fallback commands.
|
|
92
|
+
* @param config - `{ installDir, userDataDirName, port, distro, autoInstall, mode, autoRun, autoSetup, autoLaunch, syncWslPlugins, syncLinked, autoDelayMs }`.
|
|
93
|
+
*/
|
|
94
|
+
export function apply(ctx, config = {}) {
|
|
95
|
+
const cfg = resolveConfig(config)
|
|
96
|
+
|
|
97
|
+
if (cfg.autoRun !== false && process.env.DSH_WSL_DESKTOP !== '1') {
|
|
98
|
+
setTimeout(() => {
|
|
99
|
+
try {
|
|
100
|
+
scheduleAutoRun(cfg)
|
|
101
|
+
} catch (error) {
|
|
102
|
+
console.error('[wsl-desktop] auto-run failed', error)
|
|
103
|
+
}
|
|
104
|
+
}, cfg.autoDelayMs ?? 4000)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
ctx.effect(() => ctx.webServer.register({
|
|
108
|
+
kind: 'exact',
|
|
109
|
+
path: '/wsl-desktop/state',
|
|
110
|
+
handler: (req, res) => {
|
|
111
|
+
const send = (status, body) => {
|
|
112
|
+
if (res.headersSent) return
|
|
113
|
+
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
114
|
+
res.end(JSON.stringify(body))
|
|
115
|
+
}
|
|
116
|
+
if (req.method !== 'GET') return send(405, { ok: false, error: 'method not allowed' })
|
|
117
|
+
try {
|
|
118
|
+
return send(200, { ok: true, running: copyRunning(cfg), wslMode: copyWslMode(cfg) })
|
|
119
|
+
} catch (error) {
|
|
120
|
+
return send(200, { ok: true, running: false, wslMode: false })
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
}))
|
|
124
|
+
|
|
125
|
+
ctx.effect(() => ctx.webServer.register({
|
|
126
|
+
kind: 'exact',
|
|
127
|
+
path: '/wsl-desktop/open',
|
|
128
|
+
handler: (req, res) => {
|
|
129
|
+
const send = (status, body) => {
|
|
130
|
+
if (res.headersSent) return
|
|
131
|
+
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
132
|
+
res.end(JSON.stringify(body))
|
|
133
|
+
}
|
|
134
|
+
if (req.method !== 'POST' && req.method !== 'GET') return send(405, { ok: false, error: 'method not allowed' })
|
|
135
|
+
if (req.headers['sec-fetch-site'] === 'cross-site') return send(403, { ok: false, error: 'cross-site request refused' })
|
|
136
|
+
try {
|
|
137
|
+
runHelper({ ...cfg, autoLaunch: true, syncWslPlugins: true })
|
|
138
|
+
return send(200, { ok: true })
|
|
139
|
+
} catch (error) {
|
|
140
|
+
return send(500, { ok: false, error: error instanceof Error ? error.message : String(error) })
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
}))
|
|
144
|
+
|
|
145
|
+
ctx.commands.register({
|
|
146
|
+
name: 'wsl-desktop-setup',
|
|
147
|
+
description: 'Rebuild the WSL Desktop proxy copy (manual fallback)',
|
|
148
|
+
handler: async () => {
|
|
149
|
+
try {
|
|
150
|
+
return { kind: 'success', text: setup(cfg) }
|
|
151
|
+
} catch (error) {
|
|
152
|
+
return failure(error)
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
ctx.commands.register({
|
|
158
|
+
name: 'wsl-desktop-status',
|
|
159
|
+
description: 'Report the WSL Desktop copy, its mode and the last auto-run',
|
|
160
|
+
handler: async () => {
|
|
161
|
+
try {
|
|
162
|
+
const auto = readStatus(cfg.installDir)
|
|
163
|
+
const lines = [status(cfg), needsBuild(cfg) ? '需要重建(版本/模式变化或副本缺失)' : '已是最新']
|
|
164
|
+
if (auto !== null) lines.push('auto: ' + JSON.stringify(auto.steps), auto.ok ? 'auto: ok' : 'auto 失败:' + String(auto.error))
|
|
165
|
+
return { kind: 'success', text: lines.join('\n') }
|
|
166
|
+
} catch (error) {
|
|
167
|
+
return failure(error)
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
ctx.commands.register({
|
|
173
|
+
name: 'wsl-desktop-launch',
|
|
174
|
+
description: 'Launch the WSL Desktop proxy copy now',
|
|
175
|
+
handler: async () => {
|
|
176
|
+
try {
|
|
177
|
+
return { kind: 'success', text: '已启动,pid=' + String(spawnCopy(cfg)) }
|
|
178
|
+
} catch (error) {
|
|
179
|
+
return failure(error)
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
ctx.commands.register({
|
|
185
|
+
name: 'wsl-desktop-sync',
|
|
186
|
+
description: 'Mirror the enabled Windows desktop profile plugins into the WSL web profile',
|
|
187
|
+
handler: async () => {
|
|
188
|
+
try {
|
|
189
|
+
const result = syncPlugins(cfg)
|
|
190
|
+
return {
|
|
191
|
+
kind: 'success',
|
|
192
|
+
text: ['added: ' + result.added.join(', '), 'removed: ' + result.removed.join(', '), 'failed: ' + result.failed.join(', '), 'skipped: ' + result.skipped.map((s) => s.name + ' (' + s.reason + ')').join(', ')].join('\n'),
|
|
193
|
+
}
|
|
194
|
+
} catch (error) {
|
|
195
|
+
return failure(error)
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
ctx.commands.register({
|
|
201
|
+
name: 'wsl-desktop-remove',
|
|
202
|
+
description: 'Remove the WSL Desktop copy and its isolated Electron profile',
|
|
203
|
+
handler: async () => {
|
|
204
|
+
try {
|
|
205
|
+
return { kind: 'success', text: remove(cfg) }
|
|
206
|
+
} catch (error) {
|
|
207
|
+
return failure(error)
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
})
|
|
211
|
+
}
|
package/lib/launch.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Start the patched Desktop copy as a detached process. The launcher script
|
|
3
|
+
* (启动-WSL-Desktop.cmd) does the same for manual use; this is the automatic
|
|
4
|
+
* path.
|
|
5
|
+
*
|
|
6
|
+
* @module @karoy/dsh-wsl-desktop/launch
|
|
7
|
+
*/
|
|
8
|
+
import { spawn } from 'node:child_process'
|
|
9
|
+
import path from 'node:path'
|
|
10
|
+
|
|
11
|
+
export class LaunchError extends Error {}
|
|
12
|
+
|
|
13
|
+
/** Build the environment the copy expects (no ELECTRON_RUN_AS_NODE, isolated DSH_HOME). */
|
|
14
|
+
export function copyEnvironment(config) {
|
|
15
|
+
const env = { ...process.env }
|
|
16
|
+
delete env.ELECTRON_RUN_AS_NODE
|
|
17
|
+
delete env.NODE_OPTIONS
|
|
18
|
+
delete env.ELECTRON_ENABLE_LOGGING
|
|
19
|
+
Object.assign(env, {
|
|
20
|
+
DSH_WSL_DESKTOP: '1',
|
|
21
|
+
DSH_WSL_MODE: config.mode,
|
|
22
|
+
DSH_WSL_PORT: String(config.port),
|
|
23
|
+
DSH_WSL_DISTRO: config.distro ?? '',
|
|
24
|
+
DSH_WSL_AUTOINSTALL: config.autoInstall ? '1' : '0',
|
|
25
|
+
DSH_HOME: path.join(config.installDir, 'dsh-home'),
|
|
26
|
+
})
|
|
27
|
+
return env
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Launch the copy.
|
|
32
|
+
* @returns the child process id.
|
|
33
|
+
*/
|
|
34
|
+
export function spawnCopy(config) {
|
|
35
|
+
const exe = path.join(config.installDir, 'DeepSeek Harness.exe')
|
|
36
|
+
const child = spawn(exe, [], {
|
|
37
|
+
detached: true,
|
|
38
|
+
stdio: 'ignore',
|
|
39
|
+
env: copyEnvironment(config),
|
|
40
|
+
windowsHide: false,
|
|
41
|
+
})
|
|
42
|
+
child.unref()
|
|
43
|
+
if (child.pid === undefined) throw new LaunchError('wsl-desktop: could not start the copy')
|
|
44
|
+
return child.pid
|
|
45
|
+
}
|