Harden computer use runtime and CLI (#4705)
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
+42
@@ -0,0 +1,42 @@
|
||||
public struct ActionArgumentValidationError: Error, Equatable {
|
||||
public let message: String
|
||||
|
||||
public init(_ message: String) {
|
||||
self.message = message
|
||||
}
|
||||
}
|
||||
|
||||
public enum ActionArgumentValidation {
|
||||
public static func positiveInteger(
|
||||
_ value: Double?,
|
||||
defaultValue: Int,
|
||||
name: String
|
||||
) -> Result<Int, ActionArgumentValidationError> {
|
||||
guard let value else { return .success(defaultValue) }
|
||||
guard value.isFinite, value > 0, let parsed = boundedInteger(value, as: Int.self) else {
|
||||
return .failure(ActionArgumentValidationError("\(name) must be a positive integer"))
|
||||
}
|
||||
return .success(parsed)
|
||||
}
|
||||
|
||||
public static func positiveNumber(
|
||||
_ value: Double?,
|
||||
defaultValue: Double,
|
||||
name: String
|
||||
) -> Result<Double, ActionArgumentValidationError> {
|
||||
guard let value else { return .success(defaultValue) }
|
||||
guard value.isFinite, value > 0 else {
|
||||
return .failure(ActionArgumentValidationError("\(name) must be a positive number"))
|
||||
}
|
||||
return .success(value)
|
||||
}
|
||||
|
||||
public static func scrollDirection(_ value: String) -> Result<String, ActionArgumentValidationError> {
|
||||
switch value {
|
||||
case "up", "down", "left", "right":
|
||||
return .success(value)
|
||||
default:
|
||||
return .failure(ActionArgumentValidationError("unsupported scroll direction: \(value)"))
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
import Foundation
|
||||
|
||||
public enum ComputerSnapshotCachePolicy {
|
||||
public static let maxEntries = 32
|
||||
public static let maxAge: TimeInterval = 2 * 60
|
||||
|
||||
public static func isExpired(createdAt: Date, now: Date = Date()) -> Bool {
|
||||
now.timeIntervalSince(createdAt) > maxAge
|
||||
}
|
||||
|
||||
public static func shouldPrune(entryCount: Int, createdAt: Date, now: Date = Date()) -> Bool {
|
||||
entryCount > maxEntries || isExpired(createdAt: createdAt, now: now)
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,16 @@ public struct SnapshotRenderNode: Equatable {
|
||||
}
|
||||
}
|
||||
|
||||
public struct SnapshotTabStripCompaction: Equatable {
|
||||
public var retainedIndexes: Set<Int>
|
||||
public var omittedCount: Int
|
||||
|
||||
public init(retainedIndexes: Set<Int>, omittedCount: Int) {
|
||||
self.retainedIndexes = retainedIndexes
|
||||
self.omittedCount = omittedCount
|
||||
}
|
||||
}
|
||||
|
||||
public enum SnapshotRenderHeuristics {
|
||||
public static func displayName(_ node: SnapshotRenderNode) -> String? {
|
||||
if let title = clean(node.title) {
|
||||
@@ -124,6 +134,24 @@ public enum SnapshotRenderHeuristics {
|
||||
shouldSuppressChildren(SnapshotRenderNode(role: role, title: name))
|
||||
}
|
||||
|
||||
public static func tabStripCompaction(
|
||||
parent: SnapshotRenderNode,
|
||||
children: [SnapshotRenderNode]
|
||||
) -> SnapshotTabStripCompaction? {
|
||||
guard isBrowserTabStripContainer(parent) else { return nil }
|
||||
let tabIndexes = Set(children.indices.filter { isBrowserTabLike(children[$0]) })
|
||||
guard tabIndexes.count >= 10 else { return nil }
|
||||
|
||||
let selectedTabIndexes = Set(tabIndexes.filter { isSelectedBrowserTab(children[$0]) })
|
||||
guard !selectedTabIndexes.isEmpty else { return nil }
|
||||
|
||||
let nonTabIndexes = Set(children.indices.filter { !tabIndexes.contains($0) })
|
||||
let retainedIndexes = nonTabIndexes.union(selectedTabIndexes)
|
||||
let omittedCount = tabIndexes.count - selectedTabIndexes.count
|
||||
guard omittedCount > 0 else { return nil }
|
||||
return SnapshotTabStripCompaction(retainedIndexes: retainedIndexes, omittedCount: omittedCount)
|
||||
}
|
||||
|
||||
public static func line(index: Int, node: SnapshotRenderNode) -> String {
|
||||
let name = displayName(node)
|
||||
let roleText = roleText(node)
|
||||
@@ -210,6 +238,26 @@ public enum SnapshotRenderHeuristics {
|
||||
.replacingOccurrences(of: "]", with: "\\]")
|
||||
}
|
||||
|
||||
private static func isBrowserTabStripContainer(_ node: SnapshotRenderNode) -> Bool {
|
||||
let roleText = roleText(node)
|
||||
let title = clean(node.title)?.lowercased() ?? ""
|
||||
let label = clean(node.label)?.lowercased() ?? ""
|
||||
let description = clean(node.roleDescription)?.lowercased() ?? ""
|
||||
return roleText == "scroll area" ||
|
||||
description == "tab bar" ||
|
||||
title == "tab bar" ||
|
||||
label == "tab bar"
|
||||
}
|
||||
|
||||
private static func isBrowserTabLike(_ node: SnapshotRenderNode) -> Bool {
|
||||
let roleDescription = clean(node.roleDescription)?.lowercased() ?? ""
|
||||
return node.role == "AXTab" || roleDescription == "tab"
|
||||
}
|
||||
|
||||
private static func isSelectedBrowserTab(_ node: SnapshotRenderNode) -> Bool {
|
||||
node.traits.contains("selected") || clean(node.value) == "1"
|
||||
}
|
||||
|
||||
private static func splitCamelCase(_ value: String) -> String {
|
||||
var result = ""
|
||||
for character in value {
|
||||
|
||||
Reference in New Issue
Block a user