dsh-native-uia 0.0.0-stage → 0.4.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 (62) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +663 -2
  3. package/client/client.js +269 -0
  4. package/cordis.patch.yml +10 -0
  5. package/icon.svg +11 -0
  6. package/locale/en.json +6 -0
  7. package/locale/zh.json +6 -0
  8. package/package.json +75 -4
  9. package/scripts/bench-live.mjs +157 -0
  10. package/scripts/check-lease.mjs +308 -0
  11. package/scripts/install.mjs +154 -0
  12. package/scripts/lib/dsh-tools-stub.mjs +14 -0
  13. package/scripts/lib/load-tools-source.mjs +25 -0
  14. package/scripts/lib/sources.mjs +62 -0
  15. package/scripts/load-tools.mjs +74 -0
  16. package/scripts/measure-parse.mjs +77 -0
  17. package/scripts/regression-halt-latch.mjs +100 -0
  18. package/scripts/regression-quality.mjs +207 -0
  19. package/scripts/regression-tools.mjs +192 -0
  20. package/scripts/regression-worker.ps1 +1071 -0
  21. package/scripts/smoke.mjs +158 -0
  22. package/scripts/verify-capture-scale.mjs +37 -0
  23. package/scripts/verify-live-clipboard.mjs +108 -0
  24. package/scripts/verify-live-style.mjs +46 -0
  25. package/scripts/verify-live.mjs +104 -0
  26. package/scripts/verify-locator.mjs +52 -0
  27. package/scripts/verify.mjs +2110 -0
  28. package/src/activity-window.js +276 -0
  29. package/src/dashboard.js +103 -0
  30. package/src/guard.js +201 -0
  31. package/src/index.js +525 -0
  32. package/src/native-uia-remote.js +80 -0
  33. package/src/notes.js +83 -0
  34. package/src/state.js +267 -0
  35. package/src/themes.js +74 -0
  36. package/src/tools/defs/act.js +533 -0
  37. package/src/tools/defs/lifecycle.js +205 -0
  38. package/src/tools/defs/observe.js +309 -0
  39. package/src/tools/defs/pointer.js +298 -0
  40. package/src/tools/format.js +301 -0
  41. package/src/tools/index.js +128 -0
  42. package/src/tools/ocr.js +91 -0
  43. package/src/tools/recipes.js +44 -0
  44. package/src/tools/runtime.js +42 -0
  45. package/src/worker-client.js +268 -0
  46. package/worker/assets/dsh-whale.path +1 -0
  47. package/worker/bin/build-ocr.ps1 +41 -0
  48. package/worker/bin/ocr.cs +69 -0
  49. package/worker/bin/ocr.exe +0 -0
  50. package/worker/indicator-server.ps1 +2963 -0
  51. package/worker/lib/01-core.ps1 +64 -0
  52. package/worker/lib/02-uia.ps1 +221 -0
  53. package/worker/lib/03-window.ps1 +194 -0
  54. package/worker/lib/04-match.ps1 +435 -0
  55. package/worker/lib/05-value.ps1 +149 -0
  56. package/worker/lib/06-snapshot.ps1 +345 -0
  57. package/worker/lib/07-action.ps1 +291 -0
  58. package/worker/lib/08-command.ps1 +1485 -0
  59. package/worker/lib/09-pointer.ps1 +840 -0
  60. package/worker/lib/10-dispatch.ps1 +134 -0
  61. package/worker/ps-stdio.ps1 +173 -0
  62. package/worker/uia-worker.ps1 +599 -0
@@ -0,0 +1,134 @@
1
+ # 10-dispatch.ps1 -- handler table, budgets, request dispatch
2
+ #
3
+ # Part of worker/uia-worker.ps1, dot-sourced by it in the order scripts/lib/sources.mjs declares.
4
+ # It lives in its own file only so a section can be read without scrolling the whole RPC server.
5
+ # Dot-sourcing shares the entry's script scope, so a helper here may use $script:W and may call
6
+ # any function from any other part: neighbour order is not a dependency.
7
+ # ---------------------------------------------------------------- dispatch
8
+ $handlers = @{
9
+ ping = 'Cmd-Ping'
10
+ list_windows = 'Cmd-ListWindows'
11
+ resolve_window = 'Cmd-ResolveWindow'
12
+ snapshot = 'Cmd-Snapshot'
13
+ describe = 'Cmd-Describe'
14
+ descriptor_match = 'Cmd-DescriptorMatch'
15
+ act = 'Cmd-Act'
16
+ set_pointer_mode = 'Cmd-SetPointerMode'
17
+ escape_pressed = 'Cmd-EscapePressed'
18
+
19
+ prepare_batch = 'Cmd-PrepareBatch'
20
+ commit_batch = 'Cmd-CommitBatch'
21
+ wait = 'Cmd-Wait'
22
+ capture = 'Cmd-Capture'
23
+ capture_screen = 'Cmd-CaptureScreen'
24
+ ocr = 'Cmd-Ocr'
25
+ find_text = 'Cmd-FindText'
26
+ launch = 'Cmd-Launch'
27
+ close_window = 'Cmd-CloseWindow'
28
+ pointer_target = 'Cmd-PointerTarget'
29
+ pointer_click = 'Cmd-PointerClick'
30
+ pointer_drag = 'Cmd-PointerDrag'
31
+ pointer_scroll = 'Cmd-PointerScroll'
32
+ pointer_mouse = 'Cmd-PointerMouse'
33
+ window_action = 'Cmd-WindowAction'
34
+ type_text = 'Cmd-TypeText'
35
+ clipboard_read = 'Cmd-ClipboardRead'
36
+ clipboard_write = 'Cmd-ClipboardWrite'
37
+ clipboard_clear = 'Cmd-ClipboardClear'
38
+ key = 'Cmd-Key'
39
+ shutdown = 'Cmd-Shutdown'
40
+ }
41
+ $defaultBudget = @{
42
+ snapshot = 15000
43
+ commit_batch = 25000
44
+ prepare_batch = 12000
45
+ capture = 12000
46
+ capture_screen = 15000
47
+ ocr = 20000
48
+ launch = 25000
49
+ resolve_window = 15000
50
+ wait = 25000
51
+ find_text = 12000
52
+ close_window = 12000
53
+ act = 15000
54
+ set_pointer_mode = 3000
55
+ escape_pressed = 2000
56
+
57
+ list_windows = 8000
58
+ pointer_click = 5000
59
+ pointer_drag = 8000
60
+ pointer_scroll = 8000
61
+ pointer_mouse = 8000
62
+ window_action = 8000
63
+ type_text = 8000
64
+ clipboard_read = 5000
65
+ clipboard_write = 5000
66
+ clipboard_clear = 5000
67
+ key = 5000
68
+ pointer_target = 3000
69
+ describe = 8000
70
+ descriptor_match = 6000
71
+ ping = 5000
72
+ }
73
+
74
+ function Invoke-Request($line) {
75
+ $id = 0
76
+ $cmd = ''
77
+ $A = @{}
78
+ try {
79
+ $obj = $null
80
+ try { $obj = ConvertFrom-Json $line } catch { Send-Err 0 'bad_json' ('not valid JSON: ' + $_.Exception.Message); return $true }
81
+ if ($null -eq $obj) { Send-Err 0 'bad_json' 'empty request'; return $true }
82
+ $pp = $obj.PSObject.Properties['id']
83
+ if ($null -ne $pp -and $null -ne $pp.Value) { try { $id = [long]$pp.Value } catch { $id = 0 } }
84
+ $pp = $obj.PSObject.Properties['cmd']
85
+ if ($null -ne $pp -and $null -ne $pp.Value) { $cmd = [string]$pp.Value }
86
+ $pp = $obj.PSObject.Properties['args']
87
+ if ($null -ne $pp -and $null -ne $pp.Value) { $d = Dict $pp.Value; if ($d -is [System.Collections.IDictionary]) { $A = $d } }
88
+ if ($cmd -eq '' -or -not $handlers.ContainsKey($cmd)) {
89
+ Send-Err $id 'unknown_cmd' ("unknown cmd '" + (OneLine $cmd) + "'")
90
+ return $true
91
+ }
92
+ $budgetMs = IntArg $A 'timeoutMs' 0
93
+ if ($budgetMs -gt 0) { $budgetMs = $budgetMs + 2500 } else {
94
+ $b = $defaultBudget[$cmd]
95
+ if ($null -eq $b) { $b = 10000 }
96
+ $budgetMs = [int]$b
97
+ }
98
+ Set-Deadline $budgetMs
99
+ $fn = $handlers[$cmd]
100
+ $result = & $fn $A
101
+ Set-Deadline 0
102
+ if ($cmd -eq 'shutdown') {
103
+ Send-Ok $id $result
104
+ Cleanup-All
105
+ return $false
106
+ }
107
+ Send-Ok $id $result
108
+ } catch {
109
+ Set-Deadline 0
110
+ $code, $msg = Split-Err $_
111
+ $log = $cmd + ' -> ' + $code + ': ' + (OneLine $msg)
112
+ if ($script:W.errors.Count -lt 200) { $script:W.errors.Add($log) | Out-Null }
113
+ Log $log
114
+ Send-Err $id $code $msg
115
+ }
116
+ return $true
117
+ }
118
+
119
+ function Cleanup-All {
120
+ # No Marshal.ReleaseComObject here either: managed UIA elements are not COM objects, so the
121
+ # call always threw and the empty catch swallowed it. Dropping every reference and letting the
122
+ # GC run below is the whole cleanup.
123
+ foreach ($rk in @($script:W.refs.Keys)) {
124
+ $r = $script:W.refs[$rk]
125
+ if ($null -ne $r) { $r.ae = $null }
126
+ }
127
+ $script:W.refs.Clear()
128
+ $script:W.refOrder.Clear()
129
+ $script:W.windows.Clear()
130
+ $script:W.winByHwnd.Clear()
131
+ $script:W.totalRefs = 0
132
+ try { [GC]::Collect(); [GC]::WaitForPendingFinalizers(); [GC]::Collect() } catch { }
133
+ }
134
+
@@ -0,0 +1,173 @@
1
+ # ps-stdio.ps1 -- stdio JSON framing helpers shared verbatim by the two resident
2
+ # PowerShell processes: uia-worker.ps1 (the UIA worker) and indicator-server.ps1
3
+ # (the overlay indicator).
4
+ #
5
+ # Each host loads this file at the top of its framing/io section:
6
+ # . (Join-Path $PSScriptRoot 'ps-stdio.ps1')
7
+ # Dot-sourcing keeps the caller's scope, so the functions below run against the
8
+ # host's script scope and the $script:escCache initialised below lands there.
9
+ # (scripts/regression-worker.ps1 loads these functions by AST extraction instead,
10
+ # which skips top-level assignments, so it initialises $script:escCache itself.)
11
+ #
12
+ # HOST-AGNOSTIC RULE: none of these functions may reference $script:W, $script:S
13
+ # or any other host-owned variable. A dot-sourced function body resolves $script:
14
+ # against whichever host loaded it, so one host-specific reference would silently
15
+ # break in the other process.
16
+ #
17
+ # Send-Ok / Send-Err are deliberately NOT shared: the worker writes $script:W.stats,
18
+ # the indicator writes $script:S.stats and decorates every result with exStyles and
19
+ # the last palette. Those shapes are per-host contracts, so they stay in their hosts.
20
+ #
21
+ # SAVED AS UTF-8 WITH BOM, CONTENT ASCII-ONLY: Windows PowerShell 5.1 decodes a
22
+ # BOM-less .ps1 with the system codepage, so an unlabelled file is one editor save
23
+ # away from mojibake. ASCII-only source cannot be corrupted that way.
24
+ #
25
+ # Escaping contract: every response line is escaped to pure ASCII -- chars > 0x7E
26
+ # become \uXXXX so the byte stream on the pipe is 7-bit clean regardless of the
27
+ # codepage the pipe was created with. JsonOf preserves the "absent vs null"
28
+ # distinction: a field explicitly marked [DBNull]::Value is emitted as JSON null,
29
+ # ordinary $null values stay dropped.
30
+
31
+ $script:escCache = @{}
32
+
33
+ function Esc([string]$s) {
34
+ if ($null -eq $s) { return '' }
35
+ $t = $s.Replace('\', '\\').Replace('"', '\"')
36
+ $t = $t.Replace("`b", '\b').Replace("`f", '\f').Replace("`n", '\n').Replace("`r", '\r').Replace("`t", '\t')
37
+ if ($t.Length -gt 0 -and $t -notmatch '[^\x20-\x7E]') { return $t }
38
+ $sb = New-Object System.Text.StringBuilder
39
+ $chars = $t.ToCharArray()
40
+ for ($i = 0; $i -lt $chars.Length; $i++) {
41
+ $c = $chars[$i]
42
+ $code = [int]$c
43
+ if ($code -ge 32 -and $code -le 126) {
44
+ [void]$sb.Append($c)
45
+ } else {
46
+ $key = [string]$code
47
+ $rep = $script:escCache.Item($key)
48
+ if ($null -eq $rep) { $rep = '\u' + $code.ToString('x4'); $script:escCache[$key] = $rep }
49
+ [void]$sb.Append($rep)
50
+ }
51
+ }
52
+ return $sb.ToString()
53
+ }
54
+
55
+ function JsonOf($o) {
56
+ if ($null -eq $o) { return 'null' }
57
+ if ($o -is [bool]) { if ($o) { return 'true' } else { return 'false' } }
58
+ if ($o -is [string]) { return '"' + (Esc $o) + '"' }
59
+ if ($o -is [System.Collections.IDictionary]) {
60
+ $parts = New-Object System.Collections.Generic.List[string]
61
+ # GetEnumerator(), never $o.Keys: PowerShell's member lookup resolves `$dict.Keys` to the VALUE
62
+ # of a dictionary entry literally named "keys" before it resolves the .Keys property, so a
63
+ # result field called `keys` serialized as {} (the iteration walked the string 'shift' and
64
+ # indexed the dictionary by character). Enumerator entries are immune to member shadowing.
65
+ foreach ($entry in $o.GetEnumerator()) {
66
+ $k = $entry.Key
67
+ $v = $entry.Value
68
+ # A field explicitly marked [DBNull]::Value is emitted as JSON null. Ordinary $null values
69
+ # stay dropped (many consumers distinguish "absent" from "null", e.g. act.before/after), so
70
+ # an intentional null has to be opted into case by case.
71
+ if ($v -is [System.DBNull]) { [void]$parts.Add('"' + (Esc ([string]$k)) + '":null'); continue }
72
+ # Reference equality, not value equality: PowerShell answers TRUE to
73
+ # `$null -eq $false`, `$null -eq 0`, `$null -eq @()` and `$null -eq ''`, so a
74
+ # value test silently deletes performedCount=0, truncated=false and empty
75
+ # names as well. The "absent vs null" contract is preserved for real nulls.
76
+ if ([Object]::ReferenceEquals($v, $null)) { continue }
77
+ [void]$parts.Add('"' + (Esc ([string]$k)) + '":' + (JsonOf $v))
78
+ }
79
+ if ($parts.Count -eq 0) { return '{}' }
80
+ return '{' + ([string]::Join(',', $parts)) + '}'
81
+ }
82
+ if ($o -is [System.Management.Automation.PSCustomObject]) {
83
+ $parts = New-Object System.Collections.Generic.List[string]
84
+ foreach ($kk in $o.PSObject.Properties) {
85
+ if ($null -eq $kk.Value) { continue }
86
+ [void]$parts.Add('"' + (Esc $kk.Name) + '":' + (JsonOf $kk.Value))
87
+ }
88
+ if ($parts.Count -eq 0) { return '{}' }
89
+ return '{' + ([string]::Join(',', $parts)) + '}'
90
+ }
91
+ if ($o -is [byte] -or $o -is [int16] -or $o -is [int32] -or $o -is [int64] -or $o -is [uint32] -or $o -is [sbyte] -or $o -is [uint16] -or $o -is [uint64]) {
92
+ return [string]([long]$o)
93
+ }
94
+ if ($o -is [double] -or $o -is [single] -or $o -is [decimal]) {
95
+ $d = [double]$o
96
+ if ([double]::IsNaN($d) -or [double]::IsInfinity($d)) { return 'null' }
97
+ if ($d -eq [Math]::Floor($d) -and [Math]::Abs($d) -lt 9.0E+15) { return ([long]$d).ToString([System.Globalization.CultureInfo]::InvariantCulture) }
98
+ return $d.ToString('R', [System.Globalization.CultureInfo]::InvariantCulture)
99
+ }
100
+ if ($o -is [System.Collections.IEnumerable]) {
101
+ $parts = New-Object System.Collections.Generic.List[string]
102
+ foreach ($it in $o) { [void]$parts.Add((JsonOf $it)) }
103
+ return '[' + ([string]::Join(',', $parts)) + ']'
104
+ }
105
+ return '"' + (Esc ([string]$o)) + '"'
106
+ }
107
+
108
+ function OneLine([string]$s) {
109
+ if ($null -eq $s) { return '' }
110
+ $t = $s.Replace("`r", ' ').Replace("`n", ' ')
111
+ if ($t.Length -gt 400) { $t = $t.Substring(0, 400) + '~' }
112
+ return $t
113
+ }
114
+
115
+ function Err($code, $message) {
116
+ throw ('ERR|' + $code + '|' + $message)
117
+ }
118
+
119
+ function ArgOf($A, $name) { if ($null -eq $A) { return $null } ; if (-not $A.Contains($name)) { return $null }; return $A[$name] }
120
+ function IntArg($A, $name, $default) {
121
+ $v = ArgOf $A $name
122
+ if ($null -eq $v -or $v -is [string] -and ([string]$v).Length -eq 0) { return $default }
123
+ try { return [int][double]$v } catch { return $default }
124
+ }
125
+ function StrArg($A, $name, $default) {
126
+ $v = ArgOf $A $name
127
+ if ($null -eq $v) { return $default }
128
+ if ($v -is [string]) { return $v }
129
+ return [string]$v
130
+ }
131
+ function BoolArg($A, $name, $default) {
132
+ $v = ArgOf $A $name
133
+ if ($null -eq $v) { return $default }
134
+ if ($v -is [bool]) { return $v }
135
+ $s = ([string]$v).ToLowerInvariant()
136
+ if ($s -eq 'true' -or $s -eq '1' -or $s -eq 'yes') { return $true }
137
+ if ($s -eq 'false' -or $s -eq '0' -or $s -eq 'no') { return $false }
138
+ return $default
139
+ }
140
+
141
+ # Normalises a handler argument that may arrive as PSCustomObject into a hashtable.
142
+ # Scalars pass through unchanged; every caller gates on -is [IDictionary] before use,
143
+ # so the loose return is safe in both hosts.
144
+ function Dict($o) {
145
+ if ($null -eq $o) { return $null }
146
+ if ($o -is [System.Collections.IDictionary]) { return $o }
147
+ if ($o -is [System.Management.Automation.PSCustomObject]) {
148
+ $h = @{}
149
+ foreach ($kk in $o.PSObject.Properties) { $h[$kk.Name] = (Dict $kk.Value) }
150
+ return $h
151
+ }
152
+ return $o
153
+ }
154
+
155
+ # Splits a caught ErrorRecord into (code, message). ERR|-thrown errors keep their
156
+ # declared code; raw exceptions are classified -- a released UIA element is
157
+ # element_gone, COM/method failures surface as uia_error, anything else is
158
+ # internal_error.
159
+ function Split-Err($errRecord) {
160
+ $msg = $errRecord.Exception.Message
161
+ if ($null -eq $msg) { $msg = '' }
162
+ if ($msg.StartsWith('ERR|')) {
163
+ $parts = $msg.Split('|', 3)
164
+ if ($parts.Count -ge 3) { return @($parts[1], $parts[2]) }
165
+ if ($parts.Count -eq 2) { return @($parts[1], '') }
166
+ }
167
+ $ex = $errRecord.Exception
168
+ $code = 'internal_error'
169
+ if ($ex -is [System.Runtime.InteropServices.InvalidComObjectException]) { $code = 'element_gone' }
170
+ elseif ($ex -is [System.Runtime.InteropServices.COMException]) { $code = 'uia_error' }
171
+ elseif ($ex -is [System.Management.Automation.MethodInvocationException]) { $code = 'uia_error' }
172
+ return @($code, $msg)
173
+ }