@copylee/dsh-computer-use 0.13.1 → 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/README.md +7 -7
- package/helper/Capture.cs +4 -0
- package/helper/Overlay.cs +166 -103
- package/helper/Program.cs +2 -0
- package/helper/Uia.cs +198 -5
- package/lib/index.js +2675 -2346
- package/lib/types/computer.d.ts +29 -8
- package/lib/types/overlay.d.ts +25 -1
- package/lib/types/prompt.d.ts +3 -1
- package/lib/types/tools.d.ts +2 -0
- package/lib/types/ui-diff.d.ts +15 -6
- package/package.json +1 -1
package/helper/Uia.cs
CHANGED
|
@@ -112,25 +112,218 @@ namespace DshComputerUse
|
|
|
112
112
|
if (!target.Current.IsEnabled)
|
|
113
113
|
throw new Exception("The named target is disabled");
|
|
114
114
|
object pattern = null;
|
|
115
|
-
if (action == "invoke"
|
|
116
|
-
|
|
117
|
-
|
|
115
|
+
if (action == "invoke")
|
|
116
|
+
{
|
|
117
|
+
if (!target.TryGetCurrentPattern(InvokePattern.Pattern, out pattern)) return result;
|
|
118
|
+
}
|
|
119
|
+
else if (action == "focus")
|
|
120
|
+
{
|
|
121
|
+
if (!target.Current.IsKeyboardFocusable) return result;
|
|
122
|
+
}
|
|
123
|
+
else if (action == "set_checked")
|
|
124
|
+
{
|
|
125
|
+
if (!target.TryGetCurrentPattern(TogglePattern.Pattern, out pattern)) return result;
|
|
126
|
+
}
|
|
127
|
+
else if (action == "expand" || action == "collapse")
|
|
128
|
+
{
|
|
129
|
+
if (!target.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out pattern)) return result;
|
|
130
|
+
}
|
|
131
|
+
else if (action == "select")
|
|
132
|
+
{
|
|
133
|
+
if (!target.TryGetCurrentPattern(SelectionItemPattern.Pattern, out pattern)) return result;
|
|
134
|
+
}
|
|
135
|
+
else if (action == "set_value")
|
|
136
|
+
{
|
|
137
|
+
if (IsPasswordElement(target)) throw new Exception("Refused to set value on a password field");
|
|
138
|
+
if (!target.TryGetCurrentPattern(ValuePattern.Pattern, out pattern)) return result;
|
|
139
|
+
if (((ValuePattern)pattern).Current.IsReadOnly) throw new Exception("The named target is read-only");
|
|
140
|
+
}
|
|
141
|
+
else return result;
|
|
142
|
+
|
|
118
143
|
if (!IsActionWindow(hwnd, pid, point))
|
|
119
144
|
throw new Exception("The named target window changed");
|
|
120
145
|
|
|
121
146
|
// A provider error after dispatch is indeterminate. Let it surface
|
|
122
147
|
// rather than claiming the host can safely click again.
|
|
123
|
-
if (action == "invoke")
|
|
124
|
-
|
|
148
|
+
if (action == "invoke")
|
|
149
|
+
{
|
|
150
|
+
((InvokePattern)pattern).Invoke();
|
|
151
|
+
}
|
|
152
|
+
else if (action == "focus")
|
|
125
153
|
{
|
|
126
154
|
target.SetFocus();
|
|
127
155
|
for (int i = 0; i < 10 && !target.Current.HasKeyboardFocus; i++) Thread.Sleep(25);
|
|
128
156
|
if (!target.Current.HasKeyboardFocus) throw new Exception("Could not verify focus on the named control");
|
|
129
157
|
}
|
|
158
|
+
else if (action == "set_checked")
|
|
159
|
+
{
|
|
160
|
+
bool want = Args.Bool(r, "checked", true);
|
|
161
|
+
var tp = (TogglePattern)pattern;
|
|
162
|
+
bool isOn = tp.Current.ToggleState == ToggleState.On;
|
|
163
|
+
if (isOn != want) tp.Toggle();
|
|
164
|
+
result["changed"] = isOn != want;
|
|
165
|
+
result["checked"] = want;
|
|
166
|
+
}
|
|
167
|
+
else if (action == "expand" || action == "collapse")
|
|
168
|
+
{
|
|
169
|
+
var ec = (ExpandCollapsePattern)pattern;
|
|
170
|
+
var cur = ec.Current.ExpandCollapseState;
|
|
171
|
+
bool wantExpand = action == "expand";
|
|
172
|
+
bool already = wantExpand ? (cur == ExpandCollapseState.Expanded) : (cur == ExpandCollapseState.Collapsed);
|
|
173
|
+
if (!already)
|
|
174
|
+
{
|
|
175
|
+
if (wantExpand) ec.Expand();
|
|
176
|
+
else ec.Collapse();
|
|
177
|
+
}
|
|
178
|
+
result["changed"] = !already;
|
|
179
|
+
}
|
|
180
|
+
else if (action == "select")
|
|
181
|
+
{
|
|
182
|
+
var sp = (SelectionItemPattern)pattern;
|
|
183
|
+
bool already = sp.Current.IsSelected;
|
|
184
|
+
if (!already) sp.Select();
|
|
185
|
+
result["changed"] = !already;
|
|
186
|
+
}
|
|
187
|
+
else if (action == "set_value")
|
|
188
|
+
{
|
|
189
|
+
string val = Args.Str(r, "value", "");
|
|
190
|
+
((ValuePattern)pattern).SetValue(val);
|
|
191
|
+
result["changed"] = true;
|
|
192
|
+
}
|
|
130
193
|
result["performed"] = true;
|
|
131
194
|
return result;
|
|
132
195
|
}
|
|
133
196
|
|
|
197
|
+
static ControlType ControlTypeByName(string role)
|
|
198
|
+
{
|
|
199
|
+
if (string.IsNullOrEmpty(role)) return null;
|
|
200
|
+
string k = role.Trim().ToLowerInvariant();
|
|
201
|
+
if (k == "button") return ControlType.Button;
|
|
202
|
+
if (k == "edit" || k == "textbox" || k == "input") return ControlType.Edit;
|
|
203
|
+
if (k == "hyperlink" || k == "link") return ControlType.Hyperlink;
|
|
204
|
+
if (k == "menuitem") return ControlType.MenuItem;
|
|
205
|
+
if (k == "listitem") return ControlType.ListItem;
|
|
206
|
+
if (k == "tabitem" || k == "tab") return ControlType.TabItem;
|
|
207
|
+
if (k == "checkbox") return ControlType.CheckBox;
|
|
208
|
+
if (k == "radiobutton" || k == "radio") return ControlType.RadioButton;
|
|
209
|
+
if (k == "combobox") return ControlType.ComboBox;
|
|
210
|
+
if (k == "treeitem") return ControlType.TreeItem;
|
|
211
|
+
if (k == "text") return ControlType.Text;
|
|
212
|
+
if (k == "document") return ControlType.Document;
|
|
213
|
+
if (k == "slider") return ControlType.Slider;
|
|
214
|
+
if (k == "dataitem") return ControlType.DataItem;
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Targeted UIA lookup in C#: pushes AutomationId / ControlType / Name
|
|
220
|
+
* predicates down into UI Automation so matching a control avoids
|
|
221
|
+
* serializing hundreds of unrelated nodes across stdio.
|
|
222
|
+
*/
|
|
223
|
+
public static object Find(Dictionary<string, object> r)
|
|
224
|
+
{
|
|
225
|
+
IntPtr hwnd = Args.Has(r, "hwnd") ? new IntPtr(Convert.ToInt64(r["hwnd"])) : Native.GetForegroundWindow();
|
|
226
|
+
string automationId = Args.Str(r, "automationId", "").Trim();
|
|
227
|
+
string role = Args.Str(r, "role", "").Trim();
|
|
228
|
+
string exactName = Args.Str(r, "name", "").Trim();
|
|
229
|
+
string query = Args.Str(r, "query", "").Trim();
|
|
230
|
+
int maxMatches = Math.Max(1, Math.Min(120, Args.Int(r, "maxMatches", 24)));
|
|
231
|
+
List<object> result = null;
|
|
232
|
+
Exception error = null;
|
|
233
|
+
|
|
234
|
+
var worker = new Thread(delegate ()
|
|
235
|
+
{
|
|
236
|
+
try
|
|
237
|
+
{
|
|
238
|
+
WakeAccessibility(hwnd);
|
|
239
|
+
var conds = new List<Condition>();
|
|
240
|
+
conds.Add(new PropertyCondition(AutomationElement.IsOffscreenProperty, false));
|
|
241
|
+
var ct = ControlTypeByName(role);
|
|
242
|
+
if (ct != null) conds.Add(new PropertyCondition(AutomationElement.ControlTypeProperty, ct));
|
|
243
|
+
if (automationId.Length > 0) conds.Add(new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
|
|
244
|
+
if (exactName.Length > 0) conds.Add(new PropertyCondition(AutomationElement.NameProperty, exactName));
|
|
245
|
+
Condition cond = conds.Count == 1 ? conds[0] : new AndCondition(conds.ToArray());
|
|
246
|
+
|
|
247
|
+
var cache = new CacheRequest();
|
|
248
|
+
cache.Add(AutomationElement.NameProperty);
|
|
249
|
+
cache.Add(AutomationElement.ControlTypeProperty);
|
|
250
|
+
cache.Add(AutomationElement.ClassNameProperty);
|
|
251
|
+
cache.Add(AutomationElement.AutomationIdProperty);
|
|
252
|
+
cache.Add(AutomationElement.RuntimeIdProperty);
|
|
253
|
+
cache.Add(AutomationElement.BoundingRectangleProperty);
|
|
254
|
+
cache.Add(AutomationElement.IsEnabledProperty);
|
|
255
|
+
cache.Add(AutomationElement.IsKeyboardFocusableProperty);
|
|
256
|
+
cache.Add(AutomationElement.IsPasswordProperty);
|
|
257
|
+
cache.Add(AutomationElement.NativeWindowHandleProperty);
|
|
258
|
+
cache.Add(ValuePattern.ValueProperty);
|
|
259
|
+
cache.Add(TogglePattern.ToggleStateProperty);
|
|
260
|
+
cache.AutomationElementMode = AutomationElementMode.None;
|
|
261
|
+
cache.TreeScope = TreeScope.Element;
|
|
262
|
+
|
|
263
|
+
var root = AutomationElement.FromHandle(hwnd);
|
|
264
|
+
AutomationElementCollection found;
|
|
265
|
+
using (cache.Activate()) { found = root.FindAll(TreeScope.Descendants, cond); }
|
|
266
|
+
|
|
267
|
+
var list = new List<object>();
|
|
268
|
+
string qLower = query.ToLowerInvariant();
|
|
269
|
+
foreach (AutomationElement el in found)
|
|
270
|
+
{
|
|
271
|
+
if (list.Count >= maxMatches) break;
|
|
272
|
+
var rect = (System.Windows.Rect)el.GetCachedPropertyValue(AutomationElement.BoundingRectangleProperty);
|
|
273
|
+
if (rect.IsEmpty || rect.Width < 2 || rect.Height < 2) continue;
|
|
274
|
+
var type = (ControlType)el.GetCachedPropertyValue(AutomationElement.ControlTypeProperty);
|
|
275
|
+
string elName = (el.GetCachedPropertyValue(AutomationElement.NameProperty) as string) ?? "";
|
|
276
|
+
string elAid = (el.GetCachedPropertyValue(AutomationElement.AutomationIdProperty) as string) ?? "";
|
|
277
|
+
if (qLower.Length > 0
|
|
278
|
+
&& elName.ToLowerInvariant().IndexOf(qLower, StringComparison.Ordinal) < 0
|
|
279
|
+
&& elAid.ToLowerInvariant().IndexOf(qLower, StringComparison.Ordinal) < 0)
|
|
280
|
+
continue;
|
|
281
|
+
|
|
282
|
+
bool isPwd = false;
|
|
283
|
+
try
|
|
284
|
+
{
|
|
285
|
+
object pwdProp = el.GetCachedPropertyValue(AutomationElement.IsPasswordProperty, true);
|
|
286
|
+
if (pwdProp is bool && (bool)pwdProp) isPwd = true;
|
|
287
|
+
if (!isPwd)
|
|
288
|
+
{
|
|
289
|
+
object nhProp = el.GetCachedPropertyValue(AutomationElement.NativeWindowHandleProperty, true);
|
|
290
|
+
if (nhProp is int && (int)nhProp != 0 && IsWin32PasswordHwnd(new IntPtr((int)nhProp))) isPwd = true;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
catch (Exception) { }
|
|
294
|
+
|
|
295
|
+
var d = new Dictionary<string, object>();
|
|
296
|
+
d["role"] = type.ProgrammaticName.Replace("ControlType.", "");
|
|
297
|
+
if (isPwd) d["password"] = true;
|
|
298
|
+
d["name"] = elName.Length > 120 ? elName.Substring(0, 120) + "…" : elName;
|
|
299
|
+
if (elAid.Length > 0) d["automationId"] = elAid;
|
|
300
|
+
var runtimeId = el.GetCachedPropertyValue(AutomationElement.RuntimeIdProperty, true) as int[];
|
|
301
|
+
if (runtimeId != null && runtimeId.Length > 0) d["runtimeId"] = String.Join(",", runtimeId);
|
|
302
|
+
d["focusable"] = (bool)el.GetCachedPropertyValue(AutomationElement.IsKeyboardFocusableProperty);
|
|
303
|
+
object value = el.GetCachedPropertyValue(ValuePattern.ValueProperty, true);
|
|
304
|
+
if (isPwd) d["value"] = "[password]";
|
|
305
|
+
else if (value is string && ((string)value).Length > 0) { var v = (string)value; d["value"] = v.Length > 120 ? v.Substring(0, 120) + "…" : v; }
|
|
306
|
+
object toggle = el.GetCachedPropertyValue(TogglePattern.ToggleStateProperty, true);
|
|
307
|
+
if (toggle is ToggleState) d["checked"] = (ToggleState)toggle == ToggleState.On;
|
|
308
|
+
if (!(bool)el.GetCachedPropertyValue(AutomationElement.IsEnabledProperty)) d["disabled"] = true;
|
|
309
|
+
d["x"] = (int)rect.X; d["y"] = (int)rect.Y; d["width"] = (int)rect.Width; d["height"] = (int)rect.Height;
|
|
310
|
+
d["windowHwnd"] = hwnd.ToInt64();
|
|
311
|
+
list.Add(d);
|
|
312
|
+
}
|
|
313
|
+
result = list;
|
|
314
|
+
}
|
|
315
|
+
catch (Exception ex) { error = ex; }
|
|
316
|
+
});
|
|
317
|
+
worker.IsBackground = true;
|
|
318
|
+
worker.SetApartmentState(ApartmentState.MTA);
|
|
319
|
+
worker.Start();
|
|
320
|
+
if (!worker.Join(Args.Int(r, "timeoutMs", 4000))) throw new Exception("UI Automation targeted lookup timed out");
|
|
321
|
+
if (error != null) throw error;
|
|
322
|
+
var outMap = Windows.Describe(hwnd);
|
|
323
|
+
outMap["elements"] = result ?? new List<object>();
|
|
324
|
+
return outMap;
|
|
325
|
+
}
|
|
326
|
+
|
|
134
327
|
static bool BrowserChrome(AutomationElement element)
|
|
135
328
|
{
|
|
136
329
|
// Page content can supply arbitrary UIA names and IDs.
|