Skip to content

Open source CapCut alternative.

TypeScript

Harden hosted-vision parsing + add keyboard input primitive

David Led480c0e5182f
diff --git a/apps/Donkey/Sources/DonkeyAI/DebugUIInspectionHostedAdapter.swift b/apps/Donkey/Sources/DonkeyAI/DebugUIInspectionHostedAdapter.swift
index 815c4e2d..e6ec40fa 100644
--- a/apps/Donkey/Sources/DonkeyAI/DebugUIInspectionHostedAdapter.swift
+++ b/apps/Donkey/Sources/DonkeyAI/DebugUIInspectionHostedAdapter.swift
@@ -269,7 +269,9 @@ public enum DebugUIInspectionResponseDecoder {
else {
throw DebugUIInspectionHostedAdapterError.missingOutputText
}
- frameData = Data(outputText.utf8)
+ // Vision models occasionally wrap the JSON in markdown fences or add trailing prose;
+ // extract the JSON object so a stray character doesn't fail the whole inspection.
+ frameData = Data(jsonObjectSubstring(outputText).utf8)
}
do {
@@ -281,6 +283,22 @@ public enum DebugUIInspectionResponseDecoder {
}
}
+ /// Extracts the JSON object from model output that may be fenced or have trailing prose.
+ static func jsonObjectSubstring(_ text: String) -> String {
+ var trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
+ if trimmed.hasPrefix("```") {
+ trimmed = trimmed
+ .replacingOccurrences(of: "```json", with: "")
+ .replacingOccurrences(of: "```JSON", with: "")
+ .replacingOccurrences(of: "```", with: "")
+ .trimmingCharacters(in: .whitespacesAndNewlines)
+ }
+ if let start = trimmed.firstIndex(of: "{"), let end = trimmed.lastIndex(of: "}"), start <= end {
+ return String(trimmed[start...end])
+ }
+ return trimmed
+ }
+
public static func containsActionOutput(_ value: RemoteInferenceJSONValue) -> Bool {
switch value {
case .string(let text):
diff --git a/apps/Donkey/Sources/DonkeyAI/VisionGroundingFlow.swift b/apps/Donkey/Sources/DonkeyAI/VisionGroundingFlow.swift
index 1cb6158a..2219ac5c 100644
--- a/apps/Donkey/Sources/DonkeyAI/VisionGroundingFlow.swift
+++ b/apps/Donkey/Sources/DonkeyAI/VisionGroundingFlow.swift
@@ -52,17 +52,24 @@ public enum VisionGroundingFlow {
return Outcome(screenPoint: nil, reason: "emptyScreenshot", resolvedAppName: target.appName)
}
- let frame: DebugUIInspectionFrame
- do {
- frame = try await analyzer.inspect(
- DebugUIInspectionRequest(
- screenshotBase64: shot.pngData.base64EncodedString(),
- pixelSize: HotLoopSize(width: Double(shot.imageWidth), height: Double(shot.imageHeight), space: .crop),
- minConfidence: minConfidence
- )
- )
- } catch {
- return Outcome(screenPoint: nil, reason: "visionInspectionFailed", resolvedAppName: target.appName)
+ // The hosted vision model occasionally returns malformed JSON; retry a few times.
+ let request = DebugUIInspectionRequest(
+ screenshotBase64: shot.pngData.base64EncodedString(),
+ pixelSize: HotLoopSize(width: Double(shot.imageWidth), height: Double(shot.imageHeight), space: .crop),
+ minConfidence: minConfidence
+ )
+ var frame: DebugUIInspectionFrame?
+ var lastError: Error?
+ for _ in 0..<3 {
+ do {
+ frame = try await analyzer.inspect(request)
+ break
+ } catch {
+ lastError = error
+ }
+ }
+ guard let frame else {
+ return Outcome(screenPoint: nil, reason: "visionInspectionFailed: \(lastError.map { "\($0)" } ?? "unknown")", resolvedAppName: target.appName)
}
guard let element = resolveElement(targetQuery, in: frame.elements) else {
diff --git a/apps/Donkey/Sources/DonkeyRuntime/AccessibilityActionExecutor.swift b/apps/Donkey/Sources/DonkeyRuntime/AccessibilityActionExecutor.swift
index 137f50ed..d60637f5 100644
--- a/apps/Donkey/Sources/DonkeyRuntime/AccessibilityActionExecutor.swift
+++ b/apps/Donkey/Sources/DonkeyRuntime/AccessibilityActionExecutor.swift
@@ -2,6 +2,34 @@ import CoreGraphics
import DonkeyContracts
import Foundation
+/// Low-level keyboard input via CGEvent. Real input — gate behind explicit permission/guards.
+public enum MacKeyboardInput {
+ /// Types arbitrary text by posting per-character unicode key events.
+ public static func type(_ text: String) {
+ guard let source = CGEventSource(stateID: .hidSystemState) else { return }
+ for scalar in text.unicodeScalars where scalar.value <= 0xFFFF {
+ var unit = UniChar(scalar.value)
+ guard let down = CGEvent(keyboardEventSource: source, virtualKey: 0, keyDown: true),
+ let up = CGEvent(keyboardEventSource: source, virtualKey: 0, keyDown: false)
+ else { continue }
+ down.keyboardSetUnicodeString(stringLength: 1, unicodeString: &unit)
+ up.keyboardSetUnicodeString(stringLength: 1, unicodeString: &unit)
+ down.post(tap: .cghidEventTap)
+ up.post(tap: .cghidEventTap)
+ }
+ }
+
+ /// Posts a Return keypress (virtual key 36).
+ public static func pressReturn() {
+ guard let source = CGEventSource(stateID: .hidSystemState),
+ let down = CGEvent(keyboardEventSource: source, virtualKey: 36, keyDown: true),
+ let up = CGEvent(keyboardEventSource: source, virtualKey: 36, keyDown: false)
+ else { return }
+ down.post(tap: .cghidEventTap)
+ up.post(tap: .cghidEventTap)
+ }
+}
+
/// Low-level pointer input via CGEvent (move + left click at a screen point). Real input — callers
/// must gate this behind explicit permission/guards.
public enum MacPointerInput {