Open source CapCut alternative.
TypeScript
feat(app): realtime per-turn cursor-path visualization
David Le0a32ee6104dc
diff --git a/apps/Donkey/Sources/Donkey/UserQueryCommandHandler.swift b/apps/Donkey/Sources/Donkey/UserQueryCommandHandler.swiftindex fd15200f..bd246c98 100644--- a/apps/Donkey/Sources/Donkey/UserQueryCommandHandler.swift+++ b/apps/Donkey/Sources/Donkey/UserQueryCommandHandler.swift@@ -1,3 +1,4 @@+import AppKitimport DonkeyAIimport DonkeyContractsimport DonkeyHarness@@ -174,7 +175,8 @@ struct LocalAppUserQueryCommandHandler: UserQueryCommandHandling {command: redactedCommand,traceID: traceID,taskID: taskID,- preparedTurnMetadata: Self.genericHarnessMetadata(preparedTurn: genericPreparedTurn)+ preparedTurnMetadata: Self.genericHarnessMetadata(preparedTurn: genericPreparedTurn),+ visualize: context?.agentVisualizationChanged)}@@ -213,7 +215,8 @@ struct LocalAppUserQueryCommandHandler: UserQueryCommandHandling {command: String,traceID: String,taskID: String,- preparedTurnMetadata: [String: String]+ preparedTurnMetadata: [String: String],+ visualize: (@MainActor @Sendable (AgentVisualizationPlan) -> Void)? = nil) async -> UserQueryCommandHandlingResult {guard permissionPolicy.allowedCapabilities.contains(.input) else {return await failHarnessRun(@@ -334,7 +337,25 @@ struct LocalAppUserQueryCommandHandler: UserQueryCommandHandling {// Suspend the warm-cache monitor for the whole drive so it never races us writing the same// app's parse. The run between these calls has no early exit, so resume always pairs with it.VisionWarmCacheActivity.shared.suspend()- let runSteps = await runtime.run(taskID: taskID, planner: planner, maxSteps: 24)+ // Realtime per-turn cursor path: after each step that physically moved the pointer (a click),+ // animate the overlay cursor traveling to that exact target, so the visualization tracks what+ // the harness is actually doing rather than a precomputed plan.+ var onStep: (@Sendable (HarnessStepExecutionResult) async -> Void)?+ if let present = visualize {+ onStep = { (step: HarnessStepExecutionResult) async -> Void in+ await MainActor.run {+ let screenSize = (NSScreen.main ?? NSScreen.screens.first)?.frame.size ?? .zero+ guard let plan = Self.cursorVisualizationPlan(+ for: step,+ appName: appName,+ traceID: traceID,+ screenSize: screenSize+ ) else { return }+ present(plan)+ }+ }+ }+ let runSteps = await runtime.run(taskID: taskID, planner: planner, maxSteps: 24, onStep: onStep)VisionWarmCacheActivity.shared.resume()let finalTask = await genericHarnessLifecycle.coordinator.task(id: taskID)@@ -558,6 +579,55 @@ struct LocalAppUserQueryCommandHandler: UserQueryCommandHandling {return (resolved.appName ?? requested, resolved.bundleIdentifier)}+ /// Builds a one-step cursor-path visualization toward the exact screen point a just-executed action+ /// clicked, in screen-normalized coordinates for the overlay. Returns nil for steps that did not+ /// move the pointer (observation, conversation, AXPress without a coordinate fallback).+ static func cursorVisualizationPlan(+ for step: HarnessStepExecutionResult,+ appName: String,+ traceID: String,+ screenSize: CGSize+ ) -> AgentVisualizationPlan? {+ guard let result = step.toolResult,+ result.status == .succeeded,+ let raw = result.metadata["screenPoint"],+ screenSize.width > 0, screenSize.height > 0 else {+ return nil+ }+ let parts = raw.split(separator: ",")+ guard parts.count == 2,+ let px = Double(parts[0].trimmingCharacters(in: .whitespaces)),+ let py = Double(parts[1].trimmingCharacters(in: .whitespaces)) else {+ return nil+ }+ let label = result.metadata["label"].flatMap { $0.isEmpty ? nil : $0 } ?? appName+ let target = AgentVisualizationStepTarget(+ point: HotLoopPoint(+ x: px / Double(screenSize.width),+ y: py / Double(screenSize.height),+ space: .normalizedTarget+ ),+ description: label,+ source: .actionTrace,+ confidence: 0.95+ )+ return AgentVisualizationPlan(+ title: appName,+ executionMode: .live,+ sourceTraceID: traceID,+ steps: [+ AgentVisualizationStep(+ kind: .moveToTarget,+ label: label,+ target: target,+ travelDuration: 0.45,+ holdDuration: 0.5+ )+ ],+ metadata: ["realPointerMoved": "true"]+ )+ }+static func genericHarnessToolNames() -> [String] {let allowedSensitiveTools: Set<String> = ["automation.applescript.generate"diff --git a/apps/Donkey/Sources/DonkeyHarness/GenericHarnessRuntime.swift b/apps/Donkey/Sources/DonkeyHarness/GenericHarnessRuntime.swiftindex 71db0416..1402c7ff 100644--- a/apps/Donkey/Sources/DonkeyHarness/GenericHarnessRuntime.swift+++ b/apps/Donkey/Sources/DonkeyHarness/GenericHarnessRuntime.swift@@ -42,11 +42,15 @@ public struct GenericHarnessRuntime: Sendable {////// The planner is consulted with the *current* task each iteration, so it sees the world model the/// previous tool produced — that is what makes this a real harness loop rather than a fixed plan.+ /// `onStep`, when provided, is awaited after each executed step with that step's result. It lets a+ /// caller react to realtime loop progress (e.g. drive a cursor-path overlay toward the target the+ /// step just acted on) without the runtime knowing anything about the UI.@discardableResultpublic func run(taskID: String,planner: any HarnessNextStepPlanning,- maxSteps: Int = 16+ maxSteps: Int = 16,+ onStep: (@Sendable (HarnessStepExecutionResult) async -> Void)? = nil) async -> [HarnessStepExecutionResult] {var results: [HarnessStepExecutionResult] = []for _ in 0..<maxSteps {@@ -60,6 +64,7 @@ public struct GenericHarnessRuntime: Sendable {break}results.append(step)+ await onStep?(step)if step.stoppedForGate || !step.task.status.canExecuteTools {break}diff --git a/apps/Donkey/Tests/DonkeyRuntimeTests/RealtimeCursorVisualizationTests.swift b/apps/Donkey/Tests/DonkeyRuntimeTests/RealtimeCursorVisualizationTests.swiftnew file mode 100644index 00000000..2535c49a--- /dev/null+++ b/apps/Donkey/Tests/DonkeyRuntimeTests/RealtimeCursorVisualizationTests.swift@@ -0,0 +1,74 @@+import CoreGraphics+import DonkeyContracts+import DonkeyHarness+import Foundation+import Testing+@testable import Donkey++@Suite+struct RealtimeCursorVisualizationTests {+ private func step(metadata: [String: String], status: HarnessToolResultStatus = .succeeded) -> HarnessStepExecutionResult {+ HarnessStepExecutionResult(+ task: HarnessTaskState(threadID: "thread-1", goal: "do it"),+ toolResult: HarnessToolResult(+ callID: "call-1",+ toolName: "vision.click",+ status: status,+ summary: "Clicked",+ metadata: metadata+ )+ )+ }++ @Test+ func mapsClickScreenPointToNormalizedCursorTarget() throws {+ let plan = try #require(LocalAppUserQueryCommandHandler.cursorVisualizationPlan(+ for: step(metadata: ["screenPoint": "200,150", "label": "Search"]),+ appName: "Notes",+ traceID: "trace-1",+ screenSize: CGSize(width: 1000, height: 500)+ ))++ #expect(plan.executionMode == .live)+ #expect(plan.metadata["realPointerMoved"] == "true")+ #expect(plan.steps.count == 1)+ let target = try #require(plan.steps.first?.target?.point)+ #expect(target.space == .normalizedTarget)+ #expect(abs(target.x - 0.2) < 0.0001)+ #expect(abs(target.y - 0.3) < 0.0001)++ // The plan must yield a renderable cursor overlay request landing at the same point.+ let request = try #require(plan.cursorOverlayRequest())+ let cursor = try #require(request.steps.first)+ #expect(abs(cursor.target.x - 0.2) < 0.0001)+ #expect(abs(cursor.target.y - 0.3) < 0.0001)+ #expect(cursor.label == "Search")+ }++ @Test+ func ignoresStepsWithoutAScreenPoint() {+ // Observation / non-pointer steps carry no screenPoint, so no cursor path is shown.+ #expect(LocalAppUserQueryCommandHandler.cursorVisualizationPlan(+ for: step(metadata: ["label": "observed"]),+ appName: "Notes",+ traceID: "trace-1",+ screenSize: CGSize(width: 1000, height: 500)+ ) == nil)+ }++ @Test+ func ignoresFailedStepsAndDegenerateScreens() {+ #expect(LocalAppUserQueryCommandHandler.cursorVisualizationPlan(+ for: step(metadata: ["screenPoint": "10,10"], status: .failed),+ appName: "Notes",+ traceID: "trace-1",+ screenSize: CGSize(width: 1000, height: 500)+ ) == nil)+ #expect(LocalAppUserQueryCommandHandler.cursorVisualizationPlan(+ for: step(metadata: ["screenPoint": "10,10"]),+ appName: "Notes",+ traceID: "trace-1",+ screenSize: .zero+ ) == nil)+ }+}