Skip to content

Open source CapCut alternative.

TypeScript

Add guidance overlay flow grounded in live accessibility (overlay phase 2)

David Led287dc36780e
diff --git a/apps/Donkey/Sources/DonkeyRuntime/GuidanceOverlayFlow.swift b/apps/Donkey/Sources/DonkeyRuntime/GuidanceOverlayFlow.swift
new file mode 100644
index 00000000..2237e089
--- /dev/null
+++ b/apps/Donkey/Sources/DonkeyRuntime/GuidanceOverlayFlow.swift
@@ -0,0 +1,108 @@
+@preconcurrency import ApplicationServices
+import DonkeyContracts
+import Foundation
+
+/// Produces an accessibility-grounded cursor overlay for a guidance request ("show me where X is").
+///
+/// Visualization-only: it captures the target app's accessibility tree, grounds the requested
+/// controls to a cursor path, and returns an overlay request. It never performs input
+/// (`realPointerMoved` stays false). Because it relies only on the accessibility tree, it works on
+/// any app that exposes one — native or Electron (Chromium) — which is exactly the case where
+/// AppleScript cannot help.
+@MainActor
+public enum GuidanceOverlayFlow {
+ public struct Outcome: Sendable {
+ public var request: PointerCoachCursorGuideRequest?
+ public var reason: String
+ public var resolvedAppName: String?
+
+ public init(request: PointerCoachCursorGuideRequest?, reason: String, resolvedAppName: String?) {
+ self.request = request
+ self.reason = reason
+ self.resolvedAppName = resolvedAppName
+ }
+ }
+
+ public static func cursorGuide(
+ appName: String?,
+ bundleIdentifier: String?,
+ targets: [AccessibilityCursorPathTarget],
+ title: String,
+ traceID: String,
+ windowResolver: MacWindowResolver = MacWindowResolver()
+ ) -> Outcome {
+ guard AXIsProcessTrusted() else {
+ return Outcome(request: nil, reason: "accessibilityNotTrusted", resolvedAppName: nil)
+ }
+ guard !targets.isEmpty else {
+ return Outcome(request: nil, reason: "noTargets", resolvedAppName: nil)
+ }
+ guard let target = resolveTarget(
+ appName: appName,
+ bundleIdentifier: bundleIdentifier,
+ resolver: windowResolver
+ ) else {
+ return Outcome(request: nil, reason: "noWindowForApp", resolvedAppName: nil)
+ }
+
+ let limits = MacAccessibilitySnapshotLimits(maxDepth: 8, maxChildrenPerNode: 120, maxTotalNodes: 1_200)
+ guard let tree = try? ApplicationServicesMacAccessibilitySnapshotCapturer().captureTree(
+ target: target,
+ limits: limits
+ ) else {
+ return Outcome(request: nil, reason: "accessibilityCaptureFailed", resolvedAppName: target.appName)
+ }
+ let snapshot = MacAccessibilitySnapshot(
+ target: target,
+ limits: limits,
+ root: tree.root,
+ totalNodeCount: tree.totalNodeCount,
+ isTreeTruncated: tree.isTreeTruncated
+ )
+ let index = LocalAppAccessibilityControlDiscovery().discover(in: snapshot)
+ let steps = AccessibilityCursorPathBuilder.buildSteps(
+ targetApp: target.appName ?? appName ?? "",
+ windowBounds: snapshot.target.bounds,
+ controls: index.controls,
+ targets: targets,
+ phaseID: "guidance"
+ )
+ guard !steps.isEmpty else {
+ return Outcome(request: nil, reason: "noGroundedTargets", resolvedAppName: target.appName)
+ }
+ let trace = AgentPathTrace(
+ taskID: traceID,
+ title: title,
+ sourceTraceID: traceID,
+ steps: steps,
+ metadata: ["mode": "guidance", "realPointerMoved": "false"]
+ )
+ guard let request = trace.visualizationPlan()?.cursorOverlayRequest() else {
+ return Outcome(request: nil, reason: "noOverlayRequest", resolvedAppName: target.appName)
+ }
+ return Outcome(request: request, reason: "ok", resolvedAppName: target.appName)
+ }
+
+ private static func resolveTarget(
+ appName: String?,
+ bundleIdentifier: String?,
+ resolver: MacWindowResolver
+ ) -> MacWindowTargetCandidate? {
+ if appName != nil || bundleIdentifier != nil {
+ let candidates = resolver.enumerateCandidates()
+ if let match = candidates.first(where: { candidate in
+ if let bundleIdentifier, let candidateBundle = candidate.bundleIdentifier {
+ return candidateBundle.caseInsensitiveCompare(bundleIdentifier) == .orderedSame
+ }
+ if let appName, let candidateApp = candidate.appName {
+ return candidateApp.localizedCaseInsensitiveContains(appName)
+ || appName.localizedCaseInsensitiveContains(candidateApp)
+ }
+ return false
+ }) {
+ return match
+ }
+ }
+ return try? resolver.selectTarget()
+ }
+}
diff --git a/apps/Donkey/Tests/DonkeyRuntimeTests/GuidanceOverlayLiveSmokeTests.swift b/apps/Donkey/Tests/DonkeyRuntimeTests/GuidanceOverlayLiveSmokeTests.swift
new file mode 100644
index 00000000..96e03042
--- /dev/null
+++ b/apps/Donkey/Tests/DonkeyRuntimeTests/GuidanceOverlayLiveSmokeTests.swift
@@ -0,0 +1,61 @@
+import AppKit
+@testable import Donkey
+import DonkeyContracts
+import DonkeyRuntime
+import DonkeyUI
+import Foundation
+import Testing
+
+/// Live, env-gated smoke test for the guidance overlay (plan phase 2): capture a running app's
+/// accessibility tree, ground a named control, and animate the pointer to it — visualization only,
+/// no input. Works on any app with an accessibility tree (native or Electron).
+///
+/// env DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer DONKEY_LIVE_SMOKE=1 \
+/// [DONKEY_GUIDE_APP=Spotify] [DONKEY_GUIDE_TARGET=Search] \
+/// swift test --filter GuidanceOverlayLiveSmokeTests
+@Suite
+struct GuidanceOverlayLiveSmokeTests {
+ @Test
+ @MainActor
+ func showsPointerAtAControlInARunningApp() {
+ let env = ProcessInfo.processInfo.environment
+ guard env["DONKEY_LIVE_SMOKE"] == "1" else { return }
+
+ let appName = env["DONKEY_GUIDE_APP"] ?? "Music"
+ let targetText = env["DONKEY_GUIDE_TARGET"] ?? "Search"
+
+ // Bring the app to the front so the overlay is visible over it.
+ NSWorkspace.shared.launchApplication(appName)
+ pump(0.8)
+
+ let outcome = GuidanceOverlayFlow.cursorGuide(
+ appName: appName,
+ bundleIdentifier: nil,
+ targets: [
+ AccessibilityCursorPathTarget(query: targetText, label: "Here: \(targetText)", kind: .targetControl)
+ ],
+ title: "Where is \(targetText)",
+ traceID: "guidance-smoke"
+ )
+
+ guard let request = outcome.request else {
+ Issue.record("No grounded overlay (reason=\(outcome.reason), app=\(outcome.resolvedAppName ?? "nil")). Grant Accessibility + ensure \(appName) is running with a '\(targetText)' control.")
+ return
+ }
+
+ #expect(!request.steps.isEmpty)
+
+ if NSApplication.shared.activationPolicy() == .prohibited {
+ NSApplication.shared.setActivationPolicy(.accessory)
+ }
+ let overlay = PointerCoachCursorOverlayController()
+ overlay.show(request: request)
+ let duration = request.steps.reduce(1.5) { $0 + $1.travelDuration + $1.holdDuration }
+ pump(duration)
+ }
+
+ @MainActor
+ private func pump(_ seconds: TimeInterval) {
+ RunLoop.main.run(until: Date().addingTimeInterval(seconds))
+ }
+}