dsh-native-uia 0.2.0 → 0.6.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.
Files changed (55) hide show
  1. package/README.md +81 -398
  2. package/client/client.js +236 -48
  3. package/cordis.patch.yml +7 -8
  4. package/locale/en.json +2 -2
  5. package/locale/zh.json +2 -2
  6. package/package.json +8 -17
  7. package/scripts/check-lease.mjs +1 -1
  8. package/scripts/lib/dsh-tools-stub.mjs +14 -0
  9. package/scripts/lib/load-tools-source.mjs +17 -16
  10. package/scripts/lib/sources.mjs +62 -0
  11. package/scripts/load-tools.mjs +74 -0
  12. package/scripts/measure-parse.mjs +2 -2
  13. package/scripts/regression-tools.mjs +155 -6
  14. package/scripts/regression-worker.ps1 +29 -0
  15. package/scripts/verify-live-style.mjs +46 -0
  16. package/scripts/verify-live.mjs +12 -18
  17. package/scripts/verify.mjs +953 -192
  18. package/src/activity-window.js +71 -7
  19. package/src/dashboard.js +27 -69
  20. package/src/guard.js +39 -10
  21. package/src/index.js +187 -77
  22. package/src/notes.js +83 -0
  23. package/src/state.js +97 -10
  24. package/src/themes.js +66 -0
  25. package/src/tools/defs/act.js +671 -0
  26. package/src/tools/defs/lifecycle.js +205 -0
  27. package/src/tools/defs/observe.js +325 -0
  28. package/src/tools/defs/pointer.js +328 -0
  29. package/src/tools/format.js +338 -0
  30. package/src/tools/index.js +125 -0
  31. package/src/tools/ocr.js +91 -0
  32. package/src/tools/recipes.js +49 -0
  33. package/src/tools/runtime.js +42 -0
  34. package/src/worker-client.js +32 -10
  35. package/worker/assets/dsh-whale.path +1 -0
  36. package/worker/indicator-server.ps1 +1107 -72
  37. package/worker/lib/01-core.ps1 +64 -0
  38. package/worker/lib/02-uia.ps1 +224 -0
  39. package/worker/lib/03-window.ps1 +194 -0
  40. package/worker/lib/04-match.ps1 +435 -0
  41. package/worker/lib/05-value.ps1 +254 -0
  42. package/worker/lib/06-snapshot.ps1 +412 -0
  43. package/worker/lib/07-action.ps1 +329 -0
  44. package/worker/lib/08-command.ps1 +1495 -0
  45. package/worker/lib/09-pointer.ps1 +840 -0
  46. package/worker/lib/10-dispatch.ps1 +132 -0
  47. package/worker/ps-stdio.ps1 +7 -2
  48. package/worker/uia-worker.ps1 +599 -4220
  49. package/locale/desktop/en.json +0 -6
  50. package/locale/desktop/zh.json +0 -6
  51. package/scripts/regression-halt-latch.mjs +0 -100
  52. package/src/native-uia-remote.js +0 -80
  53. package/src/panel-face.js +0 -14
  54. package/src/tools.js +0 -1482
  55. package/web/control.html +0 -1413
@@ -0,0 +1,328 @@
1
+ import { classifyKeys } from '../../guard.js';
2
+ import { defineTool } from '@deepseek-ai/dsh-tools';
3
+ import { STR, INT, BOOL, text, str, aborted, DISARMED_MSG } from '../runtime.js';
4
+ import { stuckNote } from '../format.js';
5
+
6
+ /**
7
+ * Tools of the pointer domain. Every one takes the shared `d` bundle built by registerTools: the
8
+ * register wrapper (idle touch + dispose tracking), the worker, the store and the run's finish
9
+ * hook. Policy is never decided here — `d.refuse` renders what guard.js already judged.
10
+ */
11
+ export function registerPointer(d) {
12
+ const { worker, store, register, refuse, touch, ensureArmed } = d;
13
+ // The activation permission gate: every delivery tool calls this first with its exec.agent. The
14
+ // FIRST call in the session draws the host question card (input-box上方), the verdict is cached,
15
+ // and a refusal ends the tool with an honest message instead of touching the desktop.
16
+ const gate = async (exec) => {
17
+ if (await ensureArmed(exec?.agent, exec?.signal)) return true;
18
+ return DISARMED_MSG;
19
+ };
20
+ // One normaliser for every tool that takes held keys: array or "ctrl+shift" string both work, and
21
+ // the worker re-validates (an unknown modifier becomes bad_arg there, not a silent no-op here).
22
+ const normMods = (value) => {
23
+ if (value === undefined || value === null || value === '') return [];
24
+ const list = Array.isArray(value) ? value : String(value).split('+');
25
+ return list.map((m) => String(m).trim().toLowerCase()).filter(Boolean);
26
+ };
27
+ // The blocks below keep their original indentation so the gate regexes (which were written
28
+ // against the single-file source) still match the concatenated view byte for byte.
29
+
30
+
31
+ // ---- physical (foreground) action tools: the human-action set -------------------------------
32
+ // These drive the real cursor/keyboard and are visible. Coordinates are physical screen pixels, the
33
+ // same space snapshot rects and find_text centers use. Every click/drag/scroll MUST name the window
34
+ // it lands in (expect.windowId) so it can never click blind across the whole desktop, and the point
35
+ // is run through the same destructive-text guard by resolving what is actually under it first.
36
+ const classifyPoint = async (x, y) => {
37
+ const t = await worker.call('pointer_target', { x, y });
38
+ const under = t?.under ?? t;
39
+ return classify({ action: 'invoke', descriptor: { title: under?.title, className: under?.class ?? under?.hitClass, name: under?.app } });
40
+ };
41
+ const pointArgs = (args) => ({ x: Number(args.x), y: Number(args.y) });
42
+ // A ref is a better pointer target than arithmetic: the centre comes from the element's own rect,
43
+ // read fresh through the worker's describe (Resolve-Ref re-binds and refuses a stale ref), so a
44
+ // row that moved or a list that scrolled cannot turn into a click at the old coordinates. x/y
45
+ // stays for surfaces with no element at all (canvas, OCR hit).
46
+ const refPoint = async (ref) => {
47
+ const d = await worker.call('describe', { ref: String(ref) });
48
+ const r = d?.rect;
49
+ if (!r || !(r.w > 0) || !(r.h > 0)) return null;
50
+ return { x: Math.round(r.x + r.w / 2), y: Math.round(r.y + r.h / 2), controlType: d.controlType, name: d.name };
51
+ };
52
+ const resolvePoint = async (args) => {
53
+ if (args.ref !== undefined && String(args.ref) !== '') {
54
+ const p = await refPoint(args.ref);
55
+ if (p === null) return { error: `ref ${args.ref} 没有可点的矩形(元素已不可见或没有布局框):改用 x,y,或重新 computer_snapshot 后再操作` };
56
+ return p;
57
+ }
58
+ if (typeof args.x !== 'number' || typeof args.y !== 'number') {
59
+ return { error: '需要 ref(推荐,中心由元素 rect 现算)或数值坐标 x、y(物理像素,和快照 rect / find_text center 同一坐标空间)' };
60
+ }
61
+ return pointArgs(args);
62
+ };
63
+
64
+
65
+ d.disposers.push(register(defineTool({
66
+ name: 'computer_click',
67
+ description: 'Move the real cursor and click. Target it EITHER with ref (preferred: the centre is computed from the element\'s own rect on this call, so a moved row or a scrolled list still lands correctly) OR with physical (x,y) for surfaces that have no element (canvas, OCR hit, screenshot). Use it for self-drawn UIs (WeChat), for right/double/triple clicks, or when the action must be visible. windowId is required and the click is refused if that window is not actually under the point.',
68
+ parameters: {
69
+ windowId: { ...STR, required: true, description: 'The window you expect to be under the point; used as the target gate' },
70
+ ref: { ...STR, description: 'Element ref from the newest snapshot of this window — alternative to x,y; the (x,y) is derived from its current rect' },
71
+ x: { ...INT, description: 'physical x (omit when passing ref)' }, y: { ...INT, description: 'physical y (omit when passing ref)' },
72
+ button: { type: 'string', enum: ['left', 'right', 'middle'], description: 'default left' },
73
+ double: { ...BOOL, description: 'double-click' },
74
+ triple: { ...BOOL, description: 'triple-click (select a paragraph / whole line); implies not double' },
75
+ modifiers: { type: 'array', items: { type: 'string' }, description: 'held during the click: ctrl / shift / alt / win — e.g. ["ctrl"] to add to a selection' },
76
+ durationMs: { ...INT, description: 'cursor glide time (default auto by distance)' },
77
+ },
78
+ output: { schema: STR, render: (_a, v) => text(v) },
79
+ timeoutMs: 30000,
80
+ async execute(args, exec) {
81
+ await aborted(exec.signal);
82
+ if (!(await gate(exec))) return DISARMED_MSG;
83
+ const p = await resolvePoint(args);
84
+ if (p.error) return p.error;
85
+ const { x, y } = p;
86
+ const outcome = await classifyPoint(x, y);
87
+ if (outcome.verdict === 'deny') return refuse(outcome, { tool: 'computer_click', ...args });
88
+ const r = await worker.call('pointer_click', { x, y, button: args.button, double: args.double, triple: args.triple, mods: normMods(args.modifiers), durationMs: args.durationMs, expect: { windowId: String(args.windowId) } });
89
+ store.actionDone(1);
90
+ store.step({ label: `点击 ${args.ref ? args.ref : ''}(${x},${y})`, kind: 'pointer', status: 'passed', ms: r.ms, detail: `via=${r.via} under=${str(r.under?.title ?? r.under?.class, 40)}` });
91
+ const held = r.mods ? ` 按住 ${r.mods}` : '';
92
+ const kind = r.triple ? ' 三击' : r.double ? ' 双击' : '';
93
+ const on = args.ref ? ` ${args.ref}${p.name ? `「${str(p.name, 30)}」` : ''}` : '';
94
+ // Same point, same button, over and over: the third repeat is a stuck loop, not a retry.
95
+ const tally = store.noteAttempt(`click:${x},${y}:${args.button ?? 'left'}`, true);
96
+ const stuck = stuckNote('repeat', tally.total);
97
+ return `已点击${on} (${x},${y}) ${args.button || 'left'}${kind}${held},耗时 ${r.ms}ms,落点窗口 "${str(r.under?.title ?? '', 50)}"${stuck}`;
98
+ },
99
+ })));
100
+
101
+
102
+ d.disposers.push(register(defineTool({
103
+ name: 'computer_drag',
104
+ description: 'Press at a start point, move to an end point, release — for sliders, repositioning, marquee-select. Either point can be a ref (centre derived from its current rect) or explicit physical coordinates; both must be inside windowId.',
105
+ parameters: {
106
+ windowId: { ...STR, required: true },
107
+ ref: { ...STR, description: 'ref to press at (alternative to fromX/fromY)' },
108
+ fromX: { ...INT, description: 'physical start x (omit when passing ref)' }, fromY: { ...INT, description: 'physical start y (omit when passing ref)' },
109
+ toRef: { ...STR, description: 'ref to release at (alternative to toX/toY)' },
110
+ toX: { ...INT, description: 'physical end x (omit when passing toRef)' }, toY: { ...INT, description: 'physical end y (omit when passing toRef)' },
111
+ button: { type: 'string', enum: ['left', 'right', 'middle'] },
112
+ durationMs: { ...INT },
113
+ },
114
+ output: { schema: STR, render: (_a, v) => text(v) },
115
+ timeoutMs: 30000,
116
+ async execute(args, exec) {
117
+ await aborted(exec.signal);
118
+ if (!(await gate(exec))) return DISARMED_MSG;
119
+ const from = await resolvePoint({ ref: args.ref, x: args.fromX, y: args.fromY });
120
+ if (from.error) return `起点:${from.error}`;
121
+ const to = await resolvePoint({ ref: args.toRef, x: args.toX, y: args.toY });
122
+ if (to.error) return `终点:${to.error}`;
123
+ const outcome = await classifyPoint(from.x, from.y);
124
+ if (outcome.verdict === 'deny') return refuse(outcome, { tool: 'computer_drag', ...args });
125
+ const r = await worker.call('pointer_drag', { from: { x: from.x, y: from.y }, to: { x: to.x, y: to.y }, button: args.button, durationMs: args.durationMs, expect: { windowId: String(args.windowId) } });
126
+ store.actionDone(1);
127
+ store.step({ label: `拖拽 (${from.x},${from.y})→(${to.x},${to.y})`, kind: 'pointer', status: 'passed', ms: r.ms });
128
+ return `已拖拽 (${from.x},${from.y}) → (${to.x},${to.y}),耗时 ${r.ms ?? '?'}ms`;
129
+ },
130
+ })));
131
+
132
+
133
+ d.disposers.push(register(defineTool({
134
+ name: 'computer_scroll',
135
+ description: 'Scroll the mouse wheel at a point: direction up|down|left|right, clicks = notches (default 3). The point is either a ref (its rect centre) or physical (x,y); windowId gates it.',
136
+ parameters: {
137
+ windowId: { ...STR, required: true },
138
+ ref: { ...STR, description: 'ref whose centre to scroll over (alternative to x,y)' },
139
+ x: { ...INT, description: 'physical x (omit when passing ref)' }, y: { ...INT, description: 'physical y (omit when passing ref)' },
140
+ direction: { type: 'string', enum: ['up', 'down', 'left', 'right'] },
141
+ clicks: { ...INT, description: 'wheel notches, 1..50' },
142
+ modifiers: { type: 'array', items: { type: 'string' }, description: 'held during the scroll: ["ctrl"] = zoom, ["shift"] = horizontal pan' },
143
+ },
144
+ output: { schema: STR, render: (_a, v) => text(v) },
145
+ timeoutMs: 20000,
146
+ async execute(args, exec) {
147
+ await aborted(exec.signal);
148
+ if (!(await gate(exec))) return DISARMED_MSG;
149
+ const p = await resolvePoint(args);
150
+ if (p.error) return p.error;
151
+ const { x, y } = p;
152
+ const outcome = await classifyPoint(x, y);
153
+ if (outcome.verdict === 'deny') return refuse(outcome, { tool: 'computer_scroll', ...args });
154
+ const r = await worker.call('pointer_scroll', { x, y, direction: args.direction, clicks: args.clicks, mods: normMods(args.modifiers), expect: { windowId: String(args.windowId) } });
155
+ store.actionDone(1);
156
+ store.step({ label: `滚动 ${args.direction || 'down'} @(${x},${y})`, kind: 'pointer', status: 'passed', ms: r.ms });
157
+ const held = r.mods ? `(按住 ${r.mods})` : '';
158
+ return `已在 (${x},${y}) ${args.direction || 'down'} 滚动 ${r.clicks} 格${held},耗时 ${r.ms}ms`;
159
+ },
160
+ })));
161
+
162
+
163
+ d.disposers.push(register(defineTool({
164
+ name: 'computer_type',
165
+ description: 'Type text into the focused control. Click the input first (computer_click) or pass windowId to focus it. Default via="keyboard" sends per-character Unicode events and does not touch the clipboard. For long text (>500 chars) or text containing newlines use via:"clipboard": the full text is written with Set-Clipboard and delivered as one ctrl+v paste — note it OVERWRITES the user\'s clipboard and does not restore it. After typing, computer_wait {type:"value_equals", ref} gives a deterministic check that the content landed.',
166
+ parameters: {
167
+ text: { ...STR, required: true, description: 'text to type, up to 4000 chars' },
168
+ via: { type: 'string', enum: ['keyboard', 'clipboard'], description: 'keyboard (default) = per-character events; clipboard = Set-Clipboard + ctrl+v, overwrites the user clipboard and does not restore; prefer for >500 chars or text with newlines' },
169
+ windowId: { ...STR, description: 'optional: raise+focus this window before typing' },
170
+ },
171
+ output: { schema: STR, render: (_a, v) => text(v) },
172
+ timeoutMs: 20000,
173
+ async execute(args, exec) {
174
+ await aborted(exec.signal);
175
+ if (!(await gate(exec))) return DISARMED_MSG;
176
+ const t = String(args.text ?? '');
177
+ if (t === '') return 'text 为空';
178
+ const via = args.via === 'clipboard' ? 'clipboard' : 'keyboard';
179
+ const r = await worker.call('type_text', { text: t, windowId: args.windowId ? String(args.windowId) : '', via });
180
+ store.actionDone(1);
181
+ store.step({ label: `输入 ${str(t, 24)} (${t.length} 字)`, kind: 'pointer', status: 'passed', ms: r.ms });
182
+ // The clipboard path is one paste, not 8000 key events — the honest answer names the channel
183
+ // AND the side effect the caller accepted (the clipboard is overwritten, not restored).
184
+ return via === 'clipboard'
185
+ ? `已通过剪贴板粘贴 ${t.length} 个字符(Set-Clipboard + ctrl+v),耗时 ${r.ms}ms。原剪贴板内容已被覆盖且未恢复;可用 computer_wait {type:"value_equals", ref} 确认内容已落盘。`
186
+ : `已输入 ${t.length} 个字符,耗时 ${r.ms}ms(SendInput 接受 ${r.sent} 个按键事件)`;
187
+ },
188
+ })));
189
+
190
+
191
+ d.disposers.push(register(defineTool({
192
+ name: 'computer_key',
193
+ description: 'Press one key or a hotkey combo on the physical keyboard: "enter", "tab", "esc", "f5", "down", "ctrl+c", "ctrl+shift+s", "alt+tab". Letters/digits and named keys (see worker) supported.',
194
+ parameters: {
195
+ keys: { ...STR, required: true, description: 'single key or modifiers joined by +, e.g. "ctrl+c"' },
196
+ repeat: { ...INT, description: 'press the combo N times in one call (1..100, default 1) — tab/tab/tab without three round trips' },
197
+ holdMs: { ...INT, description: 'hold the main key down this long before releasing (1..30000, default 15) — space to pan, shift to range-select' },
198
+ },
199
+ output: { schema: STR, render: (_a, v) => text(v) },
200
+ timeoutMs: 15000,
201
+ async execute(args, exec) {
202
+ await aborted(exec.signal);
203
+ if (!(await gate(exec))) return DISARMED_MSG;
204
+ const combo = String(args.keys ?? '').trim().toLowerCase();
205
+ if (combo === '') return 'keys 为空';
206
+ // The combo has no UIA descriptor to feed the name table, so classification runs on the combo
207
+ // text itself — in guard.js, like every other policy decision: the session-ending combos deny
208
+ // there with the shared CATEGORIES wording, ordinary combos pass. No policy is matched here.
209
+ const outcome = classifyKeys(combo);
210
+ if (outcome.verdict === 'deny') return refuse(outcome, { tool: 'computer_key', keys: combo });
211
+ const r = await worker.call('key', { keys: combo, repeat: args.repeat, holdMs: args.holdMs });
212
+ store.actionDone(1);
213
+ // Clipboard reads are gated on "the model itself copied something this run", and this is the
214
+ // only place that can create that permission: a ctrl+c the model pressed. It is not a grant
215
+ // for the user's earlier clipboard content; it says the CURRENT buffer came from a copy we
216
+ // just performed, so reading it back is reading our own action's result.
217
+ if (/(?:^|\+)ctrl\+c(?:\+|$)/.test(combo) || /(?:^|\+)ctrl\+insert(?:\+|$)/.test(combo)) {
218
+ store.ctrlCAt = Date.now();
219
+ }
220
+ store.step({ label: `按键 ${combo}${r.repeat > 1 ? ` ×${r.repeat}` : ''}`, kind: 'pointer', status: 'passed', ms: r.ms });
221
+ return `已按下 ${combo}${r.repeat > 1 ? ` ${r.repeat} 次` : ''},耗时 ${r.ms ?? '?'}ms`;
222
+ },
223
+ })));
224
+
225
+ d.disposers.push(register(defineTool({
226
+ name: 'computer_mouse',
227
+ description: 'Everything a click cannot express: hover (action:"move" and release nothing), press-and-hold across calls (action:"down" then a later action:"up" — marquee select, staged drag, holding a scrollbar), and reading where the cursor is (action:"position"). down/up act at the current cursor; the store tracks the pairing so a dangling press is visible.',
228
+ parameters: {
229
+ action: { type: 'string', enum: ['move', 'down', 'up', 'position'], required: true, description: 'move = hover to a ref or (x,y); down/up = press or release a button at the current cursor; position = read the cursor' },
230
+ ref: { ...STR, description: 'move only: hover this element (its rect centre) instead of x,y' },
231
+ x: { ...INT, description: 'move only: physical x (omit when passing ref)' },
232
+ y: { ...INT, description: 'move only: physical y (omit when passing ref)' },
233
+ button: { type: 'string', enum: ['left', 'right', 'middle'], description: 'down/up only, default left' },
234
+ durationMs: { ...INT, description: 'move only: glide time (default auto by distance)' },
235
+ windowId: { ...STR, description: 'gates the point: the window that must be under the cursor / destination' },
236
+ },
237
+ output: { schema: STR, render: (_a, v) => text(v) },
238
+ timeoutMs: 30000,
239
+ async execute(args, exec) {
240
+ await aborted(exec.signal);
241
+ if (!(await gate(exec))) return DISARMED_MSG;
242
+ const action = String(args.action ?? 'position').toLowerCase();
243
+ // Every cursor-moving/pressing branch needs the target gate. The worker refuses an unnamed
244
+ // target on purpose (that is what stops a stray physical click), so say so here instead of
245
+ // forwarding windowId:'' and surfacing the worker's generic refusal. Named `targetNeeded`
246
+ // because `gate` is this domain's permission helper and a local would shadow it.
247
+ const targetNeeded = args.windowId === undefined || String(args.windowId) === ''
248
+ ? 'computer_mouse 的 move/down/up 需要 windowId:它声明光标下应当是哪个窗口,worker 用它挡住误落到别的窗口上的点击。只是想读光标位置用 action:"position"。'
249
+ : null;
250
+ if (action === 'move') {
251
+ if (targetNeeded) return targetNeeded;
252
+ const p = await resolvePoint(args);
253
+ if (p.error) return p.error;
254
+ const { x, y } = p;
255
+ const outcome = await classifyPoint(x, y);
256
+ if (outcome.verdict === 'deny') return refuse(outcome, { tool: 'computer_mouse', ...args });
257
+ const r = await worker.call('pointer_mouse', { action, x, y, durationMs: args.durationMs, expect: { windowId: String(args.windowId) } });
258
+ store.actionDone(1);
259
+ store.step({ label: `悬停 (${r.atCursor?.x},${r.atCursor?.y})`, kind: 'pointer', status: 'passed', ms: r.ms, detail: `hover=${str(r.hover?.title ?? r.hover?.class, 40)}` });
260
+ return `光标已移到 (${r.atCursor?.x},${r.atCursor?.y}),耗时 ${r.ms}ms,下方窗口 "${str(r.hover?.title ?? '', 50)}"(悬停才出现的菜单/提示现在会展开)`;
261
+ }
262
+ if (action === 'position') {
263
+ const r = await worker.call('pointer_mouse', { action });
264
+ return `光标在 (${r.x},${r.y}),屏幕 ${r.screen?.w}x${r.screen?.h},下方窗口 "${str(r.under?.title ?? r.under?.class ?? '', 50)}"`;
265
+ }
266
+ // down / up: the worker checks that the declared window holds at the cursor, and the store
267
+ // tracks the pairing so a dangling press is visible instead of silent.
268
+ if (targetNeeded) return targetNeeded;
269
+ const r = await worker.call('pointer_mouse', { action, button: args.button, expect: { windowId: String(args.windowId) } });
270
+ store.actionDone(1);
271
+ const button = args.button ?? 'left';
272
+ if (action === 'down') {
273
+ store.holdButton(button);
274
+ store.step({ label: `按下 ${button} @(${r.atCursor?.x},${r.atCursor?.y})`, kind: 'pointer', status: 'passed', ms: r.ms });
275
+ return `已按下 ${button} 键于 (${r.atCursor?.x},${r.atCursor?.y}),它保持按下;用完必须用 action:"up" 释放。`;
276
+ }
277
+ const wasHeld = store.releaseButton(button);
278
+ store.step({ label: `释放 ${button} @(${r.atCursor?.x},${r.atCursor?.y})`, kind: 'pointer', status: 'passed', ms: r.ms });
279
+ return wasHeld
280
+ ? `已释放 ${button} 键于 (${r.atCursor?.x},${r.atCursor?.y})`
281
+ : `已释放 ${button} 键于 (${r.atCursor?.x},${r.atCursor?.y})(本轮没有记录到对应按下,释放仍已发送)`;
282
+ },
283
+ })));
284
+
285
+ d.disposers.push(register(defineTool({
286
+ name: 'computer_clipboard',
287
+ description: 'The Windows text clipboard. write replaces it (max 20000 chars), clear empties it, read returns the text — but read is gated to "this run copied it itself": it only answers after a computer_key ctrl+c, so the user\'s earlier clipboard contents are not exposed to a run that never asked for them. Use it to move long text between apps without 4000 tiny key events, or to capture what an app just copied.',
288
+ parameters: {
289
+ op: { type: 'string', enum: ['read', 'write', 'clear'], required: true, description: 'read | write | clear' },
290
+ text: { ...STR, description: 'write only: the text to place on the clipboard' },
291
+ },
292
+ output: { schema: STR, render: (_a, v) => text(v) },
293
+ timeoutMs: 15000,
294
+ async execute(args, exec) {
295
+ await aborted(exec.signal);
296
+ const op = String(args.op ?? '').toLowerCase();
297
+ // write/clear mutate shared state, so they pass the permission gate; read stays ungated (it
298
+ // already has its own stricter ctrl+c precondition).
299
+ if (op !== 'read' && !(await gate(exec))) return DISARMED_MSG;
300
+ if (op === 'read') {
301
+ if (!store.ctrlCAt) {
302
+ return '剪贴板读取被拦:本轮还没有自己按过 ctrl+c。先用 computer_key keys:"ctrl+c" 复制目标内容(或让用户自己复制后由用户告知),再读;这样读到的才是本轮的产物,而不是用户之前的剪贴板。';
303
+ }
304
+ const r = await worker.call('clipboard_read', {}, { timeoutMs: 9000 });
305
+ if (r.empty === true) return '剪贴板里现在是空文本(或只有非文本内容)';
306
+ const clipped = r.truncated === true ? `,已截断到 20000 字符(原长 ${r.length})` : '';
307
+ store.step({ label: '读取剪贴板', kind: 'pointer', status: 'passed', detail: `${r.length} 字符` });
308
+ return `剪贴板文本(${r.length} 字符${clipped}):\n${r.text}`;
309
+ }
310
+ if (op === 'write') {
311
+ const payload = String(args.text ?? '');
312
+ if (payload === '') return 'write 需要 text(要写进剪贴板的内容)';
313
+ const r = await worker.call('clipboard_write', { text: payload }, { timeoutMs: 9000 });
314
+ store.actionDone(1);
315
+ store.step({ label: '写入剪贴板', kind: 'pointer', status: 'passed', detail: `${r.length} 字符` });
316
+ return `已写入剪贴板 ${r.length} 字符(替换了原有内容;需要粘贴时用 computer_key keys:"ctrl+v")`;
317
+ }
318
+ if (op === 'clear') {
319
+ const r = await worker.call('clipboard_clear', {}, { timeoutMs: 9000 });
320
+ store.actionDone(1);
321
+ store.step({ label: '清空剪贴板', kind: 'pointer', status: 'passed' });
322
+ return r.cleared === true ? '剪贴板已清空' : '剪贴板清空没有回执,按未确认处理';
323
+ }
324
+ return 'op 必须是 read | write | clear';
325
+ },
326
+ })));
327
+
328
+ }