@copylee/dsh-computer-use 0.11.0 → 0.13.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 +31 -9
- package/helper/Capture.cs +682 -0
- package/helper/Input.cs +500 -0
- package/helper/Native.cs +136 -0
- package/helper/Overlay.cs +1685 -0
- package/helper/Program.cs +244 -0
- package/helper/Uia.cs +882 -0
- package/helper/Windows.cs +761 -0
- package/lib/client.js +191 -114
- package/lib/index.js +940 -368
- package/lib/types/access.d.ts +7 -0
- package/lib/types/cdp.d.ts +14 -3
- package/lib/types/client/select.d.ts +1 -1
- package/lib/types/computer.d.ts +25 -5
- package/lib/types/diagnostics.d.ts +3 -0
- package/lib/types/helper-client.d.ts +5 -2
- package/lib/types/index.d.ts +4 -1
- package/lib/types/lease.d.ts +30 -0
- package/lib/types/overlay.d.ts +26 -2
- package/lib/types/ui-diff.d.ts +28 -0
- package/package.json +3 -2
- package/helper/CuHelper.cs +0 -4414
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// JSON-RPC stdio entry point, argument helpers, and command dispatch (C# 5).
|
|
2
|
+
|
|
3
|
+
using System;
|
|
4
|
+
using System.Collections;
|
|
5
|
+
using System.Collections.Generic;
|
|
6
|
+
using System.Diagnostics;
|
|
7
|
+
using System.Drawing;
|
|
8
|
+
using System.Drawing.Drawing2D;
|
|
9
|
+
using System.Drawing.Imaging;
|
|
10
|
+
using System.Drawing.Text;
|
|
11
|
+
using System.IO;
|
|
12
|
+
using System.Reflection;
|
|
13
|
+
using System.Runtime.InteropServices;
|
|
14
|
+
using System.Text;
|
|
15
|
+
using System.Threading;
|
|
16
|
+
using System.Web.Script.Serialization;
|
|
17
|
+
using System.Windows.Automation;
|
|
18
|
+
using System.Windows.Forms;
|
|
19
|
+
using Microsoft.Win32;
|
|
20
|
+
|
|
21
|
+
namespace DshComputerUse
|
|
22
|
+
{
|
|
23
|
+
internal static class Args
|
|
24
|
+
{
|
|
25
|
+
public static bool Has(Dictionary<string, object> d, string k) { object v; return d.TryGetValue(k, out v) && v != null; }
|
|
26
|
+
public static int Int(Dictionary<string, object> d, string k, int def)
|
|
27
|
+
{
|
|
28
|
+
object v; if (!d.TryGetValue(k, out v) || v == null) return def;
|
|
29
|
+
return (int)Math.Round(Convert.ToDouble(v));
|
|
30
|
+
}
|
|
31
|
+
public static double Double(Dictionary<string, object> d, string k, double def)
|
|
32
|
+
{
|
|
33
|
+
object v;
|
|
34
|
+
return d.TryGetValue(k, out v) && v != null ? Convert.ToDouble(v, System.Globalization.CultureInfo.InvariantCulture) : def;
|
|
35
|
+
}
|
|
36
|
+
public static string Str(Dictionary<string, object> d, string k, string def)
|
|
37
|
+
{
|
|
38
|
+
object v; if (!d.TryGetValue(k, out v) || v == null) return def;
|
|
39
|
+
return Convert.ToString(v);
|
|
40
|
+
}
|
|
41
|
+
public static bool Bool(Dictionary<string, object> d, string k, bool def)
|
|
42
|
+
{
|
|
43
|
+
object v; if (!d.TryGetValue(k, out v) || v == null) return def;
|
|
44
|
+
return Convert.ToBoolean(v);
|
|
45
|
+
}
|
|
46
|
+
public static List<string> Strs(Dictionary<string, object> d, string k)
|
|
47
|
+
{
|
|
48
|
+
var list = new List<string>(); object v;
|
|
49
|
+
if (d.TryGetValue(k, out v) && v is IEnumerable && !(v is string))
|
|
50
|
+
foreach (var item in (IEnumerable)v) list.Add(Convert.ToString(item));
|
|
51
|
+
return list;
|
|
52
|
+
}
|
|
53
|
+
public static List<int> Ints(Dictionary<string, object> d, string k)
|
|
54
|
+
{
|
|
55
|
+
var list = new List<int>(); object v;
|
|
56
|
+
if (d.TryGetValue(k, out v) && v is IEnumerable && !(v is string))
|
|
57
|
+
foreach (var item in (IEnumerable)v) list.Add((int)Math.Round(Convert.ToDouble(item)));
|
|
58
|
+
return list;
|
|
59
|
+
}
|
|
60
|
+
public static List<List<int>> IntLists(Dictionary<string, object> d, string k)
|
|
61
|
+
{
|
|
62
|
+
var list = new List<List<int>>(); object v;
|
|
63
|
+
if (d.TryGetValue(k, out v) && v is IEnumerable && !(v is string))
|
|
64
|
+
foreach (var row in (IEnumerable)v)
|
|
65
|
+
{
|
|
66
|
+
var inner = new List<int>();
|
|
67
|
+
foreach (var item in (IEnumerable)row) inner.Add((int)Math.Round(Convert.ToDouble(item)));
|
|
68
|
+
list.Add(inner);
|
|
69
|
+
}
|
|
70
|
+
return list;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
internal static class Program
|
|
75
|
+
{
|
|
76
|
+
static readonly object OutLock = new object();
|
|
77
|
+
static TextWriter Out;
|
|
78
|
+
public const string Version = "1";
|
|
79
|
+
|
|
80
|
+
[STAThread]
|
|
81
|
+
static int Main(string[] argv)
|
|
82
|
+
{
|
|
83
|
+
// Must precede every GDI / WinForms call, or the process is stuck in
|
|
84
|
+
// DPI-virtualised coordinates.
|
|
85
|
+
try { if (!Native.SetProcessDpiAwarenessContext(new IntPtr(-4))) Native.SetProcessDPIAware(); }
|
|
86
|
+
catch (Exception) { try { Native.SetProcessDPIAware(); } catch (Exception) { } }
|
|
87
|
+
|
|
88
|
+
var utf8 = new UTF8Encoding(false);
|
|
89
|
+
var writer = new StreamWriter(Console.OpenStandardOutput(), utf8);
|
|
90
|
+
writer.AutoFlush = true;
|
|
91
|
+
Out = writer;
|
|
92
|
+
var input = new StreamReader(Console.OpenStandardInput(), utf8);
|
|
93
|
+
var parser = new JavaScriptSerializer();
|
|
94
|
+
parser.MaxJsonLength = int.MaxValue;
|
|
95
|
+
Apps.Warm();
|
|
96
|
+
Windows.RecoverCards(false);
|
|
97
|
+
|
|
98
|
+
var hello = new Dictionary<string, object>();
|
|
99
|
+
hello["event"] = "ready"; hello["version"] = Version; hello["pid"] = Process.GetCurrentProcess().Id;
|
|
100
|
+
Emit(hello);
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
string line;
|
|
104
|
+
while ((line = input.ReadLine()) != null)
|
|
105
|
+
{
|
|
106
|
+
if (line.Trim().Length == 0) continue;
|
|
107
|
+
object id = null;
|
|
108
|
+
var reply = new Dictionary<string, object>();
|
|
109
|
+
try
|
|
110
|
+
{
|
|
111
|
+
var req = (Dictionary<string, object>)parser.DeserializeObject(line);
|
|
112
|
+
if (req.ContainsKey("id")) id = req["id"];
|
|
113
|
+
reply["id"] = id;
|
|
114
|
+
reply["result"] = Commands.Run(req);
|
|
115
|
+
reply["ok"] = true;
|
|
116
|
+
}
|
|
117
|
+
catch (Exception ex)
|
|
118
|
+
{
|
|
119
|
+
reply["id"] = id;
|
|
120
|
+
reply["ok"] = false;
|
|
121
|
+
reply["error"] = ex.Message;
|
|
122
|
+
}
|
|
123
|
+
Emit(reply);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
finally { Windows.RecoverCards(true); Overlay.Shutdown(); }
|
|
127
|
+
return 0;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
public static void Emit(Dictionary<string, object> msg)
|
|
131
|
+
{
|
|
132
|
+
lock (OutLock)
|
|
133
|
+
{
|
|
134
|
+
var s = new JavaScriptSerializer();
|
|
135
|
+
s.MaxJsonLength = int.MaxValue;
|
|
136
|
+
Out.Write(s.Serialize(msg));
|
|
137
|
+
Out.Write('\n');
|
|
138
|
+
Out.Flush();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
public static void EmitEvent(string name, string reason)
|
|
143
|
+
{
|
|
144
|
+
ThreadPool.QueueUserWorkItem(delegate
|
|
145
|
+
{
|
|
146
|
+
var e = new Dictionary<string, object>();
|
|
147
|
+
e["event"] = name; e["reason"] = reason;
|
|
148
|
+
try { Emit(e); } catch (Exception) { }
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
public static void EmitDataEvent(string name, Dictionary<string, object> data)
|
|
153
|
+
{
|
|
154
|
+
ThreadPool.QueueUserWorkItem(delegate
|
|
155
|
+
{
|
|
156
|
+
var e = new Dictionary<string, object>();
|
|
157
|
+
e["event"] = name;
|
|
158
|
+
e["data"] = data;
|
|
159
|
+
try { Emit(e); } catch (Exception) { }
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
internal static class Commands
|
|
165
|
+
{
|
|
166
|
+
public static object Run(Dictionary<string, object> r)
|
|
167
|
+
{
|
|
168
|
+
string cmd = Args.Str(r, "cmd", "");
|
|
169
|
+
switch (cmd)
|
|
170
|
+
{
|
|
171
|
+
case "ping": { var d = new Dictionary<string, object>(); d["version"] = Program.Version; return d; }
|
|
172
|
+
case "displays": return Screen2.Displays();
|
|
173
|
+
case "screenshot": return Screen2.Capture(r);
|
|
174
|
+
case "settle": return Screen2.Settle(r);
|
|
175
|
+
case "cursor": { Native.POINT p; Native.GetCursorPos(out p); return Pt(p.X, p.Y); }
|
|
176
|
+
case "move":
|
|
177
|
+
using (new PassThrough())
|
|
178
|
+
{
|
|
179
|
+
int mx = Args.Int(r, "x", 0), my = Args.Int(r, "y", 0);
|
|
180
|
+
Overlay.Dodge(mx, my);
|
|
181
|
+
Input.Move(mx, my);
|
|
182
|
+
if (Overlay.ShowClickTarget) Overlay.MarkPointer(mx, my, "DSH", 900);
|
|
183
|
+
}
|
|
184
|
+
return null;
|
|
185
|
+
case "click":
|
|
186
|
+
Native.POINT clickPoint;
|
|
187
|
+
if (Args.Has(r, "x")) clickPoint = new Native.POINT(Args.Int(r, "x", 0), Args.Int(r, "y", 0));
|
|
188
|
+
else if (!Native.GetCursorPos(out clickPoint)) throw new Exception("Cannot read cursor position");
|
|
189
|
+
using (CardLayer.UncoverClick(clickPoint))
|
|
190
|
+
using (new PassThrough()) return Input.Click(r);
|
|
191
|
+
case "button": using (new PassThrough()) return Input.Button(r);
|
|
192
|
+
case "drag": using (new PassThrough()) return Input.Drag(r);
|
|
193
|
+
case "scroll": using (new PassThrough()) return Input.Scroll(r);
|
|
194
|
+
case "keys": return Input.Keys(r);
|
|
195
|
+
case "key_state": return Input.KeyState(r);
|
|
196
|
+
case "type": return Input.Type(Args.Str(r, "text", ""), Args.Int(r, "chunkDelay", 8), Args.Int(r, "charDelay", 0));
|
|
197
|
+
case "clipboard_hold": Clip.Hold(); return null;
|
|
198
|
+
case "clipboard_release": Clip.Release(); return null;
|
|
199
|
+
case "paste": return Input.Paste(Args.Str(r, "text", ""), Args.Bool(r, "restore", true), Args.Int(r, "waitMs", 250));
|
|
200
|
+
case "windows": return Windows.List();
|
|
201
|
+
case "window_recover": Windows.RecoverCards(false); return null;
|
|
202
|
+
case "background": return Windows.Background(Args.Bool(r, "tray", true));
|
|
203
|
+
case "tray_restore": return Tray.Restore(Args.Str(r, "exe", ""));
|
|
204
|
+
case "card_opacity": CardLayer.SetOpacity(Args.Int(r, "opacity", 100)); return null;
|
|
205
|
+
case "foreground": return Windows.Describe(Native.GetForegroundWindow());
|
|
206
|
+
case "window_at": return Windows.At(Args.Int(r, "x", 0), Args.Int(r, "y", 0));
|
|
207
|
+
case "focus":
|
|
208
|
+
var focusHwnd = new IntPtr(Convert.ToInt64(r["hwnd"]));
|
|
209
|
+
uint focusPid;
|
|
210
|
+
if (Args.Has(r, "pid") && (Native.GetWindowThreadProcessId(focusHwnd, out focusPid) == 0 || focusPid != Convert.ToUInt32(r["pid"])))
|
|
211
|
+
throw new Exception("window no longer belongs to the original process");
|
|
212
|
+
return Windows.Focus(focusHwnd);
|
|
213
|
+
case "window_cmd": return Windows.Command(new IntPtr(Convert.ToInt64(r["hwnd"])), Args.Str(r, "op", ""));
|
|
214
|
+
case "apps": return Apps.Search(Args.Str(r, "query", ""), Args.Int(r, "limit", 8));
|
|
215
|
+
case "launch": return Apps.Launch(r);
|
|
216
|
+
case "open_url": return Apps.OpenUrl(Args.Str(r, "url", ""));
|
|
217
|
+
case "browser_tab": return Uia.BrowserTab(r);
|
|
218
|
+
case "browser_open_tab": return Uia.OpenBrowserTab(r);
|
|
219
|
+
case "act": return Uia.Act(r);
|
|
220
|
+
case "clipboard_get": { var d = new Dictionary<string, object>(); d["text"] = Clip.Get(); return d; }
|
|
221
|
+
case "clipboard_set": if (Args.Bool(r, "agent", false)) Clip.AgentSet(Args.Str(r, "text", "")); else Clip.Set(Args.Str(r, "text", ""), false); return null;
|
|
222
|
+
case "clipboard_restore": { var d = new Dictionary<string, object>(); d["restored"] = Clip.RestoreAgent(); return d; }
|
|
223
|
+
case "ui": return Uia.Elements(r);
|
|
224
|
+
case "text": return Uia.Text(r);
|
|
225
|
+
case "ocr": return Ocr.Read(r);
|
|
226
|
+
case "marks": return Uia.Marks(r);
|
|
227
|
+
case "password_focus": return Uia.PasswordFocus();
|
|
228
|
+
case "ui_point_click": return Uia.ClickAtPoint(r);
|
|
229
|
+
case "overlay_show": Overlay.ExcludeFromCapture = Args.Bool(r, "excludeFromCapture", true); Overlay.Show(r); return null;
|
|
230
|
+
case "overlay_pause": Overlay.SetPaused(Args.Bool(r, "paused", false)); return null;
|
|
231
|
+
case "window_card": CardLayer.SetOpacity(Args.Int(r, "opacity", 100)); return Windows.Card(new IntPtr(Convert.ToInt64(r["hwnd"])), Args.Int(r, "x", 0), Args.Int(r, "y", 0), Args.Int(r, "width", 0), Args.Int(r, "height", 0));
|
|
232
|
+
case "window_uncard": return Windows.Uncard(new IntPtr(Convert.ToInt64(r["hwnd"])));
|
|
233
|
+
case "overlay_status": Overlay.Status(Args.Str(r, "status", "")); return null;
|
|
234
|
+
case "pet_update": Overlay.PetUpdate(Args.Str(r, "thinking", ""), Args.Strs(r, "steps")); return null;
|
|
235
|
+
case "pet_finish": Overlay.Finish(Args.Int(r, "holdMs", 4000)); return null;
|
|
236
|
+
case "overlay_hide": Overlay.Hide(); return null;
|
|
237
|
+
default: throw new Exception("unknown command: " + cmd);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
public static Dictionary<string, object> Pt(int x, int y) { var d = new Dictionary<string, object>(); d["x"] = x; d["y"] = y; return d; }
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
}
|