dsh-zcode-cua 0.0.0-stage → 1.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/CHANGELOG.md +133 -0
- package/LICENSE +21 -0
- package/README.md +208 -2
- package/README.zh.md +209 -0
- package/client.js +209 -0
- package/cordis.patch.yml +43 -0
- package/docs/DETAIL.md +1 -0
- package/docs/DETAIL.zh.md +1 -0
- package/docs/preview.svg +51 -0
- package/icon.svg +28 -0
- package/index.js +226 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +73 -6
- package/server/access.js +94 -0
- package/server/helper-launcher.js +64 -0
- package/server/index.js +498 -0
- package/server/overlay/Overlay.cs +777 -0
- package/server/overlay/host.ps1 +130 -0
- package/server/overlay.js +374 -0
- package/server/paths.js +231 -0
- package/skills/zcode-computer-use/SKILL.md +304 -0
package/client.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Composer switch for the per-session Computer Use grant.
|
|
3
|
+
*
|
|
4
|
+
* The host half (`index.js`) denies the `js` tool unless the session opted in;
|
|
5
|
+
* this is the only place that opt-in can happen, so the control has to be
|
|
6
|
+
* reachable from the conversation the user is looking at rather than from
|
|
7
|
+
* Settings (a setting would be profile-wide, and the whole point is that a new
|
|
8
|
+
* session starts with the desktop untouched).
|
|
9
|
+
*
|
|
10
|
+
* The state lives on the Host, not in the browser: two windows on the same
|
|
11
|
+
* session must agree, and a reload must not silently re-grant. So the switch
|
|
12
|
+
* reads and writes `GET/POST /plugins/dsh-zcode-cua/access` and keeps only a
|
|
13
|
+
* display copy.
|
|
14
|
+
*
|
|
15
|
+
* Styling follows the host: no Harness client package is imported (that is
|
|
16
|
+
* forbidden for plugins), only the theme's alias tokens, so the control tracks
|
|
17
|
+
* light and dark like everything beside it.
|
|
18
|
+
*
|
|
19
|
+
* @module dsh-zcode-cua/client
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
window.__ModuleLoader__.load({
|
|
23
|
+
id: 'dsh-zcode-cua',
|
|
24
|
+
factory(require) {
|
|
25
|
+
const React = require('react');
|
|
26
|
+
const h = React.createElement;
|
|
27
|
+
|
|
28
|
+
const ACCESS_PATH = '/plugins/dsh-zcode-cua/access';
|
|
29
|
+
|
|
30
|
+
/** Endpoint URL for one session. */
|
|
31
|
+
function accessUrl(sessionId) {
|
|
32
|
+
return `${ACCESS_PATH}?session=${encodeURIComponent(sessionId)}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function readGrant(sessionId, signal) {
|
|
36
|
+
const response = await fetch(accessUrl(sessionId), {
|
|
37
|
+
method: 'GET',
|
|
38
|
+
headers: { accept: 'application/json' },
|
|
39
|
+
cache: 'no-store',
|
|
40
|
+
signal,
|
|
41
|
+
});
|
|
42
|
+
if (!response.ok) throw new Error(`host answered ${response.status}`);
|
|
43
|
+
const body = await response.json();
|
|
44
|
+
return body?.granted === true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function writeGrant(sessionId, granted, signal) {
|
|
48
|
+
const response = await fetch(accessUrl(sessionId), {
|
|
49
|
+
method: 'POST',
|
|
50
|
+
headers: { 'content-type': 'application/json', accept: 'application/json' },
|
|
51
|
+
cache: 'no-store',
|
|
52
|
+
body: JSON.stringify({ granted }),
|
|
53
|
+
signal,
|
|
54
|
+
});
|
|
55
|
+
if (!response.ok) throw new Error(`host answered ${response.status}`);
|
|
56
|
+
const body = await response.json();
|
|
57
|
+
return body?.granted === true;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The switch.
|
|
62
|
+
*
|
|
63
|
+
* Three states are visible, because "off" and "could not ask" must not look
|
|
64
|
+
* alike: a switch that silently reads as off when the host route failed
|
|
65
|
+
* would make the user click a control that does nothing.
|
|
66
|
+
*
|
|
67
|
+
* off — the tool is denied for this session
|
|
68
|
+
* on — the tool may run in this session
|
|
69
|
+
* unknown — the host has not answered yet (loading, or the route failed)
|
|
70
|
+
*/
|
|
71
|
+
function AccessToggle(props) {
|
|
72
|
+
const sessionId = props.sessionId;
|
|
73
|
+
const [granted, setGranted] = React.useState(false);
|
|
74
|
+
const [known, setKnown] = React.useState(false);
|
|
75
|
+
const [busy, setBusy] = React.useState(false);
|
|
76
|
+
const [failure, setFailure] = React.useState('');
|
|
77
|
+
|
|
78
|
+
React.useEffect(() => {
|
|
79
|
+
if (typeof sessionId !== 'string' || sessionId === '') return undefined;
|
|
80
|
+
const controller = new AbortController();
|
|
81
|
+
let live = true;
|
|
82
|
+
readGrant(sessionId, controller.signal)
|
|
83
|
+
.then((value) => {
|
|
84
|
+
if (!live) return;
|
|
85
|
+
setGranted(value);
|
|
86
|
+
setKnown(true);
|
|
87
|
+
setFailure('');
|
|
88
|
+
})
|
|
89
|
+
.catch((error) => {
|
|
90
|
+
if (!live || controller.signal.aborted) return;
|
|
91
|
+
setKnown(false);
|
|
92
|
+
setFailure(String(error?.message ?? error));
|
|
93
|
+
});
|
|
94
|
+
return () => {
|
|
95
|
+
live = false;
|
|
96
|
+
controller.abort();
|
|
97
|
+
};
|
|
98
|
+
}, [sessionId]);
|
|
99
|
+
|
|
100
|
+
const toggle = React.useCallback(() => {
|
|
101
|
+
if (typeof sessionId !== 'string' || sessionId === '' || busy) return;
|
|
102
|
+
const next = !granted;
|
|
103
|
+
setBusy(true);
|
|
104
|
+
setFailure('');
|
|
105
|
+
writeGrant(sessionId, next)
|
|
106
|
+
.then((value) => {
|
|
107
|
+
setGranted(value);
|
|
108
|
+
setKnown(true);
|
|
109
|
+
})
|
|
110
|
+
.catch((error) => {
|
|
111
|
+
setKnown(false);
|
|
112
|
+
setFailure(String(error?.message ?? error));
|
|
113
|
+
})
|
|
114
|
+
.finally(() => setBusy(false));
|
|
115
|
+
}, [sessionId, granted, busy]);
|
|
116
|
+
|
|
117
|
+
const usable = typeof sessionId === 'string' && sessionId !== '';
|
|
118
|
+
const label = !known
|
|
119
|
+
? '电脑控制 · 状态未知'
|
|
120
|
+
: granted
|
|
121
|
+
? '电脑控制 · 已开启'
|
|
122
|
+
: '电脑控制 · 已关闭';
|
|
123
|
+
const title = !usable
|
|
124
|
+
? '这个会话还没有 id,无法记录开关状态。'
|
|
125
|
+
: failure !== ''
|
|
126
|
+
? `无法读写开关状态:${failure}`
|
|
127
|
+
: granted
|
|
128
|
+
? '本会话可以操作桌面(点击、打字、截图)。点一下关闭。'
|
|
129
|
+
: '本会话不能操作桌面。点一下开启;只对这个会话有效,DSH 重启后回到关闭。';
|
|
130
|
+
|
|
131
|
+
// Colour comes from the token as TEXT, never as a fill behind white text.
|
|
132
|
+
// `--dsw-alias-brand-primary` is a near-white accent in the dark theme, so
|
|
133
|
+
// filling the pill with it and writing white on top made the label vanish
|
|
134
|
+
// — measured on a real host, not guessed. Keeping the background
|
|
135
|
+
// transparent and tinting the border, dot and label works in both themes,
|
|
136
|
+
// because each token is defined per theme.
|
|
137
|
+
const accent = granted
|
|
138
|
+
? 'var(--dsw-alias-brand-primary)'
|
|
139
|
+
: 'var(--dsw-alias-label-secondary)';
|
|
140
|
+
const border = granted
|
|
141
|
+
? 'var(--dsw-alias-brand-primary)'
|
|
142
|
+
: 'var(--dsw-alias-border-l1)';
|
|
143
|
+
const dot = granted
|
|
144
|
+
? 'var(--dsw-alias-state-success-primary)'
|
|
145
|
+
: 'var(--dsw-alias-state-idle-primary)';
|
|
146
|
+
|
|
147
|
+
return h(
|
|
148
|
+
'button',
|
|
149
|
+
{
|
|
150
|
+
type: 'button',
|
|
151
|
+
onClick: toggle,
|
|
152
|
+
disabled: !usable || busy,
|
|
153
|
+
title,
|
|
154
|
+
'aria-pressed': granted,
|
|
155
|
+
'data-dsh-zcode-cua-access': known ? (granted ? 'on' : 'off') : 'unknown',
|
|
156
|
+
style: {
|
|
157
|
+
display: 'inline-flex',
|
|
158
|
+
alignItems: 'center',
|
|
159
|
+
gap: 6,
|
|
160
|
+
height: 26,
|
|
161
|
+
padding: '0 10px',
|
|
162
|
+
margin: 0,
|
|
163
|
+
border: `1px solid ${border}`,
|
|
164
|
+
borderRadius: 999,
|
|
165
|
+
background: 'transparent',
|
|
166
|
+
color: accent,
|
|
167
|
+
font: 'inherit',
|
|
168
|
+
fontSize: 12,
|
|
169
|
+
lineHeight: 1,
|
|
170
|
+
cursor: usable && !busy ? 'pointer' : 'not-allowed',
|
|
171
|
+
opacity: busy ? 0.6 : known ? 1 : 0.75,
|
|
172
|
+
whiteSpace: 'nowrap',
|
|
173
|
+
userSelect: 'none',
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
h('span', {
|
|
177
|
+
'aria-hidden': true,
|
|
178
|
+
style: {
|
|
179
|
+
width: 7,
|
|
180
|
+
height: 7,
|
|
181
|
+
borderRadius: '50%',
|
|
182
|
+
background: dot,
|
|
183
|
+
flex: '0 0 auto',
|
|
184
|
+
},
|
|
185
|
+
}),
|
|
186
|
+
label,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return {
|
|
191
|
+
inject: ['slots'],
|
|
192
|
+
apply(ctx) {
|
|
193
|
+
ctx.slots.inject('conversation.composer.dock', () =>
|
|
194
|
+
ctx.slots.register(
|
|
195
|
+
{
|
|
196
|
+
name: 'conversation.composer.dock',
|
|
197
|
+
// After the credits readouts (10) and the CLI live row (11): this
|
|
198
|
+
// is a permission control, and it should sit at the far end of the
|
|
199
|
+
// row rather than between two readouts.
|
|
200
|
+
id: 'dsh-zcode-cua-access',
|
|
201
|
+
order: 12,
|
|
202
|
+
},
|
|
203
|
+
AccessToggle,
|
|
204
|
+
),
|
|
205
|
+
);
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
},
|
|
209
|
+
});
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Two rows, two jobs.
|
|
2
|
+
#
|
|
3
|
+
# `zcode-cua` registers the stdio MCP server that actually drives the desktop:
|
|
4
|
+
# it spawns ZCode's `node-repl-host`, resolves the installed cua-helper, and
|
|
5
|
+
# turns Computer Use on by supplying the permission-broker environment the
|
|
6
|
+
# upstream server requires.
|
|
7
|
+
#
|
|
8
|
+
# `zcode-cua-access` is this package's own Host half (`index.js`). It holds the
|
|
9
|
+
# per-session switch: a tool guard that denies the `js` tool until the session
|
|
10
|
+
# opts in from the composer, plus the two small routes that switch reads and
|
|
11
|
+
# writes. The gate has to sit here rather than in the MCP server — a check
|
|
12
|
+
# inside the child process would still have started it, and the point is that a
|
|
13
|
+
# denied call never reaches the desktop at all.
|
|
14
|
+
#
|
|
15
|
+
# The `args` path is RELATIVE on purpose. It is resolved against the profile
|
|
16
|
+
# directory, where every profile-installed bundle lives at
|
|
17
|
+
# `node_modules/<package-name>`, so this file works on any machine without
|
|
18
|
+
# naming a checkout location.
|
|
19
|
+
#
|
|
20
|
+
# Running from a source checkout instead (a linked local install)? Local linked
|
|
21
|
+
# installs place the package under `node_modules/<package-name>` too, so the
|
|
22
|
+
# same relative path keeps working. If you cloned this repository somewhere and
|
|
23
|
+
# installed it by that directory, nothing here needs editing.
|
|
24
|
+
- insert:
|
|
25
|
+
- id: zcode-cua-access
|
|
26
|
+
name: 'dsh-zcode-cua'
|
|
27
|
+
- id: zcode-cua
|
|
28
|
+
name: '@deepseek-ai/dsh-mcp-client'
|
|
29
|
+
config:
|
|
30
|
+
serverName: zcode-cua
|
|
31
|
+
transport: stdio
|
|
32
|
+
command: 'node'
|
|
33
|
+
args:
|
|
34
|
+
- 'node_modules/dsh-zcode-cua/server/index.js'
|
|
35
|
+
env: {}
|
|
36
|
+
cwd: ''
|
|
37
|
+
toolCallTimeoutMs: 180000
|
|
38
|
+
failOnStartupError: false
|
|
39
|
+
reconnect:
|
|
40
|
+
enabled: true
|
|
41
|
+
initialDelayMs: 500
|
|
42
|
+
maxDelayMs: 30000
|
|
43
|
+
maxAttempts: 10
|
package/docs/DETAIL.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+

|
|
@@ -0,0 +1 @@
|
|
|
1
|
+

|
package/docs/preview.svg
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="640" height="300" viewBox="0 0 640 300" role="img" aria-labelledby="t d">
|
|
2
|
+
<title id="t">A desktop window on the left, its accessibility tree in the middle, and a pointer acting on one element on the right</title>
|
|
3
|
+
<desc id="d">Illustration of how Computer Use works: the model observes a window, reads the accessibility tree of roles and names, then clicks one element by index or by screen pixel.</desc>
|
|
4
|
+
|
|
5
|
+
<!-- window -->
|
|
6
|
+
<rect x="40" y="66" width="196" height="168" rx="10" fill="none" stroke="#64748B" stroke-width="2"/>
|
|
7
|
+
<path d="M40 96 H236" stroke="#64748B" stroke-width="2"/>
|
|
8
|
+
<circle cx="56" cy="81" r="3.5" fill="#64748B"/>
|
|
9
|
+
<circle cx="68" cy="81" r="3.5" fill="#64748B"/>
|
|
10
|
+
<circle cx="80" cy="81" r="3.5" fill="#64748B"/>
|
|
11
|
+
|
|
12
|
+
<!-- rows inside the window; the third one is the target -->
|
|
13
|
+
<rect x="56" y="112" width="128" height="12" rx="6" fill="#94A3B8" opacity="0.45"/>
|
|
14
|
+
<rect x="56" y="136" width="96" height="12" rx="6" fill="#94A3B8" opacity="0.45"/>
|
|
15
|
+
<rect x="56" y="160" width="140" height="26" rx="7" fill="none" stroke="#3B82F6" stroke-width="2"/>
|
|
16
|
+
<rect x="64" y="169" width="72" height="8" rx="4" fill="#3B82F6" opacity="0.55"/>
|
|
17
|
+
<rect x="56" y="200" width="112" height="12" rx="6" fill="#94A3B8" opacity="0.45"/>
|
|
18
|
+
|
|
19
|
+
<!-- accessibility tree -->
|
|
20
|
+
<path d="M300 150 H330" stroke="#3B82F6" stroke-width="2" stroke-dasharray="5 5"/>
|
|
21
|
+
<circle cx="348" cy="150" r="18" fill="none" stroke="#64748B" stroke-width="2"/>
|
|
22
|
+
<path d="M366 150 H392" stroke="#64748B" stroke-width="1.75"/>
|
|
23
|
+
<path d="M392 108 V192" stroke="#64748B" stroke-width="1.75"/>
|
|
24
|
+
|
|
25
|
+
<!-- tree leaves -->
|
|
26
|
+
<path d="M392 108 H412" stroke="#64748B" stroke-width="1.75"/>
|
|
27
|
+
<rect x="412" y="98" width="72" height="20" rx="6" fill="#94A3B8" opacity="0.35"/>
|
|
28
|
+
<rect x="420" y="105" width="44" height="6" rx="3" fill="#64748B" opacity="0.8"/>
|
|
29
|
+
|
|
30
|
+
<path d="M392 150 H412" stroke="#3B82F6" stroke-width="2"/>
|
|
31
|
+
<rect x="412" y="138" width="96" height="24" rx="7" fill="none" stroke="#3B82F6" stroke-width="2"/>
|
|
32
|
+
<rect x="422" y="147" width="56" height="6" rx="3" fill="#3B82F6"/>
|
|
33
|
+
|
|
34
|
+
<path d="M392 192 H412" stroke="#64748B" stroke-width="1.75"/>
|
|
35
|
+
<rect x="412" y="182" width="72" height="20" rx="6" fill="#94A3B8" opacity="0.35"/>
|
|
36
|
+
<rect x="420" y="189" width="40" height="6" rx="3" fill="#64748B" opacity="0.8"/>
|
|
37
|
+
|
|
38
|
+
<!-- index badge on the highlighted leaf -->
|
|
39
|
+
<circle cx="516" cy="126" r="11" fill="#3B82F6"/>
|
|
40
|
+
<path d="M512 126 h8 M516 122 v8" stroke="#FFFFFF" stroke-width="1.75" stroke-linecap="round"/>
|
|
41
|
+
|
|
42
|
+
<!-- pointer acting on the highlighted row in the window -->
|
|
43
|
+
<path d="M196 178 l0 26 l7 -7 l5 11 l6 -3 l-5 -11 l10 0 z" fill="#F59E0B" stroke="#78350F" stroke-width="1.25" stroke-linejoin="round"/>
|
|
44
|
+
<circle cx="196" cy="178" r="15" fill="none" stroke="#F59E0B" stroke-width="2" opacity="0.5"/>
|
|
45
|
+
|
|
46
|
+
<!-- screenshot frame corners around the window, suggesting a captured raster -->
|
|
47
|
+
<path d="M32 58 h12 M32 58 v12" stroke="#10B981" stroke-width="2.5" stroke-linecap="round"/>
|
|
48
|
+
<path d="M244 58 h-12 M244 58 v12" stroke="#10B981" stroke-width="2.5" stroke-linecap="round"/>
|
|
49
|
+
<path d="M32 242 h12 M32 242 v-12" stroke="#10B981" stroke-width="2.5" stroke-linecap="round"/>
|
|
50
|
+
<path d="M244 242 h-12 M244 242 v-12" stroke="#10B981" stroke-width="2.5" stroke-linecap="round"/>
|
|
51
|
+
</svg>
|
package/icon.svg
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 128 128" width="128" height="128" role="img" aria-label="ZCode computer control">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="bg" x1="0" y1="0" x2="0" y2="1">
|
|
4
|
+
<stop offset="0" stop-color="#2B3A55"/>
|
|
5
|
+
<stop offset="1" stop-color="#16202F"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
<linearGradient id="cursor" x1="0" y1="0" x2="1" y2="1">
|
|
8
|
+
<stop offset="0" stop-color="#8FC7FF"/>
|
|
9
|
+
<stop offset="1" stop-color="#1F8CFF"/>
|
|
10
|
+
</linearGradient>
|
|
11
|
+
</defs>
|
|
12
|
+
|
|
13
|
+
<rect x="4" y="4" width="120" height="120" rx="26" fill="url(#bg)"/>
|
|
14
|
+
<rect x="4.75" y="4.75" width="118.5" height="118.5" rx="25.25" fill="none" stroke="#5B7BA6" stroke-opacity="0.55" stroke-width="1.5"/>
|
|
15
|
+
|
|
16
|
+
<!-- screen -->
|
|
17
|
+
<rect x="26" y="30" width="76" height="52" rx="7" fill="none" stroke="#5B7BA6" stroke-width="3"/>
|
|
18
|
+
<line x1="46" y1="94" x2="82" y2="94" stroke="#5B7BA6" stroke-width="3" stroke-linecap="round"/>
|
|
19
|
+
<line x1="64" y1="82" x2="64" y2="94" stroke="#5B7BA6" stroke-width="3" stroke-linecap="round"/>
|
|
20
|
+
|
|
21
|
+
<!-- pointer arrow -->
|
|
22
|
+
<g transform="translate(70 52)">
|
|
23
|
+
<path d="M0 0 L0 34 L9 25 L15 39 L22 36 L16 22 L28 22 Z"
|
|
24
|
+
fill="url(#cursor)" stroke="#EAF4FF" stroke-width="2.2" stroke-linejoin="round"/>
|
|
25
|
+
<circle cx="-6" cy="30" r="7" fill="#1F8CFF" opacity="0.28"/>
|
|
26
|
+
<circle cx="-6" cy="30" r="3.4" fill="#8FC7FF" opacity="0.85"/>
|
|
27
|
+
</g>
|
|
28
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host half of the per-session Computer Use switch.
|
|
3
|
+
*
|
|
4
|
+
* The MCP server that actually drives the desktop lives in `server/index.js`
|
|
5
|
+
* and is spawned by `@deepseek-ai/dsh-mcp-client`, not by this file. This entry
|
|
6
|
+
* is the Harness plugin that decides whether that tool may run at all: it
|
|
7
|
+
* registers a tool guard (deny unless the session opted in) and two small HTTP
|
|
8
|
+
* routes the composer switch uses to read and write that state.
|
|
9
|
+
*
|
|
10
|
+
* Keeping the gate in a Host plugin rather than inside the MCP server is what
|
|
11
|
+
* makes it a gate rather than advice: the guard runs before the call reaches
|
|
12
|
+
* the child process, so a denied call never starts a helper, never moves the
|
|
13
|
+
* mouse, and never spends a screenshot.
|
|
14
|
+
*
|
|
15
|
+
* @module dsh-zcode-cua
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { clearGrants, guardBody, isGranted, setGranted } from "./server/access.js";
|
|
19
|
+
|
|
20
|
+
/** Route the composer switch reads and writes. */
|
|
21
|
+
const ACCESS_PATH = "/plugins/dsh-zcode-cua/access";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Services this plugin needs.
|
|
25
|
+
*
|
|
26
|
+
* `tools` must be declared here, not merely used: Cordis refuses to read
|
|
27
|
+
* `ctx.tools` on an undeclared service (`cannot get property "tools" without
|
|
28
|
+
* inject`), which fails the whole plugin row rather than one call.
|
|
29
|
+
*
|
|
30
|
+
* `webServer` and `connection` are declared here rather than at the injection
|
|
31
|
+
* site below for a reason that outranks convenience: the access route WRITES
|
|
32
|
+
* the grant. Without the connection fence, any page the user has open in a
|
|
33
|
+
* browser could `POST` this route and switch computer control on for a session,
|
|
34
|
+
* which is a remote page acquiring the ability to move the mouse. A gate whose
|
|
35
|
+
* only weakness is that anyone can open it is not a gate, so a Host that cannot
|
|
36
|
+
* supply the fence does not get the route at all — the switch disappears and the
|
|
37
|
+
* guard stays. That is why these two are hard requirements instead of optional
|
|
38
|
+
* injections.
|
|
39
|
+
*/
|
|
40
|
+
export const inject = ["tools", "webServer", "connection"];
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Answer one access request.
|
|
44
|
+
*
|
|
45
|
+
* The session id comes from the caller, never from server-side guesswork: the
|
|
46
|
+
* Harness webserver serves every session on one origin, so there is no
|
|
47
|
+
* request-scoped session to read. The switch already holds its `sessionId`
|
|
48
|
+
* prop, which makes the client the only party that knows which session the
|
|
49
|
+
* user is looking at.
|
|
50
|
+
*
|
|
51
|
+
* `GET ?session=<id>` answers `{ granted }`.
|
|
52
|
+
* `POST ?session=<id>` with `{ granted: boolean }` sets it and answers the same.
|
|
53
|
+
*
|
|
54
|
+
* A missing session id is a 400 rather than a silent `{ granted: false }`:
|
|
55
|
+
* "this request forgot to name a session" and "this session is off" must not
|
|
56
|
+
* look alike, or a broken switch reads as a working one.
|
|
57
|
+
*/
|
|
58
|
+
async function handleAccess(req, res, logger, connection) {
|
|
59
|
+
/**
|
|
60
|
+
* The trust fence, and it must run before anything else reads the request.
|
|
61
|
+
*
|
|
62
|
+
* `connection.requestRejection(req)` answers `401`/`403` for a request that
|
|
63
|
+
* fails the Host/Origin check (DNS rebinding, a foreign page's cross-site
|
|
64
|
+
* call) or that carries no browser authentication, and `undefined` for one
|
|
65
|
+
* that may proceed. Calling it first is what stops a web page the user has
|
|
66
|
+
* open from flipping this switch: that page shares the browser but not the
|
|
67
|
+
* credential, and its Origin is not this Host.
|
|
68
|
+
*
|
|
69
|
+
* This route is a WRITE route for a capability that moves the mouse, so the
|
|
70
|
+
* fence is not optional here. If the service ever answers something other
|
|
71
|
+
* than the three documented values, the request is refused: a fence that
|
|
72
|
+
* fails open is worse than no fence, because the code reads as protected.
|
|
73
|
+
*/
|
|
74
|
+
const rejection = requestRejectionOf(connection, req);
|
|
75
|
+
if (rejection !== undefined) {
|
|
76
|
+
res.statusCode = rejection;
|
|
77
|
+
res.setHeader("cache-control", "no-store");
|
|
78
|
+
res.end();
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
let url;
|
|
82
|
+
try {
|
|
83
|
+
url = new URL(req.url ?? "/", "http://localhost");
|
|
84
|
+
} catch {
|
|
85
|
+
writeJson(res, 400, { error: "malformed request url" });
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const sessionId = (url.searchParams.get("session") ?? "").trim();
|
|
89
|
+
if (sessionId === "") {
|
|
90
|
+
writeJson(res, 400, { error: "missing session" });
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (req.method === "GET") {
|
|
94
|
+
writeJson(res, 200, { granted: isGranted(sessionId) });
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (req.method !== "POST") {
|
|
98
|
+
writeJson(res, 405, { error: "method not allowed" });
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
let body;
|
|
102
|
+
try {
|
|
103
|
+
body = await readJson(req);
|
|
104
|
+
} catch (error) {
|
|
105
|
+
writeJson(res, 400, { error: `unreadable body: ${messageOf(error)}` });
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (typeof body?.granted !== "boolean") {
|
|
109
|
+
writeJson(res, 400, { error: "body must carry a boolean granted" });
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
setGranted(sessionId, body.granted);
|
|
113
|
+
logger?.debug?.(`dsh-zcode-cua: computer control ${body.granted ? "on" : "off"} for ${sessionId}`);
|
|
114
|
+
writeJson(res, 200, { granted: isGranted(sessionId) });
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function messageOf(error) {
|
|
118
|
+
return error instanceof Error ? error.message : String(error);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* `401`/`403` when the request fails the Host/Origin fence or carries no
|
|
123
|
+
* browser credential, `undefined` when it may proceed.
|
|
124
|
+
*
|
|
125
|
+
* The service is passed in rather than read from a module-level binding: a
|
|
126
|
+
* module-level variable would be shared by every `apply` in the process, so a
|
|
127
|
+
* second mount (a test, a reload that overlaps the previous generation) would
|
|
128
|
+
* silently fence its requests with the other instance's service.
|
|
129
|
+
*
|
|
130
|
+
* Deliberately total: a service that is missing, a method that throws, or a
|
|
131
|
+
* value outside the documented three all resolve to `403`. The alternative —
|
|
132
|
+
* letting an unexpected shape fall through as "no rejection" — would turn a
|
|
133
|
+
* broken fence into an open one, and the failure would be invisible because the
|
|
134
|
+
* code still reads as guarded.
|
|
135
|
+
*/
|
|
136
|
+
function requestRejectionOf(connection, req) {
|
|
137
|
+
if (connection === undefined || connection === null) return 403;
|
|
138
|
+
if (typeof connection.requestRejection !== "function") return 403;
|
|
139
|
+
let rejection;
|
|
140
|
+
try {
|
|
141
|
+
rejection = connection.requestRejection(req);
|
|
142
|
+
} catch {
|
|
143
|
+
return 403;
|
|
144
|
+
}
|
|
145
|
+
if (rejection === undefined) return undefined;
|
|
146
|
+
return rejection === 401 || rejection === 403 ? rejection : 403;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function writeJson(res, status, payload) {
|
|
150
|
+
const text = JSON.stringify(payload);
|
|
151
|
+
res.writeHead(status, {
|
|
152
|
+
"content-type": "application/json; charset=utf-8",
|
|
153
|
+
"cache-control": "no-store",
|
|
154
|
+
"content-length": Buffer.byteLength(text),
|
|
155
|
+
});
|
|
156
|
+
res.end(text);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Read and parse a small JSON request body, bounded so a stuck upload cannot hang the route. */
|
|
160
|
+
function readJson(req, limit = 8 * 1024) {
|
|
161
|
+
return new Promise((resolve, reject) => {
|
|
162
|
+
let size = 0;
|
|
163
|
+
const chunks = [];
|
|
164
|
+
req.on("data", (chunk) => {
|
|
165
|
+
size += chunk.length;
|
|
166
|
+
if (size > limit) {
|
|
167
|
+
reject(new Error(`body exceeds ${limit} bytes`));
|
|
168
|
+
req.destroy();
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
chunks.push(chunk);
|
|
172
|
+
});
|
|
173
|
+
req.on("end", () => {
|
|
174
|
+
const text = Buffer.concat(chunks).toString("utf8").trim();
|
|
175
|
+
if (text === "") {
|
|
176
|
+
resolve(undefined);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
try {
|
|
180
|
+
resolve(JSON.parse(text));
|
|
181
|
+
} catch (error) {
|
|
182
|
+
reject(error);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
req.on("error", reject);
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Mount the gate.
|
|
191
|
+
*
|
|
192
|
+
* `ctx.tools.guard` is scoped to the plugin that registers it and is monotonic
|
|
193
|
+
* while mounted, which is the property a capability gate needs: no later
|
|
194
|
+
* registration can lift it. The disposer removes it on unload, and the grants
|
|
195
|
+
* are cleared at the same time so a re-mounted plugin does not inherit
|
|
196
|
+
* authority granted to the previous generation.
|
|
197
|
+
*/
|
|
198
|
+
export function apply(ctx, _config) {
|
|
199
|
+
const connection = Reflect.get(ctx, "connection");
|
|
200
|
+
|
|
201
|
+
ctx.effect(() => {
|
|
202
|
+
const removeGuard = ctx.tools.guard(guardBody);
|
|
203
|
+
return () => {
|
|
204
|
+
removeGuard();
|
|
205
|
+
clearGrants();
|
|
206
|
+
};
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
// No `ctx.inject` wrapper here: `webServer` and `connection` are hard
|
|
210
|
+
// requirements declared in `inject`, so by the time `apply` runs both exist.
|
|
211
|
+
// Registering the route unconditionally is the point — a Host that could not
|
|
212
|
+
// supply the connection fence would never reach this line, rather than
|
|
213
|
+
// silently serving an unfenced write route.
|
|
214
|
+
//
|
|
215
|
+
// `connection` is captured per mount, not held in a module variable: two
|
|
216
|
+
// mounts in one process (a test, an overlapping reload) must each fence with
|
|
217
|
+
// their own service.
|
|
218
|
+
const dispose = ctx.effect(() =>
|
|
219
|
+
ctx.webServer.register({
|
|
220
|
+
kind: "exact",
|
|
221
|
+
path: ACCESS_PATH,
|
|
222
|
+
handler: (req, res) => handleAccess(req, res, ctx.logger, connection),
|
|
223
|
+
}),
|
|
224
|
+
);
|
|
225
|
+
return dispose;
|
|
226
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "ZCode computer control",
|
|
4
|
+
"description": "Drives native desktop apps through ZCode's Computer Use runtime: click, type, read the accessibility tree, take screenshots. Adds one tool, js, that runs JavaScript in a fresh Node kernel where agent.computerUse is available. Requires ZCode installed, and the helper's permission broker stays inside this plugin's own process. Off until you call the tool.",
|
|
5
|
+
"detail": "docs/DETAIL.md"
|
|
6
|
+
}
|
|
7
|
+
}
|
package/locale/zh.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "ZCode 电脑控制",
|
|
4
|
+
"description": "通过 ZCode 的 Computer Use 运行时操作本机原生桌面应用:点击、打字、读取无障碍树、截图。只增加一个工具 js——在全新的 Node 内核里跑 JavaScript,其中 agent.computerUse 可用。需要本机已装 ZCode;helper 的权限 broker 只活在本插件自己的进程里。不调用工具就什么都不发生。",
|
|
5
|
+
"detail": "docs/DETAIL.zh.md"
|
|
6
|
+
}
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,73 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "dsh-zcode-cua",
|
|
3
|
+
"displayName": "DSH ZCode Computer Control",
|
|
4
|
+
"version": "1.3.1",
|
|
5
|
+
"description": "把 ZCode 的电脑控制(Computer Use)接进 DeepSeek Harness:点击、打字、读无障碍树与截图。",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"dsh-plugin",
|
|
8
|
+
"deepseek-harness",
|
|
9
|
+
"computer-use",
|
|
10
|
+
"cua",
|
|
11
|
+
"zcode",
|
|
12
|
+
"mcp",
|
|
13
|
+
"mcp-server",
|
|
14
|
+
"gui-automation",
|
|
15
|
+
"windows"
|
|
16
|
+
],
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"author": "wkylnj",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"main": "./index.js",
|
|
21
|
+
"bin": {
|
|
22
|
+
"dsh-zcode-cua": "server/index.js"
|
|
23
|
+
},
|
|
24
|
+
"icon": "./icon.svg",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": "./index.js",
|
|
27
|
+
"./server": "./server/index.js",
|
|
28
|
+
"./client": "./client.js",
|
|
29
|
+
"./locale/*.json": "./locale/*.json",
|
|
30
|
+
"./icon": "./icon.svg",
|
|
31
|
+
"./package.json": "./package.json"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"index.js",
|
|
35
|
+
"client.js",
|
|
36
|
+
"cordis.patch.yml",
|
|
37
|
+
"server",
|
|
38
|
+
"locale/*.json",
|
|
39
|
+
"docs",
|
|
40
|
+
"skills",
|
|
41
|
+
"icon.svg",
|
|
42
|
+
"README.md",
|
|
43
|
+
"README.zh.md",
|
|
44
|
+
"CHANGELOG.md",
|
|
45
|
+
"LICENSE"
|
|
46
|
+
],
|
|
47
|
+
"dsh": {
|
|
48
|
+
"bundle": {
|
|
49
|
+
"patch": "./cordis.patch.yml"
|
|
50
|
+
},
|
|
51
|
+
"client": {
|
|
52
|
+
"platform": "web",
|
|
53
|
+
"immediately": true,
|
|
54
|
+
"inject": [
|
|
55
|
+
"@deepseek-ai/dsh-client-ui-conversation"
|
|
56
|
+
]
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
"engines": {
|
|
60
|
+
"node": "\u003e=18.17.0"
|
|
61
|
+
},
|
|
62
|
+
"os": [
|
|
63
|
+
"win32"
|
|
64
|
+
],
|
|
65
|
+
"repository": {
|
|
66
|
+
"type": "git",
|
|
67
|
+
"url": "git+https://github.com/wkylnj/dsh-zcode-cua.git"
|
|
68
|
+
},
|
|
69
|
+
"bugs": {
|
|
70
|
+
"url": "https://github.com/wkylnj/dsh-zcode-cua/issues"
|
|
71
|
+
},
|
|
72
|
+
"homepage": "https://github.com/wkylnj/dsh-zcode-cua#readme"
|
|
73
|
+
}
|