@kir_sleeping/dsh-relay 0.2.1 → 0.3.1
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 +27 -138
- package/client.js +56 -5
- package/docs/demo.html +869 -0
- package/docs/integration.md +224 -0
- package/docs/reference.md +160 -0
- package/docs/usage.md +130 -0
- package/icon.ico +0 -0
- package/icon.png +0 -0
- package/icon.svg +42 -7
- package/index.js +104 -18
- package/native-dialog.js +182 -36
- package/package.json +11 -3
package/index.js
CHANGED
|
@@ -36,7 +36,7 @@ import { confirm } from './native-dialog.js'
|
|
|
36
36
|
|
|
37
37
|
export const name = 'relay'
|
|
38
38
|
|
|
39
|
-
export const inject = ['webServer', 'agents', 'sessionQuery', 'workspaceRegistry', 'credentials']
|
|
39
|
+
export const inject = ['webServer', 'agents', 'sessionQuery', 'workspaceRegistry', 'credentials', 'sessionController']
|
|
40
40
|
|
|
41
41
|
/** 会话名与 source 共用的字符集;`~` 被留作 id 分隔符。 */
|
|
42
42
|
const NAME_PATTERN = /^[A-Za-z0-9_-]{1,64}$/u
|
|
@@ -45,6 +45,13 @@ const DEFAULT_TITLE = '外部任务'
|
|
|
45
45
|
|
|
46
46
|
const MAX_BODY_BYTES = 1024 * 1024
|
|
47
47
|
|
|
48
|
+
/** submit 要收图片,单独放宽;其它路由维持 1MB。 */
|
|
49
|
+
const MAX_SUBMIT_BODY_BYTES = 12 * 1024 * 1024
|
|
50
|
+
|
|
51
|
+
const MAX_IMAGES = 4
|
|
52
|
+
const MAX_IMAGE_BASE64 = 8 * 1024 * 1024
|
|
53
|
+
const IMAGE_TYPES = new Set(['image/png', 'image/jpeg', 'image/webp', 'image/gif'])
|
|
54
|
+
|
|
48
55
|
/** 凭证记录的 key 前缀。 */
|
|
49
56
|
const SOURCE_PREFIX = 'relay/'
|
|
50
57
|
|
|
@@ -312,7 +319,7 @@ export function apply(ctx, config) {
|
|
|
312
319
|
* 用户放行一次,不等于这句话是用户说的。后台发起的消息无论如何都不会被
|
|
313
320
|
* 标记成用户发言——包括用户点过允许的情况。
|
|
314
321
|
*/
|
|
315
|
-
async function admitMessage(source, record, trigger, content) {
|
|
322
|
+
async function admitMessage(source, record, trigger, content, imageCount) {
|
|
316
323
|
if (record.level === 0) {
|
|
317
324
|
return { ok: false, status: 403, code: 'ERR_ACCESS_DENIED', message: '这个来源处于禁止名单。' }
|
|
318
325
|
}
|
|
@@ -320,10 +327,11 @@ export function apply(ctx, config) {
|
|
|
320
327
|
const needsConfirm = record.level === 1 || (record.level === 2 && trigger !== 'user')
|
|
321
328
|
if (!needsConfirm) return { ok: true, approved: true }
|
|
322
329
|
|
|
330
|
+
const suffix = imageCount > 0 ? `\n\n(含 ${imageCount} 张图片)` : ''
|
|
323
331
|
const answer = await askUser(
|
|
324
332
|
trigger === 'user'
|
|
325
|
-
?
|
|
326
|
-
:
|
|
333
|
+
? `「${source}」请求发送一条消息,是否允许?${suffix}`
|
|
334
|
+
: `「${source}」在后台请求处理一条消息,是否允许?${suffix}`,
|
|
327
335
|
{ detail: content },
|
|
328
336
|
)
|
|
329
337
|
if (answer.reason !== 'answered') return dialogFailure(answer)
|
|
@@ -727,8 +735,10 @@ export function apply(ctx, config) {
|
|
|
727
735
|
function onInboxClaimed(payload) {
|
|
728
736
|
const agent = payload && payload.agent
|
|
729
737
|
const message = payload && payload.message
|
|
730
|
-
if (!agent || !message
|
|
731
|
-
const
|
|
738
|
+
if (!agent || !message) return
|
|
739
|
+
const rpcId = message.source && typeof message.source.rpcId === 'string' ? message.source.rpcId : undefined
|
|
740
|
+
if (rpcId === undefined) return
|
|
741
|
+
const task = byMessage.get(rpcId)
|
|
732
742
|
if (!task || task.state !== 'pending' || task.claimed) return
|
|
733
743
|
task.turn = payload.turn
|
|
734
744
|
task.claimed = true
|
|
@@ -808,7 +818,11 @@ export function apply(ctx, config) {
|
|
|
808
818
|
|
|
809
819
|
const events = snapshot && Array.isArray(snapshot.events) ? snapshot.events : []
|
|
810
820
|
const index = events.findIndex(
|
|
811
|
-
(event) => event
|
|
821
|
+
(event) => event
|
|
822
|
+
&& event.type === 'user/message'
|
|
823
|
+
&& event.data
|
|
824
|
+
&& event.data.source
|
|
825
|
+
&& event.data.source.rpcId === id,
|
|
812
826
|
)
|
|
813
827
|
if (index < 0) return null
|
|
814
828
|
|
|
@@ -897,13 +911,13 @@ export function apply(ctx, config) {
|
|
|
897
911
|
sendJson(res, status, { code, message }, origin)
|
|
898
912
|
}
|
|
899
913
|
|
|
900
|
-
function readJson(req) {
|
|
914
|
+
function readJson(req, limit = MAX_BODY_BYTES) {
|
|
901
915
|
return new Promise((resolve, reject) => {
|
|
902
916
|
const chunks = []
|
|
903
917
|
let size = 0
|
|
904
918
|
req.on('data', (chunk) => {
|
|
905
919
|
size += chunk.length
|
|
906
|
-
if (size >
|
|
920
|
+
if (size > limit) {
|
|
907
921
|
reject(new Error('request body too large'))
|
|
908
922
|
req.destroy()
|
|
909
923
|
return
|
|
@@ -1101,10 +1115,64 @@ export function apply(ctx, config) {
|
|
|
1101
1115
|
}
|
|
1102
1116
|
}
|
|
1103
1117
|
|
|
1118
|
+
/** 校验客户端发来的图片数组,转成 prompt 的 content parts。 */
|
|
1119
|
+
function buildImageParts(input) {
|
|
1120
|
+
if (input === undefined) return []
|
|
1121
|
+
if (!Array.isArray(input) || input.length === 0) {
|
|
1122
|
+
throw new RelayError('ERR_IMAGE', 'images 必须是非空数组。', 400)
|
|
1123
|
+
}
|
|
1124
|
+
if (input.length > MAX_IMAGES) {
|
|
1125
|
+
throw new RelayError('ERR_IMAGE_COUNT', `一次最多 ${MAX_IMAGES} 张图片。`, 400)
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
const parts = []
|
|
1129
|
+
for (const [index, item] of input.entries()) {
|
|
1130
|
+
if (!item || typeof item !== 'object') {
|
|
1131
|
+
throw new RelayError('ERR_IMAGE', `第 ${index + 1} 张图片不是对象。`, 400)
|
|
1132
|
+
}
|
|
1133
|
+
if (typeof item.mediaType !== 'string' || !IMAGE_TYPES.has(item.mediaType)) {
|
|
1134
|
+
throw new RelayError(
|
|
1135
|
+
'ERR_IMAGE_TYPE',
|
|
1136
|
+
`第 ${index + 1} 张图片的 mediaType 只支持 ${[...IMAGE_TYPES].join(' / ')}。`,
|
|
1137
|
+
400,
|
|
1138
|
+
)
|
|
1139
|
+
}
|
|
1140
|
+
if (typeof item.data !== 'string' || item.data === '') {
|
|
1141
|
+
throw new RelayError('ERR_IMAGE', `第 ${index + 1} 张图片缺少 base64 数据。`, 400)
|
|
1142
|
+
}
|
|
1143
|
+
if (item.data.length > MAX_IMAGE_BASE64) {
|
|
1144
|
+
throw new RelayError('ERR_IMAGE_SIZE', `第 ${index + 1} 张图片超过大小上限。`, 413)
|
|
1145
|
+
}
|
|
1146
|
+
parts.push({ type: 'image', mediaType: item.mediaType, data: item.data })
|
|
1147
|
+
}
|
|
1148
|
+
return parts
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
/** 把 sessionController 抛出的 RemoteError 翻成我们自己的错误码。 */
|
|
1152
|
+
function mapPromptError(error) {
|
|
1153
|
+
const code = error && typeof error.code === 'string' ? error.code : ''
|
|
1154
|
+
const reason = error && error.details && typeof error.details.reason === 'string' ? error.details.reason : ''
|
|
1155
|
+
const message = error && error.message ? String(error.message) : '消息投递失败。'
|
|
1156
|
+
|
|
1157
|
+
if (code === 'session/attachment-invalid') {
|
|
1158
|
+
if (reason === 'MODEL_DOES_NOT_SUPPORT_IMAGES') {
|
|
1159
|
+
return { status: 415, code: 'ERR_MODEL_NO_IMAGE', message: '当前模型不支持图片输入。' }
|
|
1160
|
+
}
|
|
1161
|
+
return { status: 413, code: 'ERR_IMAGE_REJECTED', message }
|
|
1162
|
+
}
|
|
1163
|
+
if (code === 'gateway/bad-request') {
|
|
1164
|
+
return { status: 400, code: 'ERR_BAD_REQUEST', message }
|
|
1165
|
+
}
|
|
1166
|
+
if (code === 'session/agent-busy') {
|
|
1167
|
+
return { status: 409, code: 'ERR_SESSION_BUSY', message }
|
|
1168
|
+
}
|
|
1169
|
+
return { status: 500, code: 'ERR_DELIVERY', message }
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1104
1172
|
async function handleSubmit(req, res, origin) {
|
|
1105
1173
|
let body
|
|
1106
1174
|
try {
|
|
1107
|
-
body = await readJson(req)
|
|
1175
|
+
body = await readJson(req, MAX_SUBMIT_BODY_BYTES)
|
|
1108
1176
|
} catch {
|
|
1109
1177
|
fail(res, 400, 'ERR_REQUEST_JSON', '请求体不是合法的 JSON 对象。', origin)
|
|
1110
1178
|
return
|
|
@@ -1151,7 +1219,10 @@ export function apply(ctx, config) {
|
|
|
1151
1219
|
fail(res, 401, 'ERR_INPUT_FINGERPRINT', '来源与凭证不匹配。', origin)
|
|
1152
1220
|
return
|
|
1153
1221
|
}
|
|
1154
|
-
gate = await admitMessage(
|
|
1222
|
+
gate = await admitMessage(
|
|
1223
|
+
source, verified.record, trigger, content,
|
|
1224
|
+
Array.isArray(body.images) ? body.images.length : 0,
|
|
1225
|
+
)
|
|
1155
1226
|
} catch (error) {
|
|
1156
1227
|
fail(res, 500, 'ERR_INTERNAL', error && error.message ? String(error.message) : '授权检查失败。', origin)
|
|
1157
1228
|
return
|
|
@@ -1202,19 +1273,34 @@ export function apply(ctx, config) {
|
|
|
1202
1273
|
tasks.set(id, task)
|
|
1203
1274
|
byMessage.set(id, task)
|
|
1204
1275
|
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1276
|
+
let imageParts
|
|
1277
|
+
try {
|
|
1278
|
+
imageParts = buildImageParts(body.images)
|
|
1279
|
+
} catch (error) {
|
|
1280
|
+
tasks.delete(id)
|
|
1281
|
+
byMessage.delete(id)
|
|
1282
|
+
const failure = error instanceof RelayError ? error : new RelayError('ERR_IMAGE', '图片参数不合法。', 400)
|
|
1283
|
+
fail(res, failure.status, failure.code, failure.message, origin)
|
|
1284
|
+
return
|
|
1210
1285
|
}
|
|
1211
1286
|
|
|
1287
|
+
const parts = [
|
|
1288
|
+
{ type: 'text', text: serialize(content, source, trigger, gate.approved) },
|
|
1289
|
+
...imageParts,
|
|
1290
|
+
]
|
|
1291
|
+
|
|
1212
1292
|
try {
|
|
1213
|
-
|
|
1293
|
+
// 走 sessionController:模型能力检查、附件准入、图片串行化都在里面,
|
|
1294
|
+
// 比自己去拼 UserMessage 少三处容易漏的地方。
|
|
1295
|
+
await ctx.sessionController.prompt(
|
|
1296
|
+
{ requestId: id, sessionId: sessionName, mode: 'queue', content: parts },
|
|
1297
|
+
new AbortController().signal,
|
|
1298
|
+
)
|
|
1214
1299
|
} catch (error) {
|
|
1215
1300
|
tasks.delete(id)
|
|
1216
1301
|
byMessage.delete(id)
|
|
1217
|
-
|
|
1302
|
+
const failure = mapPromptError(error)
|
|
1303
|
+
fail(res, failure.status, failure.code, failure.message, origin)
|
|
1218
1304
|
return
|
|
1219
1305
|
}
|
|
1220
1306
|
|
package/native-dialog.js
CHANGED
|
@@ -10,12 +10,44 @@
|
|
|
10
10
|
|
|
11
11
|
import { spawn } from 'node:child_process'
|
|
12
12
|
import { readFileSync } from 'node:fs'
|
|
13
|
+
import { fileURLToPath } from 'node:url'
|
|
13
14
|
|
|
14
15
|
const DEFAULT_TIMEOUT_MS = 60_000
|
|
15
16
|
|
|
16
17
|
/** 塞进系统窗口的正文上限,避免命令过长;超出部分在窗口里也读不过来。 */
|
|
17
18
|
const DETAIL_LIMIT = 4000
|
|
18
19
|
|
|
20
|
+
/** 弹窗用的图标:Windows 吃 .ico,其它平台用 .png。 */
|
|
21
|
+
const ICON_ICO = fileURLToPath(new URL('./icon.ico', import.meta.url))
|
|
22
|
+
const ICON_PNG = fileURLToPath(new URL('./icon.png', import.meta.url))
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 给对话框算一个它能读懂的图标路径。
|
|
26
|
+
*
|
|
27
|
+
* 纯 Windows 直接用包里的路径。WSL 下插件跑在 Linux、弹窗由 Windows 进程渲染,
|
|
28
|
+
* 必须换成 \\wsl.localhost\<发行版>\... 这种共享路径才读得到。
|
|
29
|
+
* 拿不到发行版名就返回 undefined,图标缺席不影响弹窗。
|
|
30
|
+
*/
|
|
31
|
+
/** WSL 下把 Linux 路径换成 Windows 进程读得到的共享路径;取不到发行版名就放弃。 */
|
|
32
|
+
function windowsReadable(file) {
|
|
33
|
+
const distro = process.env.WSL_DISTRO_NAME
|
|
34
|
+
if (typeof distro !== 'string' || distro === '') return undefined
|
|
35
|
+
return '\\\\wsl.localhost\\' + distro + file.replaceAll('/', '\\')
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function dialogIcon(platform) {
|
|
39
|
+
if (platform === 'win32') return ICON_ICO
|
|
40
|
+
if (platform === 'wsl') return windowsReadable(ICON_ICO)
|
|
41
|
+
return ICON_PNG
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 标题栏里的大图标。POSIX 对话框没有这个位置,返回 undefined。 */
|
|
45
|
+
function dialogLogo(platform) {
|
|
46
|
+
if (platform === 'win32') return ICON_PNG
|
|
47
|
+
if (platform === 'wsl') return windowsReadable(ICON_PNG)
|
|
48
|
+
return undefined
|
|
49
|
+
}
|
|
50
|
+
|
|
19
51
|
/** 检测 WSL:弹窗要交回 Windows 宿主,WSL 里没有可用的图形对话框。 */
|
|
20
52
|
function detectPlatform() {
|
|
21
53
|
if (process.platform !== 'linux') return process.platform
|
|
@@ -47,69 +79,172 @@ function clipDetail(detail) {
|
|
|
47
79
|
* 右下角置顶小窗口:标题 + 正文预览 + 展开/收起 + 允许/拒绝。
|
|
48
80
|
* 正文可滚动,窗口可以长高到接近整屏;退出码就是答案。
|
|
49
81
|
*/
|
|
50
|
-
function windowsScript(title, message, detail) {
|
|
82
|
+
function windowsScript(title, message, detail, iconPath, logoPath) {
|
|
51
83
|
const hasDetail = detail !== ''
|
|
52
|
-
const width =
|
|
53
|
-
const
|
|
54
|
-
const
|
|
84
|
+
const width = 420
|
|
85
|
+
const barHeight = 56
|
|
86
|
+
const headerY = barHeight + 14
|
|
87
|
+
const headerHeight = hasDetail ? 46 : 44
|
|
88
|
+
const textY = headerY + headerHeight
|
|
89
|
+
const textHeight = 62
|
|
90
|
+
const buttonHeight = 34
|
|
91
|
+
const bottomPad = 16
|
|
92
|
+
const baseHeight = hasDetail
|
|
93
|
+
? textY + textHeight + 14 + buttonHeight + bottomPad
|
|
94
|
+
: headerY + headerHeight + 12 + buttonHeight + bottomPad
|
|
95
|
+
const buttonY = baseHeight - bottomPad - buttonHeight
|
|
96
|
+
const buttonsTop = bottomPad + buttonHeight
|
|
55
97
|
|
|
56
98
|
const lines = [
|
|
57
99
|
'Add-Type -AssemblyName System.Windows.Forms',
|
|
58
100
|
'Add-Type -AssemblyName System.Drawing',
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
101
|
+
`$ink = [System.Drawing.ColorTranslator]::FromHtml('#111827')`,
|
|
102
|
+
`$muted = [System.Drawing.ColorTranslator]::FromHtml('#6B7280')`,
|
|
103
|
+
`$line = [System.Drawing.ColorTranslator]::FromHtml('#E5E7EB')`,
|
|
104
|
+
`$field = [System.Drawing.ColorTranslator]::FromHtml('#F3F4F6')`,
|
|
105
|
+
`$accent = [System.Drawing.ColorTranslator]::FromHtml('#2563EB')`,
|
|
63
106
|
'$form = New-Object System.Windows.Forms.Form',
|
|
64
107
|
`$form.Text = ${powerShellString(title)}`,
|
|
65
|
-
'$form.
|
|
66
|
-
'$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle',
|
|
108
|
+
'$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::None',
|
|
67
109
|
'$form.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual',
|
|
68
110
|
'$form.TopMost = $true',
|
|
69
111
|
'$form.ShowInTaskbar = $true',
|
|
70
|
-
'$form.
|
|
71
|
-
'$form.
|
|
112
|
+
'$form.BackColor = [System.Drawing.Color]::White',
|
|
113
|
+
'$form.Font = New-Object System.Drawing.Font("Microsoft YaHei UI", 10)',
|
|
114
|
+
// 展开动画逐帧改尺寸,不双缓冲会闪
|
|
115
|
+
'$flags = [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic',
|
|
116
|
+
'$buffered = [System.Windows.Forms.Form].GetProperty("DoubleBuffered", $flags)',
|
|
117
|
+
'if ($null -ne $buffered) { $buffered.SetValue($form, $true) }',
|
|
72
118
|
`$form.ClientSize = New-Object System.Drawing.Size(${width}, ${baseHeight})`,
|
|
73
119
|
'$area = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea',
|
|
74
120
|
'$form.Left = $area.Right - $form.Width - 24',
|
|
75
121
|
'$form.Top = $area.Bottom - $form.Height - 24',
|
|
122
|
+
// 圆角窗口:WinForms 没有原生圆角,裁一个圆角 Region;尺寸变了要重算
|
|
123
|
+
'function Set-Rounded {',
|
|
124
|
+
' $p = New-Object System.Drawing.Drawing2D.GraphicsPath',
|
|
125
|
+
' $r = 12; $w = $form.ClientSize.Width; $h = $form.ClientSize.Height',
|
|
126
|
+
' $p.AddArc(0, 0, $r * 2, $r * 2, 180, 90)',
|
|
127
|
+
' $p.AddArc($w - $r * 2, 0, $r * 2, $r * 2, 270, 90)',
|
|
128
|
+
' $p.AddArc($w - $r * 2, $h - $r * 2, $r * 2, $r * 2, 0, 90)',
|
|
129
|
+
' $p.AddArc(0, $h - $r * 2, $r * 2, $r * 2, 90, 90)',
|
|
130
|
+
' $p.CloseFigure()',
|
|
131
|
+
' $form.Region = New-Object System.Drawing.Region($p)',
|
|
132
|
+
' $p.Dispose()',
|
|
133
|
+
'}',
|
|
134
|
+
'Set-Rounded',
|
|
135
|
+
// 统一的高度变更入口:文本框、按钮、圆角、贴底一次改完
|
|
136
|
+
'function Set-Height([int]$h) {',
|
|
137
|
+
' $form.SuspendLayout()',
|
|
138
|
+
` $form.ClientSize = New-Object System.Drawing.Size(${width}, $h)`,
|
|
139
|
+
` $text.Height = $h - ${textY} - ${buttonsTop + 14}`,
|
|
140
|
+
` $allow.Top = $h - ${buttonsTop}`,
|
|
141
|
+
' $deny.Top = $allow.Top',
|
|
142
|
+
' $expand.Top = $allow.Top',
|
|
143
|
+
' $form.ResumeLayout($true)',
|
|
144
|
+
' Set-Rounded',
|
|
145
|
+
' $form.Top = $area.Bottom - $form.Height - 24',
|
|
146
|
+
'}',
|
|
147
|
+
// 标题栏:无边框窗口自己提供拖动和关闭
|
|
148
|
+
'$bar = New-Object System.Windows.Forms.Panel',
|
|
149
|
+
`$bar.SetBounds(0, 0, ${width}, ${barHeight})`,
|
|
150
|
+
'$bar.BackColor = [System.Drawing.Color]::White',
|
|
151
|
+
'$form.Controls.Add($bar)',
|
|
152
|
+
]
|
|
153
|
+
|
|
154
|
+
if (typeof logoPath === 'string' && logoPath !== '') {
|
|
155
|
+
lines.push(
|
|
156
|
+
'$logo = New-Object System.Windows.Forms.PictureBox',
|
|
157
|
+
'$logo.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::Zoom',
|
|
158
|
+
'$logo.BackColor = [System.Drawing.Color]::White',
|
|
159
|
+
`$logo.SetBounds(18, 12, 32, 32)`,
|
|
160
|
+
`try { $logo.Image = [System.Drawing.Image]::FromFile(${powerShellString(logoPath)}) } catch { $logo.Visible = $false }`,
|
|
161
|
+
'$bar.Controls.Add($logo)',
|
|
162
|
+
'$titleX = 62',
|
|
163
|
+
)
|
|
164
|
+
} else {
|
|
165
|
+
lines.push('$titleX = 18')
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
lines.push(
|
|
169
|
+
'$titleLabel = New-Object System.Windows.Forms.Label',
|
|
170
|
+
`$titleLabel.Text = ${powerShellString(title)}`,
|
|
171
|
+
'$titleLabel.Font = New-Object System.Drawing.Font("Microsoft YaHei UI", 11, [System.Drawing.FontStyle]::Bold)',
|
|
172
|
+
'$titleLabel.ForeColor = $ink',
|
|
173
|
+
'$titleLabel.BackColor = [System.Drawing.Color]::Transparent',
|
|
174
|
+
'$titleLabel.SetBounds($titleX, 18, 260, 22)',
|
|
175
|
+
'$bar.Controls.Add($titleLabel)',
|
|
176
|
+
'$closeBtn = New-Object System.Windows.Forms.Button',
|
|
177
|
+
"$closeBtn.Text = 'X'",
|
|
178
|
+
'$closeBtn.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat',
|
|
179
|
+
'$closeBtn.FlatAppearance.BorderSize = 0',
|
|
180
|
+
'$closeBtn.ForeColor = $muted',
|
|
181
|
+
'$closeBtn.BackColor = [System.Drawing.Color]::White',
|
|
182
|
+
'$closeBtn.TabStop = $false',
|
|
183
|
+
`$closeBtn.SetBounds(${width} - 44, 16, 30, 26)`,
|
|
184
|
+
'$closeBtn.Add_Click({ $script:answer = 1; $form.Close() })',
|
|
185
|
+
'$bar.Controls.Add($closeBtn)',
|
|
186
|
+
'$script:drag = $false',
|
|
187
|
+
'$bar.Add_MouseDown({ param($s, $e) $script:drag = $true; $script:dragX = $e.X; $script:dragY = $e.Y })',
|
|
188
|
+
'$titleLabel.Add_MouseDown({ param($s, $e) $script:drag = $true; $script:dragX = $e.X; $script:dragY = $e.Y })',
|
|
189
|
+
'$bar.Add_MouseMove({ param($s, $e) if ($script:drag) { $form.Left += $e.X - $script:dragX; $form.Top += $e.Y - $script:dragY } })',
|
|
190
|
+
'$titleLabel.Add_MouseMove({ param($s, $e) if ($script:drag) { $form.Left += $e.X - $script:dragX; $form.Top += $e.Y - $script:dragY } })',
|
|
191
|
+
'$bar.Add_MouseUp({ param($s, $e) $script:drag = $false })',
|
|
192
|
+
'$titleLabel.Add_MouseUp({ param($s, $e) $script:drag = $false })',
|
|
193
|
+
'$divider = New-Object System.Windows.Forms.Panel',
|
|
194
|
+
`$divider.SetBounds(0, ${barHeight}, ${width}, 1)`,
|
|
195
|
+
'$divider.BackColor = $line',
|
|
196
|
+
'$form.Controls.Add($divider)',
|
|
197
|
+
// 来源名放最前:先看到"谁在问",再决定
|
|
76
198
|
'$header = New-Object System.Windows.Forms.Label',
|
|
77
199
|
`$header.Text = ${powerShellString(message)}`,
|
|
78
|
-
|
|
79
|
-
'$header.
|
|
200
|
+
'$header.Font = New-Object System.Drawing.Font("Microsoft YaHei UI", 10)',
|
|
201
|
+
'$header.ForeColor = $ink',
|
|
202
|
+
'$header.BackColor = [System.Drawing.Color]::Transparent',
|
|
203
|
+
`$header.SetBounds(18, ${headerY}, ${width - 36}, ${headerHeight})`,
|
|
80
204
|
'$form.Controls.Add($header)',
|
|
81
|
-
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
if (typeof iconPath === 'string' && iconPath !== '') {
|
|
208
|
+
// 任务栏图标;加载失败不该让弹窗打不开
|
|
209
|
+
lines.push(`try { $form.Icon = New-Object System.Drawing.Icon(${powerShellString(iconPath)}) } catch { }`)
|
|
210
|
+
}
|
|
82
211
|
|
|
83
212
|
if (hasDetail) {
|
|
84
213
|
lines.push(
|
|
85
214
|
'$text = New-Object System.Windows.Forms.TextBox',
|
|
86
215
|
'$text.Multiline = $true',
|
|
87
216
|
'$text.ReadOnly = $true',
|
|
88
|
-
'$text.ScrollBars = [System.Windows.Forms.ScrollBars]::Vertical',
|
|
89
217
|
'$text.WordWrap = $true',
|
|
90
|
-
'$text.
|
|
218
|
+
'$text.ScrollBars = [System.Windows.Forms.ScrollBars]::Vertical',
|
|
219
|
+
'$text.BorderStyle = [System.Windows.Forms.BorderStyle]::None',
|
|
220
|
+
'$text.BackColor = $field',
|
|
221
|
+
'$text.ForeColor = $muted',
|
|
91
222
|
`$text.Text = ${powerShellString(detail)}`,
|
|
92
|
-
`$text.SetBounds(
|
|
93
|
-
'$text.Anchor = $anchorAll',
|
|
223
|
+
`$text.SetBounds(18, ${textY}, ${width - 36}, ${textHeight})`,
|
|
94
224
|
'$form.Controls.Add($text)',
|
|
95
225
|
'$expand = New-Object System.Windows.Forms.Button',
|
|
96
226
|
"$expand.Text = '展开全文'",
|
|
97
|
-
|
|
98
|
-
'$expand.
|
|
227
|
+
'$expand.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat',
|
|
228
|
+
'$expand.FlatAppearance.BorderSize = 1',
|
|
229
|
+
'$expand.FlatAppearance.BorderColor = $line',
|
|
230
|
+
'$expand.ForeColor = $muted',
|
|
231
|
+
'$expand.BackColor = [System.Drawing.Color]::White',
|
|
232
|
+
`$expand.SetBounds(18, ${buttonY}, 96, ${buttonHeight})`,
|
|
99
233
|
'$form.Controls.Add($expand)',
|
|
100
234
|
'$script:expanded = $false',
|
|
101
235
|
'$expand.Add_Click({',
|
|
102
236
|
' if (-not $script:expanded) {',
|
|
103
|
-
'
|
|
104
|
-
|
|
105
|
-
|
|
237
|
+
' # 展开有上限:最多到屏幕工作区的 60%,再长就靠文本框自己滚',
|
|
238
|
+
' $cap = [int]($area.Height * 0.6)',
|
|
239
|
+
` $target = [Math]::Max(${baseHeight}, [Math]::Min($cap, $area.Height - 120))`,
|
|
106
240
|
' $script:expanded = $true',
|
|
241
|
+
" $expand.Text = '收起'",
|
|
107
242
|
' } else {',
|
|
108
|
-
` $
|
|
109
|
-
" $expand.Text = '展开全文'",
|
|
243
|
+
` $target = ${baseHeight}`,
|
|
110
244
|
' $script:expanded = $false',
|
|
245
|
+
" $expand.Text = '展开全文'",
|
|
111
246
|
' }',
|
|
112
|
-
'
|
|
247
|
+
' Set-Height $target',
|
|
113
248
|
'})',
|
|
114
249
|
)
|
|
115
250
|
}
|
|
@@ -117,14 +252,21 @@ function windowsScript(title, message, detail) {
|
|
|
117
252
|
lines.push(
|
|
118
253
|
'$deny = New-Object System.Windows.Forms.Button',
|
|
119
254
|
"$deny.Text = '拒绝'",
|
|
120
|
-
|
|
121
|
-
'$deny.
|
|
255
|
+
'$deny.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat',
|
|
256
|
+
'$deny.FlatAppearance.BorderSize = 1',
|
|
257
|
+
'$deny.FlatAppearance.BorderColor = $line',
|
|
258
|
+
'$deny.ForeColor = $ink',
|
|
259
|
+
'$deny.BackColor = [System.Drawing.Color]::White',
|
|
260
|
+
`$deny.SetBounds(${width} - 186, ${buttonY}, 78, ${buttonHeight})`,
|
|
122
261
|
'$deny.Add_Click({ $script:answer = 1; $form.Close() })',
|
|
123
262
|
'$form.Controls.Add($deny)',
|
|
124
263
|
'$allow = New-Object System.Windows.Forms.Button',
|
|
125
264
|
"$allow.Text = '允许'",
|
|
126
|
-
|
|
127
|
-
'$allow.
|
|
265
|
+
'$allow.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat',
|
|
266
|
+
'$allow.FlatAppearance.BorderSize = 0',
|
|
267
|
+
'$allow.ForeColor = [System.Drawing.Color]::White',
|
|
268
|
+
'$allow.BackColor = $accent',
|
|
269
|
+
`$allow.SetBounds(${width} - 98, ${buttonY}, 78, ${buttonHeight})`,
|
|
128
270
|
'$allow.Add_Click({ $script:answer = 0; $form.Close() })',
|
|
129
271
|
'$form.Controls.Add($allow)',
|
|
130
272
|
'$form.AcceptButton = $allow',
|
|
@@ -199,10 +341,10 @@ function run(command, args, timeoutMs, signal) {
|
|
|
199
341
|
})
|
|
200
342
|
}
|
|
201
343
|
|
|
202
|
-
async function runWindows(title, message, detail, timeoutMs, signal) {
|
|
344
|
+
async function runWindows(title, message, detail, timeoutMs, signal, iconPath, logoPath) {
|
|
203
345
|
// 走 -EncodedCommand:脚本以 UTF-16LE 传进去,绕开控制台代码页。
|
|
204
346
|
// 中文如果直接放在命令行或 ANSI 脚本里,PowerShell 会按系统代码页解码成乱码。
|
|
205
|
-
const encoded = Buffer.from(windowsScript(title, message, detail), 'utf16le').toString('base64')
|
|
347
|
+
const encoded = Buffer.from(windowsScript(title, message, detail, iconPath, logoPath), 'utf16le').toString('base64')
|
|
206
348
|
const result = await run(
|
|
207
349
|
'powershell.exe',
|
|
208
350
|
['-NoProfile', '-NonInteractive', '-EncodedCommand', encoded],
|
|
@@ -216,7 +358,7 @@ async function runWindows(title, message, detail, timeoutMs, signal) {
|
|
|
216
358
|
return { allowed: result.code === 0, reason: 'answered' }
|
|
217
359
|
}
|
|
218
360
|
|
|
219
|
-
async function runPosix(platform, title, message, detail, timeoutMs, signal) {
|
|
361
|
+
async function runPosix(platform, title, message, detail, timeoutMs, signal, iconPath) {
|
|
220
362
|
const text = detail === ''
|
|
221
363
|
? message
|
|
222
364
|
: `${message}\n\n${detail.length > 300 ? `${detail.slice(0, 300)}…` : detail}`
|
|
@@ -234,11 +376,13 @@ async function runPosix(platform, title, message, detail, timeoutMs, signal) {
|
|
|
234
376
|
|
|
235
377
|
const answer = await run('zenity', [
|
|
236
378
|
'--question', `--title=${title}`, `--text=${text}`, '--ok-label=允许', '--cancel-label=拒绝', '--no-wrap',
|
|
379
|
+
...(typeof iconPath === 'string' ? [`--window-icon=${iconPath}`] : []),
|
|
237
380
|
], timeoutMs, signal)
|
|
238
381
|
|
|
239
382
|
if (answer.missing) {
|
|
240
383
|
const fallback = await run('kdialog', [
|
|
241
384
|
'--title', title, '--yesno', text, '--yes-label', '允许', '--no-label', '拒绝',
|
|
385
|
+
...(typeof iconPath === 'string' ? ['--icon', iconPath] : []),
|
|
242
386
|
], timeoutMs, signal)
|
|
243
387
|
if (fallback.missing) return { allowed: false, reason: 'unavailable' }
|
|
244
388
|
if (fallback.timedOut) return { allowed: false, reason: 'timeout' }
|
|
@@ -269,10 +413,12 @@ export async function confirm(request) {
|
|
|
269
413
|
const detail = clipDetail(request.detail ?? '')
|
|
270
414
|
const timeoutMs = Number.isFinite(request.timeoutMs) ? request.timeoutMs : DEFAULT_TIMEOUT_MS
|
|
271
415
|
|
|
416
|
+
const iconPath = dialogIcon(platform)
|
|
417
|
+
const logoPath = dialogLogo(platform)
|
|
272
418
|
if (platform === 'win32' || platform === 'wsl') {
|
|
273
|
-
return runWindows(title, message, detail, timeoutMs, request.signal)
|
|
419
|
+
return runWindows(title, message, detail, timeoutMs, request.signal, iconPath, logoPath)
|
|
274
420
|
}
|
|
275
|
-
return runPosix(platform, title, message, detail, timeoutMs, request.signal)
|
|
421
|
+
return runPosix(platform, title, message, detail, timeoutMs, request.signal, iconPath)
|
|
276
422
|
}
|
|
277
423
|
|
|
278
424
|
export { detectPlatform }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kir_sleeping/dsh-relay",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Relays local application requests to a DeepSeek Harness agent over HTTP.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "sleeping",
|
|
@@ -25,7 +25,9 @@
|
|
|
25
25
|
".": "./index.js",
|
|
26
26
|
"./client": "./client.js",
|
|
27
27
|
"./locale/*.json": "./locale/*.json",
|
|
28
|
-
"./icon": "./icon.svg"
|
|
28
|
+
"./icon": "./icon.svg",
|
|
29
|
+
"./icon.ico": "./icon.ico",
|
|
30
|
+
"./icon.png": "./icon.png"
|
|
29
31
|
},
|
|
30
32
|
"files": [
|
|
31
33
|
"index.js",
|
|
@@ -35,7 +37,13 @@
|
|
|
35
37
|
"locale/*.json",
|
|
36
38
|
"icon.svg",
|
|
37
39
|
"README.md",
|
|
38
|
-
"LICENSE"
|
|
40
|
+
"LICENSE",
|
|
41
|
+
"icon.ico",
|
|
42
|
+
"icon.png",
|
|
43
|
+
"docs/usage.md",
|
|
44
|
+
"docs/integration.md",
|
|
45
|
+
"docs/reference.md",
|
|
46
|
+
"docs/demo.html"
|
|
39
47
|
],
|
|
40
48
|
"engines": {
|
|
41
49
|
"dsh": ">=0.2.2-rc.1 <0.3.0"
|