@kir_sleeping/dsh-relay 0.2.0 → 0.3.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/README.md +34 -75
- package/client.js +56 -5
- 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 +7 -3
package/README.md
CHANGED
|
@@ -1,55 +1,63 @@
|
|
|
1
1
|
# Relay
|
|
2
|
+
<p align="center">
|
|
3
|
+
<img src="https://raw.githubusercontent.com/kir-sleeping/DSH-relay/main/docs/icon/relay-lockup-512.png" width="200" alt="DSH-Relay">
|
|
4
|
+
</p>
|
|
2
5
|
|
|
3
|
-
|
|
6
|
+
dsh-relay 是一个 DSH 入口中继器,把 DSH 的调用能力开放给本机应用。
|
|
7
|
+
|
|
8
|
+
让你的任何应用轻松接入 DSH。
|
|
4
9
|
|
|
5
10
|
应用不需要内置 AI、不需要自己管会话、不需要理解 agent 的内部结构——像调一个普通 HTTP API 一样把任务交出去,拿回结果。所有调用共享同一份上下文,所以 agent 在邮件应用里知道你在便签里写过什么。
|
|
6
11
|
|
|
7
12
|
```
|
|
8
13
|
应用 A ─┐
|
|
9
|
-
应用 B ─┼─→ Relay ─→ DSH agent ─→
|
|
14
|
+
应用 B ─┼─→ Relay ─→ DSH agent ─→ 轻松接入
|
|
10
15
|
应用 C ─┘
|
|
11
16
|
```
|
|
12
17
|
|
|
13
18
|
---
|
|
14
19
|
|
|
20
|
+
**从这里开始**
|
|
21
|
+
|
|
22
|
+
- 我只是想用 → [使用说明](https://github.com/kir-sleeping/DSH-relay/blob/main/docs/usage.md)
|
|
23
|
+
- 我要写一个应用接进来 → [外部接入指南](https://github.com/kir-sleeping/DSH-relay/blob/main/docs/integration.md) · [可运行示例](https://github.com/kir-sleeping/DSH-relay/blob/main/docs/demo.html)
|
|
24
|
+
- 我要改这个插件 → [开发者参考](https://github.com/kir-sleeping/DSH-relay/blob/main/docs/reference.md)
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
15
28
|
## 它解决什么
|
|
16
29
|
|
|
17
30
|
现在的 AI 是每个应用各配一个,彼此失忆:你在每个里面都要重新解释一遍自己。Relay 把这件事反过来——**agent 在应用外面,应用来调它**。
|
|
18
31
|
|
|
19
32
|
由此得到三件单应用做不到的事:
|
|
20
33
|
|
|
21
|
-
-
|
|
34
|
+
- **上下文统一**:谁说过的话都在一条线上,不用重复交代,并且我相信,在接入应用足够丰富后,这一点会变得极为重要。
|
|
22
35
|
- **应用不需要内置 AI**:接进来只改一个请求,模型、工具、权限、压缩都不用自己实现。
|
|
23
36
|
- **应用可以把数据交出去**:不是让用户复制粘贴,而是把结构化数据连同任务一起发过去。
|
|
24
37
|
|
|
25
38
|
---
|
|
26
39
|
|
|
27
|
-
##
|
|
40
|
+
## 玩法
|
|
28
41
|
|
|
29
|
-
|
|
30
|
-
|---|---|
|
|
31
|
-
| 入口 | `POST /x/relay/submit`,可配置前缀,挂在 DSH 现有端口上 |
|
|
32
|
-
| 交付 | 同步等 / 稍后取 / SSE 流式 |
|
|
33
|
-
| 鉴权 | 凭证 + 0–3 级授权,首次接入需用户在系统原生确认框里点「允许」 |
|
|
34
|
-
| 会话 | 默认 `main`,全部落在独立的 Relay 工作区里 |
|
|
35
|
-
| 区分发起方 | `trigger: user \| app`——用户点的,还是应用自己发的 |
|
|
42
|
+
- 浏览器扩展:看到一篇值得留的文章,点一下「收进知识库」,扩展就把当前文章发给 DSH,让它整理成笔记并存进接好的知识库。全程不用复制粘贴。
|
|
36
43
|
|
|
37
|
-
|
|
44
|
+
- PPT 插件: 在第三页的流程图旁批注「把这一步拆成两步」,插件把页码、对象和批注一起发给 DSH,让它用接入的 PPT 工具去改。你可以留在 PPT 里检查改完的效果。
|
|
45
|
+
|
|
46
|
+
我期待有更多更大胆的想法
|
|
38
47
|
|
|
48
|
+
---
|
|
39
49
|
## 安装
|
|
40
50
|
|
|
41
51
|
```bash
|
|
42
52
|
dsh plugin add @kir_sleeping/dsh-relay
|
|
43
53
|
```
|
|
44
54
|
|
|
45
|
-
开发时用软链安装(`dsh plugin add link:<本机源码目录>`),改代码即生效,不用反复打包。
|
|
46
|
-
|
|
47
|
-
> 开发与发布的注意事项写在 `index.js` 头部注释里,改代码前先看那段。
|
|
48
|
-
|
|
49
55
|
安装后插件自动加载,并在 `$DSH_HOME/relay/workspace` 下建一个名为「外部任务」的会话(目录权限 700)。
|
|
50
56
|
|
|
51
57
|
首次创建时会写一份 `AGENTS.md`——它告诉 agent:这些消息是谁发的、`trigger` / `approved` 各是什么意思、哪些内容要当不可信数据处理、回答会返回给谁。用户可以随便改,改过之后插件不会覆盖(只在文件不存在时写入)。
|
|
52
58
|
|
|
59
|
+
**权限给到级别 3 就是全权限**:能读写文件、执行命令、联网。装之前先想清楚给谁开。详见[使用说明](https://github.com/kir-sleeping/DSH-relay/blob/main/docs/usage.md)。
|
|
60
|
+
|
|
53
61
|
---
|
|
54
62
|
|
|
55
63
|
## 快速开始
|
|
@@ -71,73 +79,24 @@ curl -N -X POST http://127.0.0.1:3080/x/relay/submit \
|
|
|
71
79
|
-d '{"content":"介绍一下这个项目","source":"my-app","fingerprint":"…","trigger":"user","stream":true}'
|
|
72
80
|
```
|
|
73
81
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
---
|
|
77
|
-
|
|
78
|
-
## 配置
|
|
79
|
-
|
|
80
|
-
在 profile 的 `cordis.patch.yml` 里那一行插件的 `config` 中设置,全部可选:
|
|
82
|
+
## 文档
|
|
81
83
|
|
|
82
|
-
|
|
|
84
|
+
| 文件 | 读者 | 内容 |
|
|
83
85
|
|---|---|---|
|
|
84
|
-
|
|
|
85
|
-
|
|
|
86
|
-
|
|
|
87
|
-
|
|
|
88
|
-
| `queueLimitMs` | `600000` | 排队多久未开始就作废 |
|
|
89
|
-
| `maxWaitMs` | `3600000` | 单次等待上限 |
|
|
90
|
-
| `resultTtlMs` | `86400000` | 结果在内存里保留多久(过期后从会话日志重建) |
|
|
91
|
-
| `authTimeoutMs` | `60000` | 确认框等待上限 |
|
|
86
|
+
| [docs/integration.md](https://github.com/kir-sleeping/DSH-relay/blob/main/docs/integration.md) | 写外部接入的人 | 接入契约:session、trigger、后台任务、错误处理 |
|
|
87
|
+
| [docs/usage.md](https://github.com/kir-sleeping/DSH-relay/blob/main/docs/usage.md) | 普通用户 | 权限级别、弹窗什么时候出现、怎么管理 |
|
|
88
|
+
| [docs/reference.md](https://github.com/kir-sleeping/DSH-relay/blob/main/docs/reference.md) | 改插件的人 | 内部结构、不变式、扩展点 |
|
|
89
|
+
| [docs/demo.html](https://github.com/kir-sleeping/DSH-relay/blob/main/docs/demo.html) | 写外部接入的人 | 可运行的单文件示例:收件箱 + 每日小报 + 流式对话 |
|
|
92
90
|
|
|
93
|
-
|
|
91
|
+
## AGENT
|
|
94
92
|
|
|
95
|
-
|
|
93
|
+
如果你是 agent,建议先看 [usage.md](https://github.com/kir-sleeping/DSH-relay/blob/main/docs/usage.md):教用户怎么用、为什么会出现那些弹窗、不想反复弹窗该怎么做。
|
|
96
94
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
**应用不是可信的,用户才是。**
|
|
100
|
-
|
|
101
|
-
- 首次接入必须由用户在系统原生确认框里点「允许」,才拿到凭证。默认级别 1(每条消息都要确认)。
|
|
102
|
-
- 级别在「设置 → 应用权限」里随时可改、可撤销,不需要应用配合。撤销立即生效。
|
|
103
|
-
- **`trigger` 表示谁发起的,`approved` 表示这次调用被允许发生。两者独立。** 后台发起的消息永远不会被当成用户说的话,即使用户在确认框里点过允许——放行不等于署名。
|
|
104
|
-
- 外部入口只能触达 Relay 工作区里的会话,碰不到用户的私人会话。
|
|
105
|
-
- 服务只监听 `127.0.0.1`。不要用反向代理把它暴露出去。
|
|
106
|
-
|
|
107
|
-
**外部会话的 agent 以默认预设创建,拥有完整工具集**(bash、读写文件、联网搜索、定时任务……)——这样它才能读工作区里的 `AGENTS.md`、真正做事。代价是:一个级别 3 的应用,理论上可以让 agent 在你机器上执行命令。
|
|
108
|
-
|
|
109
|
-
这是当前版本有意选择的激进姿态:先把能力打通,等权限系统支持按工具收窄之后再收紧。**在收紧之前,不要给不信任的应用级别 3。**
|
|
110
|
-
|
|
111
|
-
---
|
|
112
|
-
|
|
113
|
-
## 状态
|
|
114
|
-
|
|
115
|
-
已实现并实测:受理 / 三种交付 / 事件关联 / 结果重建 / 排队作废 / 凭证与分级 / 原生确认框 / 流式。
|
|
116
|
-
|
|
117
|
-
已知限制:
|
|
118
|
-
|
|
119
|
-
- 没有桌面时无法弹确认框(远程访问、无人值守),需要确认的操作会返回 `ERR_AUTH_UNAVAILABLE`;级别 3 的已授权来源不受影响。
|
|
120
|
-
- 原生确认框的**右下角小窗**只在 Windows / WSL 上有;macOS 与 Linux 仍是居中对话框。
|
|
121
|
-
- 无幂等、无取消、无跨机器调用。
|
|
122
|
-
- `trigger` 是应用自报的,Relay 无法验证"真的是用户点的"——级别 3 下只能如实转述。
|
|
123
|
-
|
|
124
|
-
---
|
|
125
|
-
|
|
126
|
-
## 文档
|
|
127
|
-
|
|
128
|
-
| 文件 | 内容 |
|
|
129
|
-
|---|---|
|
|
130
|
-
| [relay-guide.md](../../relay-guide.md) | 接入方文档:接口、字段、错误码、示例 |
|
|
131
|
-
| [relay-demo.html](../../relay-demo.html) | 可运行的单文件示例 |
|
|
132
|
-
| [relay-reference.md](../../relay-reference.md) | 维护者参考:内部结构、不变式、扩展点 |
|
|
133
|
-
| [relay-plan.md](../../relay-plan.md) | 设计:契约与取舍 |
|
|
134
|
-
| [relay-auth-plan.md](../../relay-auth-plan.md) | 鉴权设计 |
|
|
95
|
+
想做外部接入,先看 [demo.html](https://github.com/kir-sleeping/DSH-relay/blob/main/docs/demo.html) 和 [integration.md](https://github.com/kir-sleeping/DSH-relay/blob/main/docs/integration.md)。大部分注意事项以 demo 里的实现形式给出,比文档更直接。
|
|
135
96
|
|
|
136
97
|
---
|
|
137
98
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
本 README 随包分发;其余文档(接入指南、示例、设计稿)在仓库根目录,不在 npm 包里。
|
|
99
|
+
> 本 README 随包分发;其余文档在仓库的 `docs/` 目录,不在 npm 包里,所以上面的链接都指向 GitHub。
|
|
141
100
|
|
|
142
101
|
## License
|
|
143
102
|
|
package/client.js
CHANGED
|
@@ -20,7 +20,23 @@ window.__ModuleLoader__.load({
|
|
|
20
20
|
]
|
|
21
21
|
|
|
22
22
|
const styles = {
|
|
23
|
-
|
|
23
|
+
header: {
|
|
24
|
+
display: 'flex', alignItems: 'center', justifyContent: 'space-between',
|
|
25
|
+
gap: 12, margin: '0 0 12px',
|
|
26
|
+
},
|
|
27
|
+
hint: { opacity: 0.7, fontSize: 13, lineHeight: 1.6 },
|
|
28
|
+
helpButton: {
|
|
29
|
+
font: 'inherit', fontSize: 13, width: 24, height: 24, lineHeight: '22px',
|
|
30
|
+
textAlign: 'center', cursor: 'pointer', color: 'inherit', background: 'transparent',
|
|
31
|
+
border: '1px solid currentColor', borderRadius: 999, opacity: 0.7, flex: 'none',
|
|
32
|
+
},
|
|
33
|
+
help: {
|
|
34
|
+
margin: '0 0 16px', padding: '12px 14px', borderRadius: 8,
|
|
35
|
+
border: '1px solid currentColor', fontSize: 13, lineHeight: 1.75, opacity: 0.9,
|
|
36
|
+
},
|
|
37
|
+
helpTitle: { fontWeight: 600, margin: '0 0 8px' },
|
|
38
|
+
helpList: { margin: '0 0 12px', paddingLeft: 18 },
|
|
39
|
+
helpItem: { margin: '0 0 6px' },
|
|
24
40
|
table: { width: '100%', borderCollapse: 'collapse', fontSize: 13 },
|
|
25
41
|
th: { textAlign: 'left', fontWeight: 600, padding: '6px 8px', borderBottom: '1px solid currentColor', opacity: 0.6 },
|
|
26
42
|
td: { padding: '8px', borderBottom: '1px solid currentColor', verticalAlign: 'middle' },
|
|
@@ -65,6 +81,7 @@ window.__ModuleLoader__.load({
|
|
|
65
81
|
const [rows, setRows] = React.useState([])
|
|
66
82
|
const [error, setError] = React.useState('')
|
|
67
83
|
const [busy, setBusy] = React.useState(false)
|
|
84
|
+
const [helpOpen, setHelpOpen] = React.useState(false)
|
|
68
85
|
|
|
69
86
|
const load = React.useCallback(async () => {
|
|
70
87
|
try {
|
|
@@ -112,11 +129,45 @@ window.__ModuleLoader__.load({
|
|
|
112
129
|
: h('button', { style: styles.button, disabled: busy, onClick: () => run('/sources/clear', { source: row.source }) }, '解除禁止'),
|
|
113
130
|
h('button', { style: styles.button, disabled: busy, onClick: () => run('/sources/delete', { source: row.source }) }, '删除')))))
|
|
114
131
|
|
|
132
|
+
const help = h('div', { style: styles.help },
|
|
133
|
+
h('p', { style: styles.helpTitle }, '四个级别分别是什么意思'),
|
|
134
|
+
h('ul', { style: styles.helpList },
|
|
135
|
+
h('li', { style: styles.helpItem },
|
|
136
|
+
h('b', null, '0 禁止'),
|
|
137
|
+
' 直接拒绝,不弹窗,应用收不到任何回应。'),
|
|
138
|
+
h('li', { style: styles.helpItem },
|
|
139
|
+
h('b', null, '1 每条确认'),
|
|
140
|
+
' 应用每发一条消息都弹一次系统确认框。新接入的应用默认是这一级。'),
|
|
141
|
+
h('li', { style: styles.helpItem },
|
|
142
|
+
h('b', null, '2 信任已确认标记'),
|
|
143
|
+
' 应用声明是你亲手触发的就放行;它自己在后台发起的仍然弹窗。'),
|
|
144
|
+
h('li', { style: styles.helpItem },
|
|
145
|
+
h('b', null, '3 完全信任'),
|
|
146
|
+
' 不再询问,直接执行。')),
|
|
147
|
+
h('p', { style: styles.helpTitle }, '给权限前请想清楚'),
|
|
148
|
+
h('ul', { style: styles.helpList },
|
|
149
|
+
h('li', { style: styles.helpItem },
|
|
150
|
+
'这套权限防的是',
|
|
151
|
+
h('b', null, '有害请求'),
|
|
152
|
+
',不是防你自己。你信任的应用可以给 3 级——那样它全程没有弹窗,用起来才顺。'),
|
|
153
|
+
h('li', { style: styles.helpItem },
|
|
154
|
+
h('b', null, '3 级就是全权限:'),
|
|
155
|
+
'agent 能读写文件、执行命令、联网。只给自己写的或完全信得过的应用。'),
|
|
156
|
+
h('li', { style: styles.helpItem },
|
|
157
|
+
'放宽权限(提高级别、重置凭证)会弹一次系统确认框;收紧(降低级别、禁止)立即生效,不弹窗。'),
|
|
158
|
+
h('li', { style: styles.helpItem },
|
|
159
|
+
'拿不准就留在 1 级,先看着它跑几次再决定。')))
|
|
160
|
+
|
|
115
161
|
return h('div', null,
|
|
116
|
-
h('
|
|
117
|
-
'
|
|
118
|
-
'
|
|
119
|
-
|
|
162
|
+
h('div', { style: styles.header },
|
|
163
|
+
h('span', { style: styles.hint }, '各权限级别的含义和给权限的注意事项,点右侧问号。'),
|
|
164
|
+
h('button', {
|
|
165
|
+
type: 'button',
|
|
166
|
+
style: styles.helpButton,
|
|
167
|
+
title: helpOpen ? '收起说明' : '查看说明',
|
|
168
|
+
onClick: () => setHelpOpen((open) => !open),
|
|
169
|
+
}, '?')),
|
|
170
|
+
helpOpen ? help : null,
|
|
120
171
|
error ? h('p', { style: styles.error }, error) : null,
|
|
121
172
|
rows.length === 0
|
|
122
173
|
? h('p', { style: styles.empty }, '还没有外部应用接入。应用第一次调用时会弹出确认框。')
|
package/icon.ico
ADDED
|
Binary file
|
package/icon.png
ADDED
|
Binary file
|
package/icon.svg
CHANGED
|
@@ -1,7 +1,42 @@
|
|
|
1
|
-
<svg xmlns="http://www.w3.org/2000/svg"
|
|
2
|
-
<
|
|
3
|
-
|
|
4
|
-
<path
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="512" height="512" viewBox="0 0 1254 1254" role="img" aria-labelledby="relay-title">
|
|
2
|
+
<title id="relay-title">DSH Relay</title>
|
|
3
|
+
|
|
4
|
+
<path fill="#2563eb" d="
|
|
5
|
+
M 216 178 H 288
|
|
6
|
+
C 283 200 280 221 280 244
|
|
7
|
+
C 280 432 435 585.5 627 585.5
|
|
8
|
+
C 819 585.5 973 432 973 244
|
|
9
|
+
C 973 221 970 200 965.5 178
|
|
10
|
+
H 1038 C 1060 178 1078 196 1078 218 V 994
|
|
11
|
+
C 1078 1016 1060 1034 1038 1034 H 870
|
|
12
|
+
C 872 1017 874 1002 874 985 V 779
|
|
13
|
+
C 874 736 855 700 816 670 V 930
|
|
14
|
+
C 816 973 793 1011 756 1034 V 675
|
|
15
|
+
C 756 655 741 640 721 640 H 533
|
|
16
|
+
C 513 640 498 655 498 675 V 1034
|
|
17
|
+
C 461 1011 437 973 437 930 V 670
|
|
18
|
+
C 399 700 379 736 379 779 V 985
|
|
19
|
+
C 379 1002 380 1017 382 1034 H 216
|
|
20
|
+
C 194 1034 176 1016 176 994 V 218
|
|
21
|
+
C 176 196 194 178 216 178 Z" />
|
|
22
|
+
<path fill="#2563eb" d="M 549.5 693.5 H 705 V 1038.5 H 549.5 Z" />
|
|
23
|
+
|
|
24
|
+
<path fill="#06b6d4" d="
|
|
25
|
+
M 327.5 178 H 926.5
|
|
26
|
+
C 932 201 935 223 935 245
|
|
27
|
+
C 935 412 797 549 627 549
|
|
28
|
+
C 457 549 319 412 319 245
|
|
29
|
+
C 319 223 322 201 327.5 178 Z" />
|
|
30
|
+
<path fill="#fff" d="
|
|
31
|
+
M 404 367
|
|
32
|
+
C 413 326 485 267 578 267
|
|
33
|
+
C 668 267 744 313 762 387
|
|
34
|
+
C 769 415 756 417 729 405
|
|
35
|
+
C 695 391 663 360 626 360
|
|
36
|
+
C 587 359 565 389 570 424
|
|
37
|
+
C 574 454 596 480 619.5 495.5
|
|
38
|
+
Q 627 502 613 501.5
|
|
39
|
+
C 536 499 456 458 420 405
|
|
40
|
+
C 408 389 402 376 404 367 Z" />
|
|
41
|
+
<ellipse cx="810.5" cy="261" rx="46.5" ry="45" fill="#fff" />
|
|
42
|
+
</svg>
|
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.0",
|
|
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,9 @@
|
|
|
35
37
|
"locale/*.json",
|
|
36
38
|
"icon.svg",
|
|
37
39
|
"README.md",
|
|
38
|
-
"LICENSE"
|
|
40
|
+
"LICENSE",
|
|
41
|
+
"icon.ico",
|
|
42
|
+
"icon.png"
|
|
39
43
|
],
|
|
40
44
|
"engines": {
|
|
41
45
|
"dsh": ">=0.2.2-rc.1 <0.3.0"
|