@xiangyixie/dsh-superagent 0.12.0 → 0.14.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/CHANGELOG.md +175 -0
- package/LICENSE +21 -21
- package/README.md +522 -40
- package/bin/archive.js +185 -185
- package/bin/doctor.js +84 -84
- package/bin/sync.js +187 -187
- package/bin/workbench.js +98 -98
- package/cordis.patch.yml +9 -9
- package/lib/audit.js +562 -555
- package/lib/browser.js +795 -0
- package/lib/console.js +763 -0
- package/lib/cost.js +327 -267
- package/lib/cron.js +387 -0
- package/lib/desktop.js +507 -0
- package/lib/doctor.js +879 -878
- package/lib/goal.js +649 -0
- package/lib/guard.js +856 -841
- package/lib/index.js +3285 -2031
- package/lib/inject.js +138 -138
- package/lib/ledger.js +284 -270
- package/lib/memory.js +736 -736
- package/lib/orchestrator.js +5480 -3851
- package/lib/policy.js +739 -739
- package/lib/report.js +1098 -1068
- package/lib/runtime-control.js +1151 -0
- package/lib/schedule.js +1030 -0
- package/lib/store.js +877 -873
- package/lib/sync.js +456 -456
- package/lib/templates.js +690 -690
- package/lib/user-memory.js +587 -0
- package/lib/watch.js +818 -810
- package/lib/workbench.js +1119 -1119
- package/lib/workspace.js +494 -433
- package/package.json +101 -92
- package/public/console.html +1618 -0
package/lib/desktop.js
ADDED
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Windows desktop control without third-party packages.
|
|
3
|
+
*
|
|
4
|
+
* Every call compiles a small C# helper with `Add-Type` inside a fresh
|
|
5
|
+
* `powershell.exe` and talks to it through `user32.dll`. That is slower than a
|
|
6
|
+
* native module, but it needs no compiler on the user's machine, no npm
|
|
7
|
+
* dependency, and no long-lived process holding the desktop.
|
|
8
|
+
*
|
|
9
|
+
* Background delivery is the default: input is posted to the target window's
|
|
10
|
+
* message queue, so the user's foreground window keeps focus. A visible
|
|
11
|
+
* activation is only performed when the caller passes `activate: true`, and the
|
|
12
|
+
* audit trail records that it happened.
|
|
13
|
+
*
|
|
14
|
+
* @module dsh-superagent/desktop
|
|
15
|
+
*/
|
|
16
|
+
import { spawn } from 'node:child_process'
|
|
17
|
+
import { mkdirSync } from 'node:fs'
|
|
18
|
+
import fsp from 'node:fs/promises'
|
|
19
|
+
import os from 'node:os'
|
|
20
|
+
import path from 'node:path'
|
|
21
|
+
|
|
22
|
+
const POWERSHELL_TIMEOUT_MS = 30000
|
|
23
|
+
|
|
24
|
+
/** Message ids and flags the helper uses. Kept in one place so the C# and JS agree. */
|
|
25
|
+
const WM = { mousemove: 0x0200, lbuttondown: 0x0201, lbuttonup: 0x0202, char: 0x0102, keydown: 0x0100, keyup: 0x0101 }
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* C# helper. `Add-Type` compiles it once per PowerShell process; nothing is
|
|
29
|
+
* written to disk and nothing persists between calls.
|
|
30
|
+
*/
|
|
31
|
+
const HELPER_SOURCE = String.raw`
|
|
32
|
+
using System;
|
|
33
|
+
using System.Text;
|
|
34
|
+
using System.Drawing;
|
|
35
|
+
using System.Drawing.Imaging;
|
|
36
|
+
using System.Collections.Generic;
|
|
37
|
+
using System.Runtime.InteropServices;
|
|
38
|
+
|
|
39
|
+
public class DshDesktop {
|
|
40
|
+
public delegate bool EnumProc(IntPtr hWnd, IntPtr lParam);
|
|
41
|
+
|
|
42
|
+
[StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left, Top, Right, Bottom; }
|
|
43
|
+
[StructLayout(LayoutKind.Sequential)] public struct POINT { public int X, Y; }
|
|
44
|
+
[StructLayout(LayoutKind.Sequential)] public struct GUITHREADINFO {
|
|
45
|
+
public uint cbSize; public uint flags;
|
|
46
|
+
public IntPtr hwndActive, hwndFocus, hwndCapture, hwndMenuOwner, hwndMoveSize, hwndCaret;
|
|
47
|
+
public RECT rcCaret;
|
|
48
|
+
}
|
|
49
|
+
[StructLayout(LayoutKind.Sequential)] public struct MOUSEINPUT {
|
|
50
|
+
public int dx; public int dy; public uint mouseData; public uint dwFlags; public uint time; public IntPtr dwExtraInfo;
|
|
51
|
+
}
|
|
52
|
+
[StructLayout(LayoutKind.Sequential)] public struct KEYBDINPUT {
|
|
53
|
+
public ushort wVk; public ushort wScan; public uint dwFlags; public uint time; public IntPtr dwExtraInfo;
|
|
54
|
+
}
|
|
55
|
+
[StructLayout(LayoutKind.Explicit)] public struct InputUnion {
|
|
56
|
+
[FieldOffset(0)] public MOUSEINPUT mi;
|
|
57
|
+
[FieldOffset(0)] public KEYBDINPUT ki;
|
|
58
|
+
}
|
|
59
|
+
[StructLayout(LayoutKind.Sequential)] public struct INPUT { public uint type; public InputUnion U; }
|
|
60
|
+
|
|
61
|
+
[DllImport("user32.dll")] public static extern bool EnumWindows(EnumProc cb, IntPtr lParam);
|
|
62
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int GetWindowTextW(IntPtr hWnd, StringBuilder text, int max);
|
|
63
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int GetWindowTextLengthW(IntPtr hWnd);
|
|
64
|
+
[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hWnd);
|
|
65
|
+
[DllImport("user32.dll")] public static extern bool IsIconic(IntPtr hWnd);
|
|
66
|
+
[DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr hWnd, out RECT rect);
|
|
67
|
+
[DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint pid);
|
|
68
|
+
[DllImport("user32.dll")] public static extern bool ScreenToClient(IntPtr hWnd, ref POINT point);
|
|
69
|
+
[DllImport("user32.dll")] public static extern bool PostMessageW(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
|
|
70
|
+
[DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd);
|
|
71
|
+
[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int command);
|
|
72
|
+
[DllImport("user32.dll")] public static extern bool SetCursorPos(int x, int y);
|
|
73
|
+
[DllImport("user32.dll")] public static extern void mouse_event(uint flags, uint dx, uint dy, uint data, UIntPtr extra);
|
|
74
|
+
[DllImport("user32.dll")] public static extern bool GetGUIThreadInfo(uint threadId, ref GUITHREADINFO info);
|
|
75
|
+
[DllImport("user32.dll")] public static extern bool PrintWindow(IntPtr hWnd, IntPtr hdc, uint flags);
|
|
76
|
+
[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
|
|
77
|
+
[DllImport("user32.dll")] public static extern bool SetProcessDPIAware();
|
|
78
|
+
[DllImport("user32.dll")] public static extern uint SendInput(uint count, INPUT[] inputs, int size);
|
|
79
|
+
|
|
80
|
+
const uint KEYEVENTF_UNICODE = 0x0004;
|
|
81
|
+
const uint KEYEVENTF_KEYUP = 0x0002;
|
|
82
|
+
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
|
|
83
|
+
const uint MOUSEEVENTF_LEFTUP = 0x0004;
|
|
84
|
+
const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
|
|
85
|
+
const uint MOUSEEVENTF_RIGHTUP = 0x0010;
|
|
86
|
+
|
|
87
|
+
public static string[] List() {
|
|
88
|
+
List<string> rows = new List<string>();
|
|
89
|
+
EnumWindows((hWnd, lParam) => {
|
|
90
|
+
if (!IsWindowVisible(hWnd)) return true;
|
|
91
|
+
int length = GetWindowTextLengthW(hWnd);
|
|
92
|
+
if (length <= 0) return true;
|
|
93
|
+
StringBuilder text = new StringBuilder(length + 1);
|
|
94
|
+
GetWindowTextW(hWnd, text, text.Capacity);
|
|
95
|
+
string title = text.ToString();
|
|
96
|
+
if (title.Length == 0) return true;
|
|
97
|
+
RECT rect;
|
|
98
|
+
GetWindowRect(hWnd, out rect);
|
|
99
|
+
uint pid;
|
|
100
|
+
GetWindowThreadProcessId(hWnd, out pid);
|
|
101
|
+
rows.Add(string.Join("\t", new string[] {
|
|
102
|
+
hWnd.ToInt64().ToString(), title, pid.ToString(),
|
|
103
|
+
rect.Left.ToString(), rect.Top.ToString(),
|
|
104
|
+
(rect.Right - rect.Left).ToString(), (rect.Bottom - rect.Top).ToString(),
|
|
105
|
+
IsIconic(hWnd) ? "1" : "0"
|
|
106
|
+
}));
|
|
107
|
+
return true;
|
|
108
|
+
}, IntPtr.Zero);
|
|
109
|
+
return rows.ToArray();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
public static IntPtr FindByTitle(string fragment) {
|
|
113
|
+
IntPtr found = IntPtr.Zero;
|
|
114
|
+
EnumWindows((hWnd, lParam) => {
|
|
115
|
+
if (!IsWindowVisible(hWnd)) return true;
|
|
116
|
+
int length = GetWindowTextLengthW(hWnd);
|
|
117
|
+
if (length <= 0) return true;
|
|
118
|
+
StringBuilder text = new StringBuilder(length + 1);
|
|
119
|
+
GetWindowTextW(hWnd, text, text.Capacity);
|
|
120
|
+
if (text.ToString().IndexOf(fragment, StringComparison.OrdinalIgnoreCase) >= 0) {
|
|
121
|
+
found = hWnd;
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
return true;
|
|
125
|
+
}, IntPtr.Zero);
|
|
126
|
+
return found;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
public static void Activate(IntPtr hWnd) {
|
|
130
|
+
if (IsIconic(hWnd)) ShowWindow(hWnd, 9);
|
|
131
|
+
SetForegroundWindow(hWnd);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
public static bool ClickBackground(IntPtr hWnd, int screenX, int screenY) {
|
|
135
|
+
POINT point = new POINT { X = screenX, Y = screenY };
|
|
136
|
+
if (!ScreenToClient(hWnd, ref point)) return false;
|
|
137
|
+
IntPtr lParam = (IntPtr)((point.Y << 16) | (point.X & 0xFFFF));
|
|
138
|
+
PostMessageW(hWnd, 0x0200, IntPtr.Zero, lParam);
|
|
139
|
+
PostMessageW(hWnd, 0x0201, (IntPtr)1, lParam);
|
|
140
|
+
PostMessageW(hWnd, 0x0202, IntPtr.Zero, lParam);
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
public static void ClickForeground(int x, int y, bool right) {
|
|
145
|
+
SetCursorPos(x, y);
|
|
146
|
+
mouse_event(right ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_LEFTDOWN, 0, 0, 0, UIntPtr.Zero);
|
|
147
|
+
mouse_event(right ? MOUSEEVENTF_RIGHTUP : MOUSEEVENTF_LEFTUP, 0, 0, 0, UIntPtr.Zero);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
public static IntPtr FocusOf(IntPtr hWnd) {
|
|
151
|
+
uint pid;
|
|
152
|
+
uint thread = GetWindowThreadProcessId(hWnd, out pid);
|
|
153
|
+
GUITHREADINFO info = new GUITHREADINFO();
|
|
154
|
+
info.cbSize = (uint)Marshal.SizeOf(typeof(GUITHREADINFO));
|
|
155
|
+
if (!GetGUIThreadInfo(thread, ref info)) return IntPtr.Zero;
|
|
156
|
+
return info.hwndFocus != IntPtr.Zero ? info.hwndFocus : hWnd;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
public static int TypeBackground(IntPtr hWnd, string text) {
|
|
160
|
+
IntPtr target = FocusOf(hWnd);
|
|
161
|
+
foreach (char ch in text) {
|
|
162
|
+
PostMessageW(target, 0x0102, (IntPtr)ch, IntPtr.Zero);
|
|
163
|
+
}
|
|
164
|
+
return text.Length;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
public static void TypeForeground(string text) {
|
|
168
|
+
List<INPUT> inputs = new List<INPUT>();
|
|
169
|
+
foreach (char ch in text) {
|
|
170
|
+
INPUT down = new INPUT();
|
|
171
|
+
down.type = 1;
|
|
172
|
+
down.U.ki.wScan = (ushort)ch;
|
|
173
|
+
down.U.ki.dwFlags = KEYEVENTF_UNICODE;
|
|
174
|
+
inputs.Add(down);
|
|
175
|
+
INPUT up = new INPUT();
|
|
176
|
+
up.type = 1;
|
|
177
|
+
up.U.ki.wScan = (ushort)ch;
|
|
178
|
+
up.U.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
|
|
179
|
+
inputs.Add(up);
|
|
180
|
+
}
|
|
181
|
+
if (inputs.Count > 0) SendInput((uint)inputs.Count, inputs.ToArray(), Marshal.SizeOf(typeof(INPUT)));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
public static bool KeyBackground(IntPtr hWnd, int virtualKey) {
|
|
185
|
+
IntPtr target = FocusOf(hWnd);
|
|
186
|
+
PostMessageW(target, 0x0100, (IntPtr)virtualKey, IntPtr.Zero);
|
|
187
|
+
PostMessageW(target, 0x0101, (IntPtr)virtualKey, IntPtr.Zero);
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
public static void KeyForeground(int virtualKey) {
|
|
192
|
+
INPUT down = new INPUT();
|
|
193
|
+
down.type = 1;
|
|
194
|
+
down.U.ki.wVk = (ushort)virtualKey;
|
|
195
|
+
INPUT up = new INPUT();
|
|
196
|
+
up.type = 1;
|
|
197
|
+
up.U.ki.wVk = (ushort)virtualKey;
|
|
198
|
+
up.U.ki.dwFlags = KEYEVENTF_KEYUP;
|
|
199
|
+
SendInput(2, new INPUT[] { down, up }, Marshal.SizeOf(typeof(INPUT)));
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
public static bool CaptureWindow(IntPtr hWnd, string file) {
|
|
203
|
+
RECT rect;
|
|
204
|
+
if (!GetWindowRect(hWnd, out rect)) return false;
|
|
205
|
+
int width = rect.Right - rect.Left;
|
|
206
|
+
int height = rect.Bottom - rect.Top;
|
|
207
|
+
if (width <= 0 || height <= 0) return false;
|
|
208
|
+
using (Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb)) {
|
|
209
|
+
using (Graphics graphics = Graphics.FromImage(bitmap)) {
|
|
210
|
+
IntPtr hdc = graphics.GetHdc();
|
|
211
|
+
// 2 = PW_RENDERFULLCONTENT, so a covered window still yields its own pixels.
|
|
212
|
+
bool ok = PrintWindow(hWnd, hdc, 2);
|
|
213
|
+
graphics.ReleaseHdc(hdc);
|
|
214
|
+
if (!ok) return false;
|
|
215
|
+
}
|
|
216
|
+
bitmap.Save(file, ImageFormat.Png);
|
|
217
|
+
}
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
public static bool CaptureScreen(string file, int x, int y, int width, int height) {
|
|
222
|
+
if (width <= 0 || height <= 0) return false;
|
|
223
|
+
using (Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb)) {
|
|
224
|
+
using (Graphics graphics = Graphics.FromImage(bitmap)) {
|
|
225
|
+
graphics.CopyFromScreen(x, y, 0, 0, new Size(width, height));
|
|
226
|
+
}
|
|
227
|
+
bitmap.Save(file, ImageFormat.Png);
|
|
228
|
+
}
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
`
|
|
233
|
+
|
|
234
|
+
const PRELUDE = String.raw`
|
|
235
|
+
$ErrorActionPreference = 'Stop'
|
|
236
|
+
$ProgressPreference = 'SilentlyContinue'
|
|
237
|
+
# Window titles are frequently non-ASCII. Without this the JSON reply comes back
|
|
238
|
+
# in the OEM code page and every Chinese or accented title is corrupted.
|
|
239
|
+
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
|
240
|
+
Add-Type -AssemblyName System.Drawing
|
|
241
|
+
Add-Type -AssemblyName System.Windows.Forms
|
|
242
|
+
Add-Type -TypeDefinition @'
|
|
243
|
+
${HELPER_SOURCE}
|
|
244
|
+
'@ -ReferencedAssemblies System.Drawing
|
|
245
|
+
[DshDesktop]::SetProcessDPIAware() | Out-Null
|
|
246
|
+
$payload = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($env:DSH_DESKTOP_PAYLOAD)) | ConvertFrom-Json
|
|
247
|
+
`
|
|
248
|
+
|
|
249
|
+
/** Emit one JSON reply so the caller never has to parse prose. */
|
|
250
|
+
const REPLY = String.raw`
|
|
251
|
+
function Reply($object) { $object | ConvertTo-Json -Compress -Depth 6; exit 0 }
|
|
252
|
+
`
|
|
253
|
+
|
|
254
|
+
const SCRIPTS = {
|
|
255
|
+
list: `${PRELUDE}${REPLY}
|
|
256
|
+
$rows = [DshDesktop]::List()
|
|
257
|
+
$items = @()
|
|
258
|
+
foreach ($row in $rows) {
|
|
259
|
+
$parts = $row.Split("\t")
|
|
260
|
+
$items += [pscustomobject]@{
|
|
261
|
+
hwnd = $parts[0]; title = $parts[1]; pid = [int]$parts[2]
|
|
262
|
+
rect = [pscustomobject]@{ x = [int]$parts[3]; y = [int]$parts[4]; width = [int]$parts[5]; height = [int]$parts[6] }
|
|
263
|
+
minimized = ($parts[7] -eq '1')
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
Reply ([pscustomobject]@{ ok = $true; windows = $items })
|
|
267
|
+
`,
|
|
268
|
+
screenshot: `${PRELUDE}${REPLY}
|
|
269
|
+
if ($payload.window) {
|
|
270
|
+
$hwnd = [DshDesktop]::FindByTitle([string]$payload.window)
|
|
271
|
+
if ($hwnd -eq [IntPtr]::Zero) { Reply ([pscustomobject]@{ ok = $false; error = "no visible window title contains '$($payload.window)'" }) }
|
|
272
|
+
if (-not [DshDesktop]::CaptureWindow($hwnd, [string]$payload.path)) {
|
|
273
|
+
Reply ([pscustomobject]@{ ok = $false; error = 'PrintWindow could not capture that window' })
|
|
274
|
+
}
|
|
275
|
+
Reply ([pscustomobject]@{ ok = $true; path = [string]$payload.path; window = [string]$payload.window; scope = 'window' })
|
|
276
|
+
}
|
|
277
|
+
$bounds = [System.Windows.Forms.SystemInformation]::VirtualScreen
|
|
278
|
+
if (-not [DshDesktop]::CaptureScreen([string]$payload.path, $bounds.X, $bounds.Y, $bounds.Width, $bounds.Height)) {
|
|
279
|
+
Reply ([pscustomobject]@{ ok = $false; error = 'CopyFromScreen failed' })
|
|
280
|
+
}
|
|
281
|
+
Reply ([pscustomobject]@{ ok = $true; path = [string]$payload.path; scope = 'screen'; rect = [pscustomobject]@{ x = $bounds.X; y = $bounds.Y; width = $bounds.Width; height = $bounds.Height } })
|
|
282
|
+
`,
|
|
283
|
+
click: `${PRELUDE}${REPLY}
|
|
284
|
+
$hwnd = [IntPtr]::Zero
|
|
285
|
+
if ($payload.window) {
|
|
286
|
+
$hwnd = [DshDesktop]::FindByTitle([string]$payload.window)
|
|
287
|
+
if ($hwnd -eq [IntPtr]::Zero) { Reply ([pscustomobject]@{ ok = $false; error = "no visible window title contains '$($payload.window)'" }) }
|
|
288
|
+
}
|
|
289
|
+
if ($payload.activate) {
|
|
290
|
+
if ($hwnd -ne [IntPtr]::Zero) { [DshDesktop]::Activate($hwnd) }
|
|
291
|
+
[DshDesktop]::ClickForeground([int]$payload.x, [int]$payload.y, [bool]$payload.right)
|
|
292
|
+
Reply ([pscustomobject]@{ ok = $true; mode = 'foreground'; x = [int]$payload.x; y = [int]$payload.y; activated = ($hwnd -ne [IntPtr]::Zero) })
|
|
293
|
+
}
|
|
294
|
+
if ($hwnd -eq [IntPtr]::Zero) { Reply ([pscustomobject]@{ ok = $false; error = 'a background click needs window (window title) so it can be posted without stealing focus' }) }
|
|
295
|
+
if (-not [DshDesktop]::ClickBackground($hwnd, [int]$payload.x, [int]$payload.y)) {
|
|
296
|
+
Reply ([pscustomobject]@{ ok = $false; error = 'ScreenToClient refused the point' })
|
|
297
|
+
}
|
|
298
|
+
Reply ([pscustomobject]@{ ok = $true; mode = 'background'; x = [int]$payload.x; y = [int]$payload.y; hwnd = $hwnd.ToInt64() })
|
|
299
|
+
`,
|
|
300
|
+
type: `${PRELUDE}${REPLY}
|
|
301
|
+
$hwnd = [IntPtr]::Zero
|
|
302
|
+
if ($payload.window) {
|
|
303
|
+
$hwnd = [DshDesktop]::FindByTitle([string]$payload.window)
|
|
304
|
+
if ($hwnd -eq [IntPtr]::Zero) { Reply ([pscustomobject]@{ ok = $false; error = "no visible window title contains '$($payload.window)'" }) }
|
|
305
|
+
}
|
|
306
|
+
if ($payload.activate) {
|
|
307
|
+
if ($hwnd -ne [IntPtr]::Zero) { [DshDesktop]::Activate($hwnd) }
|
|
308
|
+
[DshDesktop]::TypeForeground([string]$payload.text)
|
|
309
|
+
Reply ([pscustomobject]@{ ok = $true; mode = 'foreground'; chars = ([string]$payload.text).Length; activated = ($hwnd -ne [IntPtr]::Zero) })
|
|
310
|
+
}
|
|
311
|
+
if ($hwnd -eq [IntPtr]::Zero) { Reply ([pscustomobject]@{ ok = $false; error = 'background typing needs window (window title)' }) }
|
|
312
|
+
$count = [DshDesktop]::TypeBackground($hwnd, [string]$payload.text)
|
|
313
|
+
Reply ([pscustomobject]@{ ok = $true; mode = 'background'; chars = $count; hwnd = $hwnd.ToInt64() })
|
|
314
|
+
`,
|
|
315
|
+
keys: `${PRELUDE}${REPLY}
|
|
316
|
+
$hwnd = [IntPtr]::Zero
|
|
317
|
+
if ($payload.window) {
|
|
318
|
+
$hwnd = [DshDesktop]::FindByTitle([string]$payload.window)
|
|
319
|
+
if ($hwnd -eq [IntPtr]::Zero) { Reply ([pscustomobject]@{ ok = $false; error = "no visible window title contains '$($payload.window)'" }) }
|
|
320
|
+
}
|
|
321
|
+
if ($payload.activate) {
|
|
322
|
+
if ($hwnd -ne [IntPtr]::Zero) { [DshDesktop]::Activate($hwnd) }
|
|
323
|
+
foreach ($key in $payload.virtualKeys) { [DshDesktop]::KeyForeground([int]$key) }
|
|
324
|
+
Reply ([pscustomobject]@{ ok = $true; mode = 'foreground'; keys = $payload.virtualKeys.Count; activated = ($hwnd -ne [IntPtr]::Zero) })
|
|
325
|
+
}
|
|
326
|
+
if ($hwnd -eq [IntPtr]::Zero) { Reply ([pscustomobject]@{ ok = $false; error = 'background key presses need window (window title)' }) }
|
|
327
|
+
foreach ($key in $payload.virtualKeys) { [DshDesktop]::KeyBackground($hwnd, [int]$key) | Out-Null }
|
|
328
|
+
Reply ([pscustomobject]@{ ok = $true; mode = 'background'; keys = $payload.virtualKeys.Count; hwnd = $hwnd.ToInt64() })
|
|
329
|
+
`,
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Directory PowerShell may use for its compile scratch file.
|
|
334
|
+
*
|
|
335
|
+
* `Add-Type` writes a `.cs` file into `%TEMP%` and then compiles it. Under a
|
|
336
|
+
* restricted token that can read `C:\Windows\TEMP` but not create files there,
|
|
337
|
+
* the compiler reports "source file not found" — a confusing error that looks
|
|
338
|
+
* like a C# syntax problem. Pointing TEMP at a directory the user owns avoids
|
|
339
|
+
* it. This also keeps the helper working when the host runs inside a sandbox.
|
|
340
|
+
*/
|
|
341
|
+
let powershellTemp = null
|
|
342
|
+
function tempDirForPowerShell() {
|
|
343
|
+
if (powershellTemp) return powershellTemp
|
|
344
|
+
const base = process.env.LOCALAPPDATA || os.tmpdir()
|
|
345
|
+
const dir = path.join(base, 'Temp', 'dsh-runtime-control')
|
|
346
|
+
mkdirSync(dir, { recursive: true })
|
|
347
|
+
powershellTemp = dir
|
|
348
|
+
return dir
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Run one helper script with a JSON payload and parse its JSON reply.
|
|
353
|
+
* @param {string} name - key of {@link SCRIPTS}.
|
|
354
|
+
* @param {object} payload
|
|
355
|
+
* @param {{ timeoutMs?: number, env?: NodeJS.ProcessEnv }} [options]
|
|
356
|
+
*/
|
|
357
|
+
export async function runDesktopScript(name, payload, { timeoutMs = POWERSHELL_TIMEOUT_MS, env = process.env } = {}) {
|
|
358
|
+
const script = SCRIPTS[name]
|
|
359
|
+
if (!script) throw new Error(`no desktop script named ${name}`)
|
|
360
|
+
const encoded = Buffer.from(script, 'utf16le').toString('base64')
|
|
361
|
+
const temp = tempDirForPowerShell()
|
|
362
|
+
const child = spawn('powershell.exe', [
|
|
363
|
+
'-NoProfile',
|
|
364
|
+
'-NonInteractive',
|
|
365
|
+
'-ExecutionPolicy', 'Bypass',
|
|
366
|
+
'-EncodedCommand', encoded,
|
|
367
|
+
], {
|
|
368
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
369
|
+
windowsHide: true,
|
|
370
|
+
env: {
|
|
371
|
+
...env,
|
|
372
|
+
TEMP: temp,
|
|
373
|
+
TMP: temp,
|
|
374
|
+
DSH_DESKTOP_PAYLOAD: Buffer.from(JSON.stringify(payload ?? {}), 'utf8').toString('base64'),
|
|
375
|
+
},
|
|
376
|
+
})
|
|
377
|
+
let stdout = ''
|
|
378
|
+
let stderr = ''
|
|
379
|
+
child.stdout.on('data', chunk => {
|
|
380
|
+
stdout += String(chunk)
|
|
381
|
+
})
|
|
382
|
+
child.stderr.on('data', chunk => {
|
|
383
|
+
stderr += String(chunk)
|
|
384
|
+
})
|
|
385
|
+
const timer = setTimeout(() => child.kill(), timeoutMs)
|
|
386
|
+
const code = await new Promise(resolve => child.on('close', resolve))
|
|
387
|
+
clearTimeout(timer)
|
|
388
|
+
const text = stdout.trim()
|
|
389
|
+
if (!text) {
|
|
390
|
+
throw new Error(
|
|
391
|
+
`powershell produced no result (exit ${code})${stderr ? `: ${stderr.trim().slice(0, 1500)}` : ''}`,
|
|
392
|
+
)
|
|
393
|
+
}
|
|
394
|
+
let parsed
|
|
395
|
+
try {
|
|
396
|
+
parsed = JSON.parse(text.split('\n').at(-1))
|
|
397
|
+
} catch {
|
|
398
|
+
throw new Error(`powershell output was not JSON: ${text.slice(0, 300)}`)
|
|
399
|
+
}
|
|
400
|
+
if (parsed?.ok === false) throw new Error(parsed.error ?? 'the desktop helper refused the action')
|
|
401
|
+
return parsed
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** Snapshot of every visible titled top-level window. */
|
|
405
|
+
export async function listWindows(options = {}) {
|
|
406
|
+
const result = await runDesktopScript('list', {}, options)
|
|
407
|
+
return result.windows ?? []
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Capture the screen or one window.
|
|
412
|
+
* @param {{ path: string, window?: string|null, timeoutMs?: number }} options
|
|
413
|
+
*/
|
|
414
|
+
export async function captureScreenshot({ path: file, window = null, ...options }) {
|
|
415
|
+
await fsp.mkdir(path.dirname(file), { recursive: true })
|
|
416
|
+
return runDesktopScript('screenshot', { path: file, window }, options)
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Click at screen coordinates.
|
|
421
|
+
* @param {{ x: number, y: number, window?: string|null, activate?: boolean, right?: boolean }} options
|
|
422
|
+
*/
|
|
423
|
+
export async function clickAt({ x, y, window = null, activate = false, right = false, ...options }) {
|
|
424
|
+
return runDesktopScript('click', { x, y, window, activate, right }, options)
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Type text into a window.
|
|
429
|
+
* @param {{ text: string, window?: string|null, activate?: boolean }} options
|
|
430
|
+
*/
|
|
431
|
+
export async function typeText({ text, window = null, activate = false, ...options }) {
|
|
432
|
+
return runDesktopScript('type', { text, window, activate }, options)
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Press virtual-key codes (see {@link VIRTUAL_KEYS}).
|
|
437
|
+
* @param {{ virtualKeys: number[], window?: string|null, activate?: boolean }} options
|
|
438
|
+
*/
|
|
439
|
+
export async function pressKeys({ virtualKeys, window = null, activate = false, ...options }) {
|
|
440
|
+
return runDesktopScript('keys', { virtualKeys, window, activate }, options)
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/** The subset of virtual-key codes the key tool accepts by name. */
|
|
444
|
+
export const VIRTUAL_KEYS = {
|
|
445
|
+
enter: 13,
|
|
446
|
+
tab: 9,
|
|
447
|
+
escape: 27,
|
|
448
|
+
esc: 27,
|
|
449
|
+
space: 32,
|
|
450
|
+
backspace: 8,
|
|
451
|
+
delete: 46,
|
|
452
|
+
up: 38,
|
|
453
|
+
down: 40,
|
|
454
|
+
left: 37,
|
|
455
|
+
right: 39,
|
|
456
|
+
home: 36,
|
|
457
|
+
end: 35,
|
|
458
|
+
pageup: 33,
|
|
459
|
+
pagedown: 34,
|
|
460
|
+
f1: 112,
|
|
461
|
+
f2: 113,
|
|
462
|
+
f3: 114,
|
|
463
|
+
f4: 115,
|
|
464
|
+
f5: 116,
|
|
465
|
+
f6: 117,
|
|
466
|
+
f7: 118,
|
|
467
|
+
f8: 119,
|
|
468
|
+
f9: 120,
|
|
469
|
+
f10: 121,
|
|
470
|
+
f11: 122,
|
|
471
|
+
f12: 123,
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Resolve a key name such as `enter`, `A`, or `Ctrl+A` into virtual-key codes.
|
|
476
|
+
* A chord is rejected: the helper presses keys one after another, so a chord
|
|
477
|
+
* would be a lie about what the desktop received.
|
|
478
|
+
* @param {string[]} names
|
|
479
|
+
* @returns {number[]}
|
|
480
|
+
*/
|
|
481
|
+
export function resolveVirtualKeys(names) {
|
|
482
|
+
const out = []
|
|
483
|
+
for (const raw of names) {
|
|
484
|
+
const name = String(raw ?? '').trim()
|
|
485
|
+
if (!name) continue
|
|
486
|
+
const lower = name.toLowerCase()
|
|
487
|
+
if (Object.hasOwn(VIRTUAL_KEYS, lower)) {
|
|
488
|
+
out.push(VIRTUAL_KEYS[lower])
|
|
489
|
+
continue
|
|
490
|
+
}
|
|
491
|
+
if (/^[a-z]$/i.test(name)) {
|
|
492
|
+
out.push(name.toUpperCase().charCodeAt(0))
|
|
493
|
+
continue
|
|
494
|
+
}
|
|
495
|
+
if (/^\d$/.test(name)) {
|
|
496
|
+
out.push(name.charCodeAt(0))
|
|
497
|
+
continue
|
|
498
|
+
}
|
|
499
|
+
throw new Error(
|
|
500
|
+
`unknown key "${name}"; use a letter, a digit, or one of: ${Object.keys(VIRTUAL_KEYS).join(', ')}`,
|
|
501
|
+
)
|
|
502
|
+
}
|
|
503
|
+
if (out.length === 0) throw new Error('keys must name at least one key')
|
|
504
|
+
return out
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
export { WM, POWERSHELL_TIMEOUT_MS }
|