Skip to content

Open source CapCut alternative.

TypeScript

Extract shared screenshot compression for hosted vision requests

David Leaf9fe0ac688c
diff --git a/apps/Donkey/Sources/Donkey/LocalUIElementDetection/DebugUIInspectionCoordinator.swift b/apps/Donkey/Sources/Donkey/LocalUIElementDetection/DebugUIInspectionCoordinator.swift
index 5479361c..8d865926 100644
--- a/apps/Donkey/Sources/Donkey/LocalUIElementDetection/DebugUIInspectionCoordinator.swift
+++ b/apps/Donkey/Sources/Donkey/LocalUIElementDetection/DebugUIInspectionCoordinator.swift
@@ -1304,63 +1304,8 @@ final class DebugUIInspectionCoordinator {
private static func compressedRemoteAIImage(
from screenshot: CapturedWindowScreenshot
- ) -> DebugUICompressedImage {
- let fallbackSize = HotLoopSize(
- width: Double(screenshot.imageWidth),
- height: Double(screenshot.imageHeight),
- space: .window
- )
- guard let source = CGImageSourceCreateWithData(screenshot.pngData as CFData, nil) else {
- return DebugUICompressedImage(
- data: screenshot.pngData,
- contentType: "image/png",
- pixelSize: fallbackSize
- )
- }
-
- let maxPixelDimension = 896
- let thumbnailOptions: [CFString: Any] = [
- kCGImageSourceCreateThumbnailFromImageAlways: true,
- kCGImageSourceShouldCacheImmediately: true,
- kCGImageSourceCreateThumbnailWithTransform: true,
- kCGImageSourceThumbnailMaxPixelSize: maxPixelDimension
- ]
- guard let image = CGImageSourceCreateThumbnailAtIndex(source, 0, thumbnailOptions as CFDictionary),
- let encoded = NSMutableData() as CFMutableData?,
- let destination = CGImageDestinationCreateWithData(
- encoded,
- UTType.jpeg.identifier as CFString,
- 1,
- nil
- )
- else {
- return DebugUICompressedImage(
- data: screenshot.pngData,
- contentType: "image/png",
- pixelSize: fallbackSize
- )
- }
-
- CGImageDestinationAddImage(destination, image, [
- kCGImageDestinationLossyCompressionQuality: 0.48
- ] as CFDictionary)
- guard CGImageDestinationFinalize(destination) else {
- return DebugUICompressedImage(
- data: screenshot.pngData,
- contentType: "image/png",
- pixelSize: fallbackSize
- )
- }
-
- return DebugUICompressedImage(
- data: encoded as Data,
- contentType: "image/jpeg",
- pixelSize: HotLoopSize(
- width: Double(image.width),
- height: Double(image.height),
- space: .window
- )
- )
+ ) -> CompressedScreenshot {
+ ScreenshotCompression.compressedForModel(screenshot)
}
}
@@ -1378,10 +1323,4 @@ private struct DebugUIWindowCapture: Sendable {
var parsePixelSize: HotLoopSize
}
-private struct DebugUICompressedImage: Sendable {
- var data: Data
- var contentType: String
- var pixelSize: HotLoopSize
-}
-
#endif
diff --git a/apps/Donkey/Sources/DonkeyRuntime/ScreenshotCompression.swift b/apps/Donkey/Sources/DonkeyRuntime/ScreenshotCompression.swift
new file mode 100644
index 00000000..2929c24b
--- /dev/null
+++ b/apps/Donkey/Sources/DonkeyRuntime/ScreenshotCompression.swift
@@ -0,0 +1,66 @@
+import CoreGraphics
+import DonkeyContracts
+import Foundation
+import ImageIO
+import UniformTypeIdentifiers
+
+/// A screenshot compressed for sending to a hosted model: downscaled + JPEG-encoded, with the
+/// resulting pixel size (model coordinates are in this space and must be scaled back by callers).
+public struct CompressedScreenshot: Sendable {
+ public var data: Data
+ public var contentType: String
+ public var pixelSize: HotLoopSize
+
+ public init(data: Data, contentType: String, pixelSize: HotLoopSize) {
+ self.data = data
+ self.contentType = contentType
+ self.pixelSize = pixelSize
+ }
+
+ public var base64DataURL: String {
+ "data:\(contentType);base64,\(data.base64EncodedString())"
+ }
+}
+
+/// Shared screenshot compression for hosted vision requests — downscale to a max dimension and
+/// JPEG-compress so payloads stay small. Used by debug UI inspection and the vision action planner
+/// so every screenshot we send the model is prepared the same way.
+public enum ScreenshotCompression {
+ public static func compressedForModel(
+ _ screenshot: CapturedWindowScreenshot,
+ maxPixelDimension: Int = 896,
+ jpegQuality: Double = 0.48
+ ) -> CompressedScreenshot {
+ let fallback = CompressedScreenshot(
+ data: screenshot.pngData,
+ contentType: "image/png",
+ pixelSize: HotLoopSize(width: Double(screenshot.imageWidth), height: Double(screenshot.imageHeight), space: .window)
+ )
+ guard let source = CGImageSourceCreateWithData(screenshot.pngData as CFData, nil) else {
+ return fallback
+ }
+ let thumbnailOptions: [CFString: Any] = [
+ kCGImageSourceCreateThumbnailFromImageAlways: true,
+ kCGImageSourceShouldCacheImmediately: true,
+ kCGImageSourceCreateThumbnailWithTransform: true,
+ kCGImageSourceThumbnailMaxPixelSize: maxPixelDimension
+ ]
+ guard let image = CGImageSourceCreateThumbnailAtIndex(source, 0, thumbnailOptions as CFDictionary),
+ let encoded = NSMutableData() as CFMutableData?,
+ let destination = CGImageDestinationCreateWithData(encoded, UTType.jpeg.identifier as CFString, 1, nil)
+ else {
+ return fallback
+ }
+ CGImageDestinationAddImage(destination, image, [
+ kCGImageDestinationLossyCompressionQuality: jpegQuality
+ ] as CFDictionary)
+ guard CGImageDestinationFinalize(destination) else {
+ return fallback
+ }
+ return CompressedScreenshot(
+ data: encoded as Data,
+ contentType: "image/jpeg",
+ pixelSize: HotLoopSize(width: Double(image.width), height: Double(image.height), space: .window)
+ )
+ }
+}