mirror of
https://github.com/hanzoai/bot.git
synced 2026-10-30 17:24:26 +00:00
Comprehensive rename of all openclaw references to hanzo-bot equivalents: - Package: openclaw → @hanzo/bot - Binary: openclaw → hanzo-bot - Types: OpenClaw* → Bot* - Env vars: OPENCLAW_* → BOT_* - Android: ai.openclaw.android → ai.hanzo.bot.android - iOS/macOS: ai.openclaw.* → ai.hanzo.* - Swift modules: OpenClaw* → Bot* - Plugins: openclaw.plugin.json → bot.plugin.json - Paths: .openclaw/ → .hanzo/bot/ - URLs: github.com/openclaw → github.com/hanzoai
70 lines
2.3 KiB
Swift
70 lines
2.3 KiB
Swift
import Foundation
|
|
import Network
|
|
import BotKit
|
|
|
|
final class NetworkStatusService: @unchecked Sendable {
|
|
func currentStatus(timeoutMs: Int = 1500) async -> HanzoBotNetworkStatusPayload {
|
|
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) -> HanzoBotNetworkStatusPayload {
|
|
let status: HanzoBotNetworkPathStatus = switch path.status {
|
|
case .satisfied: .satisfied
|
|
case .requiresConnection: .requiresConnection
|
|
case .unsatisfied: .unsatisfied
|
|
@unknown default: .unsatisfied
|
|
}
|
|
|
|
var interfaces: [HanzoBotNetworkInterfaceType] = []
|
|
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 HanzoBotNetworkStatusPayload(
|
|
status: status,
|
|
isExpensive: path.isExpensive,
|
|
isConstrained: path.isConstrained,
|
|
interfaces: interfaces)
|
|
}
|
|
|
|
private static func fallbackPayload() -> HanzoBotNetworkStatusPayload {
|
|
HanzoBotNetworkStatusPayload(
|
|
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
|
|
}
|
|
}
|