File size: 1,413 Bytes
7b2dfc5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import Foundation
enum AssistantThermalLevel: String, Sendable, Codable, Equatable {
case nominal
case fair
case serious
case critical
case unknown
}
struct AssistantResourceSnapshot: Sendable, Equatable {
let thermalLevel: AssistantThermalLevel
let lowPowerModeEnabled: Bool
let availableMemoryBytes: UInt64?
init(
thermalLevel: AssistantThermalLevel,
lowPowerModeEnabled: Bool,
availableMemoryBytes: UInt64? = nil
) {
self.thermalLevel = thermalLevel
self.lowPowerModeEnabled = lowPowerModeEnabled
self.availableMemoryBytes = availableMemoryBytes
}
}
enum AssistantResourceDecision: Sendable, Equatable {
case allow
case warn(String)
case deny(String)
}
struct ResourceBudgetPolicy: Sendable {
static func modelLoadDecision(
for snapshot: AssistantResourceSnapshot
) -> AssistantResourceDecision {
switch snapshot.thermalLevel {
case .critical:
return .deny(
"The iPhone is too hot to load the on-device model safely. Let it cool down and try again."
)
case .serious:
return .warn(
"The iPhone is running hot. Model loading and generation may be slower until it cools down."
)
case .fair, .nominal, .unknown:
if snapshot.lowPowerModeEnabled {
return .warn(
"Low Power Mode is on. On-device generation may be slower."
)
}
return .allow
}
}
}
|