@kir_sleeping/dsh-relay 0.0.0-stage → 0.1.0

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/locale/en.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "Relay",
4
+ "description": "Relays local application requests to this agent over HTTP on the harness port. External sessions live in their own workspace."
5
+ }
6
+ }
package/locale/zh.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "Relay",
4
+ "description": "让本机应用通过 HTTP 把任务交给这个 agent。外部会话单独放在一个工作区里。"
5
+ }
6
+ }
@@ -0,0 +1,278 @@
1
+ /**
2
+ * 原生确认框:由操作系统渲染在主机屏幕上,只有真人能点。
3
+ *
4
+ * Windows / WSL 上是右下角的置顶小窗口(toast 形态),带正文预览和「展开全文」;
5
+ * macOS 和 Linux 仍是居中的系统对话框,正文直接附在提示文字里。
6
+ *
7
+ * 返回 true 表示用户点了"允许",其余一切情况(拒绝、关窗口、超时、工具缺失)
8
+ * 都返回 false —— 调用方不需要区分,失败方向的默认行为都是"不放行"。
9
+ */
10
+
11
+ import { spawn } from 'node:child_process'
12
+ import { readFileSync } from 'node:fs'
13
+
14
+ const DEFAULT_TIMEOUT_MS = 60_000
15
+
16
+ /** 塞进系统窗口的正文上限,避免命令过长;超出部分在窗口里也读不过来。 */
17
+ const DETAIL_LIMIT = 4000
18
+
19
+ /** 检测 WSL:弹窗要交回 Windows 宿主,WSL 里没有可用的图形对话框。 */
20
+ function detectPlatform() {
21
+ if (process.platform !== 'linux') return process.platform
22
+ if (typeof process.env.WSL_DISTRO_NAME === 'string' && process.env.WSL_DISTRO_NAME !== '') return 'wsl'
23
+ try {
24
+ const version = readFileSync('/proc/version', 'utf8')
25
+ if (/microsoft/i.test(version)) return 'wsl'
26
+ } catch {
27
+ /* 读不到就按普通 linux 处理 */
28
+ }
29
+ return 'linux'
30
+ }
31
+
32
+ function appleString(value) {
33
+ return `"${String(value).replaceAll('\\', '\\\\').replaceAll('"', '\\"')}"`
34
+ }
35
+
36
+ function powerShellString(value) {
37
+ return `'${String(value).replaceAll("'", "''")}'`
38
+ }
39
+
40
+ function clipDetail(detail) {
41
+ if (typeof detail !== 'string' || detail === '') return ''
42
+ const text = detail.length > DETAIL_LIMIT ? `${detail.slice(0, DETAIL_LIMIT)}\n…(内容过长,后面省略)` : detail
43
+ return text
44
+ }
45
+
46
+ /**
47
+ * 右下角置顶小窗口:标题 + 正文预览 + 展开/收起 + 允许/拒绝。
48
+ * 正文可滚动,窗口可以长高到接近整屏;退出码就是答案。
49
+ */
50
+ function windowsScript(title, message, detail) {
51
+ const hasDetail = detail !== ''
52
+ const width = 340
53
+ const baseHeight = hasDetail ? 186 : 138
54
+ const buttonTop = baseHeight - 46
55
+
56
+ const lines = [
57
+ 'Add-Type -AssemblyName System.Windows.Forms',
58
+ 'Add-Type -AssemblyName System.Drawing',
59
+ '$anchorTLR = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Right',
60
+ '$anchorAll = $anchorTLR -bor [System.Windows.Forms.AnchorStyles]::Bottom',
61
+ '$anchorBL = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Left',
62
+ '$anchorBR = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right',
63
+ '$form = New-Object System.Windows.Forms.Form',
64
+ `$form.Text = ${powerShellString(title)}`,
65
+ '$form.Font = New-Object System.Drawing.Font("Microsoft YaHei UI", 11)',
66
+ '$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle',
67
+ '$form.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual',
68
+ '$form.TopMost = $true',
69
+ '$form.ShowInTaskbar = $true',
70
+ '$form.MaximizeBox = $false',
71
+ '$form.MinimizeBox = $false',
72
+ `$form.ClientSize = New-Object System.Drawing.Size(${width}, ${baseHeight})`,
73
+ '$area = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea',
74
+ '$form.Left = $area.Right - $form.Width - 24',
75
+ '$form.Top = $area.Bottom - $form.Height - 24',
76
+ '$header = New-Object System.Windows.Forms.Label',
77
+ `$header.Text = ${powerShellString(message)}`,
78
+ `$header.SetBounds(14, 10, ${width - 28}, ${hasDetail ? 46 : 50})`,
79
+ '$header.Anchor = $anchorTLR',
80
+ '$form.Controls.Add($header)',
81
+ ]
82
+
83
+ if (hasDetail) {
84
+ lines.push(
85
+ '$text = New-Object System.Windows.Forms.TextBox',
86
+ '$text.Multiline = $true',
87
+ '$text.ReadOnly = $true',
88
+ '$text.ScrollBars = [System.Windows.Forms.ScrollBars]::Vertical',
89
+ '$text.WordWrap = $true',
90
+ '$text.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle',
91
+ `$text.Text = ${powerShellString(detail)}`,
92
+ `$text.SetBounds(14, 58, ${width - 28}, 28)`,
93
+ '$text.Anchor = $anchorAll',
94
+ '$form.Controls.Add($text)',
95
+ '$expand = New-Object System.Windows.Forms.Button',
96
+ "$expand.Text = '展开全文'",
97
+ `$expand.SetBounds(14, ${buttonTop}, 96, 32)`,
98
+ '$expand.Anchor = $anchorBL',
99
+ '$form.Controls.Add($expand)',
100
+ '$script:expanded = $false',
101
+ '$expand.Add_Click({',
102
+ ' if (-not $script:expanded) {',
103
+ ' $target = [Math]::Min([Math]::Max(360, $area.Height - 120), 640)',
104
+ ` $form.ClientSize = New-Object System.Drawing.Size(${width}, $target)`,
105
+ " $expand.Text = '收起'",
106
+ ' $script:expanded = $true',
107
+ ' } else {',
108
+ ` $form.ClientSize = New-Object System.Drawing.Size(${width}, ${baseHeight})`,
109
+ " $expand.Text = '展开全文'",
110
+ ' $script:expanded = $false',
111
+ ' }',
112
+ ' $form.Top = $area.Bottom - $form.Height - 24',
113
+ '})',
114
+ )
115
+ }
116
+
117
+ lines.push(
118
+ '$deny = New-Object System.Windows.Forms.Button',
119
+ "$deny.Text = '拒绝'",
120
+ `$deny.SetBounds(${width - 186}, ${buttonTop}, 78, 32)`,
121
+ '$deny.Anchor = $anchorBR',
122
+ '$deny.Add_Click({ $script:answer = 1; $form.Close() })',
123
+ '$form.Controls.Add($deny)',
124
+ '$allow = New-Object System.Windows.Forms.Button',
125
+ "$allow.Text = '允许'",
126
+ `$allow.SetBounds(${width - 98}, ${buttonTop}, 78, 32)`,
127
+ '$allow.Anchor = $anchorBR',
128
+ '$allow.Add_Click({ $script:answer = 0; $form.Close() })',
129
+ '$form.Controls.Add($allow)',
130
+ '$form.AcceptButton = $allow',
131
+ '$script:answer = 1',
132
+ '[System.Windows.Forms.Application]::Run($form)',
133
+ 'exit $script:answer',
134
+ )
135
+
136
+ return lines.join("\n")
137
+ }
138
+
139
+ function run(command, args, timeoutMs, signal) {
140
+ return new Promise((resolve) => {
141
+ let child
142
+ try {
143
+ child = spawn(command, args, { stdio: ['ignore', 'pipe', 'pipe'], windowsHide: false })
144
+ } catch {
145
+ resolve({ code: null, stdout: '', missing: true })
146
+ return
147
+ }
148
+
149
+ let stdout = ''
150
+ let settled = false
151
+ let timer = null
152
+
153
+ const finish = (result) => {
154
+ if (settled) return
155
+ settled = true
156
+ if (timer !== null) clearTimeout(timer)
157
+ if (signal !== undefined) signal.removeEventListener?.('abort', onAbort)
158
+ resolve(result)
159
+ }
160
+
161
+ const onAbort = () => {
162
+ try {
163
+ child.kill('SIGKILL')
164
+ } catch {
165
+ /* 已经退出 */
166
+ }
167
+ finish({ code: null, stdout, aborted: true })
168
+ }
169
+
170
+ child.stdout?.on('data', (chunk) => {
171
+ stdout += String(chunk)
172
+ })
173
+ child.stderr?.on('data', () => {})
174
+
175
+ child.on('error', (error) => {
176
+ finish({ code: null, stdout, missing: error && error.code === 'ENOENT' })
177
+ })
178
+
179
+ child.on('close', (code) => {
180
+ finish({ code: typeof code === 'number' ? code : null, stdout })
181
+ })
182
+
183
+ timer = setTimeout(() => {
184
+ try {
185
+ child.kill('SIGKILL')
186
+ } catch {
187
+ /* 已经退出 */
188
+ }
189
+ finish({ code: null, stdout, timedOut: true })
190
+ }, timeoutMs)
191
+
192
+ if (signal !== undefined && typeof signal.addEventListener === 'function') {
193
+ if (signal.aborted) {
194
+ onAbort()
195
+ return
196
+ }
197
+ signal.addEventListener('abort', onAbort, { once: true })
198
+ }
199
+ })
200
+ }
201
+
202
+ async function runWindows(title, message, detail, timeoutMs, signal) {
203
+ // 走 -EncodedCommand:脚本以 UTF-16LE 传进去,绕开控制台代码页。
204
+ // 中文如果直接放在命令行或 ANSI 脚本里,PowerShell 会按系统代码页解码成乱码。
205
+ const encoded = Buffer.from(windowsScript(title, message, detail), 'utf16le').toString('base64')
206
+ const result = await run(
207
+ 'powershell.exe',
208
+ ['-NoProfile', '-NonInteractive', '-EncodedCommand', encoded],
209
+ timeoutMs,
210
+ signal,
211
+ )
212
+
213
+ if (result.missing) return { allowed: false, reason: 'unavailable' }
214
+ if (result.timedOut) return { allowed: false, reason: 'timeout' }
215
+ if (result.aborted) return { allowed: false, reason: 'aborted' }
216
+ return { allowed: result.code === 0, reason: 'answered' }
217
+ }
218
+
219
+ async function runPosix(platform, title, message, detail, timeoutMs, signal) {
220
+ const text = detail === ''
221
+ ? message
222
+ : `${message}\n\n${detail.length > 300 ? `${detail.slice(0, 300)}…` : detail}`
223
+
224
+ if (platform === 'darwin') {
225
+ const result = await run('osascript', [
226
+ '-e',
227
+ `display dialog ${appleString(text)} buttons {"拒绝","允许"} default button "允许" with title ${appleString(title)}`,
228
+ ], timeoutMs, signal)
229
+ if (result.missing) return { allowed: false, reason: 'unavailable' }
230
+ if (result.timedOut) return { allowed: false, reason: 'timeout' }
231
+ if (result.aborted) return { allowed: false, reason: 'aborted' }
232
+ return { allowed: result.stdout.includes('button returned:允许'), reason: 'answered' }
233
+ }
234
+
235
+ const answer = await run('zenity', [
236
+ '--question', `--title=${title}`, `--text=${text}`, '--ok-label=允许', '--cancel-label=拒绝', '--no-wrap',
237
+ ], timeoutMs, signal)
238
+
239
+ if (answer.missing) {
240
+ const fallback = await run('kdialog', [
241
+ '--title', title, '--yesno', text, '--yes-label', '允许', '--no-label', '拒绝',
242
+ ], timeoutMs, signal)
243
+ if (fallback.missing) return { allowed: false, reason: 'unavailable' }
244
+ if (fallback.timedOut) return { allowed: false, reason: 'timeout' }
245
+ if (fallback.aborted) return { allowed: false, reason: 'aborted' }
246
+ return { allowed: fallback.code === 0, reason: 'answered' }
247
+ }
248
+
249
+ if (answer.timedOut) return { allowed: false, reason: 'timeout' }
250
+ if (answer.aborted) return { allowed: false, reason: 'aborted' }
251
+ return { allowed: answer.code === 0, reason: 'answered' }
252
+ }
253
+
254
+ /**
255
+ * 弹一个「允许 / 拒绝」确认框。
256
+ *
257
+ * @param {object} request
258
+ * @param {string} request.title 窗口标题
259
+ * @param {string} request.message 标题行;调用方负责把应用名嵌进固定句式
260
+ * @param {string} [request.detail] 正文预览;空则不显示正文区
261
+ * @param {number} [request.timeoutMs] 等待上限,缺省 60 秒
262
+ * @param {AbortSignal} [request.signal] 调用方取消
263
+ * @returns {Promise<{allowed: boolean, reason: string}>} reason 便于记录
264
+ */
265
+ export async function confirm(request) {
266
+ const platform = detectPlatform()
267
+ const title = request.title ?? 'Relay'
268
+ const message = request.message ?? ''
269
+ const detail = clipDetail(request.detail ?? '')
270
+ const timeoutMs = Number.isFinite(request.timeoutMs) ? request.timeoutMs : DEFAULT_TIMEOUT_MS
271
+
272
+ if (platform === 'win32' || platform === 'wsl') {
273
+ return runWindows(title, message, detail, timeoutMs, request.signal)
274
+ }
275
+ return runPosix(platform, title, message, detail, timeoutMs, request.signal)
276
+ }
277
+
278
+ export { detectPlatform }
package/package.json CHANGED
@@ -1,6 +1,55 @@
1
1
  {
2
2
  "name": "@kir_sleeping/dsh-relay",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Relays local application requests to a DeepSeek Harness agent over HTTP.",
5
+ "license": "MIT",
6
+ "author": "sleeping",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/kir-sleeping/DSH-relay.git"
10
+ },
11
+ "homepage": "https://github.com/kir-sleeping/DSH-relay#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/kir-sleeping/DSH-relay/issues"
14
+ },
15
+ "keywords": [
16
+ "dsh",
17
+ "dsh-plugin",
18
+ "agent",
19
+ "http",
20
+ "relay",
21
+ "bridge"
22
+ ],
23
+ "type": "module",
24
+ "exports": {
25
+ ".": "./index.js",
26
+ "./client": "./client.js",
27
+ "./locale/*.json": "./locale/*.json",
28
+ "./icon": "./icon.svg"
29
+ },
30
+ "files": [
31
+ "index.js",
32
+ "client.js",
33
+ "native-dialog.js",
34
+ "cordis.patch.yml",
35
+ "locale/*.json",
36
+ "icon.svg",
37
+ "README.md",
38
+ "LICENSE"
39
+ ],
40
+ "engines": {
41
+ "dsh": ">=0.2.2-rc.1 <0.3.0"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "dsh": {
47
+ "bundle": {
48
+ "patch": "./cordis.patch.yml"
49
+ },
50
+ "client": {
51
+ "platform": "web",
52
+ "immediately": true
53
+ }
54
+ }
55
+ }