Skip to content

Commit 90bd259

Browse files
committed
keep element ids across actions, which consume the actionable snapshot
1 parent 574c41e commit 90bd259

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

‎apps/desktop/native/computer-use/ComputerUse.swift‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ func discoverApps() throws -> [[String: Any]] {
704704
@MainActor
705705
final class Driver {
706706
var snapshots: [String: Snapshot] = [:]
707+
/// Each app's latest observation, kept after actions consume `snapshots` so the next read reuses its element ids.
708+
var lastObserved: [String: Snapshot] = [:]
707709
func point(_ p: Parameters, app: NSRunningApplication, snapshot: Snapshot, end: Bool = false) throws -> CGPoint {
708710
if let elementId = p.elementId, !end {
709711
guard let frame = elementRect(try snapshot.element(elementId)) else { throw ComputerError("element_unavailable", "Element has no usable frame.") }
@@ -757,7 +759,7 @@ final class Driver {
757759
return ["kind": "action", "action": "activate_app", "bundleId": app.bundleIdentifier!, "dispatched": true, "verified": true]
758760
case "get_app_state":
759761
try requireAccessibility()
760-
let app = try await appFor(p.bundleId, launch: true); let snapshot = Snapshot(app: app, previous: snapshots[app.bundleIdentifier!])
762+
let app = try await appFor(p.bundleId, launch: true); let snapshot = Snapshot(app: app, previous: lastObserved[app.bundleIdentifier!])
761763
let preferredFrame = try p.windowId.map { try windowRect($0, pid: app.processIdentifier) }
762764
try await snapshot.read(preferredWindowFrame: preferredFrame)
763765
let axWindows = snapshot.elements.values.filter { attribute($0, kAXRoleAttribute) as? String == kAXWindowRole }
@@ -781,7 +783,9 @@ final class Driver {
781783
result.merge(rectJSON(rect)) { _, new in new }; return result
782784
}
783785
if snapshots.count >= 16 { snapshots.removeAll() }
786+
if lastObserved.count >= 16 { lastObserved.removeAll() }
784787
snapshots[app.bundleIdentifier!] = snapshot
788+
lastObserved[app.bundleIdentifier!] = snapshot
785789
let root = AXUIElementCreateApplication(app.processIdentifier)
786790
let focusedID = attribute(root, kAXFocusedWindowAttribute).flatMap { CFGetTypeID($0) == AXUIElementGetTypeID() ? snapshot.windowId(holding: unsafeBitCast($0, to: AXUIElement.self)) : nil }
787791
for index in snapshot.nodes.indices {

0 commit comments

Comments
 (0)