@copylee/dsh-computer-use 0.8.1 → 0.8.3
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 +45 -136
- package/helper/CuHelper.cs +283 -32
- package/lib/client.js +10 -1
- package/lib/index.js +458 -106
- package/lib/types/browser-host.d.ts +19 -0
- package/lib/types/computer.d.ts +12 -2
- package/lib/types/coords.d.ts +1 -1
- package/lib/types/index.d.ts +1 -0
- package/lib/types/overlay.d.ts +36 -6
- package/lib/types/target.d.ts +7 -1
- package/package.json +1 -1
package/helper/CuHelper.cs
CHANGED
|
@@ -288,10 +288,19 @@ namespace DshComputerUse
|
|
|
288
288
|
case "card_opacity": CardLayer.SetOpacity(Args.Int(r, "opacity", 100)); return null;
|
|
289
289
|
case "foreground": return Windows.Describe(Native.GetForegroundWindow());
|
|
290
290
|
case "window_at": return Windows.At(Args.Int(r, "x", 0), Args.Int(r, "y", 0));
|
|
291
|
-
case "focus":
|
|
291
|
+
case "focus":
|
|
292
|
+
var focusHwnd = new IntPtr(Convert.ToInt64(r["hwnd"]));
|
|
293
|
+
uint focusPid;
|
|
294
|
+
if (Args.Has(r, "pid") && (Native.GetWindowThreadProcessId(focusHwnd, out focusPid) == 0 || focusPid != Convert.ToUInt32(r["pid"])))
|
|
295
|
+
throw new Exception("window no longer belongs to the original process");
|
|
296
|
+
return Windows.Focus(focusHwnd);
|
|
292
297
|
case "window_cmd": return Windows.Command(new IntPtr(Convert.ToInt64(r["hwnd"])), Args.Str(r, "op", ""));
|
|
293
298
|
case "apps": return Apps.Search(Args.Str(r, "query", ""), Args.Int(r, "limit", 8));
|
|
294
299
|
case "launch": return Apps.Launch(r);
|
|
300
|
+
case "open_url": return Apps.OpenUrl(Args.Str(r, "url", ""));
|
|
301
|
+
case "browser_tab": return Uia.BrowserTab(r);
|
|
302
|
+
case "browser_open_tab": return Uia.OpenBrowserTab(r);
|
|
303
|
+
case "act": return Uia.Act(r);
|
|
295
304
|
case "clipboard_get": { var d = new Dictionary<string, object>(); d["text"] = Clip.Get(); return d; }
|
|
296
305
|
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;
|
|
297
306
|
case "clipboard_restore": { var d = new Dictionary<string, object>(); d["restored"] = Clip.RestoreAgent(); return d; }
|
|
@@ -302,8 +311,8 @@ namespace DshComputerUse
|
|
|
302
311
|
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));
|
|
303
312
|
case "window_uncard": return Windows.Uncard(new IntPtr(Convert.ToInt64(r["hwnd"])));
|
|
304
313
|
case "overlay_status": Overlay.Status(Args.Str(r, "status", "")); return null;
|
|
305
|
-
case "pet_update": Overlay.PetUpdate(Args.Str(r, "
|
|
306
|
-
case "pet_finish": Overlay.Finish(Args.
|
|
314
|
+
case "pet_update": Overlay.PetUpdate(Args.Str(r, "thinking", ""), Args.Strs(r, "steps")); return null;
|
|
315
|
+
case "pet_finish": Overlay.Finish(Args.Int(r, "holdMs", 4000)); return null;
|
|
307
316
|
case "overlay_hide": Overlay.Hide(); return null;
|
|
308
317
|
default: throw new Exception("unknown command: " + cmd);
|
|
309
318
|
}
|
|
@@ -1600,6 +1609,32 @@ namespace DshComputerUse
|
|
|
1600
1609
|
|
|
1601
1610
|
internal static class Apps
|
|
1602
1611
|
{
|
|
1612
|
+
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
|
|
1613
|
+
static extern int AssocQueryString(uint flags, uint str, string association, string verb, StringBuilder output, ref uint length);
|
|
1614
|
+
|
|
1615
|
+
public static object OpenUrl(string value)
|
|
1616
|
+
{
|
|
1617
|
+
Uri url;
|
|
1618
|
+
if (!Uri.TryCreate(value, UriKind.Absolute, out url) || (url.Scheme != "http" && url.Scheme != "https"))
|
|
1619
|
+
throw new Exception("Expected an HTTP(S) URL");
|
|
1620
|
+
const uint executable = 2;
|
|
1621
|
+
var path = new StringBuilder(32768);
|
|
1622
|
+
uint length = (uint)path.Capacity;
|
|
1623
|
+
if (AssocQueryString(0, executable, url.Scheme, "open", path, ref length) != 0)
|
|
1624
|
+
throw new Exception("Could not resolve the default browser for " + url.Scheme);
|
|
1625
|
+
string exe = Path.GetFileName(path.ToString()).ToLowerInvariant();
|
|
1626
|
+
string flag;
|
|
1627
|
+
switch (exe)
|
|
1628
|
+
{
|
|
1629
|
+
case "chrome.exe": case "msedge.exe": case "brave.exe": case "vivaldi.exe": case "chromium.exe":
|
|
1630
|
+
flag = "--new-window"; break;
|
|
1631
|
+
case "firefox.exe": flag = "-new-window"; break;
|
|
1632
|
+
default: throw new Exception("Cannot safely open a new window in the default browser (" + exe + ")");
|
|
1633
|
+
}
|
|
1634
|
+
string escapedUrl = url.AbsoluteUri.Replace("\"", "%22").Replace("\\", "%5C");
|
|
1635
|
+
using (var process = Process.Start(new ProcessStartInfo(path.ToString(), flag + " \"" + escapedUrl + "\"") { UseShellExecute = false })) { }
|
|
1636
|
+
return new Dictionary<string, object> { { "exe", exe } };
|
|
1637
|
+
}
|
|
1603
1638
|
static List<KeyValuePair<string, string>> cache;
|
|
1604
1639
|
static DateTime cachedAt;
|
|
1605
1640
|
static readonly object Gate = new object();
|
|
@@ -1741,6 +1776,223 @@ namespace DshComputerUse
|
|
|
1741
1776
|
// than creating an unbounded number of stuck threads on repeated reads.
|
|
1742
1777
|
static readonly SemaphoreSlim WindowCollectors = new SemaphoreSlim(4, 4);
|
|
1743
1778
|
|
|
1779
|
+
static bool SameWindow(IntPtr hwnd, uint expectedPid)
|
|
1780
|
+
{
|
|
1781
|
+
uint pid;
|
|
1782
|
+
return expectedPid != 0 && Native.GetWindowThreadProcessId(hwnd, out pid) != 0 && pid == expectedPid;
|
|
1783
|
+
}
|
|
1784
|
+
|
|
1785
|
+
static bool IsActionWindow(IntPtr hwnd, uint pid, Native.POINT point)
|
|
1786
|
+
{
|
|
1787
|
+
if (!SameWindow(hwnd, pid) || Native.GetForegroundWindow() != hwnd) return false;
|
|
1788
|
+
var hit = Windows.At(point.X, point.Y);
|
|
1789
|
+
return hit.ContainsKey("hwnd") && Convert.ToInt64(hit["hwnd"]) == hwnd.ToInt64();
|
|
1790
|
+
}
|
|
1791
|
+
|
|
1792
|
+
public static object Act(Dictionary<string, object> r)
|
|
1793
|
+
{
|
|
1794
|
+
var hwnd = new IntPtr(Convert.ToInt64(r["hwnd"]));
|
|
1795
|
+
uint pid = Convert.ToUInt32(r["pid"]);
|
|
1796
|
+
var point = new Native.POINT(Args.Int(r, "x", 0), Args.Int(r, "y", 0));
|
|
1797
|
+
string runtimeId = Args.Str(r, "runtimeId", "");
|
|
1798
|
+
string action = Args.Str(r, "action", "");
|
|
1799
|
+
var result = new Dictionary<string, object> { { "performed", false } };
|
|
1800
|
+
if (runtimeId.Length == 0 || !IsActionWindow(hwnd, pid, point))
|
|
1801
|
+
throw new Exception("The named target window changed");
|
|
1802
|
+
|
|
1803
|
+
// FromPoint is scoped to the visible hit. Ancestors are searched only
|
|
1804
|
+
// for the exact listed element; no action is attempted on a substitute.
|
|
1805
|
+
var hit = AutomationElement.FromPoint(new System.Windows.Point(point.X, point.Y));
|
|
1806
|
+
AutomationElement target = null;
|
|
1807
|
+
for (var el = hit; el != null; el = TreeWalker.ControlViewWalker.GetParent(el))
|
|
1808
|
+
{
|
|
1809
|
+
if (String.Join(",", el.GetRuntimeId()) == runtimeId) { target = el; break; }
|
|
1810
|
+
if (el.Current.ControlType == ControlType.Window) break;
|
|
1811
|
+
}
|
|
1812
|
+
if (target == null || target.Current.IsOffscreen || !target.Current.IsEnabled)
|
|
1813
|
+
throw new Exception("The named target changed or is no longer actionable");
|
|
1814
|
+
object pattern = null;
|
|
1815
|
+
if (action == "invoke" && !target.TryGetCurrentPattern(InvokePattern.Pattern, out pattern)) return result;
|
|
1816
|
+
if (action == "focus" && !target.Current.IsKeyboardFocusable) return result;
|
|
1817
|
+
if (action != "invoke" && action != "focus") return result;
|
|
1818
|
+
if (!IsActionWindow(hwnd, pid, point))
|
|
1819
|
+
throw new Exception("The named target window changed");
|
|
1820
|
+
|
|
1821
|
+
// A provider error after dispatch is indeterminate. Let it surface
|
|
1822
|
+
// rather than claiming the host can safely click again.
|
|
1823
|
+
if (action == "invoke") ((InvokePattern)pattern).Invoke();
|
|
1824
|
+
else
|
|
1825
|
+
{
|
|
1826
|
+
target.SetFocus();
|
|
1827
|
+
for (int i = 0; i < 10 && !target.Current.HasKeyboardFocus; i++) Thread.Sleep(25);
|
|
1828
|
+
if (!target.Current.HasKeyboardFocus) throw new Exception("Could not verify focus on the named control");
|
|
1829
|
+
}
|
|
1830
|
+
result["performed"] = true;
|
|
1831
|
+
return result;
|
|
1832
|
+
}
|
|
1833
|
+
|
|
1834
|
+
static bool BrowserChrome(AutomationElement element)
|
|
1835
|
+
{
|
|
1836
|
+
// Page content can supply arbitrary UIA names and IDs.
|
|
1837
|
+
for (var parent = TreeWalker.ControlViewWalker.GetParent(element); parent != null; parent = TreeWalker.ControlViewWalker.GetParent(parent))
|
|
1838
|
+
{
|
|
1839
|
+
if (parent.Current.ControlType == ControlType.Document) return false;
|
|
1840
|
+
if (parent.Current.ControlType == ControlType.Window) return true;
|
|
1841
|
+
}
|
|
1842
|
+
return false;
|
|
1843
|
+
}
|
|
1844
|
+
|
|
1845
|
+
static bool BrowserContentFocused()
|
|
1846
|
+
{
|
|
1847
|
+
for (var el = AutomationElement.FocusedElement; el != null; el = TreeWalker.ControlViewWalker.GetParent(el))
|
|
1848
|
+
{
|
|
1849
|
+
if (el.Current.ControlType == ControlType.Document) return true;
|
|
1850
|
+
if (el.Current.ControlType == ControlType.Window) return false;
|
|
1851
|
+
}
|
|
1852
|
+
return false;
|
|
1853
|
+
}
|
|
1854
|
+
|
|
1855
|
+
public static object OpenBrowserTab(Dictionary<string, object> r)
|
|
1856
|
+
{
|
|
1857
|
+
var hwnd = new IntPtr(Convert.ToInt64(r["hwnd"]));
|
|
1858
|
+
uint pid = Convert.ToUInt32(r["pid"]);
|
|
1859
|
+
string expected = Args.Str(r, "tab", "");
|
|
1860
|
+
Uri url;
|
|
1861
|
+
if (!Uri.TryCreate(Args.Str(r, "url", ""), UriKind.Absolute, out url) || (url.Scheme != "http" && url.Scheme != "https"))
|
|
1862
|
+
throw new Exception("Expected an HTTP(S) URL");
|
|
1863
|
+
Func<bool> current = delegate { return SameWindow(hwnd, pid) && Native.GetForegroundWindow() == hwnd; };
|
|
1864
|
+
Func<Dictionary<string, object>> read = delegate { return BrowserTab(r) as Dictionary<string, object>; };
|
|
1865
|
+
var initial = new Dictionary<string, object>(r) { { "listTabs", true } };
|
|
1866
|
+
var before = current() ? BrowserTab(initial) as Dictionary<string, object> : null;
|
|
1867
|
+
object tabIds;
|
|
1868
|
+
if (expected.Length == 0 || before == null || Args.Str(before, "tab", "") != expected || !current())
|
|
1869
|
+
throw new Exception("The browser window or active tab changed before opening a new tab");
|
|
1870
|
+
if (!before.TryGetValue("tabs", out tabIds) || !(tabIds is List<string>))
|
|
1871
|
+
throw new Exception("Could not verify the browser's existing tabs");
|
|
1872
|
+
var existing = new HashSet<string>((List<string>)tabIds);
|
|
1873
|
+
if (!existing.Contains(expected)) throw new Exception("The active browser tab could not be verified");
|
|
1874
|
+
|
|
1875
|
+
Input.Keys(new Dictionary<string, object> { { "combos", new[] { new[] { 0x11, 0x54 } } } });
|
|
1876
|
+
var clock = Stopwatch.StartNew();
|
|
1877
|
+
string created = "";
|
|
1878
|
+
while (clock.ElapsedMilliseconds < 2400 && current())
|
|
1879
|
+
{
|
|
1880
|
+
var fresh = read();
|
|
1881
|
+
if (fresh != null && !existing.Contains(Args.Str(fresh, "tab", ""))
|
|
1882
|
+
&& Args.Str(fresh, "url", "unavailable") == "")
|
|
1883
|
+
{
|
|
1884
|
+
created = Args.Str(fresh, "tab", "");
|
|
1885
|
+
if (created.Length > 0) break;
|
|
1886
|
+
}
|
|
1887
|
+
Thread.Sleep(65);
|
|
1888
|
+
}
|
|
1889
|
+
if (created.Length == 0 || !current())
|
|
1890
|
+
throw new Exception("Could not confirm a new blank tab in the original browser window");
|
|
1891
|
+
Input.Keys(new Dictionary<string, object> { { "combos", new[] { new[] { 0x11, 0x4C } } } });
|
|
1892
|
+
clock.Restart();
|
|
1893
|
+
bool addressReady = false;
|
|
1894
|
+
while (clock.ElapsedMilliseconds < 1200 && current())
|
|
1895
|
+
{
|
|
1896
|
+
var blank = read();
|
|
1897
|
+
if (blank != null)
|
|
1898
|
+
{
|
|
1899
|
+
if (Args.Str(blank, "tab", "") != created || Args.Str(blank, "url", "unavailable") != "") break;
|
|
1900
|
+
if (Args.Bool(blank, "addressFocused", false)) { addressReady = true; break; }
|
|
1901
|
+
}
|
|
1902
|
+
Thread.Sleep(65);
|
|
1903
|
+
}
|
|
1904
|
+
if (!addressReady || !current()) throw new Exception("Could not verify focus in the new tab's address bar");
|
|
1905
|
+
Input.Paste(url.AbsoluteUri, true, 120);
|
|
1906
|
+
clock.Restart();
|
|
1907
|
+
while (clock.ElapsedMilliseconds < 1200 && current())
|
|
1908
|
+
{
|
|
1909
|
+
var address = read();
|
|
1910
|
+
if (address != null)
|
|
1911
|
+
{
|
|
1912
|
+
if (Args.Str(address, "tab", "") != created || !Args.Bool(address, "addressFocused", false)) break;
|
|
1913
|
+
if (Args.Str(address, "url", "") == url.AbsoluteUri && current())
|
|
1914
|
+
{
|
|
1915
|
+
Input.Keys(new Dictionary<string, object> { { "combos", new[] { new[] { 0x0D } } } });
|
|
1916
|
+
return new Dictionary<string, object> { { "tab", created } };
|
|
1917
|
+
}
|
|
1918
|
+
}
|
|
1919
|
+
Thread.Sleep(65);
|
|
1920
|
+
}
|
|
1921
|
+
throw new Exception("Could not verify the new tab's address before navigation");
|
|
1922
|
+
}
|
|
1923
|
+
|
|
1924
|
+
public static object BrowserTab(Dictionary<string, object> r)
|
|
1925
|
+
{
|
|
1926
|
+
IntPtr hwnd = new IntPtr(Convert.ToInt64(r["hwnd"]));
|
|
1927
|
+
uint pid = Convert.ToUInt32(r["pid"]);
|
|
1928
|
+
string select = Args.Str(r, "select", "");
|
|
1929
|
+
if (!SameWindow(hwnd, pid) || !WindowCollectors.Wait(0)) return null;
|
|
1930
|
+
var clock = Stopwatch.StartNew();
|
|
1931
|
+
Dictionary<string, object> result = null;
|
|
1932
|
+
var worker = new Thread(delegate ()
|
|
1933
|
+
{
|
|
1934
|
+
try
|
|
1935
|
+
{
|
|
1936
|
+
var root = AutomationElement.FromHandle(hwnd);
|
|
1937
|
+
var controls = root.FindAll(TreeScope.Descendants, new OrCondition(
|
|
1938
|
+
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit),
|
|
1939
|
+
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem)));
|
|
1940
|
+
var state = new Dictionary<string, object>();
|
|
1941
|
+
var tabs = Args.Bool(r, "listTabs", false) ? new List<string>() : null;
|
|
1942
|
+
foreach (AutomationElement el in controls)
|
|
1943
|
+
{
|
|
1944
|
+
if (el.Current.IsOffscreen && (tabs == null || el.Current.ControlType != ControlType.TabItem)) continue;
|
|
1945
|
+
object pattern;
|
|
1946
|
+
if (el.Current.ControlType == ControlType.Edit)
|
|
1947
|
+
{
|
|
1948
|
+
string id = el.Current.AutomationId;
|
|
1949
|
+
string name = el.Current.Name;
|
|
1950
|
+
if (id != "view_1012" && id != "urlbar-input" && name != "Address and search bar" && name != "地址和搜索栏") continue;
|
|
1951
|
+
if (BrowserChrome(el) && el.TryGetCurrentPattern(ValuePattern.Pattern, out pattern))
|
|
1952
|
+
{
|
|
1953
|
+
state["url"] = ((ValuePattern)pattern).Current.Value;
|
|
1954
|
+
state["addressFocused"] = el.Current.HasKeyboardFocus;
|
|
1955
|
+
}
|
|
1956
|
+
}
|
|
1957
|
+
else if (el.TryGetCurrentPattern(SelectionItemPattern.Pattern, out pattern) && BrowserChrome(el))
|
|
1958
|
+
{
|
|
1959
|
+
var tab = (SelectionItemPattern)pattern;
|
|
1960
|
+
string id = String.Join(",", el.GetRuntimeId());
|
|
1961
|
+
if (tabs != null) tabs.Add(id);
|
|
1962
|
+
if (select.Length > 0 && id == select)
|
|
1963
|
+
{
|
|
1964
|
+
if (clock.ElapsedMilliseconds < 1200 && SameWindow(hwnd, pid) && Native.GetForegroundWindow() == hwnd)
|
|
1965
|
+
{
|
|
1966
|
+
if (!tab.Current.IsSelected) tab.Select();
|
|
1967
|
+
state["selected"] = true;
|
|
1968
|
+
}
|
|
1969
|
+
break;
|
|
1970
|
+
}
|
|
1971
|
+
if (tab.Current.IsSelected) state["tab"] = id;
|
|
1972
|
+
}
|
|
1973
|
+
}
|
|
1974
|
+
if (tabs != null) state["tabs"] = tabs;
|
|
1975
|
+
if (Args.Bool(r, "details", false) && Native.GetForegroundWindow() == hwnd)
|
|
1976
|
+
{
|
|
1977
|
+
var render = ChromiumRoot(hwnd);
|
|
1978
|
+
Native.RECT rect;
|
|
1979
|
+
if (render != hwnd && Native.GetWindowRect(render, out rect) && rect.Right > rect.Left && rect.Bottom > rect.Top)
|
|
1980
|
+
state["content"] = new Dictionary<string, object> {
|
|
1981
|
+
{ "x", rect.Left }, { "y", rect.Top }, { "width", rect.Right - rect.Left }, { "height", rect.Bottom - rect.Top },
|
|
1982
|
+
};
|
|
1983
|
+
state["contentFocused"] = BrowserContentFocused();
|
|
1984
|
+
}
|
|
1985
|
+
result = state;
|
|
1986
|
+
}
|
|
1987
|
+
catch (Exception) { }
|
|
1988
|
+
finally { WindowCollectors.Release(); }
|
|
1989
|
+
});
|
|
1990
|
+
worker.IsBackground = true;
|
|
1991
|
+
worker.SetApartmentState(ApartmentState.MTA);
|
|
1992
|
+
worker.Start();
|
|
1993
|
+
return worker.Join(1250) ? result : null;
|
|
1994
|
+
}
|
|
1995
|
+
|
|
1744
1996
|
static List<object> CollectWindow(IntPtr hwnd, int max, bool includeText)
|
|
1745
1997
|
{
|
|
1746
1998
|
IntPtr render = ChromiumRoot(hwnd);
|
|
@@ -1805,12 +2057,19 @@ namespace DshComputerUse
|
|
|
1805
2057
|
{
|
|
1806
2058
|
result = CollectWindow(hwnd, max, all);
|
|
1807
2059
|
if (result.Count < 4) { Thread.Sleep(400); result = CollectWindow(hwnd, max, all); } // Chromium builds its tree lazily
|
|
2060
|
+
foreach (Dictionary<string, object> item in result) item["windowHwnd"] = hwnd.ToInt64();
|
|
1808
2061
|
}
|
|
1809
2062
|
// An open menu or flyout is a window of its own, not a descendant of the one it belongs to.
|
|
1810
2063
|
if (popups || popupsOnly) foreach (var extra in Popups(hwnd))
|
|
1811
2064
|
{
|
|
1812
2065
|
if (result.Count >= max) break;
|
|
1813
|
-
try
|
|
2066
|
+
try
|
|
2067
|
+
{
|
|
2068
|
+
var popup = CollectWindow(extra, max - result.Count, all);
|
|
2069
|
+
foreach (Dictionary<string, object> item in popup) item["windowHwnd"] = extra.ToInt64();
|
|
2070
|
+
result.AddRange(popup);
|
|
2071
|
+
}
|
|
2072
|
+
catch (Exception) { }
|
|
1814
2073
|
}
|
|
1815
2074
|
}
|
|
1816
2075
|
catch (Exception ex) { error = ex; }
|
|
@@ -1949,6 +2208,7 @@ namespace DshComputerUse
|
|
|
1949
2208
|
cache.Add(AutomationElement.BoundingRectangleProperty);
|
|
1950
2209
|
cache.Add(AutomationElement.IsEnabledProperty);
|
|
1951
2210
|
cache.Add(AutomationElement.AutomationIdProperty);
|
|
2211
|
+
cache.Add(AutomationElement.RuntimeIdProperty);
|
|
1952
2212
|
cache.Add(AutomationElement.ClassNameProperty);
|
|
1953
2213
|
cache.Add(AutomationElement.IsKeyboardFocusableProperty);
|
|
1954
2214
|
cache.Add(ValuePattern.ValueProperty);
|
|
@@ -1974,7 +2234,9 @@ namespace DshComputerUse
|
|
|
1974
2234
|
if (type == ControlType.Group) d["editable"] = true;
|
|
1975
2235
|
d["name"] = name.Length > 120 ? name.Substring(0, 120) + "…" : name;
|
|
1976
2236
|
string aid = (el.GetCachedPropertyValue(AutomationElement.AutomationIdProperty) as string) ?? "";
|
|
1977
|
-
if (aid.Length > 0) d["automationId"] = aid
|
|
2237
|
+
if (aid.Length > 0) d["automationId"] = aid;
|
|
2238
|
+
var runtimeId = el.GetCachedPropertyValue(AutomationElement.RuntimeIdProperty, true) as int[];
|
|
2239
|
+
if (runtimeId != null && runtimeId.Length > 0) d["runtimeId"] = String.Join(",", runtimeId);
|
|
1978
2240
|
d["focusable"] = (bool)el.GetCachedPropertyValue(AutomationElement.IsKeyboardFocusableProperty);
|
|
1979
2241
|
object value = el.GetCachedPropertyValue(ValuePattern.ValueProperty, true);
|
|
1980
2242
|
if (value is string && ((string)value).Length > 0) { var v = (string)value; d["value"] = v.Length > 120 ? v.Substring(0, 120) + "…" : v; }
|
|
@@ -2148,9 +2410,8 @@ namespace DshComputerUse
|
|
|
2148
2410
|
static bool petMode;
|
|
2149
2411
|
static LayeredForm pet;
|
|
2150
2412
|
public static volatile IntPtr PetHwnd = IntPtr.Zero;
|
|
2151
|
-
static string
|
|
2413
|
+
static string petThinking = "";
|
|
2152
2414
|
static List<string> petSteps = new List<string>();
|
|
2153
|
-
static bool petThinkingLatest;
|
|
2154
2415
|
/** Bottom-right corner of the card body; the card grows upward when expanded. */
|
|
2155
2416
|
static Point petAnchor = Point.Empty;
|
|
2156
2417
|
static int petShadow;
|
|
@@ -2227,7 +2488,8 @@ namespace DshComputerUse
|
|
|
2227
2488
|
if (visible && !petFinished) { RenderPill(); return; }
|
|
2228
2489
|
if (visible) HideCore(); // the previous run's "done" card is still up
|
|
2229
2490
|
petMode = Args.Bool(r, "pet", false);
|
|
2230
|
-
|
|
2491
|
+
petThinking = Args.Str(r, "thinking", "");
|
|
2492
|
+
petSteps = new List<string>();
|
|
2231
2493
|
if (status.Length > 0) petSteps.Add(status);
|
|
2232
2494
|
pillAtBottom = false;
|
|
2233
2495
|
paused = false;
|
|
@@ -2295,23 +2557,21 @@ namespace DshComputerUse
|
|
|
2295
2557
|
Ui(delegate { status = newStatus; RenderPill(); });
|
|
2296
2558
|
}
|
|
2297
2559
|
|
|
2298
|
-
/**
|
|
2299
|
-
public static void PetUpdate(string
|
|
2560
|
+
/** Latest host-provided reasoning and recent operations, oldest first. */
|
|
2561
|
+
public static void PetUpdate(string thinking, List<string> steps)
|
|
2300
2562
|
{
|
|
2301
2563
|
if (thread == null || !visible || !petMode || petFinished) return;
|
|
2302
2564
|
invoker.BeginInvoke((MethodInvoker)delegate
|
|
2303
2565
|
{
|
|
2304
2566
|
if (petFinished) return;
|
|
2305
|
-
petText = text;
|
|
2306
2567
|
petThinking = thinking;
|
|
2307
2568
|
if (steps.Count > 0) petSteps = steps;
|
|
2308
|
-
petThinkingLatest = thinkingLatest;
|
|
2309
2569
|
RenderPill();
|
|
2310
2570
|
});
|
|
2311
2571
|
}
|
|
2312
2572
|
|
|
2313
2573
|
/** The run ended: the card says so for a moment (longer while the pointer rests on it), then everything hides. */
|
|
2314
|
-
public static void Finish(
|
|
2574
|
+
public static void Finish(int holdMs)
|
|
2315
2575
|
{
|
|
2316
2576
|
if (thread == null) return;
|
|
2317
2577
|
Ui(delegate
|
|
@@ -2322,7 +2582,6 @@ namespace DshComputerUse
|
|
|
2322
2582
|
glows.Clear();
|
|
2323
2583
|
RemoveHook();
|
|
2324
2584
|
paused = false;
|
|
2325
|
-
if (text.Length > 0) petText = text;
|
|
2326
2585
|
petFinished = true;
|
|
2327
2586
|
finishedAt = DateTime.UtcNow;
|
|
2328
2587
|
finishHoldMs = holdMs;
|
|
@@ -2625,6 +2884,7 @@ namespace DshComputerUse
|
|
|
2625
2884
|
petShadow = shadow;
|
|
2626
2885
|
float bodyW = 420 * s, pad = 16 * s;
|
|
2627
2886
|
float headH = 44 * s, replyH = 26 * s;
|
|
2887
|
+
string progress = petFinished ? "已交还电脑控制" : petThinking.Length > 0 ? "思考 · " + petThinking : "正在操作,等待下一步…";
|
|
2628
2888
|
if (petExpanded)
|
|
2629
2889
|
{
|
|
2630
2890
|
// Grow with the reply, up to about eight lines.
|
|
@@ -2632,14 +2892,12 @@ namespace DshComputerUse
|
|
|
2632
2892
|
using (var pg = Graphics.FromImage(probe))
|
|
2633
2893
|
using (var pf = new Font("Microsoft YaHei UI", 10.5f * 1.333f * scale, FontStyle.Regular, GraphicsUnit.Pixel))
|
|
2634
2894
|
{
|
|
2635
|
-
|
|
2636
|
-
float need = pg.MeasureString(t, pf, new SizeF(420 * scale - 32 * scale, 100000), new StringFormat(StringFormat.GenericTypographic)).Height + 6 * scale;
|
|
2895
|
+
float need = pg.MeasureString(progress, pf, new SizeF(420 * scale - 32 * scale, 100000), new StringFormat(StringFormat.GenericTypographic)).Height + 6 * scale;
|
|
2637
2896
|
replyH = Math.Max(26 * scale, Math.Min(172 * scale, need));
|
|
2638
2897
|
}
|
|
2639
2898
|
}
|
|
2640
2899
|
int stepRows = petExpanded ? Math.Min(5, Math.Max(1, petSteps.Count)) : 0;
|
|
2641
|
-
|
|
2642
|
-
float listH = petExpanded ? 8 * s + (thinkingRow ? 24 * s : 0) + stepRows * 22 * s : 24 * s;
|
|
2900
|
+
float listH = petExpanded ? 8 * s + stepRows * 22 * s : 24 * s;
|
|
2643
2901
|
float inputH = input != null ? 46 * s : 0;
|
|
2644
2902
|
float bodyH = headH + replyH + listH + inputH + 12 * s;
|
|
2645
2903
|
int W = (int)Math.Ceiling(bodyW) + 2 * shadow, H = (int)Math.Ceiling(bodyH) + 2 * shadow;
|
|
@@ -2719,17 +2977,18 @@ namespace DshComputerUse
|
|
|
2719
2977
|
using (var b = new SolidBrush(paused ? Accent : muted)) g.DrawString(st, fState, b, tx, hy + (hh - stSize.Height) / 2f, fmt);
|
|
2720
2978
|
}
|
|
2721
2979
|
|
|
2722
|
-
//
|
|
2980
|
+
// Reasoning and operations use separate rows throughout.
|
|
2723
2981
|
float lineW = bodyW - 2 * pad;
|
|
2724
2982
|
float ry = body.Y + headH;
|
|
2725
|
-
|
|
2726
|
-
using (var b = new SolidBrush(petText.Length > 0 ? ink : muted))
|
|
2983
|
+
using (var b = new SolidBrush(petThinking.Length > 0 && !petFinished ? ink : muted))
|
|
2727
2984
|
{
|
|
2728
2985
|
if (petExpanded)
|
|
2729
|
-
g.DrawString(TailWrapped(g,
|
|
2986
|
+
g.DrawString(TailWrapped(g, progress, fText, lineW, replyH, wrap), fText, b, new RectangleF(body.X + pad, ry, lineW, replyH), wrap);
|
|
2730
2987
|
else
|
|
2731
2988
|
{
|
|
2732
|
-
string shown =
|
|
2989
|
+
string shown = petThinking.Length > 0 && !petFinished
|
|
2990
|
+
? "思考 · " + Tail(g, petThinking, fText, lineW - g.MeasureString("思考 · ", fText, 100000, fmt).Width, fmt)
|
|
2991
|
+
: Tail(g, progress, fText, lineW, fmt);
|
|
2733
2992
|
SizeF size = g.MeasureString(shown.Length > 0 ? shown : " ", fText, 100000, fmt);
|
|
2734
2993
|
g.DrawString(shown, fText, b, body.X + pad, ry + (replyH - size.Height) / 2f, fmt);
|
|
2735
2994
|
}
|
|
@@ -2742,13 +3001,6 @@ namespace DshComputerUse
|
|
|
2742
3001
|
ly += 4 * s;
|
|
2743
3002
|
using (var pen = new Pen(edge, Math.Max(1f, s))) g.DrawLine(pen, body.X + pad, ly, body.Right - pad, ly);
|
|
2744
3003
|
ly += 4 * s;
|
|
2745
|
-
if (thinkingRow)
|
|
2746
|
-
{
|
|
2747
|
-
float prefixW = g.MeasureString("思考:", fAct, 100000, fmt).Width;
|
|
2748
|
-
string th = Tail(g, petThinking, fAct, lineW - prefixW, fmt);
|
|
2749
|
-
using (var b = new SolidBrush(muted)) { g.DrawString("思考:", fAct, b, body.X + pad, ly + 3 * s, fmt); g.DrawString(th, fAct, b, body.X + pad + prefixW, ly + 3 * s, fmt); }
|
|
2750
|
-
ly += 24 * s;
|
|
2751
|
-
}
|
|
2752
3004
|
int first = Math.Max(0, petSteps.Count - stepRows);
|
|
2753
3005
|
for (int i = first; i < petSteps.Count; i++)
|
|
2754
3006
|
{
|
|
@@ -2764,8 +3016,7 @@ namespace DshComputerUse
|
|
|
2764
3016
|
else
|
|
2765
3017
|
{
|
|
2766
3018
|
string act;
|
|
2767
|
-
if (petFinished) act = petSteps.Count > 0 ? "
|
|
2768
|
-
else if (petThinkingLatest && petThinking.Length > 0) act = "思考:" + Tail(g, petThinking, fAct, lineW - 14 * s - g.MeasureString("思考:", fAct, 100000, fmt).Width, fmt);
|
|
3019
|
+
if (petFinished) act = petSteps.Count > 0 ? "最后操作 · " + petSteps[petSteps.Count - 1] : "";
|
|
2769
3020
|
else
|
|
2770
3021
|
{
|
|
2771
3022
|
var parts = new List<string>();
|
package/lib/client.js
CHANGED
|
@@ -28,6 +28,14 @@ window.__ModuleLoader__.load({
|
|
|
28
28
|
//#endregion
|
|
29
29
|
let react = require("react");
|
|
30
30
|
react = __toESM(react, 1);
|
|
31
|
+
//#region lib/types/browser-host.js
|
|
32
|
+
function httpOrigin(value) {
|
|
33
|
+
try {
|
|
34
|
+
const url = new URL(value);
|
|
35
|
+
if (url.protocol === "http:" || url.protocol === "https:") return url.origin;
|
|
36
|
+
} catch {}
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
31
39
|
//#region lib/types/settings.js
|
|
32
40
|
/** The Loader entry id (settings namespace); see cordis.patch.yml. */
|
|
33
41
|
const ENTRY_ID = "copylee-computer-use";
|
|
@@ -59,6 +67,7 @@ window.__ModuleLoader__.load({
|
|
|
59
67
|
};
|
|
60
68
|
const bool = (key) => typeof out[key] === "boolean" ? out[key] : DEFAULTS[key];
|
|
61
69
|
return {
|
|
70
|
+
harnessUrl: typeof out.harnessUrl === "string" ? httpOrigin(out.harnessUrl.trim()) : void 0,
|
|
62
71
|
accessMode: out.accessMode === "allow-all" || out.accessMode === "per-app" ? out.accessMode : DEFAULTS.accessMode,
|
|
63
72
|
overlay: bool("overlay"),
|
|
64
73
|
overlayLabel: typeof out.overlayLabel === "string" && out.overlayLabel.trim() !== "" ? out.overlayLabel.trim().slice(0, 40) : DEFAULTS.overlayLabel,
|
|
@@ -1173,7 +1182,7 @@ window.__ModuleLoader__.load({
|
|
|
1173
1182
|
}), h("span", { style: {
|
|
1174
1183
|
...S.hint,
|
|
1175
1184
|
fontWeight: 400
|
|
1176
|
-
} }, draft.hostWindow === "pet" ? "DSH
|
|
1185
|
+
} }, draft.hostWindow === "pet" ? "卡片上方显示思考,下方显示操作。可拖动、暂停或停止。正文开始输出时,回到 DSH 对话。" : "操控结束或正文开始输出时,恢复 DSH 窗口。")), h("div", { style: S.label }, "悬浮卡片透明度", h(Select, {
|
|
1177
1186
|
label: "悬浮卡片透明度",
|
|
1178
1187
|
disabled: disabled || draft.hostWindow !== "card" && draft.hostWindow !== "pet",
|
|
1179
1188
|
value: String(draft.cardOpacity),
|