dsh-open-in-dsh 0.3.0 → 0.3.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 CHANGED
@@ -1,46 +1,40 @@
1
1
  # dsh-open-in-dsh
2
2
 
3
- 在 Windows 资源管理器 / macOS Finder 里**右键一个文件夹** →「**通过 DSH 打开**」,
4
- DSH 就把这个目录作为工作区切入,并进到里面的一个空白会话。
3
+ 在 Windows 资源管理器或 macOS Finder 里右键一个文件夹,选「通过 DSH 打开」,DSH 就把该目录作为工作区切入,并进到里面的一个空白会话。
5
4
 
6
- - 已经登记过的工作区 → 直接切过去
7
- - 没登记过 → 自动新建对应工作区
8
- - **复用**该工作区已有的空白会话(不会每点一次就堆一个空会话)
9
- - 不闪控制台窗口,也不多开浏览器
10
- - **桌面版**:DSH 没开就把它启动起来;开着但被挡住/缩在托盘里就把它提到最前
5
+ - 已登记过的工作区:直接切过去。
6
+ - 没登记过的工作区:自动新建。
7
+ - 默认复用该工作区已有的空白会话,不会每点一次就堆一个空会话;`alwaysNewSession: true` 时改为每次新建。
8
+ - 不闪控制台窗口,也不多开浏览器。
9
+ - 桌面版:DSH 没开就把它启动起来;开着但被挡住或缩在托盘里就把它提到最前。
11
10
 
12
- 反方向也有入口:DSH 里的**文件/目录右键菜单**提供「用 QSpace Pro / Finder / VS Code /
13
- Warp / Terminal 打开」(Windows 是资源管理器),见下文「DSH 右键菜单里的『打开方式』」。
11
+ 反方向也有入口:DSH 的文件/目录右键菜单提供「用 QSpace Pro / Finder / VS Code / Warp / Terminal 打开」(Windows 是资源管理器);会话行菜单与右侧文档预览提供「用 QSpace 打开」。见下文「DSH 右键菜单」。
14
12
 
15
13
  ## 平台要求
16
14
 
17
15
  | 平台 | 系统集成 | 需要什么 |
18
16
  |---|---|---|
19
17
  | Windows 10/11 | 资源管理器右键动词(文件夹 / 文件夹空白处) | `wscript.exe` + PowerShell(安装脚本用 `pwsh` 运行;动词运行时优先 `pwsh`,没有则回退 Windows PowerShell)。写 HKCU,不需要管理员权限 |
20
- | macOS | Finder **快速操作** + 终端命令 `dsh-open` | 系统自带 Automator 服务、`curl`、`osascript`;可写入 `~/Library/Services` 与 `~/bin` |
18
+ | macOS | Finder 快速操作 + 终端命令 `dsh-open` | 系统自带 Automator 服务、`curl`、`osascript`;可写入 `~/Library/Services` 与 `~/bin` |
21
19
  | macOS(可选) | QSpace Pro 的「服务」子菜单 | 只有装了 QSpace Pro 才有意义,安装器会自动检测,也可用 `--qspace` 控制 |
22
20
 
23
21
  DSH 侧:桌面版或 `dsh web` 都可以;声明的内核区间见「兼容性」。
24
22
 
25
23
  ## 安装
26
24
 
27
- ### 1. 插件(宿主 + 浏览器两半边)
25
+ ### 1. 安装插件
28
26
 
29
27
  ```sh
30
28
  dsh plugin --profile <profile> add dsh-open-in-dsh
31
29
  ```
32
30
 
33
- 把 `<profile>` 换成目标 profile(桌面版 `desktop`、`dsh web` 用 `web`)。
34
- 从源码安装时把最后一项换成 checkout 里的 `packages/dsh-open-in-dsh` 路径。
31
+ 把 `<profile>` 换成目标 profile(桌面版 `desktop`、`dsh web` 用 `web`)。从源码安装时把最后一项换成 checkout 里的 `packages/dsh-open-in-dsh` 路径。
35
32
 
36
- 装完刷新页面;**改宿主半边(`src/index.ts`,重新 `npm run build` 后)必须重启宿主**——桌面版要完全退出应用再打开
37
- (关窗口只是缩进托盘),否则运行中的进程还在用模块缓存里的旧代码。
33
+ 装完刷新页面。改宿主半边(`src/index.ts`,重新 `npm run build` 后)必须重启宿主:桌面版要完全退出应用再打开(关窗口只是缩进托盘),否则运行中的进程还在用模块缓存里的旧代码。
38
34
 
39
35
  ### 2. 注册系统右键菜单
40
36
 
41
- 菜单脚本装在插件包目录里(用 `dsh plugin add` 安装时是
42
- `~/.dsh/profiles/<profile>/node_modules/dsh-open-in-dsh/scripts/`;源码安装则在 checkout 内)。
43
- **装插件本身不会改系统菜单**,需要跑一次安装脚本:
37
+ 菜单脚本装在插件包目录里(用 `dsh plugin add` 安装时是 `~/.dsh/profiles/<profile>/node_modules/dsh-open-in-dsh/scripts/`;源码安装则在 checkout 内)。装插件本身不会改系统菜单,需要跑一次安装脚本。
44
38
 
45
39
  **Windows**
46
40
 
@@ -49,11 +43,9 @@ pwsh -File "$env:USERPROFILE\.dsh\profiles\<profile>\node_modules\dsh-open-in-ds
49
43
  ```
50
44
 
51
45
  - `-Action status`:只查看当前注册与解析结果;`-Action uninstall`:移除。
52
- - 不带 `-Port` / `-App` 就是自动:检测到桌面版就注册桌面版端口与应用,否则退回 `dsh web` + 浏览器 URL。
53
- 也可以显式钉住:`-Port 3080 -App <exe 路径>`。
54
- - 写入两个 HKCU 动词:`Directory\shell\DSHOpen`(右键文件夹)与
55
- `Directory\Background\shell\DSHOpen`(右键文件夹空白处)。
56
- - **装完重启一次资源管理器**——Explorer 按进程缓存右键菜单,不重启看不到新条目。
46
+ - 不带 `-Port` / `-App` 就是自动:桌面版只把「每次点击时现场发现端口」这件事写进动词(存的是 `0`),并记住应用路径;没有桌面版时退回 `dsh web`(3080)+ 浏览器 URL。也可以显式钉住:`-Port 3080 -App <exe 路径>`。
47
+ - 写入两个 HKCU 动词:`Directory\shell\DSHOpen`(右键文件夹)与 `Directory\Background\shell\DSHOpen`(右键文件夹空白处)。
48
+ - 装完重启一次资源管理器:Explorer 按进程缓存右键菜单,不重启看不到新条目。
57
49
 
58
50
  **macOS**
59
51
 
@@ -70,10 +62,9 @@ sh ~/.dsh/profiles/<profile>/node_modules/dsh-open-in-dsh/scripts/macos/install-
70
62
  | `~/Library/Services/通过 DSH 打开 (QSpace).workflow` | 同一个标签/脚本,作用域是 QSpace Pro(装了才生成) |
71
63
  | `~/bin/dsh-open` | 终端命令;不带参数就是当前目录 |
72
64
 
73
- 第一次安装后如果菜单没出现:`killall Finder` 或注销重登;菜单项/快捷键在
74
- 系统设置 → 键盘 → 键盘快捷键 → 服务。
65
+ 第一次安装后如果菜单没出现:`killall Finder` 或注销重登;菜单项/快捷键在系统设置 → 键盘 → 键盘快捷键 → 服务。
75
66
 
76
- ### 卸载
67
+ ## 卸载
77
68
 
78
69
  ```sh
79
70
  # 1) 系统菜单
@@ -87,8 +78,7 @@ dsh plugin --profile <profile> remove dsh-open-in-dsh
87
78
 
88
79
  ## 配置
89
80
 
90
- bundle 自带的 `cordis.patch.yml` 只是默认值;覆盖写在 profile 的 `cordis.patch.yml`
91
- (或 `$DSH_HOME/settings.yaml` 的同名段),**整行替换 config,所以要写全**:
81
+ bundle 自带的 `cordis.patch.yml` 只是默认值;覆盖写在 profile 的 `cordis.patch.yml`(或 `$DSH_HOME/settings.yaml` 的同名段),整行替换 `config`,所以要写全:
92
82
 
93
83
  ```yaml
94
84
  - id: open-in-dsh
@@ -108,9 +98,7 @@ bundle 自带的 `cordis.patch.yml` 只是默认值;覆盖写在 profile 的 `
108
98
  | `backlogTtlMs` | `60000` | 页面没开着时,请求在这段时间内仍可被补收 |
109
99
  | `socketPath` | `~/.dsh/run/dsh-open-in-dsh.sock` | macOS 交接套接字路径;空串表示不监听 |
110
100
 
111
- `Config` schema 的作用是让这一行的 `config` 在加载时**被校验并补上默认值**,并让
112
- `Config.listConfigs` 读得到键名;它不会长出一个可编辑表单。「设置 → 插件」是只读清单,
113
- 开关插件请用侧边栏「插件」页。
101
+ `Config` schema 的作用是让这一行的 `config` 在加载时被校验并补上默认值,并让 `Config.listConfigs` 读得到键名;它不会长出一个可编辑表单。「设置 → 插件」是只读清单,开关插件请用侧边栏「插件」页。
114
102
 
115
103
  ## 工作方式
116
104
 
@@ -122,81 +110,69 @@ bundle 自带的 `cordis.patch.yml` 只是默认值;覆盖写在 profile 的 `
122
110
  |---|---|---|
123
111
  | 1 | 菜单动词 | `open-in-dsh.vbs` 用环境变量把 `%V` 交给 `open-in-dsh.ps1`,后者解析端口/应用并 POST 到 `127.0.0.1:<port>/dsh-open-in-dsh/api/v1/open`;没人应答就先把桌面版启动起来再重投 |
124
112
  | 2 | 宿主半边 | 校验路径(必须存在且是目录、只接受回环来源),入队并广播 |
125
- | 3 | 页面 | 通过 SSE 收到请求,走客户端自己的 `workspaces.create({ path })` + `uiWorkspace.openWorkspace(id)` 完成跳转 |
113
+ | 3 | 页面 | 通过 SSE 收到请求:`alwaysNewSession` 为 `false` 时走 `workspaces.create({ path })` + `uiWorkspace.openWorkspace(id)`;为 `true` 时走 `workspaces.create({ path })` + `sessions.create({ workspaceId })` + `uiWorkspace.openSession(id)` |
126
114
  | 4 | 启动器 | 桌面版:再启动一次应用把窗口提到最前(单实例锁 → `second-instance` → 聚焦);`dsh web`:没人跟随时打开浏览器 URL |
127
115
 
128
- 动词指向 `wscript.exe` 而不是 PowerShell,路径通过进程环境变量传:
129
- GUI 子系统的 wscript 不会创建控制台窗口,因此右键不会闪窗;`%V` 里的空格、单引号、
130
- 盘符根目录的结尾反斜杠也不经过引号转义。
116
+ 动词指向 `wscript.exe` 而不是 PowerShell,路径通过进程环境变量传:GUI 子系统的 wscript 不会创建控制台窗口,因此右键不会闪窗;`%V` 里的空格、单引号、盘符根目录的结尾反斜杠也不经过引号转义。
131
117
 
132
118
  ### macOS
133
119
 
134
- 桌面外壳从 2.0.19 起不再接受 `--dsh-desktop-workspace` 启动参数(Chromium 选项解析会直接
135
- `bad option` 退出),所以快速操作走**插件自己的 Unix 域套接字**
136
- (默认 `~/.dsh/run/dsh-open-in-dsh.sock`,权限 0600):启动器把同样的 JSON 打到套接字上,
137
- 与 Windows 那条 HTTP 路由共用同一个 handler。macOS 桌面版端口不固定(默认 43120,被占用
138
- 自动 +1),而且外壳在进路由前就把非 Electron renderer 的请求拒成 403,所以这条链路只能走
139
- 套接字,不能走浏览器 URL。
120
+ 桌面外壳从 2.0.19 起不再接受 `--dsh-desktop-workspace` 启动参数(Chromium 选项解析会直接 `bad option` 退出),所以快速操作走插件自己的 Unix 域套接字(默认 `~/.dsh/run/dsh-open-in-dsh.sock`,权限 0600):启动器把同样的 JSON 打到套接字上,与 Windows 那条 HTTP 路由共用同一个 handler;就绪探测走套接字上的 status 路由。macOS 桌面版端口不固定(默认 43120,被占用自动 +1),而且外壳在进路由前就把非 Electron renderer 的请求拒成 403,所以这条链路只能走套接字,不能走浏览器 URL。
140
121
 
141
- 启动器(`scripts/macos/open-in-dsh`)把路径规范化(软链、相对路径、结尾斜杠、空格与引号)→
142
- 找 app(先看正在跑的那个,再看 `/Applications`,最后按 bundle id 走 Spotlight):
122
+ 启动器(`scripts/macos/open-in-dsh`)把路径规范化(软链、相对路径、结尾斜杠、空格与引号)→ 找 app(先看正在跑的那个,再看 `/Applications` 与 `~/Applications`,最后按 bundle ID 走 Spotlight):
143
123
 
144
- - **已经开着**:`curl --unix-socket <socket>` 投路径,然后不带参数再执行一次 app 可执行文件
145
- 把窗口提到最前;
146
- - **没开**:`open -a "<app>"` 起应用,等宿主把套接字绑上(默认最多 30 s,`DSH_OPEN_WAIT` 可改)
147
- 再投递;
148
- - **没装桌面版**:回落到同一条路由的 TCP 版(`dsh web`,默认 3080;`--port` 或 `DSH_OPEN_PORT` 可改)。
124
+ - 已经开着:`curl --unix-socket <socket>` 投路径,然后不带参数再执行一次 app 可执行文件把窗口提到最前。
125
+ - 没开:`open -a "<app>"` 起应用,等宿主把套接字绑上(默认最多 30 s,`DSH_OPEN_WAIT` 可改)再投递。
126
+ - 没装桌面版:回落到同一条路由的 TCP 版(`dsh web`,默认 3080;`--port` 或 `DSH_OPEN_PORT` 可改)。
149
127
 
150
- ### 五条路由
128
+ ### HTTP 路由
129
+
130
+ 所有路由都以 `/dsh-open-in-dsh/api/v1` 开头,成功时返回:
151
131
 
152
132
  | 路由 | 用途 |
153
133
  |---|---|
154
- | `POST /open` | 启动器提交目录;返回 `{seq, clients, recent}` |
134
+ | `POST /open` | 启动器提交目录;返回 `{ok, seq, path, clients, recent}` |
155
135
  | `GET /events` | 页面跟随请求(SSE,带 `hello` 与回放) |
156
- | `GET /status` | 只报「有几个页面在跟」,无副作用 |
157
- | `GET /apps` | 菜单先问本平台有哪些白名单 app 真的装了;返回 `{platform, apps}` |
136
+ | `GET /status` | 只报当前跟随的页面数与序号,无副作用;返回 `{ok, clients, seq, recent}` |
137
+ | `GET /apps` | 菜单先问本平台有哪些白名单 app 真的装了;返回 `{ok, platform, apps}` |
158
138
  | `POST /open-with` | 菜单把已存在的文件/目录交给白名单 app;返回 `{ok, app, path, directory}` |
139
+ | `POST /open-session` | 会话行菜单提交会话 ID,宿主解析出 cwd 后用同一套白名单 argv 打开;返回 `{ok, app, sessionId, path, directory}` |
159
140
 
160
- ### DSH 右键菜单里的「打开方式」
141
+ ### DSH 右键菜单
161
142
 
162
- 文件/目录的右键菜单通过内核的 `@deepseek-ai/dsh-client-ui-context-menu` 注册(客户端
163
- `dsh.client.inject` 声明依赖),点开后按宿主报告的可用 app 列动作:
143
+ 文件/目录的右键菜单通过内核的 `@deepseek-ai/dsh-client-ui-context-menu` 注册(客户端 `dsh.client.inject` 声明依赖),点开后按宿主报告的可用 app 列动作:
164
144
 
165
145
  | 平台 | 动作 |
166
146
  |---|---|
167
147
  | macOS | QSpace Pro、Finder、VS Code、Warp、Terminal(装了才出现) |
168
148
  | Windows | 资源管理器(目录直接打开,文件用 `/select,` 定位) |
169
149
 
150
+ 会话行菜单注册在同一套右键菜单机制上,只提供「在 QSpace 中显示」一项:`qspace` 不可用时整项不出现;会话没有 cwd 时给一条禁用的「会话没有工作目录」。会话行菜单只把会话 ID 交给宿主,目录由宿主从会话头解析。右侧文档预览的 `sidebar.right.tab.document.actions` 另有一个「用 QSpace 打开所在目录」的按钮,走 `/open-with`,传的是当前文件所在目录的绝对路径。
151
+
170
152
  ### 为什么用 SSE 而不是轮询
171
153
 
172
- Chrome 会把后台页面的定时器节流到每分钟一次,而刚点完右键时 DSH 窗口正好被资源管理器盖住
173
- (就是后台页面)。SSE 是网络事件,后台窗口照样立刻收到。
154
+ Chrome 会把后台页面的定时器节流到每分钟一次,而刚点完右键时 DSH 窗口正好被资源管理器盖住(就是后台页面)。SSE 是网络事件,后台窗口照样立刻收到。
174
155
 
175
156
  ## 安全边界
176
157
 
177
- 这条路由的作用是**让浏览器把任意本地目录当成工作区打开**,而工作区里会跑命令,
178
- 所以它等同于一个代码执行入口:
158
+ 这条路由的作用是让浏览器把任意本地目录当成工作区打开,而工作区里会跑命令,所以它等同于一个代码执行入口:
179
159
 
180
- - **只接受回环来源**。内核 0.2.1 起 webserver 不再接受通配地址(`0.0.0.0` / `::` 加载即报错),
181
- 但仍可以钉到某个具体接口地址(比如本机的局域网 IP),那依旧是网络可达的——回环检查不能省。
182
- - **带 `Origin` 的请求必须同源**。另一个网页即使能连到端口,也不能在背后点火。
160
+ - 只接受回环来源。内核 0.2.1 起 webserver 不再接受通配地址(`0.0.0.0` / `::` 加载即报错),但仍可以钉到某个具体接口地址(比如本机的局域网 IP),那依旧是网络可达的——回环检查不能省。
161
+ - 带 `Origin` 的请求必须同源。另一个网页即使能连到端口,也不能在背后点火。
183
162
  - 启动器(wscript / PowerShell)不带 `Origin`,只靠回环这一条通过。
184
- - 「打开方式」的 app 必须是白名单 id,路径必须是**已存在**的绝对路径;命令由白名单表拼成
185
- argv、用 `execFile` 直接执行,不经过 shell。
163
+ - 「打开方式」的 app 必须是白名单 ID,路径必须是已存在的绝对路径;命令由白名单表拼成 argv、用 `execFile` 直接执行,不经过 shell。
164
+ - `/open-session` 只收会话 ID 与白名单 app ID,目录由宿主从会话头解析;路径不出现在请求里,伪造请求无法用它打开任意目录。
186
165
 
187
166
  ## 已知限制
188
167
 
189
- - 只注册了「文件夹」和「文件夹空白处」。右键**盘符**(如 `D:\`)需要再注册
190
- `Drive\shell\DSHOpen`,路径规范化已支持盘符根目录。
191
- - 桌面版提窗口靠「再启动一次应用」(每次多一个立即退出的短进程)。能不能真的抢到前台取决于
192
- Windows 的前台权限策略;应用没开时走冷启动,窗口必然起来。
193
- - 桌面版**登录页**出现时页面还没跑起来:请求会待在回放窗口里,登录进去、页面加载后才落地,
194
- 超过 `backlogTtlMs` 就过期,需要重按一次。
168
+ - 只注册了「文件夹」和「文件夹空白处」。右键盘符(如 `D:\`)需要再注册 `Drive\shell\DSHOpen`,路径规范化已支持盘符根目录。
169
+ - 桌面版提窗口靠「再启动一次应用」(每次多一个立即退出的短进程)。能不能真的抢到前台取决于 Windows 的前台权限策略;应用没开时走冷启动,窗口必然起来。
170
+ - 桌面版登录页出现时页面还没跑起来:请求会待在回放窗口里,登录进去、页面加载后才落地,超过 `backlogTtlMs` 就过期,需要重按一次。
195
171
  - 宿主半边改代码要重启宿主才生效;桌面版必须整个退出再打开。
196
- - macOS:快速操作在**子菜单**里,不是顶层条目(顶层需要 Finder Sync 扩展 + Xcode 工程 + 签名);
197
- 右键文件夹空白处没有入口(服务总要有选中项);移动 checkout 后启动器绝对路径会失效,
198
- 重跑一次 `install-macos.sh install`——`status` 会打印 `STALE` 并以非 0 退出。
172
+ - macOS:快速操作在子菜单里,不是顶层条目(顶层需要 Finder Sync 扩展 + Xcode 工程 + 签名);右键文件夹空白处没有入口(服务总要有选中项);移动 checkout 后启动器绝对路径会失效,重跑一次 `install-macos.sh install`——`status` 会打印 `STALE` 并以非 0 退出。
199
173
  - 宿主重启后插件重新绑定套接字/路由;套接字不在时启动器明确报错并写日志,不会假装成功。
174
+ - Windows 桌面版的端口**每次启动都变**(桌面壳用 --port 0)。动词里存的是 0,启动器每次现场发现:从 DSH 进程的 loopback 连接反推端口,再用 /dsh-open-in-dsh/api/v1/status 确认(这版 Windows 上监听套接字不在
175
+ etstat 表里,所以只能靠连接反推)。发现失败会回落到 19387 / 3080,并照旧弹窗报错。
200
176
 
201
177
  ## 验证(开发)
202
178
 
@@ -205,16 +181,13 @@ npm test # 82 个测试(含客户端构建与语法
205
181
  node --test test/macos.test.ts # macOS 启动器/安装器 17 个测试
206
182
  ```
207
183
 
208
- `npm test` 覆盖宿主路由与回放、`%V` 的各种形态、脚本镜像(路由路径/端口/`-Probe` 一致)、
209
- 白名单与「打开方式」策略、会话定位,以及真实 `node:http` 上的集成路由(含跨域拒绝)。
184
+ `npm test` 覆盖宿主路由与回放、`%V` 的各种形态、脚本镜像(路由路径/端口/`-Probe` 一致)、白名单与「打开方式」策略、会话定位,以及真实 `node:http` 上的集成路由(含跨域拒绝)。
210
185
 
211
186
  ## 兼容性
212
187
 
213
188
  - **DSH**:`engines.dsh: ^0.2.1-rc.2 || ^0.2.2-alpha.2`;Node.js `>=24`。
214
- - **会话存储**:宿主半边通过 `dsh-session-ops/store` 读会话头(peer 依赖,`dsh-session-ops >= 0.1.0`),
215
- 与 `dsh-session-ops` 共用同一份存储格式。
216
- - 本插件不 import 任何内核包,只使用内核提供的服务(`webServer`、`workspaces`、`sessions`、
217
- `uiWorkspace`、客户端模块系统);接口变化时最坏结果是某个入口失效,不会影响 DSH 本身。
189
+ - **会话存储**:宿主半边通过 `dsh-session-ops/store` 读会话头(peer 依赖,`dsh-session-ops >= 0.1.0`),与 `dsh-session-ops` 共用同一份存储格式。
190
+ - 本插件不 import 任何内核包,只使用内核提供的服务(`webServer`、`workspaces`、`sessions`、`uiWorkspace`、客户端模块系统);接口变化时最坏结果是某个入口失效,不会影响 DSH 本身。
218
191
  - DSH 侧「打开方式」菜单依赖内核的 `@deepseek-ai/dsh-client-ui-context-menu` 客户端包。
219
192
 
220
193
  ## License
package/docs/DETAIL.md CHANGED
@@ -3,11 +3,11 @@
3
3
  Wires "open with DSH" into the OS file manager and DSH context menus.
4
4
 
5
5
  ## Two directions
6
- - OS side: Windows Explorer context menu; macOS Finder quick action, QSpace service and the dsh-open handoff.
7
- - DSH side: file/directory context menu actions for QSpace Pro, Finder, VS Code, Warp and Terminal.
6
+ - OS side: Windows Explorer context menu; macOS Finder quick action, QSpace Pro service and the `dsh-open` command.
7
+ - DSH side: file/directory context menu actions for QSpace Pro, Finder, VS Code, Warp and Terminal; the session row menu and the document preview offer "open in QSpace".
8
8
 
9
9
  ## Implementation notes
10
- - The host exposes an open-with route with an app allowlist; arbitrary commands are rejected.
10
+ - The host exposes six exact-match routes under `/dsh-open-in-dsh/api/v1`: `open`, `events` (SSE), `status`, `apps`, `open-with` and `open-session`. App ids are allowlisted, paths are validated by the host, and `execFile` runs without a shell.
11
11
  - Handoff uses the plugin's own Unix socket (macOS) or the loopback HTTP route (Windows).
12
12
 
13
13
  ## Notes
package/docs/DETAIL.zh.md CHANGED
@@ -3,12 +3,26 @@
3
3
  把「用 DSH 打开」接到系统文件管理器与 DSH 的右键菜单上。
4
4
 
5
5
  ## 两个方向
6
- - 系统侧:Windows 资源管理器右键「通过 DSH 打开」;macOS 提供 Finder 快速操作、QSpace 服务项,以及 dsh-open 交接。
7
- - DSH 侧:文件/目录右键菜单提供「用 QSpace Pro / Finder / VS Code / Warp / Terminal 打开」。
8
6
 
9
- ## 实现要点
10
- - 宿主开放 open-with 路由,应用名走白名单映射,不接受任意命令。
11
- - 交接走插件自己的 Unix socket(macOS)或回环 HTTP 路由(Windows),不经浏览器页面。
7
+ - 系统侧:Windows 资源管理器右键「通过 DSH 打开」;macOS 提供 Finder 快速操作、QSpace Pro 服务项,以及终端命令 `dsh-open`。
8
+ - DSH 侧:文件/目录右键菜单提供「用 QSpace Pro / Finder / VS Code / Warp / Terminal 打开」;会话行菜单与右侧文档预览提供「用 QSpace 打开」。
9
+
10
+ ## 路由
11
+
12
+ 宿主的六条精确匹配路由都在 `/dsh-open-in-dsh/api/v1` 下,应用名走白名单 ID,路径由宿主校验,`execFile` 直接执行,不经过 shell:
13
+
14
+ - `POST /open`:启动器提交目录,返回 `{ok, seq, path, clients, recent}`。
15
+ - `GET /events`:页面跟随请求(SSE,先发 `hello`,再回放 `since` 之后的请求)。
16
+ - `GET /status`:只报当前跟随的页面数与序号,无副作用。
17
+ - `GET /apps`:本平台实际可用的白名单 app,返回 `{ok, platform, apps}`。
18
+ - `POST /open-with`:把已存在的文件/目录交给白名单 app,返回 `{ok, app, path, directory}`。
19
+ - `POST /open-session`:会话行菜单提交会话 ID,宿主解析出 cwd 后用同一套 argv 打开,返回 `{ok, app, sessionId, path, directory}`。
20
+
21
+ ## 交接通道
22
+
23
+ - macOS:插件自己的 Unix 域套接字(默认 `~/.dsh/run/dsh-open-in-dsh.sock`,权限 0600),与 HTTP 的 open 路由共用同一个 handler;就绪探测走套接字上的 status 路由。
24
+ - Windows:回环 HTTP 路由,由 `wscript.exe` 启动的 PowerShell 启动器投递。
12
25
 
13
26
  ## 注意
14
- - 菜单化后不再依赖对 app.asar 打补丁。
27
+
28
+ - 菜单化后不再依赖对 app.asar 打补丁。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-open-in-dsh",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -191,6 +191,12 @@ New-MenuIcon $iconPath
191
191
  # on detection succeeding twice.
192
192
  $resolution = (& $launcher -Probe -Port $Port -App $App) | ConvertFrom-Json
193
193
  $verbPort = [int]$resolution.port
194
+ # The Desktop shell starts the profile with `--port 0`, so its port changes on
195
+ # every launch. Baking the port that happens to be live right now would be wrong
196
+ # after the next restart: store 0 instead, and every click resolves the live
197
+ # port (the launcher's default when no port is passed).
198
+ $verbPortAuto = ($Port -le 0) -and ($resolution.auto -eq $true) -and ($resolution.mode -eq 'desktop')
199
+ if ($verbPortAuto) { $verbPort = 0 }
194
200
  $verbApp = [string]$resolution.app
195
201
 
196
202
  $appArg = ''
@@ -208,7 +214,7 @@ Write-Output "registered '$VerbText' on:"
208
214
  Write-Output " $($verbKeys[0]) (right-click a folder)"
209
215
  Write-Output " $($verbKeys[1]) (right-click inside a folder)"
210
216
  Write-Output " host: $wscript -> $verbHost (no console window)"
211
- Write-Output " target: $($resolution.mode) host, port $verbPort"
217
+ Write-Output " target: $($resolution.mode) host, port $($verbPort)$(if ($verbPortAuto) { ' (auto: resolved on every click)' } else { '' })"
212
218
  if ($verbApp -ne '') {
213
219
  Write-Output " app: $verbApp (cold start + window raise)"
214
220
  } else {
@@ -42,6 +42,7 @@ set -u
42
42
 
43
43
  # Mirrored from lib/index.js by hand; test/macos.test.ts keeps the copies honest.
44
44
  ROUTE_OPEN='/dsh-open-in-dsh/api/v1/open'
45
+ ROUTE_STATUS='/dsh-open-in-dsh/api/v1/status'
45
46
  DEFAULT_WEB_PORT=3080
46
47
  SOCKET_PATH_DEFAULT="$HOME/.dsh/run/dsh-open-in-dsh.sock"
47
48
 
@@ -19,12 +19,14 @@
19
19
  Two deployments run the same script, and the presence of a Desktop app
20
20
  decides which one this is:
21
21
 
22
- * **DSH 桌面版** (profile `desktop`, port 19387). The host is the Electron
23
- app itself, so "nothing is listening" means "the app is closed", and
24
- starting the app is the only thing that can help. There is deliberately
25
- no browser fallback here: the Desktop host serves every page at the
26
- authenticated URL the shell owns, and a bare
27
- `http://127.0.0.1:19387/` answers
22
+ * **DSH 桌面版** (profile `desktop`, OS-assigned port). The host is the
23
+ Electron app itself, so "nothing is listening" means "the app is closed",
24
+ and starting the app is the only thing that can help. The shell starts the
25
+ profile with `--port 0`, so the port differs on every launch and is
26
+ discovered from the live loopback connections (`-Port 0`, the default).
27
+ There is deliberately no browser fallback here: the Desktop host serves
28
+ every page at the authenticated URL the shell owns, and a bare
29
+ `http://127.0.0.1:<port>/` answers
28
30
  `401 dsh web authentication required`. Opening that URL would give the
29
31
  user an error page instead of their folder.
30
32
 
@@ -89,11 +91,12 @@ param(
89
91
 
90
92
  $ErrorActionPreference = 'Stop'
91
93
 
92
- # The two ports that exist in the wild. 19387 is the Desktop shell's own
93
- # hard-coded port (`dsh-desktop-host` starts the profile with
94
- # `--no-open --port 19387`); 3080 is the historical `dsh web` port this
95
- # repository's README was written around. Mirrored in `install-menu.ps1` and
96
- # asserted against the scripts by `test/scripts.test.ts`.
94
+ # The two ports that exist in the wild. 3080 is the historical `dsh web` port
95
+ # this repository's README was written around; 19387 is the Desktop shell's old
96
+ # hard-coded port — it is no longer where the shell listens (0.2.2 starts the
97
+ # profile with `--port 0`), so it is kept only as a probe candidate for a stale
98
+ # install. Mirrored in `install-menu.ps1` and asserted against the scripts by
99
+ # `test/scripts.test.ts`.
97
100
  $DesktopPort = 19387
98
101
  $WebPort = 3080
99
102
 
@@ -192,38 +195,90 @@ function Resolve-DshApp([string]$Explicit) {
192
195
  }
193
196
 
194
197
  <# GET the side-effect-free follower count; $null when nothing answers. #>
195
- function Test-HostAlive([int]$HostPort) {
198
+ function Test-HostAlive([int]$HostPort, [int]$TimeoutSec = 3) {
196
199
  try {
197
- return (Invoke-RestMethod -Uri "http://127.0.0.1:$HostPort/dsh-open-in-dsh/api/v1/status" -Method Get -TimeoutSec 2)
200
+ return (Invoke-RestMethod -Uri "http://127.0.0.1:$HostPort/dsh-open-in-dsh/api/v1/status" -Method Get -TimeoutSec $TimeoutSec)
198
201
  } catch {
199
202
  return $null
200
203
  }
201
204
  }
202
205
 
206
+ <#
207
+ Ports worth probing, most likely first.
208
+
209
+ DSH 0.2.2's Desktop shell starts the profile with `--port 0`, so the OS picks
210
+ a port in the dynamic range (1024-15000 here) on every launch, and nothing
211
+ writes it to disk. The listener itself is not in this build's `netstat`
212
+ table, but the shell's own loopback connections are — so the port shows up as
213
+ the far end of a connection owned by a DSH process. Those candidates are
214
+ confirmed by the route itself, which only the real host answers.
215
+
216
+ @returns candidate ports, de-duplicated, in probe order.
217
+ #>
218
+ function Get-CandidateHostPorts {
219
+ $ports = New-Object System.Collections.Generic.List[int]
220
+ $dshPids = @(Get-Process -Name 'DeepSeek Harness' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Id)
221
+ try {
222
+ foreach ($line in (netstat -ano -p tcp 2>$null)) {
223
+ if ($line -match '127\.0\.0\.1:(\d+)\s+127\.0\.0\.1:(\d+)\s+ESTABLISHED\s+(\d+)\s*$') {
224
+ $owner = [int]$Matches[3]
225
+ if ($dshPids -contains $owner) {
226
+ $ports.Add([int]$Matches[1])
227
+ $ports.Add([int]$Matches[2])
228
+ }
229
+ }
230
+ }
231
+ } catch {
232
+ # No netstat output: only the known ports are left to try.
233
+ }
234
+ $ports.Add($DesktopPort)
235
+ $ports.Add($WebPort)
236
+ $ports | Where-Object { $_ -gt 0 -and $_ -le 65535 } | Select-Object -Unique
237
+ }
238
+
239
+ <#
240
+ The live host port, or 0 when no host answers.
241
+
242
+ A short timeout is enough: a host on loopback answers in milliseconds, and a
243
+ closed or filtered port is the slow case.
244
+ #>
245
+ function Find-LiveHostPort {
246
+ foreach ($candidate in (Get-CandidateHostPorts)) {
247
+ if ($null -ne (Test-HostAlive ([int]$candidate) 1)) { return [int]$candidate }
248
+ }
249
+ return 0
250
+ }
251
+
203
252
  $desktopApp = ''
204
253
  if (-not $NoAppLaunch) { $desktopApp = Resolve-DshApp $App }
205
254
 
206
- # No explicit port: prefer the Desktop host when the Desktop app exists. That
207
- # is the whole point of the check — with the app installed, a `dsh web` left
208
- # running on 3080 must not capture the right-click.
209
- if ($Port -le 0) {
210
- if ($desktopApp -ne '') { $Port = $DesktopPort } else { $Port = $WebPort }
255
+ # No explicit port: discover the live host first. The Desktop shell picks a new
256
+ # port on every launch, so a port baked into the verb would be wrong the moment
257
+ # the app restarts. The two historical ports are the fallback for a cold start,
258
+ # where there is no connection to read the port from.
259
+ $portWasExplicit = $Port -gt 0
260
+ if (-not $portWasExplicit) {
261
+ $Port = Find-LiveHostPort
262
+ if ($Port -le 0) {
263
+ if ($desktopApp -ne '') { $Port = $DesktopPort } else { $Port = $WebPort }
264
+ }
211
265
  }
212
266
 
213
- # Which deployment this run is: the Desktop one only when the Desktop port was
214
- # resolved *and* an app exists to start. Pinning `-Port 3080` on a machine that
215
- # also has the Desktop app therefore keeps the `dsh web` behaviour instead of
216
- # launching the wrong thing — and an explicit `-App` still works there as the
217
- # old `-AppShortcut` did: the window to open when nobody is following.
218
- $desktopMode = ($desktopApp -ne '') -and ($Port -eq $DesktopPort)
267
+ # Which deployment this run is: the Desktop one whenever an app exists to start
268
+ # *and* the port in hand is not the `dsh web` default — pinning `-Port 3080` on a
269
+ # machine that also has the Desktop app therefore keeps the `dsh web` behaviour
270
+ # instead of launching the wrong thing, and an explicit `-App` still works there
271
+ # as the old `-AppShortcut` did: the window to open when nobody is following.
272
+ $desktopMode = ($desktopApp -ne '') -and ($Port -ne $WebPort)
219
273
 
220
274
  if ($Probe) {
221
275
  $status = Test-HostAlive $Port
222
276
  $report = [ordered]@{
223
- port = $Port
224
- app = $desktopApp
225
- mode = if ($desktopMode) { 'desktop' } else { 'web' }
226
- host = ($null -ne $status)
277
+ port = $Port
278
+ auto = (-not $portWasExplicit)
279
+ app = $desktopApp
280
+ mode = if ($desktopMode) { 'desktop' } else { 'web' }
281
+ host = ($null -ne $status)
227
282
  clients = if ($null -ne $status) { [int]$status.clients } else { $null }
228
283
  }
229
284
  [pscustomobject]$report | ConvertTo-Json -Compress
@@ -236,21 +291,28 @@ if ($null -eq $target) {
236
291
  exit 0
237
292
  }
238
293
 
239
- $endpoint = "http://127.0.0.1:$Port/dsh-open-in-dsh/api/v1/open"
240
294
  $payload = @{ path = $target } | ConvertTo-Json -Compress
295
+ $script:lastError = ''
241
296
 
242
- $reply = $null
243
- $lastError = ''
244
- try {
245
- $reply = Invoke-RestMethod -Uri $endpoint -Method Post -ContentType 'application/json' -Body $payload -TimeoutSec 5
246
- } catch {
247
- $lastError = $_.Exception.Message
297
+ <# POST the folder; $null when nothing accepted it. #>
298
+ function Send-Open([int]$HostPort) {
299
+ try {
300
+ return (Invoke-RestMethod -Uri "http://127.0.0.1:$HostPort/dsh-open-in-dsh/api/v1/open" `
301
+ -Method Post -ContentType 'application/json' -Body $payload -TimeoutSec 5)
302
+ } catch {
303
+ $script:lastError = $_.Exception.Message
304
+ return $null
305
+ }
248
306
  }
249
307
 
308
+ $reply = $null
309
+ if ($Port -gt 0) { $reply = Send-Open $Port }
310
+
250
311
  # Nothing answered. In the Desktop deployment that is not an error, it is the
251
312
  # cold start: the host *is* the app, so the app has to come up first and the
252
- # announcement has to wait for it. In the `dsh web` deployment the host is a
253
- # service the user starts, and there is nothing to launch.
313
+ # announcement has to wait for it. The port is discovered again on every round,
314
+ # because the app that just started picked its own. In the `dsh web` deployment
315
+ # the host is a service the user starts, and there is nothing to launch.
254
316
  $launched = $false
255
317
  if ($null -eq $reply -and $desktopMode) {
256
318
  try {
@@ -264,20 +326,26 @@ if ($null -eq $reply -and $desktopMode) {
264
326
  $deadline = (Get-Date).AddSeconds($WaitSeconds)
265
327
  while ($null -eq $reply -and (Get-Date) -lt $deadline) {
266
328
  Start-Sleep -Milliseconds 700
267
- try {
268
- $reply = Invoke-RestMethod -Uri $endpoint -Method Post -ContentType 'application/json' -Body $payload -TimeoutSec 3
269
- $lastError = ''
270
- } catch {
271
- $lastError = $_.Exception.Message
329
+ if ($portWasExplicit) {
330
+ $reply = Send-Open $Port
331
+ } else {
332
+ $live = Find-LiveHostPort
333
+ if ($live -gt 0) {
334
+ $Port = $live
335
+ $reply = Send-Open $Port
336
+ }
272
337
  }
273
338
  }
339
+ if ($null -ne $reply) { $script:lastError = '' }
274
340
  }
275
341
 
342
+ $endpoint = "http://127.0.0.1:$Port/dsh-open-in-dsh/api/v1/open"
343
+
276
344
  if ($null -eq $reply) {
277
345
  if ($desktopMode) {
278
- Show-Failure "已启动 DSH 桌面版,但在 $WaitSeconds 秒内没能连上它($endpoint)。`n`n请确认 DSH 已正常打开后重试。`n`n$lastError"
346
+ Show-Failure "已启动 DSH 桌面版,但在 $WaitSeconds 秒内没能连上它($endpoint)。`n`n请确认 DSH 已正常打开后重试。`n`n$($script:lastError)"
279
347
  } else {
280
- Show-Failure "无法连接 DSH($endpoint)。`n`n请确认 dsh web 正在运行,然后重试。`n`n$lastError"
348
+ Show-Failure "无法连接 DSH($endpoint)。`n`n请确认 dsh web 正在运行,然后重试。`n`n$($script:lastError)"
281
349
  }
282
350
  exit 1
283
351
  }
@@ -35,10 +35,10 @@ If Len(Trim(path)) = 0 Then WScript.Quit 0
35
35
  script = fso.GetParentFolderName(WScript.ScriptFullName) & "\open-in-dsh.ps1"
36
36
  If Not fso.FileExists(script) Then WScript.Quit 1
37
37
 
38
- ' The Desktop shell's own port (dsh-desktop-host starts the profile with
39
- ' `--port 19387`). Kept in step with $DesktopPort in open-in-dsh.ps1 and with
40
- ' the installer by test/scripts.test.ts.
41
- port = "19387"
38
+ ' 0 means "resolve the port in the launcher": the Desktop shell starts the
39
+ ' profile with `--port 0`, so its port differs on every launch and cannot be
40
+ ' baked into this file. An explicit number still wins.
41
+ port = "0"
42
42
  If args.Count >= 2 Then
43
43
  If IsNumeric(args(1)) Then port = Trim(args(1))
44
44
  End If