Open source CapCut alternative.
TypeScript
refactor(app): make UI parse machinery + overlay renderer production-ready
David Le0ccbbfcadbc5
diff --git a/apps/Donkey/Sources/Donkey/LocalUIElementDetection/DebugUIInspectionOverlayController.swift b/apps/Donkey/Sources/Donkey/LocalUIElementDetection/DebugUIInspectionOverlayController.swiftindex 60ddd404..ae54ed29 100644--- a/apps/Donkey/Sources/Donkey/LocalUIElementDetection/DebugUIInspectionOverlayController.swift+++ b/apps/Donkey/Sources/Donkey/LocalUIElementDetection/DebugUIInspectionOverlayController.swift@@ -6,7 +6,7 @@ import DonkeyRuntimeimport QuartzCore@MainActor-final class DebugUIInspectionOverlayController {+final class DebugUIInspectionOverlayController: DebugUIInspectionOverlayRendering {private var surfaces: [UInt32: DebugUIInspectionSurface] = [:]func render(diff --git a/apps/Donkey/Sources/Donkey/LocalUIElementDetection/DebugUIInspectionOverlayRendering.swift b/apps/Donkey/Sources/Donkey/LocalUIElementDetection/DebugUIInspectionOverlayRendering.swiftnew file mode 100644index 00000000..e82aa048--- /dev/null+++ b/apps/Donkey/Sources/Donkey/LocalUIElementDetection/DebugUIInspectionOverlayRendering.swift@@ -0,0 +1,22 @@+import DonkeyContracts+import DonkeyRuntime++/// Rendering seam for the UI-understanding engine. The engine (parse + per-window cache + warming)+/// runs in every build; only the visual overlay is a debug build concern, so the engine talks to an+/// abstract renderer and the concrete AppKit overlay lives behind `DONKEY_DEBUG_OVERLAY`.+@MainActor+protocol DebugUIInspectionOverlayRendering {+ func render(frame: DebugUIInspectionFrame, snapshot: DebugUIScreenCaptureSnapshot)+ func closeScreens(except activeScreenIDs: Set<UInt32>)+ func setHidden(_ hidden: Bool)+ func close()+}++/// Production renderer: the engine still parses and caches, it just paints nothing.+@MainActor+struct NoopDebugUIInspectionOverlayRenderer: DebugUIInspectionOverlayRendering {+ func render(frame: DebugUIInspectionFrame, snapshot: DebugUIScreenCaptureSnapshot) {}+ func closeScreens(except activeScreenIDs: Set<UInt32>) {}+ func setHidden(_ hidden: Bool) {}+ func close() {}+}diff --git a/apps/Donkey/Sources/DonkeyAI/DebugUIInspectionFrameFusion.swift b/apps/Donkey/Sources/DonkeyAI/DebugUIInspectionFrameFusion.swiftindex 97434a2c..853213be 100644--- a/apps/Donkey/Sources/DonkeyAI/DebugUIInspectionFrameFusion.swift+++ b/apps/Donkey/Sources/DonkeyAI/DebugUIInspectionFrameFusion.swift@@ -1,5 +1,3 @@-#if DONKEY_DEBUG_OVERLAY-import DonkeyContractsimport Foundation@@ -129,5 +127,3 @@ public enum DebugUIInspectionFrameFusion {.joined(separator: " ")}}--#endifdiff --git a/apps/Donkey/Sources/DonkeyAI/ScreenshotParseDebugUIOverlayMapper.swift b/apps/Donkey/Sources/DonkeyAI/ScreenshotParseDebugUIOverlayMapper.swiftindex 70c5aa9b..c22877d6 100644--- a/apps/Donkey/Sources/DonkeyAI/ScreenshotParseDebugUIOverlayMapper.swift+++ b/apps/Donkey/Sources/DonkeyAI/ScreenshotParseDebugUIOverlayMapper.swift@@ -1,5 +1,3 @@-#if DONKEY_DEBUG_OVERLAY-import DonkeyContractsimport DonkeyRuntimeimport Foundation@@ -277,5 +275,3 @@ public enum ScreenshotParseDebugUIOverlayMapper {}}}--#endifdiff --git a/apps/Donkey/Sources/DonkeyAI/VisionParseDebugUIOverlayMapper.swift b/apps/Donkey/Sources/DonkeyAI/VisionParseDebugUIOverlayMapper.swiftindex 5ddfa730..89cf2395 100644--- a/apps/Donkey/Sources/DonkeyAI/VisionParseDebugUIOverlayMapper.swift+++ b/apps/Donkey/Sources/DonkeyAI/VisionParseDebugUIOverlayMapper.swift@@ -1,5 +1,3 @@-#if DONKEY_DEBUG_OVERLAY-import DonkeyContractsimport DonkeyRuntimeimport Foundation@@ -119,5 +117,3 @@ public enum VisionParseDebugUIOverlayMapper {]}}--#endifdiff --git a/apps/Donkey/Sources/DonkeyRuntime/LocalUIElementDetection/DebugUIAccessibilityInspectionService.swift b/apps/Donkey/Sources/DonkeyRuntime/LocalUIElementDetection/DebugUIAccessibilityInspectionService.swiftindex 263c27d9..7d215a16 100644--- a/apps/Donkey/Sources/DonkeyRuntime/LocalUIElementDetection/DebugUIAccessibilityInspectionService.swift+++ b/apps/Donkey/Sources/DonkeyRuntime/LocalUIElementDetection/DebugUIAccessibilityInspectionService.swift@@ -1,5 +1,3 @@-#if DONKEY_DEBUG_OVERLAY-@preconcurrency import AppKitimport CoreGraphicsimport CryptoKit@@ -1773,5 +1771,3 @@ private extension LocalUIElementDetectionTrace {return area > 0 && overlap / area >= 0.72}}--#endifdiff --git a/apps/Donkey/Sources/DonkeyRuntime/LocalUIElementDetection/DebugUIInspectionRuntime.swift b/apps/Donkey/Sources/DonkeyRuntime/LocalUIElementDetection/DebugUIInspectionRuntime.swiftindex d6ba121a..7b69302d 100644--- a/apps/Donkey/Sources/DonkeyRuntime/LocalUIElementDetection/DebugUIInspectionRuntime.swift+++ b/apps/Donkey/Sources/DonkeyRuntime/LocalUIElementDetection/DebugUIInspectionRuntime.swift@@ -1,5 +1,3 @@-#if DONKEY_DEBUG_OVERLAY-@preconcurrency import AppKitimport CoreGraphicsimport CryptoKit@@ -173,13 +171,17 @@ public struct DebugUIElementTracker: Equatable, Sendable {private var pendingContent: [String: PendingTrackedElement]private var appearanceThreshold: Intprivate var disappearanceTolerance: Int+ private var stableDisappearanceTolerance: Int+ private var stableIDPrefixes: [String]private var movementConfirmationSamples: Intpublic init(previousElements: [DebugUIElement] = [],appearanceThreshold: Int = 2,disappearanceTolerance: Int = 2,- movementConfirmationSamples: Int = 2+ movementConfirmationSamples: Int = 2,+ stableDisappearanceTolerance: Int? = nil,+ stableIDPrefixes: [String] = []) {self.previousElements = previousElementsself.lastObservedElements = previousElements@@ -189,9 +191,25 @@ public struct DebugUIElementTracker: Equatable, Sendable {self.pendingContent = [:]self.appearanceThreshold = max(1, appearanceThreshold)self.disappearanceTolerance = max(0, disappearanceTolerance)+ // Stable-ID elements (e.g. accessibility boxes) keep a fixed identity across scans, so a+ // brief detection gap should not yank them — that is what reads as flicker. Volatile+ // elements (e.g. vision parses that reassign IDs every pass) must stay at the base+ // tolerance, otherwise stale boxes linger and stack. Never below the base tolerance.+ self.stableDisappearanceTolerance = max(+ max(0, disappearanceTolerance),+ stableDisappearanceTolerance ?? disappearanceTolerance+ )+ self.stableIDPrefixes = stableIDPrefixesself.movementConfirmationSamples = max(1, movementConfirmationSamples)}+ private func disappearanceTolerance(for element: DebugUIElement) -> Int {+ for prefix in stableIDPrefixes where element.id.hasPrefix(prefix) {+ return stableDisappearanceTolerance+ }+ return disappearanceTolerance+ }+public mutating func update(with frame: DebugUIInspectionFrame,renderNewElementsImmediately: Bool = false@@ -256,7 +274,7 @@ public struct DebugUIElementTracker: Equatable, Sendable {for previous in previousElements where !trackedIDs.contains(previous.id) {let missingCount = (missingCounts[previous.id] ?? 0) + 1- if missingCount <= disappearanceTolerance {+ if missingCount <= disappearanceTolerance(for: previous) {missingCounts[previous.id] = missingCountrendered.append(previous)} else {@@ -846,5 +864,3 @@ protocol DebugUIScreenCapturing: Sendable {}extension DebugUIScreenCaptureService: DebugUIScreenCapturing {}--#endifdiff --git a/apps/Donkey/Tests/DonkeyRuntimeTests/DebugUIInspectionTests.swift b/apps/Donkey/Tests/DonkeyRuntimeTests/DebugUIInspectionTests.swiftindex e0115d18..5e151757 100644--- a/apps/Donkey/Tests/DonkeyRuntimeTests/DebugUIInspectionTests.swift+++ b/apps/Donkey/Tests/DonkeyRuntimeTests/DebugUIInspectionTests.swift@@ -196,6 +196,63 @@ struct DebugUIInspectionTests {#expect(second.elements.map(\.id) == ["ai-2-c"])}+ @Test+ func trackerKeepsStableAccessibilityBoxThroughBriefDetectionGap() {+ // Mirrors the debug overlay's tracker config: vision boxes (ai-) drop immediately, but+ // stable accessibility boxes (ax-) get a few frames of hysteresis so a single missed scan+ // does not strobe the overlay.+ var tracker = DebugUIElementTracker(+ appearanceThreshold: 1,+ disappearanceTolerance: 0,+ movementConfirmationSamples: 1,+ stableDisappearanceTolerance: 4,+ stableIDPrefixes: ["ax-", "window-chrome-"]+ )+ _ = tracker.update(+ with: DebugUIInspectionFrame(elements: [+ element(id: "ax-1.1.1.1", label: "Scroll Area", x: 10, y: 20)+ ]),+ renderNewElementsImmediately: true+ )++ // One scan omits the accessibility box: it must persist instead of flickering out.+ let gap = tracker.update(with: DebugUIInspectionFrame(elements: []))+ #expect(gap.elements.map(\.id) == ["ax-1.1.1.1"])++ // A genuinely vanished box still clears once the hysteresis is exhausted.+ for _ in 0..<4 {+ _ = tracker.update(with: DebugUIInspectionFrame(elements: []))+ }+ let cleared = tracker.update(with: DebugUIInspectionFrame(elements: []))+ #expect(cleared.elements.isEmpty)+ }++ @Test+ func trackerDropsVolatileBoxImmediatelyEvenWithStableHysteresis() {+ // The stable hysteresis must not leak onto vision boxes — a parse that omits an ai- box+ // clears it on the very next frame, so stale vision boxes never stack.+ var tracker = DebugUIElementTracker(+ appearanceThreshold: 1,+ disappearanceTolerance: 0,+ movementConfirmationSamples: 1,+ stableDisappearanceTolerance: 4,+ stableIDPrefixes: ["ax-", "window-chrome-"]+ )+ _ = tracker.update(+ with: DebugUIInspectionFrame(elements: [+ element(id: "ai-1-a", label: "Coldplay", x: 10, y: 20)+ ]),+ renderNewElementsImmediately: true+ )+ let next = tracker.update(+ with: DebugUIInspectionFrame(elements: [+ element(id: "ai-2-b", label: "Search", x: 300, y: 400)+ ]),+ renderNewElementsImmediately: true+ )+ #expect(next.elements.map(\.id) == ["ai-2-b"])+ }+@Testfunc trackerPreservesStableIDForMovedSemanticMatch() {var tracker = DebugUIElementTracker()