Skip to content

Open source CapCut alternative.

TypeScript

feat(app): consume warmed UI understanding in agent decisions

David Le9398be5af2f2
diff --git a/apps/Donkey/Sources/DonkeyAI/VisionComputerUseTools.swift b/apps/Donkey/Sources/DonkeyAI/VisionComputerUseTools.swift
index fa7a4f2c..d75c149d 100644
--- a/apps/Donkey/Sources/DonkeyAI/VisionComputerUseTools.swift
+++ b/apps/Donkey/Sources/DonkeyAI/VisionComputerUseTools.swift
@@ -41,6 +41,9 @@ public final class VisionComputerUseToolProvider {
let bundleIdentifier: String?
let analyzer: any DebugUIInspectionAnalyzing
let store: ParsedVisionStore
+ /// Fused accessibility + vision understanding kept warm by the always-on `UIUnderstandingCoordinator`.
+ /// Consulted before the LLM vision path so a decision reuses already-extracted elements.
+ let understandingStore: WindowUIUnderstandingStore
let minConfidence: Double
let reuseChangedFractionThreshold: Double
let capture: VisionActionDriver.ScreenshotCapture
@@ -56,6 +59,7 @@ public final class VisionComputerUseToolProvider {
bundleIdentifier: String?,
analyzer: any DebugUIInspectionAnalyzing,
store: ParsedVisionStore = .shared,
+ understandingStore: WindowUIUnderstandingStore = .shared,
minConfidence: Double = 0.25,
reuseChangedFractionThreshold: Double = VisionComputerUseToolProvider.reuseChangedFractionThreshold,
capture: @escaping VisionActionDriver.ScreenshotCapture = { try await ScreenCaptureKitWindowScreenshotCapturer().capture(target: $0) },
@@ -66,6 +70,7 @@ public final class VisionComputerUseToolProvider {
self.bundleIdentifier = bundleIdentifier
self.analyzer = analyzer
self.store = store
+ self.understandingStore = understandingStore
self.minConfidence = minConfidence
self.reuseChangedFractionThreshold = reuseChangedFractionThreshold
self.capture = capture
@@ -155,6 +160,19 @@ public final class VisionComputerUseToolProvider {
let elementImageHeight: Int
if let signature,
+ let understanding = understandingStore.reusableEntry(
+ appKey: appKey,
+ signature: signature,
+ changedFractionThreshold: reuseChangedFractionThreshold
+ ) {
+ // The always-on engine already extracted this window (accessibility + vision), often
+ // while it was in the background. Reuse it so the decision is instant and grounded in
+ // accessibility, not just an inline vision parse.
+ usedCache = true
+ elements = understanding.elements
+ elementImageWidth = understanding.imagePixelWidth
+ elementImageHeight = understanding.imagePixelHeight
+ } else if let signature,
let reusable = store.reusableEntry(
appKey: appKey,
signature: signature,
diff --git a/apps/Donkey/Sources/DonkeyRuntime/WindowUIUnderstandingStore.swift b/apps/Donkey/Sources/DonkeyRuntime/WindowUIUnderstandingStore.swift
new file mode 100644
index 00000000..27d88c31
--- /dev/null
+++ b/apps/Donkey/Sources/DonkeyRuntime/WindowUIUnderstandingStore.swift
@@ -0,0 +1,75 @@
+import DonkeyContracts
+import Foundation
+
+/// Process-wide cache of the latest *fused accessibility + vision* understanding per app, produced by
+/// the always-on `UIUnderstandingCoordinator`. The agent's decision tools read this first so a
+/// `screen.captureAndAnalyze` reuses elements that were already extracted (often for a background
+/// window) instead of paying for a fresh parse.
+///
+/// Like `ParsedVisionStore`, elements are stored in the parse image's pixel space together with that
+/// pixel size, and reuse is gated by a `ScreenshotSignature` so a stale entry is never reused for a
+/// window that has since changed. Keyed by a stable app key (bundle identifier, falling back to app
+/// name) so the agent can look it up by the app it is acting on.
+public final class WindowUIUnderstandingStore: @unchecked Sendable {
+ public static let shared = WindowUIUnderstandingStore()
+
+ public struct Entry: Sendable {
+ public var signature: ScreenshotSignature
+ /// Fused accessibility + vision elements with `bbox` in the parse image's pixel space.
+ public var elements: [DebugUIElement]
+ public var imagePixelWidth: Int
+ public var imagePixelHeight: Int
+ public var capturedAtUptimeMS: Double
+
+ public init(
+ signature: ScreenshotSignature,
+ elements: [DebugUIElement],
+ imagePixelWidth: Int,
+ imagePixelHeight: Int,
+ capturedAtUptimeMS: Double
+ ) {
+ self.signature = signature
+ self.elements = elements
+ self.imagePixelWidth = imagePixelWidth
+ self.imagePixelHeight = imagePixelHeight
+ self.capturedAtUptimeMS = capturedAtUptimeMS
+ }
+ }
+
+ private let lock = NSLock()
+ private var entries: [String: Entry] = [:]
+
+ public init() {}
+
+ public func store(appKey: String, entry: Entry) {
+ let key = Self.normalize(appKey)
+ guard !key.isEmpty else { return }
+ lock.lock()
+ defer { lock.unlock() }
+ entries[key] = entry
+ }
+
+ public func latest(appKey: String) -> Entry? {
+ let key = Self.normalize(appKey)
+ guard !key.isEmpty else { return nil }
+ lock.lock()
+ defer { lock.unlock() }
+ return entries[key]
+ }
+
+ /// Returns the cached entry only when its fingerprint is close enough to `signature` to treat the
+ /// window as unchanged, so the caller can reuse its elements without re-parsing.
+ public func reusableEntry(
+ appKey: String,
+ signature: ScreenshotSignature,
+ changedFractionThreshold: Double
+ ) -> Entry? {
+ guard let entry = latest(appKey: appKey) else { return nil }
+ let changed = signature.changedFraction(from: entry.signature)
+ return changed <= changedFractionThreshold ? entry : nil
+ }
+
+ private static func normalize(_ key: String) -> String {
+ key.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
+ }
+}