mirror of
https://github.com/hanzoai/bot.git
synced 2026-10-30 17:24:26 +00:00
Automated rebrand of all openclaw references to hanzo bot: - Package: openclaw -> @hanzo/bot - Binary: openclaw -> hanzo-bot - Config: ~/.openclaw -> ~/.hanzo/bot - Docker: ghcr.io/hanzoai/bot - Env vars: OPENCLAW_* -> BOT_* - Bundle IDs: ai.hanzo.bot.* - Product name: Hanzo Bot
70 lines
2.4 KiB
Swift
70 lines
2.4 KiB
Swift
import Foundation
|
|
import Network
|
|
import Hanzo BotKit
|
|
|
|
final class NetworkStatusService: @unchecked Sendable {
|
|
func currentStatus(timeoutMs: Int = 1500) async -> Hanzo BotNetworkStatusPayload {
|
|
await withCheckedContinuation { cont in
|
|
let monitor = NWPathMonitor()
|
|
let queue = DispatchQueue(label: "ai.hanzo.bot.ios.network-status")
|
|
let state = NetworkStatusState()
|
|
|
|
monitor.pathUpdateHandler = { path in
|
|
guard state.markCompleted() else { return }
|
|
monitor.cancel()
|
|
cont.resume(returning: Self.payload(from: path))
|
|
}
|
|
|
|
monitor.start(queue: queue)
|
|
|
|
queue.asyncAfter(deadline: .now() + .milliseconds(timeoutMs)) {
|
|
guard state.markCompleted() else { return }
|
|
monitor.cancel()
|
|
cont.resume(returning: Self.fallbackPayload())
|
|
}
|
|
}
|
|
}
|
|
|
|
private static func payload(from path: NWPath) -> Hanzo BotNetworkStatusPayload {
|
|
let status: Hanzo BotNetworkPathStatus = switch path.status {
|
|
case .satisfied: .satisfied
|
|
case .requiresConnection: .requiresConnection
|
|
case .unsatisfied: .unsatisfied
|
|
@unknown default: .unsatisfied
|
|
}
|
|
|
|
var interfaces: [Hanzo BotNetworkInterfaceType] = []
|
|
if path.usesInterfaceType(.wifi) { interfaces.append(.wifi) }
|
|
if path.usesInterfaceType(.cellular) { interfaces.append(.cellular) }
|
|
if path.usesInterfaceType(.wiredEthernet) { interfaces.append(.wired) }
|
|
if interfaces.isEmpty { interfaces.append(.other) }
|
|
|
|
return Hanzo BotNetworkStatusPayload(
|
|
status: status,
|
|
isExpensive: path.isExpensive,
|
|
isConstrained: path.isConstrained,
|
|
interfaces: interfaces)
|
|
}
|
|
|
|
private static func fallbackPayload() -> Hanzo BotNetworkStatusPayload {
|
|
Hanzo BotNetworkStatusPayload(
|
|
status: .unsatisfied,
|
|
isExpensive: false,
|
|
isConstrained: false,
|
|
interfaces: [.other])
|
|
}
|
|
}
|
|
|
|
private final class NetworkStatusState: @unchecked Sendable {
|
|
private let lock = NSLock()
|
|
private var completed = false
|
|
|
|
func markCompleted() -> Bool {
|
|
self.lock.lock()
|
|
defer { self.lock.unlock() }
|
|
if self.completed { return false }
|
|
self.completed = true
|
|
return true
|
|
}
|
|
}
|