forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpatialOverlayResolver.swift
More file actions
207 lines (182 loc) · 6.06 KB
/
Copy pathSpatialOverlayResolver.swift
File metadata and controls
207 lines (182 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import Foundation
struct SpatialOverlayDesktopSnapshot: Equatable {
let screens: [SpatialOverlayScreen]
let windows: [SpatialOverlayWindow]
let candidates: [SpatialOverlayAnchorCandidate]
init(
screens: [SpatialOverlayScreen],
windows: [SpatialOverlayWindow] = [],
candidates: [SpatialOverlayAnchorCandidate] = []
) {
self.screens = screens
self.windows = windows
self.candidates = candidates
}
}
struct SpatialOverlayAnchorSpec: Equatable {
let id: String
let use: SpatialOverlayAnchorUse
let minimumConfidence: Double
let preferredSources: [SpatialOverlayTargetSource]
init(
id: String,
use: SpatialOverlayAnchorUse,
minimumConfidence: Double,
preferredSources: [SpatialOverlayTargetSource] = [
.accessibility,
.ocr,
.semanticState,
.layoutHeuristic,
.fixedScreenAnchor,
.appWindow,
.cgWindowList,
]
) {
self.id = id
self.use = use
self.minimumConfidence = minimumConfidence
self.preferredSources = preferredSources
}
}
struct SpatialOverlayAnchorResolution: Equatable {
let spec: SpatialOverlayAnchorSpec
let candidate: SpatialOverlayAnchorCandidate
}
enum SpatialOverlayResolutionFailure: Error, Equatable, CustomStringConvertible {
case noCandidates
case noCandidateAllowedForUse(SpatialOverlayAnchorUse)
case belowConfidenceThreshold(required: Double, best: Double)
var description: String {
switch self {
case .noCandidates:
return "No anchor candidates were available"
case .noCandidateAllowedForUse(let use):
return "No anchor candidate is allowed for \(use)"
case .belowConfidenceThreshold(let required, let best):
return "Best anchor confidence \(best) is below required threshold \(required)"
}
}
}
protocol SpatialOverlayTargetProvider {
func candidates(in snapshot: SpatialOverlayDesktopSnapshot, for spec: SpatialOverlayAnchorSpec)
-> [SpatialOverlayAnchorCandidate]
}
struct SpatialOverlayStaticTargetProvider: SpatialOverlayTargetProvider {
func candidates(in snapshot: SpatialOverlayDesktopSnapshot, for spec: SpatialOverlayAnchorSpec)
-> [SpatialOverlayAnchorCandidate]
{
snapshot.candidates.filter { candidateMatches($0, spec: spec) }
}
private func candidateMatches(
_ candidate: SpatialOverlayAnchorCandidate, spec: SpatialOverlayAnchorSpec
)
-> Bool
{
let specTokens = Set(
spec.id
.split(separator: ".")
.map(String.init)
.filter { token in
!["claude", "chatgpt", "guidance", "click", "display", "perform", "anchor", "target"]
.contains(token)
})
guard !specTokens.isEmpty else { return true }
let searchable =
([candidate.id]
+ candidate.evidence.flatMap { evidence in
[evidence.label ?? ""] + evidence.diagnostics
})
.joined(separator: " ")
.lowercased()
return specTokens.contains { searchable.contains($0.lowercased()) }
}
}
struct SpatialOverlayAnchorResolver {
let providers: [SpatialOverlayTargetProvider]
init(providers: [SpatialOverlayTargetProvider] = [SpatialOverlayStaticTargetProvider()]) {
self.providers = providers
}
func resolve(
_ spec: SpatialOverlayAnchorSpec,
in snapshot: SpatialOverlayDesktopSnapshot
) -> Result<SpatialOverlayAnchorResolution, SpatialOverlayResolutionFailure> {
let candidates = providers.flatMap { $0.candidates(in: snapshot, for: spec) }
guard !candidates.isEmpty else {
return .failure(.noCandidates)
}
let allowed = candidates.filter { candidateIsAllowed($0, for: spec.use) }
guard !allowed.isEmpty else {
return .failure(.noCandidateAllowedForUse(spec.use))
}
let confident = allowed.filter { $0.confidence >= spec.minimumConfidence }
guard !confident.isEmpty else {
let bestConfidence = allowed.map(\.confidence).max() ?? 0
return .failure(
.belowConfidenceThreshold(required: spec.minimumConfidence, best: bestConfidence))
}
let ranked = confident.sorted { lhs, rhs in
let lhsRank = sourceRank(lhs, spec: spec)
let rhsRank = sourceRank(rhs, spec: spec)
if lhsRank != rhsRank {
return lhsRank < rhsRank
}
if lhs.confidence != rhs.confidence {
return lhs.confidence > rhs.confidence
}
return lhs.id < rhs.id
}
guard let best = ranked.first else {
return .failure(.noCandidates)
}
return .success(SpatialOverlayAnchorResolution(spec: spec, candidate: best))
}
private func sourceRank(
_ candidate: SpatialOverlayAnchorCandidate, spec: SpatialOverlayAnchorSpec
)
-> Int
{
let sources = candidate.evidence.map(\.source)
return spec.preferredSources.enumerated().compactMap { index, source in
sources.contains(source) ? index : nil
}.min() ?? spec.preferredSources.count
}
private func candidateIsAllowed(
_ candidate: SpatialOverlayAnchorCandidate,
for use: SpatialOverlayAnchorUse
) -> Bool {
guard candidate.allowedUses.contains(use) else { return false }
guard use == .performClick else { return true }
return candidate.evidence.contains { $0.source == .accessibility || $0.source == .ocr }
}
}
struct SpatialOverlayReplayFixture: Equatable {
let id: String
let snapshot: SpatialOverlayDesktopSnapshot
let placementSpec: SpatialOverlayPlacementSpec
init(
id: String,
snapshot: SpatialOverlayDesktopSnapshot,
placementSpec: SpatialOverlayPlacementSpec
) {
self.id = id
self.snapshot = snapshot
self.placementSpec = placementSpec
}
func place(_ anchorSpec: SpatialOverlayAnchorSpec)
-> Result<SpatialOverlayPlacementResult, Error>
{
let resolver = SpatialOverlayAnchorResolver()
switch resolver.resolve(anchorSpec, in: snapshot) {
case .success(let resolution):
switch SpatialOverlayPlacementSolver.place(target: resolution.candidate, spec: placementSpec)
{
case .success(let placement):
return .success(placement)
case .failure(let failure):
return .failure(failure)
}
case .failure(let failure):
return .failure(failure)
}
}
}